Hi,

I've been staring at the syzbot KASAN report from yesterday:
https://syzkaller.appspot.com/bug?extid=9a6f37b22c12d89b6ade
("invalid memory access in uvm_fault_lower_io", 4 crashes, has a C
repro) and I think I know what is going on.

uvm_fault_lower_io() drops the map and amap locks before calling
pgo_get() with PGO_SYNCIO, since that can sleep on disk I/O. While it
sleeps, another thread of the same process can mmap(MAP_FIXED) over
the address range. uvm_map() unmaps the old range and
uvm_unmap_detach() puts the map entry back into the vmmpepl pool.

When pgo_get() comes back with an error, the code does this:

    if (result != VM_PAGER_OK) {
            ...
            if (!UVM_ET_ISNOFAULT(ufi->entry))
                    return (EIO);

so it reads ufi->entry->etype without the map lock held anymore, and
the entry may already be freed at that point. The relookup in
uvmfault_relock() happens a few lines later, after the deref.

This matches the KASAN output pretty exactly:

  alloc: uvm_mapent_alloc <- uvm_map <- uvm_mmapfile <- sys_mmap
  free : uvm_unmap_detach <- uvm_map <- uvm_mmapfile <- sys_mmap
  use  : uvm_fault_lower_io+0x670 <- uvm_fault_lower <- uvm_fault
         <- kpageflttrap <- _copyin

invalid read of 4 bytes, 168 byte item, pool vmmpepl. etype is the
4 byte read.

(Line numbers: the deref is at uvm_fault.c:1627 in my checkout from
today. The kasan build says 1626, so slightly different revs.)

I reproduced it in a VM using the kasan kernel from the syzbot assets
(SYZKALLER #0 Wed Nov 11), syzbot's C repro compiled as-is, and it
panics within a minute or two. KASAN output is identical to the
reports, same +0x670 offset and the same alloc/free stacks.

The repro is basically one thread doing mmap(MAP_FIXED) of a file
over and over while another thread faults on the same range past EOF,
so the pager returns an error and takes that EIO path.

NetBSD had what looks like the same bug back in 2019, fixed by chs@
in cf05d6d298b9, "in uvm_fault_lower_io(), fetch all the map entry
values that we need before we unlock everything". That change never
made it over here as far as I can tell, uvm_fault.c still reads
ufi->entry inside the pgo_get window.

Smallest fix along those lines is stashing the check before the I/O
window, something like:

    int nofault = UVM_ET_ISNOFAULT(ufi->entry);
    ...
    if (result != VM_PAGER_OK) {
    -       if (!UVM_ET_ISNOFAULT(ufi->entry))
    +       if (!nofault)
                    return (EIO);

but you probably have a better idea whether the relookup should
happen before the deref instead.

Minimized syz repro inlined below, C repro is syzbot's:
https://syzkaller.appspot.com/text?tag=ReproC&x=126003be580000

Not sure how far one can push this. Reading etype off a recycled
pool item does not leak anything by itself, but taking the
promote/PGO_DONTCARE path on a stale entry looks unhealthy.

Unrelated, but since I was in the area: the arprequest OOB read
(extid=49e574c357cc1bdb4c35) is RTM_ADD with a truncated AF_LINK
gateway sockaddr, LLADDR(gate) gets memcpy'd 6 bytes in
arprequest(). That one needs suser() for RTM_ADD so it is only a
root-triggered panic, but maybe worth a length check in rtm_output
anyway.

Borja Tarraso

--- syz repro (syzbot) ---
pipe(&(0x7f0000000140)={<r0=>0xffffffffffffffff})
close(r0)
open(&(0x7f0000000180)='./file0\x00', 0x200, 0xc2)
ktrace(0x0, 0x2, 0x1904, 0x0)
mmap(&(0x7f0000000000/0x400000)=nil, 0x400000, 0x3, 0x10, r0, 0x0)
(async, rerun: 64)
sysctl$net_inet6_ip6(&(0x7f0000000040)={0x4, 0x18, 0x29, 0x1}, 0x4,
0x0, 0x0, 0x0, 0x0) (rerun: 64)

Reply via email to