Skip to contentSkip to conversation

exportfs: fix unexporting of '/'

PATCH In review Ondrej Mosnacek 3 participants 5 messages Mar 22, 2021 – Apr 7, 2021

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

PATCH 1/1 exportfs: fix unexporting of '/'

The code that has been added to strip trailing slashes from path in unexportfs_parsed() forgot to account for the case of the root directory, which is simply '/'. In that case it accesses path[-1] and reduces the path to an empty string, which then fails to match any export.

Fix it by stopping the stripping when the path is just a single character - it doesn't matter if it's a '/' or not, we want to keep it either way in that case.

Reproducer:

exportfs localhost:/
exportfs -u localhost:/

Without this patch, the unexport step fails with "exportfs: Could not find 'localhost:/' to unexport."

Fixes a9a7728d8743 ("exportfs: Deal with path's trailing "/" in unexportfs_parsed()")
---
 utils/exportfs/exportfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/exportfs/exportfs.c +1 -1
index 262dd19a..1aedd3d6 100644
@@ -383,7 +383,7 @@ unexportfs_parsed(char *hname, char *path, int verbose)
 	 * so need to deal with it.
 	*/
 	size_t nlen = strlen(path);
-	while (path[nlen - 1] == '/')
+	while (nlen > 1 && path[nlen - 1] == '/')
 		nlen--;
 
 	for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
Signature · 1 line
2.30.2
#1 lore raw
Quoted 9 lines · Ondrej Mosnacek

The code that has been added to strip trailing slashes from path in unexportfs_parsed() forgot to account for the case of the root directory, which is simply '/'. In that case it accesses path[-1] and reduces the path to an empty string, which then fails to match any export.

Fix it by stopping the stripping when the path is just a single character - it doesn't matter if it's a '/' or not, we want to keep it either way in that case.

Makes sense to me.

(Note nfs-exporting / is often a bad idea. I assume you had some good reason in this case.)

--b.

Quoted 31 lines

Reproducer:

exportfs localhost:/
exportfs -u localhost:/

Without this patch, the unexport step fails with "exportfs: Could not find 'localhost:/' to unexport."

Fixes a9a7728d8743 ("exportfs: Deal with path's trailing "/" in unexportfs_parsed()")
---
 utils/exportfs/exportfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/exportfs/exportfs.c +1 -1
index 262dd19a..1aedd3d6 100644
@@ -383,7 +383,7 @@ unexportfs_parsed(char *hname, char *path, int verbose)
 	 * so need to deal with it.
 	*/
 	size_t nlen = strlen(path);
-	while (path[nlen - 1] == '/')
+	while (nlen > 1 && path[nlen - 1] == '/')
 		nlen--;
 
 	for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
Signature · 1 line
2.30.2
#2 lore raw
Quoted 7 lines · J. Bruce Fields
Quoted 6 lines · Ondrej Mosnacek

The code that has been added to strip trailing slashes from path in unexportfs_parsed() forgot to account for the case of the root directory, which is simply '/'. In that case it accesses path[-1] and reduces the path to an empty string, which then fails to match any export.

Quoted 8 lines
Quoted 3 lines

Fix it by stopping the stripping when the path is just a single character - it doesn't matter if it's a '/' or not, we want to keep it either way in that case.

Makes sense to me.

(Note nfs-exporting / is often a bad idea. I assume you had some good reason in this case.)

I hit it in a test, so if the only issue is that it exposes more than necessary, then I guess it's fine in this case :)

Quoted 31 lines

--b.

Quoted 28 lines

Reproducer:

exportfs localhost:/
exportfs -u localhost:/

Without this patch, the unexport step fails with "exportfs: Could not find 'localhost:/' to unexport."

Fixes a9a7728d8743 ("exportfs: Deal with path's trailing "/" in unexportfs_parsed()")
---
 utils/exportfs/exportfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/exportfs/exportfs.c +1 -1
index 262dd19a..1aedd3d6 100644
@@ -383,7 +383,7 @@ unexportfs_parsed(char *hname, char *path, int verbose)
       * so need to deal with it.
      */
      size_t nlen = strlen(path);
-     while (path[nlen - 1] == '/')
+     while (nlen > 1 && path[nlen - 1] == '/')
              nlen--;
 
Quoted 4 lines
Quoted 3 lines
utils/exportfs/exportfs.c +0 -1
      for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
--

2.30.2

--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

#3 lore raw
Quoted 12 lines · Ondrej Mosnacek
Quoted 11 lines · J. Bruce Fields
Quoted 9 lines · Ondrej Mosnacek

The code that has been added to strip trailing slashes from path in unexportfs_parsed() forgot to account for the case of the root directory, which is simply '/'. In that case it accesses path[-1] and reduces the path to an empty string, which then fails to match any export.

Fix it by stopping the stripping when the path is just a single character - it doesn't matter if it's a '/' or not, we want to keep it either way in that case.

Quoted 7 lines
Quoted 4 lines

Makes sense to me.

(Note nfs-exporting / is often a bad idea. I assume you had some good reason in this case.)

I hit it in a test, so if the only issue is that it exposes more than necessary, then I guess it's fine in this case :)

Yes, in a lot of typical setups, exporting "/" would expose files to the network that you don't really want to.

But, anyway, it should work. Looks like a good fix.

--b.

Quoted 42 lines
Quoted 32 lines

--b.

Quoted 28 lines

Reproducer:

exportfs localhost:/
exportfs -u localhost:/

Without this patch, the unexport step fails with "exportfs: Could not find 'localhost:/' to unexport."

Fixes a9a7728d8743 ("exportfs: Deal with path's trailing "/" in unexportfs_parsed()")
---
 utils/exportfs/exportfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/exportfs/exportfs.c +1 -1
index 262dd19a..1aedd3d6 100644
@@ -383,7 +383,7 @@ unexportfs_parsed(char *hname, char *path, int verbose)
       * so need to deal with it.
      */
      size_t nlen = strlen(path);
-     while (path[nlen - 1] == '/')
+     while (nlen > 1 && path[nlen - 1] == '/')
              nlen--;
 
Quoted 4 lines
Quoted 3 lines
utils/exportfs/exportfs.c +0 -1
      for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
--

2.30.2

--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

#4 lore raw
In reply to Ondrej Mosnacek
Quoted 15 lines · Ondrej Mosnacek

The code that has been added to strip trailing slashes from path in unexportfs_parsed() forgot to account for the case of the root directory, which is simply '/'. In that case it accesses path[-1] and reduces the path to an empty string, which then fails to match any export.

Fix it by stopping the stripping when the path is just a single character - it doesn't matter if it's a '/' or not, we want to keep it either way in that case.

Reproducer:

exportfs localhost:/
exportfs -u localhost:/

Quoted 6 lines

Without this patch, the unexport step fails with "exportfs: Could not find 'localhost:/' to unexport."

Fixes a9a7728d8743 ("exportfs: Deal with path's trailing "/" in unexportfs_parsed()")

Committed... (tag: nfs-utils-2-5-4-rc2)

steved.

Quoted 18 lines
---
 utils/exportfs/exportfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utils/exportfs/exportfs.c +1 -1
index 262dd19a..1aedd3d6 100644
@@ -383,7 +383,7 @@ unexportfs_parsed(char *hname, char *path, int verbose)
 	 * so need to deal with it.
 	*/
 	size_t nlen = strlen(path);
-	while (path[nlen - 1] == '/')
+	while (nlen > 1 && path[nlen - 1] == '/')
 		nlen--;
 
 	for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {