All of lore.kernel.org
 help / color / mirror / Atom feed
* [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
@ 2023-06-12  9:40 syzbot
  2023-06-14 15:51 ` David Howells
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: syzbot @ 2023-06-12  9:40 UTC (permalink / raw)
  To: davem, dhowells, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

Hello,

syzbot found the following issue on:

HEAD commit:    e7c5433c5aaa tools: ynl: Remove duplicated include in hand..
git tree:       net-next
console+strace: https://syzkaller.appspot.com/x/log.txt?x=112cc875280000
kernel config:  https://syzkaller.appspot.com/x/.config?x=526f919910d4a671
dashboard link: https://syzkaller.appspot.com/bug?extid=6efc50cc1f8d718d6cb7
compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2
syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=104f3fa5280000
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=176d012d280000

Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/13c08af1fd21/disk-e7c5433c.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/35820511752b/vmlinux-e7c5433c.xz
kernel image: https://storage.googleapis.com/syzbot-assets/6a8cbec0d40f/bzImage-e7c5433c.xz

The issue was bisected to:

commit 2dc334f1a63a8839b88483a3e73c0f27c9c1791c
Author: David Howells <dhowells@redhat.com>
Date:   Wed Jun 7 18:19:09 2023 +0000

    splice, net: Use sendmsg(MSG_SPLICE_PAGES) rather than ->sendpage()

bisection log:  https://syzkaller.appspot.com/x/bisect.txt?x=12c10c75280000
final oops:     https://syzkaller.appspot.com/x/report.txt?x=11c10c75280000
console output: https://syzkaller.appspot.com/x/log.txt?x=16c10c75280000

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com
Fixes: 2dc334f1a63a ("splice, net: Use sendmsg(MSG_SPLICE_PAGES) rather than ->sendpage()")

==================================================================
BUG: KASAN: slab-out-of-bounds in sg_assign_page include/linux/scatterlist.h:109 [inline]
BUG: KASAN: slab-out-of-bounds in sg_set_page include/linux/scatterlist.h:139 [inline]
BUG: KASAN: slab-out-of-bounds in extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg lib/scatterlist.c:1352 [inline]
BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339
Read of size 8 at addr ffff88807e016ff8 by task syz-executor415/5028

CPU: 1 PID: 5028 Comm: syz-executor415 Not tainted 6.4.0-rc5-syzkaller-00915-ge7c5433c5aaa #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/25/2023
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106
 print_address_description.constprop.0+0x2c/0x3c0 mm/kasan/report.c:351
 print_report mm/kasan/report.c:462 [inline]
 kasan_report+0x11c/0x130 mm/kasan/report.c:572
 sg_assign_page include/linux/scatterlist.h:109 [inline]
 sg_set_page include/linux/scatterlist.h:139 [inline]
 extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
 extract_iter_to_sg lib/scatterlist.c:1352 [inline]
 extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339
 af_alg_sendmsg+0x1917/0x2990 crypto/af_alg.c:1045
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x954/0xe30 fs/splice.c:917
 do_splice_from fs/splice.c:969 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1157
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1103
 do_splice_direct+0x1ad/0x280 fs/splice.c:1209
 do_sendfile+0xb19/0x12c0 fs/read_write.c:1254
 __do_sys_sendfile64 fs/read_write.c:1316 [inline]
 __se_sys_sendfile64 fs/read_write.c:1308 [inline]
 __x64_sys_sendfile64+0x14d/0x210 fs/read_write.c:1308
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7f8d65191a89
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 11 15 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f8d65101308 EFLAGS: 00000246 ORIG_RAX: 0000000000000028
RAX: ffffffffffffffda RBX: 00007f8d65219428 RCX: 00007f8d65191a89
RDX: 0000000020000180 RSI: 0000000000000003 RDI: 0000000000000005
RBP: 00007f8d65219420 R08: 00007f8d65101700 R09: 0000000000000000
R10: 00000000ffffffff R11: 0000000000000246 R12: 00007f8d6521942c
R13: 00007f8d651e7064 R14: 7265687069636b73 R15: 0000000000022000
 </TASK>

Allocated by task 5028:
 kasan_save_stack+0x22/0x40 mm/kasan/common.c:45
 kasan_set_track+0x25/0x30 mm/kasan/common.c:52
 ____kasan_kmalloc mm/kasan/common.c:374 [inline]
 ____kasan_kmalloc mm/kasan/common.c:333 [inline]
 __kasan_kmalloc+0xa2/0xb0 mm/kasan/common.c:383
 kasan_kmalloc include/linux/kasan.h:196 [inline]
 __do_kmalloc_node mm/slab_common.c:966 [inline]
 __kmalloc+0x5e/0x190 mm/slab_common.c:979
 kmalloc include/linux/slab.h:563 [inline]
 sock_kmalloc+0xb2/0x100 net/core/sock.c:2630
 af_alg_alloc_tsgl crypto/af_alg.c:614 [inline]
 af_alg_sendmsg+0x17a4/0x2990 crypto/af_alg.c:1028
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x954/0xe30 fs/splice.c:917
 do_splice_from fs/splice.c:969 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1157
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1103
 do_splice_direct+0x1ad/0x280 fs/splice.c:1209
 do_sendfile+0xb19/0x12c0 fs/read_write.c:1254
 __do_sys_sendfile64 fs/read_write.c:1316 [inline]
 __se_sys_sendfile64 fs/read_write.c:1308 [inline]
 __x64_sys_sendfile64+0x14d/0x210 fs/read_write.c:1308
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

The buggy address belongs to the object at ffff88807e016000
 which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 0 bytes to the right of
 allocated 4088-byte region [ffff88807e016000, ffff88807e016ff8)

The buggy address belongs to the physical page:
page:ffffea0001f80400 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x7e010
head:ffffea0001f80400 order:3 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000010200(slab|head|node=0|zone=1|lastcpupid=0x7ff)
page_type: 0xffffffff()
raw: 00fff00000010200 ffff888012442140 dead000000000122 0000000000000000
raw: 0000000000000000 0000000080040004 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask 0xd2040(__GFP_IO|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 5019, tgid 5019 (modprobe), ts 80491362379, free_ts 80455993447
 set_page_owner include/linux/page_owner.h:31 [inline]
 post_alloc_hook+0x2db/0x350 mm/page_alloc.c:1731
 prep_new_page mm/page_alloc.c:1738 [inline]
 get_page_from_freelist+0xf41/0x2c00 mm/page_alloc.c:3502
 __alloc_pages+0x1cb/0x4a0 mm/page_alloc.c:4768
 alloc_pages+0x1aa/0x270 mm/mempolicy.c:2279
 alloc_slab_page mm/slub.c:1851 [inline]
 allocate_slab+0x25f/0x390 mm/slub.c:1998
 new_slab mm/slub.c:2051 [inline]
 ___slab_alloc+0xa91/0x1400 mm/slub.c:3192
 __slab_alloc.constprop.0+0x56/0xa0 mm/slub.c:3291
 __slab_alloc_node mm/slub.c:3344 [inline]
 slab_alloc_node mm/slub.c:3441 [inline]
 __kmem_cache_alloc_node+0x136/0x320 mm/slub.c:3490
 __do_kmalloc_node mm/slab_common.c:965 [inline]
 __kmalloc+0x4e/0x190 mm/slab_common.c:979
 kmalloc include/linux/slab.h:563 [inline]
 tomoyo_realpath_from_path+0xc3/0x600 security/tomoyo/realpath.c:251
 tomoyo_get_realpath security/tomoyo/file.c:151 [inline]
 tomoyo_path_perm+0x230/0x430 security/tomoyo/file.c:822
 security_inode_getattr+0xd3/0x140 security/security.c:2114
 vfs_getattr fs/stat.c:167 [inline]
 vfs_statx+0x16e/0x430 fs/stat.c:242
 vfs_fstatat+0x90/0xb0 fs/stat.c:276
 __do_sys_newfstatat+0x8a/0x110 fs/stat.c:446
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
page last free stack trace:
 reset_page_owner include/linux/page_owner.h:24 [inline]
 free_pages_prepare mm/page_alloc.c:1302 [inline]
 free_unref_page_prepare+0x62e/0xcb0 mm/page_alloc.c:2564
 free_unref_page+0x33/0x370 mm/page_alloc.c:2659
 __folio_put_large mm/swap.c:119 [inline]
 __folio_put+0x109/0x140 mm/swap.c:127
 folio_put include/linux/mm.h:1430 [inline]
 put_page+0x21b/0x280 include/linux/mm.h:1499
 page_to_skb+0x810/0xa10 drivers/net/virtio_net.c:560
 receive_mergeable drivers/net/virtio_net.c:1469 [inline]
 receive_buf+0x1128/0x5020 drivers/net/virtio_net.c:1590
 virtnet_receive drivers/net/virtio_net.c:1888 [inline]
 virtnet_poll+0x742/0x14b0 drivers/net/virtio_net.c:1960
 __napi_poll+0xb7/0x6f0 net/core/dev.c:6501
 napi_poll net/core/dev.c:6568 [inline]
 net_rx_action+0x8a9/0xcb0 net/core/dev.c:6701
 __do_softirq+0x1d4/0x905 kernel/softirq.c:571

Memory state around the buggy address:
 ffff88807e016e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 ffff88807e016f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff88807e016f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fc
                                                                ^
 ffff88807e017000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff88807e017080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
For information about bisection process see: https://goo.gl/tpsmEJ#bisection

If the bug is already fixed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.

If you want to change bug's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the bug is a duplicate of another bug, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-12  9:40 [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg syzbot
@ 2023-06-14 15:51 ` David Howells
  2023-06-14 23:17   ` syzbot
  2023-06-15 12:07 ` David Howells
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: David Howells @ 2023-06-14 15:51 UTC (permalink / raw)
  To: syzbot
  Cc: dhowells, davem, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main

    crypto: af_alg/hash: Fix recvmsg() after sendmsg(MSG_MORE)
    
    If an AF_ALG socket bound to a hashing algorithm is sent a zero-length
    message with MSG_MORE set and then recvmsg() is called without first
    sending another message without MSG_MORE set to end the operation, an oops
    will occur because the crypto context and result doesn't now get set up in
    advance because hash_sendmsg() now defers that as long as possible in the
    hope that it can use crypto_ahash_digest() - and then because the message
    is zero-length, it the data wrangling loop is skipped.
    
    Fix this by always making a pass of the loop, even in the case that no data
    is provided to the sendmsg().
    
    Fix also extract_iter_to_sg() to handle a zero-length iterator by returning
    0 immediately.
    
    Whilst we're at it, remove the code to create a kvmalloc'd scatterlist if
    we get more than ALG_MAX_PAGES - this shouldn't happen.
    
    Fixes: c662b043cdca ("crypto: af_alg/hash: Support MSG_SPLICE_PAGES")
    Reported-by: syzbot+13a08c0bf4d212766c3c@syzkaller.appspotmail.com
    Link: https://lore.kernel.org/r/000000000000b928f705fdeb873a@google.com/
    Signed-off-by: David Howells <dhowells@redhat.com>
    cc: Herbert Xu <herbert@gondor.apana.org.au>
    cc: "David S. Miller" <davem@davemloft.net>
    cc: Eric Dumazet <edumazet@google.com>
    cc: Jakub Kicinski <kuba@kernel.org>
    cc: Paolo Abeni <pabeni@redhat.com>
    cc: Jens Axboe <axboe@kernel.dk>
    cc: Matthew Wilcox <willy@infradead.org>
    cc: linux-crypto@vger.kernel.org
    cc: netdev@vger.kernel.org

diff --git a/crypto/algif_hash.c b/crypto/algif_hash.c
index dfb048cefb60..1176533a55c9 100644
--- a/crypto/algif_hash.c
+++ b/crypto/algif_hash.c
@@ -83,26 +83,14 @@ static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
 
 	ctx->more = false;
 
-	while (msg_data_left(msg)) {
+	do {
 		ctx->sgl.sgt.sgl = ctx->sgl.sgl;
 		ctx->sgl.sgt.nents = 0;
 		ctx->sgl.sgt.orig_nents = 0;
 
 		err = -EIO;
 		npages = iov_iter_npages(&msg->msg_iter, max_pages);
-		if (npages == 0)
-			goto unlock_free;
-
-		if (npages > ARRAY_SIZE(ctx->sgl.sgl)) {
-			err = -ENOMEM;
-			ctx->sgl.sgt.sgl =
-				kvmalloc(array_size(npages,
-						    sizeof(*ctx->sgl.sgt.sgl)),
-					 GFP_KERNEL);
-			if (!ctx->sgl.sgt.sgl)
-				goto unlock_free;
-		}
-		sg_init_table(ctx->sgl.sgl, npages);
+		sg_init_table(ctx->sgl.sgl, max_t(size_t, npages, 1));
 
 		ctx->sgl.need_unpin = iov_iter_extract_will_pin(&msg->msg_iter);
 
@@ -111,7 +99,8 @@ static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
 		if (err < 0)
 			goto unlock_free;
 		len = err;
-		sg_mark_end(ctx->sgl.sgt.sgl + ctx->sgl.sgt.nents - 1);
+		if (len > 0)
+			sg_mark_end(ctx->sgl.sgt.sgl + ctx->sgl.sgt.nents - 1);
 
 		if (!msg_data_left(msg)) {
 			err = hash_alloc_result(sk, ctx);
@@ -148,7 +137,7 @@ static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
 
 		copied += len;
 		af_alg_free_sg(&ctx->sgl);
-	}
+	} while (msg_data_left(msg));
 
 	ctx->more = msg->msg_flags & MSG_MORE;
 	err = 0;
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index e97d7060329e..77a7b18ee751 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -1340,7 +1340,7 @@ ssize_t extract_iter_to_sg(struct iov_iter *iter, size_t maxsize,
 			   struct sg_table *sgtable, unsigned int sg_max,
 			   iov_iter_extraction_t extraction_flags)
 {
-	if (maxsize == 0)
+	if (!maxsize || !iter->count)
 		return 0;
 
 	switch (iov_iter_type(iter)) {


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-14 15:51 ` David Howells
@ 2023-06-14 23:17   ` syzbot
  0 siblings, 0 replies; 11+ messages in thread
From: syzbot @ 2023-06-14 23:17 UTC (permalink / raw)
  To: davem, dhowells, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

Hello,

syzbot has tested the proposed patch but the reproducer is still triggering an issue:
KASAN: slab-out-of-bounds Read in extract_iter_to_sg

==================================================================
BUG: KASAN: slab-out-of-bounds in sg_assign_page include/linux/scatterlist.h:109 [inline]
BUG: KASAN: slab-out-of-bounds in sg_set_page include/linux/scatterlist.h:139 [inline]
BUG: KASAN: slab-out-of-bounds in extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg lib/scatterlist.c:1352 [inline]
BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg+0x180b/0x1970 lib/scatterlist.c:1339
Read of size 8 at addr ffff8880282aaff8 by task syz-executor.0/5450

CPU: 0 PID: 5450 Comm: syz-executor.0 Not tainted 6.4.0-rc5-syzkaller-gfa0e21fa4443-dirty #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/27/2023
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106
 print_address_description.constprop.0+0x2c/0x3c0 mm/kasan/report.c:351
 print_report mm/kasan/report.c:462 [inline]
 kasan_report+0x11c/0x130 mm/kasan/report.c:572
 sg_assign_page include/linux/scatterlist.h:109 [inline]
 sg_set_page include/linux/scatterlist.h:139 [inline]
 extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
 extract_iter_to_sg lib/scatterlist.c:1352 [inline]
 extract_iter_to_sg+0x180b/0x1970 lib/scatterlist.c:1339
 af_alg_sendmsg+0x1917/0x2990 crypto/af_alg.c:1045
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x954/0xe30 fs/splice.c:917
 do_splice_from fs/splice.c:969 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1157
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1103
 do_splice_direct+0x1ad/0x280 fs/splice.c:1209
 do_sendfile+0xb19/0x12c0 fs/read_write.c:1254
 __do_sys_sendfile64 fs/read_write.c:1316 [inline]
 __se_sys_sendfile64 fs/read_write.c:1308 [inline]
 __x64_sys_sendfile64+0x14d/0x210 fs/read_write.c:1308
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7f10eb08c169
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 f1 19 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f10ebe5e168 EFLAGS: 00000246 ORIG_RAX: 0000000000000028
RAX: ffffffffffffffda RBX: 00007f10eb1ac120 RCX: 00007f10eb08c169
RDX: 0000000020000180 RSI: 0000000000000003 RDI: 0000000000000005
RBP: 00007f10eb0e7ca1 R08: 0000000000000000 R09: 0000000000000000
R10: 00000000ffffffff R11: 0000000000000246 R12: 0000000000000000
R13: 00007ffea268bd7f R14: 00007f10ebe5e300 R15: 0000000000022000
 </TASK>

Allocated by task 5450:
 kasan_save_stack+0x22/0x40 mm/kasan/common.c:45
 kasan_set_track+0x25/0x30 mm/kasan/common.c:52
 ____kasan_kmalloc mm/kasan/common.c:374 [inline]
 ____kasan_kmalloc mm/kasan/common.c:333 [inline]
 __kasan_kmalloc+0xa2/0xb0 mm/kasan/common.c:383
 kasan_kmalloc include/linux/kasan.h:196 [inline]
 __do_kmalloc_node mm/slab_common.c:966 [inline]
 __kmalloc+0x5e/0x190 mm/slab_common.c:979
 kmalloc include/linux/slab.h:563 [inline]
 sock_kmalloc+0xb2/0x100 net/core/sock.c:2674
 af_alg_alloc_tsgl crypto/af_alg.c:614 [inline]
 af_alg_sendmsg+0x17a4/0x2990 crypto/af_alg.c:1028
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x954/0xe30 fs/splice.c:917
 do_splice_from fs/splice.c:969 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1157
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1103
 do_splice_direct+0x1ad/0x280 fs/splice.c:1209
 do_sendfile+0xb19/0x12c0 fs/read_write.c:1254
 __do_sys_sendfile64 fs/read_write.c:1316 [inline]
 __se_sys_sendfile64 fs/read_write.c:1308 [inline]
 __x64_sys_sendfile64+0x14d/0x210 fs/read_write.c:1308
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

The buggy address belongs to the object at ffff8880282aa000
 which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 0 bytes to the right of
 allocated 4088-byte region [ffff8880282aa000, ffff8880282aaff8)

The buggy address belongs to the physical page:
page:ffffea0000a0aa00 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x282a8
head:ffffea0000a0aa00 order:3 entire_mapcount:0 nr_pages_mapped:0 pincount:0
anon flags: 0xfff00000010200(slab|head|node=0|zone=1|lastcpupid=0x7ff)
page_type: 0xffffffff()
raw: 00fff00000010200 ffff888012442140 0000000000000000 0000000000000001
raw: 0000000000000000 0000000080040004 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask 0x1d2040(__GFP_IO|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC|__GFP_HARDWALL), pid 5156, tgid 5156 (dhcpcd-run-hook), ts 68845362597, free_ts 68027991491
 set_page_owner include/linux/page_owner.h:31 [inline]
 post_alloc_hook+0x2db/0x350 mm/page_alloc.c:1731
 prep_new_page mm/page_alloc.c:1738 [inline]
 get_page_from_freelist+0xf41/0x2c00 mm/page_alloc.c:3502
 __alloc_pages+0x1cb/0x4a0 mm/page_alloc.c:4768
 alloc_pages+0x1aa/0x270 mm/mempolicy.c:2279
 alloc_slab_page mm/slub.c:1851 [inline]
 allocate_slab+0x25f/0x390 mm/slub.c:1998
 new_slab mm/slub.c:2051 [inline]
 ___slab_alloc+0xa91/0x1400 mm/slub.c:3192
 __slab_alloc.constprop.0+0x56/0xa0 mm/slub.c:3291
 __slab_alloc_node mm/slub.c:3344 [inline]
 slab_alloc_node mm/slub.c:3441 [inline]
 __kmem_cache_alloc_node+0x136/0x320 mm/slub.c:3490
 __do_kmalloc_node mm/slab_common.c:965 [inline]
 __kmalloc+0x4e/0x190 mm/slab_common.c:979
 kmalloc include/linux/slab.h:563 [inline]
 tomoyo_realpath_from_path+0xc3/0x600 security/tomoyo/realpath.c:251
 tomoyo_get_realpath security/tomoyo/file.c:151 [inline]
 tomoyo_check_open_permission+0x29a/0x3a0 security/tomoyo/file.c:771
 tomoyo_file_open security/tomoyo/tomoyo.c:332 [inline]
 tomoyo_file_open+0xa1/0xc0 security/tomoyo/tomoyo.c:327
 security_file_open+0x49/0xb0 security/security.c:2797
 do_dentry_open+0x575/0x13f0 fs/open.c:907
 do_open fs/namei.c:3636 [inline]
 path_openat+0x1baa/0x2750 fs/namei.c:3791
 do_filp_open+0x1ba/0x410 fs/namei.c:3818
page last free stack trace:
 reset_page_owner include/linux/page_owner.h:24 [inline]
 free_pages_prepare mm/page_alloc.c:1302 [inline]
 free_unref_page_prepare+0x62e/0xcb0 mm/page_alloc.c:2564
 free_unref_page+0x33/0x370 mm/page_alloc.c:2659
 qlink_free mm/kasan/quarantine.c:166 [inline]
 qlist_free_all+0x6a/0x170 mm/kasan/quarantine.c:185
 kasan_quarantine_reduce+0x195/0x220 mm/kasan/quarantine.c:292
 __kasan_slab_alloc+0x63/0x90 mm/kasan/common.c:305
 kasan_slab_alloc include/linux/kasan.h:186 [inline]
 slab_post_alloc_hook mm/slab.h:711 [inline]
 slab_alloc_node mm/slub.c:3451 [inline]
 slab_alloc mm/slub.c:3459 [inline]
 __kmem_cache_alloc_lru mm/slub.c:3466 [inline]
 kmem_cache_alloc+0x17c/0x3b0 mm/slub.c:3475
 vm_area_alloc+0x20/0x230 kernel/fork.c:489
 mmap_region+0x407/0x28d0 mm/mmap.c:2631
 do_mmap+0x831/0xf60 mm/mmap.c:1394
 vm_mmap_pgoff+0x1a2/0x3b0 mm/util.c:543
 ksys_mmap_pgoff+0x7d/0x5a0 mm/mmap.c:1440
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

Memory state around the buggy address:
 ffff8880282aae80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 ffff8880282aaf00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff8880282aaf80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fc
                                                                ^
 ffff8880282ab000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff8880282ab080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================


Tested on:

commit:         fa0e21fa rtnetlink: extend RTEXT_FILTER_SKIP_STATS to ..
git tree:       git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main
console output: https://syzkaller.appspot.com/x/log.txt?x=13c29b07280000
kernel config:  https://syzkaller.appspot.com/x/.config?x=526f919910d4a671
dashboard link: https://syzkaller.appspot.com/bug?extid=6efc50cc1f8d718d6cb7
compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2
patch:          https://syzkaller.appspot.com/x/patch.diff?x=127f213b280000


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-12  9:40 [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg syzbot
  2023-06-14 15:51 ` David Howells
@ 2023-06-15 12:07 ` David Howells
  2023-06-15 15:06   ` syzbot
  2023-06-15 14:21 ` David Howells
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: David Howells @ 2023-06-15 12:07 UTC (permalink / raw)
  To: syzbot
  Cc: dhowells, davem, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main

diff --git a/fs/splice.c b/fs/splice.c
index 67ddaac1f5c5..17d692449e83 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -886,7 +886,6 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
 			}
 
 			seg = min_t(size_t, remain, buf->len);
-			seg = min_t(size_t, seg, PAGE_SIZE);
 
 			ret = pipe_buf_confirm(pipe, buf);
 			if (unlikely(ret)) {
@@ -897,10 +896,9 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
 
 			bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset);
 			remain -= seg;
-			if (seg >= buf->len)
-				tail++;
-			if (bc >= ARRAY_SIZE(bvec))
+			if (remain == 0 || bc >= ARRAY_SIZE(bvec))
 				break;
+			tail++;
 		}
 
 		if (!bc)
diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
index 457598dfa128..6e70839257f7 100644
--- a/net/ipv4/ip_output.c
+++ b/net/ipv4/ip_output.c
@@ -1041,7 +1041,8 @@ static int __ip_append_data(struct sock *sk,
 	} else if ((flags & MSG_SPLICE_PAGES) && length) {
 		if (inet->hdrincl)
 			return -EPERM;
-		if (rt->dst.dev->features & NETIF_F_SG)
+		if (rt->dst.dev->features & NETIF_F_SG &&
+		    getfrag == ip_generic_getfrag)
 			/* We need an empty buffer to attach stuff to */
 			paged = true;
 		else
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index c06ff7519f19..1e8c90e97608 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -1593,7 +1593,8 @@ static int __ip6_append_data(struct sock *sk,
 	} else if ((flags & MSG_SPLICE_PAGES) && length) {
 		if (inet_sk(sk)->hdrincl)
 			return -EPERM;
-		if (rt->dst.dev->features & NETIF_F_SG)
+		if (rt->dst.dev->features & NETIF_F_SG &&
+		    getfrag == ip_generic_getfrag)
 			/* We need an empty buffer to attach stuff to */
 			paged = true;
 		else


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-12  9:40 [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg syzbot
  2023-06-14 15:51 ` David Howells
  2023-06-15 12:07 ` David Howells
@ 2023-06-15 14:21 ` David Howells
  2023-06-15 16:01 ` David Howells
  2023-06-15 20:24   ` David Howells
  4 siblings, 0 replies; 11+ messages in thread
From: David Howells @ 2023-06-15 14:21 UTC (permalink / raw)
  To: herbert
  Cc: dhowells, syzbot, davem, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

David Howells <dhowells@redhat.com> wrote:

> @@ -83,26 +83,14 @@ static int hash_sendmsg(struct socket *sock, struct msghdr *msg,

Actually, this patch definitely won't fix this bug since the test is using an
skcipher not a hash.

David


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-15 12:07 ` David Howells
@ 2023-06-15 15:06   ` syzbot
  0 siblings, 0 replies; 11+ messages in thread
From: syzbot @ 2023-06-15 15:06 UTC (permalink / raw)
  To: davem, dhowells, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

Hello,

syzbot has tested the proposed patch but the reproducer is still triggering an issue:
KASAN: slab-out-of-bounds Read in extract_iter_to_sg

==================================================================
BUG: KASAN: slab-out-of-bounds in sg_assign_page include/linux/scatterlist.h:109 [inline]
BUG: KASAN: slab-out-of-bounds in sg_set_page include/linux/scatterlist.h:139 [inline]
BUG: KASAN: slab-out-of-bounds in extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg lib/scatterlist.c:1352 [inline]
BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339
Read of size 8 at addr ffff888070558ff8 by task syz-executor.0/5573

CPU: 0 PID: 5573 Comm: syz-executor.0 Not tainted 6.4.0-rc5-syzkaller-01229-g97c5209b3d37-dirty #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/27/2023
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106
 print_address_description.constprop.0+0x2c/0x3c0 mm/kasan/report.c:351
 print_report mm/kasan/report.c:462 [inline]
 kasan_report+0x11c/0x130 mm/kasan/report.c:572
 sg_assign_page include/linux/scatterlist.h:109 [inline]
 sg_set_page include/linux/scatterlist.h:139 [inline]
 extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
 extract_iter_to_sg lib/scatterlist.c:1352 [inline]
 extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339
 af_alg_sendmsg+0x1917/0x2990 crypto/af_alg.c:1045
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x964/0xee0 fs/splice.c:915
 do_splice_from fs/splice.c:967 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1155
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1101
 do_splice_direct+0x1ad/0x280 fs/splice.c:1207
 do_sendfile+0xb19/0x12c0 fs/read_write.c:1254
 __do_sys_sendfile64 fs/read_write.c:1316 [inline]
 __se_sys_sendfile64 fs/read_write.c:1308 [inline]
 __x64_sys_sendfile64+0x14d/0x210 fs/read_write.c:1308
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7ff232a8c169
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 f1 19 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ff2337f5168 EFLAGS: 00000246 ORIG_RAX: 0000000000000028
RAX: ffffffffffffffda RBX: 00007ff232bac120 RCX: 00007ff232a8c169
RDX: 0000000020000180 RSI: 0000000000000003 RDI: 0000000000000005
RBP: 00007ff232ae7ca1 R08: 0000000000000000 R09: 0000000000000000
R10: 00000000ffffffff R11: 0000000000000246 R12: 0000000000000000
R13: 00007fffec66777f R14: 00007ff2337f5300 R15: 0000000000022000
 </TASK>

Allocated by task 5573:
 kasan_save_stack+0x22/0x40 mm/kasan/common.c:45
 kasan_set_track+0x25/0x30 mm/kasan/common.c:52
 ____kasan_kmalloc mm/kasan/common.c:374 [inline]
 ____kasan_kmalloc mm/kasan/common.c:333 [inline]
 __kasan_kmalloc+0xa2/0xb0 mm/kasan/common.c:383
 kasan_kmalloc include/linux/kasan.h:196 [inline]
 __do_kmalloc_node mm/slab_common.c:966 [inline]
 __kmalloc+0x5e/0x190 mm/slab_common.c:979
 kmalloc include/linux/slab.h:563 [inline]
 sock_kmalloc+0xb2/0x100 net/core/sock.c:2674
 af_alg_alloc_tsgl crypto/af_alg.c:614 [inline]
 af_alg_sendmsg+0x17a4/0x2990 crypto/af_alg.c:1028
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x964/0xee0 fs/splice.c:915
 do_splice_from fs/splice.c:967 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1155
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1101
 do_splice_direct+0x1ad/0x280 fs/splice.c:1207
 do_sendfile+0xb19/0x12c0 fs/read_write.c:1254
 __do_sys_sendfile64 fs/read_write.c:1316 [inline]
 __se_sys_sendfile64 fs/read_write.c:1308 [inline]
 __x64_sys_sendfile64+0x14d/0x210 fs/read_write.c:1308
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

The buggy address belongs to the object at ffff888070558000
 which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 0 bytes to the right of
 allocated 4088-byte region [ffff888070558000, ffff888070558ff8)

The buggy address belongs to the physical page:
page:ffffea0001c15600 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x70558
head:ffffea0001c15600 order:3 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000010200(slab|head|node=0|zone=1|lastcpupid=0x7ff)
page_type: 0xffffffff()
raw: 00fff00000010200 ffff888012442140 dead000000000122 0000000000000000
raw: 0000000000000000 0000000000040004 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask 0x1d20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC|__GFP_HARDWALL), pid 5573, tgid 5567 (syz-executor.0), ts 94731634455, free_ts 69153490166
 set_page_owner include/linux/page_owner.h:31 [inline]
 post_alloc_hook+0x2db/0x350 mm/page_alloc.c:1731
 prep_new_page mm/page_alloc.c:1738 [inline]
 get_page_from_freelist+0xf41/0x2c00 mm/page_alloc.c:3502
 __alloc_pages+0x1cb/0x4a0 mm/page_alloc.c:4768
 alloc_pages+0x1aa/0x270 mm/mempolicy.c:2279
 alloc_slab_page mm/slub.c:1851 [inline]
 allocate_slab+0x25f/0x390 mm/slub.c:1998
 new_slab mm/slub.c:2051 [inline]
 ___slab_alloc+0xa91/0x1400 mm/slub.c:3192
 __slab_alloc.constprop.0+0x56/0xa0 mm/slub.c:3291
 __slab_alloc_node mm/slub.c:3344 [inline]
 slab_alloc_node mm/slub.c:3441 [inline]
 __kmem_cache_alloc_node+0x136/0x320 mm/slub.c:3490
 __do_kmalloc_node mm/slab_common.c:965 [inline]
 __kmalloc+0x4e/0x190 mm/slab_common.c:979
 kmalloc include/linux/slab.h:563 [inline]
 sock_kmalloc+0xb2/0x100 net/core/sock.c:2674
 af_alg_alloc_tsgl crypto/af_alg.c:614 [inline]
 af_alg_sendmsg+0x17a4/0x2990 crypto/af_alg.c:1028
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0xde/0x190 net/socket.c:747
 splice_to_socket+0x964/0xee0 fs/splice.c:915
 do_splice_from fs/splice.c:967 [inline]
 direct_splice_actor+0x114/0x180 fs/splice.c:1155
 splice_direct_to_actor+0x34a/0x9c0 fs/splice.c:1101
 do_splice_direct+0x1ad/0x280 fs/splice.c:1207
page last free stack trace:
 reset_page_owner include/linux/page_owner.h:24 [inline]
 free_pages_prepare mm/page_alloc.c:1302 [inline]
 __free_pages_ok+0x77f/0x1060 mm/page_alloc.c:1441
 kvfree+0x46/0x50 mm/util.c:650
 fq_reset include/net/fq_impl.h:386 [inline]
 ieee80211_txq_teardown_flows+0x165/0x270 net/mac80211/tx.c:1638
 ieee80211_remove_interfaces+0x13d/0x690 net/mac80211/iface.c:2280
 ieee80211_unregister_hw+0x4b/0x240 net/mac80211/main.c:1483
 mac80211_hwsim_del_radio drivers/net/wireless/virtual/mac80211_hwsim.c:5408 [inline]
 hwsim_exit_net+0x412/0x840 drivers/net/wireless/virtual/mac80211_hwsim.c:6284
 ops_exit_list+0xb0/0x170 net/core/net_namespace.c:170
 cleanup_net+0x4ee/0xb10 net/core/net_namespace.c:614
 process_one_work+0x99a/0x15e0 kernel/workqueue.c:2405
 worker_thread+0x67d/0x10c0 kernel/workqueue.c:2552
 kthread+0x344/0x440 kernel/kthread.c:379
 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308

Memory state around the buggy address:
 ffff888070558e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 ffff888070558f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff888070558f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fc
                                                                ^
 ffff888070559000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff888070559080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================


Tested on:

commit:         97c5209b leds: trigger: netdev: uninitialized variable..
git tree:       git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main
console output: https://syzkaller.appspot.com/x/log.txt?x=158fa0e3280000
kernel config:  https://syzkaller.appspot.com/x/.config?x=526f919910d4a671
dashboard link: https://syzkaller.appspot.com/bug?extid=6efc50cc1f8d718d6cb7
compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2
patch:          https://syzkaller.appspot.com/x/patch.diff?x=17ff78cf280000


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-12  9:40 [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg syzbot
                   ` (2 preceding siblings ...)
  2023-06-15 14:21 ` David Howells
@ 2023-06-15 16:01 ` David Howells
  2023-06-15 16:29   ` syzbot
  2023-06-15 20:24   ` David Howells
  4 siblings, 1 reply; 11+ messages in thread
From: David Howells @ 2023-06-15 16:01 UTC (permalink / raw)
  To: syzbot
  Cc: dhowells, davem, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main


diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 38d2265c77fd..e97abe6055a1 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -4333,8 +4333,7 @@ static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
 		}
 		sgtable.orig_nents = sgtable.nents;
 
-		rc = extract_iter_to_sg(iter, count, &sgtable,
-					num_sgs - sgtable.nents, 0);
+		rc = extract_iter_to_sg(iter, count, &sgtable, num_sgs, 0);
 		iov_iter_revert(iter, rc);
 		sgtable.orig_nents = sgtable.nents;
 	}
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index e97d7060329e..6fd20bfc01a4 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -1120,7 +1120,8 @@ static ssize_t extract_user_to_sg(struct iov_iter *iter,
 	pages -= sg_max;
 
 	do {
-		res = iov_iter_extract_pages(iter, &pages, maxsize, sg_max,
+		res = iov_iter_extract_pages(iter, &pages, maxsize,
+					     sg_max - sgtable->nents,
 					     extraction_flags, &off);
 		if (res < 0)
 			goto failed;
@@ -1129,7 +1130,6 @@ static ssize_t extract_user_to_sg(struct iov_iter *iter,
 		maxsize -= len;
 		ret += len;
 		npages = DIV_ROUND_UP(off + len, PAGE_SIZE);
-		sg_max -= npages;
 
 		for (; npages > 0; npages--) {
 			struct page *page = *pages;
@@ -1142,7 +1142,7 @@ static ssize_t extract_user_to_sg(struct iov_iter *iter,
 			len -= seg;
 			off = 0;
 		}
-	} while (maxsize > 0 && sg_max > 0);
+	} while (maxsize > 0 && sgtable->nents < sg_max);
 
 	return ret;
 
@@ -1183,11 +1183,10 @@ static ssize_t extract_bvec_to_sg(struct iov_iter *iter,
 		sg_set_page(sg, bv[i].bv_page, len, off);
 		sgtable->nents++;
 		sg++;
-		sg_max--;
 
 		ret += len;
 		maxsize -= len;
-		if (maxsize <= 0 || sg_max == 0)
+		if (maxsize <= 0 || sgtable->nents >= sg_max)
 			break;
 		start = 0;
 	}
@@ -1242,14 +1241,13 @@ static ssize_t extract_kvec_to_sg(struct iov_iter *iter,
 			sg_set_page(sg, page, len, off);
 			sgtable->nents++;
 			sg++;
-			sg_max--;
 
 			len -= seg;
 			kaddr += PAGE_SIZE;
 			off = 0;
-		} while (len > 0 && sg_max > 0);
+		} while (len > 0 && sgtable->nents < sg_max);
 
-		if (maxsize <= 0 || sg_max == 0)
+		if (maxsize <= 0 || sgtable->nents >= sg_max)
 			break;
 		start = 0;
 	}
@@ -1294,11 +1292,10 @@ static ssize_t extract_xarray_to_sg(struct iov_iter *iter,
 		sg_set_page(sg, folio_page(folio, 0), len, offset);
 		sgtable->nents++;
 		sg++;
-		sg_max--;
 
 		maxsize -= len;
 		ret += len;
-		if (maxsize <= 0 || sg_max == 0)
+		if (maxsize <= 0 || sgtable->nents >= sg_max)
 			break;
 	}
 
@@ -1318,7 +1315,8 @@ static ssize_t extract_xarray_to_sg(struct iov_iter *iter,
  *
  * Extract the page fragments from the given amount of the source iterator and
  * add them to a scatterlist that refers to all of those bits, to a maximum
- * addition of @sg_max elements.
+ * addition of @sg_max elements.  @sgtable->nents indicates how many of the
+ * elements are already used.
  *
  * The pages referred to by UBUF- and IOVEC-type iterators are extracted and
  * pinned; BVEC-, KVEC- and XARRAY-type are extracted but aren't pinned; PIPE-
@@ -1343,6 +1341,11 @@ ssize_t extract_iter_to_sg(struct iov_iter *iter, size_t maxsize,
 	if (maxsize == 0)
 		return 0;
 
+	if (WARN_ON_ONCE(sg_max == 0))
+		return -EIO;
+	if (WARN_ON_ONCE(sgtable->nents >= sg_max))
+		return -EIO;
+
 	switch (iov_iter_type(iter)) {
 	case ITER_UBUF:
 	case ITER_IOVEC:


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-15 16:01 ` David Howells
@ 2023-06-15 16:29   ` syzbot
  0 siblings, 0 replies; 11+ messages in thread
From: syzbot @ 2023-06-15 16:29 UTC (permalink / raw)
  To: davem, dhowells, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Reported-and-tested-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com

Tested on:

commit:         97c5209b leds: trigger: netdev: uninitialized variable..
git tree:       git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main
console output: https://syzkaller.appspot.com/x/log.txt?x=1258baf7280000
kernel config:  https://syzkaller.appspot.com/x/.config?x=526f919910d4a671
dashboard link: https://syzkaller.appspot.com/bug?extid=6efc50cc1f8d718d6cb7
compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2
patch:          https://syzkaller.appspot.com/x/patch.diff?x=12c65e17280000

Note: testing is done by a robot and is best-effort only.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-12  9:40 [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg syzbot
@ 2023-06-15 20:24   ` David Howells
  2023-06-15 12:07 ` David Howells
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 11+ messages in thread
From: David Howells @ 2023-06-15 20:24 UTC (permalink / raw)
  Cc: dhowells, syzbot, davem, herbert, kuba, linux-crypto,
	linux-kernel, netdev, syzkaller-bugs

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main

    crypto: Fix af_alg_sendmsg(MSG_SPLICE_PAGES) sglist limit
    
    When af_alg_sendmsg() calls extract_iter_to_sg(), it passes MAX_SGL_ENTS as
    the maximum number of elements that may be written to, but some of the
    elements may already have been used (as recorded in sgl->cur), so
    extract_iter_to_sg() may end up overrunning the scatterlist.
    
    Fix this to limit the number of elements to "MAX_SGL_ENTS - sgl->cur".
    
    Note: It probably makes sense in future to alter the behaviour of
    extract_iter_to_sg() to stop if "sgtable->nents >= sg_max" instead, but
    this is a smaller fix for now.
    
    The bug causes errors looking something like:
    
    BUG: KASAN: slab-out-of-bounds in sg_assign_page include/linux/scatterlist.h:109 [inline]
    BUG: KASAN: slab-out-of-bounds in sg_set_page include/linux/scatterlist.h:139 [inline]
    BUG: KASAN: slab-out-of-bounds in extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
    BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg lib/scatterlist.c:1352 [inline]
    BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339
    
    Fixes: bf63e250c4b1 ("crypto: af_alg: Support MSG_SPLICE_PAGES")
    Reported-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com
    Signed-off-by: David Howells <dhowells@redhat.com>
    cc: Herbert Xu <herbert@gondor.apana.org.au>
    cc: "David S. Miller" <davem@davemloft.net>
    cc: Eric Dumazet <edumazet@google.com>
    cc: Jakub Kicinski <kuba@kernel.org>
    cc: Paolo Abeni <pabeni@redhat.com>
    cc: Jens Axboe <axboe@kernel.dk>
    cc: Matthew Wilcox <willy@infradead.org>
    cc: linux-crypto@vger.kernel.org
    cc: netdev@vger.kernel.org

diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index 7d4b6016b83d..cdb1dcc5dd1a 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -1043,7 +1043,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
 			};
 
 			plen = extract_iter_to_sg(&msg->msg_iter, len, &sgtable,
-						  MAX_SGL_ENTS, 0);
+						  MAX_SGL_ENTS - sgl->cur, 0);
 			if (plen < 0) {
 				err = plen;
 				goto unlock;


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
@ 2023-06-15 20:24   ` David Howells
  0 siblings, 0 replies; 11+ messages in thread
From: David Howells @ 2023-06-15 20:24 UTC (permalink / raw)
  Cc: dhowells, syzbot, davem, herbert, kuba, linux-crypto,
	linux-kernel, netdev, syzkaller-bugs

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main

    crypto: Fix af_alg_sendmsg(MSG_SPLICE_PAGES) sglist limit
    
    When af_alg_sendmsg() calls extract_iter_to_sg(), it passes MAX_SGL_ENTS as
    the maximum number of elements that may be written to, but some of the
    elements may already have been used (as recorded in sgl->cur), so
    extract_iter_to_sg() may end up overrunning the scatterlist.
    
    Fix this to limit the number of elements to "MAX_SGL_ENTS - sgl->cur".
    
    Note: It probably makes sense in future to alter the behaviour of
    extract_iter_to_sg() to stop if "sgtable->nents >= sg_max" instead, but
    this is a smaller fix for now.
    
    The bug causes errors looking something like:
    
    BUG: KASAN: slab-out-of-bounds in sg_assign_page include/linux/scatterlist.h:109 [inline]
    BUG: KASAN: slab-out-of-bounds in sg_set_page include/linux/scatterlist.h:139 [inline]
    BUG: KASAN: slab-out-of-bounds in extract_bvec_to_sg lib/scatterlist.c:1183 [inline]
    BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg lib/scatterlist.c:1352 [inline]
    BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339
    
    Fixes: bf63e250c4b1 ("crypto: af_alg: Support MSG_SPLICE_PAGES")
    Reported-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com
    Signed-off-by: David Howells <dhowells@redhat.com>
    cc: Herbert Xu <herbert@gondor.apana.org.au>
    cc: "David S. Miller" <davem@davemloft.net>
    cc: Eric Dumazet <edumazet@google.com>
    cc: Jakub Kicinski <kuba@kernel.org>
    cc: Paolo Abeni <pabeni@redhat.com>
    cc: Jens Axboe <axboe@kernel.dk>
    cc: Matthew Wilcox <willy@infradead.org>
    cc: linux-crypto@vger.kernel.org
    cc: netdev@vger.kernel.org

diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index 7d4b6016b83d..cdb1dcc5dd1a 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -1043,7 +1043,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
 			};
 
 			plen = extract_iter_to_sg(&msg->msg_iter, len, &sgtable,
-						  MAX_SGL_ENTS, 0);
+						  MAX_SGL_ENTS - sgl->cur, 0);
 			if (plen < 0) {
 				err = plen;
 				goto unlock;


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg
  2023-06-15 20:24   ` David Howells
  (?)
@ 2023-06-15 21:02   ` syzbot
  -1 siblings, 0 replies; 11+ messages in thread
From: syzbot @ 2023-06-15 21:02 UTC (permalink / raw)
  To: davem, dhowells, herbert, kuba, linux-crypto, linux-kernel,
	netdev, syzkaller-bugs

Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Reported-and-tested-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com

Tested on:

commit:         97c5209b leds: trigger: netdev: uninitialized variable..
git tree:       git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main
console output: https://syzkaller.appspot.com/x/log.txt?x=13705753280000
kernel config:  https://syzkaller.appspot.com/x/.config?x=526f919910d4a671
dashboard link: https://syzkaller.appspot.com/bug?extid=6efc50cc1f8d718d6cb7
compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2
patch:          https://syzkaller.appspot.com/x/patch.diff?x=10d19cbb280000

Note: testing is done by a robot and is best-effort only.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-06-15 21:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-12  9:40 [syzbot] [crypto?] KASAN: slab-out-of-bounds Read in extract_iter_to_sg syzbot
2023-06-14 15:51 ` David Howells
2023-06-14 23:17   ` syzbot
2023-06-15 12:07 ` David Howells
2023-06-15 15:06   ` syzbot
2023-06-15 14:21 ` David Howells
2023-06-15 16:01 ` David Howells
2023-06-15 16:29   ` syzbot
2023-06-15 20:24 ` David Howells
2023-06-15 20:24   ` David Howells
2023-06-15 21:02   ` syzbot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.