Skip to contentSkip to conversation

nfs-utils fixes

PATCH In review [email protected] 3 participants 11 messages Apr 16, 2020 – May 8, 2020

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover nfs-utils fixes

From: Trond Myklebust <[email protected]>

This patchset fixes a couple of missed API changes in mountd to ensure that the [exports]rootdir root jail works correctly. It fixes up the 'same_path' function, as well as 'uuid_by_path'. It also improves the error handling, and tries to distinguish between bona fide path resolution problems, and other transient issues in order to avoid having knfsd return spurious ESTALE errors.

Trond Myklebust (7):
mountd: Add a helper nfsd_path_statfs64() for uuid_by_path()
nfsd: Support running nfsd_name_to_handle_at() in the root jail
mountd: Fix up path checking helper same_path()
Fix autoconf probe for 'struct nfs_filehandle'
mountd: Ensure dump_to_cache() sets errno appropriately
mountd: Ignore transient and non-fatal filesystem errors in nfsd_fh()
mountd: Check the stat() return values in match_fsid()

 configure.ac                |   7 +-
 support/include/nfsd_path.h |   9 ++
 support/misc/nfsd_path.c    | 109 ++++++++++++++++++++++
 utils/mountd/cache.c        | 174 ++++++++++++++++++++++++------------
 4 files changed, 242 insertions(+), 57 deletions(-)
Signature · 1 line
2.25.2
author maintainer
#1 lore raw

PATCH 1/7 mountd: Add a helper nfsd_path_statfs64() for uuid_by_path()

From: Trond Myklebust <[email protected]>

Ensure uuid_by_path() works correctly when 'rootdir' is set in the [exports] section of nfs.conf.

---
 support/include/nfsd_path.h |  5 +++++
 support/misc/nfsd_path.c    | 43 +++++++++++++++++++++++++++++++++++++
 utils/mountd/cache.c        |  2 +-
 3 files changed, 49 insertions(+), 1 deletion(-)
support/include/nfsd_path.h +5 -0
index b42416bbff58..8331ff96a277 100644
@@ -6,6 +6,8 @@
 
 #include <sys/stat.h>
 
+struct statfs64;
+
 void 		nfsd_path_init(void);
 
 const char *	nfsd_path_nfsd_rootdir(void);
@@ -15,6 +17,9 @@ char *		nfsd_path_prepend_dir(const char *dir, const char *pathname);
 int 		nfsd_path_stat(const char *pathname, struct stat *statbuf);
 int 		nfsd_path_lstat(const char *pathname, struct stat *statbuf);
 
+int		nfsd_path_statfs64(const char *pathname,
+				   struct statfs64 *statbuf);
+
 char *		nfsd_realpath(const char *path, char *resolved_path);
 
 ssize_t		nfsd_path_read(int fd, char *buf, size_t len);
support/misc/nfsd_path.c +43 -0

55 lines not on this page · raw message

utils/mountd/cache.c +1 -1
index 8f54e37b7936..7d8657c91323 100644
@@ -352,7 +352,7 @@ static int uuid_by_path(char *path, int type, size_t uuidlen, char *uuid)
 	const char *val;
 	int rc;
 
-	rc = statfs64(path, &st);
+	rc = nfsd_path_statfs64(path, &st);
 
 	if (type == 0 && rc == 0) {
 		const unsigned long *bad;
Signature · 1 line
2.25.2
author maintainer
#2 lore raw

PATCH 2/7 nfsd: Support running nfsd_name_to_handle_at() in the root jail

From: Trond Myklebust <[email protected]>

When running nfsd_name_to_handle_at(), we usually want to see the same namespace as knfsd, so add helpers.

---
 support/include/nfsd_path.h |  4 +++
 support/misc/nfsd_path.c    | 66 +++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)
support/include/nfsd_path.h +4 -0
index 8331ff96a277..3b73aadd8af7 100644
@@ -6,6 +6,7 @@
 
 #include <sys/stat.h>
 
+struct file_handle;
 struct statfs64;
 
 void 		nfsd_path_init(void);
@@ -25,4 +26,7 @@ char *		nfsd_realpath(const char *path, char *resolved_path);
 ssize_t		nfsd_path_read(int fd, char *buf, size_t len);
 ssize_t		nfsd_path_write(int fd, const char *buf, size_t len);
 
+int		nfsd_name_to_handle_at(int fd, const char *path,
+				       struct file_handle *fh,
+				       int *mount_id, int flags);
 #endif
support/misc/nfsd_path.c +66 -0

81 lines not on this page · raw message

Signature · 1 line
2.25.2
author maintainer
#4 lore raw

PATCH 4/7 Fix autoconf probe for 'struct nfs_filehandle'

In reply to [email protected]

From: Trond Myklebust <[email protected]>

It was failing because fcntl.h is not one of the standard includes.

---
 configure.ac | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
configure.ac +6 -1
index 00b32800c526..df88e58fd0d4 100644
@@ -531,7 +531,12 @@ AC_TYPE_PID_T
 AC_TYPE_SIZE_T
 AC_HEADER_TIME
 AC_STRUCT_TM
-AC_CHECK_TYPES([struct file_handle])
+AC_CHECK_TYPES([struct file_handle], [], [], [[
+		#define _GNU_SOURCE
+		#include <sys/types.h>
+		#include <sys/stat.h>
+		#include <fcntl.h>
+	]])
 
 dnl *************************************************************
 dnl Check for functions
Signature · 1 line
2.25.2
author maintainer
#5 lore raw

PATCH 5/7 mountd: Ensure dump_to_cache() sets errno appropriately

From: Trond Myklebust <[email protected]>

cache_write() will set errno if it returns -1, so that callers can handle errors appropriately, however dump_to_cache() needs to do so too.

---
 utils/mountd/cache.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
utils/mountd/cache.c +15 -4
index 94e9e44b46b9..0f323226b12a 100644
@@ -936,12 +936,13 @@ static void write_secinfo(char **bp, int *blen, struct exportent *ep, int flag_m
 
 }
 
-static int dump_to_cache(int f, char *buf, int buflen, char *domain,
+static int dump_to_cache(int f, char *buf, int blen, char *domain,
 			 char *path, struct exportent *exp, int ttl)
 {
 	char *bp = buf;
-	int blen = buflen;
 	time_t now = time(0);
+	size_t buflen;
+	ssize_t err;
 
 	if (ttl <= 1)
 		ttl = DEFAULT_TTL;
@@ -974,8 +975,18 @@ static int dump_to_cache(int f, char *buf, int buflen, char *domain,
 	} else
 		qword_adduint(&bp, &blen, now + ttl);
 	qword_addeol(&bp, &blen);
-	if (blen <= 0) return -1;
-	if (cache_write(f, buf, bp - buf) != bp - buf) return -1;
+	if (blen <= 0) {
+		errno = ENOBUFS;
+		return -1;
+	}
+	buflen = bp - buf;
+	err = cache_write(f, buf, buflen);
+	if (err < 0)
+		return err;
+	if ((size_t)err != buflen) {
+		errno = ENOSPC;
+		return -1;
+	}
 	return 0;
 }
 
Signature · 1 line
2.25.2
author maintainer
#6 lore raw

PATCH 6/7 mountd: Ignore transient and non-fatal filesystem errors in nfsd_fh()

From: Trond Myklebust <[email protected]>

In nfsd_fh(), if the error returned by the downcall is transient, then we should ignore it. Only reject the export if the filesystem path is truly not exportable.
This fixes a case where we can see spurious NFSERR_STALE errors being returned by knfsd.

---
 utils/mountd/cache.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
utils/mountd/cache.c +13 -12
index 0f323226b12a..79d3ee085a90 100644
@@ -843,8 +843,14 @@ static void nfsd_fh(int f)
 			}
 		}
 	}
-	if (found && 
-	    found->e_mountpoint &&
+
+	if (!found) {
+		/* The missing dev could be what we want, so just be
+		 * quiet rather than returning stale yet
+		 */
+		if (dev_missing)
+			goto out;
+	} else if (found->e_mountpoint &&
 	    !is_mountpoint(found->e_mountpoint[0]?
 			   found->e_mountpoint:
 			   found->e_path)) {
@@ -855,17 +861,12 @@ static void nfsd_fh(int f)
 		 */
 		/* FIXME we need to make sure we re-visit this later */
 		goto out;
+	} else if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0) {
+		if (!path_lookup_error(errno))
+			goto out;
+		/* The kernel is saying the path is unexportable */
+		found = NULL;
 	}
-	if (!found && dev_missing) {
-		/* The missing dev could be what we want, so just be
-		 * quite rather than returning stale yet
-		 */
-		goto out;
-	}
-
-	if (found)
-		if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0)
-			found = 0;
 
 	bp = buf; blen = sizeof(buf);
 	qword_add(&bp, &blen, dom);
Signature · 1 line
2.25.2
author maintainer
#7 lore raw

PATCH 7/7 mountd: Check the stat() return values in match_fsid()

From: Trond Myklebust <[email protected]>

Propagate errors from the stat() calls in match_fsid() so that the caller can be more careful about how they are handled.

---
 utils/mountd/cache.c | 45 +++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 15 deletions(-)
utils/mountd/cache.c +30 -15
index 79d3ee085a90..6cba2883026f 100644
@@ -659,55 +659,66 @@ static int parse_fsid(int fsidtype, int fsidlen, char *fsid,
 	return 0;
 }
 
-static bool match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
+static int match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
 {
 	struct stat stb;
 	int type;
 	char u[16];
 
 	if (nfsd_path_stat(path, &stb) != 0)
-		return false;
+		goto path_error;
 	if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode))
-		return false;
+		goto nomatch;
 
 	switch (parsed->fsidtype) {
 	case FSID_DEV:
 	case FSID_MAJOR_MINOR:
 	case FSID_ENCODE_DEV:
 		if (stb.st_ino != parsed->inode)
-			return false;
+			goto nomatch;
 		if (parsed->major != major(stb.st_dev) ||
 		    parsed->minor != minor(stb.st_dev))
-			return false;
-		return true;
+			goto nomatch;
+		goto match;
 	case FSID_NUM:
 		if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
 		     exp->m_export.e_fsid != parsed->fsidnum))
-			return false;
-		return true;
+			goto nomatch;
+		goto match;
 	case FSID_UUID4_INUM:
 	case FSID_UUID16_INUM:
 		if (stb.st_ino != parsed->inode)
-			return false;
+			goto nomatch;
 		goto check_uuid;
 	case FSID_UUID8:
 	case FSID_UUID16:
-		if (!is_mountpoint(path))
-			return false;
+		errno = 0;
+		if (!is_mountpoint(path)) {
+			if (!errno)
+				goto nomatch;
+			goto path_error;
+		}
 	check_uuid:
 		if (exp->m_export.e_uuid) {
 			get_uuid(exp->m_export.e_uuid, parsed->uuidlen, u);
 			if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
-				return true;
+				goto match;
 		}
 		else
 			for (type = 0;
 			     uuid_by_path(path, type, parsed->uuidlen, u);
 			     type++)
 				if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
-					return true;
+					goto match;
 	}
-	return false;
+nomatch:
+	return 0;
+match:
+	return 1;
+path_error:
+	if (path_lookup_error(errno))
+		goto nomatch;
+	return -1;
 }
 
 static struct addrinfo *lookup_client_addr(char *dom)
@@ -815,8 +826,12 @@ static void nfsd_fh(int f)
 					   exp->m_export.e_path))
 				dev_missing ++;
 
-			if (!match_fsid(&parsed, exp, path))
+			switch(match_fsid(&parsed, exp, path)) {
+			case 0:
 				continue;
+			case -1:
+				goto out;
+			}
 			if (is_ipaddr_client(dom)
 					&& !ipaddr_client_matches(exp, ai))
 				continue;
Signature · 1 line
2.25.2
author maintainer
#3 lore raw

PATCH 3/7 mountd: Fix up path checking helper same_path()

In reply to [email protected]

From: Trond Myklebust <[email protected]>

Convert 'same_path()' so that it works when 'rootdir' is set in the [exports] section of nfs.conf.

---
 utils/mountd/cache.c | 83 +++++++++++++++++++++++++++++++-------------
 1 file changed, 59 insertions(+), 24 deletions(-)
utils/mountd/cache.c +59 -24
index 7d8657c91323..94e9e44b46b9 100644
@@ -72,6 +72,18 @@ static ssize_t cache_write(int fd, const char *buf, size_t len)
 	return nfsd_path_write(fd, buf, len);
 }
 
+static bool path_lookup_error(int err)
+{
+	switch (err) {
+	case ELOOP:
+	case ENAMETOOLONG:
+	case ENOENT:
+	case ENOTDIR:
+		return 1;
+	}
+	return 0;
+}
+
 /*
  * Support routines for text-based upcalls.
  * Fields are separated by spaces.
@@ -430,23 +442,9 @@ static inline int count_slashes(char *p)
 	return cnt;
 }
 
-static int same_path(char *child, char *parent, int len)
+#if defined(HAVE_STRUCT_FILE_HANDLE)
+static int check_same_path_by_handle(const char *child, const char *parent)
 {
-	static char p[PATH_MAX];
-	struct stat sc, sp;
-
-	if (len <= 0)
-		len = strlen(child);
-	strncpy(p, child, len);
-	p[len] = 0;
-	if (strcmp(p, parent) == 0)
-		return 1;
-
-	/* If number of '/' are different, they must be different */
-	if (count_slashes(p) != count_slashes(parent))
-		return 0;
-
-#if defined(HAVE_NAME_TO_HANDLE_AT) && defined(HAVE_STRUCT_FILE_HANDLE)
 	struct {
 		struct file_handle fh;
 		unsigned char handle[128];
@@ -455,13 +453,17 @@ static int same_path(char *child, char *parent, int len)
 
 	fchild.fh.handle_bytes = 128;
 	fparent.fh.handle_bytes = 128;
-	if (name_to_handle_at(AT_FDCWD, p, &fchild.fh, &mnt_child, 0) != 0) {
-		if (errno == ENOSYS)
-			goto fallback;
-		return 0;
+
+	/* This process should have the CAP_DAC_READ_SEARCH capability */
+	if (nfsd_name_to_handle_at(AT_FDCWD, child, &fchild.fh, &mnt_child, 0) < 0)
+		return -1;
+	if (nfsd_name_to_handle_at(AT_FDCWD, parent, &fparent.fh, &mnt_parent, 0) < 0) {
+		/* If the child resolved, but the parent did not, they differ */
+		if (path_lookup_error(errno))
+			return 0;
+		/* Otherwise, we just don't know */
+		return -1;
 	}
-	if (name_to_handle_at(AT_FDCWD, parent, &fparent.fh, &mnt_parent, 0) != 0)
-		return 0;
 
 	if (mnt_child != mnt_parent ||
 	    fchild.fh.handle_bytes != fparent.fh.handle_bytes ||
@@ -471,14 +473,24 @@ static int same_path(char *child, char *parent, int len)
 		return 0;
 
 	return 1;
-fallback:
+}
+#else
+static int check_same_path_by_handle(const char *child, const char *parent)
+{
+	errno = ENOSYS;
+	return -1;
+}
 #endif
 
+static int check_same_path_by_inode(const char *child, const char *parent)
+{
+	struct stat sc, sp;
+
 	/* This is nearly good enough.  However if a directory is
 	 * bind-mounted in two places and both are exported, it
 	 * could give a false positive
 	 */
-	if (nfsd_path_lstat(p, &sc) != 0)
+	if (nfsd_path_lstat(child, &sc) != 0)
 		return 0;
 	if (nfsd_path_lstat(parent, &sp) != 0)
 		return 0;
@@ -490,6 +502,29 @@ fallback:
 	return 1;
 }
 
+static int same_path(char *child, char *parent, int len)
+{
+	static char p[PATH_MAX];
+	int err;
+
+	if (len <= 0)
+		len = strlen(child);
+	strncpy(p, child, len);
+	p[len] = 0;
+	if (strcmp(p, parent) == 0)
+		return 1;
+
+	/* If number of '/' are different, they must be different */
+	if (count_slashes(p) != count_slashes(parent))
+		return 0;
+
+	/* Try to use filehandle approach before falling back to stat() */
+	err = check_same_path_by_handle(p, parent);
+	if (err != -1)
+		return err;
+	return check_same_path_by_inode(p, parent);
+}
+
 static int is_subdirectory(char *child, char *parent)
 {
 	/* Check is child is strictly a subdirectory of
Signature · 1 line
2.25.2
#8 lore raw

Re: [PATCH 6/7] mountd: Ignore transient and non-fatal filesystem errors in nfsd_fh()

In reply to [email protected]
Quoted 47 lines · On Thu, Apr 16, 2020 at 06:12:51PM -0400, [email protected] wrote:

From: Trond Myklebust <[email protected]>

In nfsd_fh(), if the error returned by the downcall is transient, then we should ignore it. Only reject the export if the filesystem path is truly not exportable.
This fixes a case where we can see spurious NFSERR_STALE errors being returned by knfsd.

---
 utils/mountd/cache.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
utils/mountd/cache.c +13 -5
index 0f323226b12a..79d3ee085a90 100644
@@ -843,8 +843,14 @@ static void nfsd_fh(int f)
 			}
 		}
 	}
-	if (found && 
-	    found->e_mountpoint &&
+
+	if (!found) {
+		/* The missing dev could be what we want, so just be
+		 * quiet rather than returning stale yet
+		 */
+		if (dev_missing)
+			goto out;
+	} else if (found->e_mountpoint &&
 	    !is_mountpoint(found->e_mountpoint[0]?
 			   found->e_mountpoint:
 			   found->e_path)) {
@@ -855,17 +861,12 @@ static void nfsd_fh(int f)
 		 */
 		/* FIXME we need to make sure we re-visit this later */
 		goto out;
+	} else if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0) {
+		if (!path_lookup_error(errno))
+			goto out;
+		/* The kernel is saying the path is unexportable */
+		found = NULL;
 	}
-	if (!found && dev_missing) {
-		/* The missing dev could be what we want, so just be
-		 * quite rather than returning stale yet

utils/mountd/cache.c static void nfsd_fh(int f) · line 872

Quoted 10 lines
utils/mountd/cache.c +0 -7
-		 */
-		goto out;
-	}
-
-	if (found)
-		if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0)
-			found = 0;
 
 	bp = buf; blen = sizeof(buf);
 	qword_add(&bp, &blen, dom);

Is everybody else better at reading this kind of patch? Between the code reshuffling and the conditionals, it's almost totally opaque to me. I'd have split it up something like the below.--b.

commit b0b623281017

Author: Trond Myklebust <[email protected]>
Date:   Thu Apr 16 18:12:51 2020 -0400

mountd: Ignore transient and non-fatal filesystem errors in nfsd_fh()

In nfsd_fh(), if the error returned by the downcall is transient, then we should ignore it. Only reject the export if the filesystem path is truly not exportable.
This fixes a case where we can see spurious NFSERR_STALE errors being returned by knfsd.

utils/mountd/cache.c +6 -2
index bcd4b3ce5bb2..0426b171f040 100644
@@ -826,8 +826,12 @@ static void nfsd_fh(int f)
 		 */
 		/* FIXME we need to make sure we re-visit this later */
 		goto out;
-	} else if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0)
-			found = 0;
+	} else if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0) {
+		if (!path_lookup_error(errno))
+			goto out;
+		/* The kernel is saying the path is unexportable */
+		found = NULL;
+	}
 
 	bp = buf; blen = sizeof(buf);
 	qword_add(&bp, &blen, dom);

commit 931a2e77f954

Author: J. Bruce Fields <[email protected]>
Date:   Mon Apr 27 13:22:30 2020 -0400

rearrange logic; no change in behavior

utils/mountd/cache.c +9 -12
index 8f54e37b7936..bcd4b3ce5bb2 100644
@@ -808,8 +808,14 @@ static void nfsd_fh(int f)
 			}
 		}
 	}
-	if (found && 
-	    found->e_mountpoint &&
+
+	if (!found) {
+		/* The missing dev could be what we want, so just be
+		 * quiet rather than returning stale yet
+		 */
+		if (dev_missing)
+			goto out;
+	} else if (found->e_mountpoint &&
 	    !is_mountpoint(found->e_mountpoint[0]?
 			   found->e_mountpoint:
 			   found->e_path)) {
@@ -820,16 +826,7 @@ static void nfsd_fh(int f)
 		 */
 		/* FIXME we need to make sure we re-visit this later */
 		goto out;
-	}
-	if (!found && dev_missing) {
-		/* The missing dev could be what we want, so just be
-		 * quite rather than returning stale yet
-		 */
-		goto out;
-	}
-
-	if (found)
-		if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0)
+	} else if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0)
 			found = 0;
 
 	bp = buf; blen = sizeof(buf);
#9 lore raw

Re: [PATCH 6/7] mountd: Ignore transient and non-fatal filesystem errors in nfsd_fh()

Ack to the series, otherwise, looks good.--b.

#10 lore raw
In reply to [email protected]
Quoted 19 lines · On 4/16/20 6:12 PM, [email protected] wrote:

From: Trond Myklebust <[email protected]>

This patchset fixes a couple of missed API changes in mountd to ensure that the [exports]rootdir root jail works correctly. It fixes up the 'same_path' function, as well as 'uuid_by_path'. It also improves the error handling, and tries to distinguish between bona fide path resolution problems, and other transient issues in order to avoid having knfsd return spurious ESTALE errors.

Trond Myklebust (7):
mountd: Add a helper nfsd_path_statfs64() for uuid_by_path()
nfsd: Support running nfsd_name_to_handle_at() in the root jail
mountd: Fix up path checking helper same_path()
Fix autoconf probe for 'struct nfs_filehandle'
mountd: Ensure dump_to_cache() sets errno appropriately
mountd: Ignore transient and non-fatal filesystem errors in nfsd_fh()
mountd: Check the stat() return values in match_fsid()

Quoted 6 lines
 configure.ac                |   7 +-
 support/include/nfsd_path.h |   9 ++
 support/misc/nfsd_path.c    | 109 ++++++++++++++++++++++
 utils/mountd/cache.c        | 174 ++++++++++++++++++++++++------------
 4 files changed, 242 insertions(+), 57 deletions(-)

Committed the series (tag: nfs-utils-2-4-4-rc4)

steved.