Skip to contentSkip to conversation

fsloc: fix crash on an invalid entry in a locations list

PATCH New Haolin Xue 1 participant 1 message Nov 17

Subsystems (2)
lore
Share & tools
lore

Haolin Xue

author
#0 lore raw

PATCH v2 1/1 fsloc: fix crash on an invalid entry in a locations list

parse_list() stores each parsed entry in res->h_mp[i] using the loop index, but only increments res->h_num for entries that pass validation. An entry that is rejected and skipped with "continue" therefore leaves h_mp[i] unset while i has already moved on, so a later valid entry is stored above the slot that h_num accounts for. Every consumer walks h_mp[0] .. h_mp[h_num - 1] (replicas_print(), release_replicas() and write_fsloc()), so one skipped entry makes them dereference an uninitialized element of the malloc()ed servers structure.

With a malformed "refer=" or "replicas=" option in /etc/exports, e.g.

/export *(refer=/export:/export@server2)

where the first location is missing its host, rpc.mountd dereferences heap garbage while it (re)builds the export cache: it either takes a SIGSEGV or aborts in free() from release_replicas(). A typo in the exports file should at worst produce a warning, not kill the daemon and lose the whole export, valid locations included.

Number the array slots independently so that h_mp[] is always dense and h_num always matches it. Valid lists are unaffected: FSLOC_MAX_LIST still bounds the number of entries kept, and rejected entries are still reported with the same L_WARNING.

Add a tests/ regression case that feeds malformed lists to replicas_lookup() and checks that the surviving locations are parsed with a dense h_mp[].

Fixes ec75fd41fcf2 ("Add missing fsloc.[ch] files - oops.")

---
Changes in v2:
- Rebased onto the current upstream master (c1f521c4, tag nfs-utils-3-1-2.rc4). support/export/fsloc.c is untouched upstream since v1 was posted, so that hunk re-applies verbatim (blob 1b869b60).
- Resolved one conflict, in tests/Makefile.am: upstream gained a new test (t0003-get-maintainer.sh, commit bbb50905, 2026-11-13) after v1 was posted. The rebased patch keeps t0003 in TESTS and appends fsloc_parse_test, and lists the shell test scripts explicitly in EXTRA_DIST so that the built fsloc_parse_test binary is not distributed; v1 had replaced the whole $(TESTS) list, which would have dropped t0003 from the tarball.
- No change to the fix or to the test's expectations.

Verified on this base: "make check" passes 3/3 (t0001-statd-basic-mon-unmon.sh, t0003-get-maintainer.sh, fsloc_parse_test); restoring only the old fsloc.c makes fsloc_parse_test abort with SIGABRT ("double free or corruption (out)").

 support/export/fsloc.c   |   6 +-
 tests/Makefile.am        |  11 ++-
 tests/fsloc_parse_test.c | 152 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 163 insertions(+), 6 deletions(-)
 create mode 100644 tests/fsloc_parse_test.c
support/export/fsloc.c +3 -3
index 1b869b60..a9e5a86b 100644
@@ -82,7 +82,7 @@ static struct servers *method_stub(char *key)
  */
 static struct servers *parse_list(char **list)
 {
-	int i;
+	int i, n;
 	struct servers *res;
 	struct mount_point *mp;
 	char *cp;
@@ -93,7 +93,7 @@ static struct servers *parse_list(char **list)
 	res->h_num = 0;
 
 	/* parse each of the answers in sucession. */
-	for (i=0; list[i] && i<FSLOC_MAX_LIST; i++) {
+	for (i = 0, n = 0; list[i] && n < FSLOC_MAX_LIST; i++) {
 		mp = calloc(1, sizeof(struct mount_point));
 		if (!mp) {
 			release_replicas(res);
@@ -105,7 +105,7 @@ static struct servers *parse_list(char **list)
 			free(mp);
 			continue; /* XXX Need better error handling */
 		}
-		res->h_mp[i] = mp;
+		res->h_mp[n++] = mp;
 		res->h_num++;
 		mp->h_path = strndup(list[i], cp - list[i]);
 		cp++;
tests/Makefile.am +8 -3
index 851b88ac..29a639dd 100644
@@ -1,15 +1,20 @@
 ## Process this file with automake to produce Makefile.in
 
-check_PROGRAMS = statdb_dump
+check_PROGRAMS = statdb_dump fsloc_parse_test
 statdb_dump_SOURCES = statdb_dump.c
 
 statdb_dump_LDADD = ../support/nfs/.libs/libnfs.a \
 		    ../support/nsm/libnsm.a \
 		    ../support/misc/libmisc.a $(LIBCAP)
 
+fsloc_parse_test_SOURCES = fsloc_parse_test.c
+fsloc_parse_test_LDADD = ../support/export/libexport.a \
+			 ../support/nfs/.libs/libnfs.a \
+			 ../support/misc/libmisc.a $(LIBCAP)
+
 SUBDIRS = nsm_client
 
 MAINTAINERCLEANFILES = Makefile.in
 
-TESTS = t0001-statd-basic-mon-unmon.sh t0003-get-maintainer.sh
-EXTRA_DIST = test-lib.sh $(TESTS)
+TESTS = t0001-statd-basic-mon-unmon.sh t0003-get-maintainer.sh fsloc_parse_test
+EXTRA_DIST = test-lib.sh t0001-statd-basic-mon-unmon.sh t0003-get-maintainer.sh
tests/fsloc_parse_test.c new file +152 -0

152 lines not on this page · raw message

Signature · 1 line
2.43.0