Repository navigation
Commit 6c08fc8
Bluetooth: Fix refcount use-after-free issue
There is no lock preventing both l2cap_sock_release() and
chan->ops->close() from running at the same time.
If we consider Thread A running l2cap_chan_timeout() and Thread B running
l2cap_sock_release(), expected behavior is:
A::l2cap_chan_timeout()->l2cap_chan_close()->l2cap_sock_teardown_cb()
A::l2cap_chan_timeout()->l2cap_sock_close_cb()->l2cap_sock_kill()
B::l2cap_sock_release()->sock_orphan()
B::l2cap_sock_release()->l2cap_sock_kill()
where,
sock_orphan() clears "sk->sk_socket" and l2cap_sock_teardown_cb() marks
socket as SOCK_ZAPPED.
In l2cap_sock_kill(), there is an "if-statement" that checks if both
sock_orphan() and sock_teardown() has been run i.e. sk->sk_socket is NULL
and socket is marked as SOCK_ZAPPED. Socket is killed if the condition is
satisfied.
In the race condition, following occurs:
A::l2cap_chan_timeout()->l2cap_chan_close()->l2cap_sock_teardown_cb()
B::l2cap_sock_release()->sock_orphan()
B::l2cap_sock_release()->l2cap_sock_kill()
A::l2cap_chan_timeout()->l2cap_sock_close_cb()->l2cap_sock_kill()
In this scenario, "if-statement" is true in both B::l2cap_sock_kill() and
A::l2cap_sock_kill() and we hit "refcount: underflow; use-after-free" bug.
Similar condition occurs at other places where teardown/sock_kill is
happening:
l2cap_disconnect_rsp()->l2cap_chan_del()->l2cap_sock_teardown_cb()
l2cap_disconnect_rsp()->l2cap_sock_close_cb()->l2cap_sock_kill()
l2cap_conn_del()->l2cap_chan_del()->l2cap_sock_teardown_cb()
l2cap_conn_del()->l2cap_sock_close_cb()->l2cap_sock_kill()
l2cap_disconnect_req()->l2cap_chan_del()->l2cap_sock_teardown_cb()
l2cap_disconnect_req()->l2cap_sock_close_cb()->l2cap_sock_kill()
l2cap_sock_cleanup_listen()->l2cap_chan_close()->l2cap_sock_teardown_cb()
l2cap_sock_cleanup_listen()->l2cap_sock_kill()
Protect teardown/sock_kill and orphan/sock_kill by adding hold_lock on
l2cap channel to ensure that the socket is killed only after marked as
zapped and orphan.
Signed-off-by: Manish Mandlik <mmandlik@google.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>1 parent 151129d commit 6c08fc8
2 files changed
Lines changed: 28 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
422 | 425 | | |
423 | 426 | | |
424 | 427 | | |
| |||
431 | 434 | | |
432 | 435 | | |
433 | 436 | | |
434 | | - | |
435 | | - | |
436 | 437 | | |
437 | | - | |
438 | 438 | | |
| 439 | + | |
439 | 440 | | |
| 441 | + | |
| 442 | + | |
440 | 443 | | |
441 | 444 | | |
442 | 445 | | |
| |||
1737 | 1740 | | |
1738 | 1741 | | |
1739 | 1742 | | |
1740 | | - | |
1741 | | - | |
1742 | 1743 | | |
| 1744 | + | |
| 1745 | + | |
1743 | 1746 | | |
1744 | 1747 | | |
1745 | 1748 | | |
| |||
4405 | 4408 | | |
4406 | 4409 | | |
4407 | 4410 | | |
| 4411 | + | |
4408 | 4412 | | |
4409 | 4413 | | |
4410 | 4414 | | |
| |||
4413 | 4417 | | |
4414 | 4418 | | |
4415 | 4419 | | |
4416 | | - | |
4417 | 4420 | | |
4418 | 4421 | | |
4419 | | - | |
4420 | | - | |
4421 | 4422 | | |
| 4423 | + | |
| 4424 | + | |
4422 | 4425 | | |
4423 | 4426 | | |
4424 | 4427 | | |
| |||
4450 | 4453 | | |
4451 | 4454 | | |
4452 | 4455 | | |
| 4456 | + | |
4453 | 4457 | | |
4454 | 4458 | | |
4455 | 4459 | | |
4456 | 4460 | | |
| 4461 | + | |
4457 | 4462 | | |
4458 | 4463 | | |
4459 | 4464 | | |
4460 | 4465 | | |
4461 | | - | |
4462 | 4466 | | |
4463 | 4467 | | |
4464 | | - | |
4465 | | - | |
4466 | 4468 | | |
| 4469 | + | |
| 4470 | + | |
4467 | 4471 | | |
4468 | 4472 | | |
4469 | 4473 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1042 | 1042 | | |
1043 | 1043 | | |
1044 | 1044 | | |
1045 | | - | |
| 1045 | + | |
1046 | 1046 | | |
1047 | 1047 | | |
1048 | 1048 | | |
| |||
1203 | 1203 | | |
1204 | 1204 | | |
1205 | 1205 | | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
1206 | 1209 | | |
1207 | 1210 | | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
1208 | 1215 | | |
1209 | 1216 | | |
1210 | 1217 | | |
| |||
1222 | 1229 | | |
1223 | 1230 | | |
1224 | 1231 | | |
| 1232 | + | |
1225 | 1233 | | |
| 1234 | + | |
1226 | 1235 | | |
1227 | 1236 | | |
1228 | | - | |
1229 | | - | |
1230 | 1237 | | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
1231 | 1241 | | |
1232 | 1242 | | |
1233 | 1243 | | |
| |||
0 commit comments