Skip to content

Commit a1bfb6c

Browse files
committed
can: j1939: implement NETDEV_UNREGISTER notification handler
jira KERNEL-318 cve CVE-2025-39925 Rebuild_History Non-Buildable kernel-6.12.0-124.20.1.el10_1 commit-author Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> commit 7fcbe5b syzbot is reporting unregister_netdevice: waiting for vcan0 to become free. Usage count = 2 problem, for j1939 protocol did not have NETDEV_UNREGISTER notification handler for undoing changes made by j1939_sk_bind(). Commit 25fe97c ("can: j1939: move j1939_priv_put() into sk_destruct callback") expects that a call to j1939_priv_put() can be unconditionally delayed until j1939_sk_sock_destruct() is called. But we need to call j1939_priv_put() against an extra ref held by j1939_sk_bind() call (as a part of undoing changes made by j1939_sk_bind()) as soon as NETDEV_UNREGISTER notification fires (i.e. before j1939_sk_sock_destruct() is called via j1939_sk_release()). Otherwise, the extra ref on "struct j1939_priv" held by j1939_sk_bind() call prevents "struct net_device" from dropping the usage count to 1; making it impossible for unregister_netdevice() to continue. Reported-by: syzbot <syzbot+881d65229ca4f9ae8c84@syzkaller.appspotmail.com> Closes: https://syzkaller.appspot.com/bug?extid=881d65229ca4f9ae8c84 Tested-by: syzbot <syzbot+881d65229ca4f9ae8c84@syzkaller.appspotmail.com> Fixes: 9d71dd0 ("can: add support of SAE J1939 protocol") Fixes: 25fe97c ("can: j1939: move j1939_priv_put() into sk_destruct callback") Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Tested-by: Oleksij Rempel <o.rempel@pengutronix.de> Acked-by: Oleksij Rempel <o.rempel@pengutronix.de> Link: https://patch.msgid.link/ac9db9a4-6c30-416e-8b94-96e6559d55b2@I-love.SAKURA.ne.jp [mkl: remove space in front of label] Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> (cherry picked from commit 7fcbe5b) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent a0230fb commit a1bfb6c

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

net/can/j1939/j1939-priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ void j1939_priv_get(struct j1939_priv *priv);
212212

213213
/* notify/alert all j1939 sockets bound to ifindex */
214214
void j1939_sk_netdev_event_netdown(struct j1939_priv *priv);
215+
void j1939_sk_netdev_event_unregister(struct j1939_priv *priv);
215216
int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk);
216217
void j1939_tp_init(struct j1939_priv *priv);
217218

net/can/j1939/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ static int j1939_netdev_notify(struct notifier_block *nb,
377377
j1939_sk_netdev_event_netdown(priv);
378378
j1939_ecu_unmap_all(priv);
379379
break;
380+
case NETDEV_UNREGISTER:
381+
j1939_sk_netdev_event_unregister(priv);
382+
break;
380383
}
381384

382385
j1939_priv_put(priv);

net/can/j1939/socket.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,55 @@ void j1939_sk_netdev_event_netdown(struct j1939_priv *priv)
12981298
read_unlock_bh(&priv->j1939_socks_lock);
12991299
}
13001300

1301+
void j1939_sk_netdev_event_unregister(struct j1939_priv *priv)
1302+
{
1303+
struct sock *sk;
1304+
struct j1939_sock *jsk;
1305+
bool wait_rcu = false;
1306+
1307+
rescan: /* The caller is holding a ref on this "priv" via j1939_priv_get_by_ndev(). */
1308+
read_lock_bh(&priv->j1939_socks_lock);
1309+
list_for_each_entry(jsk, &priv->j1939_socks, list) {
1310+
/* Skip if j1939_jsk_add() is not called on this socket. */
1311+
if (!(jsk->state & J1939_SOCK_BOUND))
1312+
continue;
1313+
sk = &jsk->sk;
1314+
sock_hold(sk);
1315+
read_unlock_bh(&priv->j1939_socks_lock);
1316+
/* Check if j1939_jsk_del() is not yet called on this socket after holding
1317+
* socket's lock, for both j1939_sk_bind() and j1939_sk_release() call
1318+
* j1939_jsk_del() with socket's lock held.
1319+
*/
1320+
lock_sock(sk);
1321+
if (jsk->state & J1939_SOCK_BOUND) {
1322+
/* Neither j1939_sk_bind() nor j1939_sk_release() called j1939_jsk_del().
1323+
* Make this socket no longer bound, by pretending as if j1939_sk_bind()
1324+
* dropped old references but did not get new references.
1325+
*/
1326+
j1939_jsk_del(priv, jsk);
1327+
j1939_local_ecu_put(priv, jsk->addr.src_name, jsk->addr.sa);
1328+
j1939_netdev_stop(priv);
1329+
/* Call j1939_priv_put() now and prevent j1939_sk_sock_destruct() from
1330+
* calling the corresponding j1939_priv_put().
1331+
*
1332+
* j1939_sk_sock_destruct() is supposed to call j1939_priv_put() after
1333+
* an RCU grace period. But since the caller is holding a ref on this
1334+
* "priv", we can defer synchronize_rcu() until immediately before
1335+
* the caller calls j1939_priv_put().
1336+
*/
1337+
j1939_priv_put(priv);
1338+
jsk->priv = NULL;
1339+
wait_rcu = true;
1340+
}
1341+
release_sock(sk);
1342+
sock_put(sk);
1343+
goto rescan;
1344+
}
1345+
read_unlock_bh(&priv->j1939_socks_lock);
1346+
if (wait_rcu)
1347+
synchronize_rcu();
1348+
}
1349+
13011350
static int j1939_sk_no_ioctlcmd(struct socket *sock, unsigned int cmd,
13021351
unsigned long arg)
13031352
{

0 commit comments

Comments
 (0)