coredump.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. #include <linux/slab.h>
  2. #include <linux/file.h>
  3. #include <linux/fdtable.h>
  4. #include <linux/freezer.h>
  5. #include <linux/mm.h>
  6. #include <linux/stat.h>
  7. #include <linux/fcntl.h>
  8. #include <linux/swap.h>
  9. #include <linux/string.h>
  10. #include <linux/init.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/highmem.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/key.h>
  16. #include <linux/personality.h>
  17. #include <linux/binfmts.h>
  18. #include <linux/coredump.h>
  19. #include <linux/utsname.h>
  20. #include <linux/pid_namespace.h>
  21. #include <linux/module.h>
  22. #include <linux/namei.h>
  23. #include <linux/mount.h>
  24. #include <linux/security.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/tsacct_kern.h>
  27. #include <linux/cn_proc.h>
  28. #include <linux/audit.h>
  29. #include <linux/tracehook.h>
  30. #include <linux/kmod.h>
  31. #include <linux/fsnotify.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/pipe_fs_i.h>
  34. #include <linux/oom.h>
  35. #include <linux/compat.h>
  36. #include <linux/sched.h>
  37. #include <linux/fs.h>
  38. #include <linux/path.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/tlb.h>
  42. #include <asm/exec.h>
  43. #include <trace/events/task.h>
  44. #include "internal.h"
  45. #include <trace/events/sched.h>
  46. int core_uses_pid;
  47. unsigned int core_pipe_limit;
  48. char core_pattern[CORENAME_MAX_SIZE] = "core";
  49. static int core_name_size = CORENAME_MAX_SIZE;
  50. struct core_name {
  51. char *corename;
  52. int used, size;
  53. };
  54. /* The maximal length of core_pattern is also specified in sysctl.c */
  55. static int expand_corename(struct core_name *cn, int size)
  56. {
  57. char *corename = krealloc(cn->corename, size, GFP_KERNEL);
  58. if (!corename)
  59. return -ENOMEM;
  60. if (size > core_name_size) /* racy but harmless */
  61. core_name_size = size;
  62. cn->size = ksize(corename);
  63. cn->corename = corename;
  64. return 0;
  65. }
  66. static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
  67. va_list arg)
  68. {
  69. int free, need;
  70. va_list arg_copy;
  71. again:
  72. free = cn->size - cn->used;
  73. va_copy(arg_copy, arg);
  74. need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
  75. va_end(arg_copy);
  76. if (need < free) {
  77. cn->used += need;
  78. return 0;
  79. }
  80. if (!expand_corename(cn, cn->size + need - free + 1))
  81. goto again;
  82. return -ENOMEM;
  83. }
  84. static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
  85. {
  86. va_list arg;
  87. int ret;
  88. va_start(arg, fmt);
  89. ret = cn_vprintf(cn, fmt, arg);
  90. va_end(arg);
  91. return ret;
  92. }
  93. static __printf(2, 3)
  94. int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
  95. {
  96. int cur = cn->used;
  97. va_list arg;
  98. int ret;
  99. va_start(arg, fmt);
  100. ret = cn_vprintf(cn, fmt, arg);
  101. va_end(arg);
  102. for (; cur < cn->used; ++cur) {
  103. if (cn->corename[cur] == '/')
  104. cn->corename[cur] = '!';
  105. }
  106. return ret;
  107. }
  108. static int cn_print_exe_file(struct core_name *cn)
  109. {
  110. struct file *exe_file;
  111. char *pathbuf, *path;
  112. int ret;
  113. exe_file = get_mm_exe_file(current->mm);
  114. if (!exe_file)
  115. return cn_esc_printf(cn, "%s (path unknown)", current->comm);
  116. pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
  117. if (!pathbuf) {
  118. ret = -ENOMEM;
  119. goto put_exe_file;
  120. }
  121. path = file_path(exe_file, pathbuf, PATH_MAX);
  122. if (IS_ERR(path)) {
  123. ret = PTR_ERR(path);
  124. goto free_buf;
  125. }
  126. ret = cn_esc_printf(cn, "%s", path);
  127. free_buf:
  128. kfree(pathbuf);
  129. put_exe_file:
  130. fput(exe_file);
  131. return ret;
  132. }
  133. /* format_corename will inspect the pattern parameter, and output a
  134. * name into corename, which must have space for at least
  135. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  136. */
  137. static int format_corename(struct core_name *cn, struct coredump_params *cprm)
  138. {
  139. const struct cred *cred = current_cred();
  140. const char *pat_ptr = core_pattern;
  141. int ispipe = (*pat_ptr == '|');
  142. int pid_in_pattern = 0;
  143. int err = 0;
  144. cn->used = 0;
  145. cn->corename = NULL;
  146. if (expand_corename(cn, core_name_size))
  147. return -ENOMEM;
  148. cn->corename[0] = '\0';
  149. if (ispipe)
  150. ++pat_ptr;
  151. /* Repeat as long as we have more pattern to process and more output
  152. space */
  153. while (*pat_ptr) {
  154. if (*pat_ptr != '%') {
  155. err = cn_printf(cn, "%c", *pat_ptr++);
  156. } else {
  157. switch (*++pat_ptr) {
  158. /* single % at the end, drop that */
  159. case 0:
  160. goto out;
  161. /* Double percent, output one percent */
  162. case '%':
  163. err = cn_printf(cn, "%c", '%');
  164. break;
  165. /* pid */
  166. case 'p':
  167. pid_in_pattern = 1;
  168. err = cn_printf(cn, "%d",
  169. task_tgid_vnr(current));
  170. break;
  171. /* global pid */
  172. case 'P':
  173. err = cn_printf(cn, "%d",
  174. task_tgid_nr(current));
  175. break;
  176. case 'i':
  177. err = cn_printf(cn, "%d",
  178. task_pid_vnr(current));
  179. break;
  180. case 'I':
  181. err = cn_printf(cn, "%d",
  182. task_pid_nr(current));
  183. break;
  184. /* uid */
  185. case 'u':
  186. err = cn_printf(cn, "%u",
  187. from_kuid(&init_user_ns,
  188. cred->uid));
  189. break;
  190. /* gid */
  191. case 'g':
  192. err = cn_printf(cn, "%u",
  193. from_kgid(&init_user_ns,
  194. cred->gid));
  195. break;
  196. case 'd':
  197. err = cn_printf(cn, "%d",
  198. __get_dumpable(cprm->mm_flags));
  199. break;
  200. /* signal that caused the coredump */
  201. case 's':
  202. err = cn_printf(cn, "%d",
  203. cprm->siginfo->si_signo);
  204. break;
  205. /* UNIX time of coredump */
  206. case 't': {
  207. struct timeval tv;
  208. do_gettimeofday(&tv);
  209. err = cn_printf(cn, "%lu", tv.tv_sec);
  210. break;
  211. }
  212. /* hostname */
  213. case 'h':
  214. down_read(&uts_sem);
  215. err = cn_esc_printf(cn, "%s",
  216. utsname()->nodename);
  217. up_read(&uts_sem);
  218. break;
  219. /* executable */
  220. case 'e':
  221. err = cn_esc_printf(cn, "%s", current->comm);
  222. break;
  223. case 'E':
  224. err = cn_print_exe_file(cn);
  225. break;
  226. /* core limit size */
  227. case 'c':
  228. err = cn_printf(cn, "%lu",
  229. rlimit(RLIMIT_CORE));
  230. break;
  231. default:
  232. break;
  233. }
  234. ++pat_ptr;
  235. }
  236. if (err)
  237. return err;
  238. }
  239. out:
  240. /* Backward compatibility with core_uses_pid:
  241. *
  242. * If core_pattern does not include a %p (as is the default)
  243. * and core_uses_pid is set, then .%pid will be appended to
  244. * the filename. Do not do this for piped commands. */
  245. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  246. err = cn_printf(cn, ".%d", task_tgid_vnr(current));
  247. if (err)
  248. return err;
  249. }
  250. return ispipe;
  251. }
  252. static int zap_process(struct task_struct *start, int exit_code, int flags)
  253. {
  254. struct task_struct *t;
  255. int nr = 0;
  256. /* ignore all signals except SIGKILL, see prepare_signal() */
  257. start->signal->flags = SIGNAL_GROUP_COREDUMP | flags;
  258. start->signal->group_exit_code = exit_code;
  259. start->signal->group_stop_count = 0;
  260. for_each_thread(start, t) {
  261. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  262. if (t != current && t->mm) {
  263. sigaddset(&t->pending.signal, SIGKILL);
  264. signal_wake_up(t, 1);
  265. nr++;
  266. }
  267. }
  268. return nr;
  269. }
  270. static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  271. struct core_state *core_state, int exit_code)
  272. {
  273. struct task_struct *g, *p;
  274. unsigned long flags;
  275. int nr = -EAGAIN;
  276. spin_lock_irq(&tsk->sighand->siglock);
  277. if (!signal_group_exit(tsk->signal)) {
  278. mm->core_state = core_state;
  279. tsk->signal->group_exit_task = tsk;
  280. nr = zap_process(tsk, exit_code, 0);
  281. clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
  282. }
  283. spin_unlock_irq(&tsk->sighand->siglock);
  284. if (unlikely(nr < 0))
  285. return nr;
  286. tsk->flags |= PF_DUMPCORE;
  287. if (atomic_read(&mm->mm_users) == nr + 1)
  288. goto done;
  289. /*
  290. * We should find and kill all tasks which use this mm, and we should
  291. * count them correctly into ->nr_threads. We don't take tasklist
  292. * lock, but this is safe wrt:
  293. *
  294. * fork:
  295. * None of sub-threads can fork after zap_process(leader). All
  296. * processes which were created before this point should be
  297. * visible to zap_threads() because copy_process() adds the new
  298. * process to the tail of init_task.tasks list, and lock/unlock
  299. * of ->siglock provides a memory barrier.
  300. *
  301. * do_exit:
  302. * The caller holds mm->mmap_sem. This means that the task which
  303. * uses this mm can't pass exit_mm(), so it can't exit or clear
  304. * its ->mm.
  305. *
  306. * de_thread:
  307. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  308. * we must see either old or new leader, this does not matter.
  309. * However, it can change p->sighand, so lock_task_sighand(p)
  310. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  311. * it can't fail.
  312. *
  313. * Note also that "g" can be the old leader with ->mm == NULL
  314. * and already unhashed and thus removed from ->thread_group.
  315. * This is OK, __unhash_process()->list_del_rcu() does not
  316. * clear the ->next pointer, we will find the new leader via
  317. * next_thread().
  318. */
  319. rcu_read_lock();
  320. for_each_process(g) {
  321. if (g == tsk->group_leader)
  322. continue;
  323. if (g->flags & PF_KTHREAD)
  324. continue;
  325. for_each_thread(g, p) {
  326. if (unlikely(!p->mm))
  327. continue;
  328. if (unlikely(p->mm == mm)) {
  329. lock_task_sighand(p, &flags);
  330. nr += zap_process(p, exit_code,
  331. SIGNAL_GROUP_EXIT);
  332. unlock_task_sighand(p, &flags);
  333. }
  334. break;
  335. }
  336. }
  337. rcu_read_unlock();
  338. done:
  339. atomic_set(&core_state->nr_threads, nr);
  340. return nr;
  341. }
  342. static int coredump_wait(int exit_code, struct core_state *core_state)
  343. {
  344. struct task_struct *tsk = current;
  345. struct mm_struct *mm = tsk->mm;
  346. int core_waiters = -EBUSY;
  347. init_completion(&core_state->startup);
  348. core_state->dumper.task = tsk;
  349. core_state->dumper.next = NULL;
  350. down_write(&mm->mmap_sem);
  351. if (!mm->core_state)
  352. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  353. up_write(&mm->mmap_sem);
  354. if (core_waiters > 0) {
  355. struct core_thread *ptr;
  356. freezer_do_not_count();
  357. wait_for_completion(&core_state->startup);
  358. freezer_count();
  359. /*
  360. * Wait for all the threads to become inactive, so that
  361. * all the thread context (extended register state, like
  362. * fpu etc) gets copied to the memory.
  363. */
  364. ptr = core_state->dumper.next;
  365. while (ptr != NULL) {
  366. wait_task_inactive(ptr->task, 0);
  367. ptr = ptr->next;
  368. }
  369. }
  370. return core_waiters;
  371. }
  372. static void coredump_finish(struct mm_struct *mm, bool core_dumped)
  373. {
  374. struct core_thread *curr, *next;
  375. struct task_struct *task;
  376. spin_lock_irq(&current->sighand->siglock);
  377. if (core_dumped && !__fatal_signal_pending(current))
  378. current->signal->group_exit_code |= 0x80;
  379. current->signal->group_exit_task = NULL;
  380. current->signal->flags = SIGNAL_GROUP_EXIT;
  381. spin_unlock_irq(&current->sighand->siglock);
  382. next = mm->core_state->dumper.next;
  383. while ((curr = next) != NULL) {
  384. next = curr->next;
  385. task = curr->task;
  386. /*
  387. * see exit_mm(), curr->task must not see
  388. * ->task == NULL before we read ->next.
  389. */
  390. smp_mb();
  391. curr->task = NULL;
  392. wake_up_process(task);
  393. }
  394. mm->core_state = NULL;
  395. }
  396. static bool dump_interrupted(void)
  397. {
  398. /*
  399. * SIGKILL or freezing() interrupt the coredumping. Perhaps we
  400. * can do try_to_freeze() and check __fatal_signal_pending(),
  401. * but then we need to teach dump_write() to restart and clear
  402. * TIF_SIGPENDING.
  403. */
  404. return signal_pending(current);
  405. }
  406. static void wait_for_dump_helpers(struct file *file)
  407. {
  408. struct pipe_inode_info *pipe = file->private_data;
  409. pipe_lock(pipe);
  410. pipe->readers++;
  411. pipe->writers--;
  412. wake_up_interruptible_sync(&pipe->wait);
  413. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  414. pipe_unlock(pipe);
  415. /*
  416. * We actually want wait_event_freezable() but then we need
  417. * to clear TIF_SIGPENDING and improve dump_interrupted().
  418. */
  419. wait_event_interruptible(pipe->wait, pipe->readers == 1);
  420. pipe_lock(pipe);
  421. pipe->readers--;
  422. pipe->writers++;
  423. pipe_unlock(pipe);
  424. }
  425. /*
  426. * umh_pipe_setup
  427. * helper function to customize the process used
  428. * to collect the core in userspace. Specifically
  429. * it sets up a pipe and installs it as fd 0 (stdin)
  430. * for the process. Returns 0 on success, or
  431. * PTR_ERR on failure.
  432. * Note that it also sets the core limit to 1. This
  433. * is a special value that we use to trap recursive
  434. * core dumps
  435. */
  436. static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
  437. {
  438. struct file *files[2];
  439. struct coredump_params *cp = (struct coredump_params *)info->data;
  440. int err = create_pipe_files(files, 0);
  441. if (err)
  442. return err;
  443. cp->file = files[1];
  444. err = replace_fd(0, files[0], 0);
  445. fput(files[0]);
  446. /* and disallow core files too */
  447. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
  448. return err;
  449. }
  450. void do_coredump(const siginfo_t *siginfo)
  451. {
  452. struct core_state core_state;
  453. struct core_name cn;
  454. struct mm_struct *mm = current->mm;
  455. struct linux_binfmt * binfmt;
  456. const struct cred *old_cred;
  457. struct cred *cred;
  458. int retval = 0;
  459. int ispipe;
  460. struct files_struct *displaced;
  461. /* require nonrelative corefile path and be extra careful */
  462. bool need_suid_safe = false;
  463. bool core_dumped = false;
  464. static atomic_t core_dump_count = ATOMIC_INIT(0);
  465. struct coredump_params cprm = {
  466. .siginfo = siginfo,
  467. .regs = signal_pt_regs(),
  468. .limit = rlimit(RLIMIT_CORE),
  469. /*
  470. * We must use the same mm->flags while dumping core to avoid
  471. * inconsistency of bit flags, since this flag is not protected
  472. * by any locks.
  473. */
  474. .mm_flags = mm->flags,
  475. };
  476. audit_core_dumps(siginfo->si_signo);
  477. binfmt = mm->binfmt;
  478. if (!binfmt || !binfmt->core_dump)
  479. goto fail;
  480. if (!__get_dumpable(cprm.mm_flags))
  481. goto fail;
  482. cred = prepare_creds();
  483. if (!cred)
  484. goto fail;
  485. /*
  486. * We cannot trust fsuid as being the "true" uid of the process
  487. * nor do we know its entire history. We only know it was tainted
  488. * so we dump it as root in mode 2, and only into a controlled
  489. * environment (pipe handler or fully qualified path).
  490. */
  491. if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
  492. /* Setuid core dump mode */
  493. cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
  494. need_suid_safe = true;
  495. }
  496. retval = coredump_wait(siginfo->si_signo, &core_state);
  497. if (retval < 0)
  498. goto fail_creds;
  499. old_cred = override_creds(cred);
  500. ispipe = format_corename(&cn, &cprm);
  501. if (ispipe) {
  502. int dump_count;
  503. char **helper_argv;
  504. struct subprocess_info *sub_info;
  505. if (ispipe < 0) {
  506. printk(KERN_WARNING "format_corename failed\n");
  507. printk(KERN_WARNING "Aborting core\n");
  508. goto fail_unlock;
  509. }
  510. if (cprm.limit == 1) {
  511. /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
  512. *
  513. * Normally core limits are irrelevant to pipes, since
  514. * we're not writing to the file system, but we use
  515. * cprm.limit of 1 here as a special value, this is a
  516. * consistent way to catch recursive crashes.
  517. * We can still crash if the core_pattern binary sets
  518. * RLIM_CORE = !1, but it runs as root, and can do
  519. * lots of stupid things.
  520. *
  521. * Note that we use task_tgid_vnr here to grab the pid
  522. * of the process group leader. That way we get the
  523. * right pid if a thread in a multi-threaded
  524. * core_pattern process dies.
  525. */
  526. printk(KERN_WARNING
  527. "Process %d(%s) has RLIMIT_CORE set to 1\n",
  528. task_tgid_vnr(current), current->comm);
  529. printk(KERN_WARNING "Aborting core\n");
  530. goto fail_unlock;
  531. }
  532. cprm.limit = RLIM_INFINITY;
  533. dump_count = atomic_inc_return(&core_dump_count);
  534. if (core_pipe_limit && (core_pipe_limit < dump_count)) {
  535. printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
  536. task_tgid_vnr(current), current->comm);
  537. printk(KERN_WARNING "Skipping core dump\n");
  538. goto fail_dropcount;
  539. }
  540. helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL);
  541. if (!helper_argv) {
  542. printk(KERN_WARNING "%s failed to allocate memory\n",
  543. __func__);
  544. goto fail_dropcount;
  545. }
  546. retval = -ENOMEM;
  547. sub_info = call_usermodehelper_setup(helper_argv[0],
  548. helper_argv, NULL, GFP_KERNEL,
  549. umh_pipe_setup, NULL, &cprm);
  550. if (sub_info)
  551. retval = call_usermodehelper_exec(sub_info,
  552. UMH_WAIT_EXEC);
  553. argv_free(helper_argv);
  554. if (retval) {
  555. printk(KERN_INFO "Core dump to |%s pipe failed\n",
  556. cn.corename);
  557. goto close_fail;
  558. }
  559. } else {
  560. struct inode *inode;
  561. int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
  562. O_LARGEFILE | O_EXCL;
  563. if (cprm.limit < binfmt->min_coredump)
  564. goto fail_unlock;
  565. if (need_suid_safe && cn.corename[0] != '/') {
  566. printk(KERN_WARNING "Pid %d(%s) can only dump core "\
  567. "to fully qualified path!\n",
  568. task_tgid_vnr(current), current->comm);
  569. printk(KERN_WARNING "Skipping core dump\n");
  570. goto fail_unlock;
  571. }
  572. /*
  573. * Unlink the file if it exists unless this is a SUID
  574. * binary - in that case, we're running around with root
  575. * privs and don't want to unlink another user's coredump.
  576. */
  577. if (!need_suid_safe) {
  578. mm_segment_t old_fs;
  579. old_fs = get_fs();
  580. set_fs(KERNEL_DS);
  581. /*
  582. * If it doesn't exist, that's fine. If there's some
  583. * other problem, we'll catch it at the filp_open().
  584. */
  585. (void) sys_unlink((const char __user *)cn.corename);
  586. set_fs(old_fs);
  587. }
  588. /*
  589. * There is a race between unlinking and creating the
  590. * file, but if that causes an EEXIST here, that's
  591. * fine - another process raced with us while creating
  592. * the corefile, and the other process won. To userspace,
  593. * what matters is that at least one of the two processes
  594. * writes its coredump successfully, not which one.
  595. */
  596. if (need_suid_safe) {
  597. /*
  598. * Using user namespaces, normal user tasks can change
  599. * their current->fs->root to point to arbitrary
  600. * directories. Since the intention of the "only dump
  601. * with a fully qualified path" rule is to control where
  602. * coredumps may be placed using root privileges,
  603. * current->fs->root must not be used. Instead, use the
  604. * root directory of init_task.
  605. */
  606. struct path root;
  607. task_lock(&init_task);
  608. get_fs_root(init_task.fs, &root);
  609. task_unlock(&init_task);
  610. cprm.file = file_open_root(root.dentry, root.mnt,
  611. cn.corename, open_flags, 0600);
  612. path_put(&root);
  613. } else {
  614. cprm.file = filp_open(cn.corename, open_flags, 0600);
  615. }
  616. if (IS_ERR(cprm.file))
  617. goto fail_unlock;
  618. inode = file_inode(cprm.file);
  619. if (inode->i_nlink > 1)
  620. goto close_fail;
  621. if (d_unhashed(cprm.file->f_path.dentry))
  622. goto close_fail;
  623. /*
  624. * AK: actually i see no reason to not allow this for named
  625. * pipes etc, but keep the previous behaviour for now.
  626. */
  627. if (!S_ISREG(inode->i_mode))
  628. goto close_fail;
  629. /*
  630. * Don't dump core if the filesystem changed owner or mode
  631. * of the file during file creation. This is an issue when
  632. * a process dumps core while its cwd is e.g. on a vfat
  633. * filesystem.
  634. */
  635. if (!uid_eq(inode->i_uid, current_fsuid()))
  636. goto close_fail;
  637. if ((inode->i_mode & 0677) != 0600)
  638. goto close_fail;
  639. if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
  640. goto close_fail;
  641. if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
  642. goto close_fail;
  643. }
  644. /* get us an unshared descriptor table; almost always a no-op */
  645. retval = unshare_files(&displaced);
  646. if (retval)
  647. goto close_fail;
  648. if (displaced)
  649. put_files_struct(displaced);
  650. if (!dump_interrupted()) {
  651. file_start_write(cprm.file);
  652. core_dumped = binfmt->core_dump(&cprm);
  653. file_end_write(cprm.file);
  654. }
  655. if (ispipe && core_pipe_limit)
  656. wait_for_dump_helpers(cprm.file);
  657. close_fail:
  658. if (cprm.file)
  659. filp_close(cprm.file, NULL);
  660. fail_dropcount:
  661. if (ispipe)
  662. atomic_dec(&core_dump_count);
  663. fail_unlock:
  664. kfree(cn.corename);
  665. coredump_finish(mm, core_dumped);
  666. revert_creds(old_cred);
  667. fail_creds:
  668. put_cred(cred);
  669. fail:
  670. return;
  671. }
  672. /*
  673. * Core dumping helper functions. These are the only things you should
  674. * do on a core-file: use only these functions to write out all the
  675. * necessary info.
  676. */
  677. int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
  678. {
  679. struct file *file = cprm->file;
  680. loff_t pos = file->f_pos;
  681. ssize_t n;
  682. if (cprm->written + nr > cprm->limit)
  683. return 0;
  684. while (nr) {
  685. if (dump_interrupted())
  686. return 0;
  687. n = __kernel_write(file, addr, nr, &pos);
  688. if (n <= 0)
  689. return 0;
  690. file->f_pos = pos;
  691. cprm->written += n;
  692. nr -= n;
  693. }
  694. return 1;
  695. }
  696. EXPORT_SYMBOL(dump_emit);
  697. int dump_skip(struct coredump_params *cprm, size_t nr)
  698. {
  699. static char zeroes[PAGE_SIZE];
  700. struct file *file = cprm->file;
  701. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  702. if (cprm->written + nr > cprm->limit)
  703. return 0;
  704. if (dump_interrupted() ||
  705. file->f_op->llseek(file, nr, SEEK_CUR) < 0)
  706. return 0;
  707. cprm->written += nr;
  708. return 1;
  709. } else {
  710. while (nr > PAGE_SIZE) {
  711. if (!dump_emit(cprm, zeroes, PAGE_SIZE))
  712. return 0;
  713. nr -= PAGE_SIZE;
  714. }
  715. return dump_emit(cprm, zeroes, nr);
  716. }
  717. }
  718. EXPORT_SYMBOL(dump_skip);
  719. int dump_align(struct coredump_params *cprm, int align)
  720. {
  721. unsigned mod = cprm->written & (align - 1);
  722. if (align & (align - 1))
  723. return 0;
  724. return mod ? dump_skip(cprm, align - mod) : 1;
  725. }
  726. EXPORT_SYMBOL(dump_align);
  727. /*
  728. * Ensures that file size is big enough to contain the current file
  729. * postion. This prevents gdb from complaining about a truncated file
  730. * if the last "write" to the file was dump_skip.
  731. */
  732. void dump_truncate(struct coredump_params *cprm)
  733. {
  734. struct file *file = cprm->file;
  735. loff_t offset;
  736. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  737. offset = file->f_op->llseek(file, 0, SEEK_CUR);
  738. if (i_size_read(file->f_mapping->host) < offset)
  739. do_truncate(file->f_path.dentry, offset, 0, file);
  740. }
  741. }
  742. EXPORT_SYMBOL(dump_truncate);