coredump.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 "coredump.h"
  42. #include <trace/events/sched.h>
  43. int core_uses_pid;
  44. char core_pattern[CORENAME_MAX_SIZE] = "core";
  45. unsigned int core_pipe_limit;
  46. struct core_name {
  47. char *corename;
  48. int used, size;
  49. };
  50. static atomic_t call_count = ATOMIC_INIT(1);
  51. /* The maximal length of core_pattern is also specified in sysctl.c */
  52. static int expand_corename(struct core_name *cn)
  53. {
  54. int size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
  55. char *corename = krealloc(cn->corename, size, GFP_KERNEL);
  56. if (!corename)
  57. return -ENOMEM;
  58. cn->size = size;
  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. char *cur;
  65. int need;
  66. int ret;
  67. need = vsnprintf(NULL, 0, fmt, arg);
  68. if (likely(need < cn->size - cn->used - 1))
  69. goto out_printf;
  70. ret = expand_corename(cn);
  71. if (ret)
  72. goto expand_fail;
  73. out_printf:
  74. cur = cn->corename + cn->used;
  75. vsnprintf(cur, need + 1, fmt, arg);
  76. cn->used += need;
  77. return 0;
  78. expand_fail:
  79. return ret;
  80. }
  81. static int cn_printf(struct core_name *cn, const char *fmt, ...)
  82. {
  83. va_list arg;
  84. int ret;
  85. va_start(arg, fmt);
  86. ret = cn_vprintf(cn, fmt, arg);
  87. va_end(arg);
  88. return ret;
  89. }
  90. static void cn_escape(char *str)
  91. {
  92. for (; *str; str++)
  93. if (*str == '/')
  94. *str = '!';
  95. }
  96. static int cn_print_exe_file(struct core_name *cn)
  97. {
  98. struct file *exe_file;
  99. char *pathbuf, *path;
  100. int ret;
  101. exe_file = get_mm_exe_file(current->mm);
  102. if (!exe_file) {
  103. char *commstart = cn->corename + cn->used;
  104. ret = cn_printf(cn, "%s (path unknown)", current->comm);
  105. cn_escape(commstart);
  106. return ret;
  107. }
  108. pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
  109. if (!pathbuf) {
  110. ret = -ENOMEM;
  111. goto put_exe_file;
  112. }
  113. path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
  114. if (IS_ERR(path)) {
  115. ret = PTR_ERR(path);
  116. goto free_buf;
  117. }
  118. cn_escape(path);
  119. ret = cn_printf(cn, "%s", path);
  120. free_buf:
  121. kfree(pathbuf);
  122. put_exe_file:
  123. fput(exe_file);
  124. return ret;
  125. }
  126. /* format_corename will inspect the pattern parameter, and output a
  127. * name into corename, which must have space for at least
  128. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  129. */
  130. static int format_corename(struct core_name *cn, struct coredump_params *cprm)
  131. {
  132. const struct cred *cred = current_cred();
  133. const char *pat_ptr = core_pattern;
  134. int ispipe = (*pat_ptr == '|');
  135. int pid_in_pattern = 0;
  136. int err = 0;
  137. cn->used = 0;
  138. cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
  139. cn->corename = kmalloc(cn->size, GFP_KERNEL);
  140. if (!cn->corename)
  141. return -ENOMEM;
  142. /* Repeat as long as we have more pattern to process and more output
  143. space */
  144. while (*pat_ptr) {
  145. if (*pat_ptr != '%') {
  146. if (*pat_ptr == 0)
  147. goto out;
  148. err = cn_printf(cn, "%c", *pat_ptr++);
  149. } else {
  150. switch (*++pat_ptr) {
  151. /* single % at the end, drop that */
  152. case 0:
  153. goto out;
  154. /* Double percent, output one percent */
  155. case '%':
  156. err = cn_printf(cn, "%c", '%');
  157. break;
  158. /* pid */
  159. case 'p':
  160. pid_in_pattern = 1;
  161. err = cn_printf(cn, "%d",
  162. task_tgid_vnr(current));
  163. break;
  164. /* uid */
  165. case 'u':
  166. err = cn_printf(cn, "%d", cred->uid);
  167. break;
  168. /* gid */
  169. case 'g':
  170. err = cn_printf(cn, "%d", cred->gid);
  171. break;
  172. case 'd':
  173. err = cn_printf(cn, "%d",
  174. __get_dumpable(cprm->mm_flags));
  175. break;
  176. /* signal that caused the coredump */
  177. case 's':
  178. err = cn_printf(cn, "%ld", cprm->siginfo->si_signo);
  179. break;
  180. /* UNIX time of coredump */
  181. case 't': {
  182. struct timeval tv;
  183. do_gettimeofday(&tv);
  184. err = cn_printf(cn, "%lu", tv.tv_sec);
  185. break;
  186. }
  187. /* hostname */
  188. case 'h': {
  189. char *namestart = cn->corename + cn->used;
  190. down_read(&uts_sem);
  191. err = cn_printf(cn, "%s",
  192. utsname()->nodename);
  193. up_read(&uts_sem);
  194. cn_escape(namestart);
  195. break;
  196. }
  197. /* executable */
  198. case 'e': {
  199. char *commstart = cn->corename + cn->used;
  200. err = cn_printf(cn, "%s", current->comm);
  201. cn_escape(commstart);
  202. break;
  203. }
  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. /* Backward compatibility with core_uses_pid:
  221. *
  222. * If core_pattern does not include a %p (as is the default)
  223. * and core_uses_pid is set, then .%pid will be appended to
  224. * the filename. Do not do this for piped commands. */
  225. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  226. err = cn_printf(cn, ".%d", task_tgid_vnr(current));
  227. if (err)
  228. return err;
  229. }
  230. out:
  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(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+1, 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 || !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_write(struct file *file, const void *addr, int nr)
  613. {
  614. return !dump_interrupted() &&
  615. access_ok(VERIFY_READ, addr, nr) &&
  616. file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  617. }
  618. EXPORT_SYMBOL(dump_write);
  619. int dump_seek(struct file *file, loff_t off)
  620. {
  621. int ret = 1;
  622. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  623. if (dump_interrupted() ||
  624. file->f_op->llseek(file, off, SEEK_CUR) < 0)
  625. return 0;
  626. } else {
  627. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  628. if (!buf)
  629. return 0;
  630. while (off > 0) {
  631. unsigned long n = off;
  632. if (n > PAGE_SIZE)
  633. n = PAGE_SIZE;
  634. if (!dump_write(file, buf, n)) {
  635. ret = 0;
  636. break;
  637. }
  638. off -= n;
  639. }
  640. free_page((unsigned long)buf);
  641. }
  642. return ret;
  643. }
  644. EXPORT_SYMBOL(dump_seek);