util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libmount: do not unnecessarily chmod utab.lock
@ 2020-01-09 23:52 Tycho Andersen
  2020-04-22 10:38 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Tycho Andersen @ 2020-01-09 23:52 UTC (permalink / raw)
  To: util-linux; +Cc: Tycho Andersen

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


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

* Re: [PATCH] libmount: do not unnecessarily chmod utab.lock
  2020-01-09 23:52 [PATCH] libmount: do not unnecessarily chmod utab.lock Tycho Andersen
@ 2020-04-22 10:38 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2020-04-22 10:38 UTC (permalink / raw)
  To: Tycho Andersen; +Cc: util-linux

On Thu, Jan 09, 2020 at 04:52:41PM -0700, Tycho Andersen wrote:
>  libmount/src/lock.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)

Applied, thanks. Sorry, for the delay.

    Karel

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


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

end of thread, other threads:[~2020-04-22 10:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-09 23:52 [PATCH] libmount: do not unnecessarily chmod utab.lock Tycho Andersen
2020-04-22 10:38 ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).