Commit d9d52a3e authored by Ziyang Xuan's avatar Ziyang Xuan Committed by Marc Kleine-Budde

can: j1939: j1939_netdev_start(): fix UAF for rx_kref of j1939_priv

It will trigger UAF for rx_kref of j1939_priv as following.

        cpu0                                    cpu1
j1939_sk_bind(socket0, ndev0, ...)
j1939_netdev_start
                                        j1939_sk_bind(socket1, ndev0, ...)
                                        j1939_netdev_start
j1939_priv_set
                                        j1939_priv_get_by_ndev_locked
j1939_jsk_add
.....
j1939_netdev_stop
kref_put_lock(&priv->rx_kref, ...)
                                        kref_get(&priv->rx_kref, ...)
                                        REFCOUNT_WARN("addition on 0;...")

====================================================
refcount_t: addition on 0; use-after-free.
WARNING: CPU: 1 PID: 20874 at lib/refcount.c:25 refcount_warn_saturate+0x169/0x1e0
RIP: 0010:refcount_warn_saturate+0x169/0x1e0
Call Trace:
 j1939_netdev_start+0x68b/0x920
 j1939_sk_bind+0x426/0xeb0
 ? security_socket_bind+0x83/0xb0

The rx_kref's kref_get() and kref_put() should use j1939_netdev_lock to
protect.

Fixes: 9d71dd0c ("can: add support of SAE J1939 protocol")
Link: https://lore.kernel.org/all/20210926104757.2021540-1-william.xuanziyang@huawei.com
Cc: stable@vger.kernel.org
Reported-by: syzbot+85d9878b19c94f9019ad@syzkaller.appspotmail.com
Signed-off-by: default avatarZiyang Xuan <william.xuanziyang@huawei.com>
Acked-by: default avatarOleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent b504a884
...@@ -249,11 +249,14 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) ...@@ -249,11 +249,14 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
struct j1939_priv *priv, *priv_new; struct j1939_priv *priv, *priv_new;
int ret; int ret;
priv = j1939_priv_get_by_ndev(ndev); spin_lock(&j1939_netdev_lock);
priv = j1939_priv_get_by_ndev_locked(ndev);
if (priv) { if (priv) {
kref_get(&priv->rx_kref); kref_get(&priv->rx_kref);
spin_unlock(&j1939_netdev_lock);
return priv; return priv;
} }
spin_unlock(&j1939_netdev_lock);
priv = j1939_priv_create(ndev); priv = j1939_priv_create(ndev);
if (!priv) if (!priv)
...@@ -269,10 +272,10 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) ...@@ -269,10 +272,10 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
/* Someone was faster than us, use their priv and roll /* Someone was faster than us, use their priv and roll
* back our's. * back our's.
*/ */
kref_get(&priv_new->rx_kref);
spin_unlock(&j1939_netdev_lock); spin_unlock(&j1939_netdev_lock);
dev_put(ndev); dev_put(ndev);
kfree(priv); kfree(priv);
kref_get(&priv_new->rx_kref);
return priv_new; return priv_new;
} }
j1939_priv_set(ndev, priv); j1939_priv_set(ndev, priv);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment