Skip to contentSkip to conversation

libmount: don't hold write fd to mounted device

PATCH Applied Jan Kara 3 participants 3 messages Feb 21, 2024 – Feb 26, 2024 Util-Linux package

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH 1/1 libmount: don't hold write fd to mounted device

Avoid holding writeable fd to a loop device that is being mounted. In the hardened configurations (CONFIG_BLK_DEV_WRITE_MOUNTED = n) the kernel wants to make sure nobody else has the block device writeably open when mounting so this makes the mount fail.

---
 libmount/src/hook_loopdev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libmount/src/hook_loopdev.c +10 -6
index 8c8f7f218732..e2114b0cbebe 100644
@@ -356,15 +356,19 @@ success:
 			 */
 			mnt_optlist_append_flags(ol, MS_RDONLY, cxt->map_linux);
 
-		/* we have to keep the device open until mount(1),
-		 * otherwise it will be auto-cleared by kernel
+		/*
+		 * We have to keep the device open until mount(1), otherwise it
+		 * will be auto-cleared by kernel. However we don't want to
+		 * keep writeable fd as kernel wants to block all writers to
+		 * the device being mounted (in the more hardened
+		 * configurations). So grab read-only fd instead.
 		 */
-		hd->loopdev_fd = loopcxt_get_fd(&lc);
+		hd->loopdev_fd = open(lc.device, O_RDONLY | O_CLOEXEC);
 		if (hd->loopdev_fd < 0) {
-			DBG(LOOP, ul_debugobj(cxt, "failed to get loopdev FD"));
+			DBG(LOOP,
+			    ul_debugobj(cxt, "failed to reopen loopdev FD"));
 			rc = -errno;
-		} else
-			loopcxt_set_fd(&lc, -1, 0);
+		}
 	}
 done:
 	loopcxt_deinit(&lc);
Signature · 1 line
2.35.3
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jan Kara

Avoid holding writeable fd to a loop device that is being mounted. In the hardened configurations (CONFIG_BLK_DEV_WRITE_MOUNTED = n) the kernel wants to make sure nobody else has the block device writeably open when mounting so this makes the mount fail.

---

Sounds good!

Quoted 36 lines
 libmount/src/hook_loopdev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libmount/src/hook_loopdev.c +10 -6
index 8c8f7f218732..e2114b0cbebe 100644
@@ -356,15 +356,19 @@ success:
 			 */
 			mnt_optlist_append_flags(ol, MS_RDONLY, cxt->map_linux);
 
-		/* we have to keep the device open until mount(1),
-		 * otherwise it will be auto-cleared by kernel
+		/*
+		 * We have to keep the device open until mount(1), otherwise it
+		 * will be auto-cleared by kernel. However we don't want to
+		 * keep writeable fd as kernel wants to block all writers to
+		 * the device being mounted (in the more hardened
+		 * configurations). So grab read-only fd instead.
 		 */
-		hd->loopdev_fd = loopcxt_get_fd(&lc);
+		hd->loopdev_fd = open(lc.device, O_RDONLY | O_CLOEXEC);
 		if (hd->loopdev_fd < 0) {
-			DBG(LOOP, ul_debugobj(cxt, "failed to get loopdev FD"));
+			DBG(LOOP,
+			    ul_debugobj(cxt, "failed to reopen loopdev FD"));
 			rc = -errno;
-		} else
-			loopcxt_set_fd(&lc, -1, 0);
+		}
 	}
 done:
 	loopcxt_deinit(&lc);
Signature · 1 line
2.35.3
maintainer Applied
#2 lore raw
In reply to Jan Kara
Quoted 2 lines · Jan Kara
 libmount/src/hook_loopdev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Applied, thanks.

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