26

root user can write to a file even if its write permissions are not set.

root user can read a file even if its read permissions are not set.

root user can cd into a directory even if its execute permissions are not set.

root user cannot execute a file when its execute permissions are not set.

Why?

user$ echo '#!'$(which bash) > file
user$ chmod 000 file
user$ ls -l file
---------- 1 user user 12 Jul 17 11:11 file
user$ cat file                      # Normal user cannot read
cat: file: Permission denied
user$ su
root$ echo 'echo hello' >> file     # root can write
root$ cat file                      # root can read
#!/bin/bash
echo hello
root$ ./file                        # root cannot execute
bash: ./file: Permission denied
25

In short, because the execute bit is considered special; if it's not set at all, then the file is considered to be not an executable and thus can't be executed.

However, if even ONE of the execute bits is set, root can and will execute it.

Observe:

caleburn: ~/ >cat hello.sh
#!/bin/sh

echo "Hello!"

caleburn: ~/ >chmod 000 hello.sh
caleburn: ~/ >./hello.sh
-bash: ./hello.sh: Permission denied
caleburn: ~/ >sudo ./hello.sh 
sudo: ./hello.sh: command not found

caleburn: ~/ >chmod 100 hello.sh
caleburn: ~/ >./hello.sh
/bin/sh: ./hello.sh: Permission denied
caleburn: ~/ >sudo ./hello.sh 
Hello!
0

In the old days system administration tools lived in /etc such as /etc/restore, /etc/rrestore, /etc/init, /etc/halt, etc. Imagine what would happen if root's PATH was set to /etc:/bin and root ran passwd.

It wouldn't work right.

To make matters worse, in the old days, binary executables didn't have magic headers, so checking if the binary was an executable wasn't really possible except by checking permission bits. So they made files not valid targets of exec* unless they were actually files (no directories, etc.) and had at least one execute bit set.

*The check might have been in execvp, which is a user-mode function.

It's still a useful check as in theory anything could be a shell script, so why take it out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.