coredump.c 18 KB

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