Skip to contentSkip to conversation

scsi: sg: Fix shift-out-of-bounds in sg_build_indirect()

RFC PATCH syzbot 1 participant 1 message May 20

Subsystems (2)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

When the scatter_elem_sz module parameter is set to 0 (or a negative value), sg_build_indirect() attempts to clamp it to PAGE_SIZE. However, while the global variables are updated, the local variable num is not. This results in num remaining 0. Calling get_order(0) underflows and returns a very large order (e.g., 52). Subsequently, the expression 1 << (PAGE_SHIFT + order) evaluates to 1 << 64, which triggers a shift-out-of-bounds undefined behavior.

Fix this by updating the local variable num to PAGE_SIZE during the clamping process to ensure it is never 0 or negative when passed to get_order(). Additionally, replace the unsafe 1 << (PAGE_SHIFT + order) pattern with the safer and more idiomatic PAGE_SIZE << order throughout the file. This prevents any potential undefined behavior if PAGE_SHIFT + order equals or exceeds 31 (or 64 for 1UL), and ensures consistency across sg_build_indirect(), sg_vma_fault(), sg_mmap(), sg_read_oxfer(), and sg_link_reserve().

Fixes 10db10d144c0 ("sg: convert the indirect IO path to use the block layer")
Assisted-by Gemini:gemini-3.1-pro-preview
Reported-by [email protected]

To: "James E.J. Bottomley" <[email protected]>
To: "Doug Gilbert" <[email protected]>
To: <[email protected]>
To: "Martin K. Petersen" <[email protected]>
Cc: <[email protected]>

---

drivers/scsi/sg.c +6 -5
index 74cd4e8a6..cb4450a89 100644
@@ -1232,7 +1232,7 @@ sg_vma_fault(struct vm_fault *vmf)
 				      "sg_vma_fault: offset=%lu, scatg=%d\n",
 				      offset, rsv_schp->k_use_sg));
 	sa = vma->vm_start;
-	length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
+	length = PAGE_SIZE << rsv_schp->page_order;
 	for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
 		len = vma->vm_end - sa;
 		len = (len < length) ? len : length;
@@ -1278,7 +1278,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
 	}
 
 	sa = vma->vm_start;
-	length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
+	length = PAGE_SIZE << rsv_schp->page_order;
 	for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
 		len = vma->vm_end - sa;
 		len = (len < length) ? len : length;
@@ -1877,6 +1877,7 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
 	num = scatter_elem_sz;
 	if (unlikely(num != scatter_elem_sz_prev)) {
 		if (num < PAGE_SIZE) {
+			num = PAGE_SIZE;
 			scatter_elem_sz = PAGE_SIZE;
 			scatter_elem_sz_prev = PAGE_SIZE;
 		} else
@@ -1885,7 +1886,7 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
 
 	order = get_order(num);
 retry:
-	ret_sz = 1 << (PAGE_SHIFT + order);
+	ret_sz = PAGE_SIZE << order;
 
 	for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
 	     k++, rem_sz -= ret_sz) {
@@ -1964,7 +1965,7 @@ sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
 	if ((!outp) || (num_read_xfer <= 0))
 		return 0;
 
-	num = 1 << (PAGE_SHIFT + schp->page_order);
+	num = PAGE_SIZE << schp->page_order;
 	for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
 		if (num > num_read_xfer) {
 			if (copy_to_user(outp, page_address(schp->pages[k]),
@@ -2015,7 +2016,7 @@ sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
 			 "sg_link_reserve: size=%d\n", size));
 	rem = size;
 
-	num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
+	num = PAGE_SIZE << rsv_schp->page_order;
 	for (k = 0; k < rsv_schp->k_use_sg; k++) {
 		if (rem <= num) {
 			req_schp->k_use_sg = k + 1;

base-commit: 5200f5f493f79f14bbdc349e402a40dfb32f23c8

Signature · 5 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to send it to the mailing list.
Reply with '#syz reject' to reject it.

See https://github.com/google/syzkaller/blob/master/docs/syzbot_ai_patches.md for more information.