Util-Linux package development
 help / color / mirror / Atom feed
* [PATCH] libmount: don't hold write fd to mounted device
@ 2024-02-21 17:30 Jan Kara
  2024-02-22  9:19 ` Christian Brauner
  2024-02-26  8:53 ` Karel Zak
  0 siblings, 2 replies; 3+ messages in thread
From: Jan Kara @ 2024-02-21 17:30 UTC (permalink / raw)
  To: util-linux; +Cc: Christian Brauner, Jan Kara, JunChao Sun

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.

Reported-by: JunChao Sun <sunjunchao2870@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
---
 libmount/src/hook_loopdev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/libmount/src/hook_loopdev.c b/libmount/src/hook_loopdev.c
index 8c8f7f218732..e2114b0cbebe 100644
--- a/libmount/src/hook_loopdev.c
+++ b/libmount/src/hook_loopdev.c
@@ -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);
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] libmount: don't hold write fd to mounted device
  2024-02-21 17:30 [PATCH] libmount: don't hold write fd to mounted device Jan Kara
@ 2024-02-22  9:19 ` Christian Brauner
  2024-02-26  8:53 ` Karel Zak
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2024-02-22  9:19 UTC (permalink / raw)
  To: Jan Kara; +Cc: util-linux, JunChao Sun

On Wed, Feb 21, 2024 at 06:30:50PM +0100, Jan Kara wrote:
> 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.
> 
> Reported-by: JunChao Sun <sunjunchao2870@gmail.com>
> Signed-off-by: Jan Kara <jack@suse.cz>
> ---

Sounds good!
Acked-by: Christian Brauner <brauner@kernel.org>

>  libmount/src/hook_loopdev.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/libmount/src/hook_loopdev.c b/libmount/src/hook_loopdev.c
> index 8c8f7f218732..e2114b0cbebe 100644
> --- a/libmount/src/hook_loopdev.c
> +++ b/libmount/src/hook_loopdev.c
> @@ -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);
> -- 
> 2.35.3
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libmount: don't hold write fd to mounted device
  2024-02-21 17:30 [PATCH] libmount: don't hold write fd to mounted device Jan Kara
  2024-02-22  9:19 ` Christian Brauner
@ 2024-02-26  8:53 ` Karel Zak
  1 sibling, 0 replies; 3+ messages in thread
From: Karel Zak @ 2024-02-26  8:53 UTC (permalink / raw)
  To: Jan Kara; +Cc: util-linux, Christian Brauner, JunChao Sun

On Wed, Feb 21, 2024 at 06:30:50PM +0100, Jan Kara wrote:
>  libmount/src/hook_loopdev.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)

Applied, thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-26  8:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-21 17:30 [PATCH] libmount: don't hold write fd to mounted device Jan Kara
2024-02-22  9:19 ` Christian Brauner
2024-02-26  8:53 ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox