Skip to contentSkip to conversation

nfs export symlink vulnerability fix (duplicate(ish))

PATCH In review Christopher Bii 3 participants 17 messages Dec 6, 2024 – Jan 17, 2025

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

cover nfs export symlink vulnerability fix (duplicate(ish))

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

Thanks

Christopher Bii (5):
nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an
absolute path - Rootdir is into a global variable what will also be
used to retrieve it later on - nfsd_path_nfsd_rootdir(void) is
simplified with nfsd_path_rootdir which returns the global var
rather than reprobing config for rootdir entry
nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const

    char*) -> nfsd_path_prepend_root(const char*)

NFS export symlink vulnerability fix - Replaced dangerous use of
realpath within support/nfs/export.c with nfsd_realpath variant
that is executed within the chrooted thread rather than main
thread. - Implemented nfsd_path.h methods to work securely within
chrooted thread using nfsd_run_task() helper
support/nfs/exports.c - Small changes

 support/export/export.c     |  17 +-
 support/include/nfsd_path.h |   9 +-
 support/misc/nfsd_path.c    | 362 ++++++++++++------------------------
 support/nfs/exports.c       |  49 ++---
 4 files changed, 151 insertions(+), 286 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/5 nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir which returns the global var rather than reprobing config for rootdir entry

---
 support/export/export.c     |  2 +-
 support/include/nfsd_path.h |  2 +-
 support/misc/nfsd_path.c    | 65 ++++++++++++++-----------------------
 3 files changed, 27 insertions(+), 42 deletions(-)
support/export/export.c +1 -1
index 2c8c3335..3a4fb747 100644
@@ -40,7 +40,7 @@ static int	export_check(const nfs_export *exp, const struct addrinfo *ai,
 static void
 exportent_mkrealpath(struct exportent *eep)
 {
-	const char *chroot = nfsd_path_nfsd_rootdir();
+	const char *chroot = nfsd_path_rootdir();
 	char *ret = NULL;
 
 	if (chroot) {
support/include/nfsd_path.h +1 -1
index aa1e1dd0..2b9b68af 100644
@@ -11,7 +11,7 @@ struct statfs;
 
 void 		nfsd_path_init(void);
 
-const char *	nfsd_path_nfsd_rootdir(void);
+const char *    nfsd_path_rootdir(void);
 char *		nfsd_path_strip_root(char *pathname);
 char *		nfsd_path_prepend_dir(const char *dir, const char *pathname);
 
support/misc/nfsd_path.c +25 -40

90 lines not on this page · raw message

Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/5 nfsd_path.c: - Simplification of nfsd_path_strip_root(char*)

In reply to Christopher Bii
---
 support/misc/nfsd_path.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
support/misc/nfsd_path.c +4 -12
index 5168903d..ff946301 100644
@@ -55,19 +55,11 @@ nfsd_rootdir_set(void)
 char *
 nfsd_path_strip_root(char *pathname)
 {
-	char buffer[PATH_MAX];
-	const char *dir = nfsd_path_rootdir();
-
-	if (!dir)
-		goto out;
-
-	if (realpath(dir, buffer))
-		return strstr(pathname, buffer) == pathname ?
-			pathname + strlen(buffer) : NULL;
+	if (!rootdir)
+                return pathname;
 
-	xlog(D_GENERAL, "%s: failed to resolve path %s: %m", __func__, dir);
-out:
-	return pathname;
+        return strstr(pathname, rootdir) == pathname ?
+                pathname + rootdir_pathlen : pathname;
 }
 
 char *
Signature · 1 line
2.47.1
#3 lore raw

PATCH 3/5 nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)

In reply to Christopher Bii

support/export/export.c
- use of nfsd_path_prepend_root mentionned above to make exportent->e_realpath

---
 support/export/export.c     | 17 +++--------------
 support/include/nfsd_path.h |  2 +-
 support/misc/nfsd_path.c    | 26 +++++++++++---------------
 3 files changed, 15 insertions(+), 30 deletions(-)
support/export/export.c +3 -14
index 3a4fb747..5da5780f 100644
@@ -40,20 +40,9 @@ static int	export_check(const nfs_export *exp, const struct addrinfo *ai,
 static void
 exportent_mkrealpath(struct exportent *eep)
 {
-	const char *chroot = nfsd_path_rootdir();
-	char *ret = NULL;
-
-	if (chroot) {
-		char buffer[PATH_MAX];
-		if (realpath(chroot, buffer))
-			ret = nfsd_path_prepend_dir(buffer, eep->e_path);
-		else
-			xlog(D_GENERAL, "%s: failed to resolve path %s: %m",
-					__func__, chroot);
-	}
-	if (!ret)
-		ret = xstrdup(eep->e_path);
-	eep->e_realpath = ret;
+        eep->e_realpath = nfsd_path_prepend_root(eep->e_path);
+        if (eep->e_realpath == eep->e_path)
+                eep->e_realpath = xstrdup(eep->e_path);
 }
 
 char *
support/include/nfsd_path.h +1 -1
index 2b9b68af..214bde47 100644
@@ -13,7 +13,7 @@ void 		nfsd_path_init(void);
 
 const char *    nfsd_path_rootdir(void);
 char *		nfsd_path_strip_root(char *pathname);
-char *		nfsd_path_prepend_dir(const char *dir, const char *pathname);
+char *		nfsd_path_prepend_root(const char* pathname);
 
 int 		nfsd_path_stat(const char *pathname, struct stat *statbuf);
 int 		nfsd_path_lstat(const char *pathname, struct stat *statbuf);
support/misc/nfsd_path.c +11 -15
index ff946301..0f727d3b 100644
@@ -63,22 +63,18 @@ nfsd_path_strip_root(char *pathname)
 }
 
 char *
-nfsd_path_prepend_dir(const char *dir, const char *pathname)
+nfsd_path_prepend_root(const char *pathname)
 {
-	size_t len, dirlen;
-	char *ret;
-
-	dirlen = strlen(dir);
-	while (dirlen > 0 && dir[dirlen - 1] == '/')
-		dirlen--;
-	if (!dirlen)
-		return NULL;
-	while (pathname[0] == '/')
-		pathname++;
-	len = dirlen + strlen(pathname) + 1;
-	ret = xmalloc(len + 1);
-	snprintf(ret, len+1, "%.*s/%s", (int)dirlen, dir, pathname);
-	return ret;
+        char*                   buff;
+
+        if (!rootdir)
+                return (char*)pathname;
+
+        buff = malloc(strlen(pathname) + rootdir_pathlen + 1);
+        memcpy(buff, rootdir, rootdir_pathlen);
+        strcpy(buff + rootdir_pathlen, pathname);
+
+        return buff;
 }
 
 static void
Signature · 1 line
2.47.1
#4 lore raw

PATCH 4/5 NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with nfsd_realpath variant that is executed within the chrooted thread rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread using nfsd_run_task() helper

In reply to Christopher Bii
---
 support/include/nfsd_path.h |   5 +-
 support/misc/nfsd_path.c    | 255 +++++++++++-------------------------
 support/nfs/exports.c       |   3 +-
 3 files changed, 80 insertions(+), 183 deletions(-)
support/include/nfsd_path.h +3 -2
index 214bde47..8085ddfd 100644
@@ -8,6 +8,7 @@
 
 struct file_handle;
 struct statfs;
+struct nfsd_task_t;
 
 void 		nfsd_path_init(void);
 
@@ -23,8 +24,8 @@ int		nfsd_path_statfs(const char *pathname,
 
 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);
+ssize_t		nfsd_path_read(int fd, void* buf, size_t len);
+ssize_t		nfsd_path_write(int fd, void* buf, size_t len);
 
 int		nfsd_name_to_handle_at(int fd, const char *path,
 				       struct file_handle *fh,
support/misc/nfsd_path.c +75 -180

357 lines not on this page · raw message

support/nfs/exports.c +2 -1
index a6816e60..21ec6486 100644
@@ -32,6 +32,7 @@
 #include "xio.h"
 #include "pseudoflavors.h"
 #include "reexport.h"
+#include "nfsd_path.h"
 
 #define EXPORT_DEFAULT_FLAGS	\
   (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES|NFSEXP_NOSUBTREECHECK)
@@ -200,7 +201,7 @@ getexportent(int fromkernel)
 		return NULL;
 	}
 	/* resolve symlinks */
-	if (realpath(ee.e_path, rpath) != NULL) {
+	if (nfsd_realpath(ee.e_path, rpath) != NULL) {
 		rpath[sizeof (rpath) - 1] = '\0';
 		strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
 		ee.e_path[sizeof (ee.e_path) - 1] = '\0';
Signature · 1 line
2.47.1
#5 lore raw

PATCH 5/5 support/nfs/exports.c - Small changes

In reply to Christopher Bii
---
 support/nfs/exports.c | 48 ++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 21 deletions(-)
support/nfs/exports.c +27 -21
index 21ec6486..3c2d4d46 100644
@@ -186,28 +186,34 @@ getexportent(int fromkernel)
 	}
 	ee.e_hostname = xstrdup(hostname);
 
-	if (parseopts(opt, &ee, NULL) < 0) {
-		if(ee.e_hostname)
-		{
-			xfree(ee.e_hostname);
-			ee.e_hostname=NULL;
-		}
-		if(ee.e_uuid)
-		{
-			xfree(ee.e_uuid);
-			ee.e_uuid=NULL;
-		}
+	if (parseopts(opt, &ee, NULL) < 0)
+                goto out;
 
-		return NULL;
-	}
 	/* resolve symlinks */
-	if (nfsd_realpath(ee.e_path, rpath) != NULL) {
-		rpath[sizeof (rpath) - 1] = '\0';
-		strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
-		ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-	}
+	if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };
 
-	return &ee;
+        if (strlen(rpath) > sizeof(ee.e_path) - 1){
+                xlog(L_ERROR, "%s: export path %s exceeds limit(%lu)", __func__, rpath, sizeof(ee.e_path) - 1);
+                goto out;
+        };
+
+        strcpy(ee.e_path, rpath);
+        return &ee;
+
+out:
+        if (ee.e_hostname){
+                free(ee.e_hostname);
+                ee.e_hostname = NULL;
+        };
+        if (ee.e_uuid){
+                free(ee.e_uuid);
+                ee.e_uuid = NULL;
+        };
+
+        return NULL;
 }
 
 static const struct secinfo_flag_displaymap {
@@ -432,8 +438,8 @@ mkexportent(char *hname, char *path, char *options)
 		xlog(L_ERROR, "path name %s too long", path);
 		return NULL;
 	}
-	strncpy(ee.e_path, path, sizeof (ee.e_path));
-	ee.e_path[sizeof (ee.e_path) - 1] = '\0';
+	strcpy(ee.e_path, path);
+
 	if (parseopts(options, &ee, NULL) < 0)
 		return NULL;
 	return &ee;
Signature · 1 line
2.47.1
#6 lore raw
In reply to Christopher Bii

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

/* resolve symlinks */
- if (realpath(ee.e_path, rpath) != NULL) {
- rpath[sizeof (rpath) - 1] = '\0';
- strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
- ee.e_path[sizeof (ee.e_path) - 1] = '\0';
- }
+ if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+ xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+ goto out;
+ };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 25 lines

Thanks

Christopher Bii (5):
nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an
absolute path - Rootdir is into a global variable what will also be
used to retrieve it later on - nfsd_path_nfsd_rootdir(void) is
simplified with nfsd_path_rootdir which returns the global var
rather than reprobing config for rootdir entry
nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const

     char*) -> nfsd_path_prepend_root(const char*)

NFS export symlink vulnerability fix - Replaced dangerous use of
realpath within support/nfs/export.c with nfsd_realpath variant
that is executed within the chrooted thread rather than main
thread. - Implemented nfsd_path.h methods to work securely within
chrooted thread using nfsd_run_task() helper
support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +-
  support/include/nfsd_path.h |   9 +-
  support/misc/nfsd_path.c    | 362 ++++++++++++------------------------
  support/nfs/exports.c       |  49 ++---
  4 files changed, 151 insertions(+), 286 deletions(-)
#7 lore raw

Hi Steve,

Thank you for the reply. I made sure to separate the patch on this version of my submission. Patch 4/5 changes no logic, it is only patch 5/5 that changes the failure handling. If you guys would like to test that patch independently, I believe it will do the trick.

Thanks,
Christopher Bii

Quoted 96 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

     /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 25 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +- support/include/nfsd_path.h |   9 +- support/misc/nfsd_path.c    | 362 ++++++++++++------------------------ support/nfs/exports.c       |  49 ++---

  4 files changed, 151 insertions(+), 286 deletions(-)
#8 lore raw
Quoted 3 lines · Christopher Bii

Hi Steve,

Thank you for the reply. I made sure to separate the patch on this version of my submission. Patch 4/5 changes no logic, it is only patch 5/5 that changes the failure handling. If you guys would like to test that patch independently, I believe it will do the trick.

Good ahead and make a V2 of the patch set so we can review it.

thanks!

steved.

Quoted 102 lines

Thanks,
Christopher Bii

Quoted 96 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

      /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 25 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +- support/include/nfsd_path.h |   9 +- support/misc/nfsd_path.c    | 362 ++++++++++++------------------------ support/nfs/exports.c       |  49 ++---

  4 files changed, 151 insertions(+), 286 deletions(-)
#9 lore raw

When I said in this version of the submission I meant the previous one that I already submitted. I can send it again but I would just be removing the last commit in the chain. Should I resubmit it that way or?

Commit hash with correct fix: 0969665e8e2586179efc9366f7e4506ccc72189c Or 4/5 in the email chain.

Thanks

Quoted 116 lines · Steve Dickson
Quoted 3 lines · Christopher Bii

Hi Steve,

Thank you for the reply. I made sure to separate the patch on this version of my submission. Patch 4/5 changes no logic, it is only patch 5/5 that changes the failure handling. If you guys would like to test that patch independently, I believe it will do the trick.

Good ahead and make a V2 of the patch set so we can review it.

thanks!

steved.

Quoted 103 lines

Thanks,
Christopher Bii

Quoted 97 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

      /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 26 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +-
  support/include/nfsd_path.h |   9 +-
  support/misc/nfsd_path.c    | 362 +++++++++++

+------------------------
  support/nfs/exports.c       |  49 ++---
  4 files changed, 151 insertions(+), 286 deletions(-)
#10 lore raw
Quoted 4 lines · Christopher Bii

When I said in this version of the submission I meant the previous one that I already submitted. I can send it again but I would just be removing the last commit in the chain. Should I resubmit it that way or?

Commit hash with correct fix: 0969665e8e2586179efc9366f7e4506ccc72189c Or 4/5 in the email chain.

What I'm concerned about is
* Neal's concern this is not a problem
* When one starts pulling patches out of a
patch set... doesn't that invalid the testing
that has been done.

So I would like you to address Neil's concerns and post a V2 patch after the patch test is tested w/out that patch.

thanks!

steved.

Quoted 121 lines

Thanks

Quoted 116 lines · Steve Dickson
Quoted 3 lines · Christopher Bii

Hi Steve,

Thank you for the reply. I made sure to separate the patch on this version of my submission. Patch 4/5 changes no logic, it is only patch 5/5 that changes the failure handling. If you guys would like to test that patch independently, I believe it will do the trick.

Good ahead and make a V2 of the patch set so we can review it.

thanks!

steved.

Quoted 103 lines

Thanks,
Christopher Bii

Quoted 97 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

      /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 26 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +-
  support/include/nfsd_path.h |   9 +-
  support/misc/nfsd_path.c    | 362 +++++++++++

+------------------------
  support/nfs/exports.c       |  49 ++---
  4 files changed, 151 insertions(+), 286 deletions(-)
#11 lore raw

I believe Neil is assuming realpath is called after the entry has been concatenated to the rootdir. But this is not the case. realpath is called on the exportent path before the concatenation happens. Which is a huge flaw.

Quoted 143 lines · Steve Dickson
Quoted 4 lines · Christopher Bii

When I said in this version of the submission I meant the previous one that I already submitted. I can send it again but I would just be removing the last commit in the chain. Should I resubmit it that way or?

Commit hash with correct fix: 0969665e8e2586179efc9366f7e4506ccc72189c Or 4/5 in the email chain.

What I'm concerned about is
  * Neal's concern this is not a problem
  * When one starts pulling patches out of a patch set... doesn't that invalid the testing that has been done.

So I would like you to address Neil's concerns and post a V2 patch after the patch test is tested w/out that patch.

thanks!

steved.

Quoted 121 lines

Thanks

Quoted 116 lines · Steve Dickson
Quoted 3 lines · Christopher Bii

Hi Steve,

Thank you for the reply. I made sure to separate the patch on this version of my submission. Patch 4/5 changes no logic, it is only patch 5/5 that changes the failure handling. If you guys would like to test that patch independently, I believe it will do the trick.

Good ahead and make a V2 of the patch set so we can review it.

thanks!

steved.

Quoted 103 lines

Thanks,
Christopher Bii

Quoted 97 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

      /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 26 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +-
  support/include/nfsd_path.h |   9 +-
  support/misc/nfsd_path.c    | 362 +++++++++++

+------------------------
  support/nfs/exports.c       |  49 ++---
  4 files changed, 151 insertions(+), 286 deletions(-)
#12 lore raw
In reply to Steve Dickson

Hello Steve,

I would like to bring into question the reason why the failure case is ignored in the current implementation. Whenever a rootdir is set, realpath would previously fail if the path didn't exist. So ignoring it
indeed made sense, as a blanket solution.

But was pre-exporting really a feature? I find the risks to greatly outweigh the benefits. Should a non-existant dir be pre-exported, someone could, maliciously or accidentally, symlink to any arbitrary directory on the fly without warning, possibly compromising the system.

I think this should be reconsidered as there is no longer functionality requiring us to ignore the failure of export path resolution via realpath.

Thank you,
Christopher Bii

Quoted 96 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

     /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 25 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +- support/include/nfsd_path.h |   9 +- support/misc/nfsd_path.c    | 362 ++++++++++++------------------------ support/nfs/exports.c       |  49 ++---

  4 files changed, 151 insertions(+), 286 deletions(-)
#13 lore raw
Quoted 6 lines · Christopher Bii

Hello Steve,

I would like to bring into question the reason why the failure case is ignored in the current implementation. Whenever a rootdir is set, realpath would previously fail if the path didn't exist. So ignoring it
indeed made sense, as a blanket solution.

Quoted 8 lines

But was pre-exporting really a feature? I find the risks to greatly outweigh the benefits. Should a non-existant dir be pre-exported, someone could, maliciously or accidentally, symlink to any arbitrary directory on the fly without warning, possibly compromising the system.

I think this should be reconsidered as there is no longer functionality requiring us to ignore the failure of export path resolution via realpath.

I have... the latest patch seems to work as expected... thanks!

steved.

Quoted 102 lines

Thank you,
Christopher Bii

Quoted 96 lines · Steve Dickson

Hello,

Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

First of all thank you this contribution... but :-) the patch makes an assumption that is incorrect. An export directory has to exist when exported.

The point being... even though the export does not exist when exported... it can in the future due to mounting races. This is a change NeilBrown made a few years back...

Which means the following fails which does not
with the original code

exportfs -ua
exportfs -vi *:/not_exist
exportfs: Failed to stat /not_exist: No such file or directory (which is a warning, not an error meaning /not_exist is still exported)

With these patches this valid export fails exportfs -vi *:/export_test fails with
exportfs: nfsd_realpath(): unable to resolve path /not_exist

because /not_exist is exported (aka in /var/lib/nfs/etab) so the failure is not correct because /not_exist could exist in the future.

Thank you Yongcheng Yang for this test which points out the problem.

Here is part of the patch that needs work
in getexportent:

      /* resolve symlinks */
-    if (realpath(ee.e_path, rpath) != NULL) {
-        rpath[sizeof (rpath) - 1] = '\0';
-        strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1); -        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-    }
+    if (nfsd_realpath(ee.e_path, rpath) == NULL) {
+                xlog(L_ERROR, "nfsd_realpath(): unable to resolve path %s", ee.e_path);
+                goto out;
+        };

the current code ignores the realpath() failure... your patch does not.

I would like to fix the symlink vulnerability you have pointed out... but stay with the assumptions of the original code. I'll be more than willing to work with you to make this happen!

steved.

Quoted 25 lines

Thanks

Christopher Bii (5):
   nfsd_path.h - nfsd_path.c: - Configured export rootdir must now be an absolute path - Rootdir is into a global variable what will also be used to retrieve   it later on - nfsd_path_nfsd_rootdir(void) is simplified with nfsd_path_rootdir   which returns the global var rather than reprobing config for rootdir   entry nfsd_path.c: - Simplification of nfsd_path_strip_root(char*) nfsd_path.h - nfsd_path.c: - nfsd_path_prepend_dir(const char*, const char*) -> nfsd_path_prepend_root(const char*)
   NFS export symlink vulnerability fix - Replaced dangerous use of realpath within support/nfs/export.c with   nfsd_realpath variant that is executed within the chrooted thread   rather than main thread. - Implemented nfsd_path.h methods to work securely within chrooted thread   using nfsd_run_task() helper support/nfs/exports.c - Small changes

  support/export/export.c     |  17 +- support/include/nfsd_path.h |   9 +- support/misc/nfsd_path.c    | 362 ++++++++++++------------------------ support/nfs/exports.c       |  49 ++---

  4 files changed, 151 insertions(+), 286 deletions(-)
#14 lore raw
In reply to Christopher Bii
Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

I'm still not convinced there is a vulnerability here, but I might have missed part of the conversation...

Could you please spell out in detail the threat scenario that we are trying to defend against?

From my perspective: if you export a path that a non-privileged user can modify, then that is a configuration error. We should not try to make it "safe". We could possibly try to detect that situation and fail the export when it happens.

Why is that perspective not correct? If this has already been discussed, please point me to the relevant email.

Thanks,
NeilBrown

#15 lore raw

Hello,

You are correct, it would be a configuration error. But the issue is that when a rootdir is set, export entries are assumed to be relative to the rootdir path, but this isn't the case. So my rootdir can have a restrictive set of permissions, but the export entry path relative to the system* rootdir may have an entirely different set of permissions. Or even with restrictive permissions it could be accidentally or maliciously symlinked.

Christopher Bii

Quoted 34 lines · NeilBrown
Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

I'm still not convinced there is a vulnerability here, but I might have missed part of the conversation...

Could you please spell out in detail the threat scenario that we are trying to defend against?

From my perspective: if you export a path that a non-privileged user can
modify, then that is a configuration error. We should not try to make it "safe". We could possibly try to detect that situation and fail the export when it happens.

Why is that perspective not correct? If this has already been discussed, please point me to the relevant email.

Thanks,
NeilBrown

#16 lore raw
Quoted 5 lines · Christopher Bii

Hello,

You are correct, it would be a configuration error. But the issue is that when a rootdir is set, export entries are assumed to be relative to the rootdir path, but this isn't the case.

The above statement directly contradicts the documentation which says that all exports *are* relative to rootdir (more accurately: the path is prefixed with the rootdir). So if true it is clearly a bug that needs to be fixed, and would have nothing to do with symlinks.

But I don't think it is true.

What you have been saying is that if the export point - which is at $rootdir/$exportpath - is a symlink, then that symlink is resolved without reference to the rootdir. This is true but I don't see why it is a problem.

When you create /etc/exports (or run exportfs) you should only identify directories, never symlinks. And all ancestors of these directories should only be modifiable by privileged processes.

What is your use case for exporting a symlink, or exporting anything in a directory that is not restricted to privileged users?

Thanks,
NeilBrown

Quoted 45 lines

So my rootdir can have a
restrictive set of permissions, but the export entry path relative to the system* rootdir may have an entirely different set of permissions. Or even with restrictive permissions it could be accidentally or maliciously symlinked.

Christopher Bii

Quoted 34 lines · NeilBrown
Quoted 8 lines · Christopher Bii

Hello,

It is hinted in the configuration files that an attacker could gain access to arbitrary folders by guessing symlink paths that match exported dirs, but this is not the case. They can get access to the root export with certainty by simply symlinking to "../../../../../../../", which will always return "/".

Quoted 8 lines

This is due to realpath() being called in the main thread which isn't chrooted, concatenating the result with the export root to create the export entry's final absolute path which the kernel then exports.

PS: I already sent this patch to the mailing list about the same subject but it was poorly formatted. Changes were merged into a single commit. I have broken it up into smaller commits and made the patch into a single thread. Pardon the mistake, first contribution.

I'm still not convinced there is a vulnerability here, but I might have missed part of the conversation...

Could you please spell out in detail the threat scenario that we are trying to defend against?

From my perspective: if you export a path that a non-privileged user can
modify, then that is a configuration error. We should not try to make it "safe". We could possibly try to detect that situation and fail the export when it happens.

Why is that perspective not correct? If this has already been discussed, please point me to the relevant email.

Thanks,
NeilBrown