Skip to content

bluetooth: hci_raw: avoid possible memory overflow in bt_buf_get_tx() #42093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions subsys/bluetooth/host/hci_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
bt_buf_set_type(buf, type);

if (data && size) {
if (net_buf_tailroom(buf) < size) {
net_buf_unref(buf);
return NULL;
}

net_buf_add_mem(buf, data, size);
}

Expand Down
17 changes: 17 additions & 0 deletions subsys/usb/class/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,21 @@ static void acl_read_cb(uint8_t ep, int size, void *priv)
if (IS_ENABLED(CONFIG_USB_DEVICE_BLUETOOTH_VS_H4) &&
bt_hci_raw_get_mode() == BT_HCI_RAW_MODE_H4) {
buf = bt_buf_get_tx(BT_BUF_H4, K_FOREVER, data, size);
if (!buf) {
LOG_ERR("Failed to allocate buffer");
goto restart_out_transfer;
}

pkt_len = hci_pkt_get_len(buf, &data[1], size - 1);
LOG_DBG("pkt_len %u, chunk %u", pkt_len, size);
} else {
buf = bt_buf_get_tx(BT_BUF_ACL_OUT, K_FOREVER,
data, size);
if (!buf) {
LOG_ERR("Failed to allocate buffer");
goto restart_out_transfer;
}

pkt_len = hci_pkt_get_len(buf, data, size);
LOG_DBG("pkt_len %u, chunk %u", pkt_len, size);
}
Expand All @@ -263,6 +273,13 @@ static void acl_read_cb(uint8_t ep, int size, void *priv)
buf = NULL;
}
} else {
if (net_buf_tailroom(buf) < size) {
LOG_ERR("Buffer tailroom too small");
net_buf_unref(buf);
buf = NULL;
goto restart_out_transfer;
}

/*
* Take over the next chunk if HCI packet is
* larger than USB_MAX_FS_BULK_MPS.
Expand Down