為什麼在未設定可執行位時 root 無法執行?

為什麼在未設定可執行位時 root 無法執行?

root使用者寫入文件,即使write未設定其權限。

root使用者讀取文件,即使其read權限未設定。

root使用者 cd進入目錄,即使execute未設定其權限。

root使用者不能execute未設定檔案權限時執行檔案。

為什麼?

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

答案1

簡而言之,因為執行位被認為是特殊的;如果沒有設定根本不,則該文件被認為不是可執行文件,因此無法執行。

然而,即使設定了一個執行位,root 也可以並且將會執行它。

觀察:

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!

答案2

過去,系統管理工具包括/etc、、、/etc/restore等。​​/etc/rrestore/etc/init/etc/haltrootPATH/etc:/binrootpasswd

它不會正常工作。

更糟的是,在過去,二進位可執行檔沒有魔術頭,因此除了檢查權限位元之外,檢查二進位檔案是否是可執行檔實際上是不可能的。因此,他們使檔案不是 * 的有效目標,exec除非它們實際上是檔案(沒有目錄等)並且至少設定了一個執行位元。

*檢查可能是在 execvp 中進行的,這是一個使用者模式函數。

它仍然是一個有用的檢查,因為理論上任何東西都可以是 shell 腳本,那麼為什麼要刪除它呢?

相關內容