Skip to content

Commit a293980

Browse files
nhormantorvalds
authored andcommitted
exec: let do_coredump() limit the number of concurrent dumps to pipes
Introduce core pipe limiting sysctl. Since we can dump cores to pipe, rather than directly to the filesystem, we create a condition in which a user can create a very high load on the system simply by running bad applications. If the pipe reader specified in core_pattern is poorly written, we can have lots of ourstandig resources and processes in the system. This sysctl introduces an ability to limit that resource consumption. core_pipe_limit defines how many in-flight dumps may be run in parallel, dumps beyond this value are skipped and a note is made in the kernel log. A special value of 0 in core_pipe_limit denotes unlimited core dumps may be handled (this is the default value). [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Neil Horman <nhorman@tuxdriver.com> Reported-by: Earl Chew <earl_chew@agilent.com> Cc: Oleg Nesterov <oleg@tv-sign.ru> Cc: Andi Kleen <andi@firstfloor.org> Cc: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 725eae3 commit a293980

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎Documentation/sysctl/kernel.txt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ show up in /proc/sys/kernel:
2222
- callhome [ S390 only ]
2323
- auto_msgmni
2424
- core_pattern
25+
- core_pipe_limit
2526
- core_uses_pid
2627
- ctrl-alt-del
2728
- dentry-state
@@ -135,6 +136,27 @@ core_pattern is used to specify a core dumpfile pattern name.
135136

136137
==============================================================
137138

139+
core_pipe_limit:
140+
141+
This sysctl is only applicable when core_pattern is configured to pipe core
142+
files to user space helper a (when the first character of core_pattern is a '|',
143+
see above). When collecting cores via a pipe to an application, it is
144+
occasionally usefull for the collecting application to gather data about the
145+
crashing process from its /proc/pid directory. In order to do this safely, the
146+
kernel must wait for the collecting process to exit, so as not to remove the
147+
crashing processes proc files prematurely. This in turn creates the possibility
148+
that a misbehaving userspace collecting process can block the reaping of a
149+
crashed process simply by never exiting. This sysctl defends against that. It
150+
defines how many concurrent crashing processes may be piped to user space
151+
applications in parallel. If this value is exceeded, then those crashing
152+
processes above that value are noted via the kernel log and their cores are
153+
skipped. 0 is a special value, indicating that unlimited processes may be
154+
captured in parallel, but that no waiting will take place (i.e. the collecting
155+
process is not guaranteed access to /proc/<crahing pid>/). This value defaults
156+
to 0.
157+
158+
==============================================================
159+
138160
core_uses_pid:
139161

140162
The default coredump filename is "core". By setting

‎fs/exec.c‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
int core_uses_pid;
6565
char core_pattern[CORENAME_MAX_SIZE] = "core";
66+
unsigned int core_pipe_limit;
6667
int suid_dumpable = 0;
6768

6869
/* The maximal length of core_pattern is also specified in sysctl.c */
@@ -1744,7 +1745,8 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
17441745
unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
17451746
char **helper_argv = NULL;
17461747
int helper_argc = 0;
1747-
char *delimit;
1748+
int dump_count = 0;
1749+
static atomic_t core_dump_count = ATOMIC_INIT(0);
17481750

17491751
audit_core_dumps(signr);
17501752

@@ -1826,28 +1828,36 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
18261828
goto fail_unlock;
18271829
}
18281830

1831+
dump_count = atomic_inc_return(&core_dump_count);
1832+
if (core_pipe_limit && (core_pipe_limit < dump_count)) {
1833+
printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
1834+
task_tgid_vnr(current), current->comm);
1835+
printk(KERN_WARNING "Skipping core dump\n");
1836+
goto fail_dropcount;
1837+
}
1838+
18291839
helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
18301840
if (!helper_argv) {
18311841
printk(KERN_WARNING "%s failed to allocate memory\n",
18321842
__func__);
1833-
goto fail_unlock;
1843+
goto fail_dropcount;
18341844
}
18351845

18361846
core_limit = RLIM_INFINITY;
18371847

18381848
/* SIGPIPE can happen, but it's just never processed */
1839-
if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
1849+
if (call_usermodehelper_pipe(helper_argv[0], helper_argv, NULL,
18401850
&file)) {
18411851
printk(KERN_INFO "Core dump to %s pipe failed\n",
18421852
corename);
1843-
goto fail_unlock;
1853+
goto fail_dropcount;
18441854
}
18451855
} else
18461856
file = filp_open(corename,
18471857
O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
18481858
0600);
18491859
if (IS_ERR(file))
1850-
goto fail_unlock;
1860+
goto fail_dropcount;
18511861
inode = file->f_path.dentry->d_inode;
18521862
if (inode->i_nlink > 1)
18531863
goto close_fail; /* multiple links - don't dump */
@@ -1877,6 +1887,9 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
18771887
current->signal->group_exit_code |= 0x80;
18781888
close_fail:
18791889
filp_close(file, NULL);
1890+
fail_dropcount:
1891+
if (dump_count)
1892+
atomic_dec(&core_dump_count);
18801893
fail_unlock:
18811894
if (helper_argv)
18821895
argv_free(helper_argv);

‎kernel/sysctl.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extern int max_threads;
7676
extern int core_uses_pid;
7777
extern int suid_dumpable;
7878
extern char core_pattern[];
79+
extern unsigned int core_pipe_limit;
7980
extern int pid_max;
8081
extern int min_free_kbytes;
8182
extern int pid_max_min, pid_max_max;
@@ -423,6 +424,14 @@ static struct ctl_table kern_table[] = {
423424
.proc_handler = &proc_dostring,
424425
.strategy = &sysctl_string,
425426
},
427+
{
428+
.ctl_name = CTL_UNNUMBERED,
429+
.procname = "core_pipe_limit",
430+
.data = &core_pipe_limit,
431+
.maxlen = sizeof(unsigned int),
432+
.mode = 0644,
433+
.proc_handler = &proc_dointvec,
434+
},
426435
#ifdef CONFIG_PROC_SYSCTL
427436
{
428437
.procname = "tainted",

0 commit comments

Comments
 (0)