Harden PowerShell execution with safe SearchPathW and os_spawn

This commit is contained in:
Andrea Mazzoleni 2026-08-16 22:46:12 +02:00
parent 0669f03754
commit 2c5390ecf1
2 changed files with 69 additions and 25 deletions

View file

@ -33,6 +33,14 @@
#define WIN32_ES_CONTINUOUS 0x80000000L
#endif
/* For SetSearchPathMode */
#ifndef BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE
#define BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE 0x00000001
#endif
#ifndef BASE_SEARCH_PATH_PERMANENT
#define BASE_SEARCH_PATH_PERMANENT 0x00008000
#endif
/* File Index */
#undef FILE_INVALID_FILE_ID
#define FILE_INVALID_FILE_ID ((ULONGLONG)-1LL)
@ -3611,6 +3619,9 @@ void os_init(unsigned opt)
os_exit();
}
/* enable safe search path mode permanently to prevent binary hijacking from CWD */
SetSearchPathMode(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT);
dll_advapi32 = LoadLibrary("ADVAPI32.DLL");
if (!dll_advapi32) {
os_syslog(OS_LVL_CRITICAL, "error loading the ADVAPI32 module");