Skip to content

Commit

Permalink
Fix unsecure loading library pointed by ENV variable (#142)
Browse files Browse the repository at this point in the history
* Adding function to check absolute path to avoid undefined behavior
  • Loading branch information
mk-srivastava authored Apr 22, 2024
1 parent ec879c3 commit e20cd60
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/ittnotify/jitprofiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#if ITT_PLATFORM==ITT_PLATFORM_WIN
#include <windows.h>
#include <string.h>
#include <ctype.h>
#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
#if ITT_PLATFORM != ITT_PLATFORM_MAC && ITT_PLATFORM != ITT_PLATFORM_FREEBSD && ITT_PLATFORM != ITT_PLATFORM_OPENBSD
#include <malloc.h>
Expand Down Expand Up @@ -112,6 +114,28 @@ ITT_EXTERN_C iJIT_IsProfilingActiveFlags JITAPI iJIT_IsProfilingActive()
return executionMode;
}

#if ITT_PLATFORM == ITT_PLATFORM_WIN
static int isValidAbsolutePath(char *path)
{
if (path == NULL)
{
return 0;
}
else if (strlen(path) > 2)
{
if (isalpha(path[0]) && path[1] == ':' && path[2] == '\\')
{
return 1;
}
else if (path[0] == '\\' && path[1] == '\\')
{
return 1;
}
}
return 0;
}
#endif

/* This function loads the collector dll and the relevant functions.
* on success: all functions load, iJIT_DLL_is_missing = 0, return value = 1
* on failure: all functions are NULL, iJIT_DLL_is_missing = 1, return value = 0
Expand Down Expand Up @@ -155,7 +179,7 @@ static int loadiJIT_Funcs()
{
envret = GetEnvironmentVariableA(NEW_DLL_ENVIRONMENT_VAR,
dllName, dNameLength);
if (envret)
if (envret && isValidAbsolutePath(dllName))
{
/* Try to load the dll from the PATH... */
m_libHandle = LoadLibraryExA(dllName,
Expand Down

0 comments on commit e20cd60

Please sign in to comment.