From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
patches@lists.linux.dev, Luca Boccassi <luca.boccassi@gmail.com>,
Oleg Nesterov <oleg@redhat.com>,
Christian Brauner <brauner@kernel.org>
Subject: [PATCH 6.15 47/49] coredump: hand a pidfd to the usermode coredump helper
Date: Mon, 2 Jun 2025 15:47:39 +0200 [thread overview]
Message-ID: <20250602134239.786689812@linuxfoundation.org> (raw)
In-Reply-To: <20250602134237.940995114@linuxfoundation.org>
6.15-stable review patch. If anyone has any objections, please let me know.
------------------
From: Christian Brauner <brauner@kernel.org>
commit b5325b2a270fcaf7b2a9a0f23d422ca8a5a8bdea upstream.
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.
Link: https://github.com/systemd/systemd/pull/37125 [1]
Link: https://lore.kernel.org/20250414-work-coredump-v2-3-685bf231f828@kernel.org
Tested-by: Luca Boccassi <luca.boccassi@gmail.com>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
fs/coredump.c | 56 +++++++++++++++++++++++++++++++++++++++++++----
include/linux/coredump.h | 1
2 files changed, 53 insertions(+), 4 deletions(-)
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -43,6 +43,8 @@
#include <linux/timekeeping.h>
#include <linux/sysctl.h>
#include <linux/elf.h>
+#include <linux/pidfs.h>
+#include <uapi/linux/pidfd.h>
#include <linux/uaccess.h>
#include <asm/mmu_context.h>
@@ -60,6 +62,12 @@ static void free_vma_snapshot(struct cor
#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 +347,27 @@ static int format_corename(struct core_n
case 'C':
err = cn_printf(cn, "%d", cprm->cpu);
break;
+ /* pidfd number */
+ case 'F': {
+ /*
+ * Installing a pidfd only makes sense if
+ * we actually spawn a usermode helper.
+ */
+ if (!ispipe)
+ break;
+
+ /*
+ * 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.
+ */
+ cprm->pid = task_tgid(current);
+ err = cn_printf(cn, "%d", COREDUMP_PIDFD_NUMBER);
+ break;
+ }
default:
break;
}
@@ -493,7 +522,7 @@ static void wait_for_dump_helpers(struct
}
/*
- * 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 +532,31 @@ static void wait_for_dump_helpers(struct
* 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;
int err;
+ if (cp->pid) {
+ struct file *pidfs_file __free(fput) = NULL;
+
+ pidfs_file = pidfs_alloc_file(cp->pid, 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. So we should always be able to use
+ * COREDUMP_PIDFD_NUMBER as our file descriptor value.
+ */
+ 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 +646,7 @@ void do_coredump(const kernel_siginfo_t
}
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 +685,7 @@ void do_coredump(const kernel_siginfo_t
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);
--- a/include/linux/coredump.h
+++ b/include/linux/coredump.h
@@ -28,6 +28,7 @@ struct coredump_params {
int vma_count;
size_t vma_data_size;
struct core_vma_metadata *vma_meta;
+ struct pid *pid;
};
extern unsigned int core_file_note_size_limit;
next prev parent reply other threads:[~2025-06-02 13:49 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 13:46 [PATCH 6.15 00/49] 6.15.1-rc1 review Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 01/49] arm64: dts: socfpga: agilex5: fix gpio0 address Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 02/49] arm64: dts: rockchip: fix internal USB hub instability on RK3399 Puma Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 03/49] arm64: dts: rockchip: Add missing SFC power-domains to rk3576 Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 04/49] arm64: dts: qcom: ipq9574: Add missing properties for cryptobam Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 05/49] arm64: dts: qcom: sa8775p: " Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 06/49] arm64: dts: qcom: sa8775p: Remove extra entries from the iommus property Greg Kroah-Hartman
2025-06-02 13:46 ` [PATCH 6.15 07/49] arm64: dts: qcom: sa8775p: Remove cdsp compute-cb@10 Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 08/49] arm64: dts: qcom: sm8350: Fix typo in pil_camera_mem node Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 09/49] arm64: dts: qcom: sm8450: Add missing properties for cryptobam Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 10/49] arm64: dts: qcom: sm8550: " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 11/49] arm64: dts: qcom: sm8650: " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 12/49] arm64: dts: qcom: x1e001de-devkit: Fix vreg_l2j_1p2 voltage Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 13/49] arm64: dts: qcom: x1e001de-devkit: mark l12b and l15b always-on Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 14/49] arm64: dts: qcom: x1e80100-asus-vivobook-s15: Fix vreg_l2j_1p2 voltage Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 15/49] arm64: dts: qcom: x1e80100-dell-xps13-9345: mark l12b and l15b always-on Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 16/49] arm64: dts: qcom: x1e80100-hp-omnibook-x14: Enable SMB2360 0 and 1 Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 17/49] arm64: dts: qcom: x1e80100-hp-omnibook-x14: Fix vreg_l2j_1p2 voltage Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 18/49] arm64: dts: qcom: x1e80100-hp-x14: mark l12b and l15b always-on Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 19/49] arm64: dts: qcom: x1e80100-lenovo-yoga-slim7x: Fix vreg_l2j_1p2 voltage Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 20/49] arm64: dts: qcom: x1e80100-qcp: " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 21/49] arm64: dts: qcom: x1e80100-qcp: mark l12b and l15b always-on Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 22/49] arm64: dts: qcom: x1e80100-yoga-slim7x: " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 23/49] arm64: dts: qcom: x1e80100: Fix video thermal zone Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 24/49] arm64: dts: qcom: x1e80100: Apply consistent critical thermal shutdown Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 25/49] arm64: dts: qcom: x1e80100: Add GPU cooling Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 26/49] arm64: dts: qcom: x1e80100: Fix PCIe 3rd controller DBI size Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 27/49] arm64: dts: ti: k3-am62-main: Set eMMC clock parent to default Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 28/49] arm64: dts: ti: k3-am62a-main: " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 29/49] arm64: dts: ti: k3-am62p-j722s-common-main: " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 30/49] arm64: dts: ti: k3-am62x: Remove clock-names property from IMX219 overlay Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 31/49] arm64: dts: ti: k3-am62x: Rename I2C switch to I2C mux in " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 32/49] arm64: dts: ti: k3-am62x: Rename I2C switch to I2C mux in OV5640 overlay Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 33/49] arm64: dts: ti: k3-am65-main: Add missing taps to sdhci0 Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 34/49] arm64: dts: ti: k3-am68-sk: Fix regulator hierarchy Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 35/49] arm64: dts: ti: k3-j721e-sk: Add DT nodes for power regulators Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 36/49] arm64: dts: ti: k3-j721e-sk: Remove clock-names property from IMX219 overlay Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 37/49] arm64: dts: ti: k3-j721e-sk: Add requiried voltage supplies for IMX219 Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 38/49] arm64: dts: ti: k3-j722s-evm: Enable "serdes_wiz0" and "serdes_wiz1" Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 39/49] arm64: dts: ti: k3-j722s-main: Disable " Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 40/49] arm64: dts: ti: k3-j784s4-j742s2-main-common: Fix length of serdes_ln_ctrl Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 41/49] net_sched: hfsc: Address reentrant enqueue adding class to eltree twice Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 42/49] perf/arm-cmn: Fix REQ2/SNP2 mixup Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 43/49] perf/arm-cmn: Initialise cmn->cpu earlier Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 44/49] perf/arm-cmn: Add CMN S3 ACPI binding Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 45/49] pidfs: move O_RDWR into pidfs_alloc_file() Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 46/49] coredump: fix error handling for replace_fd() Greg Kroah-Hartman
2025-06-02 13:47 ` Greg Kroah-Hartman [this message]
2025-06-02 13:47 ` [PATCH 6.15 48/49] iommu: Avoid introducing more races Greg Kroah-Hartman
2025-06-02 13:47 ` [PATCH 6.15 49/49] iommu: Handle yet another race around registration Greg Kroah-Hartman
2025-06-02 18:16 ` [PATCH 6.15 00/49] 6.15.1-rc1 review Florian Fainelli
2025-06-02 19:51 ` Peter Schneider
2025-06-03 7:22 ` Ron Economos
2025-06-03 9:23 ` Mark Brown
2025-06-03 10:10 ` Christian Heusel
2025-06-03 11:40 ` Takeshi Ogasawara
2025-06-03 12:14 ` Naresh Kamboju
2025-06-03 17:10 ` Shuah Khan
2025-06-04 9:41 ` Jon Hunter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250602134239.786689812@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=brauner@kernel.org \
--cc=luca.boccassi@gmail.com \
--cc=oleg@redhat.com \
--cc=patches@lists.linux.dev \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox