提交 35ce0332 编写于 作者: Z zgu

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
上级 46e6bf2e
......@@ -48,6 +48,9 @@ include Exportedfiles.gmk
ifeq ($(PLATFORM), solaris)
OTHER_LDLIBS += -ldoor
endif
ifeq ($(PLATFORM), windows)
EXTRA_LIBS += psapi.lib
endif
vpath %.c $(PLATFORM_SRC)/native/sun/tools/attach
......
......@@ -126,16 +126,6 @@ public class WindowsAttachProvider extends HotSpotAttachProvider {
* 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 @@ public class WindowsAttachProvider extends HotSpotAttachProvider {
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);
......
......@@ -25,6 +25,7 @@
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <Psapi.h>
#include "jni.h"
#include "jni_util.h"
......@@ -96,41 +97,6 @@ Java_sun_tools_attach_WindowsAttachProvider_volumeFlags(JNIEnv *env, jclass cls,
}
/*
* 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
......@@ -147,7 +113,7 @@ Java_sun_tools_attach_WindowsAttachProvider_enumProcesses(JNIEnv *env, jclass cl
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 @@ Java_sun_tools_attach_WindowsAttachProvider_isLibraryLoadedByProcess(JNIEnv *env
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;
......
......@@ -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 @@ static void jstring_to_cstring(JNIEnv* env, jstring jstr, char* cstr, int len);
#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 @@ static DWORD WINAPI thread_func(DataBlock *pData)
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 @@ static void thread_end (void) {
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 @@ JNIEXPORT void JNICALL Java_sun_tools_attach_WindowsVirtualMachine_enqueue
/*
* Setup data to copy to target process
*/
data._LoadLibrary = _LoadLibrary;
data._GetModuleHandle = _GetModuleHandle;
data._GetProcAddress = _GetProcAddress;
strcpy(data.jvmLib, "jvm");
......
......@@ -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;
......
......@@ -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, '\\'); \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册