LCOV - code coverage report
Current view: top level - fs - coredump.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 459 0.7 %
Date: 2023-03-27 20:00:47 Functions: 1 29 3.4 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : #include <linux/slab.h>
       3             : #include <linux/file.h>
       4             : #include <linux/fdtable.h>
       5             : #include <linux/freezer.h>
       6             : #include <linux/mm.h>
       7             : #include <linux/stat.h>
       8             : #include <linux/fcntl.h>
       9             : #include <linux/swap.h>
      10             : #include <linux/ctype.h>
      11             : #include <linux/string.h>
      12             : #include <linux/init.h>
      13             : #include <linux/pagemap.h>
      14             : #include <linux/perf_event.h>
      15             : #include <linux/highmem.h>
      16             : #include <linux/spinlock.h>
      17             : #include <linux/key.h>
      18             : #include <linux/personality.h>
      19             : #include <linux/binfmts.h>
      20             : #include <linux/coredump.h>
      21             : #include <linux/sched/coredump.h>
      22             : #include <linux/sched/signal.h>
      23             : #include <linux/sched/task_stack.h>
      24             : #include <linux/utsname.h>
      25             : #include <linux/pid_namespace.h>
      26             : #include <linux/module.h>
      27             : #include <linux/namei.h>
      28             : #include <linux/mount.h>
      29             : #include <linux/security.h>
      30             : #include <linux/syscalls.h>
      31             : #include <linux/tsacct_kern.h>
      32             : #include <linux/cn_proc.h>
      33             : #include <linux/audit.h>
      34             : #include <linux/kmod.h>
      35             : #include <linux/fsnotify.h>
      36             : #include <linux/fs_struct.h>
      37             : #include <linux/pipe_fs_i.h>
      38             : #include <linux/oom.h>
      39             : #include <linux/compat.h>
      40             : #include <linux/fs.h>
      41             : #include <linux/path.h>
      42             : #include <linux/timekeeping.h>
      43             : #include <linux/sysctl.h>
      44             : #include <linux/elf.h>
      45             : 
      46             : #include <linux/uaccess.h>
      47             : #include <asm/mmu_context.h>
      48             : #include <asm/tlb.h>
      49             : #include <asm/exec.h>
      50             : 
      51             : #include <trace/events/task.h>
      52             : #include "internal.h"
      53             : 
      54             : #include <trace/events/sched.h>
      55             : 
      56             : static bool dump_vma_snapshot(struct coredump_params *cprm);
      57             : static void free_vma_snapshot(struct coredump_params *cprm);
      58             : 
      59             : static int core_uses_pid;
      60             : static unsigned int core_pipe_limit;
      61             : static char core_pattern[CORENAME_MAX_SIZE] = "core";
      62             : static int core_name_size = CORENAME_MAX_SIZE;
      63             : 
      64             : struct core_name {
      65             :         char *corename;
      66             :         int used, size;
      67             : };
      68             : 
      69           0 : static int expand_corename(struct core_name *cn, int size)
      70             : {
      71             :         char *corename;
      72             : 
      73           0 :         size = kmalloc_size_roundup(size);
      74           0 :         corename = krealloc(cn->corename, size, GFP_KERNEL);
      75             : 
      76           0 :         if (!corename)
      77             :                 return -ENOMEM;
      78             : 
      79           0 :         if (size > core_name_size) /* racy but harmless */
      80           0 :                 core_name_size = size;
      81             : 
      82           0 :         cn->size = size;
      83           0 :         cn->corename = corename;
      84           0 :         return 0;
      85             : }
      86             : 
      87           0 : static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
      88             :                                      va_list arg)
      89             : {
      90             :         int free, need;
      91             :         va_list arg_copy;
      92             : 
      93             : again:
      94           0 :         free = cn->size - cn->used;
      95             : 
      96           0 :         va_copy(arg_copy, arg);
      97           0 :         need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
      98           0 :         va_end(arg_copy);
      99             : 
     100           0 :         if (need < free) {
     101           0 :                 cn->used += need;
     102           0 :                 return 0;
     103             :         }
     104             : 
     105           0 :         if (!expand_corename(cn, cn->size + need - free + 1))
     106             :                 goto again;
     107             : 
     108             :         return -ENOMEM;
     109             : }
     110             : 
     111           0 : static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
     112             : {
     113             :         va_list arg;
     114             :         int ret;
     115             : 
     116           0 :         va_start(arg, fmt);
     117           0 :         ret = cn_vprintf(cn, fmt, arg);
     118           0 :         va_end(arg);
     119             : 
     120           0 :         return ret;
     121             : }
     122             : 
     123             : static __printf(2, 3)
     124           0 : int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
     125             : {
     126           0 :         int cur = cn->used;
     127             :         va_list arg;
     128             :         int ret;
     129             : 
     130           0 :         va_start(arg, fmt);
     131           0 :         ret = cn_vprintf(cn, fmt, arg);
     132           0 :         va_end(arg);
     133             : 
     134           0 :         if (ret == 0) {
     135             :                 /*
     136             :                  * Ensure that this coredump name component can't cause the
     137             :                  * resulting corefile path to consist of a ".." or ".".
     138             :                  */
     139           0 :                 if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
     140           0 :                                 (cn->used - cur == 2 && cn->corename[cur] == '.'
     141           0 :                                 && cn->corename[cur+1] == '.'))
     142           0 :                         cn->corename[cur] = '!';
     143             : 
     144             :                 /*
     145             :                  * Empty names are fishy and could be used to create a "//" in a
     146             :                  * corefile name, causing the coredump to happen one directory
     147             :                  * level too high. Enforce that all components of the core
     148             :                  * pattern are at least one character long.
     149             :                  */
     150           0 :                 if (cn->used == cur)
     151           0 :                         ret = cn_printf(cn, "!");
     152             :         }
     153             : 
     154           0 :         for (; cur < cn->used; ++cur) {
     155           0 :                 if (cn->corename[cur] == '/')
     156           0 :                         cn->corename[cur] = '!';
     157             :         }
     158           0 :         return ret;
     159             : }
     160             : 
     161           0 : static int cn_print_exe_file(struct core_name *cn, bool name_only)
     162             : {
     163             :         struct file *exe_file;
     164             :         char *pathbuf, *path, *ptr;
     165             :         int ret;
     166             : 
     167           0 :         exe_file = get_mm_exe_file(current->mm);
     168           0 :         if (!exe_file)
     169           0 :                 return cn_esc_printf(cn, "%s (path unknown)", current->comm);
     170             : 
     171           0 :         pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
     172           0 :         if (!pathbuf) {
     173             :                 ret = -ENOMEM;
     174             :                 goto put_exe_file;
     175             :         }
     176             : 
     177           0 :         path = file_path(exe_file, pathbuf, PATH_MAX);
     178           0 :         if (IS_ERR(path)) {
     179           0 :                 ret = PTR_ERR(path);
     180           0 :                 goto free_buf;
     181             :         }
     182             : 
     183           0 :         if (name_only) {
     184           0 :                 ptr = strrchr(path, '/');
     185           0 :                 if (ptr)
     186           0 :                         path = ptr + 1;
     187             :         }
     188           0 :         ret = cn_esc_printf(cn, "%s", path);
     189             : 
     190             : free_buf:
     191           0 :         kfree(pathbuf);
     192             : put_exe_file:
     193           0 :         fput(exe_file);
     194           0 :         return ret;
     195             : }
     196             : 
     197             : /* format_corename will inspect the pattern parameter, and output a
     198             :  * name into corename, which must have space for at least
     199             :  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
     200             :  */
     201           0 : static int format_corename(struct core_name *cn, struct coredump_params *cprm,
     202             :                            size_t **argv, int *argc)
     203             : {
     204           0 :         const struct cred *cred = current_cred();
     205           0 :         const char *pat_ptr = core_pattern;
     206           0 :         int ispipe = (*pat_ptr == '|');
     207           0 :         bool was_space = false;
     208           0 :         int pid_in_pattern = 0;
     209           0 :         int err = 0;
     210             : 
     211           0 :         cn->used = 0;
     212           0 :         cn->corename = NULL;
     213           0 :         if (expand_corename(cn, core_name_size))
     214             :                 return -ENOMEM;
     215           0 :         cn->corename[0] = '\0';
     216             : 
     217           0 :         if (ispipe) {
     218           0 :                 int argvs = sizeof(core_pattern) / 2;
     219           0 :                 (*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
     220           0 :                 if (!(*argv))
     221             :                         return -ENOMEM;
     222           0 :                 (*argv)[(*argc)++] = 0;
     223           0 :                 ++pat_ptr;
     224           0 :                 if (!(*pat_ptr))
     225             :                         return -ENOMEM;
     226             :         }
     227             : 
     228             :         /* Repeat as long as we have more pattern to process and more output
     229             :            space */
     230           0 :         while (*pat_ptr) {
     231             :                 /*
     232             :                  * Split on spaces before doing template expansion so that
     233             :                  * %e and %E don't get split if they have spaces in them
     234             :                  */
     235           0 :                 if (ispipe) {
     236           0 :                         if (isspace(*pat_ptr)) {
     237           0 :                                 if (cn->used != 0)
     238           0 :                                         was_space = true;
     239           0 :                                 pat_ptr++;
     240           0 :                                 continue;
     241           0 :                         } else if (was_space) {
     242           0 :                                 was_space = false;
     243           0 :                                 err = cn_printf(cn, "%c", '\0');
     244           0 :                                 if (err)
     245             :                                         return err;
     246           0 :                                 (*argv)[(*argc)++] = cn->used;
     247             :                         }
     248             :                 }
     249           0 :                 if (*pat_ptr != '%') {
     250           0 :                         err = cn_printf(cn, "%c", *pat_ptr++);
     251             :                 } else {
     252           0 :                         switch (*++pat_ptr) {
     253             :                         /* single % at the end, drop that */
     254             :                         case 0:
     255             :                                 goto out;
     256             :                         /* Double percent, output one percent */
     257             :                         case '%':
     258           0 :                                 err = cn_printf(cn, "%c", '%');
     259           0 :                                 break;
     260             :                         /* pid */
     261             :                         case 'p':
     262           0 :                                 pid_in_pattern = 1;
     263           0 :                                 err = cn_printf(cn, "%d",
     264           0 :                                               task_tgid_vnr(current));
     265           0 :                                 break;
     266             :                         /* global pid */
     267             :                         case 'P':
     268           0 :                                 err = cn_printf(cn, "%d",
     269           0 :                                               task_tgid_nr(current));
     270           0 :                                 break;
     271             :                         case 'i':
     272           0 :                                 err = cn_printf(cn, "%d",
     273           0 :                                               task_pid_vnr(current));
     274           0 :                                 break;
     275             :                         case 'I':
     276           0 :                                 err = cn_printf(cn, "%d",
     277           0 :                                               task_pid_nr(current));
     278           0 :                                 break;
     279             :                         /* uid */
     280             :                         case 'u':
     281           0 :                                 err = cn_printf(cn, "%u",
     282             :                                                 from_kuid(&init_user_ns,
     283             :                                                           cred->uid));
     284           0 :                                 break;
     285             :                         /* gid */
     286             :                         case 'g':
     287           0 :                                 err = cn_printf(cn, "%u",
     288             :                                                 from_kgid(&init_user_ns,
     289             :                                                           cred->gid));
     290           0 :                                 break;
     291             :                         case 'd':
     292           0 :                                 err = cn_printf(cn, "%d",
     293             :                                         __get_dumpable(cprm->mm_flags));
     294           0 :                                 break;
     295             :                         /* signal that caused the coredump */
     296             :                         case 's':
     297           0 :                                 err = cn_printf(cn, "%d",
     298           0 :                                                 cprm->siginfo->si_signo);
     299           0 :                                 break;
     300             :                         /* UNIX time of coredump */
     301             :                         case 't': {
     302             :                                 time64_t time;
     303             : 
     304           0 :                                 time = ktime_get_real_seconds();
     305           0 :                                 err = cn_printf(cn, "%lld", time);
     306           0 :                                 break;
     307             :                         }
     308             :                         /* hostname */
     309             :                         case 'h':
     310           0 :                                 down_read(&uts_sem);
     311           0 :                                 err = cn_esc_printf(cn, "%s",
     312           0 :                                               utsname()->nodename);
     313           0 :                                 up_read(&uts_sem);
     314           0 :                                 break;
     315             :                         /* executable, could be changed by prctl PR_SET_NAME etc */
     316             :                         case 'e':
     317           0 :                                 err = cn_esc_printf(cn, "%s", current->comm);
     318           0 :                                 break;
     319             :                         /* file name of executable */
     320             :                         case 'f':
     321           0 :                                 err = cn_print_exe_file(cn, true);
     322           0 :                                 break;
     323             :                         case 'E':
     324           0 :                                 err = cn_print_exe_file(cn, false);
     325           0 :                                 break;
     326             :                         /* core limit size */
     327             :                         case 'c':
     328           0 :                                 err = cn_printf(cn, "%lu",
     329             :                                               rlimit(RLIMIT_CORE));
     330           0 :                                 break;
     331             :                         /* CPU the task ran on */
     332             :                         case 'C':
     333           0 :                                 err = cn_printf(cn, "%d", cprm->cpu);
     334           0 :                                 break;
     335             :                         default:
     336             :                                 break;
     337             :                         }
     338           0 :                         ++pat_ptr;
     339             :                 }
     340             : 
     341           0 :                 if (err)
     342             :                         return err;
     343             :         }
     344             : 
     345             : out:
     346             :         /* Backward compatibility with core_uses_pid:
     347             :          *
     348             :          * If core_pattern does not include a %p (as is the default)
     349             :          * and core_uses_pid is set, then .%pid will be appended to
     350             :          * the filename. Do not do this for piped commands. */
     351           0 :         if (!ispipe && !pid_in_pattern && core_uses_pid) {
     352           0 :                 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
     353           0 :                 if (err)
     354             :                         return err;
     355             :         }
     356             :         return ispipe;
     357             : }
     358             : 
     359           0 : static int zap_process(struct task_struct *start, int exit_code)
     360             : {
     361             :         struct task_struct *t;
     362           0 :         int nr = 0;
     363             : 
     364             :         /* Allow SIGKILL, see prepare_signal() */
     365           0 :         start->signal->flags = SIGNAL_GROUP_EXIT;
     366           0 :         start->signal->group_exit_code = exit_code;
     367           0 :         start->signal->group_stop_count = 0;
     368             : 
     369           0 :         for_each_thread(start, t) {
     370           0 :                 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
     371           0 :                 if (t != current && !(t->flags & PF_POSTCOREDUMP)) {
     372           0 :                         sigaddset(&t->pending.signal, SIGKILL);
     373           0 :                         signal_wake_up(t, 1);
     374           0 :                         nr++;
     375             :                 }
     376             :         }
     377             : 
     378           0 :         return nr;
     379             : }
     380             : 
     381           0 : static int zap_threads(struct task_struct *tsk,
     382             :                         struct core_state *core_state, int exit_code)
     383             : {
     384           0 :         struct signal_struct *signal = tsk->signal;
     385           0 :         int nr = -EAGAIN;
     386             : 
     387           0 :         spin_lock_irq(&tsk->sighand->siglock);
     388           0 :         if (!(signal->flags & SIGNAL_GROUP_EXIT) && !signal->group_exec_task) {
     389           0 :                 signal->core_state = core_state;
     390           0 :                 nr = zap_process(tsk, exit_code);
     391           0 :                 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
     392           0 :                 tsk->flags |= PF_DUMPCORE;
     393           0 :                 atomic_set(&core_state->nr_threads, nr);
     394             :         }
     395           0 :         spin_unlock_irq(&tsk->sighand->siglock);
     396           0 :         return nr;
     397             : }
     398             : 
     399           0 : static int coredump_wait(int exit_code, struct core_state *core_state)
     400             : {
     401           0 :         struct task_struct *tsk = current;
     402           0 :         int core_waiters = -EBUSY;
     403             : 
     404           0 :         init_completion(&core_state->startup);
     405           0 :         core_state->dumper.task = tsk;
     406           0 :         core_state->dumper.next = NULL;
     407             : 
     408           0 :         core_waiters = zap_threads(tsk, core_state, exit_code);
     409           0 :         if (core_waiters > 0) {
     410             :                 struct core_thread *ptr;
     411             : 
     412           0 :                 wait_for_completion_state(&core_state->startup,
     413             :                                           TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
     414             :                 /*
     415             :                  * Wait for all the threads to become inactive, so that
     416             :                  * all the thread context (extended register state, like
     417             :                  * fpu etc) gets copied to the memory.
     418             :                  */
     419           0 :                 ptr = core_state->dumper.next;
     420           0 :                 while (ptr != NULL) {
     421           0 :                         wait_task_inactive(ptr->task, TASK_ANY);
     422           0 :                         ptr = ptr->next;
     423             :                 }
     424             :         }
     425             : 
     426           0 :         return core_waiters;
     427             : }
     428             : 
     429           0 : static void coredump_finish(bool core_dumped)
     430             : {
     431             :         struct core_thread *curr, *next;
     432             :         struct task_struct *task;
     433             : 
     434           0 :         spin_lock_irq(&current->sighand->siglock);
     435           0 :         if (core_dumped && !__fatal_signal_pending(current))
     436           0 :                 current->signal->group_exit_code |= 0x80;
     437           0 :         next = current->signal->core_state->dumper.next;
     438           0 :         current->signal->core_state = NULL;
     439           0 :         spin_unlock_irq(&current->sighand->siglock);
     440             : 
     441           0 :         while ((curr = next) != NULL) {
     442           0 :                 next = curr->next;
     443           0 :                 task = curr->task;
     444             :                 /*
     445             :                  * see coredump_task_exit(), curr->task must not see
     446             :                  * ->task == NULL before we read ->next.
     447             :                  */
     448           0 :                 smp_mb();
     449           0 :                 curr->task = NULL;
     450           0 :                 wake_up_process(task);
     451             :         }
     452           0 : }
     453             : 
     454           0 : static bool dump_interrupted(void)
     455             : {
     456             :         /*
     457             :          * SIGKILL or freezing() interrupt the coredumping. Perhaps we
     458             :          * can do try_to_freeze() and check __fatal_signal_pending(),
     459             :          * but then we need to teach dump_write() to restart and clear
     460             :          * TIF_SIGPENDING.
     461             :          */
     462           0 :         return fatal_signal_pending(current) || freezing(current);
     463             : }
     464             : 
     465           0 : static void wait_for_dump_helpers(struct file *file)
     466             : {
     467           0 :         struct pipe_inode_info *pipe = file->private_data;
     468             : 
     469           0 :         pipe_lock(pipe);
     470           0 :         pipe->readers++;
     471           0 :         pipe->writers--;
     472           0 :         wake_up_interruptible_sync(&pipe->rd_wait);
     473           0 :         kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
     474           0 :         pipe_unlock(pipe);
     475             : 
     476             :         /*
     477             :          * We actually want wait_event_freezable() but then we need
     478             :          * to clear TIF_SIGPENDING and improve dump_interrupted().
     479             :          */
     480           0 :         wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
     481             : 
     482           0 :         pipe_lock(pipe);
     483           0 :         pipe->readers--;
     484           0 :         pipe->writers++;
     485           0 :         pipe_unlock(pipe);
     486           0 : }
     487             : 
     488             : /*
     489             :  * umh_pipe_setup
     490             :  * helper function to customize the process used
     491             :  * to collect the core in userspace.  Specifically
     492             :  * it sets up a pipe and installs it as fd 0 (stdin)
     493             :  * for the process.  Returns 0 on success, or
     494             :  * PTR_ERR on failure.
     495             :  * Note that it also sets the core limit to 1.  This
     496             :  * is a special value that we use to trap recursive
     497             :  * core dumps
     498             :  */
     499           0 : static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
     500             : {
     501             :         struct file *files[2];
     502           0 :         struct coredump_params *cp = (struct coredump_params *)info->data;
     503           0 :         int err = create_pipe_files(files, 0);
     504           0 :         if (err)
     505             :                 return err;
     506             : 
     507           0 :         cp->file = files[1];
     508             : 
     509           0 :         err = replace_fd(0, files[0], 0);
     510           0 :         fput(files[0]);
     511             :         /* and disallow core files too */
     512           0 :         current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
     513             : 
     514           0 :         return err;
     515             : }
     516             : 
     517           0 : void do_coredump(const kernel_siginfo_t *siginfo)
     518             : {
     519             :         struct core_state core_state;
     520             :         struct core_name cn;
     521           0 :         struct mm_struct *mm = current->mm;
     522             :         struct linux_binfmt * binfmt;
     523             :         const struct cred *old_cred;
     524             :         struct cred *cred;
     525           0 :         int retval = 0;
     526             :         int ispipe;
     527           0 :         size_t *argv = NULL;
     528           0 :         int argc = 0;
     529             :         /* require nonrelative corefile path and be extra careful */
     530           0 :         bool need_suid_safe = false;
     531           0 :         bool core_dumped = false;
     532             :         static atomic_t core_dump_count = ATOMIC_INIT(0);
     533           0 :         struct coredump_params cprm = {
     534             :                 .siginfo = siginfo,
     535           0 :                 .limit = rlimit(RLIMIT_CORE),
     536             :                 /*
     537             :                  * We must use the same mm->flags while dumping core to avoid
     538             :                  * inconsistency of bit flags, since this flag is not protected
     539             :                  * by any locks.
     540             :                  */
     541           0 :                 .mm_flags = mm->flags,
     542             :                 .vma_meta = NULL,
     543             :                 .cpu = raw_smp_processor_id(),
     544             :         };
     545             : 
     546           0 :         audit_core_dumps(siginfo->si_signo);
     547             : 
     548           0 :         binfmt = mm->binfmt;
     549           0 :         if (!binfmt || !binfmt->core_dump)
     550             :                 goto fail;
     551           0 :         if (!__get_dumpable(cprm.mm_flags))
     552             :                 goto fail;
     553             : 
     554           0 :         cred = prepare_creds();
     555           0 :         if (!cred)
     556             :                 goto fail;
     557             :         /*
     558             :          * We cannot trust fsuid as being the "true" uid of the process
     559             :          * nor do we know its entire history. We only know it was tainted
     560             :          * so we dump it as root in mode 2, and only into a controlled
     561             :          * environment (pipe handler or fully qualified path).
     562             :          */
     563           0 :         if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
     564             :                 /* Setuid core dump mode */
     565           0 :                 cred->fsuid = GLOBAL_ROOT_UID;       /* Dump root private */
     566           0 :                 need_suid_safe = true;
     567             :         }
     568             : 
     569           0 :         retval = coredump_wait(siginfo->si_signo, &core_state);
     570           0 :         if (retval < 0)
     571             :                 goto fail_creds;
     572             : 
     573           0 :         old_cred = override_creds(cred);
     574             : 
     575           0 :         ispipe = format_corename(&cn, &cprm, &argv, &argc);
     576             : 
     577           0 :         if (ispipe) {
     578             :                 int argi;
     579             :                 int dump_count;
     580             :                 char **helper_argv;
     581             :                 struct subprocess_info *sub_info;
     582             : 
     583           0 :                 if (ispipe < 0) {
     584           0 :                         printk(KERN_WARNING "format_corename failed\n");
     585           0 :                         printk(KERN_WARNING "Aborting core\n");
     586           0 :                         goto fail_unlock;
     587             :                 }
     588             : 
     589           0 :                 if (cprm.limit == 1) {
     590             :                         /* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
     591             :                          *
     592             :                          * Normally core limits are irrelevant to pipes, since
     593             :                          * we're not writing to the file system, but we use
     594             :                          * cprm.limit of 1 here as a special value, this is a
     595             :                          * consistent way to catch recursive crashes.
     596             :                          * We can still crash if the core_pattern binary sets
     597             :                          * RLIM_CORE = !1, but it runs as root, and can do
     598             :                          * lots of stupid things.
     599             :                          *
     600             :                          * Note that we use task_tgid_vnr here to grab the pid
     601             :                          * of the process group leader.  That way we get the
     602             :                          * right pid if a thread in a multi-threaded
     603             :                          * core_pattern process dies.
     604             :                          */
     605           0 :                         printk(KERN_WARNING
     606             :                                 "Process %d(%s) has RLIMIT_CORE set to 1\n",
     607             :                                 task_tgid_vnr(current), current->comm);
     608           0 :                         printk(KERN_WARNING "Aborting core\n");
     609           0 :                         goto fail_unlock;
     610             :                 }
     611           0 :                 cprm.limit = RLIM_INFINITY;
     612             : 
     613           0 :                 dump_count = atomic_inc_return(&core_dump_count);
     614           0 :                 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
     615           0 :                         printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
     616             :                                task_tgid_vnr(current), current->comm);
     617           0 :                         printk(KERN_WARNING "Skipping core dump\n");
     618           0 :                         goto fail_dropcount;
     619             :                 }
     620             : 
     621           0 :                 helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
     622             :                                             GFP_KERNEL);
     623           0 :                 if (!helper_argv) {
     624           0 :                         printk(KERN_WARNING "%s failed to allocate memory\n",
     625             :                                __func__);
     626           0 :                         goto fail_dropcount;
     627             :                 }
     628           0 :                 for (argi = 0; argi < argc; argi++)
     629           0 :                         helper_argv[argi] = cn.corename + argv[argi];
     630           0 :                 helper_argv[argi] = NULL;
     631             : 
     632           0 :                 retval = -ENOMEM;
     633           0 :                 sub_info = call_usermodehelper_setup(helper_argv[0],
     634             :                                                 helper_argv, NULL, GFP_KERNEL,
     635             :                                                 umh_pipe_setup, NULL, &cprm);
     636           0 :                 if (sub_info)
     637           0 :                         retval = call_usermodehelper_exec(sub_info,
     638             :                                                           UMH_WAIT_EXEC);
     639             : 
     640           0 :                 kfree(helper_argv);
     641           0 :                 if (retval) {
     642           0 :                         printk(KERN_INFO "Core dump to |%s pipe failed\n",
     643             :                                cn.corename);
     644           0 :                         goto close_fail;
     645             :                 }
     646             :         } else {
     647             :                 struct mnt_idmap *idmap;
     648             :                 struct inode *inode;
     649           0 :                 int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
     650             :                                  O_LARGEFILE | O_EXCL;
     651             : 
     652           0 :                 if (cprm.limit < binfmt->min_coredump)
     653             :                         goto fail_unlock;
     654             : 
     655           0 :                 if (need_suid_safe && cn.corename[0] != '/') {
     656           0 :                         printk(KERN_WARNING "Pid %d(%s) can only dump core "\
     657             :                                 "to fully qualified path!\n",
     658             :                                 task_tgid_vnr(current), current->comm);
     659           0 :                         printk(KERN_WARNING "Skipping core dump\n");
     660           0 :                         goto fail_unlock;
     661             :                 }
     662             : 
     663             :                 /*
     664             :                  * Unlink the file if it exists unless this is a SUID
     665             :                  * binary - in that case, we're running around with root
     666             :                  * privs and don't want to unlink another user's coredump.
     667             :                  */
     668           0 :                 if (!need_suid_safe) {
     669             :                         /*
     670             :                          * If it doesn't exist, that's fine. If there's some
     671             :                          * other problem, we'll catch it at the filp_open().
     672             :                          */
     673           0 :                         do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
     674             :                 }
     675             : 
     676             :                 /*
     677             :                  * There is a race between unlinking and creating the
     678             :                  * file, but if that causes an EEXIST here, that's
     679             :                  * fine - another process raced with us while creating
     680             :                  * the corefile, and the other process won. To userspace,
     681             :                  * what matters is that at least one of the two processes
     682             :                  * writes its coredump successfully, not which one.
     683             :                  */
     684           0 :                 if (need_suid_safe) {
     685             :                         /*
     686             :                          * Using user namespaces, normal user tasks can change
     687             :                          * their current->fs->root to point to arbitrary
     688             :                          * directories. Since the intention of the "only dump
     689             :                          * with a fully qualified path" rule is to control where
     690             :                          * coredumps may be placed using root privileges,
     691             :                          * current->fs->root must not be used. Instead, use the
     692             :                          * root directory of init_task.
     693             :                          */
     694             :                         struct path root;
     695             : 
     696           0 :                         task_lock(&init_task);
     697           0 :                         get_fs_root(init_task.fs, &root);
     698           0 :                         task_unlock(&init_task);
     699           0 :                         cprm.file = file_open_root(&root, cn.corename,
     700             :                                                    open_flags, 0600);
     701           0 :                         path_put(&root);
     702             :                 } else {
     703           0 :                         cprm.file = filp_open(cn.corename, open_flags, 0600);
     704             :                 }
     705           0 :                 if (IS_ERR(cprm.file))
     706             :                         goto fail_unlock;
     707             : 
     708           0 :                 inode = file_inode(cprm.file);
     709           0 :                 if (inode->i_nlink > 1)
     710             :                         goto close_fail;
     711           0 :                 if (d_unhashed(cprm.file->f_path.dentry))
     712             :                         goto close_fail;
     713             :                 /*
     714             :                  * AK: actually i see no reason to not allow this for named
     715             :                  * pipes etc, but keep the previous behaviour for now.
     716             :                  */
     717           0 :                 if (!S_ISREG(inode->i_mode))
     718             :                         goto close_fail;
     719             :                 /*
     720             :                  * Don't dump core if the filesystem changed owner or mode
     721             :                  * of the file during file creation. This is an issue when
     722             :                  * a process dumps core while its cwd is e.g. on a vfat
     723             :                  * filesystem.
     724             :                  */
     725           0 :                 idmap = file_mnt_idmap(cprm.file);
     726           0 :                 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
     727           0 :                                     current_fsuid())) {
     728           0 :                         pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
     729             :                                             cn.corename);
     730             :                         goto close_fail;
     731             :                 }
     732           0 :                 if ((inode->i_mode & 0677) != 0600) {
     733           0 :                         pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
     734             :                                             cn.corename);
     735             :                         goto close_fail;
     736             :                 }
     737           0 :                 if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
     738             :                         goto close_fail;
     739           0 :                 if (do_truncate(idmap, cprm.file->f_path.dentry,
     740             :                                 0, 0, cprm.file))
     741             :                         goto close_fail;
     742             :         }
     743             : 
     744             :         /* get us an unshared descriptor table; almost always a no-op */
     745             :         /* The cell spufs coredump code reads the file descriptor tables */
     746           0 :         retval = unshare_files();
     747           0 :         if (retval)
     748             :                 goto close_fail;
     749           0 :         if (!dump_interrupted()) {
     750             :                 /*
     751             :                  * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
     752             :                  * have this set to NULL.
     753             :                  */
     754           0 :                 if (!cprm.file) {
     755           0 :                         pr_info("Core dump to |%s disabled\n", cn.corename);
     756           0 :                         goto close_fail;
     757             :                 }
     758           0 :                 if (!dump_vma_snapshot(&cprm))
     759             :                         goto close_fail;
     760             : 
     761           0 :                 file_start_write(cprm.file);
     762           0 :                 core_dumped = binfmt->core_dump(&cprm);
     763             :                 /*
     764             :                  * Ensures that file size is big enough to contain the current
     765             :                  * file postion. This prevents gdb from complaining about
     766             :                  * a truncated file if the last "write" to the file was
     767             :                  * dump_skip.
     768             :                  */
     769           0 :                 if (cprm.to_skip) {
     770           0 :                         cprm.to_skip--;
     771           0 :                         dump_emit(&cprm, "", 1);
     772             :                 }
     773           0 :                 file_end_write(cprm.file);
     774           0 :                 free_vma_snapshot(&cprm);
     775             :         }
     776           0 :         if (ispipe && core_pipe_limit)
     777           0 :                 wait_for_dump_helpers(cprm.file);
     778             : close_fail:
     779           0 :         if (cprm.file)
     780           0 :                 filp_close(cprm.file, NULL);
     781             : fail_dropcount:
     782           0 :         if (ispipe)
     783             :                 atomic_dec(&core_dump_count);
     784             : fail_unlock:
     785           0 :         kfree(argv);
     786           0 :         kfree(cn.corename);
     787           0 :         coredump_finish(core_dumped);
     788           0 :         revert_creds(old_cred);
     789             : fail_creds:
     790             :         put_cred(cred);
     791             : fail:
     792           0 :         return;
     793             : }
     794             : 
     795             : /*
     796             :  * Core dumping helper functions.  These are the only things you should
     797             :  * do on a core-file: use only these functions to write out all the
     798             :  * necessary info.
     799             :  */
     800           0 : static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
     801             : {
     802           0 :         struct file *file = cprm->file;
     803           0 :         loff_t pos = file->f_pos;
     804             :         ssize_t n;
     805           0 :         if (cprm->written + nr > cprm->limit)
     806             :                 return 0;
     807             : 
     808             : 
     809           0 :         if (dump_interrupted())
     810             :                 return 0;
     811           0 :         n = __kernel_write(file, addr, nr, &pos);
     812           0 :         if (n != nr)
     813             :                 return 0;
     814           0 :         file->f_pos = pos;
     815           0 :         cprm->written += n;
     816           0 :         cprm->pos += n;
     817             : 
     818           0 :         return 1;
     819             : }
     820             : 
     821           0 : static int __dump_skip(struct coredump_params *cprm, size_t nr)
     822             : {
     823             :         static char zeroes[PAGE_SIZE];
     824           0 :         struct file *file = cprm->file;
     825           0 :         if (file->f_mode & FMODE_LSEEK) {
     826           0 :                 if (dump_interrupted() ||
     827           0 :                     vfs_llseek(file, nr, SEEK_CUR) < 0)
     828             :                         return 0;
     829           0 :                 cprm->pos += nr;
     830           0 :                 return 1;
     831             :         } else {
     832           0 :                 while (nr > PAGE_SIZE) {
     833           0 :                         if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
     834             :                                 return 0;
     835           0 :                         nr -= PAGE_SIZE;
     836             :                 }
     837           0 :                 return __dump_emit(cprm, zeroes, nr);
     838             :         }
     839             : }
     840             : 
     841           0 : int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
     842             : {
     843           0 :         if (cprm->to_skip) {
     844           0 :                 if (!__dump_skip(cprm, cprm->to_skip))
     845             :                         return 0;
     846           0 :                 cprm->to_skip = 0;
     847             :         }
     848           0 :         return __dump_emit(cprm, addr, nr);
     849             : }
     850             : EXPORT_SYMBOL(dump_emit);
     851             : 
     852           0 : void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
     853             : {
     854           0 :         cprm->to_skip = pos - cprm->pos;
     855           0 : }
     856             : EXPORT_SYMBOL(dump_skip_to);
     857             : 
     858           0 : void dump_skip(struct coredump_params *cprm, size_t nr)
     859             : {
     860           0 :         cprm->to_skip += nr;
     861           0 : }
     862             : EXPORT_SYMBOL(dump_skip);
     863             : 
     864             : #ifdef CONFIG_ELF_CORE
     865           0 : static int dump_emit_page(struct coredump_params *cprm, struct page *page)
     866             : {
     867             :         struct bio_vec bvec;
     868             :         struct iov_iter iter;
     869           0 :         struct file *file = cprm->file;
     870             :         loff_t pos;
     871             :         ssize_t n;
     872             : 
     873           0 :         if (cprm->to_skip) {
     874           0 :                 if (!__dump_skip(cprm, cprm->to_skip))
     875             :                         return 0;
     876           0 :                 cprm->to_skip = 0;
     877             :         }
     878           0 :         if (cprm->written + PAGE_SIZE > cprm->limit)
     879             :                 return 0;
     880           0 :         if (dump_interrupted())
     881             :                 return 0;
     882           0 :         pos = file->f_pos;
     883           0 :         bvec_set_page(&bvec, page, PAGE_SIZE, 0);
     884           0 :         iov_iter_bvec(&iter, ITER_SOURCE, &bvec, 1, PAGE_SIZE);
     885           0 :         n = __kernel_write_iter(cprm->file, &iter, &pos);
     886           0 :         if (n != PAGE_SIZE)
     887             :                 return 0;
     888           0 :         file->f_pos = pos;
     889           0 :         cprm->written += PAGE_SIZE;
     890           0 :         cprm->pos += PAGE_SIZE;
     891             : 
     892           0 :         return 1;
     893             : }
     894             : 
     895           0 : int dump_user_range(struct coredump_params *cprm, unsigned long start,
     896             :                     unsigned long len)
     897             : {
     898             :         unsigned long addr;
     899             : 
     900           0 :         for (addr = start; addr < start + len; addr += PAGE_SIZE) {
     901             :                 struct page *page;
     902             : 
     903             :                 /*
     904             :                  * To avoid having to allocate page tables for virtual address
     905             :                  * ranges that have never been used yet, and also to make it
     906             :                  * easy to generate sparse core files, use a helper that returns
     907             :                  * NULL when encountering an empty page table entry that would
     908             :                  * otherwise have been filled with the zero page.
     909             :                  */
     910           0 :                 page = get_dump_page(addr);
     911           0 :                 if (page) {
     912           0 :                         int stop = !dump_emit_page(cprm, page);
     913           0 :                         put_page(page);
     914           0 :                         if (stop)
     915             :                                 return 0;
     916             :                 } else {
     917             :                         dump_skip(cprm, PAGE_SIZE);
     918             :                 }
     919             :         }
     920             :         return 1;
     921             : }
     922             : #endif
     923             : 
     924           0 : int dump_align(struct coredump_params *cprm, int align)
     925             : {
     926           0 :         unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
     927           0 :         if (align & (align - 1))
     928             :                 return 0;
     929           0 :         if (mod)
     930           0 :                 cprm->to_skip += align - mod;
     931             :         return 1;
     932             : }
     933             : EXPORT_SYMBOL(dump_align);
     934             : 
     935             : #ifdef CONFIG_SYSCTL
     936             : 
     937           0 : void validate_coredump_safety(void)
     938             : {
     939           0 :         if (suid_dumpable == SUID_DUMP_ROOT &&
     940           0 :             core_pattern[0] != '/' && core_pattern[0] != '|') {
     941           0 :                 pr_warn(
     942             : "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
     943             : "Pipe handler or fully qualified core dump path required.\n"
     944             : "Set kernel.core_pattern before fs.suid_dumpable.\n"
     945             :                 );
     946             :         }
     947           0 : }
     948             : 
     949           0 : static int proc_dostring_coredump(struct ctl_table *table, int write,
     950             :                   void *buffer, size_t *lenp, loff_t *ppos)
     951             : {
     952           0 :         int error = proc_dostring(table, write, buffer, lenp, ppos);
     953             : 
     954           0 :         if (!error)
     955           0 :                 validate_coredump_safety();
     956           0 :         return error;
     957             : }
     958             : 
     959             : static struct ctl_table coredump_sysctls[] = {
     960             :         {
     961             :                 .procname       = "core_uses_pid",
     962             :                 .data           = &core_uses_pid,
     963             :                 .maxlen         = sizeof(int),
     964             :                 .mode           = 0644,
     965             :                 .proc_handler   = proc_dointvec,
     966             :         },
     967             :         {
     968             :                 .procname       = "core_pattern",
     969             :                 .data           = core_pattern,
     970             :                 .maxlen         = CORENAME_MAX_SIZE,
     971             :                 .mode           = 0644,
     972             :                 .proc_handler   = proc_dostring_coredump,
     973             :         },
     974             :         {
     975             :                 .procname       = "core_pipe_limit",
     976             :                 .data           = &core_pipe_limit,
     977             :                 .maxlen         = sizeof(unsigned int),
     978             :                 .mode           = 0644,
     979             :                 .proc_handler   = proc_dointvec,
     980             :         },
     981             :         { }
     982             : };
     983             : 
     984           1 : static int __init init_fs_coredump_sysctls(void)
     985             : {
     986           1 :         register_sysctl_init("kernel", coredump_sysctls);
     987           1 :         return 0;
     988             : }
     989             : fs_initcall(init_fs_coredump_sysctls);
     990             : #endif /* CONFIG_SYSCTL */
     991             : 
     992             : /*
     993             :  * The purpose of always_dump_vma() is to make sure that special kernel mappings
     994             :  * that are useful for post-mortem analysis are included in every core dump.
     995             :  * In that way we ensure that the core dump is fully interpretable later
     996             :  * without matching up the same kernel and hardware config to see what PC values
     997             :  * meant. These special mappings include - vDSO, vsyscall, and other
     998             :  * architecture specific mappings
     999             :  */
    1000           0 : static bool always_dump_vma(struct vm_area_struct *vma)
    1001             : {
    1002             :         /* Any vsyscall mappings? */
    1003           0 :         if (vma == get_gate_vma(vma->vm_mm))
    1004             :                 return true;
    1005             : 
    1006             :         /*
    1007             :          * Assume that all vmas with a .name op should always be dumped.
    1008             :          * If this changes, a new vm_ops field can easily be added.
    1009             :          */
    1010           0 :         if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
    1011             :                 return true;
    1012             : 
    1013             :         /*
    1014             :          * arch_vma_name() returns non-NULL for special architecture mappings,
    1015             :          * such as vDSO sections.
    1016             :          */
    1017           0 :         if (arch_vma_name(vma))
    1018             :                 return true;
    1019             : 
    1020           0 :         return false;
    1021             : }
    1022             : 
    1023             : #define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
    1024             : 
    1025             : /*
    1026             :  * Decide how much of @vma's contents should be included in a core dump.
    1027             :  */
    1028           0 : static unsigned long vma_dump_size(struct vm_area_struct *vma,
    1029             :                                    unsigned long mm_flags)
    1030             : {
    1031             : #define FILTER(type)    (mm_flags & (1UL << MMF_DUMP_##type))
    1032             : 
    1033             :         /* always dump the vdso and vsyscall sections */
    1034           0 :         if (always_dump_vma(vma))
    1035             :                 goto whole;
    1036             : 
    1037           0 :         if (vma->vm_flags & VM_DONTDUMP)
    1038             :                 return 0;
    1039             : 
    1040             :         /* support for DAX */
    1041           0 :         if (vma_is_dax(vma)) {
    1042             :                 if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
    1043             :                         goto whole;
    1044             :                 if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
    1045             :                         goto whole;
    1046             :                 return 0;
    1047             :         }
    1048             : 
    1049             :         /* Hugetlb memory check */
    1050           0 :         if (is_vm_hugetlb_page(vma)) {
    1051             :                 if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
    1052             :                         goto whole;
    1053             :                 if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
    1054             :                         goto whole;
    1055             :                 return 0;
    1056             :         }
    1057             : 
    1058             :         /* Do not dump I/O mapped devices or special mappings */
    1059           0 :         if (vma->vm_flags & VM_IO)
    1060             :                 return 0;
    1061             : 
    1062             :         /* By default, dump shared memory if mapped from an anonymous file. */
    1063           0 :         if (vma->vm_flags & VM_SHARED) {
    1064           0 :                 if (file_inode(vma->vm_file)->i_nlink == 0 ?
    1065           0 :                     FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
    1066             :                         goto whole;
    1067             :                 return 0;
    1068             :         }
    1069             : 
    1070             :         /* Dump segments that have been written to.  */
    1071           0 :         if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
    1072             :                 goto whole;
    1073           0 :         if (vma->vm_file == NULL)
    1074             :                 return 0;
    1075             : 
    1076           0 :         if (FILTER(MAPPED_PRIVATE))
    1077             :                 goto whole;
    1078             : 
    1079             :         /*
    1080             :          * If this is the beginning of an executable file mapping,
    1081             :          * dump the first page to aid in determining what was mapped here.
    1082             :          */
    1083           0 :         if (FILTER(ELF_HEADERS) &&
    1084           0 :             vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
    1085           0 :                 if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
    1086             :                         return PAGE_SIZE;
    1087             : 
    1088             :                 /*
    1089             :                  * ELF libraries aren't always executable.
    1090             :                  * We'll want to check whether the mapping starts with the ELF
    1091             :                  * magic, but not now - we're holding the mmap lock,
    1092             :                  * so copy_from_user() doesn't work here.
    1093             :                  * Use a placeholder instead, and fix it up later in
    1094             :                  * dump_vma_snapshot().
    1095             :                  */
    1096           0 :                 return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
    1097             :         }
    1098             : 
    1099             : #undef  FILTER
    1100             : 
    1101             :         return 0;
    1102             : 
    1103             : whole:
    1104           0 :         return vma->vm_end - vma->vm_start;
    1105             : }
    1106             : 
    1107             : /*
    1108             :  * Helper function for iterating across a vma list.  It ensures that the caller
    1109             :  * will visit `gate_vma' prior to terminating the search.
    1110             :  */
    1111             : static struct vm_area_struct *coredump_next_vma(struct vma_iterator *vmi,
    1112             :                                        struct vm_area_struct *vma,
    1113             :                                        struct vm_area_struct *gate_vma)
    1114             : {
    1115             :         if (gate_vma && (vma == gate_vma))
    1116             :                 return NULL;
    1117             : 
    1118           0 :         vma = vma_next(vmi);
    1119           0 :         if (vma)
    1120             :                 return vma;
    1121             :         return gate_vma;
    1122             : }
    1123             : 
    1124           0 : static void free_vma_snapshot(struct coredump_params *cprm)
    1125             : {
    1126           0 :         if (cprm->vma_meta) {
    1127             :                 int i;
    1128           0 :                 for (i = 0; i < cprm->vma_count; i++) {
    1129           0 :                         struct file *file = cprm->vma_meta[i].file;
    1130           0 :                         if (file)
    1131           0 :                                 fput(file);
    1132             :                 }
    1133           0 :                 kvfree(cprm->vma_meta);
    1134           0 :                 cprm->vma_meta = NULL;
    1135             :         }
    1136           0 : }
    1137             : 
    1138             : /*
    1139             :  * Under the mmap_lock, take a snapshot of relevant information about the task's
    1140             :  * VMAs.
    1141             :  */
    1142           0 : static bool dump_vma_snapshot(struct coredump_params *cprm)
    1143             : {
    1144           0 :         struct vm_area_struct *gate_vma, *vma = NULL;
    1145           0 :         struct mm_struct *mm = current->mm;
    1146           0 :         VMA_ITERATOR(vmi, mm, 0);
    1147           0 :         int i = 0;
    1148             : 
    1149             :         /*
    1150             :          * Once the stack expansion code is fixed to not change VMA bounds
    1151             :          * under mmap_lock in read mode, this can be changed to take the
    1152             :          * mmap_lock in read mode.
    1153             :          */
    1154           0 :         if (mmap_write_lock_killable(mm))
    1155             :                 return false;
    1156             : 
    1157           0 :         cprm->vma_data_size = 0;
    1158           0 :         gate_vma = get_gate_vma(mm);
    1159           0 :         cprm->vma_count = mm->map_count + (gate_vma ? 1 : 0);
    1160             : 
    1161           0 :         cprm->vma_meta = kvmalloc_array(cprm->vma_count, sizeof(*cprm->vma_meta), GFP_KERNEL);
    1162           0 :         if (!cprm->vma_meta) {
    1163           0 :                 mmap_write_unlock(mm);
    1164           0 :                 return false;
    1165             :         }
    1166             : 
    1167           0 :         while ((vma = coredump_next_vma(&vmi, vma, gate_vma)) != NULL) {
    1168           0 :                 struct core_vma_metadata *m = cprm->vma_meta + i;
    1169             : 
    1170           0 :                 m->start = vma->vm_start;
    1171           0 :                 m->end = vma->vm_end;
    1172           0 :                 m->flags = vma->vm_flags;
    1173           0 :                 m->dump_size = vma_dump_size(vma, cprm->mm_flags);
    1174           0 :                 m->pgoff = vma->vm_pgoff;
    1175           0 :                 m->file = vma->vm_file;
    1176           0 :                 if (m->file)
    1177           0 :                         get_file(m->file);
    1178           0 :                 i++;
    1179             :         }
    1180             : 
    1181           0 :         mmap_write_unlock(mm);
    1182             : 
    1183           0 :         for (i = 0; i < cprm->vma_count; i++) {
    1184           0 :                 struct core_vma_metadata *m = cprm->vma_meta + i;
    1185             : 
    1186           0 :                 if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
    1187             :                         char elfmag[SELFMAG];
    1188             : 
    1189           0 :                         if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
    1190           0 :                                         memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
    1191           0 :                                 m->dump_size = 0;
    1192             :                         } else {
    1193           0 :                                 m->dump_size = PAGE_SIZE;
    1194             :                         }
    1195             :                 }
    1196             : 
    1197           0 :                 cprm->vma_data_size += m->dump_size;
    1198             :         }
    1199             : 
    1200             :         return true;
    1201             : }

Generated by: LCOV version 1.14