Thread View: muc.lists.freebsd.bugs
3 messages
3 total messages
Started by bugzilla-noreply
Mon, 16 Nov 2026 00:39
[Bug 300809] iwmbtfw(8), ng_ubt_intel(4): unchecked lengths from adaptor and firmware files; DDC records sent one byte short
Author: bugzilla-noreply
Date: Mon, 16 Nov 2026 00:39
Date: Mon, 16 Nov 2026 00:39
429 lines
19613 bytes
19613 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300809
Bug ID: 300809
Summary: iwmbtfw(8), ng_ubt_intel(4): unchecked lengths from
adaptor and firmware files; DDC records sent one
byte short
Product: Base System
Version: CURRENT
Hardware: Any
OS: Any
Status: New
Severity: Affects Some People
Priority: ---
Component: kern
Assignee: bugs@FreeBSD.org
Reporter: luke@foolishgames.com
While porting the 9260/AX2xx support in iwmbtfw(8) and ng_ubt_intel(4)
to MidnightBSD, an audit of the code found several places where length
fields from the USB adaptor, or from the .sfi/.ddc firmware files, are
used without being checked. There is also a long-standing off-by-one
in how DDC records are sent. The attached patch fixes all of them. It
applies to main at 472a735d09eb and builds cleanly.
(generated by claude opus 5.5)
1. ng_ubt_intel.c, ubt_intel_get_img_type(): event length not checked
datalen = evt->header.length - UBT_HCI_EVENT_COMPL_HEAD_SIZE;
header.length is the length byte sent by the device. If it is
below 4, the uint8_t datalen wraps (datalen-- after the status byte
also wraps if it is exactly 3). If it is 255, the TLV loop reads
past the 256-byte malloc'ed buffer. This code runs in the kernel at
probe time, so it is an out-of-bounds read of kernel heap driven by
a USB device.
2. iwmbt_hw.c, iwmbt_get_version_tlv(): the same pattern in userland
The length is computed from event->header.length without comparing
it with the number of bytes actually transferred. A short or
oversized value wraps datalen, or reads past the 255-byte stack
buffer.
3. iwmbt_hw.c, iwmbt_get_version_tlv(): TLV lengths checked only by assert()
The Makefile builds with -DNDEBUG, so every assert(len == N) is
compiled out. A record with an unexpected length then makes
le32dec()/le16dec() read past the record, and possibly past the
buffer. IWMBT_TLV_OTP_BDADDR has no length check at all before
memcpy()ing 6 bytes. The function also starts by memcpy()ing a
10-byte struct iwmbt_version into the unrelated struct
iwmbt_version_tlv, so any TLV the device does not send is left
holding unrelated bytes.
Fix: skip records of unexpected length instead of asserting, check
the OTP_BDADDR length, and memset() the struct first.
4. iwmbt_hw.c, iwmbt_hci_command(): the stray-event loop never times out
'now' is set once before the do/while loop and never updated inside
it, so the timespeccmp(&to, &now, >) exit condition stays true. A
device that keeps sending unrelated events (the case 624635e2ad29
was written to handle) keeps the tool looping forever. The loop also
reads header.event even when the transfer returned 0 bytes, which
leaves the buffer uninitialised in callers that do not memset it,
e.g. iwmbt_enter_manufacturer().
5. iwmbt_hw.c, iwmbt_load_fwfile(): over-read on a truncated .sfi
The loop only checks that 3 bytes (the command header) remain
before adding IWMBT_HCI_CMD_SIZE(cmd) to 'ready'. If the last
command is truncated, 'ready' runs past the end of the file. In
IWMBT_SEND_FRAGMENT, XMIN(size, fw->len - sent) can then go
negative, and it is passed to iwmbt_send_fragment() as a uint8_t
length, so memcpy() reads past the firmware buffer. The 0xfc0e
(Intel Reset parameter) parsing also does le32dec(cmd->data)
without checking that cmd->length >= 4.
6. iwmbt_hw.c, iwmbt_load_ddc(): stack overflow and wrapped length
A record length byte of 255 makes cmd->length = 256, which wraps
the uint8_t to 0. memcpy() then copies up to 255 bytes to offset 3
of the 256-byte stack buffer, writing up to 2 bytes past its end.
Since 'size' and 'sent' advance by cmd->length, which is now 0, the
loop also sends the same record forever.
7. iwmbt_hw.c, iwmbt_load_ddc(): last byte of every DDC record sent as zero
cmd->length = ddc->buf[sent] + 1;
memcpy(cmd->data, ddc->buf + sent, XMIN(ddc->buf[sent], size));
A DDC record is a length byte followed by that many bytes, and Intel
Write DDC (0xfc8b) takes the whole record. The command length
covers len+1 bytes, but only len bytes are copied, so the last byte
of each record is sent as the 0 left by memset(). Linux
btintel_load_ddc_config() sends fw_ptr[0] + 1 bytes. This has been
present since iwmbtfw was added (7f32f0e2275b) and affects every
adaptor that loads a DDC file, not only the new ones.
8. main.c, iwmbt_init_firmware(): header_len uninitialised for variants
0x15/0x16
The if/else-if covers hw_variant <= 0x14 and >= 0x17 only. For 0x15
and 0x16, header_len is passed uninitialised to iwmbt_load_fwfile()
as the starting offset. Fail with an error for those variants.
9. iwmbt_fw.c, iwmbt_fw_read(): strdup() result not checked (minor)
Impact
------
Items 1-6 need a misbehaving or malicious USB device, or tampered
firmware files under /usr/local/share/iwmbt-firmware, which only root
can write. Item 1 is in the kernel and is reachable by plugging in a
device that uses Intel's USB IDs. Item 7 is a functional bug that
affects all Intel adaptors that use .ddc files.
Testing
-------
- Tested on an Intel AX210 (8087:0032), with hw_variant 0x17 and ECDSA
secure boot (sbe_type 0x01). Firmware ibt-0041-0041.sfi loads and
ibt-0041-0041.ddc gives "DDC download complete". ubt0 then attaches
through ng_ubt_intel, and hccontrol read_local_version_information
and read_bd_addr both answer.
- All 48 .sfi and .ddc files in comms/iwmbt-firmware 20230625 pass the
new checks, verified offline by walking each .sfi command stream and
each .ddc record chain. So the new checks reject none of the
shipped firmware.
- Not tested: 7260/8260/9260/AX200 hardware. In particular, item 7
changes the bytes sent to those adaptors.
The patch keeps the existing style of the surrounding code. Item 7 is
a separate hunk (the memcpy() in iwmbt_load_ddc()), so it can be
reviewed or committed on its own.
Patch (against main 472a735d09eb)
---------------------------------
diff -ruN upstream.orig/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_intel.c
upstream/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_intel.c
--- a/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_intel.c 2026-11-15
19:34:09.522687000 -0500
+++ b/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_intel.c 2026-11-15
19:34:17.826960000 -0500
@@ -158,6 +158,12 @@
if (error != USB_ERR_NORMAL_COMPLETION)
goto exit;
+ /* Reject lengths that would underflow or run past the buffer */
+ if (evt->header.length < UBT_HCI_EVENT_COMPL_HEAD_SIZE + 1 ||
+ evt->header.length >
+ UBT_INTEL_MAX_EVT_SIZE - sizeof(struct ubt_hci_evhdr))
+ goto exit;
+
datalen = evt->header.length - UBT_HCI_EVENT_COMPL_HEAD_SIZE;
data = evt->data;
status = *data++;
diff -ruN upstream.orig/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c
upstream/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c
--- a/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c 2026-11-15 19:34:09.522371000
-0500
+++ b/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c 2026-11-15 19:34:17.827120000
-0500
@@ -92,6 +92,12 @@
memset(fw, 0, sizeof(*fw));
fw->fwname = strdup(fwname);
+ if (fw->fwname == NULL) {
+ warn("%s: strdup", __func__);
+ free(buf);
+ close(fd);
+ return (0);
+ }
fw->len = sb.st_size;
fw->buf = buf;
diff -ruN upstream.orig/usr.sbin/bluetooth/iwmbtfw/iwmbt_hw.c
upstream/usr.sbin/bluetooth/iwmbtfw/iwmbt_hw.c
--- a/usr.sbin/bluetooth/iwmbtfw/iwmbt_hw.c 2026-11-15 19:34:09.522120000
-0500
+++ b/usr.sbin/bluetooth/iwmbtfw/iwmbt_hw.c 2026-11-15 19:34:17.827385000
-0500
@@ -30,7 +30,6 @@
#include <sys/endian.h>
#include <sys/stat.h>
-#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stddef.h>
@@ -135,6 +134,9 @@
return (ret);
}
+ if (*transferred < (int)sizeof(struct iwmbt_hci_evhdr))
+ goto next;
+
switch (((struct iwmbt_hci_event *)event)->header.event) {
case NG_HCI_EVENT_COMMAND_COMPL:
if (*transferred <
@@ -151,6 +153,8 @@
}
iwmbt_debug("Stray HCI event: %x",
((struct iwmbt_hci_event *)event)->header.event);
+next:
+ clock_gettime(CLOCK_MONOTONIC, &now);
} while (timespeccmp(&to, &now, >));
iwmbt_err("libusb_interrupt_transfer() failed: err=%s",
@@ -366,8 +370,14 @@
*/
while (fw->len - sent - ready >= (int) sizeof(struct iwmbt_hci_cmd)) {
cmd = (struct iwmbt_hci_cmd *)(fw->buf + sent + ready);
+ if ((int)IWMBT_HCI_CMD_SIZE(cmd) > fw->len - sent - ready) {
+ iwmbt_err("Invalid firmware, truncated HCI command "
+ "at offset %d", sent + ready);
+ return (-1);
+ }
/* Parse firmware for Intel Reset HCI command parameter */
- if (cmd->opcode == htole16(0xfc0e)) {
+ if (cmd->opcode == htole16(0xfc0e) &&
+ cmd->length >= sizeof(uint32_t)) {
*boot_param = le32dec(cmd->data);
iwmbt_debug("boot_param=0x%08x", *boot_param);
}
@@ -533,8 +543,17 @@
}
event = (struct iwmbt_hci_event_cmd_compl *)buf;
- memcpy(version, event->data, sizeof(struct iwmbt_version));
+ memset(version, 0, sizeof(*version));
+ /* Reject lengths that would underflow or run past received data */
+ if (event->header.length < IWMBT_HCI_EVENT_COMPL_HEAD_SIZE + 1 ||
+ event->header.length + sizeof(struct iwmbt_hci_evhdr) >
+ (size_t)transferred) {
+ iwmbt_debug("Invalid version event length %d",
+ event->header.length);
+ return (-1);
+ }
+
datalen = event->header.length - IWMBT_HCI_EVENT_COMPL_HEAD_SIZE;
data = event->data;
status = *data++;
@@ -552,75 +571,93 @@
switch (type) {
case IWMBT_TLV_CNVI_TOP:
- assert(len == 4);
+ if (len != 4)
+ break;
version->cnvi_top = le32dec(data);
break;
case IWMBT_TLV_CNVR_TOP:
- assert(len == 4);
+ if (len != 4)
+ break;
version->cnvr_top = le32dec(data);
break;
case IWMBT_TLV_CNVI_BT:
- assert(len == 4);
+ if (len != 4)
+ break;
version->cnvi_bt = le32dec(data);
break;
case IWMBT_TLV_CNVR_BT:
- assert(len == 4);
+ if (len != 4)
+ break;
version->cnvr_bt = le32dec(data);
break;
case IWMBT_TLV_DEV_REV_ID:
- assert(len == 2);
+ if (len != 2)
+ break;
version->dev_rev_id = le16dec(data);
break;
case IWMBT_TLV_IMAGE_TYPE:
- assert(len == 1);
+ if (len != 1)
+ break;
version->img_type = *data;
break;
case IWMBT_TLV_TIME_STAMP:
- assert(len == 2);
+ if (len != 2)
+ break;
version->min_fw_build_cw = data[0];
version->min_fw_build_yy = data[1];
version->timestamp = le16dec(data);
break;
case IWMBT_TLV_BUILD_TYPE:
- assert(len == 1);
+ if (len != 1)
+ break;
version->build_type = *data;
break;
case IWMBT_TLV_BUILD_NUM:
- assert(len == 4);
+ if (len != 4)
+ break;
version->min_fw_build_nn = *data;
version->build_num = le32dec(data);
break;
case IWMBT_TLV_SECURE_BOOT:
- assert(len == 1);
+ if (len != 1)
+ break;
version->secure_boot = *data;
break;
case IWMBT_TLV_OTP_LOCK:
- assert(len == 1);
+ if (len != 1)
+ break;
version->otp_lock = *data;
break;
case IWMBT_TLV_API_LOCK:
- assert(len == 1);
+ if (len != 1)
+ break;
version->api_lock = *data;
break;
case IWMBT_TLV_DEBUG_LOCK:
- assert(len == 1);
+ if (len != 1)
+ break;
version->debug_lock = *data;
break;
case IWMBT_TLV_MIN_FW:
- assert(len == 3);
+ if (len != 3)
+ break;
version->min_fw_build_nn = data[0];
version->min_fw_build_cw = data[1];
version->min_fw_build_yy = data[2];
break;
case IWMBT_TLV_LIMITED_CCE:
- assert(len == 1);
+ if (len != 1)
+ break;
version->limited_cce = *data;
break;
case IWMBT_TLV_SBE_TYPE:
- assert(len == 1);
+ if (len != 1)
+ break;
version->sbe_type = *data;
break;
case IWMBT_TLV_OTP_BDADDR:
+ if (len != sizeof(bdaddr_t))
+ break;
memcpy(&version->otp_bd_addr, data, sizeof(bdaddr_t));
break;
default:
@@ -712,7 +749,7 @@
iwmbt_load_ddc(struct libusb_device_handle *hdl,
const struct iwmbt_firmware *ddc)
{
- int size, sent = 0;
+ int plen, size, sent = 0;
int ret, transferred;
uint8_t buf[IWMBT_HCI_MAX_CMD_SIZE];
uint8_t evt[IWMBT_HCI_MAX_CMD_SIZE];
@@ -724,10 +761,18 @@
while (size > 0) {
+ /* A DDC record is a length byte followed by that many bytes */
+ plen = ddc->buf[sent] + 1;
+ if (plen > size || plen > (int)(sizeof(buf) -
+ offsetof(struct iwmbt_hci_cmd, data))) {
+ iwmbt_err("Invalid DDC record at offset %d", sent);
+ return (-1);
+ }
+
memset(buf, 0, sizeof(buf));
cmd->opcode = htole16(0xfc8b);
- cmd->length = ddc->buf[sent] + 1;
- memcpy(cmd->data, ddc->buf + sent, XMIN(ddc->buf[sent], size));
+ cmd->length = plen;
+ memcpy(cmd->data, ddc->buf + sent, plen);
iwmbt_debug("transferring %d bytes, offset %d",
cmd->length,
diff -ruN upstream.orig/usr.sbin/bluetooth/iwmbtfw/main.c
upstream/usr.sbin/bluetooth/iwmbtfw/main.c
--- a/usr.sbin/bluetooth/iwmbtfw/main.c 2026-11-15 19:34:09.522171000 -0500
+++ b/usr.sbin/bluetooth/iwmbtfw/main.c 2026-11-15 19:34:17.827579000 -0500
@@ -287,6 +287,7 @@
/* Check if CSS header for ECDSA follows the RSA header */
if (fw.buf[ECDSA_OFFSET] != 0x06) {
+ iwmbt_err("Invalid ECDSA CSS header");
ret = -1;
goto exit;
}
@@ -297,6 +298,10 @@
ret = -1;
goto exit;
}
+ } else {
+ iwmbt_err("Unsupported hardware variant (%d)", hw_variant);
+ ret = -1;
+ goto exit;
}
/* Load in the CSS header */
--
You are receiving this mail because:
You are the assignee for the bug.
--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
[Bug 300809] iwmbtfw(8), ng_ubt_intel(4): unchecked lengths from adaptor and firmware files; DDC records sent one byte short
Author: bugzilla-noreply
Date: Mon, 16 Nov 2026 02:55
Date: Mon, 16 Nov 2026 02:55
13 lines
2469 bytes
2469 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300809 --- Comment #1 from Lucas Holt <luke@foolishgames.com> --- There appears to be similar issues in the realtek bluetooth code also -- You are receiving this mail because: You are the assignee for the bug. -- Posted automagically by a mail2news gateway at muc.de e.V. Please direct questions, flames, donations, etc. to news-admin@muc.de
[Bug 300809] iwmbtfw(8), ng_ubt_intel(4): unchecked lengths from adaptor and firmware files; DDC records sent one byte short
Author: bugzilla-noreply
Date: Mon, 16 Nov 2026 08:13
Date: Mon, 16 Nov 2026 08:13
29 lines
3015 bytes
3015 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300809 --- Comment #2 from Mark Linimon <linimon@FreeBSD.org> --- Created attachment 277348 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=277348&action=edit length fields from the USB adaptor, or from the .sfi/.ddc firmware files, are used without being checked ^Triage: convert to Attachment, as we now prefer. *note*: the patch to usr.sbin/bluetooth/iwmbtfw/iwmbt_hw.c does not come even _close_ to applying, to the point where in a few minutes I was not able to figure it out. I have included it in this patch as converted anyway. All of the other patches do apply to head as of 20261115. iwmbt_hw.c will simply have to be reworked. -- You are receiving this mail because: You are the assignee for the bug. -- Posted automagically by a mail2news gateway at muc.de e.V. Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads