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

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