Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
From: Tycho Andersen <tycho@tycho.ws>
To: util-linux@vger.kernel.org
Cc: Tycho Andersen <tycho@tycho.ws>
Subject: [PATCH] libmount: do not unnecessarily chmod utab.lock
Date: Thu,  9 Jan 2020 16:52:41 -0700
Message-ID: <20200109235241.31865-1-tycho@tycho.ws> (raw)

Before ecfeae90a294 ("libmount: Ensure utab.lock mode 644"), you could do
something like:

irc:/tmp umount --version
umount from util-linux 2.27.1 (libmount 2.27.0: selinux, assert, debug)
irc:/tmp mkdir foo bar
irc:/tmp unshare -Urm
irc:/tmp mount --bind foo bar
irc:/tmp umount bar
irc:/tmp echo $?
0

However, afterwards, you get:

/tmp unshare -Urm
/tmp mount --bind foo bar
/tmp umount bar
umount: /tmp/bar: filesystem was unmounted, but failed to update userspace mount table.

Because of the chmod failing:

fchmod(3, 0644)                         = -1 EPERM (Operation not permitted)

Let's figure out whether the chmod is necessary before doing it, and only
do it if it is necessary. This won't fix cases where the system is already
broken, but at least on healthy systems umount will behave as before.

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
---
 libmount/src/lock.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/libmount/src/lock.c b/libmount/src/lock.c
index e6eefa13a..74b272f9d 100644
--- a/libmount/src/lock.c
+++ b/libmount/src/lock.c
@@ -204,6 +204,8 @@ static int lock_simplelock(struct libmnt_lock *ml)
 {
 	const char *lfile;
 	int rc;
+	struct stat sb;
+	const mode_t lock_mask = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
 
 	assert(ml);
 	assert(ml->simplelock);
@@ -225,12 +227,21 @@ static int lock_simplelock(struct libmnt_lock *ml)
 		rc = -errno;
 		goto err;
 	}
-	rc = fchmod(ml->lockfile_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+
+	rc = fstat(ml->lockfile_fd, &sb);
 	if (rc < 0) {
 		rc = -errno;
 		goto err;
 	}
 
+	if ((sb.st_mode & lock_mask) != lock_mask) {
+		rc = fchmod(ml->lockfile_fd, lock_mask);
+		if (rc < 0) {
+			rc = -errno;
+			goto err;
+		}
+	}
+
 	while (flock(ml->lockfile_fd, LOCK_EX) < 0) {
 		int errsv;
 		if ((errno == EAGAIN) || (errno == EINTR))
-- 
2.20.1


                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200109235241.31865-1-tycho@tycho.ws \
    --to=tycho@tycho.ws \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org
	public-inbox-index util-linux

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git