Skip to content

Commit 1d22a12

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
can: ucan: fix out of bound read in strscpy() source
Commit 7fdaf89 ("can: ucan: use strscpy() to instead of strncpy()") unintentionally introduced a one byte out of bound read on strscpy()'s source argument (which is kind of ironic knowing that strscpy() is meant to be a more secure alternative :)). Let's consider below buffers: dest[len + 1]; /* will be NUL terminated */ src[len]; /* may not be NUL terminated */ When doing: strncpy(dest, src, len); dest[len] = '\0'; strncpy() will read up to len bytes from src. On the other hand: strscpy(dest, src, len + 1); will read up to len + 1 bytes from src, that is to say, an out of bound read of one byte will occur on src if it is not NUL terminated. Note that the src[len] byte is never copied, but strscpy() still needs to read it to check whether a truncation occurred or not. This exact pattern happened in ucan. The root cause is that the source is not NUL terminated. Instead of doing a copy in a local buffer, directly NUL terminate it as soon as usb_control_msg() returns. With this, the local firmware_str[] variable can be removed. On top of this do a couple refactors: - ucan_ctl_payload->raw is only used for the firmware string, so rename it to ucan_ctl_payload->fw_str and change its type from u8 to char. - ucan_device_request_in() is only used to retrieve the firmware string, so rename it to ucan_get_fw_str() and refactor it to make it directly handle all the string termination logic. Reported-by: [email protected] Closes: https://lore.kernel.org/linux-can/[email protected]/ Fixes: 7fdaf89 ("can: ucan: use strscpy() to instead of strncpy()") Signed-off-by: Vincent Mailhol <[email protected]> Link: https://patch.msgid.link/[email protected] Cc: [email protected] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 4003c9e commit 1d22a12

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

drivers/net/can/usb/ucan.c

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ union ucan_ctl_payload {
186186
*/
187187
struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
188188

189-
u8 raw[128];
189+
u8 fw_str[128];
190190
} __packed;
191191

192192
enum {
@@ -424,18 +424,20 @@ static int ucan_ctrl_command_out(struct ucan_priv *up,
424424
UCAN_USB_CTL_PIPE_TIMEOUT);
425425
}
426426

427-
static int ucan_device_request_in(struct ucan_priv *up,
428-
u8 cmd, u16 subcmd, u16 datalen)
427+
static void ucan_get_fw_str(struct ucan_priv *up, char *fw_str, size_t size)
429428
{
430-
return usb_control_msg(up->udev,
431-
usb_rcvctrlpipe(up->udev, 0),
432-
cmd,
433-
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
434-
subcmd,
435-
0,
436-
up->ctl_msg_buffer,
437-
datalen,
438-
UCAN_USB_CTL_PIPE_TIMEOUT);
429+
int ret;
430+
431+
ret = usb_control_msg(up->udev, usb_rcvctrlpipe(up->udev, 0),
432+
UCAN_DEVICE_GET_FW_STRING,
433+
USB_DIR_IN | USB_TYPE_VENDOR |
434+
USB_RECIP_DEVICE,
435+
0, 0, fw_str, size - 1,
436+
UCAN_USB_CTL_PIPE_TIMEOUT);
437+
if (ret > 0)
438+
fw_str[ret] = '\0';
439+
else
440+
strscpy(fw_str, "unknown", size);
439441
}
440442

441443
/* Parse the device information structure reported by the device and
@@ -1314,7 +1316,6 @@ static int ucan_probe(struct usb_interface *intf,
13141316
u8 in_ep_addr;
13151317
u8 out_ep_addr;
13161318
union ucan_ctl_payload *ctl_msg_buffer;
1317-
char firmware_str[sizeof(union ucan_ctl_payload) + 1];
13181319

13191320
udev = interface_to_usbdev(intf);
13201321

@@ -1527,17 +1528,6 @@ static int ucan_probe(struct usb_interface *intf,
15271528
*/
15281529
ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
15291530

1530-
/* just print some device information - if available */
1531-
ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
1532-
sizeof(union ucan_ctl_payload));
1533-
if (ret > 0) {
1534-
/* copy string while ensuring zero termination */
1535-
strscpy(firmware_str, up->ctl_msg_buffer->raw,
1536-
sizeof(union ucan_ctl_payload) + 1);
1537-
} else {
1538-
strcpy(firmware_str, "unknown");
1539-
}
1540-
15411531
/* device is compatible, reset it */
15421532
ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
15431533
if (ret < 0)
@@ -1555,7 +1545,10 @@ static int ucan_probe(struct usb_interface *intf,
15551545

15561546
/* initialisation complete, log device info */
15571547
netdev_info(up->netdev, "registered device\n");
1558-
netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
1548+
ucan_get_fw_str(up, up->ctl_msg_buffer->fw_str,
1549+
sizeof(up->ctl_msg_buffer->fw_str));
1550+
netdev_info(up->netdev, "firmware string: %s\n",
1551+
up->ctl_msg_buffer->fw_str);
15591552

15601553
/* success */
15611554
return 0;

0 commit comments

Comments
 (0)