coredump.c 18 KB

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