coredump.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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)
  249. {
  250. struct task_struct *t;
  251. int nr = 0;
  252. start->signal->group_exit_code = exit_code;
  253. start->signal->group_stop_count = 0;
  254. t = start;
  255. do {
  256. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  257. if (t != current && t->mm) {
  258. sigaddset(&t->pending.signal, SIGKILL);
  259. signal_wake_up(t, 1);
  260. nr++;
  261. }
  262. } while_each_thread(start, t);
  263. return nr;
  264. }
  265. static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  266. struct core_state *core_state, int exit_code)
  267. {
  268. struct task_struct *g, *p;
  269. unsigned long flags;
  270. int nr = -EAGAIN;
  271. spin_lock_irq(&tsk->sighand->siglock);
  272. if (!signal_group_exit(tsk->signal)) {
  273. mm->core_state = core_state;
  274. nr = zap_process(tsk, exit_code);
  275. tsk->signal->group_exit_task = tsk;
  276. /* ignore all signals except SIGKILL, see prepare_signal() */
  277. tsk->signal->flags = SIGNAL_GROUP_COREDUMP;
  278. clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
  279. }
  280. spin_unlock_irq(&tsk->sighand->siglock);
  281. if (unlikely(nr < 0))
  282. return nr;
  283. tsk->flags |= PF_DUMPCORE;
  284. if (atomic_read(&mm->mm_users) == nr + 1)
  285. goto done;
  286. /*
  287. * We should find and kill all tasks which use this mm, and we should
  288. * count them correctly into ->nr_threads. We don't take tasklist
  289. * lock, but this is safe wrt:
  290. *
  291. * fork:
  292. * None of sub-threads can fork after zap_process(leader). All
  293. * processes which were created before this point should be
  294. * visible to zap_threads() because copy_process() adds the new
  295. * process to the tail of init_task.tasks list, and lock/unlock
  296. * of ->siglock provides a memory barrier.
  297. *
  298. * do_exit:
  299. * The caller holds mm->mmap_sem. This means that the task which
  300. * uses this mm can't pass exit_mm(), so it can't exit or clear
  301. * its ->mm.
  302. *
  303. * de_thread:
  304. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  305. * we must see either old or new leader, this does not matter.
  306. * However, it can change p->sighand, so lock_task_sighand(p)
  307. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  308. * it can't fail.
  309. *
  310. * Note also that "g" can be the old leader with ->mm == NULL
  311. * and already unhashed and thus removed from ->thread_group.
  312. * This is OK, __unhash_process()->list_del_rcu() does not
  313. * clear the ->next pointer, we will find the new leader via
  314. * next_thread().
  315. */
  316. rcu_read_lock();
  317. for_each_process(g) {
  318. if (g == tsk->group_leader)
  319. continue;
  320. if (g->flags & PF_KTHREAD)
  321. continue;
  322. p = g;
  323. do {
  324. if (p->mm) {
  325. if (unlikely(p->mm == mm)) {
  326. lock_task_sighand(p, &flags);
  327. nr += zap_process(p, exit_code);
  328. p->signal->flags = SIGNAL_GROUP_EXIT;
  329. unlock_task_sighand(p, &flags);
  330. }
  331. break;
  332. }
  333. } while_each_thread(g, p);
  334. }
  335. rcu_read_unlock();
  336. done:
  337. atomic_set(&core_state->nr_threads, nr);
  338. return nr;
  339. }
  340. static int coredump_wait(int exit_code, struct core_state *core_state)
  341. {
  342. struct task_struct *tsk = current;
  343. struct mm_struct *mm = tsk->mm;
  344. int core_waiters = -EBUSY;
  345. init_completion(&core_state->startup);
  346. core_state->dumper.task = tsk;
  347. core_state->dumper.next = NULL;
  348. down_write(&mm->mmap_sem);
  349. if (!mm->core_state)
  350. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  351. up_write(&mm->mmap_sem);
  352. if (core_waiters > 0) {
  353. struct core_thread *ptr;
  354. wait_for_completion(&core_state->startup);
  355. /*
  356. * Wait for all the threads to become inactive, so that
  357. * all the thread context (extended register state, like
  358. * fpu etc) gets copied to the memory.
  359. */
  360. ptr = core_state->dumper.next;
  361. while (ptr != NULL) {
  362. wait_task_inactive(ptr->task, 0);
  363. ptr = ptr->next;
  364. }
  365. }
  366. return core_waiters;
  367. }
  368. static void coredump_finish(struct mm_struct *mm, bool core_dumped)
  369. {
  370. struct core_thread *curr, *next;
  371. struct task_struct *task;
  372. spin_lock_irq(&current->sighand->siglock);
  373. if (core_dumped && !__fatal_signal_pending(current))
  374. current->signal->group_exit_code |= 0x80;
  375. current->signal->group_exit_task = NULL;
  376. current->signal->flags = SIGNAL_GROUP_EXIT;
  377. spin_unlock_irq(&current->sighand->siglock);
  378. next = mm->core_state->dumper.next;
  379. while ((curr = next) != NULL) {
  380. next = curr->next;
  381. task = curr->task;
  382. /*
  383. * see exit_mm(), curr->task must not see
  384. * ->task == NULL before we read ->next.
  385. */
  386. smp_mb();
  387. curr->task = NULL;
  388. wake_up_process(task);
  389. }
  390. mm->core_state = NULL;
  391. }
  392. static bool dump_interrupted(void)
  393. {
  394. /*
  395. * SIGKILL or freezing() interrupt the coredumping. Perhaps we
  396. * can do try_to_freeze() and check __fatal_signal_pending(),
  397. * but then we need to teach dump_write() to restart and clear
  398. * TIF_SIGPENDING.
  399. */
  400. return signal_pending(current);
  401. }
  402. static void wait_for_dump_helpers(struct file *file)
  403. {
  404. struct pipe_inode_info *pipe = file->private_data;
  405. pipe_lock(pipe);
  406. pipe->readers++;
  407. pipe->writers--;
  408. wake_up_interruptible_sync(&pipe->wait);
  409. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  410. pipe_unlock(pipe);
  411. /*
  412. * We actually want wait_event_freezable() but then we need
  413. * to clear TIF_SIGPENDING and improve dump_interrupted().
  414. */
  415. wait_event_interruptible(pipe->wait, pipe->readers == 1);
  416. pipe_lock(pipe);
  417. pipe->readers--;
  418. pipe->writers++;
  419. pipe_unlock(pipe);
  420. }
  421. /*
  422. * umh_pipe_setup
  423. * helper function to customize the process used
  424. * to collect the core in userspace. Specifically
  425. * it sets up a pipe and installs it as fd 0 (stdin)
  426. * for the process. Returns 0 on success, or
  427. * PTR_ERR on failure.
  428. * Note that it also sets the core limit to 1. This
  429. * is a special value that we use to trap recursive
  430. * core dumps
  431. */
  432. static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
  433. {
  434. struct file *files[2];
  435. struct coredump_params *cp = (struct coredump_params *)info->data;
  436. int err = create_pipe_files(files, 0);
  437. if (err)
  438. return err;
  439. cp->file = files[1];
  440. err = replace_fd(0, files[0], 0);
  441. fput(files[0]);
  442. /* and disallow core files too */
  443. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
  444. return err;
  445. }
  446. void do_coredump(const siginfo_t *siginfo)
  447. {
  448. struct core_state core_state;
  449. struct core_name cn;
  450. struct mm_struct *mm = current->mm;
  451. struct linux_binfmt * binfmt;
  452. const struct cred *old_cred;
  453. struct cred *cred;
  454. int retval = 0;
  455. int flag = 0;
  456. int ispipe;
  457. struct files_struct *displaced;
  458. bool need_nonrelative = false;
  459. bool core_dumped = false;
  460. static atomic_t core_dump_count = ATOMIC_INIT(0);
  461. struct coredump_params cprm = {
  462. .siginfo = siginfo,
  463. .regs = signal_pt_regs(),
  464. .limit = rlimit(RLIMIT_CORE),
  465. /*
  466. * We must use the same mm->flags while dumping core to avoid
  467. * inconsistency of bit flags, since this flag is not protected
  468. * by any locks.
  469. */
  470. .mm_flags = mm->flags,
  471. };
  472. audit_core_dumps(siginfo->si_signo);
  473. binfmt = mm->binfmt;
  474. if (!binfmt || !binfmt->core_dump)
  475. goto fail;
  476. if (!__get_dumpable(cprm.mm_flags))
  477. goto fail;
  478. cred = prepare_creds();
  479. if (!cred)
  480. goto fail;
  481. /*
  482. * We cannot trust fsuid as being the "true" uid of the process
  483. * nor do we know its entire history. We only know it was tainted
  484. * so we dump it as root in mode 2, and only into a controlled
  485. * environment (pipe handler or fully qualified path).
  486. */
  487. if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
  488. /* Setuid core dump mode */
  489. flag = O_EXCL; /* Stop rewrite attacks */
  490. cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
  491. need_nonrelative = true;
  492. }
  493. retval = coredump_wait(siginfo->si_signo, &core_state);
  494. if (retval < 0)
  495. goto fail_creds;
  496. old_cred = override_creds(cred);
  497. ispipe = format_corename(&cn, &cprm);
  498. if (ispipe) {
  499. int dump_count;
  500. char **helper_argv;
  501. struct subprocess_info *sub_info;
  502. if (ispipe < 0) {
  503. printk(KERN_WARNING "format_corename failed\n");
  504. printk(KERN_WARNING "Aborting core\n");
  505. goto fail_unlock;
  506. }
  507. if (cprm.limit == 1) {
  508. /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
  509. *
  510. * Normally core limits are irrelevant to pipes, since
  511. * we're not writing to the file system, but we use
  512. * cprm.limit of 1 here as a special value, this is a
  513. * consistent way to catch recursive crashes.
  514. * We can still crash if the core_pattern binary sets
  515. * RLIM_CORE = !1, but it runs as root, and can do
  516. * lots of stupid things.
  517. *
  518. * Note that we use task_tgid_vnr here to grab the pid
  519. * of the process group leader. That way we get the
  520. * right pid if a thread in a multi-threaded
  521. * core_pattern process dies.
  522. */
  523. printk(KERN_WARNING
  524. "Process %d(%s) has RLIMIT_CORE set to 1\n",
  525. task_tgid_vnr(current), current->comm);
  526. printk(KERN_WARNING "Aborting core\n");
  527. goto fail_unlock;
  528. }
  529. cprm.limit = RLIM_INFINITY;
  530. dump_count = atomic_inc_return(&core_dump_count);
  531. if (core_pipe_limit && (core_pipe_limit < dump_count)) {
  532. printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
  533. task_tgid_vnr(current), current->comm);
  534. printk(KERN_WARNING "Skipping core dump\n");
  535. goto fail_dropcount;
  536. }
  537. helper_argv = argv_split(GFP_KERNEL, cn.corename, NULL);
  538. if (!helper_argv) {
  539. printk(KERN_WARNING "%s failed to allocate memory\n",
  540. __func__);
  541. goto fail_dropcount;
  542. }
  543. retval = -ENOMEM;
  544. sub_info = call_usermodehelper_setup(helper_argv[0],
  545. helper_argv, NULL, GFP_KERNEL,
  546. umh_pipe_setup, NULL, &cprm);
  547. if (sub_info)
  548. retval = call_usermodehelper_exec(sub_info,
  549. UMH_WAIT_EXEC);
  550. argv_free(helper_argv);
  551. if (retval) {
  552. printk(KERN_INFO "Core dump to |%s pipe failed\n",
  553. cn.corename);
  554. goto close_fail;
  555. }
  556. } else {
  557. struct inode *inode;
  558. if (cprm.limit < binfmt->min_coredump)
  559. goto fail_unlock;
  560. if (need_nonrelative && cn.corename[0] != '/') {
  561. printk(KERN_WARNING "Pid %d(%s) can only dump core "\
  562. "to fully qualified path!\n",
  563. task_tgid_vnr(current), current->comm);
  564. printk(KERN_WARNING "Skipping core dump\n");
  565. goto fail_unlock;
  566. }
  567. cprm.file = filp_open(cn.corename,
  568. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  569. 0600);
  570. if (IS_ERR(cprm.file))
  571. goto fail_unlock;
  572. inode = file_inode(cprm.file);
  573. if (inode->i_nlink > 1)
  574. goto close_fail;
  575. if (d_unhashed(cprm.file->f_path.dentry))
  576. goto close_fail;
  577. /*
  578. * AK: actually i see no reason to not allow this for named
  579. * pipes etc, but keep the previous behaviour for now.
  580. */
  581. if (!S_ISREG(inode->i_mode))
  582. goto close_fail;
  583. /*
  584. * Dont allow local users get cute and trick others to coredump
  585. * into their pre-created files.
  586. */
  587. if (!uid_eq(inode->i_uid, current_fsuid()))
  588. goto close_fail;
  589. if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
  590. goto close_fail;
  591. if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
  592. goto close_fail;
  593. }
  594. /* get us an unshared descriptor table; almost always a no-op */
  595. retval = unshare_files(&displaced);
  596. if (retval)
  597. goto close_fail;
  598. if (displaced)
  599. put_files_struct(displaced);
  600. if (!dump_interrupted()) {
  601. file_start_write(cprm.file);
  602. core_dumped = binfmt->core_dump(&cprm);
  603. file_end_write(cprm.file);
  604. }
  605. if (ispipe && core_pipe_limit)
  606. wait_for_dump_helpers(cprm.file);
  607. close_fail:
  608. if (cprm.file)
  609. filp_close(cprm.file, NULL);
  610. fail_dropcount:
  611. if (ispipe)
  612. atomic_dec(&core_dump_count);
  613. fail_unlock:
  614. kfree(cn.corename);
  615. coredump_finish(mm, core_dumped);
  616. revert_creds(old_cred);
  617. fail_creds:
  618. put_cred(cred);
  619. fail:
  620. return;
  621. }
  622. /*
  623. * Core dumping helper functions. These are the only things you should
  624. * do on a core-file: use only these functions to write out all the
  625. * necessary info.
  626. */
  627. int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
  628. {
  629. struct file *file = cprm->file;
  630. loff_t pos = file->f_pos;
  631. ssize_t n;
  632. if (cprm->written + nr > cprm->limit)
  633. return 0;
  634. while (nr) {
  635. if (dump_interrupted())
  636. return 0;
  637. n = __kernel_write(file, addr, nr, &pos);
  638. if (n <= 0)
  639. return 0;
  640. file->f_pos = pos;
  641. cprm->written += n;
  642. nr -= n;
  643. }
  644. return 1;
  645. }
  646. EXPORT_SYMBOL(dump_emit);
  647. int dump_skip(struct coredump_params *cprm, size_t nr)
  648. {
  649. static char zeroes[PAGE_SIZE];
  650. struct file *file = cprm->file;
  651. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  652. if (cprm->written + nr > cprm->limit)
  653. return 0;
  654. if (dump_interrupted() ||
  655. file->f_op->llseek(file, nr, SEEK_CUR) < 0)
  656. return 0;
  657. cprm->written += nr;
  658. return 1;
  659. } else {
  660. while (nr > PAGE_SIZE) {
  661. if (!dump_emit(cprm, zeroes, PAGE_SIZE))
  662. return 0;
  663. nr -= PAGE_SIZE;
  664. }
  665. return dump_emit(cprm, zeroes, nr);
  666. }
  667. }
  668. EXPORT_SYMBOL(dump_skip);
  669. int dump_align(struct coredump_params *cprm, int align)
  670. {
  671. unsigned mod = cprm->written & (align - 1);
  672. if (align & (align - 1))
  673. return 0;
  674. return mod ? dump_skip(cprm, align - mod) : 1;
  675. }
  676. EXPORT_SYMBOL(dump_align);