Skip to contentSkip to conversation

Fix S_ISDIR execve() errno

PATCH Applied Kees Cook 3 participants 6 messages Aug 13, 2020 – Aug 14, 2020

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

cover Fix S_ISDIR execve() errno

Hi Andrew,

This fixes an errno change for execve() of directories, noticed by Marc Zyngier[1]. Along with the fix, include a regression test to avoid seeing this return in the future.

Thanks!

-Kees

[1] https://lore.kernel.org/lkml/20200813151305.6191993b@why

Kees Cook (2):
exec: Restore EACCES of S_ISDIR execve()
selftests/exec: Add file type errno tests

 fs/namei.c                                 |   4 +-
 tools/testing/selftests/exec/.gitignore    |   1 +
 tools/testing/selftests/exec/Makefile      |   5 +-
 tools/testing/selftests/exec/non-regular.c | 196 +++++++++++++++++++++
 4 files changed, 203 insertions(+), 3 deletions(-)
 create mode 100755 tools/testing/selftests/exec/non-regular.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/2 exec: Restore EACCES of S_ISDIR execve()

The return code for attempting to execute a directory has always been EACCES. Adjust the S_ISDIR exec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.

Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")
---
 fs/namei.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/namei.c +3 -1
index 2112e578dccc..e99e2a9da0f7 100644
@@ -2849,8 +2849,10 @@ static int may_open(const struct path *path, int acc_mode, int flag)
 	case S_IFLNK:
 		return -ELOOP;
 	case S_IFDIR:
-		if (acc_mode & (MAY_WRITE | MAY_EXEC))
+		if (acc_mode & MAY_WRITE)
 			return -EISDIR;
+		if (acc_mode & MAY_EXEC)
+			return -EACCES;
 		break;
 	case S_IFBLK:
 	case S_IFCHR:
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] exec: Restore EACCES of S_ISDIR execve()

Quoted 18 lines · Kees Cook

The return code for attempting to execute a directory has always been EACCES. Adjust the S_ISDIR exec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.

Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")
---
 fs/namei.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/namei.c +0 -0
index 2112e578dccc..e99e2a9da0f7 100644
@@ -2849,8 +2849,10 @@ static int may_open(const struct path *path, int acc_mode, int flag)
 	case S_IFLNK:

fs/namei.c static int may_open(const struct path *path, int acc_mode, int flag) · line 2858

Quoted 10 lines
fs/namei.c +3 -1
 		return -ELOOP;
 	case S_IFDIR:
-		if (acc_mode & (MAY_WRITE | MAY_EXEC))
+		if (acc_mode & MAY_WRITE)
 			return -EISDIR;
+		if (acc_mode & MAY_EXEC)
+			return -EACCES;
 		break;
 	case S_IFBLK:
 	case S_IFCHR:
#3 lore raw

Re: [PATCH 1/2] exec: Restore EACCES of S_ISDIR execve()

Quoted 24 lines · Greg Kroah-Hartman
Quoted 23 lines · Kees Cook

The return code for attempting to execute a directory has always been EACCES. Adjust the S_ISDIR exec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.

Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")
---
 fs/namei.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/namei.c +1 -1
index 2112e578dccc..e99e2a9da0f7 100644
@@ -2849,8 +2849,10 @@ static int may_open(const struct path *path, int acc_mode, int flag)
 	case S_IFLNK:
 		return -ELOOP;
 	case S_IFDIR:
-		if (acc_mode & (MAY_WRITE | MAY_EXEC))
+		if (acc_mode & MAY_WRITE)
 			return -EISDIR;
Quoted 8 lines

fs/namei.c static int may_open(const struct path *path, int acc_mode, int flag) · line 2858

Quoted 5 lines
fs/namei.c +2 -0
+		if (acc_mode & MAY_EXEC)
+			return -EACCES;
 		break;
 	case S_IFBLK:
 	case S_IFCHR:

And to round out the "let's use a different email address for each response, to drive accounting tools crazy!" effort, you can also add:

thanks,

greg "I don't have enough different email addresses" k-h

Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] exec: Restore EACCES of S_ISDIR execve()

In reply to Kees Cook
Quoted 28 lines · Kees Cook

The return code for attempting to execute a directory has always been EACCES. Adjust the S_ISDIR exec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.

Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")
---
 fs/namei.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/namei.c +3 -1
index 2112e578dccc..e99e2a9da0f7 100644
@@ -2849,8 +2849,10 @@ static int may_open(const struct path *path,
 int acc_mode, int flag)
 	case S_IFLNK:
 		return -ELOOP;
 	case S_IFDIR:
-		if (acc_mode & (MAY_WRITE | MAY_EXEC))
+		if (acc_mode & MAY_WRITE)
 			return -EISDIR;
+		if (acc_mode & MAY_EXEC)
+			return -EACCES;
 		break;
 	case S_IFBLK:
Quoted 1 line

case S_IFCHR:

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
author
#5 lore raw

PATCH 2/2 selftests/exec: Add file type errno tests

In reply to Kees Cook

Make sure execve() returns the expected errno values for non-regular files.

---
 tools/testing/selftests/exec/.gitignore    |   1 +
 tools/testing/selftests/exec/Makefile      |   5 +-
 tools/testing/selftests/exec/non-regular.c | 196 +++++++++++++++++++++
 3 files changed, 200 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/exec/non-regular.c
tools/testing/selftests/exec/.gitignore +1 -0
index 94b02a18f230..344a99c6da1b 100644
@@ -10,3 +10,4 @@ execveat.denatured
 /recursion-depth
 xxxxxxxx*
 pipe
+S_I*.test
tools/testing/selftests/exec/Makefile +3 -2
index 4453b8f8def3..0a13b110c1e6 100644
@@ -3,7 +3,7 @@ CFLAGS = -Wall
 CFLAGS += -Wno-nonnull
 CFLAGS += -D_GNU_SOURCE
 
-TEST_PROGS := binfmt_script
+TEST_PROGS := binfmt_script non-regular
 TEST_GEN_PROGS := execveat
 TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir pipe
 # Makefile is a run-time dependency, since it's accessed by the execveat test
@@ -11,7 +11,8 @@ TEST_FILES := Makefile
 
 TEST_GEN_PROGS += recursion-depth
 
-EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*
+EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*	\
+	       $(OUTPUT)/S_I*.test
 
 include ../lib.mk
 
tools/testing/selftests/exec/non-regular.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.25.1