Skip to contentSkip to conversation

exportfs: Properly parse IPv6 literal strings with null termination

PATCH In review Todd Vierling 2 participants 2 messages Sep 18, 2014 – Sep 19, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 exportfs: Properly parse IPv6 literal strings with null termination

The original implementation was using strncpy() with a truncation length to an uninitialized stack buffer, leaving a string that was only null terminated by luck.

While here, change to use no-copy semantics (no extra buffer) to avoid buffer overflows altogether. exportfs already modifies argv contents elsewhere, so this doesn't break anything anew.

Fixes 4663c648 (exportfs: Support raw IPv6 addresses with "client:/path")
---
 utils/exportfs/exportfs.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
utils/exportfs/exportfs.c +8 -10
index e7d1ac8..bdea12b 100644
@@ -351,16 +351,15 @@ static int exportfs_generic(char *arg, char *options, int verbose)
 
 static int exportfs_ipv6(char *arg, char *options, int verbose)
 {
-	char *path, *c, hname[NI_MAXHOST + strlen("/128")];
+	char *path, *c;
 
 	arg++;
 	c = strchr(arg, ']');
 	if (c == NULL)
 		return 1;
-	strncpy(hname, arg, c - arg);
 
 	/* no colon means this is a wildcarded DNS hostname */
-	if (strchr(hname, ':') == NULL)
+	if (memchr(arg, ':', c - arg) == NULL)
 		return exportfs_generic(--arg, options, verbose);
 
 	path = strstr(c, ":/");
@@ -370,9 +369,9 @@ static int exportfs_ipv6(char *arg, char *options, int verbose)
 
 	/* if there's anything between the closing brace and the
 	 * path separator, it's probably a prefix length */
-	strcat(hname, ++c);
+	memmove(c, c + 1, path - c);
 
-	exportfs_parsed(hname, path, options, verbose);
+	exportfs_parsed(arg, path, options, verbose);
 	return 0;
 }
 
@@ -458,16 +457,15 @@ static int unexportfs_generic(char *arg, int verbose)
 
 static int unexportfs_ipv6(char *arg, int verbose)
 {
-	char *path, *c, hname[NI_MAXHOST + strlen("/128")];
+	char *path, *c;
 
 	arg++;
 	c = strchr(arg, ']');
 	if (c == NULL)
 		return 1;
-	strncpy(hname, arg, c - arg);
 
 	/* no colon means this is a wildcarded DNS hostname */
-	if (strchr(hname, ':') == NULL)
+	if (memchr(arg, ':', c - arg) == NULL)
 		return unexportfs_generic(--arg, verbose);
 
 	path = strstr(c, ":/");
@@ -477,9 +475,9 @@ static int unexportfs_ipv6(char *arg, int verbose)
 
 	/* if there's anything between the closing brace and the
 	 * path separator, it's probably a prefix length */
-	strcat(hname, ++c);
+	memmove(c, c + 1, path - c);
 
-	unexportfs_parsed(hname, path, verbose);
+	unexportfs_parsed(arg, path, verbose);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Todd Vierling

The original implementation was using strncpy() with a truncation length to an uninitialized stack buffer, leaving a string that was only null terminated by luck.

Quoted 8 lines

While here, change to use no-copy semantics (no extra buffer) to avoid buffer overflows altogether. exportfs already modifies argv contents elsewhere, so this doesn't break anything anew.

Fixes 4663c648 (exportfs: Support raw IPv6 addresses with "client:/path")

Committed...

steved.

Quoted 71 lines
---
 utils/exportfs/exportfs.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
utils/exportfs/exportfs.c +8 -10
index e7d1ac8..bdea12b 100644
@@ -351,16 +351,15 @@ static int exportfs_generic(char *arg, char *options, int verbose)
 
 static int exportfs_ipv6(char *arg, char *options, int verbose)
 {
-	char *path, *c, hname[NI_MAXHOST + strlen("/128")];
+	char *path, *c;
 
 	arg++;
 	c = strchr(arg, ']');
 	if (c == NULL)
 		return 1;
-	strncpy(hname, arg, c - arg);
 
 	/* no colon means this is a wildcarded DNS hostname */
-	if (strchr(hname, ':') == NULL)
+	if (memchr(arg, ':', c - arg) == NULL)
 		return exportfs_generic(--arg, options, verbose);
 
 	path = strstr(c, ":/");
@@ -370,9 +369,9 @@ static int exportfs_ipv6(char *arg, char *options, int verbose)
 
 	/* if there's anything between the closing brace and the
 	 * path separator, it's probably a prefix length */
-	strcat(hname, ++c);
+	memmove(c, c + 1, path - c);
 
-	exportfs_parsed(hname, path, options, verbose);
+	exportfs_parsed(arg, path, options, verbose);
 	return 0;
 }
 
@@ -458,16 +457,15 @@ static int unexportfs_generic(char *arg, int verbose)
 
 static int unexportfs_ipv6(char *arg, int verbose)
 {
-	char *path, *c, hname[NI_MAXHOST + strlen("/128")];
+	char *path, *c;
 
 	arg++;
 	c = strchr(arg, ']');
 	if (c == NULL)
 		return 1;
-	strncpy(hname, arg, c - arg);
 
 	/* no colon means this is a wildcarded DNS hostname */
-	if (strchr(hname, ':') == NULL)
+	if (memchr(arg, ':', c - arg) == NULL)
 		return unexportfs_generic(--arg, verbose);
 
 	path = strstr(c, ":/");
@@ -477,9 +475,9 @@ static int unexportfs_ipv6(char *arg, int verbose)
 
 	/* if there's anything between the closing brace and the
 	 * path separator, it's probably a prefix length */
-	strcat(hname, ++c);
+	memmove(c, c + 1, path - c);
 
-	unexportfs_parsed(hname, path, verbose);
+	unexportfs_parsed(arg, path, verbose);
 	return 0;
 }