Skip to content

Commit f32ba09

Browse files
jk-ozlabskuba-moo
authored andcommitted
net: mctp: defer creation of dst after source-address check
Sashiko reports: > mctp_dst_from_route() increments the device reference count by calling > mctp_dev_hold(). When a valid route is found and dst is NULL, the > structure copy is bypassed and rc is set to 0. Instead of optimistically creating a dst from the final route (then releasing it if the saddr is invalid), perform the saddr check first. This means we don't have an unuecessary hold/release on the dev, which could leak if the dst pointer is NULL. No caller passes a NULL dst at present though (so the leak is not possible), but this is an intended use of mctp_dst_from_route(). Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au> Reviewed-by: Simon Horman <horms@kernel.org> Link: https://patch.msgid.link/20260403-dev-mctp-dst-defer-v1-1-9c2c55faf9e9@codeconstruct.com.au Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 70e32aa commit f32ba09

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

net/mctp/route.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ static mctp_eid_t mctp_dev_saddr(struct mctp_dev *dev)
897897

898898
/* must only be called on a direct route, as the final output hop */
899899
static void mctp_dst_from_route(struct mctp_dst *dst, mctp_eid_t eid,
900-
unsigned int mtu, struct mctp_route *route)
900+
mctp_eid_t saddr, unsigned int mtu,
901+
struct mctp_route *route)
901902
{
902903
mctp_dev_hold(route->dev);
903904
dst->nexthop = eid;
@@ -907,7 +908,7 @@ static void mctp_dst_from_route(struct mctp_dst *dst, mctp_eid_t eid,
907908
dst->mtu = min(dst->mtu, mtu);
908909
dst->halen = 0;
909910
dst->output = route->output;
910-
dst->saddr = mctp_dev_saddr(route->dev);
911+
dst->saddr = saddr;
911912
}
912913

913914
int mctp_dst_from_extaddr(struct mctp_dst *dst, struct net *net, int ifindex,
@@ -975,7 +976,6 @@ int mctp_route_lookup(struct net *net, unsigned int dnet,
975976
{
976977
const unsigned int max_depth = 32;
977978
unsigned int depth, mtu = 0;
978-
struct mctp_dst dst_tmp;
979979
int rc = -EHOSTUNREACH;
980980

981981
rcu_read_lock();
@@ -996,15 +996,15 @@ int mctp_route_lookup(struct net *net, unsigned int dnet,
996996
mtu = mtu ?: rt->mtu;
997997

998998
if (rt->dst_type == MCTP_ROUTE_DIRECT) {
999-
mctp_dst_from_route(&dst_tmp, daddr, mtu, rt);
999+
mctp_eid_t saddr = mctp_dev_saddr(rt->dev);
1000+
10001001
/* cannot do gateway-ed routes without a src */
1001-
if (dst_tmp.saddr == MCTP_ADDR_NULL && depth != 0) {
1002-
mctp_dst_release(&dst_tmp);
1003-
} else {
1004-
if (dst)
1005-
*dst = dst_tmp;
1006-
rc = 0;
1007-
}
1002+
if (saddr == MCTP_ADDR_NULL && depth != 0)
1003+
break;
1004+
1005+
if (dst)
1006+
mctp_dst_from_route(dst, daddr, saddr, mtu, rt);
1007+
rc = 0;
10081008
break;
10091009

10101010
} else if (rt->dst_type == MCTP_ROUTE_GATEWAY) {

0 commit comments

Comments
 (0)