The exec
syscall of the Linux kernel fails with EACCES
if the file is not executable
While you can do sh myprog.sh
, trying to run the program as ./myprog.sh
cannot work, since when you do that:
This can be verified with main.c
:
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
char *argv[] = {"myprog", NULL};
char *envp[] = {NULL};
int ret;
ret = execve("myprog.sh", argv, envp);
perror("execve");
printf("%d\n", errno);
printf("%d\n", EACCES);
}
and myprog.sh
:
#!/bin/sh
echo worked
If myprog.sh
is not executable, main
fails with:
execve: Permission denied
13
13
Tested in Ubuntu 17.10, gcc -std=c99
.
POSIX 7 mentions that at:
The exec functions, except for fexecve(), shall fail if:
[EACCES]
Search permission is denied for a directory listed in the new process image file's path prefix, or the new process image file denies execution permission.
Further rationale can be found at: https://security.stackexchange.com/questions/66550/unix-execute-permission-can-be-easily-bypassed-is-it-superfluous-or-whats-the