From f5ef29a5b5c51fe2039352dabcc4946fa2f55861 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 2 Jul 2013 10:46:10 +0200
Subject: [PATCH 61/84] libmount: be more restrictive about valid tag names

 # mount DUMMY=filename.img /mnt

The 'DUMMY=filename.img' is a filename and should not be
interpreted as tag name. The valid tag names are LABEL, UUID,
PARTLABEL and PARTUUID only.

Upstream: http://github.com/karelzak/util-linux/commit/2c6b25f01802808b142d450af3352605720899da
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1248003
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 libmount/src/cache.c  | 29 ++++++++++++-----------------
 libmount/src/fs.c     |  9 ++++++---
 libmount/src/mountP.h |  2 ++
 libmount/src/tab.c    | 20 ++++++++------------
 libmount/src/utils.c  | 12 ++++++++++++
 5 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/libmount/src/cache.c b/libmount/src/cache.c
index 7b65122..43a4daf 100644
--- a/libmount/src/cache.c
+++ b/libmount/src/cache.c
@@ -583,22 +583,18 @@ error:
 char *mnt_resolve_spec(const char *spec, struct libmnt_cache *cache)
 {
	char *cn = NULL;
+	char *t = NULL, *v = NULL;

	if (!spec)
		return NULL;

-	if (strchr(spec, '=')) {
-		char *tag, *val;
-
-		if (!blkid_parse_tag_string(spec, &tag, &val)) {
-			cn = mnt_resolve_tag(tag, val, cache);
-
-			free(tag);
-			free(val);
-		}
-	} else
+	if (blkid_parse_tag_string(spec, &t, &v) == 0 && mnt_valid_tagname(t))
+		cn = mnt_resolve_tag(t, v, cache);
+	else
		cn = mnt_resolve_path(spec, cache);

+	free(t);
+	free(v);
	return cn;
 }

@@ -663,6 +659,7 @@ int test_read_tags(struct libmnt_test *ts, int argc, char *argv[])

	while(fgets(line, sizeof(line), stdin)) {
		size_t sz = strlen(line);
+		char *t = NULL, *v = NULL;

		if (sz > 0 && line[sz - 1] == '\n')
			line[sz - 1] = '\0';
@@ -674,16 +671,14 @@ int test_read_tags(struct libmnt_test *ts, int argc, char *argv[])
			if (mnt_cache_read_tags(cache, line) < 0)
				fprintf(stderr, "%s: read tags failed\n", line);

-		} else if (strchr(line, '=')) {
-			char *tag, *val;
+		} else if (blkid_parse_tag_string(line, &t, &v) == 0) {
			const char *cn = NULL;

-			if (!blkid_parse_tag_string(line, &tag, &val)) {
-				cn = cache_find_tag(cache, tag, val);
+			if (mnt_valid_tagname(t))
+				cn = cache_find_tag(cache, t, v);
+			free(t);
+			free(v);

-				free(tag);
-				free(val);
-			}
			if (cn)
				printf("%s: %s\n", line, cn);
			else
diff --git a/libmount/src/fs.c b/libmount/src/fs.c
index c95cdc7..75e3bbb 100644
--- a/libmount/src/fs.c
+++ b/libmount/src/fs.c
@@ -318,9 +318,12 @@ int __mnt_fs_set_source_ptr(struct libmnt_fs *fs, char *source)

	assert(fs);

-	if (source && *source != '/' && strchr(source, '=')) {
-		if (blkid_parse_tag_string(source, &t, &v) != 0)
-			return -1;
+	if (source && blkid_parse_tag_string(source, &t, &v) == 0 &&
+	    !mnt_valid_tagname(t)) {
+		/* parsable but unknown tag -- ignore */
+		free(t);
+		free(v);
+		t = v = NULL;
	}

	if (fs->source != source)
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h
index e064a68..7b0848f 100644
--- a/libmount/src/mountP.h
+++ b/libmount/src/mountP.h
@@ -136,6 +136,8 @@ extern int startswith(const char *s, const char *sx)

 extern char *stripoff_last_component(char *path);

+extern int mnt_valid_tagname(const char *tagname);
+
 extern int is_file_empty(const char *name);

 extern int mkdir_p(const char *path, mode_t mode);
diff --git a/libmount/src/tab.c b/libmount/src/tab.c
index e3524a8..1ba1eec 100644
--- a/libmount/src/tab.c
+++ b/libmount/src/tab.c
@@ -801,7 +801,8 @@ struct libmnt_fs *mnt_table_find_tag(struct libmnt_table *tb, const char *tag,
 struct libmnt_fs *mnt_table_find_source(struct libmnt_table *tb,
					const char *source, int direction)
 {
-	struct libmnt_fs *fs = NULL;
+	struct libmnt_fs *fs;
+	char *t = NULL, *v = NULL;

	assert(tb);

@@ -812,18 +813,13 @@ struct libmnt_fs *mnt_table_find_source(struct libmnt_table *tb,

	DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: '%s'", source));

-	if (source && *source && strchr(source, '=')) {
-		char *tag, *val;
-
-		if (blkid_parse_tag_string(source, &tag, &val) == 0) {
-
-			fs = mnt_table_find_tag(tb, tag, val, direction);
-
-			free(tag);
-			free(val);
-		}
-	} else
+	if (blkid_parse_tag_string(source, &t, &v) || !mnt_valid_tagname(t))
		fs = mnt_table_find_srcpath(tb, source, direction);
+	else
+		fs = mnt_table_find_tag(tb, t, v, direction);
+
+	free(t);
+	free(v);

	return fs;
 }
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
index 6a444ad..9305bb8 100644
--- a/libmount/src/utils.c
+++ b/libmount/src/utils.c
@@ -65,6 +65,18 @@ int is_file_empty(const char *name)
	return (stat(name, &st) != 0 || st.st_size == 0);
 }

+int mnt_valid_tagname(const char *tagname)
+{
+	if (tagname && *tagname && (
+	    strcmp("UUID", tagname) == 0 ||
+	    strcmp("LABEL", tagname) == 0 ||
+	    strcmp("PARTUUID", tagname) == 0 ||
+	    strcmp("PARTLABEL", tagname) == 0))
+		return 1;
+
+	return 0;
+}
+
 int mnt_parse_offset(const char *str, size_t len, uintmax_t *res)
 {
	char *p;
--
2.7.4
