Commit ee8b94c
can: raw: fix receiver memory leak
Got kmemleak errors with the following ltp can_filter testcase:
for ((i=1; i<=100; i++))
do
./can_filter &
sleep 0.1
done
==============================================================
[<00000000db4a4943>] can_rx_register+0x147/0x360 [can]
[<00000000a289549d>] raw_setsockopt+0x5ef/0x853 [can_raw]
[<000000006d3d9ebd>] __sys_setsockopt+0x173/0x2c0
[<00000000407dbfec>] __x64_sys_setsockopt+0x61/0x70
[<00000000fd468496>] do_syscall_64+0x33/0x40
[<00000000b7e47d51>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
It's a bug in the concurrent scenario of unregister_netdevice_many()
and raw_release() as following:
cpu0 cpu1
unregister_netdevice_many(can_dev)
unlist_netdevice(can_dev) // dev_get_by_index() return NULL after this
net_set_todo(can_dev)
raw_release(can_socket)
dev = dev_get_by_index(, ro->ifindex); // dev == NULL
if (dev) { // receivers in dev_rcv_lists not free because dev is NULL
raw_disable_allfilters(, dev, );
dev_put(dev);
}
...
ro->bound = 0;
...
call_netdevice_notifiers(NETDEV_UNREGISTER, )
raw_notify(, NETDEV_UNREGISTER, )
if (ro->bound) // invalid because ro->bound has been set 0
raw_disable_allfilters(, dev, ); // receivers in dev_rcv_lists will never be freed
Add a net_device pointer member in struct raw_sock to record bound
can_dev, and use rtnl_lock to serialize raw_socket members between
raw_bind(), raw_release(), raw_setsockopt() and raw_notify(). Use
ro->dev to decide whether to free receivers in dev_rcv_lists.
Fixes: 8d0caed ("can: bcm/raw/isotp: use per module netdevice notifier")
Reviewed-by: Oliver Hartkopp <socketcan@hartkopp.net>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Ziyang Xuan <william.xuanziyang@huawei.com>
Link: https://lore.kernel.org/all/20230711011737.1969582-1-william.xuanziyang@huawei.com
Cc: stable@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>1 parent 0dd1805 commit ee8b94c
1 file changed
+24
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
| |||
277 | 278 | | |
278 | 279 | | |
279 | 280 | | |
280 | | - | |
| 281 | + | |
281 | 282 | | |
282 | 283 | | |
283 | 284 | | |
| |||
292 | 293 | | |
293 | 294 | | |
294 | 295 | | |
| 296 | + | |
295 | 297 | | |
296 | 298 | | |
297 | 299 | | |
| |||
337 | 339 | | |
338 | 340 | | |
339 | 341 | | |
| 342 | + | |
340 | 343 | | |
341 | 344 | | |
342 | 345 | | |
| |||
385 | 388 | | |
386 | 389 | | |
387 | 390 | | |
| 391 | + | |
388 | 392 | | |
389 | 393 | | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
399 | 397 | | |
400 | | - | |
401 | 398 | | |
402 | 399 | | |
403 | 400 | | |
404 | 401 | | |
405 | 402 | | |
406 | 403 | | |
407 | 404 | | |
| 405 | + | |
408 | 406 | | |
409 | 407 | | |
| 408 | + | |
410 | 409 | | |
411 | 410 | | |
412 | 411 | | |
| |||
422 | 421 | | |
423 | 422 | | |
424 | 423 | | |
| 424 | + | |
425 | 425 | | |
426 | 426 | | |
427 | 427 | | |
| |||
431 | 431 | | |
432 | 432 | | |
433 | 433 | | |
| 434 | + | |
434 | 435 | | |
435 | 436 | | |
436 | 437 | | |
437 | 438 | | |
438 | 439 | | |
439 | 440 | | |
440 | | - | |
441 | | - | |
442 | 441 | | |
443 | 442 | | |
444 | 443 | | |
| |||
467 | 466 | | |
468 | 467 | | |
469 | 468 | | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
481 | 473 | | |
482 | | - | |
483 | 474 | | |
484 | 475 | | |
485 | 476 | | |
| 477 | + | |
486 | 478 | | |
487 | 479 | | |
488 | 480 | | |
489 | 481 | | |
| 482 | + | |
490 | 483 | | |
491 | 484 | | |
492 | 485 | | |
| |||
553 | 546 | | |
554 | 547 | | |
555 | 548 | | |
556 | | - | |
557 | | - | |
558 | | - | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
559 | 552 | | |
560 | 553 | | |
561 | 554 | | |
| |||
596 | 589 | | |
597 | 590 | | |
598 | 591 | | |
599 | | - | |
600 | 592 | | |
601 | 593 | | |
602 | 594 | | |
| |||
614 | 606 | | |
615 | 607 | | |
616 | 608 | | |
617 | | - | |
618 | | - | |
619 | | - | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
620 | 612 | | |
621 | 613 | | |
622 | 614 | | |
| |||
640 | 632 | | |
641 | 633 | | |
642 | 634 | | |
643 | | - | |
644 | 635 | | |
645 | 636 | | |
646 | 637 | | |
| |||
0 commit comments