7003964: SERV: securely load DLLs and launch executables using fully qualified path
Summary: Linked in Windows libraries that are available on jdk7 supported platforms, and used GetModuleHandle instead of LoadLibrary for already loaded Dlls.
Reviewed-by: dcubed, alanb
--- a/jdk/make/com/sun/tools/attach/Makefile Tue Apr 12 14:23:03 2011 -0700
+++ b/jdk/make/com/sun/tools/attach/Makefile Fri Apr 15 09:53:05 2011 -0400
@@ -48,6 +48,9 @@
ifeq ($(PLATFORM), solaris)
OTHER_LDLIBS += -ldoor
endif
+ifeq ($(PLATFORM), windows)
+EXTRA_LIBS += psapi.lib
+endif
vpath %.c $(PLATFORM_SRC)/native/sun/tools/attach
--- a/jdk/src/windows/classes/sun/tools/attach/WindowsAttachProvider.java Tue Apr 12 14:23:03 2011 -0700
+++ b/jdk/src/windows/classes/sun/tools/attach/WindowsAttachProvider.java Fri Apr 15 09:53:05 2011 -0400
@@ -126,16 +126,6 @@
* of the process list.
*/
private List<VirtualMachineDescriptor> listJavaProcesses() {
- // ensure that process status helper is loaded (psapi.dll)
- if (!isProcessStatusHelperInitialized) {
- synchronized (WindowsAttachProvider.class) {
- if (!isProcessStatusHelperInitialized) {
- initializeProcessStatusHelper();
- isProcessStatusHelperInitialized = true;
- }
- }
- }
-
ArrayList<VirtualMachineDescriptor> list =
new ArrayList<VirtualMachineDescriptor>();
@@ -172,12 +162,6 @@
return list;
}
- // indicates if psapi.dll has been initialized
- private static volatile boolean isProcessStatusHelperInitialized;
-
- // loads psapi
- private static native void initializeProcessStatusHelper();
-
// enumerates processes using psapi's EnumProcesses
private static native int enumProcesses(int[] processes, int max);
--- a/jdk/src/windows/native/sun/tools/attach/WindowsAttachProvider.c Tue Apr 12 14:23:03 2011 -0700
+++ b/jdk/src/windows/native/sun/tools/attach/WindowsAttachProvider.c Fri Apr 15 09:53:05 2011 -0400
@@ -25,6 +25,7 @@
#include <windows.h>
#include <stdlib.h>
#include <string.h>
+#include <Psapi.h>
#include "jni.h"
#include "jni_util.h"
@@ -97,41 +98,6 @@
/*
- * Process status helper library functions
- */
-static BOOL (WINAPI *_EnumProcesses) (DWORD *, DWORD, DWORD *);
-static BOOL (WINAPI *_EnumProcessModules)(HANDLE, HMODULE *, DWORD, LPDWORD);
-static DWORD (WINAPI *_GetModuleBaseName) (HANDLE, HMODULE, LPTSTR, DWORD);
-
-
-/*
- * Class: sun_tools_attach_WindowsAttachProvider
- * Method: initializeProcessStatusHelper
- * Signature: ()V
- */
-JNIEXPORT void JNICALL
-Java_sun_tools_attach_WindowsAttachProvider_initializeProcessStatusHelper(JNIEnv *env, jclass cls)
-{
- HINSTANCE psapi = LoadLibrary("PSAPI.DLL") ;
- if (psapi != NULL) {
- _EnumProcesses = (BOOL(WINAPI *)(DWORD *, DWORD, DWORD *))
- GetProcAddress(psapi, "EnumProcesses") ;
- _EnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD))
- GetProcAddress(psapi, "EnumProcessModules");
- _GetModuleBaseName = (DWORD(WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD))
- GetProcAddress(psapi, "GetModuleBaseNameA");
- }
-
- if ((_EnumProcesses == NULL) ||
- (_EnumProcessModules == NULL) ||
- (_GetModuleBaseName == NULL))
- {
- JNU_ThrowInternalError(env, "Unable to initialize process status helper library");
- }
-}
-
-
-/*
* Class: sun_tools_attach_WindowsAttachProvider
* Method: enumProcesses
* Signature: ([JI)I
@@ -147,7 +113,7 @@
size = max * sizeof(DWORD);
ptr = (DWORD*)malloc(size);
if (ptr != NULL) {
- BOOL res = (*_EnumProcesses)(ptr, size, &bytesReturned);
+ BOOL res = EnumProcesses(ptr, size, &bytesReturned);
if (res != 0) {
result = (jint)(bytesReturned / sizeof(DWORD));
(*env)->SetIntArrayRegion(env, arr, 0, (jsize)result, (jint*)ptr);
@@ -192,13 +158,13 @@
size = 1024 * sizeof(HMODULE);
ptr = (HMODULE*)malloc(size);
if (ptr != NULL) {
- BOOL res = (*_EnumProcessModules)(hProcess, ptr, size, &bytesReturned);
+ BOOL res = EnumProcessModules(hProcess, ptr, size, &bytesReturned);
if (res != 0) {
int count = bytesReturned / sizeof(HMODULE);
int i = 0;
while (i < count) {
char base[256];
- BOOL res = (*_GetModuleBaseName)(hProcess, ptr[i], base, sizeof(base));
+ BOOL res = GetModuleBaseName(hProcess, ptr[i], base, sizeof(base));
if (res != 0) {
if (strcmp(base, lib) == 0) {
result = JNI_TRUE;
--- a/jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c Tue Apr 12 14:23:03 2011 -0700
+++ b/jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c Fri Apr 15 09:53:05 2011 -0400
@@ -32,13 +32,13 @@
/* kernel32 */
-typedef HINSTANCE (WINAPI* LoadLibraryFunc) (LPCTSTR);
+typedef HINSTANCE (WINAPI* GetModuleHandleFunc) (LPCTSTR);
typedef FARPROC (WINAPI* GetProcAddressFunc)(HMODULE, LPCSTR);
/* only on Windows 64-bit or 32-bit application running under WOW64 */
typedef BOOL (WINAPI *IsWow64ProcessFunc) (HANDLE, PBOOL);
-static LoadLibraryFunc _LoadLibrary;
+static GetModuleHandleFunc _GetModuleHandle;
static GetProcAddressFunc _GetProcAddress;
static IsWow64ProcessFunc _IsWow64Process;
@@ -70,7 +70,7 @@
#define MAX_PIPE_NAME_LENGTH 256
typedef struct {
- LoadLibraryFunc _LoadLibrary;
+ GetModuleHandleFunc _GetModuleHandle;
GetProcAddressFunc _GetProcAddress;
char jvmLib[MAX_LIBNAME_LENGTH]; /* "jvm.dll" */
char func1[MAX_FUNC_LENGTH];
@@ -96,7 +96,7 @@
HINSTANCE h;
EnqueueOperationFunc addr;
- h = pData->_LoadLibrary(pData->jvmLib);
+ h = pData->_GetModuleHandle(pData->jvmLib);
if (h == NULL) {
return ERR_OPEN_JVM_FAIL;
}
@@ -131,15 +131,10 @@
JNIEXPORT void JNICALL Java_sun_tools_attach_WindowsVirtualMachine_init
(JNIEnv *env, jclass cls)
{
- HINSTANCE h = LoadLibrary("kernel32");
- if (h != NULL) {
- _LoadLibrary = (LoadLibraryFunc) GetProcAddress(h, "LoadLibraryA");
- _GetProcAddress = (GetProcAddressFunc)GetProcAddress(h, "GetProcAddress");
- _IsWow64Process = (IsWow64ProcessFunc)GetProcAddress(h, "IsWow64Process");
- }
- if (_LoadLibrary == NULL || _GetProcAddress == NULL) {
- JNU_ThrowInternalError(env, "Unable to get address of LoadLibraryA or GetProcAddress");
- }
+ // All following APIs exist on Windows XP with SP2/Windows Server 2008
+ _GetModuleHandle = (GetModuleHandleFunc)GetModuleHandle;
+ _GetProcAddress = (GetProcAddressFunc)GetProcAddress;
+ _IsWow64Process = (IsWow64ProcessFunc)IsWow64Process;
}
@@ -375,7 +370,7 @@
/*
* Setup data to copy to target process
*/
- data._LoadLibrary = _LoadLibrary;
+ data._GetModuleHandle = _GetModuleHandle;
data._GetProcAddress = _GetProcAddress;
strcpy(data.jvmLib, "jvm");
--- a/jdk/src/windows/native/sun/tracing/dtrace/jvm_symbols_md.c Tue Apr 12 14:23:03 2011 -0700
+++ b/jdk/src/windows/native/sun/tracing/dtrace/jvm_symbols_md.c Fri Apr 15 09:53:05 2011 -0400
@@ -35,7 +35,7 @@
JvmSymbols* lookupJvmSymbols() {
JvmSymbols* syms = (JvmSymbols*)malloc(sizeof(JvmSymbols));
if (syms != NULL) {
- HINSTANCE jvm = LoadLibrary("jvm.dll");
+ HINSTANCE jvm = GetModuleHandle("jvm.dll");
if (jvm == NULL) {
free(syms);
return NULL;
--- a/jdk/src/windows/npt/npt_md.h Tue Apr 12 14:23:03 2011 -0700
+++ b/jdk/src/windows/npt/npt_md.h Fri Apr 15 09:53:05 2011 -0400
@@ -47,7 +47,7 @@
_handle = NULL; \
*(pnpt) = NULL; \
buf[0] = 0; \
- jvm = LoadLibrary("jvm.dll"); \
+ jvm = GetModuleHandle("jvm.dll"); \
if ( jvm == NULL ) NPT_ERROR("Cannot find jvm.dll"); \
GetModuleFileName(jvm, buf, FILENAME_MAX); \
lastSlash = strrchr(buf, '\\'); \