tcp: fix slab-out-of-bounds in tcp_zerocopy_receive()
Subsystems (3)
PATCH 1/1 tcp: fix slab-out-of-bounds in tcp_zerocopy_receive()
Apparently a refactoring patch brought a bug, that was caught by syzbot [1]
Original code was correct, do not try to be smarter than the compiler :/
[1]
BUG: KASAN: slab-out-of-bounds in tcp_zerocopy_receive net/ipv4/tcp.c:1807 [inline] BUG: KASAN: slab-out-of-bounds in do_tcp_getsockopt.isra.0+0x2c6c/0x3120 net/ipv4/tcp.c:3654 Read of size 4 at addr ffff8880943cf188 by task syz-executor.2/17508 CPU: 0 PID: 17508 Comm: syz-executor.2 Not tainted 5.3.0-rc7+ #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x172/0x1f0 lib/dump_stack.c:113 print_address_description.cold+0xd4/0x306 mm/kasan/report.c:351 __kasan_report.cold+0x1b/0x36 mm/kasan/report.c:482 kasan_report+0x12/0x17 mm/kasan/common.c:618 __asan_report_load4_noabort+0x14/0x20 mm/kasan/generic_report.c:131 tcp_zerocopy_receive net/ipv4/tcp.c:1807 [inline] do_tcp_getsockopt.isra.0+0x2c6c/0x3120 net/ipv4/tcp.c:3654 tcp_getsockopt+0xbf/0xe0 net/ipv4/tcp.c:3680 sock_common_getsockopt+0x94/0xd0 net/core/sock.c:3098 __sys_getsockopt+0x16d/0x310 net/socket.c:2129 __do_sys_getsockopt net/socket.c:2144 [inline] __se_sys_getsockopt net/socket.c:2141 [inline] __x64_sys_getsockopt+0xbe/0x150 net/socket.c:2141 do_syscall_64+0xfd/0x6a0 arch/x86/entry/common.c:296
--- net/ipv4/tcp.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/ipv4/tcp.c +2 -4
@@ -1798,13 +1798,11 @@ static int tcp_zerocopy_receive(struct sock *sk, }if (skb_frag_size (frags) !=PAGE_SIZE ||skb_frag_off (frags)) {int remaining = zc->recv_skip_hint; -int size =skb_frag_size (frags); -while (remaining && (size !=PAGE_SIZE || +while (remaining && (skb_frag_size (frags) !=PAGE_SIZE ||skb_frag_off (frags))) { - remaining -= size; + remaining -=skb_frag_size (frags); frags++; - size =skb_frag_size (frags); } zc->recv_skip_hint -= remaining;break ;
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
Quoted 2 lines · Eric Dumazet
Apparently a refactoring patch brought a bug, that was caught by syzbot [1]
That wasn't refactoring. As you know (because we talked about it at LSFMM), this is an enabling patch for supporting hch's work to fix get_user_pages().
Quoted 2 lines
Original code was correct, do not try to be smarter than the compiler :/
That wasn't an attempt to be smarter than the compiler. I was trying to keep the line length below 80 columns. Which you probably now see that you haven't done.
I must have a blind spot here. I can't see the difference between the two versions.
Quoted 20 lines
+++
b/net/ipv4/tcp.cnet/ipv4/tcp.c +2
-4@@ -1798,13 +1798,11 @@ static int tcp_zerocopy_receive(struct sock *sk, }if (skb_frag_size (frags) !=PAGE_SIZE ||skb_frag_off (frags)) {int remaining = zc->recv_skip_hint; -int size =skb_frag_size (frags); -while (remaining && (size !=PAGE_SIZE || +while (remaining && (skb_frag_size (frags) !=PAGE_SIZE ||skb_frag_off (frags))) { - remaining -= size; + remaining -=skb_frag_size (frags); frags++; - size =skb_frag_size (frags); } zc->recv_skip_hint -= remaining;break ;Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Eric Dumazet
Apparently a refactoring patch brought a bug, that was caught by syzbot [1]
That wasn't refactoring. As you know (because we talked about it at LSFMM), this is an enabling patch for supporting hch's work to fix
get_user_pages().
Changing frags->size by skb_frag_size(frags) is what I call refactoring.
This was claimed in the changelog of your patch :
<quote>
net: Use skb accessors in network core
In preparation for unifying the skb_frag and bio_vec, use the fine accessors which already exist and use skb_frag_t instead of struct skb_frag_struct.
</quote>
I guess David trusted you enough and did not checked that you made other changes.
Quoted 7 lines
Quoted 3 lines
Original code was correct, do not try to be smarter than the compiler :/
That wasn't an attempt to be smarter than the compiler. I was trying to keep the line length below 80 columns. Which you probably now see that you haven't done.
Seriously we do not care of the 80 column rule in this particular function, we care about code correctness first.
We do not mix fixes and cleanups in the same patch.
Quoted 3 lines
I must have a blind spot here. I can't see the difference between the two versions.
No worries.
The major difference is that with your code, when @remaining reaches zero, it fetches an extra "size = skb_frag_size(frags);" at line 1807 as the syzbot report correctly points.
MAX_SKB_FRAGS is 17 on x86, meaning struct skb_shared_info frags[] array has 17 elements.
Accessing the 18th element can trigger a page fault, if struct skb_shared_info sits exactly at the end of a page.
while (remaining && (size != PAGE_SIZE ||
skb_frag_off(frags))) {
remaining -= size; // when this reaches 0
frags++; // this points to the next fragment in the array, not initialized.
size = skb_frag_size(frags); // Crash here if crossing a page and next page is unmapped.
}
Original code from Soheil was correct :
while (remaining && (frags->size != PAGE_SIZE ||
frags->page_offset)) {
remaining -= frags->size;
frags++;
}My patch simply restore this code, keeping the accessors changes which were perfectly fine, thank you.
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Wed, 2 Oct 2019 20:19:59 -0700
Quoted 7 lines
Apparently a refactoring patch brought a bug, that was caught by syzbot [1]
Original code was correct, do not try to be smarter than the compiler :/
[1]
...
Quoted 5 lines
Fixes d8e18a516f8f ("net: Use skb accessors in network core")Signed-off-by Eric Dumazet <[email protected]>Cc Matthew Wilcox (Oracle) <[email protected]>Reported-by syzbot <[email protected]>
Applied and queued up for -stable.