Skip to content

Commit 8479891

Browse files
pip-izonygregkh
authored andcommitted
usb: gadget: f_mass_storage: Fix potential integer overflow in check_command_size_in_blocks()
The `check_command_size_in_blocks()` function calculates the data size in bytes by left shifting `common->data_size_from_cmnd` by the block size (`common->curlun->blkbits`). However, it does not validate whether this shift operation will cause an integer overflow. Initially, the block size is set up in `fsg_lun_open()` , and the `common->data_size_from_cmnd` is set up in `do_scsi_command()`. During initialization, there is no integer overflow check for the interaction between two variables. So if a malicious USB host sends a SCSI READ or WRITE command requesting a large amount of data (`common->data_size_from_cmnd`), the left shift operation can wrap around. This results in a truncated data size, which can bypass boundary checks and potentially lead to memory corruption or out-of-bounds accesses. Fix this by using the check_shl_overflow() macro to safely perform the shift and catch any overflows. Fixes: 144974e ("usb: gadget: mass_storage: support multi-luns with different logic block size") Signed-off-by: Seungjin Bae <eeodqql09@gmail.com> Reviewed-by: Alan Stern <stern@rowland.harvard.edu> Link: https://patch.msgid.link/20260228104324.1696455-2-eeodqql09@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7f58b41 commit 8479891

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

drivers/usb/gadget/function/f_mass_storage.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
#include <linux/kthread.h>
181181
#include <linux/sched/signal.h>
182182
#include <linux/limits.h>
183+
#include <linux/overflow.h>
183184
#include <linux/pagemap.h>
184185
#include <linux/rwsem.h>
185186
#include <linux/slab.h>
@@ -1853,8 +1854,15 @@ static int check_command_size_in_blocks(struct fsg_common *common,
18531854
int cmnd_size, enum data_direction data_dir,
18541855
unsigned int mask, int needs_medium, const char *name)
18551856
{
1856-
if (common->curlun)
1857-
common->data_size_from_cmnd <<= common->curlun->blkbits;
1857+
if (common->curlun) {
1858+
if (check_shl_overflow(common->data_size_from_cmnd,
1859+
common->curlun->blkbits,
1860+
&common->data_size_from_cmnd)) {
1861+
common->phase_error = 1;
1862+
return -EINVAL;
1863+
}
1864+
}
1865+
18581866
return check_command(common, cmnd_size, data_dir,
18591867
mask, needs_medium, name);
18601868
}

0 commit comments

Comments
 (0)