Skip to content

Commit cb3871b

Browse files
keesVudentz
authored andcommitted
Bluetooth: hci_sock: Correctly bounds check and pad HCI_MON_NEW_INDEX name
The code pattern of memcpy(dst, src, strlen(src)) is almost always wrong. In this case it is wrong because it leaves memory uninitialized if it is less than sizeof(ni->name), and overflows ni->name when longer. Normally strtomem_pad() could be used here, but since ni->name is a trailing array in struct hci_mon_new_index, compilers that don't support -fstrict-flex-arrays=3 can't tell how large this array is via __builtin_object_size(). Instead, open-code the helper and use sizeof() since it will work correctly. Additionally mark ni->name as __nonstring since it appears to not be a %NUL terminated C string. Cc: Luiz Augusto von Dentz <[email protected]> Cc: Edward AD <[email protected]> Cc: Marcel Holtmann <[email protected]> Cc: Johan Hedberg <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Eric Dumazet <[email protected]> Cc: Jakub Kicinski <[email protected]> Cc: Paolo Abeni <[email protected]> Cc: [email protected] Cc: [email protected] Fixes: 18f547f ("Bluetooth: hci_sock: fix slab oob read in create_monitor_event") Link: https://lore.kernel.org/lkml/202310110908.F2639D3276@keescook/ Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 9d1a3c7 commit cb3871b

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

include/net/bluetooth/hci_mon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct hci_mon_new_index {
5656
__u8 type;
5757
__u8 bus;
5858
bdaddr_t bdaddr;
59-
char name[8];
59+
char name[8] __nonstring;
6060
} __packed;
6161
#define HCI_MON_NEW_INDEX_SIZE 16
6262

net/bluetooth/hci_sock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
488488
ni->type = hdev->dev_type;
489489
ni->bus = hdev->bus;
490490
bacpy(&ni->bdaddr, &hdev->bdaddr);
491-
memcpy(ni->name, hdev->name, strlen(hdev->name));
491+
memcpy_and_pad(ni->name, sizeof(ni->name), hdev->name,
492+
strnlen(hdev->name, sizeof(ni->name)), '\0');
492493

493494
opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
494495
break;

0 commit comments

Comments
 (0)