使用黏滯位限制根用戶操作

使用黏滯位限制根用戶操作

我在 AWS 上有一個 ubuntu 20.04.x 實例,它顯示出非常意外的行為。

我有一個文件/tmp

root@ip-10-0-1-145:/tmp# ls -l api_requests_2023-08-23.log
-rw-r--r-- 1 www-data www-data 47450 Aug 23 19:04 api_requests_2023-08-23.log

我試圖截斷它,root因為 FAILED

root@ip-10-0-1-145:/tmp# truncate -s 0 api_requests_2023-08-23.log
truncate: cannot open 'api_requests_2023-08-23.log' for writing: Permission denied

我嘗試 strace 檢查失敗的位置,這給了我:

root@ip-10-0-1-145:/tmp# strace -e trace=file truncate -s 0 api_requests_2023-08-23.log
execve("/usr/bin/truncate", ["truncate", "-s", "0", "api_requests_2023-08-23.log"], 0x7ffcaa0ab958 /* 16 vars */) = 0
...
openat(AT_FDCWD, "api_requests_2023-08-23.log", O_WRONLY|O_CREAT|O_NONBLOCK, 0666) = -1 EACCES (Permission denied)
...
truncate: cannot open 'api_requests_2023-08-23.log' for writing: Permission denied
...
+++ exited with 1 +++

鑑於 temp 像往常一樣設置了黏性位:

root@ip-10-0-1-145:/tmp# ls -ld
drwxrwxrwt 4 root root 12288 Sep 12 09:11 .

我嘗試使用文件的所有者截斷文件:

root@ip-10-0-1-145:/tmp# sudo -u www-data truncate -s 0 api_requests_2023-08-23.log
root@ip-10-0-1-145:/tmp# ls -lh api_requests_2023-08-23.log
-rw-r--r-- 1 www-data www-data 0 Sep 12 09:13 api_requests_2023-08-23.log

作為truncate業主工作! (所以這不是由檔案系統問題引起的;檔案系統是本地的、磁碟上的、ext4 的,使用預設值安裝)。

目錄上黏性位的這種行為是定義的對於所有操作都為**'only owner or root'**。

如何root限制開啟檔案進行寫入(透過截斷>api_requests_2023-08-23.log也失敗)?

相關內容