public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Neil Horman <nhorman@tuxdriver.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, earl_chew@agilent.com,
	Oleg Nesterov <oleg@redhat.com>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Andi Kleen <andi@firstfloor.org>
Subject: Re: [PATCH 2/2] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v3)
Date: Sun, 28 Jun 2009 20:35:14 -0400	[thread overview]
Message-ID: <20090629003514.GC2479@localhost.localdomain> (raw)
In-Reply-To: <20090625163050.d6a71a13.akpm@linux-foundation.org>

Allow for the kernel to wait for a core_pattern process to complete

One of the things core_pattern processes might do is interrogate the status of a
crashing process via its /proc/pid directory.  To ensure that that directory is
not removed prematurely, we wait for the process to exit prior to cleaning it
up.

Since the addition of this feature makes it possible to block the reaping of a
crashed process (if the collecting process never exits), Also introduce a new
sysctl: core_pipe_limit.  This sysctl, when non-zero, defined the maximum number
of crashing processes that can be collected in parallel.  Processes exceeding
this limit are noted in the kernel log buffer and their cores are skipped.  If
core_pipe_limit is zero (the default), this feature is disabled entirely.  In
other words, the number of cores which may be collected in parallel are
unlimited, but access to a crashing processes /proc/pid directory is not
guaranteed, as the kernel will not wait for the crashing process to exit.

Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
Reported-by: Earl Chew <earl_chew@agilent.com>

diff --git a/Documentation/sysctl/kernel.txt b/Documentation/sysctl/kernel.txt
index 322a00b..bb226ba 100644
--- a/Documentation/sysctl/kernel.txt
+++ b/Documentation/sysctl/kernel.txt
@@ -21,6 +21,7 @@ show up in /proc/sys/kernel:
 - acct
 - auto_msgmni
 - core_pattern
+- core_pipe_limit
 - core_uses_pid
 - ctrl-alt-del
 - dentry-state
@@ -119,6 +120,27 @@ core_pattern is used to specify a core dumpfile pattern name.
 
 ==============================================================
 
+core_pipe_limit:
+
+This sysctl is only applicable when core_pattern is configured to pipe core
+files to user space helper a (when the first character of core_pattern is a '|',
+see above).  When collecting cores via a pipe to an application, it is
+occasionally usefull for the collecting application to gather data about the
+crashing process from its /proc/pid directory.  In order to do this safely, the
+kernel must wait for the collecting process to exit, so as not to remove the
+crashing processes proc files prematurely.  This in turn creates the possibility
+that a misbehaving userspace collecting process can block the reaping of a
+crashed process simply by never exiting.  This sysctl defends against that.  It
+defines how many concurrent crashing processes may be piped to user space
+applications in parallel.  If this value is exceeded, then those crashing
+processes above that value are noted via the kernel log and their cores are
+skipped.  0 is a special value, indicating that unlimited processes may be
+captured in parallel, but that no waiting will take place (i.e. the collecting
+process is not guaranteed access to /proc/<crahing pid>/).  This value defaults
+to 0.
+
+==============================================================
+
 core_uses_pid:
 
 The default coredump filename is "core".  By setting
diff --git a/fs/exec.c b/fs/exec.c
index 9defd20..33d6db6 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -55,6 +55,7 @@
 #include <linux/kmod.h>
 #include <linux/fsnotify.h>
 #include <linux/fs_struct.h>
+#include <linux/pipe_fs_i.h>
 
 #include <asm/uaccess.h>
 #include <asm/mmu_context.h>
@@ -63,6 +64,7 @@
 
 int core_uses_pid;
 char core_pattern[CORENAME_MAX_SIZE] = "core";
+unsigned int core_pipe_limit;
 int suid_dumpable = 0;
 
 /* The maximal length of core_pattern is also specified in sysctl.c */
@@ -1716,7 +1718,7 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
 	char corename[CORENAME_MAX_SIZE + 1];
 	struct mm_struct *mm = current->mm;
 	struct linux_binfmt * binfmt;
-	struct inode * inode;
+	struct inode * inode = NULL;
 	struct file * file;
 	const struct cred *old_cred;
 	struct cred *cred;
@@ -1726,7 +1728,8 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
 	unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
 	char **helper_argv = NULL;
 	int helper_argc = 0;
-	char *delimit;
+	int dump_count = 0;
+	static atomic_t core_dump_count = ATOMIC_INIT(0);
 
 	audit_core_dumps(signr);
 
@@ -1798,22 +1801,31 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
 			goto fail_unlock;
 		}
 
+		dump_count = atomic_inc_return(&core_dump_count);
+		if (core_pipe_limit && (core_pipe_limit < dump_count)) {
+			printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
+			       task_tgid_vnr(current), current->comm);
+			printk(KERN_WARNING "Skipping core dump\n");
+			goto fail_dropcount;
+		}
+
 		helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
 		if (!helper_argv) {
 			printk(KERN_WARNING "%s failed to allocate memory\n",
 			       __func__);
-			goto fail_unlock;
+			goto fail_dropcount;
 		}
 
 		core_limit = RLIM_INFINITY;
 
 		/* SIGPIPE can happen, but it's just never processed */
- 		if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
+ 		if (call_usermodehelper_pipe(helper_argv[0], helper_argv, NULL,
 				&file)) {
  			printk(KERN_INFO "Core dump to %s pipe failed\n",
 			       corename);
- 			goto fail_unlock;
+ 			goto fail_dropcount;
  		}
+		inode = file->f_path.dentry->d_inode;
 	} else {
 		if (core_limit < binfmt->min_coredump)
 			goto fail_unlock;
@@ -1853,6 +1865,12 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
 
 close_fail:
 	filp_close(file, NULL);
+fail_dropcount:
+	if (dump_count) {
+		while (core_pipe_limit && inode->i_pipe->readers)
+			pipe_wait(inode->i_pipe);
+		atomic_dec(&core_dump_count);
+	}
 fail_unlock:
 	if (helper_argv)
 		argv_free(helper_argv);
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 62e4ff9..681052f 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -77,6 +77,7 @@ extern int max_threads;
 extern int core_uses_pid;
 extern int suid_dumpable;
 extern char core_pattern[];
+extern unsigned int core_pipe_limit;
 extern int pid_max;
 extern int min_free_kbytes;
 extern int pid_max_min, pid_max_max;
@@ -407,6 +408,14 @@ static struct ctl_table kern_table[] = {
 		.proc_handler	= &proc_dostring,
 		.strategy	= &sysctl_string,
 	},
+	{
+		.ctl_name	= CTL_UNNUMBERED,
+		.procname	= "core_pipe_limit",
+		.data		= &core_pipe_limit,
+		.maxlen		= sizeof(unsigned int),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec,
+	},
 #ifdef CONFIG_PROC_SYSCTL
 	{
 		.procname	= "tainted",

  parent reply	other threads:[~2009-06-29  0:36 UTC|newest]

Thread overview: 94+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-22 17:28 [PATCH] exec: Make do_coredump more robust and safer when using pipes in core_pattern Neil Horman
2009-06-25 23:30 ` Andrew Morton
2009-06-26  1:49   ` Neil Horman
2009-06-26 10:48   ` Neil Horman
2009-06-26 16:20     ` Andrew Morton
2009-06-26 17:30       ` Neil Horman
2009-06-28 19:31       ` Andi Kleen
2009-06-28 20:52         ` Andrew Morton
2009-06-28 21:00           ` Andi Kleen
2009-06-28 21:18             ` Andrew Morton
2009-06-28 21:50               ` Eric W. Biederman
2009-06-28 21:35           ` Eric W. Biederman
2009-06-28 21:48             ` Andi Kleen
2009-06-28 22:06               ` Eric W. Biederman
2009-06-29  9:15                 ` Andi Kleen
2009-06-28 21:52             ` Andrew Morton
2009-06-26 18:00   ` Neil Horman
2009-06-26 18:02   ` [PATCH 1/2] exec: Make do_coredump more robust and safer when using pipes in core_pattern: recursive dump detection Neil Horman
2009-06-26 16:59     ` Oleg Nesterov
2009-06-26 20:24       ` Neil Horman
2009-06-26 19:14         ` [PATCH 0/2] do_coredump: misc cleanups Oleg Nesterov
2009-06-26 19:14           ` [PATCH 1/2] do_coredump: factor out put_cred() calls Oleg Nesterov
2009-06-26 22:40             ` Roland McGrath
2009-06-26 20:33               ` Oleg Nesterov
2009-06-26 19:16           ` [PATCH 2/2] do_coredump: move !ispipe code into "else" branch Oleg Nesterov
2009-06-26 20:18             ` Q: do_coredump() && d_unhashed() Oleg Nesterov
2009-06-26 22:57           ` [PATCH 0/2] do_coredump: misc cleanups Neil Horman
2009-06-26 19:37     ` [PATCH 1/2] exec: Make do_coredump more robust and safer when using pipes in core_pattern: recursive dump detection Andrew Morton
2009-06-26 20:17       ` Neil Horman
2009-06-26 18:03   ` [PATCH 2/2] exec: Make do_coredump more robust and safer when using pipes in core_pattern: wait for core collectors Neil Horman
2009-06-26 16:48     ` Oleg Nesterov
2009-06-26 20:20       ` Neil Horman
2009-06-29  0:33   ` [PATCH 1/2] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v3) Neil Horman
2009-06-29  0:35   ` Neil Horman [this message]
2009-06-28 22:24     ` [PATCH 2/2] " Oleg Nesterov
2009-06-28 23:24       ` Oleg Nesterov
2009-06-29  2:36       ` Neil Horman
2009-06-28 23:32         ` Oleg Nesterov
2009-06-29 10:21           ` Neil Horman
2009-06-30  0:06             ` Oleg Nesterov
2009-06-29  0:32 ` [PATCH 0/2] " Neil Horman
2009-06-30 17:38 ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v4) Neil Horman
2009-06-30 17:42   ` [PATCH 1/3] exec: Make do_coredump more resilient to recursive crashes (v4) Neil Horman
2009-06-30 17:43   ` [PATCH 2/3] exec: let do_coredump limit the number of concurrent dumps to pipes (v4) Neil Horman
2009-06-30 17:46   ` [PATCH 3/3] exec: Allow do_coredump to wait for user space pipe readers to complete (v4) Neil Horman
2009-07-01  5:52     ` Oleg Nesterov
2009-07-01 10:31       ` Neil Horman
2009-07-01 12:25         ` Oleg Nesterov
2009-07-01 14:12           ` Neil Horman
2009-07-01 14:48             ` Oleg Nesterov
2009-07-01 15:26 ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v5) Neil Horman
2009-07-01 15:30   ` [PATCH 1/3] exec: Make do_coredump more resilient to recursive crashes (v5) Neil Horman
2009-07-01 15:34   ` [PATCH 2/3] exec: let do_coredump limit the number of concurrent dumps to pipes (v5) Neil Horman
2009-07-01 15:37   ` [PATCH 3/3] exec: Allow do_coredump to wait for user space pipe readers to complete (v5) Neil Horman
2009-07-01 16:06     ` Oleg Nesterov
2009-07-01 18:19       ` Neil Horman
2009-07-01 18:28 ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v6) Neil Horman
2009-07-01 18:31   ` [PATCH 1/3] exec: Make do_coredump more resilient to recursive crashes (v6) Neil Horman
2009-07-01 18:32   ` [PATCH 2/3] exec: let do_coredump limit the number of concurrent dumps to pipes (v6) Neil Horman
2009-07-01 18:37   ` [PATCH 3/3] exec: Allow do_coredump to wait for user space pipe readers to complete (v6) Neil Horman
2009-07-02  8:29     ` Oleg Nesterov
2009-07-02 10:29       ` Neil Horman
2009-07-02 11:36         ` Oleg Nesterov
2009-07-02 14:44           ` Neil Horman
2009-07-02 15:37             ` Oleg Nesterov
2009-07-02 17:53               ` Neil Horman
2009-07-03 10:10                 ` Oleg Nesterov
2009-07-02 22:57 ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v7) Neil Horman
2009-07-02 22:59   ` [PATCH 1/3] exec: Make do_coredump more resilient to recursive crashes (v7) Neil Horman
2009-07-02 23:00   ` [PATCH 2/3] exec: let do_coredump limit the number of concurrent dumps to pipes (v7) Neil Horman
2009-07-02 23:01   ` [PATCH 3/3] exec: Allow do_coredump to wait for user space pipe readers to complete (v7) Neil Horman
2009-07-03 10:16     ` Oleg Nesterov
2009-07-03 10:44 ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v8) Neil Horman
2009-07-03 10:50   ` [PATCH 1/3] exec: Make do_coredump more resilient to recursive crashes (v8) Neil Horman
2009-07-07 16:14     ` Neil Horman
2009-07-03 10:51   ` [PATCH 2/3] exec: let do_coredump limit the number of concurrent dumps to pipes (v8) Neil Horman
2009-07-07 16:15     ` Neil Horman
2009-07-03 10:52   ` [PATCH 3/3] exec: Allow do_coredump to wait for user space pipe readers to complete (v8) Neil Horman
2009-07-07 16:19     ` Neil Horman
2009-07-07 16:35       ` Oleg Nesterov
2009-07-07 16:13   ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v8) Neil Horman
2009-07-20 15:49   ` [PATCH 0/3] exec: Make do_coredump more robust and safer when using pipes in core_pattern (v9) Neil Horman
2009-07-20 16:27     ` [PATCH 1/3] exec: Make do_coredump more resilient to recursive crashes (v9) Neil Horman
2009-07-20 16:29     ` [PATCH 2/3] exec: let do_coredump limit the number of concurrent dumps to pipes (v9) Neil Horman
2009-08-07 17:08       ` Randy Dunlap
2009-07-20 16:32     ` [PATCH 3/3] exec: Allow do_coredump to wait for user space pipe readers to complete (v9) Neil Horman
2009-07-29 15:13 ` [PATCH] exec: Make do_coredump more robust and safer when using pipes in core_pattern Scott James Remnant
2009-07-29 20:18   ` Neil Horman
2009-07-31 20:20     ` Scott James Remnant
2009-08-01 13:41       ` Neil Horman
2009-08-01 18:28         ` Scott James Remnant
2009-08-02  0:22           ` Neil Horman
2009-08-02 13:49             ` Scott James Remnant
2009-08-02 23:50               ` Neil Horman

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=20090629003514.GC2479@localhost.localdomain \
    --to=nhorman@tuxdriver.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=andi@firstfloor.org \
    --cc=earl_chew@agilent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    /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