coredump.c 21 KB

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