Configure PR_SET_PDEATHSIG on Linux and Job Objects on Windows to prevent orphaned subprocesses on program crash
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-win (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run

This commit is contained in:
Andrea Mazzoleni 2026-08-01 18:26:01 +02:00
parent 9a1987e024
commit cf2196a90f
4 changed files with 70 additions and 10 deletions

View file

@ -748,7 +748,7 @@ int os_validate_exec_input(const char* str)
int os_script(char** argv, char** envp, const char* run_as_user)
{
char resolved_path[PATH_MAX];
pid_t pid;
pid_t pid, ppid;
int ret;
int status;
int64_t start, stop;
@ -765,6 +765,8 @@ int os_script(char** argv, char** envp, const char* run_as_user)
start = os_tick_sec();
ppid = getpid();
pid = fork();
if (pid < 0) {
os_syslog(OS_LVL_INFO, "failed to fork script=%s, errno=%s(%d)", resolved_path, strerror(errno), errno);
@ -783,6 +785,15 @@ int os_script(char** argv, char** envp, const char* run_as_user)
*/
setpgid(0, 0);
#if defined(__linux__) && defined(PR_SET_PDEATHSIG)
/* Send SIGKILL to child if parent daemon dies unexpectedly */
prctl(PR_SET_PDEATHSIG, SIGKILL);
/* exit if parent died before prctl to avoid becoming orphaned */
if (getppid() != ppid) {
_exit(127);
}
#endif
/* drop privileges first (if configured) */
if (run_as_user && run_as_user[0] != 0) {
errno = 0;
@ -917,7 +928,7 @@ int os_script(char** argv, char** envp, const char* run_as_user)
int os_command(const char* command, const char* run_as_user, const char* stdin_text)
{
pid_t pid;
pid_t pid, ppid;
int ret;
int status;
int pipe_fds[2] = { -1, -1 };
@ -933,6 +944,8 @@ int os_command(const char* command, const char* run_as_user, const char* stdin_t
start = os_tick_sec();
ppid = getpid();
pid = fork();
if (pid < 0) {
os_syslog(OS_LVL_INFO, "failed to fork command=%s, errno=%s(%d)", command, strerror(errno), errno);
@ -954,6 +967,15 @@ int os_command(const char* command, const char* run_as_user, const char* stdin_t
*/
setpgid(0, 0);
#if defined(__linux__) && defined(PR_SET_PDEATHSIG)
/* Send SIGKILL to child if parent daemon dies unexpectedly */
prctl(PR_SET_PDEATHSIG, SIGKILL);
/* exit if parent died before prctl to avoid becoming orphaned */
if (getppid() != ppid) {
_exit(127);
}
#endif
if (pipe_fds[1] != -1)
close(pipe_fds[1]); /* Close unused write end */
@ -1130,7 +1152,7 @@ pid_t os_spawn(char** argv, int* stdout_read_fd, int* stderr_read_fd, const char
int err_pipe[2];
int has_out = (stdout_read_fd != NULL);
int has_err = (stderr_read_fd != NULL);
pid_t pid;
pid_t pid, ppid;
int fd = verify_executable(argv[0], resolved_path, 0);
if (fd < 0) {
@ -1157,6 +1179,8 @@ pid_t os_spawn(char** argv, int* stdout_read_fd, int* stderr_read_fd, const char
}
}
ppid = getpid();
pid = fork();
if (pid < 0) {
os_syslog(OS_LVL_INFO, "failed to fork path=%s, errno=%s(%d)", resolved_path, strerror(errno), errno);
@ -1177,6 +1201,15 @@ pid_t os_spawn(char** argv, int* stdout_read_fd, int* stderr_read_fd, const char
setpgid(0, 0);
#if defined(__linux__) && defined(PR_SET_PDEATHSIG)
/* Send SIGKILL to child if parent daemon dies unexpectedly */
prctl(PR_SET_PDEATHSIG, SIGKILL);
/* exit if parent died before prctl to avoid becoming orphaned */
if (getppid() != ppid) {
_exit(127);
}
#endif
/* drop privileges first (if configured) */
if (run_as_user && run_as_user[0] != 0) {
errno = 0;