Despite execution privilege, getting permission denied

executablepermissions

Compiled a binary from the golang source, but it won't execute. I tried downloading the binary, which also didn't work. Permissions all seem to be right. Running the file from go for some reason works.

Output of ~/go$ go run src/github.com/exercism/cli/exercism/main.go1:

NAME:
   exercism - A command line tool to interact with http://exercism.io

USAGE:
   main [global options] command [command options] [arguments...]

Output of ~/go/bin$ ./exercism:

bash: ./exercism: Permission denied

Output of ~/go/bin$ ls -al:

total 9932
drwxr-xr-x 2 joshua joshua     4096 Apr 28 12:17 .
drwxr-xr-x 5 joshua joshua     4096 Apr 28 12:17 ..
-rwxr-xr-x 1 joshua joshua 10159320 Apr 28 12:17 exercism

Output of ~/go/bin$ strace ./exercism:

execve("./exercism", ["./exercism"], [/* 42 vars */]) = -1 EACCES (Permission denied)
write(2, "strace: exec: Permission denied\n", 32strace: exec: Permission denied
) = 32
exit_group(1)                           = ?
+++ exited with 1 +++

Best Answer

Check that noexec is not in effect on the mount point in question. Or choose a better place to launch your script from.

$ mount | grep noexec
[ snip ]
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime)
$ cat > /dev/shm/some_script
#!/bin/sh
echo hi
$ chmod +x /dev/shm/some_script
$ /dev/shm/some_script
bash: /dev/shm/some_script: Permission denied
$ mv /dev/shm_script .
$ ./some_script
hi

noexec exists specifically to prevent security issues that come from having world-writable places storing executable files; you might put a file there, but someone else might rewrite it before you execute it, and now you're not executing the code you thought you were.

Related Question