Skip to content

Commit 92d4a19

Browse files
Mateusz Guziksashalevin
Mateusz Guzik
authored andcommitted
prctl: take mmap sem for writing to protect against others
[ Upstream commit ddf1d39 ] An unprivileged user can trigger an oops on a kernel with CONFIG_CHECKPOINT_RESTORE. proc_pid_cmdline_read takes mmap_sem for reading and obtains args + env start/end values. These get sanity checked as follows: BUG_ON(arg_start > arg_end); BUG_ON(env_start > env_end); These can be changed by prctl_set_mm. Turns out also takes the semaphore for reading, effectively rendering it useless. This results in: kernel BUG at fs/proc/base.c:240! invalid opcode: 0000 [#1] SMP Modules linked in: virtio_net CPU: 0 PID: 925 Comm: a.out Not tainted 4.4.0-rc8-next-20160105dupa+ torvalds#71 Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 task: ffff880077a68000 ti: ffff8800784d0000 task.ti: ffff8800784d0000 RIP: proc_pid_cmdline_read+0x520/0x530 RSP: 0018:ffff8800784d3db8 EFLAGS: 00010206 RAX: ffff880077c5b6b0 RBX: ffff8800784d3f18 RCX: 0000000000000000 RDX: 0000000000000002 RSI: 00007f78e8857000 RDI: 0000000000000246 RBP: ffff8800784d3e40 R08: 0000000000000008 R09: 0000000000000001 R10: 0000000000000000 R11: 0000000000000001 R12: 0000000000000050 R13: 00007f78e8857800 R14: ffff88006fcef000 R15: ffff880077c5b600 FS: 00007f78e884a740(0000) GS:ffff88007b200000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 00007f78e8361770 CR3: 00000000790a5000 CR4: 00000000000006f0 Call Trace: __vfs_read+0x37/0x100 vfs_read+0x82/0x130 SyS_read+0x58/0xd0 entry_SYSCALL_64_fastpath+0x12/0x76 Code: 4c 8b 7d a8 eb e9 48 8b 9d 78 ff ff ff 4c 8b 7d 90 48 8b 03 48 39 45 a8 0f 87 f0 fe ff ff e9 d1 fe ff ff 4c 8b 7d 90 eb c6 0f 0b <0f> 0b 0f 0b 66 66 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 RIP proc_pid_cmdline_read+0x520/0x530 ---[ end trace 97882617ae9c6818 ]--- Turns out there are instances where the code just reads aformentioned values without locking whatsoever - namely environ_read and get_cmdline. Interestingly these functions look quite resilient against bogus values, but I don't believe this should be relied upon. The first patch gets rid of the oops bug by grabbing mmap_sem for writing. The second patch is optional and puts locking around aformentioned consumers for safety. Consumers of other fields don't seem to benefit from similar treatment and are left untouched. This patch (of 2): The code was taking the semaphore for reading, which does not protect against readers nor concurrent modifications. The problem could cause a sanity checks to fail in procfs's cmdline reader, resulting in an OOPS. Note that some functions perform an unlocked read of various mm fields, but they seem to be fine despite possible modificaton. Signed-off-by: Mateusz Guzik <[email protected]> Acked-by: Cyrill Gorcunov <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: Jarod Wilson <[email protected]> Cc: Jan Stancek <[email protected]> Cc: Al Viro <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent c81f4f0 commit 92d4a19

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

kernel/sys.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,11 +1854,13 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
18541854
user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
18551855
}
18561856

1857-
if (prctl_map.exe_fd != (u32)-1)
1857+
if (prctl_map.exe_fd != (u32)-1) {
18581858
error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
1859-
down_read(&mm->mmap_sem);
1860-
if (error)
1861-
goto out;
1859+
if (error)
1860+
return error;
1861+
}
1862+
1863+
down_write(&mm->mmap_sem);
18621864

18631865
/*
18641866
* We don't validate if these members are pointing to
@@ -1895,10 +1897,8 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
18951897
if (prctl_map.auxv_size)
18961898
memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
18971899

1898-
error = 0;
1899-
out:
1900-
up_read(&mm->mmap_sem);
1901-
return error;
1900+
up_write(&mm->mmap_sem);
1901+
return 0;
19021902
}
19031903
#endif /* CONFIG_CHECKPOINT_RESTORE */
19041904

@@ -1930,7 +1930,7 @@ static int prctl_set_mm(int opt, unsigned long addr,
19301930

19311931
error = -EINVAL;
19321932

1933-
down_read(&mm->mmap_sem);
1933+
down_write(&mm->mmap_sem);
19341934
vma = find_vma(mm, addr);
19351935

19361936
switch (opt) {
@@ -2033,7 +2033,7 @@ static int prctl_set_mm(int opt, unsigned long addr,
20332033

20342034
error = 0;
20352035
out:
2036-
up_read(&mm->mmap_sem);
2036+
up_write(&mm->mmap_sem);
20372037
return error;
20382038
}
20392039

0 commit comments

Comments
 (0)