Commit 20bab8b
can: isotp: fix use-after-free race with concurrent NETDEV_UNREGISTER
isotp_release() looked up the bound network device via dev_get_by_index()
using the stored ifindex. During device unregistration the device is
unlisted from the ifindex hash before the NETDEV_UNREGISTER notifier
chain runs, so a concurrent isotp_release() could find no device, skip
can_rx_unregister() entirely, and still proceed to free the socket.
Since isotp_release() had already removed itself from the isotp
notifier list at that point, isotp_notify() would never get a chance to
clean up either, leaving a stale CAN filter that keeps pointing at the
freed socket.
Fix this the same way raw.c already does: hold a tracked reference to
the bound net_device in the socket (so->dev/so->dev_tracker) from
bind() onward instead of re-resolving it from the ifindex, and
serialize bind()/release() with rtnl_lock() so that so->dev is always
consistent with what the NETDEV_UNREGISTER notifier sees. so->dev
stays valid regardless of ifindex-hash unlisting, and is only ever
cleared by whichever of isotp_release()/isotp_notify() gets there
first, so the filter is always removed exactly once.
isotp_bind() now rejects a (re)bind with -EAGAIN while so->[tx|rx].state
isn't ISOTP_IDLE yet, so a timer left running by a prior
NETDEV_UNREGISTER can't act on a newly bound so->ifindex. Both checks
share the same lock_sock() section, so there is no window in which a
concurrent isotp_notify() clearing so->bound could be missed.
Fixes: e057dd3 ("can: add ISO 15765-2:2016 transport protocol")
Reported-by: sashiko-bot@kernel.org
Closes: https://lore.kernel.org/linux-can/20260707101420.47F261F000E9@smtp.kernel.org/
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20260712-isotp-fixes-v10-2-793a1b1ce17f@hartkopp.net
Cc: stable@kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>1 parent 9b1a02e commit 20bab8b
1 file changed
Lines changed: 59 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| 155 | + | |
| 156 | + | |
155 | 157 | | |
156 | 158 | | |
157 | 159 | | |
| |||
978 | 980 | | |
979 | 981 | | |
980 | 982 | | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
981 | 991 | | |
982 | 992 | | |
983 | 993 | | |
| |||
1219 | 1229 | | |
1220 | 1230 | | |
1221 | 1231 | | |
| 1232 | + | |
1222 | 1233 | | |
1223 | 1234 | | |
1224 | | - | |
1225 | | - | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | | - | |
1241 | | - | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
1242 | 1248 | | |
1243 | 1249 | | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
1244 | 1256 | | |
1245 | 1257 | | |
1246 | 1258 | | |
| |||
1253 | 1265 | | |
1254 | 1266 | | |
1255 | 1267 | | |
1256 | | - | |
1257 | | - | |
1258 | | - | |
1259 | 1268 | | |
1260 | 1269 | | |
1261 | 1270 | | |
| |||
1310 | 1319 | | |
1311 | 1320 | | |
1312 | 1321 | | |
| 1322 | + | |
1313 | 1323 | | |
1314 | 1324 | | |
1315 | 1325 | | |
1316 | 1326 | | |
1317 | 1327 | | |
1318 | 1328 | | |
1319 | 1329 | | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
1320 | 1341 | | |
1321 | 1342 | | |
1322 | 1343 | | |
| |||
1329 | 1350 | | |
1330 | 1351 | | |
1331 | 1352 | | |
1332 | | - | |
1333 | 1353 | | |
1334 | | - | |
| 1354 | + | |
1335 | 1355 | | |
1336 | 1356 | | |
1337 | | - | |
1338 | 1357 | | |
1339 | | - | |
| 1358 | + | |
1340 | 1359 | | |
1341 | 1360 | | |
1342 | 1361 | | |
| |||
1354 | 1373 | | |
1355 | 1374 | | |
1356 | 1375 | | |
1357 | | - | |
1358 | | - | |
1359 | 1376 | | |
1360 | 1377 | | |
1361 | 1378 | | |
1362 | 1379 | | |
1363 | 1380 | | |
1364 | 1381 | | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
1365 | 1392 | | |
1366 | 1393 | | |
| 1394 | + | |
1367 | 1395 | | |
1368 | 1396 | | |
1369 | 1397 | | |
| |||
1566 | 1594 | | |
1567 | 1595 | | |
1568 | 1596 | | |
1569 | | - | |
| 1597 | + | |
1570 | 1598 | | |
1571 | 1599 | | |
1572 | 1600 | | |
| |||
1582 | 1610 | | |
1583 | 1611 | | |
1584 | 1612 | | |
| 1613 | + | |
1585 | 1614 | | |
1586 | 1615 | | |
1587 | 1616 | | |
1588 | 1617 | | |
| 1618 | + | |
1589 | 1619 | | |
1590 | 1620 | | |
1591 | 1621 | | |
| |||
1645 | 1675 | | |
1646 | 1676 | | |
1647 | 1677 | | |
| 1678 | + | |
1648 | 1679 | | |
1649 | 1680 | | |
1650 | 1681 | | |
| |||
0 commit comments