appletalk: fix use-after-free in atalk_sendmsg due to route lookup race
Subsystems (2)
PATCH 1/1 appletalk: fix use-after-free in atalk_sendmsg due to route lookup race
atrtr_find() returns a raw pointer after releasing atalk_routes_lock. In atalk_sendmsg(), both the primary route (rt) and loopback route (rt_lo) pointers are held across sock_alloc_send_skb() which can sleep. Concurrent atrtr_delete() or atrtr_device_down() can kfree() the route during this window, causing UAF when rt->flags, rt->gateway, or rt_lo->dev are accessed.
Fix by deferring route frees with kfree_rcu() and caching all needed route fields under rcu_read_lock() before the sleeping allocation. Take dev_hold() on both route devices to prevent disappearance after the route's dev_put() in the delete path.
--- include/linux/atalk.h | 1 + net/appletalk/ddp.c | 52 +++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 19 deletions(-)
include/linux/atalk.h +1 -0
@@ -12,6 +12,7 @@ struct atalk_route {struct atalk_addr gateway;int flags;struct atalk_route *next; +struct rcu_head rcu; };/**
net/appletalk/ddp.c +33 -19
124 lines not on this page · raw message
Signature · 1 line
2.50.1 (Apple Git-155)
Simon Horman
reviewerFrom: 'Simon Horman' <[email protected]>
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
Subject: appletalk: fix use-after-free in atalk_sendmsg due to route lookup race
This patch attempts to fix a use-after-free in atalk_sendmsg() by deferring route deletion with kfree_rcu() and caching required route fields under rcu_read_lock(). It also takes a reference on the route devices using dev_hold() to prevent them from disappearing during the sleeping allocation.
net/appletalk/ddp.c static int atrtr_delete(struct atalk_addr *addr) · line 606
Quoted 10 lines
net/appletalk/ddp.c +1
-1@@ -603,7 +603,7 @@ static int atrtr_delete(struct atalk_addr *addr) tmp->target.s_node == addr->s_node)) { *r = tmp->next;dev_put (tmp->dev); -kfree (tmp); +kfree_rcu (tmp, rcu);
Simon says: while the issue below is flagged as not being introduced by
this patch, I think that if it is problem then it prevents this patch from working as intended.
So if it is an issue I suggest addressing it in the context of this patch, perhaps by adding another patch to this patch-set.
[Severity: High]
This isn't a bug introduced by this patch, but does the synchronous dev_put() allow the network device reference count to drop to zero while RCU readers are active?
While kfree_rcu() defers freeing the route, dev_put(tmp->dev) is still called synchronously here. If a device is being unregistered and this synchronous dev_put() removes the last reference, the device's refcount could drop to zero before the RCU grace period ends.
When a concurrent atalk_sendmsg() holding rcu_read_lock() then calls dev_hold() on this device, could it increment a zero refcount and cause a refcount saturation warning or use-after-free?
[ ... ]
net/appletalk/ddp.c static void atrtr_device_down(struct net_device *dev) · line 631
Quoted 6 lines
net/appletalk/ddp.c +1
-1@@ -628,7 +628,7 @@ static void atrtr_device_down(struct net_device *dev)if (tmp->dev == dev) { *r = tmp->next;dev_put (dev); -kfree (tmp); +kfree_rcu (tmp, rcu);
Simon says: likewise for the issue below.
[Severity: High]
This is a pre-existing issue, but this appears to have the same synchronous dev_put() problem as atrtr_delete() above.
[ ... ]
net/appletalk/ddp.c static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) · line 1608
Quoted 7 lines
net/appletalk/ddp.c +1
-0@@ -1600,6 +1603,7 @@ static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)/* For headers */ size =sizeof (struct ddpehdr ) + len + ddp_dl->header_length; +rcu_read_lock ();if (usat->sat_addr.s_net || usat->sat_addr.s_node ==ATADDR_ANYNODE ) { rt =atrtr_find (&usat->sat_addr);
Simon says: and the issue below too.
[Severity: High]
This isn't a bug introduced by this patch, but are there other callers of atrtr_find() that are now missing rcu_read_lock() protection?
atrtr_get_dev() calls atrtr_find(), which temporarily takes atalk_routes_lock and then returns a raw pointer:
net/appletalk/ddp.c:atrtr_get_dev() {
struct atalk_route *atr = atrtr_find(sa);
return atr ? atr->dev : NULL;
}Since atrtr_get_dev() does not hold rcu_read_lock() when dereferencing atr->dev, and this patch uses kfree_rcu() to defer route deletion, could a concurrent route deletion free the route before the read occurs?
Quoted 8 lines
net/appletalk/ddp.c +1
-2}else {@@ -1610,29 +1614,38 @@ static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) rt =atrtr_find (&at_hint); } - err = -ENETUNREACH ; -if (!rt) +if (!rt) {
net/appletalk/ddp.c static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) · line 1624
Quoted 10 lines
net/appletalk/ddp.c +4
-3+rcu_read_unlock (); + err = -ENETUNREACH ; goto out; + } dev = rt->dev; - -net_dbg_ratelimited ("SK %p: Size needed %d, device %s\n" , - sk, size, dev->name); +dev_hold (dev);
Simon says: on the other hand, this one looks like it is orthogonal to
this patch and should be considered in the context of possible follow-up.
[Severity: Critical]
This is a pre-existing issue, but is there a data race on atrtr_default.dev that causes a NULL pointer dereference here?
atrtr_device_down() sets the static atrtr_default.dev locklessly to NULL:
net/appletalk/ddp.c:atrtr_device_down() {
...
if (atrtr_default.dev == dev) atrtr_set_default(NULL); }
If atrtr_find() locklessly returns &atrtr_default, and atrtr_device_down() clears the default route between the lookup and dev = rt->dev, could this read a NULL pointer?
Since dev_hold(dev) does not check for NULL, wouldn't it attempt to increment the refcount at a NULL offset and cause a kernel crash?
Simon says: While this one seems to warrant further investigation in
the context of this patch.
[Severity: High]
This isn't a bug introduced by this patch, but does the lack of reference counting on the default route break the newly added RCU assumptions?
The static atrtr_default route does not hold a reference count on its network device (no dev_hold() is performed when atrtr_set_default() sets it). Consequently, when the device is unregistered, its refcount can drop to zero immediately.
If atalk_sendmsg() retrieves this route and calls dev_hold() inside its RCU critical section, could it increment a zero refcount? This would trigger the same refcount saturation and use-after-free issues as the synchronous dev_put() described earlier.
Quoted 4 lines
net/appletalk/ddp.c +2
-0+ rt_flags = rt->flags; + rt_gateway = rt->gateway; hard_header_len = dev->hard_header_len;