diff options
author | Xiaoming Ni <nixiaoming@huawei.com> | 2022-11-15 14:54:05 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2022-11-15 14:54:05 +0100 |
commit | cb8d2ea8c91b5671b05e06ab2282496104453378 (patch) | |
tree | 10cb02d9620037a7195728c985ec2d1ded1184e4 | |
parent | 707a7ef4c72d1d00ff61221511a70eada19185ca (diff) | |
download | busybox-w32-cb8d2ea8c91b5671b05e06ab2282496104453378.tar.gz busybox-w32-cb8d2ea8c91b5671b05e06ab2282496104453378.tar.bz2 busybox-w32-cb8d2ea8c91b5671b05e06ab2282496104453378.zip |
loop: fix a race when a free loop device is snatched
When /dev/loop-control exists and *device is empty,
the mount may fail if a concurrent mount is running.
function old new delta
set_loop 809 807 -2
Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/loop.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/libbb/loop.c b/libbb/loop.c index cb8fa2442..750642ade 100644 --- a/libbb/loop.c +++ b/libbb/loop.c | |||
@@ -218,8 +218,17 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse | |||
218 | } | 218 | } |
219 | /* failure, undo LOOP_SET_FD */ | 219 | /* failure, undo LOOP_SET_FD */ |
220 | ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary | 220 | ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary |
221 | } else { | ||
222 | /* device is not free (rc == 0), or error other than ENXIO */ | ||
223 | if (rc == 0 /* device is not free? */ | ||
224 | && !*device /* racing with other mount? */ | ||
225 | && try != dev /* tried a _kernel-offered_ loopN? */ | ||
226 | ) { | ||
227 | free(try); | ||
228 | close(lfd); | ||
229 | goto get_free_loopN; | ||
230 | } | ||
221 | } | 231 | } |
222 | /* else: device is not free (rc == 0) or error other than ENXIO */ | ||
223 | close_and_try_next_loopN: | 232 | close_and_try_next_loopN: |
224 | close(lfd); | 233 | close(lfd); |
225 | try_next_loopN: | 234 | try_next_loopN: |