coredump.c 19 KB

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