Blame SOURCES/autofs-5.1.6-fix-autofs-mount-options-construction.patch

81b4ce
autofs-5.1.6 - fix autofs mount options construction
81b4ce
81b4ce
From: Ian Kent <raven@themaw.net>
81b4ce
81b4ce
There's an off by one length error in the autofs mount options
81b4ce
construction.
81b4ce
81b4ce
Consolidate the options construction into make_options_string() and
81b4ce
use snprintf() to verify the options length calculation is correct.
81b4ce
81b4ce
Signed-off-by: Ian Kent <raven@themaw.net>
81b4ce
---
81b4ce
 CHANGELOG         |    1 
81b4ce
 daemon/direct.c   |   46 ++-----------------------
81b4ce
 daemon/indirect.c |   23 +-----------
81b4ce
 include/mounts.h  |    3 +
81b4ce
 lib/mounts.c      |   98 +++++++++++++++++++++++++++++++++++++++++++++---------
81b4ce
 5 files changed, 92 insertions(+), 79 deletions(-)
81b4ce
81b4ce
--- autofs-5.1.4.orig/CHANGELOG
81b4ce
+++ autofs-5.1.4/CHANGELOG
81b4ce
@@ -80,6 +80,7 @@ xx/xx/2018 autofs-5.1.5
81b4ce
 - fix trailing dollar sun entry expansion.
81b4ce
 - initialize struct addrinfo for getaddrinfo() calls.
81b4ce
 - fix quoted string length calc in expandsunent().
81b4ce
+- fix autofs mount options construction.
81b4ce
 
81b4ce
 19/12/2017 autofs-5.1.4
81b4ce
 - fix spec file url.
81b4ce
--- autofs-5.1.4.orig/daemon/direct.c
81b4ce
+++ autofs-5.1.4/daemon/direct.c
81b4ce
@@ -348,29 +348,10 @@ int do_mount_autofs_direct(struct autofs
81b4ce
 	}
81b4ce
 
81b4ce
 	if (!mp->options) {
81b4ce
-		mp->options = make_options_string(ap->path, ap->kpipefd, str_direct);
81b4ce
+		mp->options = make_options_string(ap->path,
81b4ce
+				ap->kpipefd, str_direct, ap->flags);
81b4ce
 		if (!mp->options)
81b4ce
 			return 0;
81b4ce
-
81b4ce
-		if ((ap->flags & MOUNT_FLAG_STRICTEXPIRE) &&
81b4ce
-		    ((get_kver_major() == 5 && get_kver_minor() > 3) ||
81b4ce
-		     (get_kver_major() > 5))) {
81b4ce
-			char *tmp = realloc(mp->options, strlen(mp->options) + 12);
81b4ce
-			if (tmp) {
81b4ce
-				strcat(tmp, ",strictexpire");
81b4ce
-				mp->options = tmp;
81b4ce
-			}
81b4ce
-		}
81b4ce
-
81b4ce
-		if ((ap->flags & MOUNT_FLAG_IGNORE) &&
81b4ce
-		    ((get_kver_major() == 5 && get_kver_minor() > 4) ||
81b4ce
-		     (get_kver_major() > 5))) {
81b4ce
-			char *tmp = realloc(mp->options, strlen(mp->options) + 7);
81b4ce
-			if (tmp) {
81b4ce
-				strcat(tmp, ",ignore");
81b4ce
-				mp->options = tmp;
81b4ce
-			}
81b4ce
-		}
81b4ce
 	}
81b4ce
 
81b4ce
 	/* In case the directory doesn't exist, try to mkdir it */
81b4ce
@@ -676,29 +657,10 @@ int mount_autofs_offset(struct autofs_po
81b4ce
 	}
81b4ce
 
81b4ce
 	if (!mp->options) {
81b4ce
-		mp->options = make_options_string(ap->path, ap->kpipefd, str_offset);
81b4ce
+		mp->options = make_options_string(ap->path,
81b4ce
+				ap->kpipefd, str_offset, ap->flags);
81b4ce
 		if (!mp->options)
81b4ce
 			return MOUNT_OFFSET_OK;
81b4ce
-
81b4ce
-		if ((ap->flags & MOUNT_FLAG_STRICTEXPIRE) &&
81b4ce
-		    ((get_kver_major() == 5 && get_kver_minor() > 3) ||
81b4ce
-		     (get_kver_major() > 5))) {
81b4ce
-			char *tmp = realloc(mp->options, strlen(mp->options) + 12);
81b4ce
-			if (tmp) {
81b4ce
-				strcat(tmp, ",strictexpire");
81b4ce
-				mp->options = tmp;
81b4ce
-			}
81b4ce
-		}
81b4ce
-
81b4ce
-		if ((ap->flags & MOUNT_FLAG_IGNORE) &&
81b4ce
-		    ((get_kver_major() == 5 && get_kver_minor() > 4) ||
81b4ce
-		     (get_kver_major() > 5))) {
81b4ce
-			char *tmp = realloc(mp->options, strlen(mp->options) + 7);
81b4ce
-			if (tmp) {
81b4ce
-				strcat(tmp, ",ignore");
81b4ce
-				mp->options = tmp;
81b4ce
-			}
81b4ce
-		}
81b4ce
 	}
81b4ce
 
81b4ce
 	strcpy(mountpoint, root);
81b4ce
--- autofs-5.1.4.orig/daemon/indirect.c
81b4ce
+++ autofs-5.1.4/daemon/indirect.c
81b4ce
@@ -78,32 +78,13 @@ static int do_mount_autofs_indirect(stru
81b4ce
 		}
81b4ce
 	}
81b4ce
 
81b4ce
-	options = make_options_string(ap->path, ap->kpipefd, str_indirect);
81b4ce
+	options = make_options_string(ap->path,
81b4ce
+				ap->kpipefd, str_indirect, ap->flags);
81b4ce
 	if (!options) {
81b4ce
 		error(ap->logopt, "options string error");
81b4ce
 		goto out_err;
81b4ce
 	}
81b4ce
 
81b4ce
-	if ((ap->flags & MOUNT_FLAG_STRICTEXPIRE) &&
81b4ce
-	    ((get_kver_major() == 5 && get_kver_minor() > 3) ||
81b4ce
-	     (get_kver_major() > 5))) {
81b4ce
-		char *tmp = realloc(options, strlen(options) + 12);
81b4ce
-		if (tmp) {
81b4ce
-			strcat(tmp, ",strictexpire");
81b4ce
-			options = tmp;
81b4ce
-		}
81b4ce
-	}
81b4ce
-
81b4ce
-	if ((ap->flags & MOUNT_FLAG_IGNORE) &&
81b4ce
-	    ((get_kver_major() == 5 && get_kver_minor() > 4) ||
81b4ce
-	     (get_kver_major() > 5))) {
81b4ce
-		char *tmp = realloc(options, strlen(options) + 7);
81b4ce
-		if (tmp) {
81b4ce
-			strcat(tmp, ",ignore");
81b4ce
-			options = tmp;
81b4ce
-		}
81b4ce
-	}
81b4ce
-
81b4ce
 	/* In case the directory doesn't exist, try to mkdir it */
81b4ce
 	if (mkdir_path(root, mp_mode) < 0) {
81b4ce
 		if (errno != EEXIST && errno != EROFS) {
81b4ce
--- autofs-5.1.4.orig/include/mounts.h
81b4ce
+++ autofs-5.1.4/include/mounts.h
81b4ce
@@ -94,7 +94,8 @@ void free_amd_entry_list(struct list_hea
81b4ce
 unsigned int query_kproto_ver(void);
81b4ce
 unsigned int get_kver_major(void);
81b4ce
 unsigned int get_kver_minor(void);
81b4ce
-char *make_options_string(char *path, int kernel_pipefd, const char *extra);
81b4ce
+char *make_options_string(char *path, int pipefd,
81b4ce
+			  const char *type, unsigned int flags);
81b4ce
 char *make_mnt_name_string(char *path);
81b4ce
 int ext_mount_add(struct list_head *, const char *, unsigned int);
81b4ce
 int ext_mount_remove(struct list_head *, const char *);
81b4ce
--- autofs-5.1.4.orig/lib/mounts.c
81b4ce
+++ autofs-5.1.4/lib/mounts.c
81b4ce
@@ -599,43 +599,111 @@ void free_amd_entry_list(struct list_hea
81b4ce
 	}
81b4ce
 }
81b4ce
 
81b4ce
+static int cacl_max_options_len(unsigned int flags)
81b4ce
+{
81b4ce
+	unsigned int kver_major = get_kver_major();
81b4ce
+	unsigned int kver_minor = get_kver_minor();
81b4ce
+	int max_len;
81b4ce
+
81b4ce
+	/* %d and %u are maximum lenght of 10 and mount type is maximum
81b4ce
+	 * length of 9 (e. ",indirect").
81b4ce
+	 * The base temaplate is "fd=%d,pgrp=%u,minproto=5,maxproto=%d"
81b4ce
+	 * plus the length of mount type plus 1 for the NULL.
81b4ce
+	 */
81b4ce
+	max_len = 79 + 1;
81b4ce
+
81b4ce
+	if (kver_major < 5 || (kver_major == 5 && kver_minor < 4))
81b4ce
+		goto out;
81b4ce
+
81b4ce
+	/* maybe add ",strictexpire" */
81b4ce
+	if (flags & MOUNT_FLAG_STRICTEXPIRE)
81b4ce
+		max_len += 13;
81b4ce
+
81b4ce
+	if (kver_major == 5 && kver_minor < 5)
81b4ce
+		goto out;
81b4ce
+
81b4ce
+	/* maybe add ",ignore" */
81b4ce
+	if (flags & MOUNT_FLAG_IGNORE)
81b4ce
+		max_len += 7;
81b4ce
+out:
81b4ce
+	return max_len;
81b4ce
+}
81b4ce
+
81b4ce
 /*
81b4ce
  * Make common autofs mount options string
81b4ce
  */
81b4ce
-char *make_options_string(char *path, int pipefd, const char *extra)
81b4ce
+char *make_options_string(char *path, int pipefd,
81b4ce
+			  const char *type, unsigned int flags)
81b4ce
 {
81b4ce
+	unsigned int kver_major = get_kver_major();
81b4ce
+	unsigned int kver_minor = get_kver_minor();
81b4ce
 	char *options;
81b4ce
-	int len;
81b4ce
+	int max_len, len, new;
81b4ce
 
81b4ce
-	options = malloc(MAX_OPTIONS_LEN + 1);
81b4ce
+	max_len = cacl_max_options_len(flags);
81b4ce
+
81b4ce
+	options = malloc(max_len);
81b4ce
 	if (!options) {
81b4ce
 		logerr("can't malloc options string");
81b4ce
 		return NULL;
81b4ce
 	}
81b4ce
 
81b4ce
-	if (extra) 
81b4ce
-		len = snprintf(options, MAX_OPTIONS_LEN,
81b4ce
+	if (type)
81b4ce
+		len = snprintf(options, max_len,
81b4ce
 				options_template_extra,
81b4ce
 				pipefd, (unsigned) getpgrp(),
81b4ce
-				AUTOFS_MAX_PROTO_VERSION, extra);
81b4ce
+				AUTOFS_MAX_PROTO_VERSION, type);
81b4ce
 	else
81b4ce
-		len = snprintf(options, MAX_OPTIONS_LEN, options_template,
81b4ce
+		len = snprintf(options, max_len, options_template,
81b4ce
 			pipefd, (unsigned) getpgrp(),
81b4ce
 			AUTOFS_MAX_PROTO_VERSION);
81b4ce
 
81b4ce
-	if (len >= MAX_OPTIONS_LEN) {
81b4ce
-		logerr("buffer to small for options - truncated");
81b4ce
-		len = MAX_OPTIONS_LEN - 1;
81b4ce
+	if (len < 0)
81b4ce
+		goto error_out;
81b4ce
+
81b4ce
+	if (len >= max_len)
81b4ce
+		goto truncated;
81b4ce
+
81b4ce
+	if (kver_major < 5 || (kver_major == 5 && kver_minor < 4))
81b4ce
+		goto out;
81b4ce
+
81b4ce
+	/* maybe add ",strictexpire" */
81b4ce
+	if (flags & MOUNT_FLAG_STRICTEXPIRE) {
81b4ce
+		new = snprintf(options + len,
81b4ce
+			       max_len, "%s", ",strictexpire");
81b4ce
+		if (new < 0)
81b4ce
+		       goto error_out;
81b4ce
+		len += new;
81b4ce
+		if (len >= max_len)
81b4ce
+			goto truncated;
81b4ce
 	}
81b4ce
 
81b4ce
-	if (len < 0) {
81b4ce
-		logerr("failed to malloc autofs mount options for %s", path);
81b4ce
-		free(options);
81b4ce
-		return NULL;
81b4ce
+	if (kver_major == 5 && kver_minor < 5)
81b4ce
+		goto out;
81b4ce
+
81b4ce
+	/* maybe add ",ignore" */
81b4ce
+	if (flags & MOUNT_FLAG_IGNORE) {
81b4ce
+		new = snprintf(options + len,
81b4ce
+			       max_len, "%s", ",ignore");
81b4ce
+		if (new < 0)
81b4ce
+		       goto error_out;
81b4ce
+		len += new;
81b4ce
+		if (len >= max_len)
81b4ce
+			goto truncated;
81b4ce
 	}
81b4ce
+out:
81b4ce
 	options[len] = '\0';
81b4ce
-
81b4ce
 	return options;
81b4ce
+
81b4ce
+truncated:
81b4ce
+	logerr("buffer to small for options - truncated");
81b4ce
+	len = max_len -1;
81b4ce
+	goto out;
81b4ce
+
81b4ce
+error_out:
81b4ce
+	logerr("error constructing mount options string for %s", path);
81b4ce
+	free(options);
81b4ce
+	return NULL;
81b4ce
 }
81b4ce
 
81b4ce
 char *make_mnt_name_string(char *path)