Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: potential integer overflow in usbg_make_tpg()

The variable tpgt in usbg_make_tpg() is defined as unsigned long and is
assigned to tpgt->tport_tpgt, which is defined as u16. This may cause an
integer overflow when tpgt is greater than USHRT_MAX (65535). I
haven't tried to trigger it myself, but it is possible to trigger it
by calling usbg_make_tpg() with a large value for tpgt.

I modified the type of tpgt to match tpgt->tport_tpgt and adjusted the
relevant code accordingly.

This patch is similar to commit 59c816c1f24d ("vhost/scsi: potential
memory corruption").

Signed-off-by: Chen Yufeng <chenyufeng@iie.ac.cn>
Link: https://lore.kernel.org/r/20250415065857.1619-1-chenyufeng@iie.ac.cn
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Chen Yufeng and committed by
Greg Kroah-Hartman
15387401 1881a32f

+2 -2
+2 -2
drivers/usb/gadget/function/f_tcm.c
··· 1641 1641 struct usbg_tport *tport = container_of(wwn, struct usbg_tport, 1642 1642 tport_wwn); 1643 1643 struct usbg_tpg *tpg; 1644 - unsigned long tpgt; 1644 + u16 tpgt; 1645 1645 int ret; 1646 1646 struct f_tcm_opts *opts; 1647 1647 unsigned i; 1648 1648 1649 1649 if (strstr(name, "tpgt_") != name) 1650 1650 return ERR_PTR(-EINVAL); 1651 - if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX) 1651 + if (kstrtou16(name + 5, 0, &tpgt)) 1652 1652 return ERR_PTR(-EINVAL); 1653 1653 ret = -ENODEV; 1654 1654 mutex_lock(&tpg_instances_lock);