Skip to contentSkip to conversation

um: Set parent-death signal for sub-processes

PATCH In review Tiwei Bie 2 participants 6 messages Oct 24, 2024 – Oct 25, 2024 User-mode Linux (UML)

lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

cover um: Set parent-death signal for sub-processes

The ubd io and write_sigio threads/processes may leak e.g. when the main process is killed by "kill -9". Fix it by setting the parent-death signal for them.

Tiwei Bie (4):
um: Add os_set_pdeathsig helper function
um: Set parent-death signal for ubd io thread/process
um: Set parent-death signal for write_sigio thread/process
um: Use os_set_pdeathsig helper in winch thread/process

 arch/um/drivers/chan_user.c | 3 +--
 arch/um/drivers/ubd_kern.c  | 1 +
 arch/um/include/shared/os.h | 2 ++
 arch/um/os-Linux/process.c  | 6 ++++++
 arch/um/os-Linux/sigio.c    | 1 +
 5 files changed, 11 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/4 um: Add os_set_pdeathsig helper function

This helper can be used to set the parent-death signal of the calling process to SIGKILL to ensure that the process will be killed if the UML kernel dies unexpectedly without proper cleanup. This helper will be used in the follow-up patches.

---
 arch/um/include/shared/os.h | 2 ++
 arch/um/os-Linux/process.c  | 6 ++++++
 2 files changed, 8 insertions(+)
arch/um/include/shared/os.h +2 -0
index 09f8201de5db..d709a24dc6fc 100644
@@ -216,6 +216,8 @@ extern int os_drop_memory(void *addr, int length);
 extern int can_drop_memory(void);
 extern int os_mincore(void *addr, unsigned long len);
 
+void os_set_pdeathsig(void);
+
 /* execvp.c */
 extern int execvp_noalloc(char *buf, const char *file, char *const argv[]);
 /* helper.c */
arch/um/os-Linux/process.c +6 -0
index f20602e793d9..9f086f939420 100644
@@ -12,6 +12,7 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/ptrace.h>
+#include <sys/prctl.h>
 #include <sys/wait.h>
 #include <asm/unistd.h>
 #include <init.h>
@@ -203,3 +204,8 @@ void init_new_thread_signals(void)
 	set_handler(SIGIO);
 	signal(SIGWINCH, SIG_IGN);
 }
+
+void os_set_pdeathsig(void)
+{
+	prctl(PR_SET_PDEATHSIG, SIGKILL);
+}
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/4 um: Set parent-death signal for ubd io thread/process

In reply to Tiwei Bie

The ubd io thread is not really a traditional thread. Set the parent-death signal for it to ensure that it will be killed if the UML kernel dies unexpectedly without proper cleanup.

---
 arch/um/drivers/ubd_kern.c | 1 +
 1 file changed, 1 insertion(+)
arch/um/drivers/ubd_kern.c +1 -0
index 7f28ec1929dc..2b8d04e67600 100644
@@ -1499,6 +1499,7 @@ int io_thread(void *arg)
 {
 	int n, count, written, res;
 
+	os_set_pdeathsig();
 	os_fix_helper_signals();
 
 	while(1){
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/4 um: Set parent-death signal for write_sigio thread/process

In reply to Tiwei Bie

The write_sigio thread is not really a traditional thread. Set the parent-death signal for it to ensure that it will be killed if the UML kernel dies unexpectedly without proper cleanup.

---
 arch/um/os-Linux/sigio.c | 1 +
 1 file changed, 1 insertion(+)
arch/um/os-Linux/sigio.c +1 -0
index 9e71794839e8..9aac8def4d63 100644
@@ -55,6 +55,7 @@ static int write_sigio_thread(void *unused)
 	int i, n, respond_fd;
 	char c;
 
+	os_set_pdeathsig();
 	os_fix_helper_signals();
 	fds = &current_poll;
 	while (1) {
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/4 um: Use os_set_pdeathsig helper in winch thread/process

In reply to Tiwei Bie

Since we have a helper now, let's switch to using it. It will make the code slightly more consistent.

---
 arch/um/drivers/chan_user.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/um/drivers/chan_user.c +1 -2
index 1434114b2f34..35f9beeb19b3 100644
@@ -10,7 +10,6 @@
 #include <signal.h>
 #include <termios.h>
 #include <sys/ioctl.h>
-#include <sys/prctl.h>
 #include "chan_user.h"
 #include <os.h>
 #include <um_malloc.h>
@@ -162,7 +161,7 @@ static __noreturn int winch_thread(void *arg)
 	int count;
 	char c = 1;
 
-	prctl(PR_SET_PDEATHSIG, SIGKILL);
+	os_set_pdeathsig();
 
 	pty_fd = data->pty_fd;
 	pipe_fd = data->pipe_fd;
Signature · 1 line
2.34.1
maintainer
#5 lore raw
In reply to Tiwei Bie
Quoted 4 lines · Tiwei Bie

The ubd io and write_sigio threads/processes may leak e.g. when the main process is killed by "kill -9". Fix it by setting the parent-death signal for them.

I always used killall, but yeah, good idea, thanks!

johannes