Repository navigation
thunderbolt: check DROM sizes, DP bandwidth request adapters and XDomain property blocks - #3209
Conversation
commit b946f490be576691c4c47998cf1d4e41d637fbfb upstream. tb_drom_parse_entries() checks for one byte remaining before reading a DROM entry header, but the header occupies two bytes. It also accepts a declared length of one byte. A one-byte tail consequently makes the parser read entry->len outside the DROM. A one-byte generic string entry reaches the subtype parser and underflows its payload-length calculation. Require a complete entry header before reading it and require the declared entry length to cover that header. Use subtraction-based bounds checks so the firmware-provided length cannot overflow the DROM extent calculation. While at it, use the actual const type instead of void * to make it clear we only read these. Signed-off-by: Pengpeng Hou <[email protected]> Signed-off-by: Mika Westerberg <[email protected]> Signed-off-by: Dj <[email protected]>
A router reports the size of its DROM, and tb_drom_parse() reads the revision and data length at bytes 13 to 15 before it has checked that the DROM is that long. The USB4 and NVM copies take any reported size, so a DROM of 13 bytes makes the parser read past its allocation. The version 1 parser also reads the vendor and model IDs at bytes 16 to 21, past the end of a DROM that only has a USB4 header. The generic product descriptor entry (index 9) is read for its vendor and product IDs without checking that the entry holds them, so a short entry at the end of the DROM reads past it too. Check the DROM against the USB4 header size before reading the header, and against the full header size before the version 1 parser reads it. Ignore a product descriptor that ends before its product ID. Signed-off-by: Dj <[email protected]>
A router's DP bandwidth notification names one of its adapters, and tb_handle_dp_bandwidth_request() indexes sw->ports[] with that number without checking it against the router's last adapter. The field is six bits wide, so a router with fewer adapters makes the handler use a port past the end of the array, and the warning for a non-DP IN adapter then reads through it. Reject the request if the router has no such adapter, as the hotplug handler already does. Fixes: 6ce3563 ("thunderbolt: Add support for DisplayPort bandwidth allocation mode") Signed-off-by: Dj <[email protected]>
tb_xdp_properties_request() takes the size of the remote properties block from the first response and keeps asking until it has all of it. If the remote host reports a block of zero dwords, nothing is copied, the offset stays at zero and the loop condition (!data_len) holds, so the same request goes out again for as long as the remote host answers. This runs on the domain's ordered workqueue, which handles nothing else meanwhile. An empty block cannot be parsed into properties anyway. Fail the request when the first response reports one, and loop only while data is missing. Each response then moves the offset forward: the length check already requires data words in every response, and the copy is clamped to what is left of the block. Fixes: d1ff702 ("thunderbolt: Add support for XDomain discovery protocol") Signed-off-by: Dj <[email protected]>
…d XDomain properties Cover the checks of the previous commits: - DROMs read through tb_drom_read() from the device property the Apple host path uses: entries of length 0 and 1, a header-only name, a single byte left at the end, an entry past the end, a product descriptor that ends inside its product ID, and a revision 1 DROM that is only as long as the USB4 header. - DP bandwidth notifications for adapters the host router does not have (max_port_number + 1, 63), for its control, lane and NHI adapters, and for a DP IN adapter with no tunnel: none is tunneled and the router reference stays balanced. - XDomain properties requests answered through a static stub on tb_cfg_request_sync(): an empty block fails after one request, and a five dword block arrives in two responses, the second clamped. The stub is a no-op unless a KUnit test activates it, as the one in tb_cfg_read() is. Signed-off-by: Dj <[email protected]>
|
Merge readiness at
Areas: none matched in CODEOWNERS Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
Path discovery follows a path through the routers by reading each hop entry from the router and taking the adapter the entry sends the path out of from its six-bit out_port field. tb_path_find_dst_port() and both passes of tb_path_discover() index sw->ports[] with that field without checking it against the router's last adapter, so a router that reports an adapter it does not have makes discovery use a port past the end of the array. The second pass reads the entries again and can get a different value than the first one checked. Look the adapter up through one helper that refuses an out_port past max_port_number with a rate-limited warning, and fail discovery of that path. The second pass checks again before it allocates any HopID. Add a KUnit test that discovers paths from a router stub reporting adapter max_port_number + 1 and 63, a valid adapter on the first read and an invalid one on the second, and the same for the source HopID lookup, and checks that no HopID stays allocated. Signed-off-by: Dj <[email protected]>
|
Verdict at Blockers: none. My first-pass blocker (a crash when the second read of path discovery ends the path early) is fixed: Checked
Evidence
|
|
Final row at How it was run: one guarded one-shot of main
The Mac went back to the stock kernel, and the kit was removed from it. Earlier, at J773g (M4 Mac mini) at How it was run: two guarded one-shots on the default cmdline, the same day on the same Mac. Both returned to the stock kernel afterwards, and the kits were removed from the Mac.
No USB4/Thunderbolt device is attached to this Mac, so this is a smoke check: no new kernel lines, PCIe and Wi-Fi unaffected, and one s2idle cycle survived. It doesn't exercise a dock.
The only extra err-level kernel lines on the candidate are the scaler module A/B's two job timeouts (#3217). They aren't TB/PCI lines. |
tb_path_discover() counts a path's hops in a first pass and reads the hop entries again in a second one, following each out adapter's link to the next router. On the second read a router can name another adapter that is in range but has no link. The next adapter is then NULL, and the following iteration reads its hop entry through it. Fail discovery when the second pass runs out of adapters before the hops counted in the first one. The error path gives back the HopIDs already allocated for the earlier hops. Extend the KUnit test with a path through a device router that, read again, leaves the host through its unlinked adapter 3 instead. Signed-off-by: DjDeveloperr <[email protected]>
tb_property_parse_dir() reads the root directory's magic and length, the first two dwords of the block, before it looks at the block's length. An XDomain peer chooses that length, and the properties request allocates exactly the dwords it reports, so a one-dword block holding the root magic makes the parser read past the allocation. Refuse a block shorter than the root directory header, and add a KUnit test for blocks of zero and one dword. Signed-off-by: DjDeveloperr <[email protected]>
|
Reviewed at Two small issues in
(Minor pre-existing note in |
|
J714s (MacBook Pro 14" M5 Pro, T6050) row at
|
There was a problem hiding this comment.
Verdict: FINISH, then merge at c03c53f3
Blockers:
drivers/thunderbolt/path.c:130:tb_path_hop_out_port()checkshop->out_port > sw->config.max_port_numberbut allowshop->out_port == 0(the control adapter, never a valid egress adapter for a data path hop).tb_switch_alloc()skipsida_init()onsw->ports[0], so withalloc_hopid == true,tb_port_alloc_out_hopid(&sw->ports[0], ...)touches an uninitializedida; withalloc_hopid == false,tb_path_discover()can return&sw->ports[0]asdst_port. Rejecthop->out_port == 0(if (!hop->out_port || hop->out_port > sw->config.max_port_number)) and add a KUnit case forout_port == 0.drivers/thunderbolt/path.c:204-213: in the second pass oftb_path_discover(), re-checkif (!hop.enable) goto err;aftertb_port_read()so a hop disabled between the two passes does not allocate HopIDs or return an active path.- Rerun the 18 cancelled CI checks once the above fixes are pushed.
Checked:
drivers/thunderbolt/eeprom.c: DROM entry header/length bounds, product descriptoroffsetofend(..., idProduct)check, and 16-byte USB4 / 22-byte v1 header checks are solid.drivers/thunderbolt/tb.c:3664: DP bandwidth requestev->port > sw->config.max_port_numbercheck properly drops the router reference viagoto put_sw.drivers/thunderbolt/property.c:249andxdomain.c: root directory header length check and zero-length XDomain properties check are solid.
Evidence
- Diff reviewed at
c03c53f3:ctl.c,eeprom.c,path.c,property.c,tb.c,tb.h,test.c,xdomain.c. - Matches the two
path.cbugs also identified in earlier review comments atc03c53f3.
tb_path_hop_out_port() accepted out_port 0, the control adapter. No tunnel path leaves through it, and its port has no HopID ida set up, so discovery could allocate HopIDs on an uninitialised ida or return the control adapter as the path end. Refuse it like a non-existent adapter. The second pass of tb_path_discover() re-reads each hop but did not check that it is still enabled, so a hop disabled between the passes was used as a live one. Fail discovery instead. Add KUnit cases for both. Signed-off-by: Ryan Murray <[email protected]>
|
Pushed 69399d9 on top with the two path.c fixes from the review: tb_path_hop_out_port() now refuses out_port 0 (the control adapter has no HopID ida and never carries a tunnel), and the second pass of tb_path_discover() fails if a hop was disabled since the first pass. |
There was a problem hiding this comment.
Verdict: HOLD at 69399d99 — code is clean and both drivers/thunderbolt/path.c blockers from c03c53f3 are resolved; awaiting exact-head CI and the docked hardware row at 69399d99.
Blockers:
- Exact-head CI and one docked USB4 hardware row at
69399d99(J714s or J773g with USB4 dock attach/detach, DP tunnel, dock I/O, and deep sleep, plus an XDomain peer if available) have not been posted yet.
Checked: Full 8-file diff (drivers/thunderbolt/{ctl.c,eeprom.c,path.c,property.c,tb.c,tb.h,test.c,xdomain.c}), delta commit 69399d99e5, HopID cleanup ordering in tb_path_discover(), and the two new KUnit cases (tb_test_path_discover_out_port_zero, tb_test_path_discover_hop_disabled).
Evidence
- Prior
path.cblockers resolved (69399d99e5):drivers/thunderbolt/path.c:131:tb_path_hop_out_port()now rejects!hop->out_port || hop->out_port > sw->config.max_port_number, preventing adapter 0 (control adapter, uninitialized HopIDida) from ever being used or returned asdst_port.drivers/thunderbolt/path.c:211-213: the second pass oftb_path_discover()now checksif (!hop.enable) goto err;before looking upout_portor allocating HopIDs, rolling back any HopIDs allocated on earlier hops.drivers/thunderbolt/test.c:744-783: addstb_test_path_discover_out_port_zero(first-read and second-read adapter 0) andtb_test_path_discover_hop_disabled(hop disabled between passes), both verifying no HopIDs leak (83/83thunderboltKUnit pass reported locally in QEMU).
- Earlier hardening commits intact: DROM header/entry/product-descriptor bounds (
eeprom.c), DP bandwidth request adapter bounds (tb.c), XDomain empty properties block rejection (xdomain.c), and rootdir header length guard (property.c) are unchanged and clean. All commits carry valid humanSigned-off-bylines. - Remaining merge gate: Once CI passes and the exact-head docked hardware row lands at
69399d99, this is ready to approve and merge.
Signed-off-by: Ryan Murray <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
|
I merged current |
There was a problem hiding this comment.
Verdict: MERGE at 70d4557cbb62
Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.
Checked: Compared the PR patch before and after the forward merge. The file set and added and removed lines match the reviewed revision. Keep hosted checks and the docked USB4 attach/detach, DP tunnel, dock I/O and suspend row.
Follow-ups (not blocking): hosted checks; docked USB4 attach/detach, DP tunnel, suspend row
|
Merging at |
What: The Thunderbolt/USB4 connection manager now checks five things a router or remote host reports before it uses them:
KUnit tests cover each.
Tested: KUnit
thunderboltsuite at c03c53f on arm64 QEMU with KASAN: 81/81 pass (8 new); each new test fails without its fix. Hardware rows still owed (below).Risk: two-way door · every Mac with USB4/Thunderbolt (shared
drivers/thunderboltcode)Not yet: hardware rows at this head on J714s (M5 Pro, USB4 dock) and J773g (M4 mini): dock attach and detach, displays over the dock, dock storage or Ethernet I/O, one deep sleep with the dock attached, against base.
Details
len - 2andkstrndup()reads past the DROM.tb_drom_parse()read the revision and data length (bytes 13 to 15) before checking that the DROM is that long. USB4 and NVM copies accept any reported size, down to 13 bytes. The version 1 parser also read the IDs at bytes 16 to 21 of a DROM sized for the 16-byte USB4 header. The DROM is now checked against the USB4 header size first, and against the full header before the version 1 parser runs.offsetofend(), notsizeof().sw->ports[]without a check againstmax_port_number, unlike the hotplug handler. Out-of-range adapters are now refused before indexing. The upstream port and non-DP-IN adapters were already refused bytb_port_is_dpin().data_length == 0nothing was copied, the offset stayed at 0, andwhile (!data_len || ...)sent the same request again forever on the ordered domain workqueue. The first response now fails the request, and the loop runs only while data is missing. Every response that passes the existing length check carries at least one data dword, and the copy is clamped to the block, so each round moves the offset forward.out_port, and indexedsw->ports[]with it unchecked. That happens intb_path_find_dst_port()and in both passes oftb_path_discover().max_port_numberwith a rate-limited warning, and discovery of that path fails.drivers/thunderbolt/path.c(master as of this PR), so this is a candidate to send upstream.max_port_number + 1and 63, a valid adapter on the first read and an invalid one on the second, and the same for the source HopID lookup. It also checks that no HopID stays allocated.tb_path_discover()follows each re-read out adapter's link. A router can name an in-range adapter without a link on the second read, which leftpNULL for the next hop's read. The second pass now fails discovery when it runs out of adapters before the hops the first pass counted; the error path gives back the HopIDs already taken. KUnit: a path through a device router whose host entry, read again, goes out of the host's unlinked adapter 3.tb_property_parse_dir()read the root directory's magic and length before checking the block length. The XDomain properties request allocates exactly the dwords the peer reports, so a one-dword block with the root magic was read past. Blocks shorter than the header are now refused. Mainline has the same code. KUnit: blocks of 0 and 1 dwords holding the root magic, each allocated to its exact size.tb_drom_read()with the host DROM given as the device property the Apple path reads. They cover lengths 0, 1 and header-only, a one-byte tail, an overrun, a short product descriptor at the end of the DROM, and a revision 1 DROM sized for the USB4 header.max_port_number + 1), 63, 0, 1, 7 (the host's upstream NHI adapter) and 5 (DP IN with no tunnel). It checks that nothing is tunneled and that the router reference is balanced.tb_cfg_request_sync(). An empty block now fails after one request; on the old code it repeated until the stub stopped answering. A five-dword block arrives in two clamped responses.tb_cfg_read().Evidence
Head
c03c53f31c7a9cbc5e0bac09f1b6d5420522fe73, basemain40e5e29cc915.W=1drivers/thunderboltpath.ctb_test_path_discover_shorter(paging fault intb_path_discover)property.ctb_test_property_parse_short_block(KASAN slab-out-of-bounds intb_property_parse_dir)4c3ae6ca3418withpath.cfrom97598779cdd5tb_test_path_discover_out_port(KASAN slab-out-of-bounds intb_path_discover)97598779cdd5with the four earlier fixes reverted (tests kept)tb_test_drom_entry_lengths(KASAN slab-out-of-bounds instrnlenfrom the one-byte name entry),tb_test_drom_product_descriptor,tb_test_drom_header_size(returns 0, expected -EIO),tb_test_dp_bandwidth_request_port(paging fault on adapter 14),tb_test_xdp_properties_progress(request repeated until the stub stopped answering)97598779cdd5merged with the two other USB4 branches,W=1checkpatch.pl: clean apart from thecommit ... upstream.line of the cherry-pick (stable style) and the test's initializer-list macros.