Skip to contentSkip to conversation

mount: Fix race in loop device reuse code

PATCH Applied Jan Kara 3 participants 4 messages Jan 20, 2022 – Jan 25, 2022 Util-Linux package

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH 1/1 mount: Fix race in loop device reuse code

Small timing changes in the kernel loop device handling broke the following loop:

while :; do mount -o loop,ro isofs.iso isofs/; umount isofs/; done

which quickly reports:
mount: /mnt: can't read superblock on /dev/loop0.
umount: /mnt: not mounted.

And this loop is broken because of a subtle interaction with systemd-udevd that also opens the loop device. The race seems to be in mount(8) handling itself and the altered kernel timing makes it happen. It look like:

bash systemd-udevd
mount -o loop,ro isofs.iso isofs/
/dev/loop0 is created and bound to isofs.iso, autoclear is set for loop0
opens /dev/loop0
umount isofs/
loop0 still lives because systemd-udev still has device open mount -o loop,ro isofs.iso isofs/
gets to mnt_context_setup_loopdev()
loopcxt_find_overlap()
sees loop0 is still valid and with proper parameters reuse = true;
close /dev/loop0
last fd closed => loop0 is
cleaned up
loopcxt_get_fd()
opens loop0 but it is no longer the device we wanted!
calls mount(2) which fails because we cannot read from the loop device

Fix the problem by rechecking that loop device is still attached after opening the device. This makes sure the kernel will not autoclear the device anymore.

---
 libmount/src/context_loopdev.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
libmount/src/context_loopdev.c +19 -0
index 88af20c0e522..62e319ce6131 100644
@@ -255,6 +255,25 @@ int mnt_context_setup_loopdev(struct libmnt_context *cxt)
 			DBG(LOOP, ul_debugobj(cxt, "re-using existing loop device %s",
 				loopcxt_get_device(&lc)));
 
+			/* Open loop device to block device autoclear... */
+			if (loopcxt_get_fd(&lc) < 0) {
+				DBG(LOOP, ul_debugobj(cxt, "failed to get loopdev FD"));
+				rc = -errno;
+				goto done;
+			}
+
+			/*
+			 * Now that we certainly have the loop device open,
+			 * verify the loop device was not autocleared in the
+			 * mean time.
+			 */
+			if (!loopcxt_get_info(&lc)) {
+				DBG(LOOP, ul_debugobj(cxt, "lost race with %s teardown",
+						loopcxt_get_device(&lc)));
+				loopcxt_deinit(&lc);
+				break;
+			}
+
 			/* Once a loop is initialized RO, there is no
 			 * way to change its parameters. */
 			if (loopcxt_is_readonly(&lc)
Signature · 1 line
2.31.1
#1 lore raw
Quoted 3 lines · Jan Kara

Fix the problem by rechecking that loop device is still attached after opening the device. This makes sure the kernel will not autoclear the device anymore.

Since this block is marked as /* overlap -- full size and offset match (reuse) */ , can just checking that neither loopcxt_get_fd() nor loopcxt_get_info() failed guarantee that full size and offset still match? Isn't there possibility that autoclear of file1 completes and another thread again assigns file2 (with different size or offset) right before /* Open loop device to block device autoclear... */ line? Don't we need to compare with more robust identifier like device:inode pair?

author
#2 lore raw
Quoted 5 lines · Tetsuo Handa
Quoted 3 lines · Jan Kara

Fix the problem by rechecking that loop device is still attached after opening the device. This makes sure the kernel will not autoclear the device anymore.

Quoted 7 lines

Since this block is marked as /* overlap -- full size and offset match (reuse) */ , can just checking that neither loopcxt_get_fd() nor loopcxt_get_info() failed guarantee that full size and offset still match? Isn't there possibility that autoclear of file1 completes and another thread again assigns file2 (with different size or offset) right before /* Open loop device to block device autoclear... */ line? Don't we need to compare with more robust identifier like device:inode pair?

I was considering that as well (even had it coded in an early version) but then decided agaist it. The underlying file, offset, size can change even while you have the loop device open (through LOOP_CHANGE_FD, LOOP_SET_STATUS, ...). So checking after having the loop device open is no better than checking before and I could not come up with realistic scenario where checking later would significantly help.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#3 lore raw
In reply to Jan Kara
Quoted 2 lines · Jan Kara
 libmount/src/context_loopdev.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Applied, thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com