nfp: fix use-after-free in nfp_ccm_mbox_skb_return()
Subsystems (4)
PATCH 1/1 nfp: fix use-after-free in nfp_ccm_mbox_skb_return()
nfp_ccm_mbox_skb_return() casts skb->cb to struct nfp_ccm_mbox_cmsg_cb, frees skb via dev_kfree_skb_any() when cb->err is non-zero, and then reads cb->err from the freed skb to return it.
Read cb->err into a local variable before freeing skb.
This issue was found while trying out new Clang static analysis checks for kernel bug classes. The finding was triaged and fixed, and this commit description was drafted with the help of AI, then verified by code inspection and compile-tested against linux-next (not reproduced and not runtime-tested on hardware).
--- drivers/net/ethernet/netronome/nfp/ccm_mbox.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/netronome/nfp/ccm_mbox.c +3 -2
@@ -420,10 +420,11 @@ static void nfp_ccm_mbox_run_queue_unlock(struct nfp_net *nn)static int nfp_ccm_mbox_skb_return (struct sk_buff *skb) {struct nfp_ccm_mbox_cmsg_cb *cb = (void *)skb->cb; +int err = cb->err; -if (cb->err) +if (err)dev_kfree_skb_any (skb); -return cb->err; +return err; }/* If wait timed out but the command is already in progress we have
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 1 · High: 0 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Critical] The patch is incomplete.
--
nfp: fix use-after-free in nfp_ccm_mbox_skb_return()
This commit changes nfp_ccm_mbox_skb_return() to save cb->err in a local variable before the skb is freed. Before the change, cb->err was read from skb->cb after dev_kfree_skb_any() had already run.
Quoted 6 lines
drivers/net/ethernet/netronome/nfp/ccm_mbox.c +0
-0@@ -420,10 +420,11 @@ static void nfp_ccm_mbox_run_queue_unlock(struct nfp_net *nn)static int nfp_ccm_mbox_skb_return (struct sk_buff *skb)
drivers/net/ethernet/netronome/nfp/ccm_mbox.c static void nfp_ccm_mbox_run_queue_unlock(struct nfp_net *nn) · line 428
Quoted 10 lines
drivers/net/ethernet/netronome/nfp/ccm_mbox.c +3
-2{struct nfp_ccm_mbox_cmsg_cb *cb = (void *)skb->cb; +int err = cb->err; -if (cb->err) +if (err)dev_kfree_skb_any (skb); -return cb->err; +return err; }
[Severity: Critical]
Is this fix complete? This change makes a non-zero return from nfp_ccm_mbox_skb_return() mean the skb has already been freed. One caller still frees the skb again after that return.
The in-progress branch of nfp_ccm_mbox_unlink_unlock() passes the helper's result straight back:
drivers/net/ethernet/netronome/nfp/ccm_mbox.c:nfp_ccm_mbox_unlink_unlock() {
...
if (nfp_ccm_mbox_in_progress(skb)) {
spin_unlock_bh(&nn->mbox_cmsg.queue.lock);
wait_event(nn->mbox_cmsg.wq, nfp_ccm_mbox_done(skb));
smp_rmb(); /* pairs with smp_wmb() after data is written */
return nfp_ccm_mbox_skb_return(skb);
}
...
return -ETIMEDOUT;
}__nfp_ccm_mbox_communicate() treats every non-zero return as "the caller still owns the skb":
drivers/net/ethernet/netronome/nfp/ccm_mbox.c:__nfp_ccm_mbox_communicate() {
...
err = nfp_ccm_mbox_unlink_unlock(nn, skb, type);
if (err)
goto err_free_skb;
return 0;
...
err_free_skb:
dev_kfree_skb_any(skb);
return err;
}Only the -ETIMEDOUT branch, which unlinks the skb without freeing it, matches what the caller expects.
Here is one sequence that seems to reach the double free:
1. A thread that is not first in the queue waits in wait_event_timeout() with NFP_CCM_TIMEOUT.
2. Another thread's nfp_ccm_mbox_run_queue_unlock() adds that skb to its batch and marks it busy with nfp_ccm_mbox_set_busy().
3. The waiter times out before the skb is done. It is still not first, so it calls nfp_ccm_mbox_unlink_unlock(). nfp_ccm_mbox_in_progress() is true, so it waits for done.
4. The batch fails in one of two ways:
- nfp_net_mbox_reconfig() fails and nfp_ccm_mbox_mark_all_err() sets
-EIO.
- nfp_ccm_mbox_copy_out() sets cb->err to -EIO or -ENOENT.
5. nfp_ccm_mbox_skb_return() frees the skb and returns the error.
6. __nfp_ccm_mbox_communicate() jumps to err_free_skb and calls dev_kfree_skb_any() on the same skb a second time.
The commit message says this fixes the free-then-use of the skb around nfp_ccm_mbox_skb_return(), but that path is still reachable here.
Should the skb be freed in exactly one place? Either the in-progress branch could leave the skb alone, or the caller could return the error directly instead of going to err_free_skb.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125221658.1420387-1-elkin%40google.com