Skip to contentSkip to conversation

usb: gadget: ncm: validate NDP length against the NTB

PATCH New Aldo Ariel Panzardo 1 participant 1 message Sep 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: ncm: validate NDP length against the NTB

The NDP length is validated for its minimum size and alignment, but not against the containing NTB. A host can provide an NDP near the end of the block with an oversized length, causing the datagram parser to read beyond the block.

The NDP index has already been checked against block_len, so compare the length with the remaining bytes using an overflow-safe subtraction.

---
 drivers/usb/gadget/function/f_ncm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_ncm.c +2 -1
index 64eabda2f..1ff05eb0f 100644
@@ -1262,7 +1262,8 @@ static int ncm_unwrap_ntb(struct gether *port,
 		 */
 		if ((ndp_len < opts->ndp_size
 				+ 2 * 2 * (opts->dgram_item_len * 2)) ||
-				(ndp_len % opts->ndplen_align != 0)) {
+				(ndp_len % opts->ndplen_align != 0) ||
+				(ndp_len > block_len - ndp_index)) {
 			INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
 			     ndp_len);
 			goto err;
Signature · 1 line
2.43.0