Skip to contentSkip to conversation

coredump: hand a pidfd to the usermode coredump helper

PATCH Superseded Christian Brauner 2 participants 8 messages Apr 14, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover coredump: hand a pidfd to the usermode coredump helper

Give userspace a way to instruct the kernel to install a pidfd for the crashing process into the process started as a usermode helper. There's still tricky race-windows that cannot be easily or sometimes not closed at all by userspace. There's various ways like looking at the start time of a process to make sure that the usermode helper process is started after the crashing process but it's all very very brittle and fraught with peril.

The crashed-but-not-reaped process can be killed by userspace before coredump processing programs like systemd-coredump have had time to manually open a PIDFD from the PID the kernel provides them, which means they can be tricked into reading from an arbitrary process, and they run with full privileges as they are usermode helper processes.

Even if that specific race-window wouldn't exist it's still the safest and cleanest way to let the kernel provide the pidfd directly instead of requiring userspace to do it manually. In parallel with this commit we already have systemd adding support for this in [1].

We create a pidfs file for the coredumping process when we process the corename pattern. When the usermode helper process is forked we then install the pidfs file as file descriptor three into the usermode helpers file descriptor table so it's available to the exec'd program.

Since usermode helpers are either children of the system_unbound_wq workqueue or kthreadd we know that the file descriptor table is empty and can thus always use three as the file descriptor number.

Note, that we'll install a pidfd for the thread-group leader even if a subthread is calling do_coredump(). We know that task linkage hasn't been removed yet and even if this @current isn't the actual thread-group leader we know that the thread-group leader cannot be reaped until @current has exited.

[1]: https://github.com/systemd/systemd/pull/37125

---
Christian Brauner (3):
pidfs: move O_RDWR into pidfs_alloc_file()
coredump: fix error handling for replace_fd()
coredump: hand a pidfd to the usermode coredump helper

 fs/coredump.c            | 92 +++++++++++++++++++++++++++++++++++++++++++++---
 fs/pidfs.c               |  1 +
 include/linux/coredump.h |  1 +
 kernel/fork.c            |  2 +-
 4 files changed, 90 insertions(+), 6 deletions(-)

---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250413-work-coredump-0f7fa7e6414c

author maintainer
#1 lore raw

PATCH 1/3 pidfs: move O_RDWR into pidfs_alloc_file()

Since all pidfds must be O_RDWR currently enfore that directly in the file allocation function itself instead of letting callers specify it.

---
 fs/pidfs.c    | 1 +
 kernel/fork.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
fs/pidfs.c +1 -0
index d64a4cbeb0da..50e69a9e104a 100644
@@ -888,6 +888,7 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
 		return ERR_PTR(-ESRCH);
 
 	flags &= ~PIDFD_CLONE;
+	flags |= O_RDWR;
 	pidfd_file = dentry_open(&path, flags, current_cred());
 	/* Raise PIDFD_THREAD explicitly as do_dentry_open() strips it. */
 	if (!IS_ERR(pidfd_file))
kernel/fork.c +1 -1
index c4b26cd8998b..d184e51196a2 100644
@@ -2071,7 +2071,7 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
 	if (pidfd < 0)
 		return pidfd;
 
-	pidfd_file = pidfs_alloc_file(pid, flags | O_RDWR);
+	pidfd_file = pidfs_alloc_file(pid, flags);
 	if (IS_ERR(pidfd_file))
 		return PTR_ERR(pidfd_file);
 
Signature · 1 line
2.47.2
author maintainer
#2 lore raw

PATCH 2/3 coredump: fix error handling for replace_fd()

In reply to Christian Brauner

The replace_fd() helper returns the file descriptor number on success and a negative error code on failure. The current error handling in umh_pipe_setup() only works because the file descriptor that is replaced is zero but that's pretty volatile. Explicitly check for a negative error code.

---
 fs/coredump.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/coredump.c +6 -1
index c33c177a701b..2ed5e6956a09 100644
@@ -507,7 +507,9 @@ static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
 {
 	struct file *files[2];
 	struct coredump_params *cp = (struct coredump_params *)info->data;
-	int err = create_pipe_files(files, 0);
+	int err;
+
+	err = create_pipe_files(files, 0);
 	if (err)
 		return err;
 
@@ -515,6 +517,9 @@ static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
 
 	err = replace_fd(0, files[0], 0);
 	fput(files[0]);
+	if (err < 0)
+		return err;
+
 	/* and disallow core files too */
 	current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
 
Signature · 1 line
2.47.2
#3 lore raw

Re: [PATCH 2/3] coredump: fix error handling for replace_fd()

Quoted 6 lines · Christian Brauner

The replace_fd() helper returns the file descriptor number on success and a negative error code on failure. The current error handling in umh_pipe_setup() only works because the file descriptor that is replaced is zero but that's pretty volatile. Explicitly check for a negative error code.

...

fs/coredump.c static int umh_pipe_setup(struct subprocess_info *info, struct cred *new) · line 524

Quoted 9 lines
fs/coredump.c +3 -0
@@ -515,6 +517,9 @@ static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
 
 	err = replace_fd(0, files[0], 0);
 	fput(files[0]);
+	if (err < 0)
+		return err;
+
 	/* and disallow core files too */
 	current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};

The patch looks trivial and correct, but if we do not want to rely on the fact that replace_fd(fd => 0) return 0 on sucess, then this patch should also do

	-	return err;
	+	return 0;

?

otherwise this cleanup looks "incomplete" to me.

Oleg.

author maintainer
#4 lore raw

Re: [PATCH 2/3] coredump: fix error handling for replace_fd()

Quoted 24 lines · Oleg Nesterov
Quoted 6 lines · Christian Brauner

The replace_fd() helper returns the file descriptor number on success and a negative error code on failure. The current error handling in umh_pipe_setup() only works because the file descriptor that is replaced is zero but that's pretty volatile. Explicitly check for a negative error code.

...

fs/coredump.c static int umh_pipe_setup(struct subprocess_info *info, struct cred *new) · line 524

Quoted 9 lines
fs/coredump.c +3 -0
@@ -515,6 +517,9 @@ static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
 
 	err = replace_fd(0, files[0], 0);
 	fput(files[0]);
+	if (err < 0)
+		return err;
+
 	/* and disallow core files too */
 	current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};

The patch looks trivial and correct, but if we do not want to rely on the fact that replace_fd(fd => 0) return 0 on sucess, then this patch should also do

Quoted 6 lines
	-	return err;
	+	return 0;

?

otherwise this cleanup looks "incomplete" to me.

Ok, done.

author maintainer
#5 lore raw

PATCH 3/3 coredump: hand a pidfd to the usermode coredump helper

In reply to Christian Brauner

Give userspace a way to instruct the kernel to install a pidfd into the usermode helper process. This makes coredump handling a lot more reliable for userspace. In parallel with this commit we already have systemd adding support for this in [1].

We create a pidfs file for the coredumping process when we process the corename pattern. When the usermode helper process is forked we then install the pidfs file as file descriptor three into the usermode helpers file descriptor table so it's available to the exec'd program.

Since usermode helpers are either children of the system_unbound_wq workqueue or kthreadd we know that the file descriptor table is empty and can thus always use three as the file descriptor number.

Note, that we'll install a pidfd for the thread-group leader even if a subthread is calling do_coredump(). We know that task linkage hasn't been removed due to delay_group_leader() and even if this @current isn't the actual thread-group leader we know that the thread-group leader cannot be reaped until @current has exited.

---
 fs/coredump.c            | 85 +++++++++++++++++++++++++++++++++++++++++++++---
 include/linux/coredump.h |  1 +
 2 files changed, 82 insertions(+), 4 deletions(-)
fs/coredump.c +81 -4
index 2ed5e6956a09..ae0c1c5efe9a 100644
@@ -43,6 +43,9 @@
 #include <linux/timekeeping.h>
 #include <linux/sysctl.h>
 #include <linux/elf.h>
+#include <linux/pidfs.h>
+#include <uapi/linux/pidfd.h>
+#include <linux/vfsdebug.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
@@ -60,6 +63,12 @@ static void free_vma_snapshot(struct coredump_params *cprm);
 #define CORE_FILE_NOTE_SIZE_DEFAULT (4*1024*1024)
 /* Define a reasonable max cap */
 #define CORE_FILE_NOTE_SIZE_MAX (16*1024*1024)
+/*
+ * File descriptor number for the pidfd for the thread-group leader of
+ * the coredumping task installed into the usermode helper's file
+ * descriptor table.
+ */
+#define COREDUMP_PIDFD_NUMBER 3
 
 static int core_uses_pid;
 static unsigned int core_pipe_limit;
@@ -339,6 +348,58 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
 			case 'C':
 				err = cn_printf(cn, "%d", cprm->cpu);
 				break;
+			/* pidfd number */
+			case 'F': {
+				struct file *pidfs_file __free(fput) = NULL;
+
+				/*
+				 * Install a pidfd only makes sense if
+				 * we actually spawn a usermode helper.
+				 */
+				if (!ispipe)
+					break;
+
+				/*
+				 * We already created a pidfs_file but the user
+				 * specified F multiple times. Just print the
+				 * number multiple times.
+				 */
+				if (!cprm->pidfs_file) {
+					/*
+					 * Create a pidfs file for the
+					 * coredumping thread that we can
+					 * install into the usermode helper's
+					 * file descriptor table later.
+					 *
+					 * Note that we'll install a pidfd for
+					 * the thread-group leader. We know that
+					 * task linkage hasn't been removed yet
+					 * and even if this @current isn't the
+					 * actual thread-group leader we know
+					 * that the thread-group leader cannot
+					 * be reaped until @current has exited.
+					 */
+					pidfs_file = pidfs_alloc_file(task_tgid(current), 0);
+					if (IS_ERR(pidfs_file))
+						return PTR_ERR(pidfs_file);
+				}
+
+				 /*
+				 * Usermode helpers are childen of
+				 * either system_unbound_wq or of
+				 * kthreadd. So we know that we're
+				 * starting off with a clean file
+				 * descriptor table. Thus, we should
+				 * always be able to use file descriptor
+				 * number 3.
+				 */
+				err = cn_printf(cn, "%d", COREDUMP_PIDFD_NUMBER);
+				if (err)
+					return err;
+
+				cprm->pidfs_file = no_free_ptr(pidfs_file);
+				break;
+			}
 			default:
 				break;
 			}
@@ -493,7 +554,7 @@ static void wait_for_dump_helpers(struct file *file)
 }
 
 /*
- * umh_pipe_setup
+ * umh_coredump_setup
  * helper function to customize the process used
  * to collect the core in userspace.  Specifically
  * it sets up a pipe and installs it as fd 0 (stdin)
@@ -503,12 +564,26 @@ static void wait_for_dump_helpers(struct file *file)
  * is a special value that we use to trap recursive
  * core dumps
  */
-static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
+static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)
 {
 	struct file *files[2];
 	struct coredump_params *cp = (struct coredump_params *)info->data;
+	struct file *pidfs_file = cp->pidfs_file;
 	int err;
 
+	if (pidfs_file) {
+		/* We must start from a pristine file descriptor table. */
+		VFS_WARN_ON_ONCE((pidfs_file = fget_raw(COREDUMP_PIDFD_NUMBER)) != NULL);
+
+		/*
+		 * Takes it's own reference on success and do_coredump()
+		 * will put the additional reference.
+		 */
+		err = replace_fd(COREDUMP_PIDFD_NUMBER, pidfs_file, 0);
+		if (err < 0)
+			return err;
+	}
+
 	err = create_pipe_files(files, 0);
 	if (err)
 		return err;
@@ -598,7 +673,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
 		}
 
 		if (cprm.limit == 1) {
-			/* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
+			/* See umh_coredump_setup() which sets RLIMIT_CORE = 1.
 			 *
 			 * Normally core limits are irrelevant to pipes, since
 			 * we're not writing to the file system, but we use
@@ -637,7 +712,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
 		retval = -ENOMEM;
 		sub_info = call_usermodehelper_setup(helper_argv[0],
 						helper_argv, NULL, GFP_KERNEL,
-						umh_pipe_setup, NULL, &cprm);
+						umh_coredump_setup, NULL, &cprm);
 		if (sub_info)
 			retval = call_usermodehelper_exec(sub_info,
 							  UMH_WAIT_EXEC);
@@ -784,6 +859,8 @@ void do_coredump(const kernel_siginfo_t *siginfo)
 	if (ispipe)
 		atomic_dec(&core_dump_count);
 fail_unlock:
+	if (cprm.pidfs_file)
+		fput(cprm.pidfs_file);
 	kfree(argv);
 	kfree(cn.corename);
 	coredump_finish(core_dumped);
include/linux/coredump.h +1 -0
index 77e6e195d1d6..af2ed4b68dcf 100644
@@ -28,6 +28,7 @@ struct coredump_params {
 	int vma_count;
 	size_t vma_data_size;
 	struct core_vma_metadata *vma_meta;
+	struct file *pidfs_file;
 };
 
 extern unsigned int core_file_note_size_limit;
Signature · 1 line
2.47.2
#6 lore raw
Quoted 42 lines · Christian Brauner
fs/coredump.c +41 -0
+			case 'F': {
+				struct file *pidfs_file __free(fput) = NULL;
+
+				/*
+				 * Install a pidfd only makes sense if
+				 * we actually spawn a usermode helper.
+				 */
+				if (!ispipe)
+					break;
+
+				/*
+				 * We already created a pidfs_file but the user
+				 * specified F multiple times. Just print the
+				 * number multiple times.
+				 */
+				if (!cprm->pidfs_file) {
+					/*
+					 * Create a pidfs file for the
+					 * coredumping thread that we can
+					 * install into the usermode helper's
+					 * file descriptor table later.
+					 *
+					 * Note that we'll install a pidfd for
+					 * the thread-group leader. We know that
+					 * task linkage hasn't been removed yet
+					 * and even if this @current isn't the
+					 * actual thread-group leader we know
+					 * that the thread-group leader cannot
+					 * be reaped until @current has exited.
+					 */
+					pidfs_file = pidfs_alloc_file(task_tgid(current), 0);
+					if (IS_ERR(pidfs_file))
+						return PTR_ERR(pidfs_file);
+				}
+
+				 /*
+				 * Usermode helpers are childen of
+				 * either system_unbound_wq or of
+				 * kthreadd. So we know that we're
+				 * starting off with a clean file
+				 * descriptor table. Thus, we should

fs/coredump.c static int format_corename(struct core_name *cn, struct coredump_params *cprm, · line 402

Quoted 10 lines
fs/coredump.c +10 -0
+				 * always be able to use file descriptor
+				 * number 3.
+				 */
+				err = cn_printf(cn, "%d", COREDUMP_PIDFD_NUMBER);
+				if (err)
+					return err;
+
+				cprm->pidfs_file = no_free_ptr(pidfs_file);
+				break;
+			}

So the new case 'F' differs from other case's in that it doesn't do "break" but returns the error... this is a bit inconsistent.

Note also that if you do cn_printf() before pidfs_alloc_file(), then you can avoid __free(fput) and no_free_ptr().

But this is minor. Can't we simplify this patch?

Rather than add the new pidfs_file member into coredump_params, we can add "struct pid *pid". format_corename() will simply do

	case 'F':
		if (ispipe) {
			// no need to do get_pid()
			cprm->pid = task_tgid(current);
			err = cn_printf(...);
		}
		break;

and umh_pipe_setup() can itself do pidfs_alloc_file(cp->pid) if it is not NULL.

This way do_coredump() doesn't need any changes.

No?

Oleg.

author maintainer
#7 lore raw
Quoted 48 lines · Oleg Nesterov
Quoted 47 lines · Christian Brauner
fs/coredump.c +46 -0
+			case 'F': {
+				struct file *pidfs_file __free(fput) = NULL;
+
+				/*
+				 * Install a pidfd only makes sense if
+				 * we actually spawn a usermode helper.
+				 */
+				if (!ispipe)
+					break;
+
+				/*
+				 * We already created a pidfs_file but the user
+				 * specified F multiple times. Just print the
+				 * number multiple times.
+				 */
+				if (!cprm->pidfs_file) {
+					/*
+					 * Create a pidfs file for the
+					 * coredumping thread that we can
+					 * install into the usermode helper's
+					 * file descriptor table later.
+					 *
+					 * Note that we'll install a pidfd for
+					 * the thread-group leader. We know that
+					 * task linkage hasn't been removed yet
+					 * and even if this @current isn't the
+					 * actual thread-group leader we know
+					 * that the thread-group leader cannot
+					 * be reaped until @current has exited.
+					 */
+					pidfs_file = pidfs_alloc_file(task_tgid(current), 0);
+					if (IS_ERR(pidfs_file))
+						return PTR_ERR(pidfs_file);
+				}
+
+				 /*
+				 * Usermode helpers are childen of
+				 * either system_unbound_wq or of
+				 * kthreadd. So we know that we're
+				 * starting off with a clean file
+				 * descriptor table. Thus, we should
+				 * always be able to use file descriptor
+				 * number 3.
+				 */
+				err = cn_printf(cn, "%d", COREDUMP_PIDFD_NUMBER);
+				if (err)
Quoted 8 lines

fs/coredump.c static int format_corename(struct core_name *cn, struct coredump_params *cprm, · line 402

Quoted 5 lines
fs/coredump.c +5 -0
+					return err;
+
+				cprm->pidfs_file = no_free_ptr(pidfs_file);
+				break;
+			}

So the new case 'F' differs from other case's in that it doesn't do "break" but returns the error... this is a bit inconsistent.

Yeah, though that already happens right at the top.

Quoted 3 lines

Note also that if you do cn_printf() before pidfs_alloc_file(), then you can avoid __free(fput) and no_free_ptr().

Seemed inconsitent to me to print first even though we didn't succeed but I agree that it doesn't matter.

Quoted 2 lines

But this is minor. Can't we simplify this patch?

Let's hear it...

Quoted 12 lines

Rather than add the new pidfs_file member into coredump_params, we can add "struct pid *pid". format_corename() will simply do

	case 'F':
		if (ispipe) {
			// no need to do get_pid()
			cprm->pid = task_tgid(current);
			err = cn_printf(...);
		}
		break;
Quoted 2 lines

and umh_pipe_setup() can itself do pidfs_alloc_file(cp->pid) if it is not NULL.

Sure, same result but works for me. I'll send v2 in a bit.

Quoted 4 lines

This way do_coredump() doesn't need any changes.

No?

Yep, sounds good.