Description
Using an untrusted path or filename as the target of a process execution or dynamic-library API lets an attacker select code that the application loads or runs. An absolute path can point directly to an attacker-controlled file. A filename or relative path can exploit lookup rules such as PATH, library search paths, and the current directory to select an unintended file.
On Windows, passing NULL for lpApplicationName in CreateProcess*() causes the executable name to be parsed from the first token of lpCommandLine. An ambiguous executable path containing spaces, or an attacker-writable directory in the search path, can select a different executable. LoadLibrary*() also applies search rules to the target DLL's dependencies, so fixing only the target does not resolve every DLL search-path issue.
This differs from operating-system command injection, where a shell interprets a command string. execve() and CreateProcessW() do not normally interpret shell metacharacters, but attacker code can still run if external input selects the executable itself. Even with a fixed executable, separately assess whether untrusted arguments become options or syntax in the target program.
Potential impact
- Arbitrary executable or dynamic-library code may run with the application's permissions.
- In a privileged service, this can cause privilege escalation, bypass a security boundary, or compromise the system.
- DLL sideloading or search-path hijacking can establish persistence or alter legitimate program behavior.
- Malicious or incompatible targets can disclose sensitive information or disrupt service.
Remediation
Accept a small set of identifiers, such as plugin names. Map each identifier to a fixed full path in trusted code or configuration. Reject unknown identifiers and user-supplied paths, filenames, or extensions. Normalizing a path, checking its extension, or calling sanitize_path() cannot authorize it.
- On POSIX, pass a fixed absolute path to
execve()orposix_spawn(). Supply a separate argument array and only the trusted environment variables the process needs. Avoidexecvp(),execvpe(),execlp(), andposix_spawnp()when the target must be independent ofPATH. - For shared libraries, pass the trusted full path to
dlopen()ordlmopen(). Check search paths, RPATH, RUNPATH, and environment variables for the library and its dependencies. - On Windows, set
lpApplicationNameto the executable’s full path and supplylpCommandLinein a separate writable buffer. Load DLLs withLoadLibraryExW()and the necessary restrictiveLOAD_LIBRARY_SEARCH_*flags. UseSetDefaultDllDirectories()if the process also needs a restricted default search path. - Run with the minimum permissions required. Limit who can modify executables, libraries, dependencies, and their directories. If you also verify a signature or hash, protect the file from replacement between verification and use.
Examples
C
Before
#include <dlfcn.h>
#include <stddef.h>
int main(int argc, char **argv) {
if (argc != 2) {
return 2;
}
void *handle = dlopen(argv[1], RTLD_NOW);
if (handle == NULL) {
return 1;
}
dlclose(handle);
return 0;
}
The caller supplies the library path in argv[1].
After
#include <dlfcn.h>
#include <stddef.h>
#include <string.h>
static const char *plugin_path(const char *selector) {
if (strcmp(selector, "image") == 0) {
return "/opt/example/lib/image-plugin.so";
}
if (strcmp(selector, "report") == 0) {
return "/opt/example/lib/report-plugin.so";
}
return NULL;
}
int main(int argc, char **argv) {
if (argc != 2) {
return 2;
}
const char *path = plugin_path(argv[1]);
if (path == NULL) {
return 2;
}
void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (handle == NULL) {
return 1;
}
dlclose(handle);
return 0;
}
image and report each select a fixed library. Protect their directories and dependencies from modification in the deployed application.
To start a process, pass its fixed path and arguments separately.
#include <spawn.h>
#include <stddef.h>
int run_worker(char *job_id) {
char *const arguments[] = {
"worker", "--job-id", job_id, NULL
};
char *const environment[] = {
"LANG=C", NULL
};
pid_t child;
return posix_spawn(
&child,
"/opt/example/bin/worker",
NULL,
NULL,
arguments,
environment);
}
posix_spawn() receives a fixed worker path, a separate argument list, and an explicit environment. Validate the length, characters, and allowed values of job_id. Check whether the worker treats it as an option or other syntax.
C++
Before
#include <dlfcn.h>
#include <iostream>
#include <string>
int main() {
std::string path;
if (!std::getline(std::cin, path)) {
return 2;
}
void *handle = ::dlopen(path.c_str(), RTLD_NOW);
if (handle == nullptr) {
return 1;
}
::dlclose(handle);
return 0;
}
The loader receives the path from standard input. Calling c_str() on the std::string passes the same path.
After
#include <dlfcn.h>
#include <iostream>
#include <string>
#include <string_view>
static const char *plugin_path(std::string_view selector) {
if (selector == "image") {
return "/opt/example/lib/image-plugin.so";
}
if (selector == "report") {
return "/opt/example/lib/report-plugin.so";
}
return nullptr;
}
int main() {
std::string selector;
if (!std::getline(std::cin, selector)) {
return 2;
}
const char *path = plugin_path(selector);
if (path == nullptr) {
return 2;
}
void *handle = ::dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (handle == nullptr) {
return 1;
}
::dlclose(handle);
return 0;
}
plugin_path() returns a fixed path for each allowed name and rejects other names. Using std::filesystem::path to represent a path or canonical() to resolve it still requires a separate authorization check.
References
- POSIX.1-2024
execfunctions - POSIX.1-2024
posix_spawn() - Linux man-pages
dlopen()·dlmopen() - Linux man-pages
execfunctions - Linux man-pages
execveat() - Microsoft Dynamic-Link Library Security
- Microsoft Dynamic-Link Library Search Order
- Microsoft
LoadLibraryExW() - Microsoft
CreateProcessW() - Microsoft
CreateProcessAsUserW() - Microsoft
CreateProcessWithLogonW() - Microsoft
CreateProcessWithTokenW() - ISO/IEC 9899:2024 Programming languages — C
- ISO/IEC 14882:2024 Programming languages — C++
- Microsoft C++
mainfunction and command-line arguments - OWASP ASVS 5.0.0 V2.2.1
- CWE-114: Process Control
- CWE-73: External Control of File Name or Path
- CWE-426: Untrusted Search Path
- CWE-427: Uncontrolled Search Path Element
- OWASP Top 10:2025 A05 Injection