coredump.c 17 KB

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