Skip to content

Commit 3b14268

Browse files
tmon-nordicnashif
authored andcommitted
drivers: usb_dc_native_posix: Check data length before copy
Fail requests if the data does not fit inside buffer. This commit only adds missing sanity checks but the native posix driver remains broken at the design level. The amount of work to fix the native posix driver in legacy USB stack is deemed too great to be worth it. Coverity-CID: 195841, GitHub issue #58564 Coverity-CID: 240244, GitHub issue #58570 Signed-off-by: Tomasz Moń <[email protected]>
1 parent fb40807 commit 3b14268

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

drivers/usb/device/usb_dc_native_posix.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
356356
uint8_t ep_idx = USB_EP_GET_IDX(ep);
357357
struct usb_ep_ctrl_prv *ctrl = &usbip_ctrl.in_ep_ctrl[ep_idx];
358358

359+
if (data_len > ARRAY_SIZE(ctrl->buf)) {
360+
return -EINVAL;
361+
}
362+
359363
memcpy(ctrl->buf, data, data_len);
360364
ctrl->buf_len = data_len;
361365
}
@@ -525,8 +529,15 @@ int handle_usb_control(struct usbip_header *hdr)
525529
ep_ctrl->cb(ep_idx, USB_DC_EP_SETUP);
526530

527531
if (ntohl(hdr->common.direction) == USBIP_DIR_OUT) {
532+
uint32_t data_len = ntohl(hdr->u.submit.transfer_buffer_length);
533+
528534
/* Data OUT stage availably */
529-
ep_ctrl->data_len = ntohl(hdr->u.submit.transfer_buffer_length);
535+
if (data_len > ARRAY_SIZE(ep_ctrl->buf)) {
536+
return -EIO;
537+
}
538+
539+
ep_ctrl->data_len = data_len;
540+
530541
if (usbip_recv(ep_ctrl->buf, ep_ctrl->data_len) < 0) {
531542
return -EIO;
532543
}
@@ -546,13 +557,22 @@ int handle_usb_data(struct usbip_header *hdr)
546557
uint8_t ep;
547558

548559
if (ntohl(hdr->common.direction) == USBIP_DIR_OUT) {
560+
uint32_t data_len;
561+
549562
if (ep_idx >= USBIP_OUT_EP_NUM) {
550563
return -EINVAL;
551564
}
552565

553566
ep_ctrl = &usbip_ctrl.out_ep_ctrl[ep_idx];
554567
ep = ep_idx | USB_EP_DIR_OUT;
555-
ep_ctrl->data_len = ntohl(hdr->u.submit.transfer_buffer_length);
568+
data_len = ntohl(hdr->u.submit.transfer_buffer_length);
569+
570+
if (data_len > ARRAY_SIZE(ep_ctrl->buf)) {
571+
return -EIO;
572+
}
573+
574+
ep_ctrl->data_len = data_len;
575+
556576
if (usbip_recv(ep_ctrl->buf, ep_ctrl->data_len) < 0) {
557577
return -EIO;
558578
}

0 commit comments

Comments
 (0)