xref: /openbmc/linux/fs/coredump.c (revision f9010dbd)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
210c28d93SAlex Kelly #include <linux/slab.h>
310c28d93SAlex Kelly #include <linux/file.h>
410c28d93SAlex Kelly #include <linux/fdtable.h>
570d78fe7SAndrey Ryabinin #include <linux/freezer.h>
610c28d93SAlex Kelly #include <linux/mm.h>
710c28d93SAlex Kelly #include <linux/stat.h>
810c28d93SAlex Kelly #include <linux/fcntl.h>
910c28d93SAlex Kelly #include <linux/swap.h>
10315c6926SPaul Wise #include <linux/ctype.h>
1110c28d93SAlex Kelly #include <linux/string.h>
1210c28d93SAlex Kelly #include <linux/init.h>
1310c28d93SAlex Kelly #include <linux/pagemap.h>
1410c28d93SAlex Kelly #include <linux/perf_event.h>
1510c28d93SAlex Kelly #include <linux/highmem.h>
1610c28d93SAlex Kelly #include <linux/spinlock.h>
1710c28d93SAlex Kelly #include <linux/key.h>
1810c28d93SAlex Kelly #include <linux/personality.h>
1910c28d93SAlex Kelly #include <linux/binfmts.h>
20179899fdSAlex Kelly #include <linux/coredump.h>
21f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
223f07c014SIngo Molnar #include <linux/sched/signal.h>
2368db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
2410c28d93SAlex Kelly #include <linux/utsname.h>
2510c28d93SAlex Kelly #include <linux/pid_namespace.h>
2610c28d93SAlex Kelly #include <linux/module.h>
2710c28d93SAlex Kelly #include <linux/namei.h>
2810c28d93SAlex Kelly #include <linux/mount.h>
2910c28d93SAlex Kelly #include <linux/security.h>
3010c28d93SAlex Kelly #include <linux/syscalls.h>
3110c28d93SAlex Kelly #include <linux/tsacct_kern.h>
3210c28d93SAlex Kelly #include <linux/cn_proc.h>
3310c28d93SAlex Kelly #include <linux/audit.h>
3410c28d93SAlex Kelly #include <linux/kmod.h>
3510c28d93SAlex Kelly #include <linux/fsnotify.h>
3610c28d93SAlex Kelly #include <linux/fs_struct.h>
3710c28d93SAlex Kelly #include <linux/pipe_fs_i.h>
3810c28d93SAlex Kelly #include <linux/oom.h>
3910c28d93SAlex Kelly #include <linux/compat.h>
40378c6520SJann Horn #include <linux/fs.h>
41378c6520SJann Horn #include <linux/path.h>
4203927c8aSArnd Bergmann #include <linux/timekeeping.h>
43f0bc21b2SXiaoming Ni #include <linux/sysctl.h>
4484158b7fSJann Horn #include <linux/elf.h>
4510c28d93SAlex Kelly 
467c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
4710c28d93SAlex Kelly #include <asm/mmu_context.h>
4810c28d93SAlex Kelly #include <asm/tlb.h>
4910c28d93SAlex Kelly #include <asm/exec.h>
5010c28d93SAlex Kelly 
5110c28d93SAlex Kelly #include <trace/events/task.h>
5210c28d93SAlex Kelly #include "internal.h"
5310c28d93SAlex Kelly 
5410c28d93SAlex Kelly #include <trace/events/sched.h>
5510c28d93SAlex Kelly 
5695c5436aSEric W. Biederman static bool dump_vma_snapshot(struct coredump_params *cprm);
57390031c9SEric W. Biederman static void free_vma_snapshot(struct coredump_params *cprm);
5895c5436aSEric W. Biederman 
59f0bc21b2SXiaoming Ni static int core_uses_pid;
60f0bc21b2SXiaoming Ni static unsigned int core_pipe_limit;
61f0bc21b2SXiaoming Ni static char core_pattern[CORENAME_MAX_SIZE] = "core";
623ceadcf6SOleg Nesterov static int core_name_size = CORENAME_MAX_SIZE;
6310c28d93SAlex Kelly 
6410c28d93SAlex Kelly struct core_name {
6510c28d93SAlex Kelly 	char *corename;
6610c28d93SAlex Kelly 	int used, size;
6710c28d93SAlex Kelly };
6810c28d93SAlex Kelly 
expand_corename(struct core_name * cn,int size)693ceadcf6SOleg Nesterov static int expand_corename(struct core_name *cn, int size)
7010c28d93SAlex Kelly {
716dd142d9SKees Cook 	char *corename;
726dd142d9SKees Cook 
736dd142d9SKees Cook 	size = kmalloc_size_roundup(size);
746dd142d9SKees Cook 	corename = krealloc(cn->corename, size, GFP_KERNEL);
7510c28d93SAlex Kelly 
76e7fd1549SOleg Nesterov 	if (!corename)
7710c28d93SAlex Kelly 		return -ENOMEM;
7810c28d93SAlex Kelly 
793ceadcf6SOleg Nesterov 	if (size > core_name_size) /* racy but harmless */
803ceadcf6SOleg Nesterov 		core_name_size = size;
813ceadcf6SOleg Nesterov 
826dd142d9SKees Cook 	cn->size = size;
83e7fd1549SOleg Nesterov 	cn->corename = corename;
8410c28d93SAlex Kelly 	return 0;
8510c28d93SAlex Kelly }
8610c28d93SAlex Kelly 
cn_vprintf(struct core_name * cn,const char * fmt,va_list arg)87b4176b7cSNicolas Iooss static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
88b4176b7cSNicolas Iooss 				     va_list arg)
8910c28d93SAlex Kelly {
905fe9d8caSOleg Nesterov 	int free, need;
91404ca80eSEric Dumazet 	va_list arg_copy;
9210c28d93SAlex Kelly 
935fe9d8caSOleg Nesterov again:
945fe9d8caSOleg Nesterov 	free = cn->size - cn->used;
95404ca80eSEric Dumazet 
96404ca80eSEric Dumazet 	va_copy(arg_copy, arg);
97404ca80eSEric Dumazet 	need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
98404ca80eSEric Dumazet 	va_end(arg_copy);
99404ca80eSEric Dumazet 
1005fe9d8caSOleg Nesterov 	if (need < free) {
10110c28d93SAlex Kelly 		cn->used += need;
10210c28d93SAlex Kelly 		return 0;
1035fe9d8caSOleg Nesterov 	}
10410c28d93SAlex Kelly 
1053ceadcf6SOleg Nesterov 	if (!expand_corename(cn, cn->size + need - free + 1))
1065fe9d8caSOleg Nesterov 		goto again;
1075fe9d8caSOleg Nesterov 
1085fe9d8caSOleg Nesterov 	return -ENOMEM;
10910c28d93SAlex Kelly }
11010c28d93SAlex Kelly 
cn_printf(struct core_name * cn,const char * fmt,...)111b4176b7cSNicolas Iooss static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
112bc03c691SOleg Nesterov {
113bc03c691SOleg Nesterov 	va_list arg;
114bc03c691SOleg Nesterov 	int ret;
115bc03c691SOleg Nesterov 
116bc03c691SOleg Nesterov 	va_start(arg, fmt);
117bc03c691SOleg Nesterov 	ret = cn_vprintf(cn, fmt, arg);
118bc03c691SOleg Nesterov 	va_end(arg);
119bc03c691SOleg Nesterov 
120bc03c691SOleg Nesterov 	return ret;
121bc03c691SOleg Nesterov }
122bc03c691SOleg Nesterov 
123b4176b7cSNicolas Iooss static __printf(2, 3)
cn_esc_printf(struct core_name * cn,const char * fmt,...)124b4176b7cSNicolas Iooss int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
12510c28d93SAlex Kelly {
126923bed03SOleg Nesterov 	int cur = cn->used;
127923bed03SOleg Nesterov 	va_list arg;
128923bed03SOleg Nesterov 	int ret;
129923bed03SOleg Nesterov 
130923bed03SOleg Nesterov 	va_start(arg, fmt);
131923bed03SOleg Nesterov 	ret = cn_vprintf(cn, fmt, arg);
132923bed03SOleg Nesterov 	va_end(arg);
133923bed03SOleg Nesterov 
134ac94b6e3SJann Horn 	if (ret == 0) {
135ac94b6e3SJann Horn 		/*
136ac94b6e3SJann Horn 		 * Ensure that this coredump name component can't cause the
137ac94b6e3SJann Horn 		 * resulting corefile path to consist of a ".." or ".".
138ac94b6e3SJann Horn 		 */
139ac94b6e3SJann Horn 		if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
140ac94b6e3SJann Horn 				(cn->used - cur == 2 && cn->corename[cur] == '.'
141ac94b6e3SJann Horn 				&& cn->corename[cur+1] == '.'))
142ac94b6e3SJann Horn 			cn->corename[cur] = '!';
143ac94b6e3SJann Horn 
144ac94b6e3SJann Horn 		/*
145ac94b6e3SJann Horn 		 * Empty names are fishy and could be used to create a "//" in a
146ac94b6e3SJann Horn 		 * corefile name, causing the coredump to happen one directory
147ac94b6e3SJann Horn 		 * level too high. Enforce that all components of the core
148ac94b6e3SJann Horn 		 * pattern are at least one character long.
149ac94b6e3SJann Horn 		 */
150ac94b6e3SJann Horn 		if (cn->used == cur)
151ac94b6e3SJann Horn 			ret = cn_printf(cn, "!");
152ac94b6e3SJann Horn 	}
153ac94b6e3SJann Horn 
154923bed03SOleg Nesterov 	for (; cur < cn->used; ++cur) {
155923bed03SOleg Nesterov 		if (cn->corename[cur] == '/')
156923bed03SOleg Nesterov 			cn->corename[cur] = '!';
157923bed03SOleg Nesterov 	}
158923bed03SOleg Nesterov 	return ret;
15910c28d93SAlex Kelly }
16010c28d93SAlex Kelly 
cn_print_exe_file(struct core_name * cn,bool name_only)161f38c85f1SLepton Wu static int cn_print_exe_file(struct core_name *cn, bool name_only)
16210c28d93SAlex Kelly {
16310c28d93SAlex Kelly 	struct file *exe_file;
164f38c85f1SLepton Wu 	char *pathbuf, *path, *ptr;
16510c28d93SAlex Kelly 	int ret;
16610c28d93SAlex Kelly 
16710c28d93SAlex Kelly 	exe_file = get_mm_exe_file(current->mm);
168923bed03SOleg Nesterov 	if (!exe_file)
169923bed03SOleg Nesterov 		return cn_esc_printf(cn, "%s (path unknown)", current->comm);
17010c28d93SAlex Kelly 
1710ee931c4SMichal Hocko 	pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
17210c28d93SAlex Kelly 	if (!pathbuf) {
17310c28d93SAlex Kelly 		ret = -ENOMEM;
17410c28d93SAlex Kelly 		goto put_exe_file;
17510c28d93SAlex Kelly 	}
17610c28d93SAlex Kelly 
1779bf39ab2SMiklos Szeredi 	path = file_path(exe_file, pathbuf, PATH_MAX);
17810c28d93SAlex Kelly 	if (IS_ERR(path)) {
17910c28d93SAlex Kelly 		ret = PTR_ERR(path);
18010c28d93SAlex Kelly 		goto free_buf;
18110c28d93SAlex Kelly 	}
18210c28d93SAlex Kelly 
183f38c85f1SLepton Wu 	if (name_only) {
184f38c85f1SLepton Wu 		ptr = strrchr(path, '/');
185f38c85f1SLepton Wu 		if (ptr)
186f38c85f1SLepton Wu 			path = ptr + 1;
187f38c85f1SLepton Wu 	}
188923bed03SOleg Nesterov 	ret = cn_esc_printf(cn, "%s", path);
18910c28d93SAlex Kelly 
19010c28d93SAlex Kelly free_buf:
19110c28d93SAlex Kelly 	kfree(pathbuf);
19210c28d93SAlex Kelly put_exe_file:
19310c28d93SAlex Kelly 	fput(exe_file);
19410c28d93SAlex Kelly 	return ret;
19510c28d93SAlex Kelly }
19610c28d93SAlex Kelly 
19710c28d93SAlex Kelly /* format_corename will inspect the pattern parameter, and output a
19810c28d93SAlex Kelly  * name into corename, which must have space for at least
19910c28d93SAlex Kelly  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
20010c28d93SAlex Kelly  */
format_corename(struct core_name * cn,struct coredump_params * cprm,size_t ** argv,int * argc)201315c6926SPaul Wise static int format_corename(struct core_name *cn, struct coredump_params *cprm,
202315c6926SPaul Wise 			   size_t **argv, int *argc)
20310c28d93SAlex Kelly {
20410c28d93SAlex Kelly 	const struct cred *cred = current_cred();
20510c28d93SAlex Kelly 	const char *pat_ptr = core_pattern;
20610c28d93SAlex Kelly 	int ispipe = (*pat_ptr == '|');
207315c6926SPaul Wise 	bool was_space = false;
20810c28d93SAlex Kelly 	int pid_in_pattern = 0;
20910c28d93SAlex Kelly 	int err = 0;
21010c28d93SAlex Kelly 
211e7fd1549SOleg Nesterov 	cn->used = 0;
2123ceadcf6SOleg Nesterov 	cn->corename = NULL;
2133ceadcf6SOleg Nesterov 	if (expand_corename(cn, core_name_size))
21410c28d93SAlex Kelly 		return -ENOMEM;
215888ffc59SOleg Nesterov 	cn->corename[0] = '\0';
216888ffc59SOleg Nesterov 
217315c6926SPaul Wise 	if (ispipe) {
218315c6926SPaul Wise 		int argvs = sizeof(core_pattern) / 2;
219315c6926SPaul Wise 		(*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
220315c6926SPaul Wise 		if (!(*argv))
221315c6926SPaul Wise 			return -ENOMEM;
222315c6926SPaul Wise 		(*argv)[(*argc)++] = 0;
223888ffc59SOleg Nesterov 		++pat_ptr;
224db973a72SSudip Mukherjee 		if (!(*pat_ptr))
225db973a72SSudip Mukherjee 			return -ENOMEM;
226315c6926SPaul Wise 	}
22710c28d93SAlex Kelly 
22810c28d93SAlex Kelly 	/* Repeat as long as we have more pattern to process and more output
22910c28d93SAlex Kelly 	   space */
23010c28d93SAlex Kelly 	while (*pat_ptr) {
231315c6926SPaul Wise 		/*
232315c6926SPaul Wise 		 * Split on spaces before doing template expansion so that
233315c6926SPaul Wise 		 * %e and %E don't get split if they have spaces in them
234315c6926SPaul Wise 		 */
235315c6926SPaul Wise 		if (ispipe) {
236315c6926SPaul Wise 			if (isspace(*pat_ptr)) {
2372bf509d9SMenglong Dong 				if (cn->used != 0)
238315c6926SPaul Wise 					was_space = true;
239315c6926SPaul Wise 				pat_ptr++;
240315c6926SPaul Wise 				continue;
241315c6926SPaul Wise 			} else if (was_space) {
242315c6926SPaul Wise 				was_space = false;
243315c6926SPaul Wise 				err = cn_printf(cn, "%c", '\0');
244315c6926SPaul Wise 				if (err)
245315c6926SPaul Wise 					return err;
246315c6926SPaul Wise 				(*argv)[(*argc)++] = cn->used;
247315c6926SPaul Wise 			}
248315c6926SPaul Wise 		}
24910c28d93SAlex Kelly 		if (*pat_ptr != '%') {
25010c28d93SAlex Kelly 			err = cn_printf(cn, "%c", *pat_ptr++);
25110c28d93SAlex Kelly 		} else {
25210c28d93SAlex Kelly 			switch (*++pat_ptr) {
25310c28d93SAlex Kelly 			/* single % at the end, drop that */
25410c28d93SAlex Kelly 			case 0:
25510c28d93SAlex Kelly 				goto out;
25610c28d93SAlex Kelly 			/* Double percent, output one percent */
25710c28d93SAlex Kelly 			case '%':
25810c28d93SAlex Kelly 				err = cn_printf(cn, "%c", '%');
25910c28d93SAlex Kelly 				break;
26010c28d93SAlex Kelly 			/* pid */
26110c28d93SAlex Kelly 			case 'p':
26210c28d93SAlex Kelly 				pid_in_pattern = 1;
26310c28d93SAlex Kelly 				err = cn_printf(cn, "%d",
26410c28d93SAlex Kelly 					      task_tgid_vnr(current));
26510c28d93SAlex Kelly 				break;
26665aafb1eSStéphane Graber 			/* global pid */
26765aafb1eSStéphane Graber 			case 'P':
26865aafb1eSStéphane Graber 				err = cn_printf(cn, "%d",
26965aafb1eSStéphane Graber 					      task_tgid_nr(current));
27065aafb1eSStéphane Graber 				break;
271b03023ecSOleg Nesterov 			case 'i':
272b03023ecSOleg Nesterov 				err = cn_printf(cn, "%d",
273b03023ecSOleg Nesterov 					      task_pid_vnr(current));
274b03023ecSOleg Nesterov 				break;
275b03023ecSOleg Nesterov 			case 'I':
276b03023ecSOleg Nesterov 				err = cn_printf(cn, "%d",
277b03023ecSOleg Nesterov 					      task_pid_nr(current));
278b03023ecSOleg Nesterov 				break;
27910c28d93SAlex Kelly 			/* uid */
28010c28d93SAlex Kelly 			case 'u':
2815202efe5SNicolas Iooss 				err = cn_printf(cn, "%u",
2825202efe5SNicolas Iooss 						from_kuid(&init_user_ns,
2835202efe5SNicolas Iooss 							  cred->uid));
28410c28d93SAlex Kelly 				break;
28510c28d93SAlex Kelly 			/* gid */
28610c28d93SAlex Kelly 			case 'g':
2875202efe5SNicolas Iooss 				err = cn_printf(cn, "%u",
2885202efe5SNicolas Iooss 						from_kgid(&init_user_ns,
2895202efe5SNicolas Iooss 							  cred->gid));
29010c28d93SAlex Kelly 				break;
29112a2b4b2SOleg Nesterov 			case 'd':
29212a2b4b2SOleg Nesterov 				err = cn_printf(cn, "%d",
29312a2b4b2SOleg Nesterov 					__get_dumpable(cprm->mm_flags));
29412a2b4b2SOleg Nesterov 				break;
29510c28d93SAlex Kelly 			/* signal that caused the coredump */
29610c28d93SAlex Kelly 			case 's':
297b4176b7cSNicolas Iooss 				err = cn_printf(cn, "%d",
298b4176b7cSNicolas Iooss 						cprm->siginfo->si_signo);
29910c28d93SAlex Kelly 				break;
30010c28d93SAlex Kelly 			/* UNIX time of coredump */
30110c28d93SAlex Kelly 			case 't': {
30203927c8aSArnd Bergmann 				time64_t time;
30303927c8aSArnd Bergmann 
30403927c8aSArnd Bergmann 				time = ktime_get_real_seconds();
30503927c8aSArnd Bergmann 				err = cn_printf(cn, "%lld", time);
30610c28d93SAlex Kelly 				break;
30710c28d93SAlex Kelly 			}
30810c28d93SAlex Kelly 			/* hostname */
309923bed03SOleg Nesterov 			case 'h':
31010c28d93SAlex Kelly 				down_read(&uts_sem);
311923bed03SOleg Nesterov 				err = cn_esc_printf(cn, "%s",
31210c28d93SAlex Kelly 					      utsname()->nodename);
31310c28d93SAlex Kelly 				up_read(&uts_sem);
31410c28d93SAlex Kelly 				break;
315f38c85f1SLepton Wu 			/* executable, could be changed by prctl PR_SET_NAME etc */
316923bed03SOleg Nesterov 			case 'e':
317923bed03SOleg Nesterov 				err = cn_esc_printf(cn, "%s", current->comm);
31810c28d93SAlex Kelly 				break;
319f38c85f1SLepton Wu 			/* file name of executable */
320f38c85f1SLepton Wu 			case 'f':
321f38c85f1SLepton Wu 				err = cn_print_exe_file(cn, true);
322f38c85f1SLepton Wu 				break;
32310c28d93SAlex Kelly 			case 'E':
324f38c85f1SLepton Wu 				err = cn_print_exe_file(cn, false);
32510c28d93SAlex Kelly 				break;
32610c28d93SAlex Kelly 			/* core limit size */
32710c28d93SAlex Kelly 			case 'c':
32810c28d93SAlex Kelly 				err = cn_printf(cn, "%lu",
32910c28d93SAlex Kelly 					      rlimit(RLIMIT_CORE));
33010c28d93SAlex Kelly 				break;
3318603b6f5SOleksandr Natalenko 			/* CPU the task ran on */
3328603b6f5SOleksandr Natalenko 			case 'C':
3338603b6f5SOleksandr Natalenko 				err = cn_printf(cn, "%d", cprm->cpu);
3348603b6f5SOleksandr Natalenko 				break;
33510c28d93SAlex Kelly 			default:
33610c28d93SAlex Kelly 				break;
33710c28d93SAlex Kelly 			}
33810c28d93SAlex Kelly 			++pat_ptr;
33910c28d93SAlex Kelly 		}
34010c28d93SAlex Kelly 
34110c28d93SAlex Kelly 		if (err)
34210c28d93SAlex Kelly 			return err;
34310c28d93SAlex Kelly 	}
34410c28d93SAlex Kelly 
345888ffc59SOleg Nesterov out:
34610c28d93SAlex Kelly 	/* Backward compatibility with core_uses_pid:
34710c28d93SAlex Kelly 	 *
34810c28d93SAlex Kelly 	 * If core_pattern does not include a %p (as is the default)
34910c28d93SAlex Kelly 	 * and core_uses_pid is set, then .%pid will be appended to
35010c28d93SAlex Kelly 	 * the filename. Do not do this for piped commands. */
35110c28d93SAlex Kelly 	if (!ispipe && !pid_in_pattern && core_uses_pid) {
35210c28d93SAlex Kelly 		err = cn_printf(cn, ".%d", task_tgid_vnr(current));
35310c28d93SAlex Kelly 		if (err)
35410c28d93SAlex Kelly 			return err;
35510c28d93SAlex Kelly 	}
35610c28d93SAlex Kelly 	return ispipe;
35710c28d93SAlex Kelly }
35810c28d93SAlex Kelly 
zap_process(struct task_struct * start,int exit_code)359752dc970SEric W. Biederman static int zap_process(struct task_struct *start, int exit_code)
36010c28d93SAlex Kelly {
36110c28d93SAlex Kelly 	struct task_struct *t;
36210c28d93SAlex Kelly 	int nr = 0;
36310c28d93SAlex Kelly 
3649a95f78eSEric W. Biederman 	/* Allow SIGKILL, see prepare_signal() */
3652f824d4dSEric W. Biederman 	start->signal->flags = SIGNAL_GROUP_EXIT;
36610c28d93SAlex Kelly 	start->signal->group_exit_code = exit_code;
36710c28d93SAlex Kelly 	start->signal->group_stop_count = 0;
36810c28d93SAlex Kelly 
369d61ba589SOleg Nesterov 	for_each_thread(start, t) {
37010c28d93SAlex Kelly 		task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
37192307383SEric W. Biederman 		if (t != current && !(t->flags & PF_POSTCOREDUMP)) {
37210c28d93SAlex Kelly 			sigaddset(&t->pending.signal, SIGKILL);
37310c28d93SAlex Kelly 			signal_wake_up(t, 1);
374*f9010dbdSMike Christie 			/* The vhost_worker does not particpate in coredumps */
375*f9010dbdSMike Christie 			if ((t->flags & (PF_USER_WORKER | PF_IO_WORKER)) != PF_USER_WORKER)
37610c28d93SAlex Kelly 				nr++;
37710c28d93SAlex Kelly 		}
378d61ba589SOleg Nesterov 	}
37910c28d93SAlex Kelly 
38010c28d93SAlex Kelly 	return nr;
38110c28d93SAlex Kelly }
38210c28d93SAlex Kelly 
zap_threads(struct task_struct * tsk,struct core_state * core_state,int exit_code)3830258b5fdSEric W. Biederman static int zap_threads(struct task_struct *tsk,
38410c28d93SAlex Kelly 			struct core_state *core_state, int exit_code)
38510c28d93SAlex Kelly {
38649697335SEric W. Biederman 	struct signal_struct *signal = tsk->signal;
38710c28d93SAlex Kelly 	int nr = -EAGAIN;
38810c28d93SAlex Kelly 
38910c28d93SAlex Kelly 	spin_lock_irq(&tsk->sighand->siglock);
39049697335SEric W. Biederman 	if (!(signal->flags & SIGNAL_GROUP_EXIT) && !signal->group_exec_task) {
39149697335SEric W. Biederman 		signal->core_state = core_state;
392752dc970SEric W. Biederman 		nr = zap_process(tsk, exit_code);
393403bad72SOleg Nesterov 		clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
3940258b5fdSEric W. Biederman 		tsk->flags |= PF_DUMPCORE;
3950258b5fdSEric W. Biederman 		atomic_set(&core_state->nr_threads, nr);
39610c28d93SAlex Kelly 	}
39710c28d93SAlex Kelly 	spin_unlock_irq(&tsk->sighand->siglock);
39810c28d93SAlex Kelly 	return nr;
39910c28d93SAlex Kelly }
40010c28d93SAlex Kelly 
coredump_wait(int exit_code,struct core_state * core_state)40110c28d93SAlex Kelly static int coredump_wait(int exit_code, struct core_state *core_state)
40210c28d93SAlex Kelly {
40310c28d93SAlex Kelly 	struct task_struct *tsk = current;
40410c28d93SAlex Kelly 	int core_waiters = -EBUSY;
40510c28d93SAlex Kelly 
40610c28d93SAlex Kelly 	init_completion(&core_state->startup);
40710c28d93SAlex Kelly 	core_state->dumper.task = tsk;
40810c28d93SAlex Kelly 	core_state->dumper.next = NULL;
40910c28d93SAlex Kelly 
4100258b5fdSEric W. Biederman 	core_waiters = zap_threads(tsk, core_state, exit_code);
41110c28d93SAlex Kelly 	if (core_waiters > 0) {
41210c28d93SAlex Kelly 		struct core_thread *ptr;
41310c28d93SAlex Kelly 
414f5d39b02SPeter Zijlstra 		wait_for_completion_state(&core_state->startup,
415f5d39b02SPeter Zijlstra 					  TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
41610c28d93SAlex Kelly 		/*
41710c28d93SAlex Kelly 		 * Wait for all the threads to become inactive, so that
41810c28d93SAlex Kelly 		 * all the thread context (extended register state, like
41910c28d93SAlex Kelly 		 * fpu etc) gets copied to the memory.
42010c28d93SAlex Kelly 		 */
42110c28d93SAlex Kelly 		ptr = core_state->dumper.next;
42210c28d93SAlex Kelly 		while (ptr != NULL) {
423f9fc8cadSPeter Zijlstra 			wait_task_inactive(ptr->task, TASK_ANY);
42410c28d93SAlex Kelly 			ptr = ptr->next;
42510c28d93SAlex Kelly 		}
42610c28d93SAlex Kelly 	}
42710c28d93SAlex Kelly 
42810c28d93SAlex Kelly 	return core_waiters;
42910c28d93SAlex Kelly }
43010c28d93SAlex Kelly 
coredump_finish(bool core_dumped)4310258b5fdSEric W. Biederman static void coredump_finish(bool core_dumped)
43210c28d93SAlex Kelly {
43310c28d93SAlex Kelly 	struct core_thread *curr, *next;
43410c28d93SAlex Kelly 	struct task_struct *task;
43510c28d93SAlex Kelly 
4366cd8f0acSOleg Nesterov 	spin_lock_irq(&current->sighand->siglock);
437acdedd99SOleg Nesterov 	if (core_dumped && !__fatal_signal_pending(current))
438acdedd99SOleg Nesterov 		current->signal->group_exit_code |= 0x80;
4390258b5fdSEric W. Biederman 	next = current->signal->core_state->dumper.next;
4400258b5fdSEric W. Biederman 	current->signal->core_state = NULL;
4416cd8f0acSOleg Nesterov 	spin_unlock_irq(&current->sighand->siglock);
4426cd8f0acSOleg Nesterov 
44310c28d93SAlex Kelly 	while ((curr = next) != NULL) {
44410c28d93SAlex Kelly 		next = curr->next;
44510c28d93SAlex Kelly 		task = curr->task;
44610c28d93SAlex Kelly 		/*
44792307383SEric W. Biederman 		 * see coredump_task_exit(), curr->task must not see
44810c28d93SAlex Kelly 		 * ->task == NULL before we read ->next.
44910c28d93SAlex Kelly 		 */
45010c28d93SAlex Kelly 		smp_mb();
45110c28d93SAlex Kelly 		curr->task = NULL;
45210c28d93SAlex Kelly 		wake_up_process(task);
45310c28d93SAlex Kelly 	}
45410c28d93SAlex Kelly }
45510c28d93SAlex Kelly 
dump_interrupted(void)456528f827eSOleg Nesterov static bool dump_interrupted(void)
457528f827eSOleg Nesterov {
458528f827eSOleg Nesterov 	/*
459528f827eSOleg Nesterov 	 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
460528f827eSOleg Nesterov 	 * can do try_to_freeze() and check __fatal_signal_pending(),
461528f827eSOleg Nesterov 	 * but then we need to teach dump_write() to restart and clear
462528f827eSOleg Nesterov 	 * TIF_SIGPENDING.
463528f827eSOleg Nesterov 	 */
46406af8679SEric W. Biederman 	return fatal_signal_pending(current) || freezing(current);
465528f827eSOleg Nesterov }
466528f827eSOleg Nesterov 
wait_for_dump_helpers(struct file * file)46710c28d93SAlex Kelly static void wait_for_dump_helpers(struct file *file)
46810c28d93SAlex Kelly {
469de32ec4cSAl Viro 	struct pipe_inode_info *pipe = file->private_data;
47010c28d93SAlex Kelly 
47110c28d93SAlex Kelly 	pipe_lock(pipe);
47210c28d93SAlex Kelly 	pipe->readers++;
47310c28d93SAlex Kelly 	pipe->writers--;
4740ddad21dSLinus Torvalds 	wake_up_interruptible_sync(&pipe->rd_wait);
47510c28d93SAlex Kelly 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
476dc7ee2aaSOleg Nesterov 	pipe_unlock(pipe);
47710c28d93SAlex Kelly 
478dc7ee2aaSOleg Nesterov 	/*
479dc7ee2aaSOleg Nesterov 	 * We actually want wait_event_freezable() but then we need
480dc7ee2aaSOleg Nesterov 	 * to clear TIF_SIGPENDING and improve dump_interrupted().
481dc7ee2aaSOleg Nesterov 	 */
4820ddad21dSLinus Torvalds 	wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
483dc7ee2aaSOleg Nesterov 
484dc7ee2aaSOleg Nesterov 	pipe_lock(pipe);
48510c28d93SAlex Kelly 	pipe->readers--;
48610c28d93SAlex Kelly 	pipe->writers++;
48710c28d93SAlex Kelly 	pipe_unlock(pipe);
48810c28d93SAlex Kelly }
48910c28d93SAlex Kelly 
49010c28d93SAlex Kelly /*
49110c28d93SAlex Kelly  * umh_pipe_setup
49210c28d93SAlex Kelly  * helper function to customize the process used
49310c28d93SAlex Kelly  * to collect the core in userspace.  Specifically
49410c28d93SAlex Kelly  * it sets up a pipe and installs it as fd 0 (stdin)
49510c28d93SAlex Kelly  * for the process.  Returns 0 on success, or
49610c28d93SAlex Kelly  * PTR_ERR on failure.
49710c28d93SAlex Kelly  * Note that it also sets the core limit to 1.  This
49810c28d93SAlex Kelly  * is a special value that we use to trap recursive
49910c28d93SAlex Kelly  * core dumps
50010c28d93SAlex Kelly  */
umh_pipe_setup(struct subprocess_info * info,struct cred * new)50110c28d93SAlex Kelly static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
50210c28d93SAlex Kelly {
50310c28d93SAlex Kelly 	struct file *files[2];
50410c28d93SAlex Kelly 	struct coredump_params *cp = (struct coredump_params *)info->data;
50510c28d93SAlex Kelly 	int err = create_pipe_files(files, 0);
50610c28d93SAlex Kelly 	if (err)
50710c28d93SAlex Kelly 		return err;
50810c28d93SAlex Kelly 
50910c28d93SAlex Kelly 	cp->file = files[1];
51010c28d93SAlex Kelly 
51145525b26SAl Viro 	err = replace_fd(0, files[0], 0);
51245525b26SAl Viro 	fput(files[0]);
51310c28d93SAlex Kelly 	/* and disallow core files too */
51410c28d93SAlex Kelly 	current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
51510c28d93SAlex Kelly 
51645525b26SAl Viro 	return err;
51710c28d93SAlex Kelly }
51810c28d93SAlex Kelly 
do_coredump(const kernel_siginfo_t * siginfo)519ae7795bcSEric W. Biederman void do_coredump(const kernel_siginfo_t *siginfo)
52010c28d93SAlex Kelly {
52110c28d93SAlex Kelly 	struct core_state core_state;
52210c28d93SAlex Kelly 	struct core_name cn;
52310c28d93SAlex Kelly 	struct mm_struct *mm = current->mm;
52410c28d93SAlex Kelly 	struct linux_binfmt * binfmt;
52510c28d93SAlex Kelly 	const struct cred *old_cred;
52610c28d93SAlex Kelly 	struct cred *cred;
52710c28d93SAlex Kelly 	int retval = 0;
52810c28d93SAlex Kelly 	int ispipe;
529315c6926SPaul Wise 	size_t *argv = NULL;
530315c6926SPaul Wise 	int argc = 0;
531fbb18169SJann Horn 	/* require nonrelative corefile path and be extra careful */
532fbb18169SJann Horn 	bool need_suid_safe = false;
533acdedd99SOleg Nesterov 	bool core_dumped = false;
53410c28d93SAlex Kelly 	static atomic_t core_dump_count = ATOMIC_INIT(0);
53510c28d93SAlex Kelly 	struct coredump_params cprm = {
5365ab1c309SDenys Vlasenko 		.siginfo = siginfo,
53710c28d93SAlex Kelly 		.limit = rlimit(RLIMIT_CORE),
53810c28d93SAlex Kelly 		/*
53910c28d93SAlex Kelly 		 * We must use the same mm->flags while dumping core to avoid
54010c28d93SAlex Kelly 		 * inconsistency of bit flags, since this flag is not protected
54110c28d93SAlex Kelly 		 * by any locks.
54210c28d93SAlex Kelly 		 */
54310c28d93SAlex Kelly 		.mm_flags = mm->flags,
54495c5436aSEric W. Biederman 		.vma_meta = NULL,
5458603b6f5SOleksandr Natalenko 		.cpu = raw_smp_processor_id(),
54610c28d93SAlex Kelly 	};
54710c28d93SAlex Kelly 
5485ab1c309SDenys Vlasenko 	audit_core_dumps(siginfo->si_signo);
54910c28d93SAlex Kelly 
55010c28d93SAlex Kelly 	binfmt = mm->binfmt;
55110c28d93SAlex Kelly 	if (!binfmt || !binfmt->core_dump)
55210c28d93SAlex Kelly 		goto fail;
55310c28d93SAlex Kelly 	if (!__get_dumpable(cprm.mm_flags))
55410c28d93SAlex Kelly 		goto fail;
55510c28d93SAlex Kelly 
55610c28d93SAlex Kelly 	cred = prepare_creds();
55710c28d93SAlex Kelly 	if (!cred)
55810c28d93SAlex Kelly 		goto fail;
55910c28d93SAlex Kelly 	/*
56010c28d93SAlex Kelly 	 * We cannot trust fsuid as being the "true" uid of the process
56110c28d93SAlex Kelly 	 * nor do we know its entire history. We only know it was tainted
56210c28d93SAlex Kelly 	 * so we dump it as root in mode 2, and only into a controlled
56310c28d93SAlex Kelly 	 * environment (pipe handler or fully qualified path).
56410c28d93SAlex Kelly 	 */
565e579d2c2SKees Cook 	if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
56610c28d93SAlex Kelly 		/* Setuid core dump mode */
56710c28d93SAlex Kelly 		cred->fsuid = GLOBAL_ROOT_UID;	/* Dump root private */
568fbb18169SJann Horn 		need_suid_safe = true;
56910c28d93SAlex Kelly 	}
57010c28d93SAlex Kelly 
5715ab1c309SDenys Vlasenko 	retval = coredump_wait(siginfo->si_signo, &core_state);
57210c28d93SAlex Kelly 	if (retval < 0)
57310c28d93SAlex Kelly 		goto fail_creds;
57410c28d93SAlex Kelly 
57510c28d93SAlex Kelly 	old_cred = override_creds(cred);
57610c28d93SAlex Kelly 
577315c6926SPaul Wise 	ispipe = format_corename(&cn, &cprm, &argv, &argc);
57810c28d93SAlex Kelly 
57910c28d93SAlex Kelly 	if (ispipe) {
580315c6926SPaul Wise 		int argi;
58110c28d93SAlex Kelly 		int dump_count;
58210c28d93SAlex Kelly 		char **helper_argv;
583907ed132SLucas De Marchi 		struct subprocess_info *sub_info;
58410c28d93SAlex Kelly 
58510c28d93SAlex Kelly 		if (ispipe < 0) {
58610c28d93SAlex Kelly 			printk(KERN_WARNING "format_corename failed\n");
58710c28d93SAlex Kelly 			printk(KERN_WARNING "Aborting core\n");
588e7fd1549SOleg Nesterov 			goto fail_unlock;
58910c28d93SAlex Kelly 		}
59010c28d93SAlex Kelly 
59110c28d93SAlex Kelly 		if (cprm.limit == 1) {
59210c28d93SAlex Kelly 			/* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
59310c28d93SAlex Kelly 			 *
59410c28d93SAlex Kelly 			 * Normally core limits are irrelevant to pipes, since
59510c28d93SAlex Kelly 			 * we're not writing to the file system, but we use
596fcbc32bcSBastien Nocera 			 * cprm.limit of 1 here as a special value, this is a
59710c28d93SAlex Kelly 			 * consistent way to catch recursive crashes.
59810c28d93SAlex Kelly 			 * We can still crash if the core_pattern binary sets
59910c28d93SAlex Kelly 			 * RLIM_CORE = !1, but it runs as root, and can do
60010c28d93SAlex Kelly 			 * lots of stupid things.
60110c28d93SAlex Kelly 			 *
60210c28d93SAlex Kelly 			 * Note that we use task_tgid_vnr here to grab the pid
60310c28d93SAlex Kelly 			 * of the process group leader.  That way we get the
60410c28d93SAlex Kelly 			 * right pid if a thread in a multi-threaded
60510c28d93SAlex Kelly 			 * core_pattern process dies.
60610c28d93SAlex Kelly 			 */
60710c28d93SAlex Kelly 			printk(KERN_WARNING
60810c28d93SAlex Kelly 				"Process %d(%s) has RLIMIT_CORE set to 1\n",
60910c28d93SAlex Kelly 				task_tgid_vnr(current), current->comm);
61010c28d93SAlex Kelly 			printk(KERN_WARNING "Aborting core\n");
61110c28d93SAlex Kelly 			goto fail_unlock;
61210c28d93SAlex Kelly 		}
61310c28d93SAlex Kelly 		cprm.limit = RLIM_INFINITY;
61410c28d93SAlex Kelly 
61510c28d93SAlex Kelly 		dump_count = atomic_inc_return(&core_dump_count);
61610c28d93SAlex Kelly 		if (core_pipe_limit && (core_pipe_limit < dump_count)) {
61710c28d93SAlex Kelly 			printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
61810c28d93SAlex Kelly 			       task_tgid_vnr(current), current->comm);
61910c28d93SAlex Kelly 			printk(KERN_WARNING "Skipping core dump\n");
62010c28d93SAlex Kelly 			goto fail_dropcount;
62110c28d93SAlex Kelly 		}
62210c28d93SAlex Kelly 
623315c6926SPaul Wise 		helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
624315c6926SPaul Wise 					    GFP_KERNEL);
62510c28d93SAlex Kelly 		if (!helper_argv) {
62610c28d93SAlex Kelly 			printk(KERN_WARNING "%s failed to allocate memory\n",
62710c28d93SAlex Kelly 			       __func__);
62810c28d93SAlex Kelly 			goto fail_dropcount;
62910c28d93SAlex Kelly 		}
630315c6926SPaul Wise 		for (argi = 0; argi < argc; argi++)
631315c6926SPaul Wise 			helper_argv[argi] = cn.corename + argv[argi];
632315c6926SPaul Wise 		helper_argv[argi] = NULL;
63310c28d93SAlex Kelly 
634907ed132SLucas De Marchi 		retval = -ENOMEM;
635907ed132SLucas De Marchi 		sub_info = call_usermodehelper_setup(helper_argv[0],
636907ed132SLucas De Marchi 						helper_argv, NULL, GFP_KERNEL,
637907ed132SLucas De Marchi 						umh_pipe_setup, NULL, &cprm);
638907ed132SLucas De Marchi 		if (sub_info)
639907ed132SLucas De Marchi 			retval = call_usermodehelper_exec(sub_info,
640907ed132SLucas De Marchi 							  UMH_WAIT_EXEC);
641907ed132SLucas De Marchi 
642315c6926SPaul Wise 		kfree(helper_argv);
64310c28d93SAlex Kelly 		if (retval) {
644888ffc59SOleg Nesterov 			printk(KERN_INFO "Core dump to |%s pipe failed\n",
64510c28d93SAlex Kelly 			       cn.corename);
64610c28d93SAlex Kelly 			goto close_fail;
64710c28d93SAlex Kelly 		}
64810c28d93SAlex Kelly 	} else {
649abf08576SChristian Brauner 		struct mnt_idmap *idmap;
65010c28d93SAlex Kelly 		struct inode *inode;
651378c6520SJann Horn 		int open_flags = O_CREAT | O_WRONLY | O_NOFOLLOW |
652378c6520SJann Horn 				 O_LARGEFILE | O_EXCL;
65310c28d93SAlex Kelly 
65410c28d93SAlex Kelly 		if (cprm.limit < binfmt->min_coredump)
65510c28d93SAlex Kelly 			goto fail_unlock;
65610c28d93SAlex Kelly 
657fbb18169SJann Horn 		if (need_suid_safe && cn.corename[0] != '/') {
65810c28d93SAlex Kelly 			printk(KERN_WARNING "Pid %d(%s) can only dump core "\
65910c28d93SAlex Kelly 				"to fully qualified path!\n",
66010c28d93SAlex Kelly 				task_tgid_vnr(current), current->comm);
66110c28d93SAlex Kelly 			printk(KERN_WARNING "Skipping core dump\n");
66210c28d93SAlex Kelly 			goto fail_unlock;
66310c28d93SAlex Kelly 		}
66410c28d93SAlex Kelly 
665fbb18169SJann Horn 		/*
666fbb18169SJann Horn 		 * Unlink the file if it exists unless this is a SUID
667fbb18169SJann Horn 		 * binary - in that case, we're running around with root
668fbb18169SJann Horn 		 * privs and don't want to unlink another user's coredump.
669fbb18169SJann Horn 		 */
670fbb18169SJann Horn 		if (!need_suid_safe) {
671fbb18169SJann Horn 			/*
672fbb18169SJann Horn 			 * If it doesn't exist, that's fine. If there's some
673fbb18169SJann Horn 			 * other problem, we'll catch it at the filp_open().
674fbb18169SJann Horn 			 */
67596271654SChristoph Hellwig 			do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
676fbb18169SJann Horn 		}
677fbb18169SJann Horn 
678fbb18169SJann Horn 		/*
679fbb18169SJann Horn 		 * There is a race between unlinking and creating the
680fbb18169SJann Horn 		 * file, but if that causes an EEXIST here, that's
681fbb18169SJann Horn 		 * fine - another process raced with us while creating
682fbb18169SJann Horn 		 * the corefile, and the other process won. To userspace,
683fbb18169SJann Horn 		 * what matters is that at least one of the two processes
684fbb18169SJann Horn 		 * writes its coredump successfully, not which one.
685fbb18169SJann Horn 		 */
686378c6520SJann Horn 		if (need_suid_safe) {
687378c6520SJann Horn 			/*
688378c6520SJann Horn 			 * Using user namespaces, normal user tasks can change
689378c6520SJann Horn 			 * their current->fs->root to point to arbitrary
690378c6520SJann Horn 			 * directories. Since the intention of the "only dump
691378c6520SJann Horn 			 * with a fully qualified path" rule is to control where
692378c6520SJann Horn 			 * coredumps may be placed using root privileges,
693378c6520SJann Horn 			 * current->fs->root must not be used. Instead, use the
694378c6520SJann Horn 			 * root directory of init_task.
695378c6520SJann Horn 			 */
696378c6520SJann Horn 			struct path root;
697378c6520SJann Horn 
698378c6520SJann Horn 			task_lock(&init_task);
699378c6520SJann Horn 			get_fs_root(init_task.fs, &root);
700378c6520SJann Horn 			task_unlock(&init_task);
701ffb37ca3SAl Viro 			cprm.file = file_open_root(&root, cn.corename,
702ffb37ca3SAl Viro 						   open_flags, 0600);
703378c6520SJann Horn 			path_put(&root);
704378c6520SJann Horn 		} else {
705378c6520SJann Horn 			cprm.file = filp_open(cn.corename, open_flags, 0600);
706378c6520SJann Horn 		}
70710c28d93SAlex Kelly 		if (IS_ERR(cprm.file))
70810c28d93SAlex Kelly 			goto fail_unlock;
70910c28d93SAlex Kelly 
710496ad9aaSAl Viro 		inode = file_inode(cprm.file);
71110c28d93SAlex Kelly 		if (inode->i_nlink > 1)
71210c28d93SAlex Kelly 			goto close_fail;
71310c28d93SAlex Kelly 		if (d_unhashed(cprm.file->f_path.dentry))
71410c28d93SAlex Kelly 			goto close_fail;
71510c28d93SAlex Kelly 		/*
71610c28d93SAlex Kelly 		 * AK: actually i see no reason to not allow this for named
71710c28d93SAlex Kelly 		 * pipes etc, but keep the previous behaviour for now.
71810c28d93SAlex Kelly 		 */
71910c28d93SAlex Kelly 		if (!S_ISREG(inode->i_mode))
72010c28d93SAlex Kelly 			goto close_fail;
72110c28d93SAlex Kelly 		/*
72240f705a7SJann Horn 		 * Don't dump core if the filesystem changed owner or mode
72340f705a7SJann Horn 		 * of the file during file creation. This is an issue when
72440f705a7SJann Horn 		 * a process dumps core while its cwd is e.g. on a vfat
72540f705a7SJann Horn 		 * filesystem.
72610c28d93SAlex Kelly 		 */
727abf08576SChristian Brauner 		idmap = file_mnt_idmap(cprm.file);
728e67fe633SChristian Brauner 		if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
729dbd9d6f8SDavid Oberhollenzer 				    current_fsuid())) {
730dbd9d6f8SDavid Oberhollenzer 			pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
731dbd9d6f8SDavid Oberhollenzer 					    cn.corename);
73210c28d93SAlex Kelly 			goto close_fail;
733dbd9d6f8SDavid Oberhollenzer 		}
734dbd9d6f8SDavid Oberhollenzer 		if ((inode->i_mode & 0677) != 0600) {
735dbd9d6f8SDavid Oberhollenzer 			pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
736dbd9d6f8SDavid Oberhollenzer 					    cn.corename);
73740f705a7SJann Horn 			goto close_fail;
738dbd9d6f8SDavid Oberhollenzer 		}
73986cc0584SAl Viro 		if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
74010c28d93SAlex Kelly 			goto close_fail;
741abf08576SChristian Brauner 		if (do_truncate(idmap, cprm.file->f_path.dentry,
742643fe55aSChristian Brauner 				0, 0, cprm.file))
74310c28d93SAlex Kelly 			goto close_fail;
74410c28d93SAlex Kelly 	}
74510c28d93SAlex Kelly 
74610c28d93SAlex Kelly 	/* get us an unshared descriptor table; almost always a no-op */
747c39ab6deSEric W. Biederman 	/* The cell spufs coredump code reads the file descriptor tables */
7481f702603SEric W. Biederman 	retval = unshare_files();
74910c28d93SAlex Kelly 	if (retval)
75010c28d93SAlex Kelly 		goto close_fail;
751e86d35c3SAl Viro 	if (!dump_interrupted()) {
7523740d93eSLuis Chamberlain 		/*
7533740d93eSLuis Chamberlain 		 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
7543740d93eSLuis Chamberlain 		 * have this set to NULL.
7553740d93eSLuis Chamberlain 		 */
7563740d93eSLuis Chamberlain 		if (!cprm.file) {
7573740d93eSLuis Chamberlain 			pr_info("Core dump to |%s disabled\n", cn.corename);
7583740d93eSLuis Chamberlain 			goto close_fail;
7593740d93eSLuis Chamberlain 		}
76095c5436aSEric W. Biederman 		if (!dump_vma_snapshot(&cprm))
76195c5436aSEric W. Biederman 			goto close_fail;
76295c5436aSEric W. Biederman 
76303d95eb2SAl Viro 		file_start_write(cprm.file);
764e86d35c3SAl Viro 		core_dumped = binfmt->core_dump(&cprm);
765d0f1088bSAl Viro 		/*
766d0f1088bSAl Viro 		 * Ensures that file size is big enough to contain the current
767d0f1088bSAl Viro 		 * file postion. This prevents gdb from complaining about
768d0f1088bSAl Viro 		 * a truncated file if the last "write" to the file was
769d0f1088bSAl Viro 		 * dump_skip.
770d0f1088bSAl Viro 		 */
771d0f1088bSAl Viro 		if (cprm.to_skip) {
772d0f1088bSAl Viro 			cprm.to_skip--;
773d0f1088bSAl Viro 			dump_emit(&cprm, "", 1);
774d0f1088bSAl Viro 		}
77503d95eb2SAl Viro 		file_end_write(cprm.file);
776390031c9SEric W. Biederman 		free_vma_snapshot(&cprm);
777e86d35c3SAl Viro 	}
77810c28d93SAlex Kelly 	if (ispipe && core_pipe_limit)
77910c28d93SAlex Kelly 		wait_for_dump_helpers(cprm.file);
78010c28d93SAlex Kelly close_fail:
78110c28d93SAlex Kelly 	if (cprm.file)
78210c28d93SAlex Kelly 		filp_close(cprm.file, NULL);
78310c28d93SAlex Kelly fail_dropcount:
78410c28d93SAlex Kelly 	if (ispipe)
78510c28d93SAlex Kelly 		atomic_dec(&core_dump_count);
78610c28d93SAlex Kelly fail_unlock:
787315c6926SPaul Wise 	kfree(argv);
78810c28d93SAlex Kelly 	kfree(cn.corename);
7890258b5fdSEric W. Biederman 	coredump_finish(core_dumped);
79010c28d93SAlex Kelly 	revert_creds(old_cred);
79110c28d93SAlex Kelly fail_creds:
79210c28d93SAlex Kelly 	put_cred(cred);
79310c28d93SAlex Kelly fail:
79410c28d93SAlex Kelly 	return;
79510c28d93SAlex Kelly }
79610c28d93SAlex Kelly 
79710c28d93SAlex Kelly /*
79810c28d93SAlex Kelly  * Core dumping helper functions.  These are the only things you should
79910c28d93SAlex Kelly  * do on a core-file: use only these functions to write out all the
80010c28d93SAlex Kelly  * necessary info.
80110c28d93SAlex Kelly  */
__dump_emit(struct coredump_params * cprm,const void * addr,int nr)802d0f1088bSAl Viro static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
803ecc8c772SAl Viro {
804ecc8c772SAl Viro 	struct file *file = cprm->file;
8052507a4fbSAl Viro 	loff_t pos = file->f_pos;
8062507a4fbSAl Viro 	ssize_t n;
8072c4cb043SOmar Sandoval 	if (cprm->written + nr > cprm->limit)
808ecc8c772SAl Viro 		return 0;
809df0c09c0SJann Horn 
810df0c09c0SJann Horn 
8112507a4fbSAl Viro 	if (dump_interrupted())
812ecc8c772SAl Viro 		return 0;
81352da40aeSAl Viro 	n = __kernel_write(file, addr, nr, &pos);
814df0c09c0SJann Horn 	if (n != nr)
8152507a4fbSAl Viro 		return 0;
8162507a4fbSAl Viro 	file->f_pos = pos;
8172c4cb043SOmar Sandoval 	cprm->written += n;
8181607f09cSMateusz Guzik 	cprm->pos += n;
819df0c09c0SJann Horn 
820ecc8c772SAl Viro 	return 1;
821ecc8c772SAl Viro }
822ecc8c772SAl Viro 
__dump_skip(struct coredump_params * cprm,size_t nr)823d0f1088bSAl Viro static int __dump_skip(struct coredump_params *cprm, size_t nr)
82410c28d93SAlex Kelly {
8259b56d543SAl Viro 	static char zeroes[PAGE_SIZE];
8269b56d543SAl Viro 	struct file *file = cprm->file;
8274e3299eaSJason A. Donenfeld 	if (file->f_mode & FMODE_LSEEK) {
828528f827eSOleg Nesterov 		if (dump_interrupted() ||
8294e3299eaSJason A. Donenfeld 		    vfs_llseek(file, nr, SEEK_CUR) < 0)
83010c28d93SAlex Kelly 			return 0;
8311607f09cSMateusz Guzik 		cprm->pos += nr;
8329b56d543SAl Viro 		return 1;
83310c28d93SAlex Kelly 	} else {
8349b56d543SAl Viro 		while (nr > PAGE_SIZE) {
835d0f1088bSAl Viro 			if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
83610c28d93SAlex Kelly 				return 0;
8379b56d543SAl Viro 			nr -= PAGE_SIZE;
83810c28d93SAlex Kelly 		}
839d0f1088bSAl Viro 		return __dump_emit(cprm, zeroes, nr);
84010c28d93SAlex Kelly 	}
84110c28d93SAlex Kelly }
842d0f1088bSAl Viro 
dump_emit(struct coredump_params * cprm,const void * addr,int nr)8439c7417b5SGeert Uytterhoeven int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
8449c7417b5SGeert Uytterhoeven {
8459c7417b5SGeert Uytterhoeven 	if (cprm->to_skip) {
8469c7417b5SGeert Uytterhoeven 		if (!__dump_skip(cprm, cprm->to_skip))
8479c7417b5SGeert Uytterhoeven 			return 0;
8489c7417b5SGeert Uytterhoeven 		cprm->to_skip = 0;
8499c7417b5SGeert Uytterhoeven 	}
8509c7417b5SGeert Uytterhoeven 	return __dump_emit(cprm, addr, nr);
8519c7417b5SGeert Uytterhoeven }
8529c7417b5SGeert Uytterhoeven EXPORT_SYMBOL(dump_emit);
8539c7417b5SGeert Uytterhoeven 
dump_skip_to(struct coredump_params * cprm,unsigned long pos)8549c7417b5SGeert Uytterhoeven void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
8559c7417b5SGeert Uytterhoeven {
8569c7417b5SGeert Uytterhoeven 	cprm->to_skip = pos - cprm->pos;
8579c7417b5SGeert Uytterhoeven }
8589c7417b5SGeert Uytterhoeven EXPORT_SYMBOL(dump_skip_to);
8599c7417b5SGeert Uytterhoeven 
dump_skip(struct coredump_params * cprm,size_t nr)8609c7417b5SGeert Uytterhoeven void dump_skip(struct coredump_params *cprm, size_t nr)
8619c7417b5SGeert Uytterhoeven {
8629c7417b5SGeert Uytterhoeven 	cprm->to_skip += nr;
8639c7417b5SGeert Uytterhoeven }
8649c7417b5SGeert Uytterhoeven EXPORT_SYMBOL(dump_skip);
8659c7417b5SGeert Uytterhoeven 
8669c7417b5SGeert Uytterhoeven #ifdef CONFIG_ELF_CORE
dump_emit_page(struct coredump_params * cprm,struct page * page)86706bbaa6dSAl Viro static int dump_emit_page(struct coredump_params *cprm, struct page *page)
86806bbaa6dSAl Viro {
869cd598003SChristoph Hellwig 	struct bio_vec bvec;
87006bbaa6dSAl Viro 	struct iov_iter iter;
87106bbaa6dSAl Viro 	struct file *file = cprm->file;
8724f526fefSAl Viro 	loff_t pos;
87306bbaa6dSAl Viro 	ssize_t n;
87406bbaa6dSAl Viro 
87506bbaa6dSAl Viro 	if (cprm->to_skip) {
87606bbaa6dSAl Viro 		if (!__dump_skip(cprm, cprm->to_skip))
87706bbaa6dSAl Viro 			return 0;
87806bbaa6dSAl Viro 		cprm->to_skip = 0;
87906bbaa6dSAl Viro 	}
88006bbaa6dSAl Viro 	if (cprm->written + PAGE_SIZE > cprm->limit)
88106bbaa6dSAl Viro 		return 0;
88206bbaa6dSAl Viro 	if (dump_interrupted())
88306bbaa6dSAl Viro 		return 0;
8844f526fefSAl Viro 	pos = file->f_pos;
885cd598003SChristoph Hellwig 	bvec_set_page(&bvec, page, PAGE_SIZE, 0);
886de4eda9dSAl Viro 	iov_iter_bvec(&iter, ITER_SOURCE, &bvec, 1, PAGE_SIZE);
887245f0922SKefeng Wang 	iov_iter_set_copy_mc(&iter);
88806bbaa6dSAl Viro 	n = __kernel_write_iter(cprm->file, &iter, &pos);
88906bbaa6dSAl Viro 	if (n != PAGE_SIZE)
89006bbaa6dSAl Viro 		return 0;
89106bbaa6dSAl Viro 	file->f_pos = pos;
89206bbaa6dSAl Viro 	cprm->written += PAGE_SIZE;
89306bbaa6dSAl Viro 	cprm->pos += PAGE_SIZE;
89406bbaa6dSAl Viro 
89506bbaa6dSAl Viro 	return 1;
89606bbaa6dSAl Viro }
89706bbaa6dSAl Viro 
dump_user_range(struct coredump_params * cprm,unsigned long start,unsigned long len)898afc63a97SJann Horn int dump_user_range(struct coredump_params *cprm, unsigned long start,
899afc63a97SJann Horn 		    unsigned long len)
900afc63a97SJann Horn {
901afc63a97SJann Horn 	unsigned long addr;
902afc63a97SJann Horn 
903afc63a97SJann Horn 	for (addr = start; addr < start + len; addr += PAGE_SIZE) {
904afc63a97SJann Horn 		struct page *page;
905afc63a97SJann Horn 
906afc63a97SJann Horn 		/*
907afc63a97SJann Horn 		 * To avoid having to allocate page tables for virtual address
908afc63a97SJann Horn 		 * ranges that have never been used yet, and also to make it
909afc63a97SJann Horn 		 * easy to generate sparse core files, use a helper that returns
910afc63a97SJann Horn 		 * NULL when encountering an empty page table entry that would
911afc63a97SJann Horn 		 * otherwise have been filled with the zero page.
912afc63a97SJann Horn 		 */
913afc63a97SJann Horn 		page = get_dump_page(addr);
914afc63a97SJann Horn 		if (page) {
91506bbaa6dSAl Viro 			int stop = !dump_emit_page(cprm, page);
916afc63a97SJann Horn 			put_page(page);
917afc63a97SJann Horn 			if (stop)
918afc63a97SJann Horn 				return 0;
919d0f1088bSAl Viro 		} else {
920d0f1088bSAl Viro 			dump_skip(cprm, PAGE_SIZE);
921d0f1088bSAl Viro 		}
922afc63a97SJann Horn 	}
923afc63a97SJann Horn 	return 1;
924afc63a97SJann Horn }
925afc63a97SJann Horn #endif
926afc63a97SJann Horn 
dump_align(struct coredump_params * cprm,int align)92722a8cb82SAl Viro int dump_align(struct coredump_params *cprm, int align)
92822a8cb82SAl Viro {
929d0f1088bSAl Viro 	unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
93022a8cb82SAl Viro 	if (align & (align - 1))
931db51242dSAl Viro 		return 0;
932d0f1088bSAl Viro 	if (mod)
933d0f1088bSAl Viro 		cprm->to_skip += align - mod;
934d0f1088bSAl Viro 	return 1;
93522a8cb82SAl Viro }
93622a8cb82SAl Viro EXPORT_SYMBOL(dump_align);
9374d22c75dSDave Kleikamp 
938f0bc21b2SXiaoming Ni #ifdef CONFIG_SYSCTL
939f0bc21b2SXiaoming Ni 
validate_coredump_safety(void)940f0bc21b2SXiaoming Ni void validate_coredump_safety(void)
941f0bc21b2SXiaoming Ni {
942f0bc21b2SXiaoming Ni 	if (suid_dumpable == SUID_DUMP_ROOT &&
943f0bc21b2SXiaoming Ni 	    core_pattern[0] != '/' && core_pattern[0] != '|') {
944f0bc21b2SXiaoming Ni 		pr_warn(
945f0bc21b2SXiaoming Ni "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
946f0bc21b2SXiaoming Ni "Pipe handler or fully qualified core dump path required.\n"
947f0bc21b2SXiaoming Ni "Set kernel.core_pattern before fs.suid_dumpable.\n"
948f0bc21b2SXiaoming Ni 		);
949f0bc21b2SXiaoming Ni 	}
950f0bc21b2SXiaoming Ni }
951f0bc21b2SXiaoming Ni 
proc_dostring_coredump(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)952f0bc21b2SXiaoming Ni static int proc_dostring_coredump(struct ctl_table *table, int write,
953f0bc21b2SXiaoming Ni 		  void *buffer, size_t *lenp, loff_t *ppos)
954f0bc21b2SXiaoming Ni {
955f0bc21b2SXiaoming Ni 	int error = proc_dostring(table, write, buffer, lenp, ppos);
956f0bc21b2SXiaoming Ni 
957f0bc21b2SXiaoming Ni 	if (!error)
958f0bc21b2SXiaoming Ni 		validate_coredump_safety();
959f0bc21b2SXiaoming Ni 	return error;
960f0bc21b2SXiaoming Ni }
961f0bc21b2SXiaoming Ni 
962f0bc21b2SXiaoming Ni static struct ctl_table coredump_sysctls[] = {
963f0bc21b2SXiaoming Ni 	{
964f0bc21b2SXiaoming Ni 		.procname	= "core_uses_pid",
965f0bc21b2SXiaoming Ni 		.data		= &core_uses_pid,
966f0bc21b2SXiaoming Ni 		.maxlen		= sizeof(int),
967f0bc21b2SXiaoming Ni 		.mode		= 0644,
968f0bc21b2SXiaoming Ni 		.proc_handler	= proc_dointvec,
969f0bc21b2SXiaoming Ni 	},
970f0bc21b2SXiaoming Ni 	{
971f0bc21b2SXiaoming Ni 		.procname	= "core_pattern",
972f0bc21b2SXiaoming Ni 		.data		= core_pattern,
973f0bc21b2SXiaoming Ni 		.maxlen		= CORENAME_MAX_SIZE,
974f0bc21b2SXiaoming Ni 		.mode		= 0644,
975f0bc21b2SXiaoming Ni 		.proc_handler	= proc_dostring_coredump,
976f0bc21b2SXiaoming Ni 	},
977f0bc21b2SXiaoming Ni 	{
978f0bc21b2SXiaoming Ni 		.procname	= "core_pipe_limit",
979f0bc21b2SXiaoming Ni 		.data		= &core_pipe_limit,
980f0bc21b2SXiaoming Ni 		.maxlen		= sizeof(unsigned int),
981f0bc21b2SXiaoming Ni 		.mode		= 0644,
982f0bc21b2SXiaoming Ni 		.proc_handler	= proc_dointvec,
983f0bc21b2SXiaoming Ni 	},
984f0bc21b2SXiaoming Ni 	{ }
985f0bc21b2SXiaoming Ni };
986f0bc21b2SXiaoming Ni 
init_fs_coredump_sysctls(void)987f0bc21b2SXiaoming Ni static int __init init_fs_coredump_sysctls(void)
988f0bc21b2SXiaoming Ni {
989f0bc21b2SXiaoming Ni 	register_sysctl_init("kernel", coredump_sysctls);
990f0bc21b2SXiaoming Ni 	return 0;
991f0bc21b2SXiaoming Ni }
992f0bc21b2SXiaoming Ni fs_initcall(init_fs_coredump_sysctls);
993f0bc21b2SXiaoming Ni #endif /* CONFIG_SYSCTL */
994f0bc21b2SXiaoming Ni 
9954d22c75dSDave Kleikamp /*
996429a22e7SJann Horn  * The purpose of always_dump_vma() is to make sure that special kernel mappings
997429a22e7SJann Horn  * that are useful for post-mortem analysis are included in every core dump.
998429a22e7SJann Horn  * In that way we ensure that the core dump is fully interpretable later
999429a22e7SJann Horn  * without matching up the same kernel and hardware config to see what PC values
1000429a22e7SJann Horn  * meant. These special mappings include - vDSO, vsyscall, and other
1001429a22e7SJann Horn  * architecture specific mappings
1002429a22e7SJann Horn  */
always_dump_vma(struct vm_area_struct * vma)1003429a22e7SJann Horn static bool always_dump_vma(struct vm_area_struct *vma)
1004429a22e7SJann Horn {
1005429a22e7SJann Horn 	/* Any vsyscall mappings? */
1006429a22e7SJann Horn 	if (vma == get_gate_vma(vma->vm_mm))
1007429a22e7SJann Horn 		return true;
1008429a22e7SJann Horn 
1009429a22e7SJann Horn 	/*
1010429a22e7SJann Horn 	 * Assume that all vmas with a .name op should always be dumped.
1011429a22e7SJann Horn 	 * If this changes, a new vm_ops field can easily be added.
1012429a22e7SJann Horn 	 */
1013429a22e7SJann Horn 	if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
1014429a22e7SJann Horn 		return true;
1015429a22e7SJann Horn 
1016429a22e7SJann Horn 	/*
1017429a22e7SJann Horn 	 * arch_vma_name() returns non-NULL for special architecture mappings,
1018429a22e7SJann Horn 	 * such as vDSO sections.
1019429a22e7SJann Horn 	 */
1020429a22e7SJann Horn 	if (arch_vma_name(vma))
1021429a22e7SJann Horn 		return true;
1022429a22e7SJann Horn 
1023429a22e7SJann Horn 	return false;
1024429a22e7SJann Horn }
1025429a22e7SJann Horn 
102684158b7fSJann Horn #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
102784158b7fSJann Horn 
1028429a22e7SJann Horn /*
1029429a22e7SJann Horn  * Decide how much of @vma's contents should be included in a core dump.
1030429a22e7SJann Horn  */
vma_dump_size(struct vm_area_struct * vma,unsigned long mm_flags)1031a07279c9SJann Horn static unsigned long vma_dump_size(struct vm_area_struct *vma,
1032a07279c9SJann Horn 				   unsigned long mm_flags)
1033429a22e7SJann Horn {
1034429a22e7SJann Horn #define FILTER(type)	(mm_flags & (1UL << MMF_DUMP_##type))
1035429a22e7SJann Horn 
1036429a22e7SJann Horn 	/* always dump the vdso and vsyscall sections */
1037429a22e7SJann Horn 	if (always_dump_vma(vma))
1038429a22e7SJann Horn 		goto whole;
1039429a22e7SJann Horn 
1040429a22e7SJann Horn 	if (vma->vm_flags & VM_DONTDUMP)
1041429a22e7SJann Horn 		return 0;
1042429a22e7SJann Horn 
1043429a22e7SJann Horn 	/* support for DAX */
1044429a22e7SJann Horn 	if (vma_is_dax(vma)) {
1045429a22e7SJann Horn 		if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
1046429a22e7SJann Horn 			goto whole;
1047429a22e7SJann Horn 		if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
1048429a22e7SJann Horn 			goto whole;
1049429a22e7SJann Horn 		return 0;
1050429a22e7SJann Horn 	}
1051429a22e7SJann Horn 
1052429a22e7SJann Horn 	/* Hugetlb memory check */
1053429a22e7SJann Horn 	if (is_vm_hugetlb_page(vma)) {
1054429a22e7SJann Horn 		if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
1055429a22e7SJann Horn 			goto whole;
1056429a22e7SJann Horn 		if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
1057429a22e7SJann Horn 			goto whole;
1058429a22e7SJann Horn 		return 0;
1059429a22e7SJann Horn 	}
1060429a22e7SJann Horn 
1061429a22e7SJann Horn 	/* Do not dump I/O mapped devices or special mappings */
1062429a22e7SJann Horn 	if (vma->vm_flags & VM_IO)
1063429a22e7SJann Horn 		return 0;
1064429a22e7SJann Horn 
1065429a22e7SJann Horn 	/* By default, dump shared memory if mapped from an anonymous file. */
1066429a22e7SJann Horn 	if (vma->vm_flags & VM_SHARED) {
1067429a22e7SJann Horn 		if (file_inode(vma->vm_file)->i_nlink == 0 ?
1068429a22e7SJann Horn 		    FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1069429a22e7SJann Horn 			goto whole;
1070429a22e7SJann Horn 		return 0;
1071429a22e7SJann Horn 	}
1072429a22e7SJann Horn 
1073429a22e7SJann Horn 	/* Dump segments that have been written to.  */
1074429a22e7SJann Horn 	if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
1075429a22e7SJann Horn 		goto whole;
1076429a22e7SJann Horn 	if (vma->vm_file == NULL)
1077429a22e7SJann Horn 		return 0;
1078429a22e7SJann Horn 
1079429a22e7SJann Horn 	if (FILTER(MAPPED_PRIVATE))
1080429a22e7SJann Horn 		goto whole;
1081429a22e7SJann Horn 
1082429a22e7SJann Horn 	/*
1083429a22e7SJann Horn 	 * If this is the beginning of an executable file mapping,
1084429a22e7SJann Horn 	 * dump the first page to aid in determining what was mapped here.
1085429a22e7SJann Horn 	 */
1086429a22e7SJann Horn 	if (FILTER(ELF_HEADERS) &&
108784158b7fSJann Horn 	    vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
108884158b7fSJann Horn 		if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
1089429a22e7SJann Horn 			return PAGE_SIZE;
1090429a22e7SJann Horn 
109184158b7fSJann Horn 		/*
109284158b7fSJann Horn 		 * ELF libraries aren't always executable.
109384158b7fSJann Horn 		 * We'll want to check whether the mapping starts with the ELF
109484158b7fSJann Horn 		 * magic, but not now - we're holding the mmap lock,
109584158b7fSJann Horn 		 * so copy_from_user() doesn't work here.
109684158b7fSJann Horn 		 * Use a placeholder instead, and fix it up later in
109784158b7fSJann Horn 		 * dump_vma_snapshot().
109884158b7fSJann Horn 		 */
109984158b7fSJann Horn 		return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
110084158b7fSJann Horn 	}
110184158b7fSJann Horn 
1102429a22e7SJann Horn #undef	FILTER
1103429a22e7SJann Horn 
1104429a22e7SJann Horn 	return 0;
1105429a22e7SJann Horn 
1106429a22e7SJann Horn whole:
1107429a22e7SJann Horn 	return vma->vm_end - vma->vm_start;
1108429a22e7SJann Horn }
1109a07279c9SJann Horn 
1110a07279c9SJann Horn /*
1111a07279c9SJann Horn  * Helper function for iterating across a vma list.  It ensures that the caller
1112a07279c9SJann Horn  * will visit `gate_vma' prior to terminating the search.
1113a07279c9SJann Horn  */
coredump_next_vma(struct vma_iterator * vmi,struct vm_area_struct * vma,struct vm_area_struct * gate_vma)1114e552cdb8SLiam R. Howlett static struct vm_area_struct *coredump_next_vma(struct vma_iterator *vmi,
1115182ea1d7SMatthew Wilcox (Oracle) 				       struct vm_area_struct *vma,
1116a07279c9SJann Horn 				       struct vm_area_struct *gate_vma)
1117a07279c9SJann Horn {
1118182ea1d7SMatthew Wilcox (Oracle) 	if (gate_vma && (vma == gate_vma))
1119a07279c9SJann Horn 		return NULL;
1120182ea1d7SMatthew Wilcox (Oracle) 
1121e552cdb8SLiam R. Howlett 	vma = vma_next(vmi);
1122182ea1d7SMatthew Wilcox (Oracle) 	if (vma)
1123182ea1d7SMatthew Wilcox (Oracle) 		return vma;
1124a07279c9SJann Horn 	return gate_vma;
1125a07279c9SJann Horn }
1126a07279c9SJann Horn 
free_vma_snapshot(struct coredump_params * cprm)1127390031c9SEric W. Biederman static void free_vma_snapshot(struct coredump_params *cprm)
1128390031c9SEric W. Biederman {
1129390031c9SEric W. Biederman 	if (cprm->vma_meta) {
1130390031c9SEric W. Biederman 		int i;
1131390031c9SEric W. Biederman 		for (i = 0; i < cprm->vma_count; i++) {
1132390031c9SEric W. Biederman 			struct file *file = cprm->vma_meta[i].file;
1133390031c9SEric W. Biederman 			if (file)
1134390031c9SEric W. Biederman 				fput(file);
1135390031c9SEric W. Biederman 		}
1136390031c9SEric W. Biederman 		kvfree(cprm->vma_meta);
1137390031c9SEric W. Biederman 		cprm->vma_meta = NULL;
1138390031c9SEric W. Biederman 	}
1139390031c9SEric W. Biederman }
1140390031c9SEric W. Biederman 
1141a07279c9SJann Horn /*
1142a07279c9SJann Horn  * Under the mmap_lock, take a snapshot of relevant information about the task's
1143a07279c9SJann Horn  * VMAs.
1144a07279c9SJann Horn  */
dump_vma_snapshot(struct coredump_params * cprm)114595c5436aSEric W. Biederman static bool dump_vma_snapshot(struct coredump_params *cprm)
1146a07279c9SJann Horn {
1147182ea1d7SMatthew Wilcox (Oracle) 	struct vm_area_struct *gate_vma, *vma = NULL;
1148a07279c9SJann Horn 	struct mm_struct *mm = current->mm;
1149e552cdb8SLiam R. Howlett 	VMA_ITERATOR(vmi, mm, 0);
1150182ea1d7SMatthew Wilcox (Oracle) 	int i = 0;
1151a07279c9SJann Horn 
1152a07279c9SJann Horn 	/*
1153a07279c9SJann Horn 	 * Once the stack expansion code is fixed to not change VMA bounds
1154a07279c9SJann Horn 	 * under mmap_lock in read mode, this can be changed to take the
1155a07279c9SJann Horn 	 * mmap_lock in read mode.
1156a07279c9SJann Horn 	 */
1157a07279c9SJann Horn 	if (mmap_write_lock_killable(mm))
115895c5436aSEric W. Biederman 		return false;
1159a07279c9SJann Horn 
116095c5436aSEric W. Biederman 	cprm->vma_data_size = 0;
1161a07279c9SJann Horn 	gate_vma = get_gate_vma(mm);
116295c5436aSEric W. Biederman 	cprm->vma_count = mm->map_count + (gate_vma ? 1 : 0);
1163a07279c9SJann Horn 
116495c5436aSEric W. Biederman 	cprm->vma_meta = kvmalloc_array(cprm->vma_count, sizeof(*cprm->vma_meta), GFP_KERNEL);
116595c5436aSEric W. Biederman 	if (!cprm->vma_meta) {
1166a07279c9SJann Horn 		mmap_write_unlock(mm);
116795c5436aSEric W. Biederman 		return false;
1168a07279c9SJann Horn 	}
1169a07279c9SJann Horn 
1170e552cdb8SLiam R. Howlett 	while ((vma = coredump_next_vma(&vmi, vma, gate_vma)) != NULL) {
117195c5436aSEric W. Biederman 		struct core_vma_metadata *m = cprm->vma_meta + i;
1172a07279c9SJann Horn 
1173a07279c9SJann Horn 		m->start = vma->vm_start;
1174a07279c9SJann Horn 		m->end = vma->vm_end;
1175a07279c9SJann Horn 		m->flags = vma->vm_flags;
1176a07279c9SJann Horn 		m->dump_size = vma_dump_size(vma, cprm->mm_flags);
1177390031c9SEric W. Biederman 		m->pgoff = vma->vm_pgoff;
1178390031c9SEric W. Biederman 		m->file = vma->vm_file;
1179390031c9SEric W. Biederman 		if (m->file)
1180390031c9SEric W. Biederman 			get_file(m->file);
1181182ea1d7SMatthew Wilcox (Oracle) 		i++;
1182a07279c9SJann Horn 	}
1183a07279c9SJann Horn 
1184a07279c9SJann Horn 	mmap_write_unlock(mm);
1185a07279c9SJann Horn 
118695c5436aSEric W. Biederman 	for (i = 0; i < cprm->vma_count; i++) {
118795c5436aSEric W. Biederman 		struct core_vma_metadata *m = cprm->vma_meta + i;
118884158b7fSJann Horn 
118984158b7fSJann Horn 		if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
119084158b7fSJann Horn 			char elfmag[SELFMAG];
119184158b7fSJann Horn 
119284158b7fSJann Horn 			if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
119384158b7fSJann Horn 					memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
119484158b7fSJann Horn 				m->dump_size = 0;
119584158b7fSJann Horn 			} else {
119684158b7fSJann Horn 				m->dump_size = PAGE_SIZE;
119784158b7fSJann Horn 			}
119884158b7fSJann Horn 		}
119984158b7fSJann Horn 
120095c5436aSEric W. Biederman 		cprm->vma_data_size += m->dump_size;
120184158b7fSJann Horn 	}
120284158b7fSJann Horn 
120395c5436aSEric W. Biederman 	return true;
1204a07279c9SJann Horn }
1205