[lvc-project] [PATCH 5.10/6.1] scsi: sg: replace scatter_elem_sz value 0 with PAGE_SIZE

Tonofa Boris b.tonofa at ideco.ru
Tue Jul 29 03:13:27 MSK 2025


From: Edward Adam Davis <eadavis at qq.com>

syzbot reported a shift‑out‑of‑bounds in sg_build_indirect() [1].

The reproducer writes 0 to /sys/module/sg/parameters/scatter_elem_sz,
then opens “/dev/sg0”; on the sg_open() path we get:

    num   = scatter_elem_sz;          /* 0 */
    order = get_order(num);           /* 52 on x86‑64 */
    ret_sz = 1 << (PAGE_SHIFT + order);   /* 1 << 64 → UB */

The result of get_order() is undefined if the size is 0. This may lead to
invalid shift calculation later caught with UBSAN.

Replace the zero value with PAGE_SIZE before calling get_order().
No functional change for valid (non-zero) settings.

[1]
UBSAN: shift-out-of-bounds in drivers/scsi/sg.c:1897:13
shift exponent 64 is too large for 32-bit type 'int'
CPU: 1 UID: 0 PID: 5832 Comm: syz-executor361 Not tainted 6.14.0-rc2-syzkaller-00185-g128c8f96eb86 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 12/27/2024
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x16c/0x1f0 lib/dump_stack.c:120
 ubsan_epilogue lib/ubsan.c:231 [inline]
 __ubsan_handle_shift_out_of_bounds+0x24f/0x3f0 lib/ubsan.c:468
 sg_build_indirect.cold+0x1b/0x20 drivers/scsi/sg.c:1897
 sg_build_reserve+0xc4/0x180 drivers/scsi/sg.c:2007
 sg_add_sfp drivers/scsi/sg.c:2189 [inline]
 sg_open+0xc37/0x1910 drivers/scsi/sg.c:348
 chrdev_open+0x237/0x6a0 fs/char_dev.c:414
 do_dentry_open+0x735/0x1c40 fs/open.c:956
 vfs_open+0x82/0x3f0 fs/open.c:1086
 do_open fs/namei.c:3830 [inline]
 path_openat+0x1e88/0x2d80 fs/namei.c:3989
 do_filp_open+0x20c/0x470 fs/namei.c:4016
 do_sys_openat2+0x17a/0x1e0 fs/open.c:1428
 do_sys_open fs/open.c:1443 [inline]
 __do_sys_openat fs/open.c:1459 [inline]
 __se_sys_openat fs/open.c:1454 [inline]
 __x64_sys_openat+0x175/0x210 fs/open.c:1454
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported-by: syzbot+270f1c719ee7baab9941 at syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=270f1c719ee7baab9941
Signed-off-by: Tonofa Boris <b.tonofa at ideco.ru>
---
 drivers/scsi/sg.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 1946cc96c172..094dee57da3a 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -1883,6 +1883,8 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
 		if (num < PAGE_SIZE) {
 			scatter_elem_sz = PAGE_SIZE;
 			scatter_elem_sz_prev = PAGE_SIZE;
+			if (!num)
+				num = PAGE_SIZE;
 		} else
 			scatter_elem_sz_prev = num;
 	}
-- 
2.50.0





More information about the lvc-project mailing list