Skip to contentSkip to conversation

thunderbolt_tb: add check for return value of 'tb_switch_find_port'

PATCH In review Natalia Petrova 2 participants 2 messages Feb 17, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thunderbolt_tb: add check for return value of 'tb_switch_find_port'

Return value of 'tb_switch_find_port' has to be checked to avoid null pointer dereferences in functions tb_tunnel_alloc_dma() and tb_port_info().

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 386e5e29d81c ("thunderbolt: Make tb_find_port() available to other files")
---
 drivers/thunderbolt/tb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/thunderbolt/tb.c +2 -0
index 9853f6c7e81d..5d6cded68d46 100644
@@ -1156,6 +1156,8 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 	sw = tb_to_switch(xd->dev.parent);
 	dst_port = tb_port_at(xd->route, sw);
 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
+	if (!nhi_port)
+		return -EIO;
 
 	mutex_lock(&tb->lock);
 	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Quoted 2 lines · Natalia Petrova

Return value of 'tb_switch_find_port' has to be checked to avoid null pointer dereferences in functions tb_tunnel_alloc_dma() and

NULL

Quoted 15 lines

tb_port_info().

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 386e5e29d81c ("thunderbolt: Make tb_find_port() available to other files")
---
 drivers/thunderbolt/tb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/thunderbolt/tb.c +0 -0
index 9853f6c7e81d..5d6cded68d46 100644

drivers/thunderbolt/tb.c static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, · line 1159

Quoted 4 lines
drivers/thunderbolt/tb.c +1 -0
@@ -1156,6 +1156,8 @@ static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
 	sw = tb_to_switch(xd->dev.parent);
 	dst_port = tb_port_at(xd->route, sw);
 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
+	if (!nhi_port)

This really cannot happen in real life. All host routers must have a host interface adapter so I'm not sure if adding these checks do anything else than adding dead code.

Quoted 6 lines
drivers/thunderbolt/tb.c +1 -0
+		return -EIO;
 
 	mutex_lock(&tb->lock);
 	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
Signature · 1 line
2.34.1