fsloc: fix crash on an invalid entry in a locations list
Subsystems (2)
PATCH 1/1 fsloc: fix crash on an invalid entry in a locations list
From: Haolin Xue <[email protected]>
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[].
--- 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
@@ -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
@@ -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 -EXTRA_DIST = test-lib.sh $(TESTS) +TESTS = t0001-statd-basic-mon-unmon.sh fsloc_parse_test +EXTRA_DIST = test-lib.sh t0001-statd-basic-mon-unmon.sh
tests/fsloc_parse_test.c new file +152 -0
152 lines not on this page · raw message