Skip to content

Commit a07279c

Browse files
thejhtorvalds
authored andcommitted
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper dump_vma_snapshot() to take a snapshot of the VMA list (including the gate VMA, if we have one) while protected by the mmap_lock, and then use that snapshot instead of walking the VMA list without locking. An alternative approach would be to keep the mmap_lock held across the entire core dumping operation; however, keeping the mmap_lock locked while we may be blocked for an unbounded amount of time (e.g. because we're dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock blocks things like the ->release handler of userfaultfd, and we don't really want critical system daemons to grind to a halt just because someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or something like that. Since both the normal ELF code and the FDPIC ELF code need this functionality (and if any other binfmt wants to add coredump support in the future, they'd probably need it, too), implement this with a common helper in fs/coredump.c. A downside of this approach is that we now need a bigger amount of kernel memory per userspace VMA in the normal ELF case, and that we need O(n) kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't be terribly bad. There currently is a data race between stack expansion and anything that reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to mitigate that for core dumping, take the mmap_lock in write mode when taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in read mode, we could end up with a corrupted core dump if someone does get_user_pages_remote() concurrently. Not really a major problem, but taking the mmap_lock either way works here, so we might as well avoid the issue.) (This doesn't do anything about the existing data races with stack expansion in other mm code.) Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Cc: Christoph Hellwig <hch@lst.de> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: "Eric W . Biederman" <ebiederm@xmission.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Hugh Dickins <hughd@google.com> Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 429a22e commit a07279c

4 files changed

Lines changed: 138 additions & 120 deletions

File tree

‎fs/binfmt_elf.c‎

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,32 +2125,6 @@ static void free_note_info(struct elf_note_info *info)
21252125

21262126
#endif
21272127

2128-
static struct vm_area_struct *first_vma(struct task_struct *tsk,
2129-
struct vm_area_struct *gate_vma)
2130-
{
2131-
struct vm_area_struct *ret = tsk->mm->mmap;
2132-
2133-
if (ret)
2134-
return ret;
2135-
return gate_vma;
2136-
}
2137-
/*
2138-
* Helper function for iterating across a vma list. It ensures that the caller
2139-
* will visit `gate_vma' prior to terminating the search.
2140-
*/
2141-
static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
2142-
struct vm_area_struct *gate_vma)
2143-
{
2144-
struct vm_area_struct *ret;
2145-
2146-
ret = this_vma->vm_next;
2147-
if (ret)
2148-
return ret;
2149-
if (this_vma == gate_vma)
2150-
return NULL;
2151-
return gate_vma;
2152-
}
2153-
21542128
static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
21552129
elf_addr_t e_shoff, int segs)
21562130
{
@@ -2177,40 +2151,25 @@ static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
21772151
static int elf_core_dump(struct coredump_params *cprm)
21782152
{
21792153
int has_dumped = 0;
2180-
int segs, i;
2181-
size_t vma_data_size = 0;
2182-
struct vm_area_struct *vma, *gate_vma;
2154+
int vma_count, segs, i;
2155+
size_t vma_data_size;
21832156
struct elfhdr elf;
21842157
loff_t offset = 0, dataoff;
21852158
struct elf_note_info info = { };
21862159
struct elf_phdr *phdr4note = NULL;
21872160
struct elf_shdr *shdr4extnum = NULL;
21882161
Elf_Half e_phnum;
21892162
elf_addr_t e_shoff;
2190-
elf_addr_t *vma_filesz = NULL;
2163+
struct core_vma_metadata *vma_meta;
2164+
2165+
if (dump_vma_snapshot(cprm, &vma_count, &vma_meta, &vma_data_size))
2166+
return 0;
21912167

2192-
/*
2193-
* We no longer stop all VM operations.
2194-
*
2195-
* This is because those proceses that could possibly change map_count
2196-
* or the mmap / vma pages are now blocked in do_exit on current
2197-
* finishing this core dump.
2198-
*
2199-
* Only ptrace can touch these memory addresses, but it doesn't change
2200-
* the map_count or the pages allocated. So no possibility of crashing
2201-
* exists while dumping the mm->vm_next areas to the core file.
2202-
*/
2203-
22042168
/*
22052169
* The number of segs are recored into ELF header as 16bit value.
22062170
* Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
22072171
*/
2208-
segs = current->mm->map_count;
2209-
segs += elf_core_extra_phdrs();
2210-
2211-
gate_vma = get_gate_vma(current->mm);
2212-
if (gate_vma != NULL)
2213-
segs++;
2172+
segs = vma_count + elf_core_extra_phdrs();
22142173

22152174
/* for notes section */
22162175
segs++;
@@ -2248,24 +2207,6 @@ static int elf_core_dump(struct coredump_params *cprm)
22482207

22492208
dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
22502209

2251-
/*
2252-
* Zero vma process will get ZERO_SIZE_PTR here.
2253-
* Let coredump continue for register state at least.
2254-
*/
2255-
vma_filesz = kvmalloc(array_size(sizeof(*vma_filesz), (segs - 1)),
2256-
GFP_KERNEL);
2257-
if (!vma_filesz)
2258-
goto end_coredump;
2259-
2260-
for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
2261-
vma = next_vma(vma, gate_vma)) {
2262-
unsigned long dump_size;
2263-
2264-
dump_size = vma_dump_size(vma, cprm->mm_flags);
2265-
vma_filesz[i++] = dump_size;
2266-
vma_data_size += dump_size;
2267-
}
2268-
22692210
offset += vma_data_size;
22702211
offset += elf_core_extra_data_size();
22712212
e_shoff = offset;
@@ -2286,21 +2227,23 @@ static int elf_core_dump(struct coredump_params *cprm)
22862227
goto end_coredump;
22872228

22882229
/* Write program headers for segments dump */
2289-
for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
2290-
vma = next_vma(vma, gate_vma)) {
2230+
for (i = 0; i < vma_count; i++) {
2231+
struct core_vma_metadata *meta = vma_meta + i;
22912232
struct elf_phdr phdr;
22922233

22932234
phdr.p_type = PT_LOAD;
22942235
phdr.p_offset = offset;
2295-
phdr.p_vaddr = vma->vm_start;
2236+
phdr.p_vaddr = meta->start;
22962237
phdr.p_paddr = 0;
2297-
phdr.p_filesz = vma_filesz[i++];
2298-
phdr.p_memsz = vma->vm_end - vma->vm_start;
2238+
phdr.p_filesz = meta->dump_size;
2239+
phdr.p_memsz = meta->end - meta->start;
22992240
offset += phdr.p_filesz;
2300-
phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
2301-
if (vma->vm_flags & VM_WRITE)
2241+
phdr.p_flags = 0;
2242+
if (meta->flags & VM_READ)
2243+
phdr.p_flags |= PF_R;
2244+
if (meta->flags & VM_WRITE)
23022245
phdr.p_flags |= PF_W;
2303-
if (vma->vm_flags & VM_EXEC)
2246+
if (meta->flags & VM_EXEC)
23042247
phdr.p_flags |= PF_X;
23052248
phdr.p_align = ELF_EXEC_PAGESIZE;
23062249

@@ -2322,9 +2265,10 @@ static int elf_core_dump(struct coredump_params *cprm)
23222265
if (!dump_skip(cprm, dataoff - cprm->pos))
23232266
goto end_coredump;
23242267

2325-
for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
2326-
vma = next_vma(vma, gate_vma)) {
2327-
if (!dump_user_range(cprm, vma->vm_start, vma_filesz[i++]))
2268+
for (i = 0; i < vma_count; i++) {
2269+
struct core_vma_metadata *meta = vma_meta + i;
2270+
2271+
if (!dump_user_range(cprm, meta->start, meta->dump_size))
23282272
goto end_coredump;
23292273
}
23302274
dump_truncate(cprm);
@@ -2340,7 +2284,7 @@ static int elf_core_dump(struct coredump_params *cprm)
23402284
end_coredump:
23412285
free_note_info(&info);
23422286
kfree(shdr4extnum);
2343-
kvfree(vma_filesz);
2287+
kvfree(vma_meta);
23442288
kfree(phdr4note);
23452289
return has_dumped;
23462290
}

‎fs/binfmt_elf_fdpic.c‎

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,29 +1454,21 @@ static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
14541454
/*
14551455
* dump the segments for an MMU process
14561456
*/
1457-
static bool elf_fdpic_dump_segments(struct coredump_params *cprm)
1457+
static bool elf_fdpic_dump_segments(struct coredump_params *cprm,
1458+
struct core_vma_metadata *vma_meta,
1459+
int vma_count)
14581460
{
1459-
struct vm_area_struct *vma;
1461+
int i;
14601462

1461-
for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
1462-
unsigned long size = vma_dump_size(vma, cprm->mm_flags);
1463+
for (i = 0; i < vma_count; i++) {
1464+
struct core_vma_metadata *meta = vma_meta + i;
14631465

1464-
if (!dump_user_range(cprm, vma->vm_start, size))
1466+
if (!dump_user_range(cprm, meta->start, meta->dump_size))
14651467
return false;
14661468
}
14671469
return true;
14681470
}
14691471

1470-
static size_t elf_core_vma_data_size(unsigned long mm_flags)
1471-
{
1472-
struct vm_area_struct *vma;
1473-
size_t size = 0;
1474-
1475-
for (vma = current->mm->mmap; vma; vma = vma->vm_next)
1476-
size += vma_dump_size(vma, mm_flags);
1477-
return size;
1478-
}
1479-
14801472
/*
14811473
* Actual dumper
14821474
*
@@ -1487,9 +1479,8 @@ static size_t elf_core_vma_data_size(unsigned long mm_flags)
14871479
static int elf_fdpic_core_dump(struct coredump_params *cprm)
14881480
{
14891481
int has_dumped = 0;
1490-
int segs;
1482+
int vma_count, segs;
14911483
int i;
1492-
struct vm_area_struct *vma;
14931484
struct elfhdr *elf = NULL;
14941485
loff_t offset = 0, dataoff;
14951486
struct memelfnote psinfo_note, auxv_note;
@@ -1503,18 +1494,8 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
15031494
elf_addr_t e_shoff;
15041495
struct core_thread *ct;
15051496
struct elf_thread_status *tmp;
1506-
1507-
/*
1508-
* We no longer stop all VM operations.
1509-
*
1510-
* This is because those proceses that could possibly change map_count
1511-
* or the mmap / vma pages are now blocked in do_exit on current
1512-
* finishing this core dump.
1513-
*
1514-
* Only ptrace can touch these memory addresses, but it doesn't change
1515-
* the map_count or the pages allocated. So no possibility of crashing
1516-
* exists while dumping the mm->vm_next areas to the core file.
1517-
*/
1497+
struct core_vma_metadata *vma_meta = NULL;
1498+
size_t vma_data_size;
15181499

15191500
/* alloc memory for large data structures: too large to be on stack */
15201501
elf = kmalloc(sizeof(*elf), GFP_KERNEL);
@@ -1524,6 +1505,9 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
15241505
if (!psinfo)
15251506
goto end_coredump;
15261507

1508+
if (dump_vma_snapshot(cprm, &vma_count, &vma_meta, &vma_data_size))
1509+
goto end_coredump;
1510+
15271511
for (ct = current->mm->core_state->dumper.next;
15281512
ct; ct = ct->next) {
15291513
tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
@@ -1543,8 +1527,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
15431527
tmp->next = thread_list;
15441528
thread_list = tmp;
15451529

1546-
segs = current->mm->map_count;
1547-
segs += elf_core_extra_phdrs();
1530+
segs = vma_count + elf_core_extra_phdrs();
15481531

15491532
/* for notes section */
15501533
segs++;
@@ -1589,7 +1572,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
15891572
/* Page-align dumped data */
15901573
dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
15911574

1592-
offset += elf_core_vma_data_size(cprm->mm_flags);
1575+
offset += vma_data_size;
15931576
offset += elf_core_extra_data_size();
15941577
e_shoff = offset;
15951578

@@ -1609,23 +1592,26 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
16091592
goto end_coredump;
16101593

16111594
/* write program headers for segments dump */
1612-
for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
1595+
for (i = 0; i < vma_count; i++) {
1596+
struct core_vma_metadata *meta = vma_meta + i;
16131597
struct elf_phdr phdr;
16141598
size_t sz;
16151599

1616-
sz = vma->vm_end - vma->vm_start;
1600+
sz = meta->end - meta->start;
16171601

16181602
phdr.p_type = PT_LOAD;
16191603
phdr.p_offset = offset;
1620-
phdr.p_vaddr = vma->vm_start;
1604+
phdr.p_vaddr = meta->start;
16211605
phdr.p_paddr = 0;
1622-
phdr.p_filesz = vma_dump_size(vma, cprm->mm_flags);
1606+
phdr.p_filesz = meta->dump_size;
16231607
phdr.p_memsz = sz;
16241608
offset += phdr.p_filesz;
1625-
phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1626-
if (vma->vm_flags & VM_WRITE)
1609+
phdr.p_flags = 0;
1610+
if (meta->flags & VM_READ)
1611+
phdr.p_flags |= PF_R;
1612+
if (meta->flags & VM_WRITE)
16271613
phdr.p_flags |= PF_W;
1628-
if (vma->vm_flags & VM_EXEC)
1614+
if (meta->flags & VM_EXEC)
16291615
phdr.p_flags |= PF_X;
16301616
phdr.p_align = ELF_EXEC_PAGESIZE;
16311617

@@ -1657,7 +1643,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
16571643
if (!dump_skip(cprm, dataoff - cprm->pos))
16581644
goto end_coredump;
16591645

1660-
if (!elf_fdpic_dump_segments(cprm))
1646+
if (!elf_fdpic_dump_segments(cprm, vma_meta, vma_count))
16611647
goto end_coredump;
16621648

16631649
if (!elf_core_write_extra_data(cprm))
@@ -1681,6 +1667,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
16811667
thread_list = thread_list->next;
16821668
kfree(tmp);
16831669
}
1670+
kvfree(vma_meta);
16841671
kfree(phdr4note);
16851672
kfree(elf);
16861673
kfree(psinfo);

0 commit comments

Comments
 (0)