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