Fix S_ISDIR execve() errno
Subsystems (3)
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
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.
--- fs/namei.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/namei.c +3 -1
@@ -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
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_ISDIRexec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.Reported-by Marc Zyngier <[email protected]>Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")Signed-off-by Kees Cook <[email protected]>--- fs/namei.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/namei.c +0
-0@@ -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
-1return -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 :
Greg Kroah-Hartman
Tested-byRe: [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_ISDIRexec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.Reported-by Marc Zyngier <[email protected]>Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")Signed-off-by Kees Cook <[email protected]>--- fs/namei.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/namei.c +1
-1@@ -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 :Reviewed-by Greg Kroah-Hartman <[email protected]>
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
Re: [PATCH 1/2] exec: Restore EACCES of S_ISDIR execve()
In reply to Kees CookQuoted 28 lines · Kees Cook
The return code for attempting to execute a directory has always been EACCES. Adjust the
S_ISDIRexec test to reflect the old errno instead of the general EISDIR for other kinds of "open" attempts on directories.Reported-by Marc Zyngier <[email protected]>Fixes 633fb6ac3980 ("exec: move S_ISREG() check earlier")Signed-off-by Kees Cook <[email protected]>--- fs/namei.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/namei.c +3
-1@@ -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...
PATCH 2/2 selftests/exec: Add file type errno tests
In reply to Kees CookMake 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
@@ -10,3 +10,4 @@ execveat.denatured /recursion-depth xxxxxxxx* pipe +S_I*.test
tools/testing/selftests/exec/Makefile +3 -2
@@ -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