dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0144-libmount-fix-access-utab-write-test.patch

5f5089
From 5d238002ff22241a7d04608102a2182aa74d2b69 Mon Sep 17 00:00:00 2001
fe719b
From: Karel Zak <kzak@redhat.com>
fe719b
Date: Wed, 8 Nov 2017 16:47:40 +0100
fe719b
Subject: [PATCH] libmount: fix access() utab write test
fe719b
fe719b
The commit c08396c7691e1e6a04b6b45892e7e4612ceed8d7 replaces
fe719b
open(O_CREATE) with ecaccess(). Unfortunately, another code depends on
fe719b
the original behavior.
fe719b
fe719b
* let's make utab when really necessary rather than in the try_write() test
fe719b
fe719b
* __mnt_new_table_from_file() returns NULL if tab-file does not
fe719b
 exists. This is incorrect for tab_update.c stuff. We need empty table
fe719b
 in this case.
fe719b
fe719b
* we can check /run/mount/ directory for write access if
fe719b
  eaccess(filename) return ENOENT (because file does not exist)
fe719b
fe719b
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1499760
fe719b
Upstream: http://github.com/karelzak/util-linux/commit/06ff935ec3ad2290025b555ff32b590680af565f
fe719b
Signed-off-by: Karel Zak <kzak@redhat.com>
fe719b
---
fe719b
 libmount/src/mountP.h     |  2 +-
fe719b
 libmount/src/tab_parse.c  | 11 +++++++----
fe719b
 libmount/src/tab_update.c |  9 +++++----
fe719b
 libmount/src/utils.c      | 42 +++++++++++++++++++++++++++++++-----------
fe719b
 4 files changed, 44 insertions(+), 20 deletions(-)
fe719b
fe719b
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h
fe719b
index 6cabcedeb..8b3f92e17 100644
fe719b
--- a/libmount/src/mountP.h
fe719b
+++ b/libmount/src/mountP.h
fe719b
@@ -278,7 +278,7 @@ struct libmnt_table {
fe719b
 	struct list_head	ents;	/* list of entries (libmnt_fs) */
fe719b
 };
fe719b
 
fe719b
-extern struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt);
fe719b
+extern struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt, int empty_for_enoent);
fe719b
 
fe719b
 /*
fe719b
  * Tab file format
fe719b
diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c
fe719b
index 987e671fa..c629c67ad 100644
fe719b
--- a/libmount/src/tab_parse.c
fe719b
+++ b/libmount/src/tab_parse.c
fe719b
@@ -714,7 +714,7 @@ int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
fe719b
 	return __mnt_table_parse_dir(tb, dirname);
fe719b
 }
fe719b
 
fe719b
-struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt)
fe719b
+struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt, int empty_for_enoent)
fe719b
 {
fe719b
 	struct libmnt_table *tb;
fe719b
 	struct stat st;
fe719b
@@ -723,7 +723,8 @@ struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt)
fe719b
 	if (!filename)
fe719b
 		return NULL;
fe719b
 	if (stat(filename, &st))
fe719b
-		return NULL;
fe719b
+		return empty_for_enoent ? mnt_new_table() : NULL;
fe719b
+
fe719b
 	tb = mnt_new_table();
fe719b
 	if (tb) {
fe719b
 		tb->fmt = fmt;
fe719b
@@ -748,8 +749,10 @@ struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt)
fe719b
  */
fe719b
 struct libmnt_table *mnt_new_table_from_file(const char *filename)
fe719b
 {
fe719b
-	assert(filename);
fe719b
-	return __mnt_new_table_from_file(filename, MNT_FMT_GUESS);
fe719b
+	if (!filename)
fe719b
+		return NULL;
fe719b
+
fe719b
+	return __mnt_new_table_from_file(filename, MNT_FMT_GUESS, 0);
fe719b
 }
fe719b
 
fe719b
 /**
fe719b
diff --git a/libmount/src/tab_update.c b/libmount/src/tab_update.c
fe719b
index 1e7f32be0..5f503cad7 100644
fe719b
--- a/libmount/src/tab_update.c
fe719b
+++ b/libmount/src/tab_update.c
fe719b
@@ -567,6 +567,7 @@ leave:
fe719b
 
fe719b
 	unlink(uq);	/* be paranoid */
fe719b
 	free(uq);
fe719b
+	DBG(UPDATE, mnt_debug_h(upd, "%s: done [rc=%d]", upd->filename, rc));
fe719b
 	return rc;
fe719b
 }
fe719b
 
fe719b
@@ -600,7 +601,7 @@ static int update_add_entry(struct libmnt_update *upd, struct libmnt_lock *lc)
fe719b
 		return rc;
fe719b
 
fe719b
 	tb = __mnt_new_table_from_file(upd->filename,
fe719b
-			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB);
fe719b
+			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB, 1);
fe719b
 	if (tb)
fe719b
 		rc = add_file_entry(tb, upd);
fe719b
 	if (lc)
fe719b
@@ -626,7 +627,7 @@ static int update_remove_entry(struct libmnt_update *upd, struct libmnt_lock *lc
fe719b
 		return rc;
fe719b
 
fe719b
 	tb = __mnt_new_table_from_file(upd->filename,
fe719b
-			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB);
fe719b
+			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB, 1);
fe719b
 	if (tb) {
fe719b
 		struct libmnt_fs *rem = mnt_table_find_target(tb, upd->target, MNT_ITER_BACKWARD);
fe719b
 		if (rem) {
fe719b
@@ -656,7 +657,7 @@ static int update_modify_target(struct libmnt_update *upd, struct libmnt_lock *l
fe719b
 		return rc;
fe719b
 
fe719b
 	tb = __mnt_new_table_from_file(upd->filename,
fe719b
-			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB);
fe719b
+			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB, 1);
fe719b
 	if (tb) {
fe719b
 		struct libmnt_fs *cur = mnt_table_find_target(tb,
fe719b
 				mnt_fs_get_srcpath(upd->fs), MNT_ITER_BACKWARD);
fe719b
@@ -693,7 +694,7 @@ static int update_modify_options(struct libmnt_update *upd, struct libmnt_lock *
fe719b
 		return rc;
fe719b
 
fe719b
 	tb = __mnt_new_table_from_file(upd->filename,
fe719b
-			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB);
fe719b
+			upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB, 1);
fe719b
 	if (tb) {
fe719b
 		struct libmnt_fs *cur = mnt_table_find_target(tb,
fe719b
 					mnt_fs_get_target(fs),
fe719b
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
fe719b
index a275d0a0e..fa33bd9a1 100644
fe719b
--- a/libmount/src/utils.c
fe719b
+++ b/libmount/src/utils.c
fe719b
@@ -651,18 +651,37 @@ done:
fe719b
 	return rc;
fe719b
 }
fe719b
 
fe719b
-static int try_write(const char *filename)
fe719b
+static int try_write(const char *filename, const char *directory)
fe719b
 {
fe719b
 	int rc = 0;
fe719b
 
fe719b
 	if (!filename)
fe719b
 		return -EINVAL;
fe719b
 
fe719b
+	DBG(UTILS, mnt_debug("try write %s dir: %s", filename, directory));
fe719b
+
fe719b
 #ifdef HAVE_EACCESS
fe719b
-	if (eaccess(filename, R_OK|W_OK) != 0)
fe719b
-		rc = -errno;
fe719b
-#else
fe719b
+	/* Try eaccess() first, because open() is overkill, may be monitored by
fe719b
+	 * audit and we don't want to fill logs by our checks...
fe719b
+	 */
fe719b
+	if (eaccess(filename, R_OK|W_OK) == 0) {
fe719b
+		DBG(UTILS, mnt_debug(" access OK"));
fe719b
+		return 0;
fe719b
+	} else if (errno != ENOENT) {
fe719b
+		DBG(UTILS, mnt_debug(" access FAILED"));
fe719b
+		return -errno;
fe719b
+	} else if (directory) {
fe719b
+		/* file does not exist; try if directory is writable */
fe719b
+		if (eaccess(directory, R_OK|W_OK) != 0)
fe719b
+			rc = -errno;
fe719b
+
fe719b
+		DBG(UTILS, mnt_debug(" access %s [%s]", rc ? "FAILED" : "OK", directory));
fe719b
+		return rc;
fe719b
+	} else
fe719b
+#endif
fe719b
 	{
fe719b
+		DBG(UTILS, mnt_debug(" doing open-write test"));
fe719b
+
fe719b
 		int fd = open(filename, O_RDWR|O_CREAT|O_CLOEXEC,
fe719b
 			    S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
fe719b
 		if (fd < 0)
fe719b
@@ -670,7 +689,6 @@ static int try_write(const char *filename)
fe719b
 		else
fe719b
 			close(fd);
fe719b
 	}
fe719b
-#endif
fe719b
 	return rc;
fe719b
 }
fe719b
 
fe719b
@@ -704,7 +722,7 @@ int mnt_has_regular_mtab(const char **mtab, int *writable)
fe719b
 		/* file exist */
fe719b
 		if (S_ISREG(st.st_mode)) {
fe719b
 			if (writable)
fe719b
-				*writable = !try_write(filename);
fe719b
+				*writable = !try_write(filename, NULL);
fe719b
 			return 1;
fe719b
 		}
fe719b
 		goto done;
fe719b
@@ -712,7 +730,7 @@ int mnt_has_regular_mtab(const char **mtab, int *writable)
fe719b
 
fe719b
 	/* try to create the file */
fe719b
 	if (writable) {
fe719b
-		*writable = !try_write(filename);
fe719b
+		*writable = !try_write(filename, NULL);
fe719b
 		if (*writable)
fe719b
 			return 1;
fe719b
 	}
fe719b
@@ -750,7 +768,7 @@ int mnt_has_regular_utab(const char **utab, int *writable)
fe719b
 		/* file exist */
fe719b
 		if (S_ISREG(st.st_mode)) {
fe719b
 			if (writable)
fe719b
-				*writable = !try_write(filename);
fe719b
+				*writable = !try_write(filename, NULL);
fe719b
 			return 1;
fe719b
 		}
fe719b
 		goto done;	/* it's not regular file */
fe719b
@@ -767,11 +785,13 @@ int mnt_has_regular_utab(const char **utab, int *writable)
fe719b
 		rc = mkdir(dirname, S_IWUSR|
fe719b
 				    S_IRUSR|S_IRGRP|S_IROTH|
fe719b
 				    S_IXUSR|S_IXGRP|S_IXOTH);
fe719b
-		free(dirname);
fe719b
-		if (rc && errno != EEXIST)
fe719b
+		if (rc && errno != EEXIST) {
fe719b
+			free(dirname);
fe719b
 			goto done;			/* probably EACCES */
fe719b
+		}
fe719b
 
fe719b
-		*writable = !try_write(filename);
fe719b
+		*writable = !try_write(filename, dirname);
fe719b
+		free(dirname);
fe719b
 		if (*writable)
fe719b
 			return 1;
fe719b
 	}
fe719b
-- 
fe719b
2.13.6
fe719b