coredump.c 20 KB

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