Blame view

fs/coredump.c 17.4 KB
10c28d937   Alex Kelly   coredump: move co...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <linux/slab.h>
  #include <linux/file.h>
  #include <linux/fdtable.h>
  #include <linux/mm.h>
  #include <linux/stat.h>
  #include <linux/fcntl.h>
  #include <linux/swap.h>
  #include <linux/string.h>
  #include <linux/init.h>
  #include <linux/pagemap.h>
  #include <linux/perf_event.h>
  #include <linux/highmem.h>
  #include <linux/spinlock.h>
  #include <linux/key.h>
  #include <linux/personality.h>
  #include <linux/binfmts.h>
179899fd5   Alex Kelly   coredump: update ...
17
  #include <linux/coredump.h>
10c28d937   Alex Kelly   coredump: move co...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <linux/utsname.h>
  #include <linux/pid_namespace.h>
  #include <linux/module.h>
  #include <linux/namei.h>
  #include <linux/mount.h>
  #include <linux/security.h>
  #include <linux/syscalls.h>
  #include <linux/tsacct_kern.h>
  #include <linux/cn_proc.h>
  #include <linux/audit.h>
  #include <linux/tracehook.h>
  #include <linux/kmod.h>
  #include <linux/fsnotify.h>
  #include <linux/fs_struct.h>
  #include <linux/pipe_fs_i.h>
  #include <linux/oom.h>
  #include <linux/compat.h>
  
  #include <asm/uaccess.h>
  #include <asm/mmu_context.h>
  #include <asm/tlb.h>
  #include <asm/exec.h>
  
  #include <trace/events/task.h>
  #include "internal.h"
179899fd5   Alex Kelly   coredump: update ...
43
  #include "coredump.h"
10c28d937   Alex Kelly   coredump: move co...
44
45
46
47
  
  #include <trace/events/sched.h>
  
  int core_uses_pid;
10c28d937   Alex Kelly   coredump: move co...
48
  unsigned int core_pipe_limit;
3ceadcf6d   Oleg Nesterov   coredump: kill ca...
49
50
  char core_pattern[CORENAME_MAX_SIZE] = "core";
  static int core_name_size = CORENAME_MAX_SIZE;
10c28d937   Alex Kelly   coredump: move co...
51
52
53
54
55
  
  struct core_name {
  	char *corename;
  	int used, size;
  };
10c28d937   Alex Kelly   coredump: move co...
56
57
  
  /* The maximal length of core_pattern is also specified in sysctl.c */
3ceadcf6d   Oleg Nesterov   coredump: kill ca...
58
  static int expand_corename(struct core_name *cn, int size)
10c28d937   Alex Kelly   coredump: move co...
59
  {
e7fd1549a   Oleg Nesterov   coredump: format_...
60
  	char *corename = krealloc(cn->corename, size, GFP_KERNEL);
10c28d937   Alex Kelly   coredump: move co...
61

e7fd1549a   Oleg Nesterov   coredump: format_...
62
  	if (!corename)
10c28d937   Alex Kelly   coredump: move co...
63
  		return -ENOMEM;
10c28d937   Alex Kelly   coredump: move co...
64

3ceadcf6d   Oleg Nesterov   coredump: kill ca...
65
66
67
68
  	if (size > core_name_size) /* racy but harmless */
  		core_name_size = size;
  
  	cn->size = ksize(corename);
e7fd1549a   Oleg Nesterov   coredump: format_...
69
  	cn->corename = corename;
10c28d937   Alex Kelly   coredump: move co...
70
71
  	return 0;
  }
bc03c691a   Oleg Nesterov   coredump: introdu...
72
  static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
10c28d937   Alex Kelly   coredump: move co...
73
  {
5fe9d8ca2   Oleg Nesterov   coredump: cn_vpri...
74
  	int free, need;
10c28d937   Alex Kelly   coredump: move co...
75

5fe9d8ca2   Oleg Nesterov   coredump: cn_vpri...
76
77
78
79
80
81
82
  again:
  	free = cn->size - cn->used;
  	need = vsnprintf(cn->corename + cn->used, free, fmt, arg);
  	if (need < free) {
  		cn->used += need;
  		return 0;
  	}
10c28d937   Alex Kelly   coredump: move co...
83

3ceadcf6d   Oleg Nesterov   coredump: kill ca...
84
  	if (!expand_corename(cn, cn->size + need - free + 1))
5fe9d8ca2   Oleg Nesterov   coredump: cn_vpri...
85
  		goto again;
10c28d937   Alex Kelly   coredump: move co...
86

5fe9d8ca2   Oleg Nesterov   coredump: cn_vpri...
87
  	return -ENOMEM;
10c28d937   Alex Kelly   coredump: move co...
88
  }
bc03c691a   Oleg Nesterov   coredump: introdu...
89
90
91
92
93
94
95
96
97
98
99
  static int cn_printf(struct core_name *cn, const char *fmt, ...)
  {
  	va_list arg;
  	int ret;
  
  	va_start(arg, fmt);
  	ret = cn_vprintf(cn, fmt, arg);
  	va_end(arg);
  
  	return ret;
  }
923bed030   Oleg Nesterov   coredump: kill cn...
100
  static int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
10c28d937   Alex Kelly   coredump: move co...
101
  {
923bed030   Oleg Nesterov   coredump: kill cn...
102
103
104
105
106
107
108
109
110
111
112
113
114
  	int cur = cn->used;
  	va_list arg;
  	int ret;
  
  	va_start(arg, fmt);
  	ret = cn_vprintf(cn, fmt, arg);
  	va_end(arg);
  
  	for (; cur < cn->used; ++cur) {
  		if (cn->corename[cur] == '/')
  			cn->corename[cur] = '!';
  	}
  	return ret;
10c28d937   Alex Kelly   coredump: move co...
115
116
117
118
119
120
121
122
123
  }
  
  static int cn_print_exe_file(struct core_name *cn)
  {
  	struct file *exe_file;
  	char *pathbuf, *path;
  	int ret;
  
  	exe_file = get_mm_exe_file(current->mm);
923bed030   Oleg Nesterov   coredump: kill cn...
124
125
  	if (!exe_file)
  		return cn_esc_printf(cn, "%s (path unknown)", current->comm);
10c28d937   Alex Kelly   coredump: move co...
126
127
128
129
130
131
132
133
134
135
136
137
  
  	pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
  	if (!pathbuf) {
  		ret = -ENOMEM;
  		goto put_exe_file;
  	}
  
  	path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
  	if (IS_ERR(path)) {
  		ret = PTR_ERR(path);
  		goto free_buf;
  	}
923bed030   Oleg Nesterov   coredump: kill cn...
138
  	ret = cn_esc_printf(cn, "%s", path);
10c28d937   Alex Kelly   coredump: move co...
139
140
141
142
143
144
145
146
147
148
149
150
  
  free_buf:
  	kfree(pathbuf);
  put_exe_file:
  	fput(exe_file);
  	return ret;
  }
  
  /* format_corename will inspect the pattern parameter, and output a
   * name into corename, which must have space for at least
   * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
   */
12a2b4b22   Oleg Nesterov   coredump: add sup...
151
  static int format_corename(struct core_name *cn, struct coredump_params *cprm)
10c28d937   Alex Kelly   coredump: move co...
152
153
154
155
156
157
  {
  	const struct cred *cred = current_cred();
  	const char *pat_ptr = core_pattern;
  	int ispipe = (*pat_ptr == '|');
  	int pid_in_pattern = 0;
  	int err = 0;
e7fd1549a   Oleg Nesterov   coredump: format_...
158
  	cn->used = 0;
3ceadcf6d   Oleg Nesterov   coredump: kill ca...
159
160
  	cn->corename = NULL;
  	if (expand_corename(cn, core_name_size))
10c28d937   Alex Kelly   coredump: move co...
161
  		return -ENOMEM;
888ffc592   Oleg Nesterov   coredump: '% at t...
162
163
164
165
  	cn->corename[0] = '\0';
  
  	if (ispipe)
  		++pat_ptr;
10c28d937   Alex Kelly   coredump: move co...
166
167
168
169
170
  
  	/* Repeat as long as we have more pattern to process and more output
  	   space */
  	while (*pat_ptr) {
  		if (*pat_ptr != '%') {
10c28d937   Alex Kelly   coredump: move co...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  			err = cn_printf(cn, "%c", *pat_ptr++);
  		} else {
  			switch (*++pat_ptr) {
  			/* single % at the end, drop that */
  			case 0:
  				goto out;
  			/* Double percent, output one percent */
  			case '%':
  				err = cn_printf(cn, "%c", '%');
  				break;
  			/* pid */
  			case 'p':
  				pid_in_pattern = 1;
  				err = cn_printf(cn, "%d",
  					      task_tgid_vnr(current));
  				break;
  			/* uid */
  			case 'u':
  				err = cn_printf(cn, "%d", cred->uid);
  				break;
  			/* gid */
  			case 'g':
  				err = cn_printf(cn, "%d", cred->gid);
  				break;
12a2b4b22   Oleg Nesterov   coredump: add sup...
195
196
197
198
  			case 'd':
  				err = cn_printf(cn, "%d",
  					__get_dumpable(cprm->mm_flags));
  				break;
10c28d937   Alex Kelly   coredump: move co...
199
200
  			/* signal that caused the coredump */
  			case 's':
5ab1c309b   Denys Vlasenko   coredump: pass si...
201
  				err = cn_printf(cn, "%ld", cprm->siginfo->si_signo);
10c28d937   Alex Kelly   coredump: move co...
202
203
204
205
206
207
208
209
210
  				break;
  			/* UNIX time of coredump */
  			case 't': {
  				struct timeval tv;
  				do_gettimeofday(&tv);
  				err = cn_printf(cn, "%lu", tv.tv_sec);
  				break;
  			}
  			/* hostname */
923bed030   Oleg Nesterov   coredump: kill cn...
211
  			case 'h':
10c28d937   Alex Kelly   coredump: move co...
212
  				down_read(&uts_sem);
923bed030   Oleg Nesterov   coredump: kill cn...
213
  				err = cn_esc_printf(cn, "%s",
10c28d937   Alex Kelly   coredump: move co...
214
215
  					      utsname()->nodename);
  				up_read(&uts_sem);
10c28d937   Alex Kelly   coredump: move co...
216
  				break;
10c28d937   Alex Kelly   coredump: move co...
217
  			/* executable */
923bed030   Oleg Nesterov   coredump: kill cn...
218
219
  			case 'e':
  				err = cn_esc_printf(cn, "%s", current->comm);
10c28d937   Alex Kelly   coredump: move co...
220
  				break;
10c28d937   Alex Kelly   coredump: move co...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  			case 'E':
  				err = cn_print_exe_file(cn);
  				break;
  			/* core limit size */
  			case 'c':
  				err = cn_printf(cn, "%lu",
  					      rlimit(RLIMIT_CORE));
  				break;
  			default:
  				break;
  			}
  			++pat_ptr;
  		}
  
  		if (err)
  			return err;
  	}
888ffc592   Oleg Nesterov   coredump: '% at t...
238
  out:
10c28d937   Alex Kelly   coredump: move co...
239
240
241
242
243
244
245
246
247
248
  	/* Backward compatibility with core_uses_pid:
  	 *
  	 * If core_pattern does not include a %p (as is the default)
  	 * and core_uses_pid is set, then .%pid will be appended to
  	 * the filename. Do not do this for piped commands. */
  	if (!ispipe && !pid_in_pattern && core_uses_pid) {
  		err = cn_printf(cn, ".%d", task_tgid_vnr(current));
  		if (err)
  			return err;
  	}
10c28d937   Alex Kelly   coredump: move co...
249
250
251
252
253
254
255
  	return ispipe;
  }
  
  static int zap_process(struct task_struct *start, int exit_code)
  {
  	struct task_struct *t;
  	int nr = 0;
10c28d937   Alex Kelly   coredump: move co...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  	start->signal->group_exit_code = exit_code;
  	start->signal->group_stop_count = 0;
  
  	t = start;
  	do {
  		task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  		if (t != current && t->mm) {
  			sigaddset(&t->pending.signal, SIGKILL);
  			signal_wake_up(t, 1);
  			nr++;
  		}
  	} while_each_thread(start, t);
  
  	return nr;
  }
403bad72b   Oleg Nesterov   coredump: only SI...
271
272
  static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  			struct core_state *core_state, int exit_code)
10c28d937   Alex Kelly   coredump: move co...
273
274
275
276
277
278
279
280
281
  {
  	struct task_struct *g, *p;
  	unsigned long flags;
  	int nr = -EAGAIN;
  
  	spin_lock_irq(&tsk->sighand->siglock);
  	if (!signal_group_exit(tsk->signal)) {
  		mm->core_state = core_state;
  		nr = zap_process(tsk, exit_code);
6cd8f0aca   Oleg Nesterov   coredump: ensure ...
282
  		tsk->signal->group_exit_task = tsk;
403bad72b   Oleg Nesterov   coredump: only SI...
283
  		/* ignore all signals except SIGKILL, see prepare_signal() */
6cd8f0aca   Oleg Nesterov   coredump: ensure ...
284
  		tsk->signal->flags = SIGNAL_GROUP_COREDUMP;
403bad72b   Oleg Nesterov   coredump: only SI...
285
  		clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
10c28d937   Alex Kelly   coredump: move co...
286
287
288
289
  	}
  	spin_unlock_irq(&tsk->sighand->siglock);
  	if (unlikely(nr < 0))
  		return nr;
079148b91   Oleg Nesterov   coredump: factor ...
290
  	tsk->flags = PF_DUMPCORE;
10c28d937   Alex Kelly   coredump: move co...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  	if (atomic_read(&mm->mm_users) == nr + 1)
  		goto done;
  	/*
  	 * We should find and kill all tasks which use this mm, and we should
  	 * count them correctly into ->nr_threads. We don't take tasklist
  	 * lock, but this is safe wrt:
  	 *
  	 * fork:
  	 *	None of sub-threads can fork after zap_process(leader). All
  	 *	processes which were created before this point should be
  	 *	visible to zap_threads() because copy_process() adds the new
  	 *	process to the tail of init_task.tasks list, and lock/unlock
  	 *	of ->siglock provides a memory barrier.
  	 *
  	 * do_exit:
  	 *	The caller holds mm->mmap_sem. This means that the task which
  	 *	uses this mm can't pass exit_mm(), so it can't exit or clear
  	 *	its ->mm.
  	 *
  	 * de_thread:
  	 *	It does list_replace_rcu(&leader->tasks, &current->tasks),
  	 *	we must see either old or new leader, this does not matter.
  	 *	However, it can change p->sighand, so lock_task_sighand(p)
  	 *	must be used. Since p->mm != NULL and we hold ->mmap_sem
  	 *	it can't fail.
  	 *
  	 *	Note also that "g" can be the old leader with ->mm == NULL
  	 *	and already unhashed and thus removed from ->thread_group.
  	 *	This is OK, __unhash_process()->list_del_rcu() does not
  	 *	clear the ->next pointer, we will find the new leader via
  	 *	next_thread().
  	 */
  	rcu_read_lock();
  	for_each_process(g) {
  		if (g == tsk->group_leader)
  			continue;
  		if (g->flags & PF_KTHREAD)
  			continue;
  		p = g;
  		do {
  			if (p->mm) {
  				if (unlikely(p->mm == mm)) {
  					lock_task_sighand(p, &flags);
  					nr += zap_process(p, exit_code);
6cd8f0aca   Oleg Nesterov   coredump: ensure ...
335
  					p->signal->flags = SIGNAL_GROUP_EXIT;
10c28d937   Alex Kelly   coredump: move co...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  					unlock_task_sighand(p, &flags);
  				}
  				break;
  			}
  		} while_each_thread(g, p);
  	}
  	rcu_read_unlock();
  done:
  	atomic_set(&core_state->nr_threads, nr);
  	return nr;
  }
  
  static int coredump_wait(int exit_code, struct core_state *core_state)
  {
  	struct task_struct *tsk = current;
  	struct mm_struct *mm = tsk->mm;
  	int core_waiters = -EBUSY;
  
  	init_completion(&core_state->startup);
  	core_state->dumper.task = tsk;
  	core_state->dumper.next = NULL;
  
  	down_write(&mm->mmap_sem);
  	if (!mm->core_state)
  		core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  	up_write(&mm->mmap_sem);
  
  	if (core_waiters > 0) {
  		struct core_thread *ptr;
  
  		wait_for_completion(&core_state->startup);
  		/*
  		 * Wait for all the threads to become inactive, so that
  		 * all the thread context (extended register state, like
  		 * fpu etc) gets copied to the memory.
  		 */
  		ptr = core_state->dumper.next;
  		while (ptr != NULL) {
  			wait_task_inactive(ptr->task, 0);
  			ptr = ptr->next;
  		}
  	}
  
  	return core_waiters;
  }
acdedd99b   Oleg Nesterov   coredump: sanitiz...
381
  static void coredump_finish(struct mm_struct *mm, bool core_dumped)
10c28d937   Alex Kelly   coredump: move co...
382
383
384
  {
  	struct core_thread *curr, *next;
  	struct task_struct *task;
6cd8f0aca   Oleg Nesterov   coredump: ensure ...
385
  	spin_lock_irq(&current->sighand->siglock);
acdedd99b   Oleg Nesterov   coredump: sanitiz...
386
387
  	if (core_dumped && !__fatal_signal_pending(current))
  		current->signal->group_exit_code |= 0x80;
6cd8f0aca   Oleg Nesterov   coredump: ensure ...
388
389
390
  	current->signal->group_exit_task = NULL;
  	current->signal->flags = SIGNAL_GROUP_EXIT;
  	spin_unlock_irq(&current->sighand->siglock);
10c28d937   Alex Kelly   coredump: move co...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  	next = mm->core_state->dumper.next;
  	while ((curr = next) != NULL) {
  		next = curr->next;
  		task = curr->task;
  		/*
  		 * see exit_mm(), curr->task must not see
  		 * ->task == NULL before we read ->next.
  		 */
  		smp_mb();
  		curr->task = NULL;
  		wake_up_process(task);
  	}
  
  	mm->core_state = NULL;
  }
528f827ee   Oleg Nesterov   coredump: introdu...
406
407
408
409
410
411
412
413
414
415
  static bool dump_interrupted(void)
  {
  	/*
  	 * SIGKILL or freezing() interrupt the coredumping. Perhaps we
  	 * can do try_to_freeze() and check __fatal_signal_pending(),
  	 * but then we need to teach dump_write() to restart and clear
  	 * TIF_SIGPENDING.
  	 */
  	return signal_pending(current);
  }
10c28d937   Alex Kelly   coredump: move co...
416
417
  static void wait_for_dump_helpers(struct file *file)
  {
de32ec4cf   Al Viro   pipe: set file->p...
418
  	struct pipe_inode_info *pipe = file->private_data;
10c28d937   Alex Kelly   coredump: move co...
419
420
421
422
  
  	pipe_lock(pipe);
  	pipe->readers++;
  	pipe->writers--;
dc7ee2aac   Oleg Nesterov   coredump: change ...
423
424
425
  	wake_up_interruptible_sync(&pipe->wait);
  	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  	pipe_unlock(pipe);
10c28d937   Alex Kelly   coredump: move co...
426

dc7ee2aac   Oleg Nesterov   coredump: change ...
427
428
429
430
431
  	/*
  	 * We actually want wait_event_freezable() but then we need
  	 * to clear TIF_SIGPENDING and improve dump_interrupted().
  	 */
  	wait_event_interruptible(pipe->wait, pipe->readers == 1);
10c28d937   Alex Kelly   coredump: move co...
432

dc7ee2aac   Oleg Nesterov   coredump: change ...
433
  	pipe_lock(pipe);
10c28d937   Alex Kelly   coredump: move co...
434
435
436
  	pipe->readers--;
  	pipe->writers++;
  	pipe_unlock(pipe);
10c28d937   Alex Kelly   coredump: move co...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  }
  
  /*
   * umh_pipe_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)
   * for the process.  Returns 0 on success, or
   * PTR_ERR on failure.
   * Note that it also sets the core limit to 1.  This
   * is a special value that we use to trap recursive
   * core dumps
   */
  static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
  {
  	struct file *files[2];
  	struct coredump_params *cp = (struct coredump_params *)info->data;
  	int err = create_pipe_files(files, 0);
  	if (err)
  		return err;
  
  	cp->file = files[1];
45525b26a   Al Viro   fix a leak in rep...
459
460
  	err = replace_fd(0, files[0], 0);
  	fput(files[0]);
10c28d937   Alex Kelly   coredump: move co...
461
462
  	/* and disallow core files too */
  	current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
45525b26a   Al Viro   fix a leak in rep...
463
  	return err;
10c28d937   Alex Kelly   coredump: move co...
464
  }
541880d9a   Al Viro   do_coredump(): ge...
465
  void do_coredump(siginfo_t *siginfo)
10c28d937   Alex Kelly   coredump: move co...
466
467
468
469
470
471
472
473
474
475
476
477
  {
  	struct core_state core_state;
  	struct core_name cn;
  	struct mm_struct *mm = current->mm;
  	struct linux_binfmt * binfmt;
  	const struct cred *old_cred;
  	struct cred *cred;
  	int retval = 0;
  	int flag = 0;
  	int ispipe;
  	struct files_struct *displaced;
  	bool need_nonrelative = false;
acdedd99b   Oleg Nesterov   coredump: sanitiz...
478
  	bool core_dumped = false;
10c28d937   Alex Kelly   coredump: move co...
479
480
  	static atomic_t core_dump_count = ATOMIC_INIT(0);
  	struct coredump_params cprm = {
5ab1c309b   Denys Vlasenko   coredump: pass si...
481
  		.siginfo = siginfo,
541880d9a   Al Viro   do_coredump(): ge...
482
  		.regs = signal_pt_regs(),
10c28d937   Alex Kelly   coredump: move co...
483
484
485
486
487
488
489
490
  		.limit = rlimit(RLIMIT_CORE),
  		/*
  		 * We must use the same mm->flags while dumping core to avoid
  		 * inconsistency of bit flags, since this flag is not protected
  		 * by any locks.
  		 */
  		.mm_flags = mm->flags,
  	};
5ab1c309b   Denys Vlasenko   coredump: pass si...
491
  	audit_core_dumps(siginfo->si_signo);
10c28d937   Alex Kelly   coredump: move co...
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  
  	binfmt = mm->binfmt;
  	if (!binfmt || !binfmt->core_dump)
  		goto fail;
  	if (!__get_dumpable(cprm.mm_flags))
  		goto fail;
  
  	cred = prepare_creds();
  	if (!cred)
  		goto fail;
  	/*
  	 * We cannot trust fsuid as being the "true" uid of the process
  	 * nor do we know its entire history. We only know it was tainted
  	 * so we dump it as root in mode 2, and only into a controlled
  	 * environment (pipe handler or fully qualified path).
  	 */
e579d2c25   Kees Cook   coredump: remove ...
508
  	if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
10c28d937   Alex Kelly   coredump: move co...
509
510
511
512
513
  		/* Setuid core dump mode */
  		flag = O_EXCL;		/* Stop rewrite attacks */
  		cred->fsuid = GLOBAL_ROOT_UID;	/* Dump root private */
  		need_nonrelative = true;
  	}
5ab1c309b   Denys Vlasenko   coredump: pass si...
514
  	retval = coredump_wait(siginfo->si_signo, &core_state);
10c28d937   Alex Kelly   coredump: move co...
515
516
517
518
  	if (retval < 0)
  		goto fail_creds;
  
  	old_cred = override_creds(cred);
12a2b4b22   Oleg Nesterov   coredump: add sup...
519
  	ispipe = format_corename(&cn, &cprm);
10c28d937   Alex Kelly   coredump: move co...
520

fb96c475f   Lucas De Marchi   coredump: remove ...
521
  	if (ispipe) {
10c28d937   Alex Kelly   coredump: move co...
522
523
  		int dump_count;
  		char **helper_argv;
907ed1328   Lucas De Marchi   usermodehelper: s...
524
  		struct subprocess_info *sub_info;
10c28d937   Alex Kelly   coredump: move co...
525
526
527
528
529
530
  
  		if (ispipe < 0) {
  			printk(KERN_WARNING "format_corename failed
  ");
  			printk(KERN_WARNING "Aborting core
  ");
e7fd1549a   Oleg Nesterov   coredump: format_...
531
  			goto fail_unlock;
10c28d937   Alex Kelly   coredump: move co...
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  		}
  
  		if (cprm.limit == 1) {
  			/* See umh_pipe_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
  			 * cprm.limit of 1 here as a speacial value, this is a
  			 * consistent way to catch recursive crashes.
  			 * We can still crash if the core_pattern binary sets
  			 * RLIM_CORE = !1, but it runs as root, and can do
  			 * lots of stupid things.
  			 *
  			 * Note that we use task_tgid_vnr here to grab the pid
  			 * of the process group leader.  That way we get the
  			 * right pid if a thread in a multi-threaded
  			 * core_pattern process dies.
  			 */
  			printk(KERN_WARNING
  				"Process %d(%s) has RLIMIT_CORE set to 1
  ",
  				task_tgid_vnr(current), current->comm);
  			printk(KERN_WARNING "Aborting core
  ");
  			goto fail_unlock;
  		}
  		cprm.limit = RLIM_INFINITY;
  
  		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
  ",
  			       task_tgid_vnr(current), current->comm);
  			printk(KERN_WARNING "Skipping core dump
  ");
  			goto fail_dropcount;
  		}
888ffc592   Oleg Nesterov   coredump: '% at t...
569
  		helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL);
10c28d937   Alex Kelly   coredump: move co...
570
571
572
573
574
575
  		if (!helper_argv) {
  			printk(KERN_WARNING "%s failed to allocate memory
  ",
  			       __func__);
  			goto fail_dropcount;
  		}
907ed1328   Lucas De Marchi   usermodehelper: s...
576
577
578
579
580
581
582
  		retval = -ENOMEM;
  		sub_info = call_usermodehelper_setup(helper_argv[0],
  						helper_argv, NULL, GFP_KERNEL,
  						umh_pipe_setup, NULL, &cprm);
  		if (sub_info)
  			retval = call_usermodehelper_exec(sub_info,
  							  UMH_WAIT_EXEC);
10c28d937   Alex Kelly   coredump: move co...
583
584
  		argv_free(helper_argv);
  		if (retval) {
888ffc592   Oleg Nesterov   coredump: '% at t...
585
586
  			printk(KERN_INFO "Core dump to |%s pipe failed
  ",
10c28d937   Alex Kelly   coredump: move co...
587
588
  			       cn.corename);
  			goto close_fail;
fb96c475f   Lucas De Marchi   coredump: remove ...
589
  		}
10c28d937   Alex Kelly   coredump: move co...
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  	} else {
  		struct inode *inode;
  
  		if (cprm.limit < binfmt->min_coredump)
  			goto fail_unlock;
  
  		if (need_nonrelative && cn.corename[0] != '/') {
  			printk(KERN_WARNING "Pid %d(%s) can only dump core "\
  				"to fully qualified path!
  ",
  				task_tgid_vnr(current), current->comm);
  			printk(KERN_WARNING "Skipping core dump
  ");
  			goto fail_unlock;
  		}
  
  		cprm.file = filp_open(cn.corename,
  				 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  				 0600);
  		if (IS_ERR(cprm.file))
  			goto fail_unlock;
496ad9aa8   Al Viro   new helper: file_...
611
  		inode = file_inode(cprm.file);
10c28d937   Alex Kelly   coredump: move co...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  		if (inode->i_nlink > 1)
  			goto close_fail;
  		if (d_unhashed(cprm.file->f_path.dentry))
  			goto close_fail;
  		/*
  		 * AK: actually i see no reason to not allow this for named
  		 * pipes etc, but keep the previous behaviour for now.
  		 */
  		if (!S_ISREG(inode->i_mode))
  			goto close_fail;
  		/*
  		 * Dont allow local users get cute and trick others to coredump
  		 * into their pre-created files.
  		 */
  		if (!uid_eq(inode->i_uid, current_fsuid()))
  			goto close_fail;
  		if (!cprm.file->f_op || !cprm.file->f_op->write)
  			goto close_fail;
  		if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
  			goto close_fail;
  	}
  
  	/* get us an unshared descriptor table; almost always a no-op */
  	retval = unshare_files(&displaced);
  	if (retval)
  		goto close_fail;
  	if (displaced)
  		put_files_struct(displaced);
e86d35c38   Al Viro   do_coredump(): do...
640
641
642
643
644
  	if (!dump_interrupted()) {
  		file_start_write(cprm.file);
  		core_dumped = binfmt->core_dump(&cprm);
  		file_end_write(cprm.file);
  	}
10c28d937   Alex Kelly   coredump: move co...
645
646
647
648
649
650
651
652
653
654
  	if (ispipe && core_pipe_limit)
  		wait_for_dump_helpers(cprm.file);
  close_fail:
  	if (cprm.file)
  		filp_close(cprm.file, NULL);
  fail_dropcount:
  	if (ispipe)
  		atomic_dec(&core_dump_count);
  fail_unlock:
  	kfree(cn.corename);
acdedd99b   Oleg Nesterov   coredump: sanitiz...
655
  	coredump_finish(mm, core_dumped);
10c28d937   Alex Kelly   coredump: move co...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
  	revert_creds(old_cred);
  fail_creds:
  	put_cred(cred);
  fail:
  	return;
  }
  
  /*
   * Core dumping helper functions.  These are the only things you should
   * do on a core-file: use only these functions to write out all the
   * necessary info.
   */
  int dump_write(struct file *file, const void *addr, int nr)
  {
528f827ee   Oleg Nesterov   coredump: introdu...
670
671
672
  	return !dump_interrupted() &&
  		access_ok(VERIFY_READ, addr, nr) &&
  		file->f_op->write(file, addr, nr, &file->f_pos) == nr;
10c28d937   Alex Kelly   coredump: move co...
673
674
675
676
677
678
679
680
  }
  EXPORT_SYMBOL(dump_write);
  
  int dump_seek(struct file *file, loff_t off)
  {
  	int ret = 1;
  
  	if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
528f827ee   Oleg Nesterov   coredump: introdu...
681
682
  		if (dump_interrupted() ||
  		    file->f_op->llseek(file, off, SEEK_CUR) < 0)
10c28d937   Alex Kelly   coredump: move co...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
  			return 0;
  	} else {
  		char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  
  		if (!buf)
  			return 0;
  		while (off > 0) {
  			unsigned long n = off;
  
  			if (n > PAGE_SIZE)
  				n = PAGE_SIZE;
  			if (!dump_write(file, buf, n)) {
  				ret = 0;
  				break;
  			}
  			off -= n;
  		}
  		free_page((unsigned long)buf);
  	}
  	return ret;
  }
  EXPORT_SYMBOL(dump_seek);