Skip to contentSkip to conversation

linux-user: Emulate CLONE_PIDFD flag in clone()

PATCH Applied Helge Deller 2 participants 3 messages Nov 29, 2022 – Mar 7, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Emulate CLONE_PIDFD flag in clone()

Add emulation for the CLONE_PIDFD flag of the clone() syscall. This flag was added in Linux kernel 5.2.

Successfully tested on a x86-64 Linux host with hppa-linux target. Can be verified by running the testsuite of the qcoro debian package, which breaks hard and kills the currently logged-in user without this patch.

linux-user/strace.c +1 -0
index 9ae5a812cd..8fa5c1ec3d 100644
@@ -1097,6 +1097,7 @@ UNUSED static struct flags clone_flags[] = {
     FLAG_GENERIC(CLONE_FS),
     FLAG_GENERIC(CLONE_FILES),
     FLAG_GENERIC(CLONE_SIGHAND),
+    FLAG_GENERIC(CLONE_PIDFD),
     FLAG_GENERIC(CLONE_PTRACE),
     FLAG_GENERIC(CLONE_VFORK),
     FLAG_GENERIC(CLONE_PARENT),
linux-user/syscall.c +26 -1
index 24b25759be..0468a1bad7 100644
@@ -213,7 +213,7 @@ struct file_clone_range {
 
 /* Flags for fork which we can implement within QEMU itself */
 #define CLONE_OPTIONAL_FORK_FLAGS               \
-    (CLONE_SETTLS | CLONE_PARENT_SETTID |       \
+    (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
      CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
 
 /* Flags for thread creation which we can implement within QEMU itself */
@@ -6747,6 +6747,17 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
             return -TARGET_EINVAL;
         }
 
+#if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
+        if (flags & CLONE_PIDFD) {
+            return -TARGET_EINVAL;
+        }
+#endif
+
+        /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
+        if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
+            return -TARGET_EINVAL;
+        }
+
         if (block_signals()) {
             return -QEMU_ERESTARTSYS;
         }
@@ -6774,6 +6785,20 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
                 ts->child_tidptr = child_tidptr;
         } else {
             cpu_clone_regs_parent(env, flags);
+            if (flags & CLONE_PIDFD) {
+                int pid_fd = 0;
+#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
+                int pid_child = ret;
+                pid_fd = pidfd_open(pid_child, 0);
+                if (pid_fd >= 0) {
+                        fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
+                                               | FD_CLOEXEC);
+                } else {
+                        pid_fd = 0;
+                }
+#endif
+                put_user_u32(pid_fd, parent_tidptr);
+                }
             fork_end(0);
         }
     }
Reviewed-by on v1 1/1
#1 lore raw

Le 29/11/2022 à 12:08, Helge Deller a écrit :

Quoted 65 lines

Add emulation for the CLONE_PIDFD flag of the clone() syscall. This flag was added in Linux kernel 5.2.

Successfully tested on a x86-64 Linux host with hppa-linux target. Can be verified by running the testsuite of the qcoro debian package, which breaks hard and kills the currently logged-in user without this patch.

linux-user/strace.c +1 -0
index 9ae5a812cd..8fa5c1ec3d 100644
@@ -1097,6 +1097,7 @@ UNUSED static struct flags clone_flags[] = {
      FLAG_GENERIC(CLONE_FS),
      FLAG_GENERIC(CLONE_FILES),
      FLAG_GENERIC(CLONE_SIGHAND),
+    FLAG_GENERIC(CLONE_PIDFD),
      FLAG_GENERIC(CLONE_PTRACE),
      FLAG_GENERIC(CLONE_VFORK),
      FLAG_GENERIC(CLONE_PARENT),
linux-user/syscall.c +19 -1
index 24b25759be..0468a1bad7 100644
@@ -213,7 +213,7 @@ struct file_clone_range {
 
  /* Flags for fork which we can implement within QEMU itself */
  #define CLONE_OPTIONAL_FORK_FLAGS               \
-    (CLONE_SETTLS | CLONE_PARENT_SETTID |       \
+    (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
       CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
 
  /* Flags for thread creation which we can implement within QEMU itself */
@@ -6747,6 +6747,17 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
              return -TARGET_EINVAL;
          }
 
+#if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
+        if (flags & CLONE_PIDFD) {
+            return -TARGET_EINVAL;
+        }
+#endif
+
+        /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
+        if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
+            return -TARGET_EINVAL;
+        }
+
          if (block_signals()) {
              return -QEMU_ERESTARTSYS;
          }
@@ -6774,6 +6785,20 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
                  ts->child_tidptr = child_tidptr;
          } else {
              cpu_clone_regs_parent(env, flags);
+            if (flags & CLONE_PIDFD) {
+                int pid_fd = 0;
+#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
+                int pid_child = ret;
+                pid_fd = pidfd_open(pid_child, 0);
+                if (pid_fd >= 0) {
+                        fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)

linux-user/syscall.c static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, · line 6804

Quoted 10 lines
linux-user/syscall.c +7 -0
+                                               | FD_CLOEXEC);
+                } else {
+                        pid_fd = 0;
+                }
+#endif
+                put_user_u32(pid_fd, parent_tidptr);
+                }
              fork_end(0);
          }
      }
#2 lore raw
In reply to Helge Deller

Le 29/11/2022 à 12:08, Helge Deller a écrit :

Quoted 65 lines

Add emulation for the CLONE_PIDFD flag of the clone() syscall. This flag was added in Linux kernel 5.2.

Successfully tested on a x86-64 Linux host with hppa-linux target. Can be verified by running the testsuite of the qcoro debian package, which breaks hard and kills the currently logged-in user without this patch.

linux-user/strace.c +1 -0
index 9ae5a812cd..8fa5c1ec3d 100644
@@ -1097,6 +1097,7 @@ UNUSED static struct flags clone_flags[] = {
      FLAG_GENERIC(CLONE_FS),
      FLAG_GENERIC(CLONE_FILES),
      FLAG_GENERIC(CLONE_SIGHAND),
+    FLAG_GENERIC(CLONE_PIDFD),
      FLAG_GENERIC(CLONE_PTRACE),
      FLAG_GENERIC(CLONE_VFORK),
      FLAG_GENERIC(CLONE_PARENT),
linux-user/syscall.c +19 -1
index 24b25759be..0468a1bad7 100644
@@ -213,7 +213,7 @@ struct file_clone_range {
 
  /* Flags for fork which we can implement within QEMU itself */
  #define CLONE_OPTIONAL_FORK_FLAGS               \
-    (CLONE_SETTLS | CLONE_PARENT_SETTID |       \
+    (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
       CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
 
  /* Flags for thread creation which we can implement within QEMU itself */
@@ -6747,6 +6747,17 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
              return -TARGET_EINVAL;
          }
 
+#if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
+        if (flags & CLONE_PIDFD) {
+            return -TARGET_EINVAL;
+        }
+#endif
+
+        /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
+        if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
+            return -TARGET_EINVAL;
+        }
+
          if (block_signals()) {
              return -QEMU_ERESTARTSYS;
          }
@@ -6774,6 +6785,20 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
                  ts->child_tidptr = child_tidptr;
          } else {
              cpu_clone_regs_parent(env, flags);
+            if (flags & CLONE_PIDFD) {
+                int pid_fd = 0;
+#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
+                int pid_child = ret;
+                pid_fd = pidfd_open(pid_child, 0);
+                if (pid_fd >= 0) {
+                        fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)

linux-user/syscall.c static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, · line 6804

Quoted 10 lines
linux-user/syscall.c +7 -0
+                                               | FD_CLOEXEC);
+                } else {
+                        pid_fd = 0;
+                }
+#endif
+                put_user_u32(pid_fd, parent_tidptr);
+                }
              fork_end(0);
          }
      }

Applied to my linux-user-for-8.0 branch.

Thanks,
Laurent