Skip to content

Commit 558564d

Browse files
ebiedermgregkh
authored andcommitted
coredump: Use the vma snapshot in fill_files_note
commit 390031c upstream. Matthew Wilcox reported that there is a missing mmap_lock in file_files_note that could possibly lead to a user after free. Solve this by using the existing vma snapshot for consistency and to avoid the need to take the mmap_lock anywhere in the coredump code except for dump_vma_snapshot. Update the dump_vma_snapshot to capture vm_pgoff and vm_file that are neeeded by fill_files_note. Add free_vma_snapshot to free the captured values of vm_file. Reported-by: Matthew Wilcox <willy@infradead.org> Link: https://lkml.kernel.org/r/20220131153740.2396974-1-willy@infradead.org Cc: stable@vger.kernel.org Fixes: a07279c ("binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot") Fixes: 2aa362c ("coredump: extend core dump note section to contain file names of mapped files") Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b7933f1 commit 558564d

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

fs/binfmt_elf.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,17 +1613,16 @@ static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata,
16131613
* long file_ofs
16141614
* followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
16151615
*/
1616-
static int fill_files_note(struct memelfnote *note)
1616+
static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm)
16171617
{
1618-
struct mm_struct *mm = current->mm;
1619-
struct vm_area_struct *vma;
16201618
unsigned count, size, names_ofs, remaining, n;
16211619
user_long_t *data;
16221620
user_long_t *start_end_ofs;
16231621
char *name_base, *name_curpos;
1622+
int i;
16241623

16251624
/* *Estimated* file count and total data size needed */
1626-
count = mm->map_count;
1625+
count = cprm->vma_count;
16271626
if (count > UINT_MAX / 64)
16281627
return -EINVAL;
16291628
size = count * 64;
@@ -1645,11 +1644,12 @@ static int fill_files_note(struct memelfnote *note)
16451644
name_base = name_curpos = ((char *)data) + names_ofs;
16461645
remaining = size - names_ofs;
16471646
count = 0;
1648-
for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1647+
for (i = 0; i < cprm->vma_count; i++) {
1648+
struct core_vma_metadata *m = &cprm->vma_meta[i];
16491649
struct file *file;
16501650
const char *filename;
16511651

1652-
file = vma->vm_file;
1652+
file = m->file;
16531653
if (!file)
16541654
continue;
16551655
filename = file_path(file, name_curpos, remaining);
@@ -1669,9 +1669,9 @@ static int fill_files_note(struct memelfnote *note)
16691669
memmove(name_curpos, filename, n);
16701670
name_curpos += n;
16711671

1672-
*start_end_ofs++ = vma->vm_start;
1673-
*start_end_ofs++ = vma->vm_end;
1674-
*start_end_ofs++ = vma->vm_pgoff;
1672+
*start_end_ofs++ = m->start;
1673+
*start_end_ofs++ = m->end;
1674+
*start_end_ofs++ = m->pgoff;
16751675
count++;
16761676
}
16771677

@@ -1682,7 +1682,7 @@ static int fill_files_note(struct memelfnote *note)
16821682
* Count usually is less than mm->map_count,
16831683
* we need to move filenames down.
16841684
*/
1685-
n = mm->map_count - count;
1685+
n = cprm->vma_count - count;
16861686
if (n != 0) {
16871687
unsigned shift_bytes = n * 3 * sizeof(data[0]);
16881688
memmove(name_base - shift_bytes, name_base,
@@ -1884,7 +1884,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
18841884
fill_auxv_note(&info->auxv, current->mm);
18851885
info->size += notesize(&info->auxv);
18861886

1887-
if (fill_files_note(&info->files) == 0)
1887+
if (fill_files_note(&info->files, cprm) == 0)
18881888
info->size += notesize(&info->files);
18891889

18901890
return 1;
@@ -2073,7 +2073,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
20732073
fill_auxv_note(info->notes + 3, current->mm);
20742074
info->numnote = 4;
20752075

2076-
if (fill_files_note(info->notes + info->numnote) == 0) {
2076+
if (fill_files_note(info->notes + info->numnote, cprm) == 0) {
20772077
info->notes_files = info->notes + info->numnote;
20782078
info->numnote++;
20792079
}

fs/coredump.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <trace/events/sched.h>
5555

5656
static bool dump_vma_snapshot(struct coredump_params *cprm);
57+
static void free_vma_snapshot(struct coredump_params *cprm);
5758

5859
int core_uses_pid;
5960
unsigned int core_pipe_limit;
@@ -816,7 +817,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
816817
file_start_write(cprm.file);
817818
core_dumped = binfmt->core_dump(&cprm);
818819
file_end_write(cprm.file);
819-
kvfree(cprm.vma_meta);
820+
free_vma_snapshot(&cprm);
820821
}
821822
if (ispipe && core_pipe_limit)
822823
wait_for_dump_helpers(cprm.file);
@@ -1088,6 +1089,20 @@ static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
10881089
return gate_vma;
10891090
}
10901091

1092+
static void free_vma_snapshot(struct coredump_params *cprm)
1093+
{
1094+
if (cprm->vma_meta) {
1095+
int i;
1096+
for (i = 0; i < cprm->vma_count; i++) {
1097+
struct file *file = cprm->vma_meta[i].file;
1098+
if (file)
1099+
fput(file);
1100+
}
1101+
kvfree(cprm->vma_meta);
1102+
cprm->vma_meta = NULL;
1103+
}
1104+
}
1105+
10911106
/*
10921107
* Under the mmap_lock, take a snapshot of relevant information about the task's
10931108
* VMAs.
@@ -1124,6 +1139,11 @@ static bool dump_vma_snapshot(struct coredump_params *cprm)
11241139
m->end = vma->vm_end;
11251140
m->flags = vma->vm_flags;
11261141
m->dump_size = vma_dump_size(vma, cprm->mm_flags);
1142+
m->pgoff = vma->vm_pgoff;
1143+
1144+
m->file = vma->vm_file;
1145+
if (m->file)
1146+
get_file(m->file);
11271147
}
11281148

11291149
mmap_write_unlock(mm);

include/linux/coredump.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ struct core_vma_metadata {
1111
unsigned long start, end;
1212
unsigned long flags;
1313
unsigned long dump_size;
14+
unsigned long pgoff;
15+
struct file *file;
1416
};
1517

1618
/*

0 commit comments

Comments
 (0)