net: mctp: hold route device before source address lookup
PATCH 1/1 net: mctp: hold route device before source address lookup
mctp_route_lookup() walks the route table under RCU and may find a direct route whose route-table reference to rt->dev is being removed concurrently by NETDEV_UNREGISTER.
Since commit 22cb45afd221 ("net: mctp: perform source address lookups when we populate our dst"), the direct-route lookup path reads the source address with mctp_dev_saddr(rt->dev) before mctp_dst_from_route() takes the dst's own mctp_dev reference. If device teardown wins that race, mctp_route_remove_dev() can delete the route and mctp_route_release() can drop the route's mctp_dev reference to zero. mctp_dev_put() frees mdev->addrs synchronously, while only the mctp_dev body is deferred with kfree_rcu().
That leaves the lookup side able to read freed mdev->addrs in mctp_dev_saddr(), or later increment a zero refcount in mctp_dev_hold().
Add a try-hold helper for mctp_dev and use it before reading the source address from a direct route. mctp_dst_from_route() now consumes that held reference when it populates dst. If no dst is requested, drop the temporary reference in mctp_route_lookup(). Do the same when the route cannot be used for a gateway path without a source address.
--- include/net/mctpdevice.h | 1 + net/mctp/device.c | 5 +++++ net/mctp/route.c | 28 ++++++++++++++++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-)
include/net/mctpdevice.h +1 -0
@@ -50,6 +50,7 @@ int mctp_register_netdev(struct net_device *dev,void mctp_unregister_netdev (struct net_device *dev);void mctp_dev_hold (struct mctp_dev *mdev); +bool mctp_dev_try_hold (struct mctp_dev *mdev);void mctp_dev_put (struct mctp_dev *mdev);void mctp_dev_set_key (struct mctp_dev *dev,struct mctp_sk_key *key);
net/mctp/device.c +5 -0
@@ -303,6 +303,11 @@ void mctp_dev_hold(struct mctp_dev *mdev)refcount_inc (&mdev->refs); } +bool mctp_dev_try_hold (struct mctp_dev *mdev) +{ +return mdev &&refcount_inc_not_zero (&mdev->refs); +} +void mctp_dev_put (struct mctp_dev *mdev) {if (mdev &&refcount_dec_and_test (&mdev->refs)) {
net/mctp/route.c +20 -8
@@ -897,15 +897,16 @@ static mctp_eid_t mctp_dev_saddr(struct mctp_dev *dev)return addr; } -/* must only be called on a direct route, as the final output hop */ +/* must only be called on a direct route, as the final output hop, with a +* reference already held on route->dev. +*/ static void mctp_dst_from_route (struct mctp_dst *dst,mctp_eid_t eid,mctp_eid_t saddr,unsigned int mtu, -struct mctp_route *route) +struct mctp_route *route,struct mctp_dev *dev) { -mctp_dev_hold (route->dev); dst->nexthop = eid; - dst->dev = route->dev; - dst->mtu =READ_ONCE (dst->dev->dev->mtu); + dst->dev = dev; + dst->mtu =READ_ONCE (dev->dev->mtu);if (mtu) dst->mtu =min (dst->mtu, mtu); dst->halen =0 ;
@@ -998,14 +999,25 @@ int mctp_route_lookup(struct net *net, unsigned int dnet, mtu = mtu ?: rt->mtu;if (rt->dst_type ==MCTP_ROUTE_DIRECT ) { -mctp_eid_t saddr =mctp_dev_saddr (rt->dev); +struct mctp_dev *dev = rt->dev; +mctp_eid_t saddr; + +if (!mctp_dev_try_hold (dev)) +break ; + + saddr =mctp_dev_saddr (dev);/* cannot do gateway-ed routes without a src */ -if (saddr ==MCTP_ADDR_NULL && depth !=0 ) +if (saddr ==MCTP_ADDR_NULL && depth !=0 ) { +mctp_dev_put (dev);break ; + }if (dst) -mctp_dst_from_route (dst, daddr, saddr, mtu, rt); +mctp_dst_from_route (dst, daddr, saddr, mtu, rt, + dev); +else +mctp_dev_put (dev); rc =0 ;break ;
Signature · 1 line
2.34.1
Jeremy Kerr
maintainerHi Yiqi Sun,
Quoted 13 lines
mctp_route_lookup()walks the route table under RCU and may find a direct route whose route-table reference tort->devis being removed concurrently byNETDEV_UNREGISTER.Since commit
22cb45afd221("net: mctp: perform source address lookups when we populate our dst"), the direct-route lookup path reads the source address withmctp_dev_saddr(rt->dev)beforemctp_dst_from_route()takes the dst's ownmctp_devreference. If device teardown wins that race,mctp_route_remove_dev()can delete the route andmctp_route_release()can drop the route'smctp_devreference to zero.mctp_dev_put()freesmdev->addrssynchronously, while only themctp_devbody is deferred withkfree_rcu().
Quoted 8 lines
That leaves the lookup side able to read freed
mdev->addrsinmctp_dev_saddr(), or later increment a zero refcount inmctp_dev_hold().Add a try-hold helper for
mctp_devand use it before reading the source address from a direct route.mctp_dst_from_route()now consumes that held reference when it populates dst. If no dst is requested, drop the temporary reference inmctp_route_lookup(). Do the same when the route cannot be used for a gateway path without a source address.
Good find, thank you. Just a couple of comments on the fix though:
Quoted 22 lines
index
1f3dccbb7aed..3d0737030917100644net/mctp/route.c +0
-0@@ -897,15 +897,16 @@ static mctp_eid_t mctp_dev_saddr(struct mctp_dev *dev) return addr; }net/mctp/route.c +3
-1-/* must only be called on a direct route, as the final output hop */ +/* must only be called on a direct route, as the final output hop, with a +* reference already held on route->dev. +*/ static void mctp_dst_from_route(struct
mctp_dst*dst,mctp_eid_teid,mctp_eid_tsaddr, unsigned int mtu,net/mctp/route.c +1
-1- struct mctp_route *route) + struct mctp_route *route, struct mctp_dev *dev){
net/mctp/route.c +0
-1-mctp_dev_hold (route->dev);
dst->nexthop= eid;net/mctp/route.c +2
-2- dst->dev = route->dev; - dst->mtu =READ_ONCE (dst->dev->dev->mtu); + dst->dev = dev; + dst->mtu =READ_ONCE (dev->dev->mtu);
Quoted 3 lines
if (mtu)
dst->mtu=min(dst->mtu, mtu);
dst->halen= 0;
I don't see the need to pass the dev argument separately here, it's duplicating route->dev; all we need is to drop the mctp_dev_hold(), and update the comment, as you have done.
net/mctp/route.c int mctp_route_lookup(struct net *net, unsigned int dnet, · line 1006
Quoted 10 lines
net/mctp/route.c +0
-0@@ -998,14 +999,25 @@ int mctp_route_lookup(struct net *net, unsigned int dnet, mtu = mtu ?: rt->mtu;
if (rt->dst_type==MCTP_ROUTE_DIRECT) {net/mctp/route.c +5
-1-mctp_eid_t saddr =mctp_dev_saddr (rt->dev); +struct mctp_dev *dev = rt->dev; +mctp_eid_t saddr; + +if (!mctp_dev_try_hold (dev)) +break ;
I would also be fine with using refcount_inc_not_zero() directly, but adding the helper is good too.
Quoted 16 lines
net/mctp/route.c +2
-0+ + saddr =mctp_dev_saddr (dev);
/* cannot do gateway-ed routes without a src */net/mctp/route.c +2
-1-if (saddr ==MCTP_ADDR_NULL && depth !=0 ) +if (saddr ==MCTP_ADDR_NULL && depth !=0 ) { +mctp_dev_put (dev);break;
net/mctp/route.c +1
-0+ }
if (dst)net/mctp/route.c +4
-1-mctp_dst_from_route (dst, daddr, saddr, mtu, rt); +mctp_dst_from_route (dst, daddr, saddr, mtu, rt, + dev); + else +mctp_dev_put (dev );
Quoted 2 lines
rc = 0;
break;
If you end up doing a v2, don't forget the [PATH net] subject prefix, to indicate the net (vs. net-next) tree.
Cheers,
Jeremy