Commit 43884dc
can: isotp: fix use-after-free race with concurrent NETDEV_UNREGISTER
commit 20bab8b upstream.
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>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>1 parent 3ff8c24 commit 43884dc
1 file changed
Lines changed: 59 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
154 | 155 | | |
155 | 156 | | |
156 | 157 | | |
| |||
964 | 965 | | |
965 | 966 | | |
966 | 967 | | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
967 | 976 | | |
968 | 977 | | |
969 | 978 | | |
| |||
1199 | 1208 | | |
1200 | 1209 | | |
1201 | 1210 | | |
| 1211 | + | |
1202 | 1212 | | |
1203 | 1213 | | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
1210 | | - | |
1211 | | - | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
1215 | | - | |
1216 | | - | |
1217 | | - | |
1218 | | - | |
1219 | | - | |
1220 | | - | |
1221 | | - | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
1222 | 1227 | | |
1223 | 1228 | | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
1224 | 1235 | | |
1225 | 1236 | | |
1226 | 1237 | | |
| |||
1233 | 1244 | | |
1234 | 1245 | | |
1235 | 1246 | | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | 1247 | | |
1240 | 1248 | | |
1241 | 1249 | | |
| |||
1289 | 1297 | | |
1290 | 1298 | | |
1291 | 1299 | | |
| 1300 | + | |
1292 | 1301 | | |
1293 | 1302 | | |
1294 | 1303 | | |
1295 | 1304 | | |
1296 | 1305 | | |
1297 | 1306 | | |
1298 | 1307 | | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
1299 | 1319 | | |
1300 | 1320 | | |
1301 | 1321 | | |
| |||
1308 | 1328 | | |
1309 | 1329 | | |
1310 | 1330 | | |
1311 | | - | |
1312 | 1331 | | |
1313 | | - | |
| 1332 | + | |
1314 | 1333 | | |
1315 | | - | |
1316 | | - | |
| 1334 | + | |
1317 | 1335 | | |
1318 | | - | |
| 1336 | + | |
1319 | 1337 | | |
1320 | 1338 | | |
1321 | 1339 | | |
| |||
1333 | 1351 | | |
1334 | 1352 | | |
1335 | 1353 | | |
1336 | | - | |
1337 | | - | |
1338 | 1354 | | |
1339 | 1355 | | |
1340 | 1356 | | |
1341 | 1357 | | |
1342 | 1358 | | |
1343 | 1359 | | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
1344 | 1370 | | |
1345 | 1371 | | |
| 1372 | + | |
1346 | 1373 | | |
1347 | 1374 | | |
1348 | 1375 | | |
| |||
1545 | 1572 | | |
1546 | 1573 | | |
1547 | 1574 | | |
1548 | | - | |
| 1575 | + | |
1549 | 1576 | | |
1550 | 1577 | | |
1551 | 1578 | | |
| |||
1561 | 1588 | | |
1562 | 1589 | | |
1563 | 1590 | | |
| 1591 | + | |
1564 | 1592 | | |
1565 | 1593 | | |
1566 | 1594 | | |
1567 | 1595 | | |
| 1596 | + | |
1568 | 1597 | | |
1569 | 1598 | | |
1570 | 1599 | | |
| |||
1624 | 1653 | | |
1625 | 1654 | | |
1626 | 1655 | | |
| 1656 | + | |
1627 | 1657 | | |
1628 | 1658 | | |
1629 | 1659 | | |
| |||
0 commit comments