[PATCH] usb: gadget: serial: reject port counts exceeding the array size

From: Ju Nan

Date: Wed Nov 18 2026 - 04:45:33 EST


The n_ports module parameter has no upper bound check, while fi_serial
and f_serial each contain only MAX_U_SERIAL_PORTS entries.

Reject n_ports values greater than MAX_U_SERIAL_PORTS in gserial_init(),
before registering the composite driver.

Signed-off-by: Ju Nan <junan76@xxxxxxx>
---
drivers/usb/gadget/legacy/serial.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/usb/gadget/legacy/serial.c b/drivers/usb/gadget/legacy/serial.c
index 4974bee6049a..20e89f052952 100644
--- a/drivers/usb/gadget/legacy/serial.c
+++ b/drivers/usb/gadget/legacy/serial.c
@@ -286,6 +286,9 @@ static int switch_gserial_enable(bool do_enable)

static int __init gserial_init(void)
{
+ if (n_ports > MAX_U_SERIAL_PORTS)
+ return -EINVAL;
+
/* We *could* export two configs; that'd be much cleaner...
* but neither of these product IDs was defined that way.
*/
--
2.56.1