2015-11-06 104 views
3

我創建了這個簡單的腳本,允許用戶刪除由他的主目錄中的Web服務器創建的文件,而不給他「su」。這兩個腳本都設置爲"chmod 4750"爲什麼我的setuid root bash shell腳本不起作用?

最瘋狂的是他們DID工作,現在他們沒有。這裏的腳本:

#!/bin/bash 

# Ask for directory to delete 
echo "Enter the file or directory you would like to delete, the assumed path is /home/user" 

read DIRECTORY 

rm -rf /home/user/"$DIRECTORY" 

echo "Deleting /home/user/$DIRECTORY ..." 

exit 0 

2:

#!/bin/bash 

# Reset permissions 
echo "Resetting the ownership of the contents of /home/user to user." 

chown -R user /home/user 

exit 0 

我會讓他們更先進一點的工作,併爲多個用戶,但現在我甚至不能簡單的版本工作。它當然是以root身份運行的。它以用戶'用戶'身份運行時用於工作,但現在不行。我得到這個:

[email protected]:/home/user$ delete.sh 
Enter the file or directory you would like to delete, the assumed path is /home/user/[your input] 
test-dir 
rm: cannot remove ‘/home/user/test-dir/test-file’: Permission denied 
Deleting /home/user/test-dir ... 

chown: changing ownership of ‘/home/user/test-dir’: Operation not permitted 

什麼都不可能是問題?

-rwsr-x--- 1 root user 291 Nov 6 05:23 delete.sh 
-rwsr-x--- 1 root user 177 Nov 6 05:45 perms.sh 
+0

檢查SUID是否可以在腳本中添加:'echo whoami'輸出root? 。 – Noproblem

+1

作爲一項安全措施,大多數系統不允許使用shell腳本。不知道他們爲什麼過去工作。 – chepner

+0

奇怪的是,它今天早些時候在同一個系統上工作。我不知道我改變了什麼或者改變了什麼。我會試試這個。 – Bob

回答

2

有一個在https://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts相當comprehansive答案

底線是,有反對兩個要點:

  1. 當內核打開文件來尋找之間的競爭條件,其解釋器應該執行並且解釋器打開文件來閱讀腳本。如果沒有經過適當的檢查就執行許多外部程序的Shell腳本可能會被誤認爲執行了錯誤的程序(例如,使用惡意的PATH),或者以破碎的方式擴展變量(例如變量值中有空格),並且通常它具有較少的控制它執行的外部程序如何處理輸入。

從歷史上看,在原始的Bourne shell(至少在4.2BSD,這是我看到這個動作),它允許任何人通過創建一個符號鏈接獲得互動root的shell著名的錯誤稱爲-i到suid shell腳本。這可能是被禁止的原始觸發器。