Skip to content

Commit f0bc21b

Browse files
nixiaomingtorvalds
authored andcommitted
fs/coredump: move coredump sysctls into its own file
This moves the fs/coredump.c respective sysctls to its own file. Link: https://lkml.kernel.org/r/20211129211943.640266-6-mcgrof@kernel.org Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com> Signed-off-by: Luis Chamberlain <mcgrof@kernel.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> Cc: Antti Palosaari <crope@iki.fi> Cc: Christian Brauner <christian.brauner@ubuntu.com> Cc: "David S. Miller" <davem@davemloft.net> Cc: Eric Biederman <ebiederm@xmission.com> Cc: Eric Biggers <ebiggers@google.com> Cc: Iurii Zaikin <yzaikin@google.com> Cc: Kees Cook <keescook@chromium.org> Cc: Lukas Middendorf <kernel@tuxforce.de> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com> Cc: Stephen Kitt <steve@sk2.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fdcd407 commit f0bc21b

4 files changed

Lines changed: 67 additions & 66 deletions

File tree

fs/coredump.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/fs.h>
4242
#include <linux/path.h>
4343
#include <linux/timekeeping.h>
44+
#include <linux/sysctl.h>
4445

4546
#include <linux/uaccess.h>
4647
#include <asm/mmu_context.h>
@@ -52,18 +53,16 @@
5253

5354
#include <trace/events/sched.h>
5455

55-
int core_uses_pid;
56-
unsigned int core_pipe_limit;
57-
char core_pattern[CORENAME_MAX_SIZE] = "core";
56+
static int core_uses_pid;
57+
static unsigned int core_pipe_limit;
58+
static char core_pattern[CORENAME_MAX_SIZE] = "core";
5859
static int core_name_size = CORENAME_MAX_SIZE;
5960

6061
struct core_name {
6162
char *corename;
6263
int used, size;
6364
};
6465

65-
/* The maximal length of core_pattern is also specified in sysctl.c */
66-
6766
static int expand_corename(struct core_name *cn, int size)
6867
{
6968
char *corename = krealloc(cn->corename, size, GFP_KERNEL);
@@ -893,6 +892,63 @@ int dump_align(struct coredump_params *cprm, int align)
893892
}
894893
EXPORT_SYMBOL(dump_align);
895894

895+
#ifdef CONFIG_SYSCTL
896+
897+
void validate_coredump_safety(void)
898+
{
899+
if (suid_dumpable == SUID_DUMP_ROOT &&
900+
core_pattern[0] != '/' && core_pattern[0] != '|') {
901+
pr_warn(
902+
"Unsafe core_pattern used with fs.suid_dumpable=2.\n"
903+
"Pipe handler or fully qualified core dump path required.\n"
904+
"Set kernel.core_pattern before fs.suid_dumpable.\n"
905+
);
906+
}
907+
}
908+
909+
static int proc_dostring_coredump(struct ctl_table *table, int write,
910+
void *buffer, size_t *lenp, loff_t *ppos)
911+
{
912+
int error = proc_dostring(table, write, buffer, lenp, ppos);
913+
914+
if (!error)
915+
validate_coredump_safety();
916+
return error;
917+
}
918+
919+
static struct ctl_table coredump_sysctls[] = {
920+
{
921+
.procname = "core_uses_pid",
922+
.data = &core_uses_pid,
923+
.maxlen = sizeof(int),
924+
.mode = 0644,
925+
.proc_handler = proc_dointvec,
926+
},
927+
{
928+
.procname = "core_pattern",
929+
.data = core_pattern,
930+
.maxlen = CORENAME_MAX_SIZE,
931+
.mode = 0644,
932+
.proc_handler = proc_dostring_coredump,
933+
},
934+
{
935+
.procname = "core_pipe_limit",
936+
.data = &core_pipe_limit,
937+
.maxlen = sizeof(unsigned int),
938+
.mode = 0644,
939+
.proc_handler = proc_dointvec,
940+
},
941+
{ }
942+
};
943+
944+
static int __init init_fs_coredump_sysctls(void)
945+
{
946+
register_sysctl_init("kernel", coredump_sysctls);
947+
return 0;
948+
}
949+
fs_initcall(init_fs_coredump_sysctls);
950+
#endif /* CONFIG_SYSCTL */
951+
896952
/*
897953
* The purpose of always_dump_vma() is to make sure that special kernel mappings
898954
* that are useful for post-mortem analysis are included in every core dump.

fs/exec.c

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,20 +2103,6 @@ COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
21032103

21042104
#ifdef CONFIG_SYSCTL
21052105

2106-
static void validate_coredump_safety(void)
2107-
{
2108-
#ifdef CONFIG_COREDUMP
2109-
if (suid_dumpable == SUID_DUMP_ROOT &&
2110-
core_pattern[0] != '/' && core_pattern[0] != '|') {
2111-
pr_warn(
2112-
"Unsafe core_pattern used with fs.suid_dumpable=2.\n"
2113-
"Pipe handler or fully qualified core dump path required.\n"
2114-
"Set kernel.core_pattern before fs.suid_dumpable.\n"
2115-
);
2116-
}
2117-
#endif
2118-
}
2119-
21202106
static int proc_dointvec_minmax_coredump(struct ctl_table *table, int write,
21212107
void *buffer, size_t *lenp, loff_t *ppos)
21222108
{
@@ -2140,50 +2126,9 @@ static struct ctl_table fs_exec_sysctls[] = {
21402126
{ }
21412127
};
21422128

2143-
#ifdef CONFIG_COREDUMP
2144-
2145-
static int proc_dostring_coredump(struct ctl_table *table, int write,
2146-
void *buffer, size_t *lenp, loff_t *ppos)
2147-
{
2148-
int error = proc_dostring(table, write, buffer, lenp, ppos);
2149-
2150-
if (!error)
2151-
validate_coredump_safety();
2152-
return error;
2153-
}
2154-
2155-
static struct ctl_table kernel_exec_sysctls[] = {
2156-
{
2157-
.procname = "core_uses_pid",
2158-
.data = &core_uses_pid,
2159-
.maxlen = sizeof(int),
2160-
.mode = 0644,
2161-
.proc_handler = proc_dointvec,
2162-
},
2163-
{
2164-
.procname = "core_pattern",
2165-
.data = core_pattern,
2166-
.maxlen = CORENAME_MAX_SIZE,
2167-
.mode = 0644,
2168-
.proc_handler = proc_dostring_coredump,
2169-
},
2170-
{
2171-
.procname = "core_pipe_limit",
2172-
.data = &core_pipe_limit,
2173-
.maxlen = sizeof(unsigned int),
2174-
.mode = 0644,
2175-
.proc_handler = proc_dointvec,
2176-
},
2177-
{ }
2178-
};
2179-
#endif
2180-
21812129
static int __init init_fs_exec_sysctls(void)
21822130
{
21832131
register_sysctl_init("fs", fs_exec_sysctls);
2184-
#ifdef CONFIG_COREDUMP
2185-
register_sysctl_init("kernel", kernel_exec_sysctls);
2186-
#endif
21872132
return 0;
21882133
}
21892134

include/linux/coredump.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ struct core_vma_metadata {
1414
unsigned long dump_size;
1515
};
1616

17-
extern int core_uses_pid;
18-
extern char core_pattern[];
19-
extern unsigned int core_pipe_limit;
20-
2117
/*
2218
* These are the only things you should do on a core-file: use only these
2319
* functions to write out all the necessary info.
@@ -37,4 +33,10 @@ extern void do_coredump(const kernel_siginfo_t *siginfo);
3733
static inline void do_coredump(const kernel_siginfo_t *siginfo) {}
3834
#endif
3935

36+
#if defined(CONFIG_COREDUMP) && defined(CONFIG_SYSCTL)
37+
extern void validate_coredump_safety(void);
38+
#else
39+
static inline void validate_coredump_safety(void) {}
40+
#endif
41+
4042
#endif /* _LINUX_COREDUMP_H */

kernel/sysctl.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@
6262
#include <linux/capability.h>
6363
#include <linux/binfmts.h>
6464
#include <linux/sched/sysctl.h>
65-
#include <linux/sched/coredump.h>
6665
#include <linux/kexec.h>
6766
#include <linux/bpf.h>
6867
#include <linux/mount.h>
6968
#include <linux/userfaultfd_k.h>
70-
#include <linux/coredump.h>
7169
#include <linux/latencytop.h>
7270
#include <linux/pid.h>
7371
#include <linux/delayacct.h>

0 commit comments

Comments
 (0)