Commit bc55cfd
committed
ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock
syzbot caught a potential deadlock between the PCM
runtime->buffer_mutex and the mm->mmap_lock. It was brought by the
recent fix to cover the racy read/write and other ioctls, and in that
commit, I overlooked a (hopefully only) corner case that may take the
revert lock, namely, the OSS mmap. The OSS mmap operation
exceptionally allows to re-configure the parameters inside the OSS
mmap syscall, where mm->mmap_mutex is already held. Meanwhile, the
copy_from/to_user calls at read/write operations also take the
mm->mmap_lock internally, hence it may lead to a AB/BA deadlock.
A similar problem was already seen in the past and we fixed it with a
refcount (in commit b248371). The former fix covered only the
call paths with OSS read/write and OSS ioctls, while we need to cover
the concurrent access via both ALSA and OSS APIs now.
This patch addresses the problem above by replacing the buffer_mutex
lock in the read/write operations with a refcount similar as we've
used for OSS. The new field, runtime->buffer_accessing, keeps the
number of concurrent read/write operations. Unlike the former
buffer_mutex protection, this protects only around the
copy_from/to_user() calls; the other codes are basically protected by
the PCM stream lock. The refcount can be a negative, meaning blocked
by the ioctls. If a negative value is seen, the read/write aborts
with -EBUSY. In the ioctl side, OTOH, they check this refcount, too,
and set to a negative value for blocking unless it's already being
accessed.
Reported-by: syzbot+6e5c88838328e99c7e1c@syzkaller.appspotmail.com
Fixes: dca947d ("ALSA: pcm: Fix races among concurrent read/write and buffer changes")
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/000000000000381a0d05db622a81@google.com
Link: https://lore.kernel.org/r/20220330120903.4738-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>1 parent 21b5954 commit bc55cfd
4 files changed
Lines changed: 39 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
| 405 | + | |
405 | 406 | | |
406 | 407 | | |
407 | 408 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
970 | 970 | | |
971 | 971 | | |
972 | 972 | | |
| 973 | + | |
973 | 974 | | |
974 | 975 | | |
975 | 976 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1906 | 1906 | | |
1907 | 1907 | | |
1908 | 1908 | | |
1909 | | - | |
1910 | 1909 | | |
1911 | 1910 | | |
1912 | 1911 | | |
1913 | | - | |
1914 | 1912 | | |
1915 | 1913 | | |
1916 | 1914 | | |
| |||
2221 | 2219 | | |
2222 | 2220 | | |
2223 | 2221 | | |
2224 | | - | |
2225 | 2222 | | |
2226 | 2223 | | |
2227 | 2224 | | |
| |||
2276 | 2273 | | |
2277 | 2274 | | |
2278 | 2275 | | |
| 2276 | + | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
2279 | 2280 | | |
2280 | 2281 | | |
2281 | 2282 | | |
| |||
2284 | 2285 | | |
2285 | 2286 | | |
2286 | 2287 | | |
| 2288 | + | |
2287 | 2289 | | |
2288 | 2290 | | |
2289 | 2291 | | |
| |||
2313 | 2315 | | |
2314 | 2316 | | |
2315 | 2317 | | |
2316 | | - | |
2317 | 2318 | | |
2318 | 2319 | | |
2319 | 2320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
685 | 685 | | |
686 | 686 | | |
687 | 687 | | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
688 | 706 | | |
689 | 707 | | |
690 | 708 | | |
| |||
695 | 713 | | |
696 | 714 | | |
697 | 715 | | |
698 | | - | |
| 716 | + | |
699 | 717 | | |
700 | 718 | | |
701 | 719 | | |
702 | 720 | | |
703 | 721 | | |
704 | 722 | | |
705 | | - | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
706 | 726 | | |
707 | 727 | | |
708 | 728 | | |
| |||
820 | 840 | | |
821 | 841 | | |
822 | 842 | | |
823 | | - | |
| 843 | + | |
824 | 844 | | |
825 | 845 | | |
826 | 846 | | |
| |||
865 | 885 | | |
866 | 886 | | |
867 | 887 | | |
868 | | - | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
869 | 891 | | |
870 | 892 | | |
871 | 893 | | |
| |||
884 | 906 | | |
885 | 907 | | |
886 | 908 | | |
887 | | - | |
| 909 | + | |
888 | 910 | | |
889 | 911 | | |
890 | 912 | | |
| |||
1369 | 1391 | | |
1370 | 1392 | | |
1371 | 1393 | | |
1372 | | - | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
1373 | 1397 | | |
1374 | 1398 | | |
1375 | 1399 | | |
1376 | 1400 | | |
1377 | | - | |
| 1401 | + | |
| 1402 | + | |
1378 | 1403 | | |
1379 | 1404 | | |
1380 | 1405 | | |
| |||
0 commit comments