Skip to content

Commit 390031c

Browse files
committed
coredump: Use the vma snapshot in fill_files_note
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>
1 parent 9ec7d32 commit 390031c

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
@@ -1641,17 +1641,16 @@ static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata,
16411641
* long file_ofs
16421642
* followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
16431643
*/
1644-
static int fill_files_note(struct memelfnote *note)
1644+
static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm)
16451645
{
1646-
struct mm_struct *mm = current->mm;
1647-
struct vm_area_struct *vma;
16481646
unsigned count, size, names_ofs, remaining, n;
16491647
user_long_t *data;
16501648
user_long_t *start_end_ofs;
16511649
char *name_base, *name_curpos;
1650+
int i;
16521651

16531652
/* *Estimated* file count and total data size needed */
1654-
count = mm->map_count;
1653+
count = cprm->vma_count;
16551654
if (count > UINT_MAX / 64)
16561655
return -EINVAL;
16571656
size = count * 64;
@@ -1673,11 +1672,12 @@ static int fill_files_note(struct memelfnote *note)
16731672
name_base = name_curpos = ((char *)data) + names_ofs;
16741673
remaining = size - names_ofs;
16751674
count = 0;
1676-
for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1675+
for (i = 0; i < cprm->vma_count; i++) {
1676+
struct core_vma_metadata *m = &cprm->vma_meta[i];
16771677
struct file *file;
16781678
const char *filename;
16791679

1680-
file = vma->vm_file;
1680+
file = m->file;
16811681
if (!file)
16821682
continue;
16831683
filename = file_path(file, name_curpos, remaining);
@@ -1697,9 +1697,9 @@ static int fill_files_note(struct memelfnote *note)
16971697
memmove(name_curpos, filename, n);
16981698
name_curpos += n;
16991699

1700-
*start_end_ofs++ = vma->vm_start;
1701-
*start_end_ofs++ = vma->vm_end;
1702-
*start_end_ofs++ = vma->vm_pgoff;
1700+
*start_end_ofs++ = m->start;
1701+
*start_end_ofs++ = m->end;
1702+
*start_end_ofs++ = m->pgoff;
17031703
count++;
17041704
}
17051705

@@ -1710,7 +1710,7 @@ static int fill_files_note(struct memelfnote *note)
17101710
* Count usually is less than mm->map_count,
17111711
* we need to move filenames down.
17121712
*/
1713-
n = mm->map_count - count;
1713+
n = cprm->vma_count - count;
17141714
if (n != 0) {
17151715
unsigned shift_bytes = n * 3 * sizeof(data[0]);
17161716
memmove(name_base - shift_bytes, name_base,
@@ -1909,7 +1909,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
19091909
fill_auxv_note(&info->auxv, current->mm);
19101910
info->size += notesize(&info->auxv);
19111911

1912-
if (fill_files_note(&info->files) == 0)
1912+
if (fill_files_note(&info->files, cprm) == 0)
19131913
info->size += notesize(&info->files);
19141914

19151915
return 1;
@@ -2098,7 +2098,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
20982098
fill_auxv_note(info->notes + 3, current->mm);
20992099
info->numnote = 4;
21002100

2101-
if (fill_files_note(info->notes + info->numnote) == 0) {
2101+
if (fill_files_note(info->notes + info->numnote, cprm) == 0) {
21022102
info->notes_files = info->notes + info->numnote;
21032103
info->numnote++;
21042104
}

fs/coredump.c

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

5757
static bool dump_vma_snapshot(struct coredump_params *cprm);
58+
static void free_vma_snapshot(struct coredump_params *cprm);
5859

5960
static int core_uses_pid;
6061
static unsigned int core_pipe_limit;
@@ -765,7 +766,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
765766
dump_emit(&cprm, "", 1);
766767
}
767768
file_end_write(cprm.file);
768-
kvfree(cprm.vma_meta);
769+
free_vma_snapshot(&cprm);
769770
}
770771
if (ispipe && core_pipe_limit)
771772
wait_for_dump_helpers(cprm.file);
@@ -1099,6 +1100,20 @@ static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
10991100
return gate_vma;
11001101
}
11011102

1103+
static void free_vma_snapshot(struct coredump_params *cprm)
1104+
{
1105+
if (cprm->vma_meta) {
1106+
int i;
1107+
for (i = 0; i < cprm->vma_count; i++) {
1108+
struct file *file = cprm->vma_meta[i].file;
1109+
if (file)
1110+
fput(file);
1111+
}
1112+
kvfree(cprm->vma_meta);
1113+
cprm->vma_meta = NULL;
1114+
}
1115+
}
1116+
11021117
/*
11031118
* Under the mmap_lock, take a snapshot of relevant information about the task's
11041119
* VMAs.
@@ -1135,6 +1150,11 @@ static bool dump_vma_snapshot(struct coredump_params *cprm)
11351150
m->end = vma->vm_end;
11361151
m->flags = vma->vm_flags;
11371152
m->dump_size = vma_dump_size(vma, cprm->mm_flags);
1153+
m->pgoff = vma->vm_pgoff;
1154+
1155+
m->file = vma->vm_file;
1156+
if (m->file)
1157+
get_file(m->file);
11381158
}
11391159

11401160
mmap_write_unlock(mm);

include/linux/coredump.h

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

1719
struct coredump_params {

0 commit comments

Comments
 (0)