[PATCH] thunderbolt: Do not bind to NHI exposed by a hot-plugged device

From: Lukas Wunner
Date: Sat Apr 27 2024 - 10:24:18 EST


An NHI should only be exposed by Thunderbolt host controllers, not by
hot-plugged devices.

Avoid binding to an NHI erroneously or maliciously exposed by a device
by looking at the upstream port of its switch:

On a host controller, the upstream port is of type TB_TYPE_NHI, whereas
on hot-plugged devices it is of type TB_TYPE_PORT.

Signed-off-by: Lukas Wunner <lukas@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
drivers/thunderbolt/tb.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index e00e62de53f3..d95ff9ed4d96 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -2786,6 +2786,13 @@ static int tb_start(struct tb *tb, bool reset)
if (IS_ERR(tb->root_switch))
return PTR_ERR(tb->root_switch);

+ /* NHI erroneously exposed by a hot-plugged device? */
+ if (!tb_port_is_nhi(tb_upstream_port(tb->root_switch))) {
+ tb_err(tb, "not a host controller\n");
+ tb_switch_put(tb->root_switch);
+ return -ENODEV;
+ }
+
/*
* ICM firmware upgrade needs running firmware and in native
* mode that is not available so disable firmware upgrade of the
--
2.43.0