Skip to contentSkip to conversation

Two hardening fixes for junction handling

PATCH In review Scott Mayhew 2 participants 5 messages Sep 3 – Sep 16

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

cover Two hardening fixes for junction handling

The first patch fixes a buffer overrun in junction_parse_xml_read() and the second fixes a use-after-free when parsing multi-location junctions in locations_to_fslocdata().

Scott Mayhew (2):
junction: fix buffer over-read in junction_parse_xml_read()
junction: fix use-after-free in locations_to_fslocdata()

 support/export/cache.c      | 6 ++++++
 support/junction/junction.c | 3 ++-
 2 files changed, 8 insertions(+), 1 deletion(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 junction: fix buffer over-read in junction_parse_xml_read()

junction_get_xattr() returns an opaque buffer, but junction_parse_xml_read() treats it as a string by passing it to xlog(). Fix it by allocating an extra byte and null-terminating the buffer in junction_get_xattr().

---
 support/junction/junction.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
support/junction/junction.c +2 -1
index c1ec8ff8..04a8ebf1 100644
@@ -293,7 +293,7 @@ junction_get_xattr(int fd, const char *path, const char *name, void **contents,
 		return FEDFS_ERR_ACCESS;
 	}
 
-	xattrbuf = malloc((size_t)len);
+	xattrbuf = malloc((size_t)len + 1);
 	if (xattrbuf == NULL) {
 		xlog(D_GENERAL, "%s: failed to get buffer for xattr %s on %s",
 			__func__, name, path);
@@ -307,6 +307,7 @@ junction_get_xattr(int fd, const char *path, const char *name, void **contents,
 		return FEDFS_ERR_ACCESS;
 	}
 
+	((char *) xattrbuf)[len] = '\0';
 	xlog(D_CALL, "%s: read xattr %s from path %s",
 			__func__, name, path);
 	*contents = xattrbuf;
Signature · 1 line
2.55.0
#2 lore raw

Re: [nfs-utils PATCH 1/2] junction: fix buffer over-read in junction_parse_xml_read()

Quoted 6 lines · Scott Mayhew

junction_get_xattr() returns an opaque buffer, but junction_parse_xml_read() treats it as a string by passing it to xlog(). Fix it by allocating an extra byte and null-terminating the buffer in junction_get_xattr().

Committed... (tag: nfs-utils-2-9-3-rc4)

steved.

Quoted 25 lines
---
  support/junction/junction.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
support/junction/junction.c +2 -1
index c1ec8ff8..04a8ebf1 100644
@@ -293,7 +293,7 @@ junction_get_xattr(int fd, const char *path, const char *name, void **contents,
  		return FEDFS_ERR_ACCESS;
  	}
 
-	xattrbuf = malloc((size_t)len);
+	xattrbuf = malloc((size_t)len + 1);
  	if (xattrbuf == NULL) {
  		xlog(D_GENERAL, "%s: failed to get buffer for xattr %s on %s",
  			__func__, name, path);
@@ -307,6 +307,7 @@ junction_get_xattr(int fd, const char *path, const char *name, void **contents,
  		return FEDFS_ERR_ACCESS;
  	}
 
+	((char *) xattrbuf)[len] = '\0';
  	xlog(D_CALL, "%s: read xattr %s from path %s",
  			__func__, name, path);
  	*contents = xattrbuf;
#3 lore raw

PATCH 2/2 junction: fix use-after-free in locations_to_fslocdata()

In reply to Scott Mayhew

While parsing a multi-location junction, locations_to_fslocdata() sets last_path = root_path and then frees root_path, leaving last_path dangling. On the next iteration, strcmp(rootpath, last_path) dereferences freed memory.

Fix it by transferring ownership of root_path to last_path when they should be aliased.

---
 support/export/cache.c | 6 ++++++
 1 file changed, 6 insertions(+)
support/export/cache.c +6 -0
index 059f48a7..9f71c1dd 100644
@@ -2806,19 +2806,25 @@ static bool locations_to_fslocdata(struct nfs_fsloc_set *locations,
 			}
 			remaining -= (size_t)len;
 			ptr += len;
+			free(last_path);
 			last_path = rootpath;
+			rootpath = NULL;
 		}
 
 		seen = true;
 		free(rootpath);
+		rootpath = NULL;
 		free(server);
+		server = NULL;
 	}
 
+	free(last_path);
 	xlog(D_CALL, "%s: fslocdata='%s', ttl=%d",
 		__func__, fslocdata, *ttl);
 	return seen;
 
 out_false:
+	free(last_path);
 	free(rootpath);
 	free(server);
 	return false;
Signature · 1 line
2.55.0
#4 lore raw

Re: [nfs-utils PATCH 2/2] junction: fix use-after-free in locations_to_fslocdata()

Quoted 9 lines · Scott Mayhew

While parsing a multi-location junction, locations_to_fslocdata() sets last_path = root_path and then frees root_path, leaving last_path dangling. On the next iteration, strcmp(rootpath, last_path) dereferences freed memory.

Fix it by transferring ownership of root_path to last_path when they should be aliased.

Committed... (tag: nfs-utils-2-9-3-rc4)

steved.

Quoted 34 lines
---
  support/export/cache.c | 6 ++++++
  1 file changed, 6 insertions(+)
support/export/cache.c +6 -0
index 059f48a7..9f71c1dd 100644
@@ -2806,19 +2806,25 @@ static bool locations_to_fslocdata(struct nfs_fsloc_set *locations,
  			}
  			remaining -= (size_t)len;
  			ptr += len;
+			free(last_path);
  			last_path = rootpath;
+			rootpath = NULL;
  		}
 
  		seen = true;
  		free(rootpath);
+		rootpath = NULL;
  		free(server);
+		server = NULL;
  	}
 
+	free(last_path);
  	xlog(D_CALL, "%s: fslocdata='%s', ttl=%d",
  		__func__, fslocdata, *ttl);
  	return seen;
 
  out_false:
+	free(last_path);
  	free(rootpath);
  	free(server);
  	return false;