b394b9
From f5ef29a5b5c51fe2039352dabcc4946fa2f55861 Mon Sep 17 00:00:00 2001
b394b9
From: Karel Zak <kzak@redhat.com>
b394b9
Date: Tue, 2 Jul 2013 10:46:10 +0200
b394b9
Subject: [PATCH 61/84] libmount: be more restrictive about valid tag names
b394b9
b394b9
 # mount DUMMY=filename.img /mnt
b394b9
b394b9
The 'DUMMY=filename.img' is a filename and should not be
b394b9
interpreted as tag name. The valid tag names are LABEL, UUID,
b394b9
PARTLABEL and PARTUUID only.
b394b9
b394b9
Upstream: http://github.com/karelzak/util-linux/commit/2c6b25f01802808b142d450af3352605720899da
b394b9
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1248003
b394b9
Signed-off-by: Karel Zak <kzak@redhat.com>
b394b9
---
b394b9
 libmount/src/cache.c  | 29 ++++++++++++-----------------
b394b9
 libmount/src/fs.c     |  9 ++++++---
b394b9
 libmount/src/mountP.h |  2 ++
b394b9
 libmount/src/tab.c    | 20 ++++++++------------
b394b9
 libmount/src/utils.c  | 12 ++++++++++++
b394b9
 5 files changed, 40 insertions(+), 32 deletions(-)
b394b9
b394b9
diff --git a/libmount/src/cache.c b/libmount/src/cache.c
b394b9
index 7b65122..43a4daf 100644
b394b9
--- a/libmount/src/cache.c
b394b9
+++ b/libmount/src/cache.c
b394b9
@@ -583,22 +583,18 @@ error:
b394b9
 char *mnt_resolve_spec(const char *spec, struct libmnt_cache *cache)
b394b9
 {
b394b9
 	char *cn = NULL;
b394b9
+	char *t = NULL, *v = NULL;
b394b9
 
b394b9
 	if (!spec)
b394b9
 		return NULL;
b394b9
 
b394b9
-	if (strchr(spec, '=')) {
b394b9
-		char *tag, *val;
b394b9
-
b394b9
-		if (!blkid_parse_tag_string(spec, &tag, &val)) {
b394b9
-			cn = mnt_resolve_tag(tag, val, cache);
b394b9
-
b394b9
-			free(tag);
b394b9
-			free(val);
b394b9
-		}
b394b9
-	} else
b394b9
+	if (blkid_parse_tag_string(spec, &t, &v) == 0 && mnt_valid_tagname(t))
b394b9
+		cn = mnt_resolve_tag(t, v, cache);
b394b9
+	else
b394b9
 		cn = mnt_resolve_path(spec, cache);
b394b9
 
b394b9
+	free(t);
b394b9
+	free(v);
b394b9
 	return cn;
b394b9
 }
b394b9
 
b394b9
@@ -663,6 +659,7 @@ int test_read_tags(struct libmnt_test *ts, int argc, char *argv[])
b394b9
 
b394b9
 	while(fgets(line, sizeof(line), stdin)) {
b394b9
 		size_t sz = strlen(line);
b394b9
+		char *t = NULL, *v = NULL;
b394b9
 
b394b9
 		if (sz > 0 && line[sz - 1] == '\n')
b394b9
 			line[sz - 1] = '\0';
b394b9
@@ -674,16 +671,14 @@ int test_read_tags(struct libmnt_test *ts, int argc, char *argv[])
b394b9
 			if (mnt_cache_read_tags(cache, line) < 0)
b394b9
 				fprintf(stderr, "%s: read tags failed\n", line);
b394b9
 
b394b9
-		} else if (strchr(line, '=')) {
b394b9
-			char *tag, *val;
b394b9
+		} else if (blkid_parse_tag_string(line, &t, &v) == 0) {
b394b9
 			const char *cn = NULL;
b394b9
 
b394b9
-			if (!blkid_parse_tag_string(line, &tag, &val)) {
b394b9
-				cn = cache_find_tag(cache, tag, val);
b394b9
+			if (mnt_valid_tagname(t))
b394b9
+				cn = cache_find_tag(cache, t, v);
b394b9
+			free(t);
b394b9
+			free(v);
b394b9
 
b394b9
-				free(tag);
b394b9
-				free(val);
b394b9
-			}
b394b9
 			if (cn)
b394b9
 				printf("%s: %s\n", line, cn);
b394b9
 			else
b394b9
diff --git a/libmount/src/fs.c b/libmount/src/fs.c
b394b9
index c95cdc7..75e3bbb 100644
b394b9
--- a/libmount/src/fs.c
b394b9
+++ b/libmount/src/fs.c
b394b9
@@ -318,9 +318,12 @@ int __mnt_fs_set_source_ptr(struct libmnt_fs *fs, char *source)
b394b9
 
b394b9
 	assert(fs);
b394b9
 
b394b9
-	if (source && *source != '/' && strchr(source, '=')) {
b394b9
-		if (blkid_parse_tag_string(source, &t, &v) != 0)
b394b9
-			return -1;
b394b9
+	if (source && blkid_parse_tag_string(source, &t, &v) == 0 &&
b394b9
+	    !mnt_valid_tagname(t)) {
b394b9
+		/* parsable but unknown tag -- ignore */
b394b9
+		free(t);
b394b9
+		free(v);
b394b9
+		t = v = NULL;
b394b9
 	}
b394b9
 
b394b9
 	if (fs->source != source)
b394b9
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h
b394b9
index e064a68..7b0848f 100644
b394b9
--- a/libmount/src/mountP.h
b394b9
+++ b/libmount/src/mountP.h
b394b9
@@ -136,6 +136,8 @@ extern int startswith(const char *s, const char *sx)
b394b9
 
b394b9
 extern char *stripoff_last_component(char *path);
b394b9
 
b394b9
+extern int mnt_valid_tagname(const char *tagname);
b394b9
+
b394b9
 extern int is_file_empty(const char *name);
b394b9
 
b394b9
 extern int mkdir_p(const char *path, mode_t mode);
b394b9
diff --git a/libmount/src/tab.c b/libmount/src/tab.c
b394b9
index e3524a8..1ba1eec 100644
b394b9
--- a/libmount/src/tab.c
b394b9
+++ b/libmount/src/tab.c
b394b9
@@ -801,7 +801,8 @@ struct libmnt_fs *mnt_table_find_tag(struct libmnt_table *tb, const char *tag,
b394b9
 struct libmnt_fs *mnt_table_find_source(struct libmnt_table *tb,
b394b9
 					const char *source, int direction)
b394b9
 {
b394b9
-	struct libmnt_fs *fs = NULL;
b394b9
+	struct libmnt_fs *fs;
b394b9
+	char *t = NULL, *v = NULL;
b394b9
 
b394b9
 	assert(tb);
b394b9
 
b394b9
@@ -812,18 +813,13 @@ struct libmnt_fs *mnt_table_find_source(struct libmnt_table *tb,
b394b9
 
b394b9
 	DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: '%s'", source));
b394b9
 
b394b9
-	if (source && *source && strchr(source, '=')) {
b394b9
-		char *tag, *val;
b394b9
-
b394b9
-		if (blkid_parse_tag_string(source, &tag, &val) == 0) {
b394b9
-
b394b9
-			fs = mnt_table_find_tag(tb, tag, val, direction);
b394b9
-
b394b9
-			free(tag);
b394b9
-			free(val);
b394b9
-		}
b394b9
-	} else
b394b9
+	if (blkid_parse_tag_string(source, &t, &v) || !mnt_valid_tagname(t))
b394b9
 		fs = mnt_table_find_srcpath(tb, source, direction);
b394b9
+	else
b394b9
+		fs = mnt_table_find_tag(tb, t, v, direction);
b394b9
+
b394b9
+	free(t);
b394b9
+	free(v);
b394b9
 
b394b9
 	return fs;
b394b9
 }
b394b9
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
b394b9
index 6a444ad..9305bb8 100644
b394b9
--- a/libmount/src/utils.c
b394b9
+++ b/libmount/src/utils.c
b394b9
@@ -65,6 +65,18 @@ int is_file_empty(const char *name)
b394b9
 	return (stat(name, &st) != 0 || st.st_size == 0);
b394b9
 }
b394b9
 
b394b9
+int mnt_valid_tagname(const char *tagname)
b394b9
+{
b394b9
+	if (tagname && *tagname && (
b394b9
+	    strcmp("UUID", tagname) == 0 ||
b394b9
+	    strcmp("LABEL", tagname) == 0 ||
b394b9
+	    strcmp("PARTUUID", tagname) == 0 ||
b394b9
+	    strcmp("PARTLABEL", tagname) == 0))
b394b9
+		return 1;
b394b9
+
b394b9
+	return 0;
b394b9
+}
b394b9
+
b394b9
 int mnt_parse_offset(const char *str, size_t len, uintmax_t *res)
b394b9
 {
b394b9
 	char *p;
b394b9
-- 
b394b9
2.7.4
b394b9