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

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