Skip to content

Commit e21ca62

Browse files
authored
Use LoadLibraryEx instead of LoadLibrary (#24)
1 parent 0a2e54e commit e21ca62

File tree

8 files changed

+119
-21
lines changed

8 files changed

+119
-21
lines changed

build/linux/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ ARCH_FLAGS= -m32
88
CFLAGS= $(ARCH_FLAGS) -Wall -Wextra -Werror -O2 -I$(SRC_DIR)
99
LIBNAME=pkcs11-logger-x86.so
1010

11-
all: init.o lock.o log.o pkcs11-logger.o translate.o utils.o
11+
all: dl.o init.o lock.o log.o pkcs11-logger.o translate.o utils.o
1212
$(CC) $(ARCH_FLAGS) -shared -o $(LIBNAME) \
1313
-Wl,-soname,$(LIBNAME) \
1414
-Wl,--version-script,pkcs11-logger.version \
15-
init.o lock.o log.o pkcs11-logger.o translate.o utils.o \
15+
dl.o init.o lock.o log.o pkcs11-logger.o translate.o utils.o \
1616
-lc -ldl -lpthread
1717
strip --strip-all $(LIBNAME)
1818

19+
dl.o: $(SRC_DIR)/dl.c $(SRC_DIR)/*.h
20+
$(CC) $(CFLAGS) -fPIC -c $(SRC_DIR)/dl.c
21+
1922
init.o: $(SRC_DIR)/init.c $(SRC_DIR)/*.h
2023
$(CC) $(CFLAGS) -fPIC -c $(SRC_DIR)/init.c
2124

build/macos/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ ARCH_FLAGS= -target arm64-apple-macos11
88
CFLAGS= $(ARCH_FLAGS) -Wall -Wextra -Werror -O2 -I$(SRC_DIR)
99
LIBNAME=pkcs11-logger-arm64.dylib
1010

11-
all: init.o lock.o log.o pkcs11-logger.o translate.o utils.o
11+
all: dl.o init.o lock.o log.o pkcs11-logger.o translate.o utils.o
1212
$(CC) $(ARCH_FLAGS) -dynamiclib -o $(LIBNAME) \
1313
-Wl,-exported_symbols_list,pkcs11-logger.symbols \
14-
init.o lock.o log.o pkcs11-logger.o translate.o utils.o \
14+
dl.o init.o lock.o log.o pkcs11-logger.o translate.o utils.o \
1515
-lc -ldl -lpthread
1616
strip -x $(LIBNAME)
1717

18+
dl.o: $(SRC_DIR)/dl.c $(SRC_DIR)/*.h
19+
$(CC) $(CFLAGS) -fPIC -c $(SRC_DIR)/dl.c
20+
1821
init.o: $(SRC_DIR)/init.c $(SRC_DIR)/*.h
1922
$(CC) $(CFLAGS) -fPIC -c $(SRC_DIR)/init.c
2023

build/windows/pkcs11-logger/pkcs11-logger.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22+
<ClCompile Include="..\..\..\src\dl.c" />
2223
<ClCompile Include="..\..\..\src\init.c" />
2324
<ClCompile Include="..\..\..\src\lock.c" />
2425
<ClCompile Include="..\..\..\src\log.c" />

build/windows/pkcs11-logger/pkcs11-logger.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<ClCompile Include="..\..\..\src\utils.c">
3434
<Filter>Source Files</Filter>
3535
</ClCompile>
36+
<ClCompile Include="..\..\..\src\dl.c">
37+
<Filter>Source Files</Filter>
38+
</ClCompile>
3639
</ItemGroup>
3740
<ItemGroup>
3841
<ClInclude Include="..\..\..\src\pkcs11-logger.h">

src/dl.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2011-2024 The Pkcs11Interop Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Written for the Pkcs11Interop project by:
19+
* Jaroslav IMRICH <jimrich@jimrich.sk>
20+
*/
21+
22+
23+
#include "pkcs11-logger.h"
24+
25+
26+
// Platform dependend function that loads dynamic library
27+
DLHANDLE pkcs11_logger_dl_open(const char* library)
28+
{
29+
#ifdef _WIN32
30+
DWORD flags = 0;
31+
32+
if (CK_TRUE == pkcs11_logger_utils_path_is_absolute(library))
33+
flags = LOAD_WITH_ALTERED_SEARCH_PATH;
34+
35+
return LoadLibraryExA(library, NULL, flags);
36+
#else
37+
return dlopen(library, RTLD_NOW | RTLD_LOCAL);
38+
#endif
39+
}
40+
41+
42+
// Platform dependend function that gets function pointer from dynamic library
43+
void *pkcs11_logger_dl_sym(DLHANDLE library, const char *function)
44+
{
45+
#ifdef _WIN32
46+
return (void*) GetProcAddress(library, function);
47+
#else
48+
return dlsym(library, function);
49+
#endif
50+
}
51+
52+
53+
// Platform dependend function that unloads dynamic library
54+
int pkcs11_logger_dl_close(DLHANDLE library)
55+
{
56+
#ifdef _WIN32
57+
return FreeLibrary(library);
58+
#else
59+
return dlclose(library);
60+
#endif
61+
}

src/init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ int pkcs11_logger_init_orig_lib(void)
105105
pkcs11_logger_log_separator();
106106

107107
// Load PKCS#11 library
108-
pkcs11_logger_globals.orig_lib_handle = DLOPEN((const char *)pkcs11_logger_globals.env_var_library_path);
108+
pkcs11_logger_globals.orig_lib_handle = pkcs11_logger_dl_open((const char *)pkcs11_logger_globals.env_var_library_path);
109109
if (NULL == pkcs11_logger_globals.orig_lib_handle)
110110
{
111111
pkcs11_logger_log("Unable to load %s", pkcs11_logger_globals.env_var_library_path);
112112
return PKCS11_LOGGER_RV_ERROR;
113113
}
114114

115115
// Get pointer to C_GetFunctionList()
116-
GetFunctionListPointer = (CK_C_GetFunctionList) DLSYM(pkcs11_logger_globals.orig_lib_handle, "C_GetFunctionList");
116+
GetFunctionListPointer = (CK_C_GetFunctionList) pkcs11_logger_dl_sym(pkcs11_logger_globals.orig_lib_handle, "C_GetFunctionList");
117117
if (NULL == GetFunctionListPointer)
118118
{
119119
pkcs11_logger_log("Unable to find C_GetFunctionList() in %s", pkcs11_logger_globals.env_var_library_path);
120-
CALL_N_CLEAR(DLCLOSE, pkcs11_logger_globals.orig_lib_handle);
120+
CALL_N_CLEAR(pkcs11_logger_dl_close, pkcs11_logger_globals.orig_lib_handle);
121121
return PKCS11_LOGGER_RV_ERROR;
122122
}
123123

@@ -126,7 +126,7 @@ int pkcs11_logger_init_orig_lib(void)
126126
if (CKR_OK != rv)
127127
{
128128
pkcs11_logger_log("Unable to call C_GetFunctionList() from %s", pkcs11_logger_globals.env_var_library_path);
129-
CALL_N_CLEAR(DLCLOSE, pkcs11_logger_globals.orig_lib_handle);
129+
CALL_N_CLEAR(pkcs11_logger_dl_close, pkcs11_logger_globals.orig_lib_handle);
130130
return PKCS11_LOGGER_RV_ERROR;
131131
}
132132

src/pkcs11-logger.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@
6161
#pragma pack(pop, cryptoki)
6262

6363
// Platform dependend type for dynamically loaded library handle
64-
typedef HINSTANCE DLHANDLE;
65-
// Platform dependend function that loads dynamic library
66-
#define DLOPEN(lib) LoadLibraryA((lib))
67-
// Platform dependend function that gets function pointer from dynamic library
68-
#define DLSYM(lib, func) GetProcAddress((lib), (func))
69-
// Platform dependend function that unloads dynamic library
70-
#define DLCLOSE FreeLibrary
64+
typedef HMODULE DLHANDLE;
7165

7266

7367
#else // #ifdef _WIN32
@@ -95,12 +89,6 @@ typedef HINSTANCE DLHANDLE;
9589

9690
// Platform dependend type for dynamically loaded library handle
9791
typedef void* DLHANDLE;
98-
// Platform dependend function that loads dynamic library
99-
#define DLOPEN(lib) dlopen((lib), RTLD_NOW | RTLD_LOCAL);
100-
// Platform dependend function that gets function pointer from dynamic library
101-
#define DLSYM(lib, func) dlsym((lib), (func))
102-
// Platform dependend function that unloads dynamic library
103-
#define DLCLOSE dlclose
10492

10593

10694
#endif // #ifdef _WIN32
@@ -172,6 +160,11 @@ PKCS11_LOGGER_GLOBALS;
172160
// Macro that removes unused argument warning
173161
#define IGNORE_ARG(P) (void)(P)
174162

163+
// dl.c - declaration of functions
164+
DLHANDLE pkcs11_logger_dl_open(const char* library);
165+
void* pkcs11_logger_dl_sym(DLHANDLE library, const char* function);
166+
int pkcs11_logger_dl_close(DLHANDLE library);
167+
175168
// init.c - declaration of functions
176169
#ifdef _WIN32
177170
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
@@ -215,3 +208,4 @@ int pkcs11_logger_utils_str_to_long(const char *str, unsigned long *val);
215208
void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len);
216209
unsigned long pkcs11_logger_utils_get_thread_id(void);
217210
int pkcs11_logger_utils_get_process_id(void);
211+
CK_BBOOL pkcs11_logger_utils_path_is_absolute(const char* path);

src/utils.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,36 @@ int pkcs11_logger_utils_get_process_id(void)
9898
return getpid();
9999
#endif
100100
}
101+
102+
103+
// Determines whether the path is absolute
104+
CK_BBOOL pkcs11_logger_utils_path_is_absolute(const char* path)
105+
{
106+
#ifdef _WIN32
107+
if ((NULL == path) || (strlen(path) < 3))
108+
return CK_FALSE;
109+
110+
char char1 = path[0];
111+
char char2 = path[1];
112+
char char3 = path[2];
113+
114+
// First character must be valid drive character
115+
if ((char1 < 'A' || char1 > 'Z') && (char1 < 'a' || char1 > 'z'))
116+
return CK_FALSE;
117+
118+
// Second character must be valid volume separator character
119+
if (char2 != ':')
120+
return CK_FALSE;
121+
122+
// Third character must be valid directory separator character
123+
if (char3 != '\\' && char3 != '/')
124+
return CK_FALSE;
125+
126+
return CK_TRUE;
127+
#else
128+
if ((NULL == path) || (strlen(path) < 1))
129+
return CK_FALSE;
130+
131+
return (path[0] == '/') ? CK_TRUE : CK_FALSE;
132+
#endif
133+
}

0 commit comments

Comments
 (0)