From 3f1b9b46c26cfd0abe97e9ebcdbf14c2fc274a16 Mon Sep 17 00:00:00 2001 From: Pengpeng Hou Date: Thu, 13 Aug 2026 15:23:57 +0000 Subject: [PATCH 1/9] thunderbolt: Require complete DROM entry headers 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 Signed-off-by: Mika Westerberg Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/thunderbolt/eeprom.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/thunderbolt/eeprom.c b/drivers/thunderbolt/eeprom.c index f2d56acb21a10..b1918fd05b590 100644 --- a/drivers/thunderbolt/eeprom.c +++ b/drivers/thunderbolt/eeprom.c @@ -324,7 +324,7 @@ int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid) } static int tb_drom_parse_entry_generic(struct tb_switch *sw, - struct tb_drom_entry_header *header) + const struct tb_drom_entry_header *header) { const struct tb_drom_entry_generic *entry = (const struct tb_drom_entry_generic *)header; @@ -360,7 +360,7 @@ static int tb_drom_parse_entry_generic(struct tb_switch *sw, } static int tb_drom_parse_entry_port(struct tb_switch *sw, - struct tb_drom_entry_header *header) + const struct tb_drom_entry_header *header) { struct tb_port *port; int res; @@ -392,11 +392,13 @@ static int tb_drom_parse_entry_port(struct tb_switch *sw, type &= 0xffffff; if (type == TB_TYPE_PORT) { - struct tb_drom_entry_port *entry = (void *) header; + const struct tb_drom_entry_port *entry = + (const struct tb_drom_entry_port *)header; + if (header->len != sizeof(*entry)) { tb_sw_warn(sw, "port entry has size %#x (expected %#zx)\n", - header->len, sizeof(struct tb_drom_entry_port)); + header->len, sizeof(*entry)); return -EIO; } port->link_nr = entry->link_nr; @@ -427,9 +429,16 @@ static int tb_drom_parse_entries(struct tb_switch *sw, size_t header_size) int res; while (pos < drom_size) { - struct tb_drom_entry_header *entry = (void *) (sw->drom + pos); - if (pos + 1 == drom_size || pos + entry->len > drom_size - || !entry->len) { + const struct tb_drom_entry_header *entry; + + if (drom_size - pos < sizeof(*entry)) { + tb_sw_warn(sw, "DROM buffer overrun\n"); + return -EIO; + } + + entry = (const struct tb_drom_entry_header *)(sw->drom + pos); + if (entry->len < sizeof(*entry) || + entry->len > drom_size - pos) { tb_sw_warn(sw, "DROM buffer overrun\n"); return -EIO; } From 33f5bc1fcf399d4853c516a81b15fb427eabb24e Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:26:55 -0500 Subject: [PATCH 2/9] thunderbolt: Check the DROM header and product descriptor sizes 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 <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/thunderbolt/eeprom.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/thunderbolt/eeprom.c b/drivers/thunderbolt/eeprom.c index b1918fd05b590..c7051ead2aba7 100644 --- a/drivers/thunderbolt/eeprom.c +++ b/drivers/thunderbolt/eeprom.c @@ -348,6 +348,13 @@ static int tb_drom_parse_entry_generic(struct tb_switch *sw, const struct tb_drom_entry_desc *desc = (const struct tb_drom_entry_desc *)entry; + /* Only the IDs are read; sizeof() would count the padding */ + if (header->len < offsetofend(struct tb_drom_entry_desc, idProduct)) { + tb_sw_warn(sw, "product descriptor entry too short (%u bytes), ignoring\n", + header->len); + break; + } + if (!sw->vendor && !sw->device) { sw->vendor = desc->idVendor; sw->device = desc->idProduct; @@ -668,6 +675,13 @@ static int tb_drom_parse(struct tb_switch *sw, u16 size) const struct tb_drom_header *header = (const void *)sw->drom; int ret; + /* The USB4 header is the shorter one, and holds the revision and length */ + if (size < USB4_DROM_HEADER_SIZE) { + tb_sw_warn(sw, "DROM too small (%u bytes)\n", size); + ret = -EIO; + goto err; + } + if (header->data_len + TB_DROM_DATA_START != size) { tb_sw_warn(sw, "DROM size mismatch\n"); ret = -EIO; @@ -685,6 +699,11 @@ static int tb_drom_parse(struct tb_switch *sw, u16 size) header->device_rom_revision); fallthrough; case 1: + if (size < TB_DROM_HEADER_SIZE) { + tb_sw_warn(sw, "DROM too small for its header (%u bytes)\n", size); + ret = -EIO; + break; + } ret = tb_drom_parse_v1(sw); break; } From bf1bc6a90acc755cc40b710db22c3bbd1390e743 Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:28:11 -0500 Subject: [PATCH 3/9] thunderbolt: Check the adapter of a DP bandwidth request 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: 6ce3563520be ("thunderbolt: Add support for DisplayPort bandwidth allocation mode") Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/thunderbolt/tb.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 41d9bdc44e8df..76d56149274e0 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -3661,6 +3661,12 @@ static void tb_handle_dp_bandwidth_request(struct work_struct *work) goto unlock; } + if (ev->port > sw->config.max_port_number) { + tb_warn(tb, "bandwidth request from non-existent adapter %llx:%u\n", + ev->route, ev->port); + goto put_sw; + } + in = &sw->ports[ev->port]; if (!tb_port_is_dpin(in)) { tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); From 6a1e04aa3f07144090e371744f387879f3986c51 Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:29:46 -0500 Subject: [PATCH 4/9] thunderbolt: Reject an empty XDomain properties block 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: d1ff70241a27 ("thunderbolt: Add support for XDomain discovery protocol") Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/thunderbolt/xdomain.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index cd60c717ab4c5..806e35222555f 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -394,6 +394,11 @@ static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, */ if (!data) { data_len = res->data_length; + /* Nothing would be copied, and the request repeated */ + if (!data_len) { + ret = -EINVAL; + goto err; + } if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) { ret = -E2BIG; goto err; @@ -410,7 +415,7 @@ static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, len = data_len - req.offset; memcpy(data + req.offset, res->data, len * 4); req.offset += len; - } while (!data_len || req.offset < data_len); + } while (req.offset < data_len); *block = data; *generation = res->generation; From 97598779cdd5272abca9e6c15b958b7cf7d60a3f Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:54:37 -0500 Subject: [PATCH 5/9] thunderbolt: Add KUnit tests for DROM sizes, DP bandwidth adapters and 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 <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/thunderbolt/ctl.c | 3 + drivers/thunderbolt/tb.c | 19 +++ drivers/thunderbolt/tb.h | 4 + drivers/thunderbolt/test.c | 278 ++++++++++++++++++++++++++++++++++ drivers/thunderbolt/xdomain.c | 2 +- 5 files changed, 305 insertions(+), 1 deletion(-) diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c index 9cb446d9df613..f14252ea4418f 100644 --- a/drivers/thunderbolt/ctl.c +++ b/drivers/thunderbolt/ctl.c @@ -965,6 +965,9 @@ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, DECLARE_COMPLETION_ONSTACK(done); int ret; + /* The KUnit tests answer requests without hardware */ + KUNIT_STATIC_STUB_REDIRECT(tb_cfg_request_sync, ctl, req, timeout_msec); + ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done); if (ret) { res.err = ret; diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 76d56149274e0..ed04a9b3eefda 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -4587,4 +4587,23 @@ void tb_test_cm_stop(struct tb *tb) if (cancel_work_sync(&tcm->dp_relink_run)) tb_domain_put(tb); } + +/* Handle a DP bandwidth notification from @port of the router at @route, with no retry. */ +void tb_test_cm_dp_bandwidth_request(struct tb *tb, u64 route, u8 port) +{ + struct tb_cm *tcm = tb_priv(tb); + struct tb_hotplug_event *ev; + + ev = kmalloc_obj(*ev); + if (!ev) + return; + + tcm->hotplug_active = true; + ev->tb = tb; + ev->route = route; + ev->port = port; + ev->retry = TB_BW_ALLOC_RETRIES; + INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request); + tb_handle_dp_bandwidth_request(&ev->work.work); +} #endif diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index 70caebcf2b38b..41ed46a6739b8 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -777,6 +777,9 @@ bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, const void *buf, size_t size); bool tb_domain_retain_noirq_control(struct tb *tb); bool tb_xdp_noirq_request(enum tb_xdp_type type); +int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, + const uuid_t *src_uuid, const uuid_t *dst_uuid, + int retry, u32 **block, u32 *generation); #endif #if IS_ENABLED(CONFIG_USB4_KUNIT_TEST) size_t tb_test_cm_size(void); @@ -786,6 +789,7 @@ struct list_head *tb_test_cm_dp_resources(struct tb *tb); void tb_test_cm_set_relink_deferred(struct tb *tb, bool deferred); bool tb_test_cm_relink_armed(struct tb *tb); void tb_test_cm_stop(struct tb *tb); +void tb_test_cm_dp_bandwidth_request(struct tb *tb, u64 route, u8 port); #endif extern const struct device_type tb_domain_type; diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index fbb318f17812d..644908336107d 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -6,11 +6,15 @@ * Author: Mika Westerberg */ +#include #include +#include #include #include #include +#include #include +#include #include "tb.h" #include "tunnel.h" @@ -4647,6 +4651,275 @@ static void tb_test_noirq_xdomain_filter(struct kunit *test) } } +/* + * A DROM as the device tree hands it to the host router: @size bytes, of which + * the DROM is the first @drom_size. Returns what tb_drom_read() returns. + */ +static int tb_test_drom_read(struct kunit *test, struct tb_switch *sw, + const u8 *drom, size_t drom_size, size_t size) +{ + struct property_entry props[2] = {}; + struct tb_nhi *nhi; + struct device *dev; + struct tb *tb; + u8 *buf; + int ret; + + buf = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + memcpy(buf, drom, drom_size); + /* The data length covers everything after the CRC32 */ + buf[14] = drom_size - 13; + buf[15] = (drom_size - 13) >> 8; + put_unaligned_le32(~crc32c(~0, buf + 13, drom_size - 13), buf + 9); + + dev = kunit_device_register(test, "tb-test-drom"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + props[0] = PROPERTY_ENTRY_U8_ARRAY_LEN("apple,thunderbolt-drom", buf, size); + KUNIT_ASSERT_EQ(test, device_create_managed_software_node(dev, props, NULL), 0); + + nhi = kunit_kzalloc(test, sizeof(*nhi), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, nhi); + tb = kunit_kzalloc(test, sizeof(*tb), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, tb); + nhi->dev = dev; + tb->nhi = nhi; + sw->tb = tb; + + ret = tb_drom_read(sw); + kfree(sw->drom); + sw->drom = NULL; + kunit_device_unregister(test, dev); + return ret; +} + +static void tb_test_drom_free_names(void *data) +{ + struct tb_switch *sw = data; + + kfree(sw->vendor_name); + kfree(sw->device_name); +} + +static struct tb_switch *tb_test_drom_switch(struct kunit *test) +{ + struct tb_switch *sw = alloc_host(test); + + KUNIT_ASSERT_NOT_NULL(test, sw); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, tb_test_drom_free_names, sw), 0); + return sw; +} + +/* USB4 DROM header: revision 3, the entries follow at byte 16 */ +#define TB_TEST_DROM_V3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 +/* Generic entry @index of @len bytes, header included */ +#define TB_TEST_DROM_GENERIC(len, index) (len), (index) +/* The vendor name "Aurora" */ +#define TB_TEST_DROM_VENDOR TB_TEST_DROM_GENERIC(9, 1), 'A', 'u', 'r', 'o', 'r', 'a', 0 + +/* An entry is at least its two-byte header, and fits in the DROM. */ +static void tb_test_drom_entry_lengths(struct kunit *test) +{ + static const u8 valid[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR }; + /* A name entry of one byte, the last two bytes of the DROM */ + static const u8 one[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(1, 2) }; + static const u8 zero[] = { TB_TEST_DROM_V3, TB_TEST_DROM_GENERIC(0, 2), + TB_TEST_DROM_VENDOR }; + /* Only one byte left for the next entry's header */ + static const u8 tail[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, 5 }; + static const u8 header_only[] = { TB_TEST_DROM_V3, TB_TEST_DROM_GENERIC(2, 2), + TB_TEST_DROM_VENDOR }; + static const u8 overrun[] = { TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(4, 2), 'x' }; + struct tb_switch *sw; + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, valid, sizeof(valid), sizeof(valid)), 0); + KUNIT_EXPECT_STREQ(test, sw->vendor_name, "Aurora"); + KUNIT_EXPECT_NULL(test, sw->device_name); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, one, sizeof(one), sizeof(one)), -EIO); + KUNIT_EXPECT_NULL(test, sw->device_name); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, zero, sizeof(zero), sizeof(zero)), -EIO); + KUNIT_EXPECT_NULL(test, sw->vendor_name); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, tail, sizeof(tail), sizeof(tail)), -EIO); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, header_only, sizeof(header_only), + sizeof(header_only)), 0); + KUNIT_EXPECT_STREQ(test, sw->device_name, ""); + KUNIT_EXPECT_STREQ(test, sw->vendor_name, "Aurora"); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, overrun, sizeof(overrun), + sizeof(overrun)), -EIO); +} + +/* The product descriptor is used only if it holds both IDs. */ +static void tb_test_drom_product_descriptor(struct kunit *test) +{ + static const u8 full[] = { + TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(15, 9), 0x00, 0x04, 0xac, 0x05, 0x34, 0x12, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + }; + /* Ends inside the product ID, at the end of the DROM */ + static const u8 short_desc[] = { + TB_TEST_DROM_V3, TB_TEST_DROM_VENDOR, + TB_TEST_DROM_GENERIC(7, 9), 0x00, 0x04, 0xac, 0x05, 0x34, + }; + struct tb_switch *sw; + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, full, sizeof(full), sizeof(full)), 0); + KUNIT_EXPECT_EQ(test, sw->vendor, 0x05ac); + KUNIT_EXPECT_EQ(test, sw->device, 0x1234); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, short_desc, sizeof(short_desc), + sizeof(short_desc)), 0); + KUNIT_EXPECT_EQ(test, sw->vendor, 0); + KUNIT_EXPECT_EQ(test, sw->device, 0); + KUNIT_EXPECT_STREQ(test, sw->vendor_name, "Aurora"); +} + +static u8 tb_test_crc8(const u8 *data, int len) +{ + u8 val = 0xff; + int i, j; + + for (i = 0; i < len; i++) { + val ^= data[i]; + for (j = 0; j < 8; j++) + val = (val << 1) ^ ((val & 0x80) ? 7 : 0); + } + return val; +} + +/* The header is read only from a DROM that holds it. */ +static void tb_test_drom_header_size(struct kunit *test) +{ + /* A revision 1 DROM as long as a USB4 header: 6 bytes short of its own */ + u8 v1[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 1 }; + /* No room for the revision and data length */ + static const u8 tiny[13] = {}; + struct tb_switch *sw; + + v1[0] = tb_test_crc8(v1 + 1, 8); + + /* The device tree property is always at least the full header */ + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, v1, sizeof(v1), 22), -EIO); + KUNIT_EXPECT_EQ(test, sw->vendor, 0); + + sw = tb_test_drom_switch(test); + KUNIT_EXPECT_EQ(test, tb_test_drom_read(test, sw, tiny, sizeof(tiny), 22), -EIO); +} + +/* + * A DP bandwidth notification names an adapter of its router: one the router + * does not have, or that is no DP IN adapter, is refused without using it. + */ +static void tb_test_dp_bandwidth_request_port(struct kunit *test) +{ + struct tb_test_cm *cm = tb_test_cm_alloc(test); + struct tb_switch *host = cm->host; + static const u8 ports[] = { 14, 63, 0, 1, 7, 5 }; + unsigned int refs; + int i; + + cm->tb->root_switch = host; + refs = kref_read(&host->dev.kobj.kref); + for (i = 0; i < ARRAY_SIZE(ports); i++) { + tb_test_cm_dp_bandwidth_request(cm->tb, 0, ports[i]); + KUNIT_EXPECT_EQ_MSG(test, kref_read(&host->dev.kobj.kref), refs, + "request for adapter %u", ports[i]); + } + /* Nothing was tunneled or changed for any of them */ + KUNIT_EXPECT_TRUE(test, list_empty(tb_test_cm_tunnels(cm->tb))); + cm->tb->root_switch = NULL; +} + +/* A remote host answering XDomain properties requests from a fixed block. */ +struct tb_test_xdp_props { + unsigned int requests; + u16 data_length; + unsigned int words; +}; + +static struct tb_cfg_result tb_test_xdp_props_sync(struct tb_ctl *ctl, + struct tb_cfg_request *req, + int timeout_msec) +{ + struct tb_test_xdp_props *peer = kunit_get_current_test()->priv; + const struct tb_xdp_properties *request = req->request; + struct tb_xdp_properties_response *res = req->response; + struct tb_cfg_result result = {}; + unsigned int i; + + /* Give up after a few, as a remote host that stops answering */ + if (++peer->requests > 4) { + result.err = -ETIMEDOUT; + return result; + } + + memset(res, 0, req->response_size); + res->hdr.xd_hdr.length_sn = (sizeof(*res) - sizeof(res->hdr.xd_hdr)) / 4 + peer->words; + res->hdr.type = PROPERTIES_RESPONSE; + res->offset = request->offset; + res->data_length = peer->data_length; + res->generation = 7; + for (i = 0; i < peer->words; i++) + res->data[i] = request->offset + i + 1; + return result; +} + +static int tb_test_xdp_props_request(struct kunit *test, u32 **block, u32 *generation) +{ + kunit_activate_static_stub(test, tb_cfg_request_sync, tb_test_xdp_props_sync); + return tb_xdp_properties_request(NULL, 0x1, &tb_test_xdp_uuid, &tb_test_service_uuid, + 0, block, generation); +} + +/* Each response moves the request on, and an empty block ends it. */ +static void tb_test_xdp_properties_progress(struct kunit *test) +{ + struct tb_test_xdp_props *peer; + u32 *block = NULL; + u32 generation = 0; + int ret, i; + + peer = kunit_kzalloc(test, sizeof(*peer), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, peer); + test->priv = peer; + + /* An empty block: refused at the first response, not asked for again */ + peer->words = 3; + ret = tb_test_xdp_props_request(test, &block, &generation); + KUNIT_EXPECT_EQ(test, ret, -EINVAL); + KUNIT_EXPECT_EQ(test, peer->requests, 1U); + if (ret >= 0) + kfree(block); + + /* Five dwords in two responses, the second one clamped to the block */ + peer->requests = 0; + peer->data_length = 5; + block = NULL; + ret = tb_test_xdp_props_request(test, &block, &generation); + KUNIT_ASSERT_EQ(test, ret, 5); + KUNIT_EXPECT_EQ(test, peer->requests, 2U); + KUNIT_EXPECT_EQ(test, generation, 7U); + for (i = 0; i < 5; i++) + KUNIT_EXPECT_EQ(test, block[i], (u32)i + 1); + kfree(block); +} + static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_apple_right_dpin_wiring), KUNIT_CASE(tb_test_dp_reduce_bandwidth_shared), @@ -4721,6 +4994,11 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_property_parse_zero_length), KUNIT_CASE(tb_test_property_parse_rootdir_overflow), KUNIT_CASE(tb_test_property_merge), + KUNIT_CASE(tb_test_drom_entry_lengths), + KUNIT_CASE(tb_test_drom_product_descriptor), + KUNIT_CASE(tb_test_drom_header_size), + KUNIT_CASE(tb_test_dp_bandwidth_request_port), + KUNIT_CASE(tb_test_xdp_properties_progress), { } }; diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index 806e35222555f..3db6a93c4eabf 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -329,7 +329,7 @@ static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence, TB_CFG_PKG_XDOMAIN_RESP); } -static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, +VISIBLE_IF_KUNIT int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route, const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry, u32 **block, u32 *generation) { From 4c3ae6ca3418898f04a7bc06eec5261e631b190c Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:55:53 -0500 Subject: [PATCH 6/9] thunderbolt: Check the adapter of each hop in path discovery 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 <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/thunderbolt/path.c | 40 ++++++++++----- drivers/thunderbolt/tb.h | 4 ++ drivers/thunderbolt/test.c | 99 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 11 deletions(-) diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c index 8d4e3d63d21c4..c9220f1a88a2f 100644 --- a/drivers/thunderbolt/path.c +++ b/drivers/thunderbolt/path.c @@ -31,20 +31,35 @@ static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop regs->unknown1, regs->unknown2, regs->unknown3); } +/* + * The adapter that @hop, read from @port, sends the path out of: the router + * reports it, so it is NULL if the router has no such adapter. + */ +static struct tb_port *tb_path_hop_out_port(struct tb_port *port, + const struct tb_regs_hop *hop) +{ + struct tb_switch *sw = port->sw; + + if (hop->out_port > sw->config.max_port_number) { + tb_port_warn_ratelimited(port, "path goes out of non-existent adapter %u\n", + hop->out_port); + return NULL; + } + + return &sw->ports[hop->out_port]; +} + static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid, int dst_hopid) { struct tb_port *port, *out_port = NULL; struct tb_regs_hop hop; - struct tb_switch *sw; int i, ret, hopid; hopid = src_hopid; port = src; for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) { - sw = port->sw; - ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2); if (ret) { tb_port_warn(port, "failed to read path at %d\n", hopid); @@ -54,7 +69,9 @@ static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid, if (!hop.enable) return NULL; - out_port = &sw->ports[hop.out_port]; + out_port = tb_path_hop_out_port(port, &hop); + if (!out_port) + return NULL; hopid = hop.next_hop; port = out_port->remote; } @@ -106,7 +123,6 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, struct tb_port *out_port; struct tb_regs_hop hop; struct tb_path *path; - struct tb_switch *sw; struct tb_port *p; size_t num_hops; int ret, i, h; @@ -129,8 +145,6 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, num_hops = 0; for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) { - sw = p->sw; - ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2); if (ret) { tb_port_warn(p, "failed to read path at %d\n", h); @@ -141,7 +155,9 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, if (!hop.enable) break; - out_port = &sw->ports[hop.out_port]; + out_port = tb_path_hop_out_port(p, &hop); + if (!out_port) + return NULL; if (last) *last = out_port; @@ -170,18 +186,20 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, for (i = 0; i < num_hops; i++) { int next_hop; - sw = p->sw; - ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2); if (ret) { tb_port_warn(p, "failed to read path at %d\n", h); goto err; } + /* Read again: the router may report another adapter this time */ + out_port = tb_path_hop_out_port(p, &hop); + if (!out_port) + goto err; + if (alloc_hopid && tb_port_alloc_in_hopid(p, h, h) < 0) goto err; - out_port = &sw->ports[hop.out_port]; next_hop = hop.next_hop; if (alloc_hopid && diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index 41ed46a6739b8..12ce300710f96 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -736,6 +736,8 @@ static inline int tb_port_write(struct tb_port *port, const void *buffer, #define tb_err(tb, fmt, arg...) dev_err((tb)->nhi->dev, fmt, ## arg) #define tb_WARN(tb, fmt, arg...) dev_WARN((tb)->nhi->dev, fmt, ## arg) #define tb_warn(tb, fmt, arg...) dev_warn((tb)->nhi->dev, fmt, ## arg) +#define tb_warn_ratelimited(tb, fmt, arg...) \ + dev_warn_ratelimited((tb)->nhi->dev, fmt, ## arg) #define tb_info(tb, fmt, arg...) dev_info((tb)->nhi->dev, fmt, ## arg) #define tb_dbg(tb, fmt, arg...) dev_dbg((tb)->nhi->dev, fmt, ## arg) @@ -760,6 +762,8 @@ static inline int tb_port_write(struct tb_port *port, const void *buffer, __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg) #define tb_port_warn(port, fmt, arg...) \ __TB_PORT_PRINT(tb_warn, port, fmt, ##arg) +#define tb_port_warn_ratelimited(port, fmt, arg...) \ + __TB_PORT_PRINT(tb_warn_ratelimited, port, fmt, ##arg) #define tb_port_info(port, fmt, arg...) \ __TB_PORT_PRINT(tb_info, port, fmt, ##arg) #define tb_port_dbg(port, fmt, arg...) \ diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index 644908336107d..a3426375200ac 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -4920,6 +4920,104 @@ static void tb_test_xdp_properties_progress(struct kunit *test) kfree(block); } +/* A router whose hop entries all go out of the adapters listed, one per read. */ +struct tb_test_hops { + unsigned int reads; + u8 out_port[2]; +}; + +static int tb_test_hops_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, + enum tb_cfg_space space, u32 offset, u32 length) +{ + struct tb_test_hops *hops = kunit_get_current_test()->priv; + struct tb_regs_hop hop = { .enable = true, .next_hop = 8 }; + unsigned int n = min_t(unsigned int, hops->reads++, ARRAY_SIZE(hops->out_port) - 1); + + if (space != TB_CFG_HOPS || length * 4 > sizeof(hop)) + return -EIO; + hop.out_port = hops->out_port[n]; + memcpy(buffer, &hop, length * 4); + return 0; +} + +static struct tb_test_hops *tb_test_hops_alloc(struct kunit *test, struct tb_switch **host) +{ + struct tb_test_hops *hops; + struct tb_nhi *nhi; + struct tb *tb; + + hops = kunit_kzalloc(test, sizeof(*hops), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, hops); + nhi = kunit_kzalloc(test, sizeof(*nhi), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, nhi); + tb = kunit_kzalloc(test, sizeof(*tb), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, tb); + *host = alloc_host(test); + KUNIT_ASSERT_NOT_NULL(test, *host); + tb->nhi = nhi; + (*host)->tb = tb; + + test->priv = hops; + kunit_activate_static_stub(test, tb_cfg_read, tb_test_hops_cfg_read); + return hops; +} + +static void tb_test_hops_set(struct tb_test_hops *hops, u8 first, u8 again) +{ + hops->reads = 0; + hops->out_port[0] = first; + hops->out_port[1] = again; +} + +/* + * Path discovery follows the adapters the routers report in their hop + * entries, and stops at one the router does not have. + */ +static void tb_test_path_discover_out_port(struct kunit *test) +{ + struct tb_switch *host; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *lane = &host->ports[1]; + u8 last_port = host->config.max_port_number; + struct tb_port *last = NULL; + struct tb_path *path; + + /* DP IN HopID 8 out of lane adapter 1, where the path ends */ + tb_test_hops_set(hops, 1, 1); + path = tb_path_discover(in, 8, NULL, -1, &last, "Video", true); + KUNIT_ASSERT_NOT_NULL(test, path); + KUNIT_EXPECT_EQ(test, path->path_length, 1); + KUNIT_EXPECT_PTR_EQ(test, path->hops[0].out_port, lane); + KUNIT_EXPECT_PTR_EQ(test, last, lane); + tb_path_free(path); + + /* An adapter past the router's last one, and the largest the entry holds */ + tb_test_hops_set(hops, last_port + 1, last_port + 1); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + tb_test_hops_set(hops, 63, 63); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + + /* Valid while the hops are counted, not when they are read again */ + tb_test_hops_set(hops, 1, last_port + 1); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 2U); + + /* Looking for the source HopID of a path that ends on the lane adapter */ + tb_test_hops_set(hops, last_port + 1, last_port + 1); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, -1, lane, 8, NULL, "Video", false)); + tb_test_hops_set(hops, 1, 1); + path = tb_path_discover(in, -1, lane, 8, NULL, "Video", false); + KUNIT_ASSERT_NOT_NULL(test, path); + KUNIT_EXPECT_EQ(test, path->hops[0].in_hop_index, 8); + tb_path_free(path); + + /* No HopID is left allocated by the refused paths */ + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(lane, 8, 8), 8); + tb_port_release_out_hopid(lane, 8); +} + static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_apple_right_dpin_wiring), KUNIT_CASE(tb_test_dp_reduce_bandwidth_shared), @@ -4999,6 +5097,7 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_drom_header_size), KUNIT_CASE(tb_test_dp_bandwidth_request_port), KUNIT_CASE(tb_test_xdp_properties_progress), + KUNIT_CASE(tb_test_path_discover_out_port), { } }; From ca623f30607e071ac9cbab1cabe434aafb71a622 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 26 Nov 2026 04:56:30 -0500 Subject: [PATCH 7/9] thunderbolt: Stop path discovery when a hop re-read ends the path early 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 --- drivers/thunderbolt/path.c | 4 +++ drivers/thunderbolt/test.c | 54 +++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c index c9220f1a88a2f..16d478696c85e 100644 --- a/drivers/thunderbolt/path.c +++ b/drivers/thunderbolt/path.c @@ -186,6 +186,10 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, for (i = 0; i < num_hops; i++) { int next_hop; + /* Ended early: a router reported an adapter without a link this time */ + if (!p) + goto err; + ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2); if (ret) { tb_port_warn(p, "failed to read path at %d\n", h); diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index a3426375200ac..cbeb0b432137d 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -4920,10 +4920,10 @@ static void tb_test_xdp_properties_progress(struct kunit *test) kfree(block); } -/* A router whose hop entries all go out of the adapters listed, one per read. */ +/* Routers whose hop entries go out of the adapters listed, one per read. */ struct tb_test_hops { - unsigned int reads; - u8 out_port[2]; + unsigned int reads, count; + u8 out_port[4]; }; static int tb_test_hops_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, @@ -4931,7 +4931,7 @@ static int tb_test_hops_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u3 { struct tb_test_hops *hops = kunit_get_current_test()->priv; struct tb_regs_hop hop = { .enable = true, .next_hop = 8 }; - unsigned int n = min_t(unsigned int, hops->reads++, ARRAY_SIZE(hops->out_port) - 1); + unsigned int n = min_t(unsigned int, hops->reads++, hops->count - 1); if (space != TB_CFG_HOPS || length * 4 > sizeof(hop)) return -EIO; @@ -4965,6 +4965,7 @@ static struct tb_test_hops *tb_test_hops_alloc(struct kunit *test, struct tb_swi static void tb_test_hops_set(struct tb_test_hops *hops, u8 first, u8 again) { hops->reads = 0; + hops->count = 2; hops->out_port[0] = first; hops->out_port[1] = again; } @@ -5018,6 +5019,50 @@ static void tb_test_path_discover_out_port(struct kunit *test) tb_port_release_out_hopid(lane, 8); } +/* + * Read again, a path can change shape: here it reaches a device router + * through lane adapter 1 and ends on the device's adapter 3, then goes out + * of the host's unlinked adapter 3 instead. Discovery fails instead of + * following a link that is not there. + */ +static void tb_test_path_discover_shorter(struct kunit *test) +{ + struct tb_switch *host, *dev; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *unlinked = &host->ports[3]; + struct tb_path *path; + + dev = alloc_dev_default(test, host, 0x1, true); + KUNIT_ASSERT_NOT_NULL(test, dev); + dev->tb = host->tb; + + /* Both reads the same: two hops, the second on the device router */ + hops->reads = 0; + hops->count = 4; + hops->out_port[0] = 1; + hops->out_port[1] = 3; + hops->out_port[2] = 1; + hops->out_port[3] = 3; + path = tb_path_discover(in, 8, NULL, -1, NULL, "Video", true); + KUNIT_ASSERT_NOT_NULL(test, path); + KUNIT_EXPECT_EQ(test, path->path_length, 2); + KUNIT_EXPECT_PTR_EQ(test, path->hops[1].in_port, &dev->ports[1]); + KUNIT_EXPECT_PTR_EQ(test, path->hops[1].out_port, &dev->ports[3]); + tb_path_free(path); + + /* The host's entry names its unlinked adapter 3 on the second read */ + hops->reads = 0; + hops->out_port[2] = 3; + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 3U); + + /* The HopIDs taken for the first hop were given back */ + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(unlinked, 8, 8), 8); + tb_port_release_out_hopid(unlinked, 8); +} + static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_apple_right_dpin_wiring), KUNIT_CASE(tb_test_dp_reduce_bandwidth_shared), @@ -5098,6 +5143,7 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_dp_bandwidth_request_port), KUNIT_CASE(tb_test_xdp_properties_progress), KUNIT_CASE(tb_test_path_discover_out_port), + KUNIT_CASE(tb_test_path_discover_shorter), { } }; From c03c53f31c7a9cbc5e0bac09f1b6d5420522fe73 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 26 Nov 2026 04:58:26 -0500 Subject: [PATCH 8/9] thunderbolt: Check the property block holds the root directory header 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 --- drivers/thunderbolt/property.c | 2 ++ drivers/thunderbolt/test.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/drivers/thunderbolt/property.c b/drivers/thunderbolt/property.c index df18a6d69d8b4..7fea31d06e47b 100644 --- a/drivers/thunderbolt/property.c +++ b/drivers/thunderbolt/property.c @@ -246,6 +246,8 @@ struct tb_property_dir *tb_property_parse_dir(const u32 *block, const struct tb_property_rootdir_entry *rootdir = (const struct tb_property_rootdir_entry *)block; + if (block_len < sizeof(*rootdir) / 4) + return NULL; if (rootdir->magic != TB_PROPERTY_ROOTDIR_MAGIC) return NULL; if (rootdir->length > block_len) diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index cbeb0b432137d..c64a1c3cf6b63 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -3039,6 +3039,23 @@ static void tb_test_property_parse_zero_length(struct kunit *test) tb_property_free_dir(dir); } +/* A block shorter than the root directory header is not read past. */ +static void tb_test_property_parse_short_block(struct kunit *test) +{ + size_t len; + + for (len = 0; len < 2; len++) { + /* Exactly the block, so a read past it is caught */ + u32 *block = kunit_kzalloc(test, max_t(size_t, len, 1) * sizeof(u32), + GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, block); + block[0] = 0x55584401; /* rootdir magic */ + KUNIT_EXPECT_NULL_MSG(test, tb_property_parse_dir(block, len), + "%zu dwords", len); + } +} + static void tb_test_property_parse_rootdir_overflow(struct kunit *test) { u32 *block = kunit_kzalloc(test, 4 * sizeof(u32), GFP_KERNEL); @@ -5136,6 +5153,7 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_property_copy), KUNIT_CASE(tb_test_property_parse_zero_length), KUNIT_CASE(tb_test_property_parse_rootdir_overflow), + KUNIT_CASE(tb_test_property_parse_short_block), KUNIT_CASE(tb_test_property_merge), KUNIT_CASE(tb_test_drom_entry_lengths), KUNIT_CASE(tb_test_drom_product_descriptor), From 69399d99e525f4b3c2e409d2f77a0a9006775f4a Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:26:24 +1100 Subject: [PATCH 9/9] thunderbolt: path: reject adapter 0 and hops disabled on the second read 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 --- drivers/thunderbolt/path.c | 9 +++++-- drivers/thunderbolt/test.c | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c index 16d478696c85e..a41fd7b1b30d6 100644 --- a/drivers/thunderbolt/path.c +++ b/drivers/thunderbolt/path.c @@ -40,8 +40,9 @@ static struct tb_port *tb_path_hop_out_port(struct tb_port *port, { struct tb_switch *sw = port->sw; - if (hop->out_port > sw->config.max_port_number) { - tb_port_warn_ratelimited(port, "path goes out of non-existent adapter %u\n", + /* Adapter 0 is the control adapter: no tunnel path leaves through it */ + if (!hop->out_port || hop->out_port > sw->config.max_port_number) { + tb_port_warn_ratelimited(port, "path goes out of invalid adapter %u\n", hop->out_port); return NULL; } @@ -196,6 +197,10 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid, goto err; } + /* The hop may have been disabled since the first pass */ + if (!hop.enable) + goto err; + /* Read again: the router may report another adapter this time */ out_port = tb_path_hop_out_port(p, &hop); if (!out_port) diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index c64a1c3cf6b63..347fe4d04495a 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -4941,6 +4941,7 @@ static void tb_test_xdp_properties_progress(struct kunit *test) struct tb_test_hops { unsigned int reads, count; u8 out_port[4]; + bool disabled[4]; }; static int tb_test_hops_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, @@ -4953,6 +4954,7 @@ static int tb_test_hops_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u3 if (space != TB_CFG_HOPS || length * 4 > sizeof(hop)) return -EIO; hop.out_port = hops->out_port[n]; + hop.enable = !hops->disabled[n]; memcpy(buffer, &hop, length * 4); return 0; } @@ -4985,6 +4987,7 @@ static void tb_test_hops_set(struct tb_test_hops *hops, u8 first, u8 again) hops->count = 2; hops->out_port[0] = first; hops->out_port[1] = again; + memset(hops->disabled, 0, sizeof(hops->disabled)); } /* @@ -5036,6 +5039,51 @@ static void tb_test_path_discover_out_port(struct kunit *test) tb_port_release_out_hopid(lane, 8); } +/* + * Adapter 0 is the control adapter: a hop entry naming it is refused, on + * either read, and leaves no HopID allocated. + */ +static void tb_test_path_discover_out_port_zero(struct kunit *test) +{ + struct tb_switch *host; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *lane = &host->ports[1]; + + tb_test_hops_set(hops, 0, 0); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 1U); + + tb_test_hops_set(hops, 1, 0); + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 2U); + + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(lane, 8, 8), 8); + tb_port_release_out_hopid(lane, 8); +} + +/* + * A hop enabled while the hops are counted but disabled when it is read + * again fails discovery instead of being used as a live hop. + */ +static void tb_test_path_discover_hop_disabled(struct kunit *test) +{ + struct tb_switch *host; + struct tb_test_hops *hops = tb_test_hops_alloc(test, &host); + struct tb_port *in = &host->ports[5], *lane = &host->ports[1]; + + tb_test_hops_set(hops, 1, 1); + hops->disabled[1] = true; + KUNIT_EXPECT_NULL(test, tb_path_discover(in, 8, NULL, -1, NULL, "Video", true)); + KUNIT_EXPECT_EQ(test, hops->reads, 2U); + + KUNIT_EXPECT_EQ(test, tb_port_alloc_in_hopid(in, 8, 8), 8); + tb_port_release_in_hopid(in, 8); + KUNIT_EXPECT_EQ(test, tb_port_alloc_out_hopid(lane, 8, 8), 8); + tb_port_release_out_hopid(lane, 8); +} + /* * Read again, a path can change shape: here it reaches a device router * through lane adapter 1 and ends on the device's adapter 3, then goes out @@ -5162,6 +5210,8 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_xdp_properties_progress), KUNIT_CASE(tb_test_path_discover_out_port), KUNIT_CASE(tb_test_path_discover_shorter), + KUNIT_CASE(tb_test_path_discover_out_port_zero), + KUNIT_CASE(tb_test_path_discover_hop_disabled), { } };