![](/img/trans.png)
[英]Docker Nginx worker process runs as non-root user but still can access a file owned by root
[英]Perl script can't open file owned by root when called from process (Nagios)
我已經安裝了Nagios的VPS,並且我想使用Nagios監視/ proc / user_beancounters文件中的VPS資源。 該文件具有以下權限:
-r-------- 1 root root 0 Oct 26 15:53 /proc/user_beancounters
因此,我從Nagios Exchange下載了腳本:
在說明中建議:
don’t forget to set the s-bit (chmod +s check_UBC.pl)
因此,我復制了腳本,並設置了s位,然后以root身份從終端運行它。 它按預期工作。 然后,我刪除它創建的臨時文件,將其轉換為nagios用戶,然后運行腳本。 它按預期工作。 我刪除它創建的臨時文件,然后啟動Nagios。 它無法讀取/ proc / user_beancounters文件! 有幫助的是,我得到的確切錯誤是“無法讀取/ proc / user_beancounters”。 我相信這是Perl腳本中的那行拋出的:
if (! open IN, "<", $UBC )
{
print "could not read $UBC\n";
exit $ERRORS{'CRITICAL'};
}
我的操作系統是CentOS版本6.2(最終版) 。
我首先想到的是SELinux伏都教,但沒有跡象表明SELinux正在此服務器上運行。 為了以防萬一,我嘗試了以下操作:
echo 0 > /selinux/enforce
但這沒什么區別。
供參考,這是我運行的nagios服務:
nagios 12939 0.0 0.0 203652 3404 ? Ssl 15:39 0:00 /usr/sbin/nagios -d /etc/nagios/nagios.cfg
這是我放置Perl腳本的地方:
-rwsr-sr-x 1 nagios nagios 2934 Oct 26 15:37 check_UBC.pl
關於我還能嘗試什么的任何建議?
PS道歉,如果這應該在另一個SE網站上進行-永遠不要確定涉及腳本,權限等的問題...
更新1
我創建了一個Shell腳本,以查看是否可以“模仿” nagios服務。 這非常簡單:
#!/bin/bash
/usr/lib64/nagios/plugins/check_UBC.pl
現在,我具有以下權限:
-rwsr-sr-x 1 root root 2934 Oct 26 15:37 check_UBC.pl
-rwxrwxrwx 1 root root 51 Oct 26 19:29 check_UBC.sh
作為根:
[root@/usr/lib64/nagios/plugins]$ ./check_UBC.pl
everything is fine..
[root@/usr/lib64/nagios/plugins]$ ./check_UBC.sh
everything is fine..
作為nagios:
-bash-4.1$ ./check_UBC.pl
everything is fine..
-bash-4.1$ ./check_UBC.sh
everything is fine..
所以還是沒有頭緒...
更新2
我的nagios命令定義:
define command{
command_name check_beancounters
command_line $USER1$/check_UBC.pl
}
和服務定義:
define service{
use local-service
host_name localhost
service_description VPS Beancounters
check_command check_beancounters
}
更新3
我設法使其正常工作,但是沒有為nagios用戶提供完全的sudo訪問權限而又沒有密碼,這還沒有結束。 在/ etc / sudoers中,我將其放在最后一行:
nagios ALL=(ALL:ALL) NOPASSWD: ALL
然后將我的命令定義更改為:
define command{
command_name check_beancounters
command_line sudo $USER1$/check_UBC.pl
}
顯然,運行解釋腳本時,Linux的最新版本不尊重+ s權限,只能運行二進制文件。 所以我想我必須為腳本編譯一個二進制包裝器?
更新4
根據Joe Young的建議,我將visudo條目更改為:
nagios ALL=NOPASSWD: /usr/lib64/nagios/plugins/check_UBC.pl
希望是相對無害的!
嘗試將check_UBC.pl
的所有者check_UBC.pl
為root
以便當nagios執行check_UBC.pl
,腳本以其所有者root
而不是nagios
用戶的setuid運行。
chown root:root check_UBC.pl
編輯:
您可以發布調用check_UBC.pl
命令定義嗎?
我能想到的最后一件事是安裝perl-suid模塊: https : check_UBC.pl
雖然,如果check_UBC.pl
運行沒有問題,我不確定會帶來什么變化。
將/ proc / user_beancounters的權限更改為444(全部讀取)會有什么風險?它只包含一個數字,對嗎? 不確定重新啟動后該特定文件是否“卡住”,或更糟糕的是,隨着服務的運行不斷地替換該文件,因此這仍然可能是一個問題。
此外,在嘗試讀取文件之前,請考慮嘗試測試文件的實際“存在”。 由於我們位於/ proc目錄中,因此情況確實會不時發生變化。
最后,您要求打開文件,但是從句法上講它是否要求以只讀模式打開? 您可能希望嘗試通過系統調用在外殼程序腳本中簡單地“捕獲”文件內容,並查看是否收到響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.