
在我安裝的 Ubuntu Focal 上,kernel.perf_event_paranoid
預設為 4:
$ sysctl kernel.perf_event_paranoid
kernel.perf_event_paranoid = 4
(我已經檢查了/etc/sysctl.conf
相關的配置目錄,我還沒有設定它。)
這對我來說似乎很奇怪,因為內核文件沒有描述大於 2 的值的任何額外效果:
perf_event_paranoid:
Controls use of the performance events system by unprivileged
users (without CAP_SYS_ADMIN). The default value is 2.
-1: Allow use of (almost) all events by all users
Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>=0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
Disallow raw tracepoint access by users without CAP_SYS_ADMIN
>=1: Disallow CPU event access by users without CAP_SYS_ADMIN
>=2: Disallow kernel profiling by users without CAP_SYS_ADMIN
(來源.)
我快速搜尋了主線內核原始碼,但找不到任何與perf_event_paranoid
大於 2 的數字進行比較的地方。
然而,設定為 4 確實有效果。我運行了以下 perf 命令,perf_event_paranoid
設定為 4,作為非根用戶:
perf stat -e context-switches,cpu-migrations -r 1 -- sleep 1
它顯示以下錯誤:
Error:
Access to performance monitoring and observability operations is limited.
Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open
access to performance monitoring and observability operations for processes
without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.
More information can be found at 'Perf events and tool security' document:
https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html
perf_event_paranoid setting is 4:
-1: Allow use of (almost) all events by all users
Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>= 0: Disallow raw and ftrace function tracepoint access
>= 1: Disallow CPU event access
>= 2: Disallow kernel profiling
To make the adjusted perf_event_paranoid setting permanent preserve it
in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)
如果我更改kernel.perf_event_paranoid
為 3,並執行相同的命令,我會再次以非 root 使用者身分得到以下結果:
Performance counter stats for 'sleep 1':
0 context-switches:u
0 cpu-migrations:u
1.000502603 seconds time elapsed
0.000460000 seconds user
0.000000000 seconds sys
因此,將perf_event_paranoid
設定從預設的 4 更改為 3 會產生一定的效果,即使 Linux 核心文件沒有說明具體內容。
是什麼賦予了? Ubuntu 是否附帶了一個自訂補丁來添加新的、更偏執的效能等級?
其他發行版
Debian 似乎帶有一個非標準補丁,創建了perf_event_paranoid
3 級:
kernel.perf_event_paranoid=3
Jeff Vander Stoep 於 7 月27日發布了該perf_event_open()
補丁CAP_SYS_ADMIN
。目前,perf_event_paranoid
預設為 2,這不允許不具備適當功能的進程存取某些效能功能(原始追蹤點存取、CPU 事件存取和核心分析);該補丁不會更改預設值。他還提交了另一個補丁,允許配置內核以使 3 成為預設的 perf_event_paranoid 值。
(來源。)
但 Ubuntu 似乎比這更偏執。
答案1
4 級的作用與 Debian 補丁完全相同:它禁止非特權程序使用perf_event_open()
.然而,限制在偏執4級而不是偏執3級時生效。
目前的偏執等級記錄在核心原始碼檔案中的註釋中kernel/events/core.c
。
/*
* perf event paranoia level:
* -1 - not paranoid at all
* 0 - disallow raw tracepoint access for unpriv
* 1 - disallow cpu events for unpriv
* 2 - disallow kernel profiling for unpriv
* 4 - disallow all unpriv perf event use
*/
可以找到進行此更改的提交這裡。 (注意:提交訊息和內核註釋相互矛盾。內核註釋對我來說似乎是正確的。)
總結一下:
- 等級 -1 到 2:與主線核心相同。
- 3級:與2級相同?無法100%確認這一點。有一個常數
PERF_SECURITY_TRACEPOINT
設定為 3,某些地方仍在使用該常數。 - 等級 4:對非特權使用者完全停用 perf_event_open。
答案2
正如 Nick 正確指出的那樣,除了對非特權用戶禁用它之外,偏執等級 4 似乎也對特權文件應用了相同的限制。
例如,在我的 Ubuntu 20.04 系統上,我按照以下設定設定了一個 perf_users 群組官方文檔,並具有perf
以下能力
$ getcap perf
perf = cap_sys_ptrace,cap_syslog,38+ep
對於非特權使用者來說,以偏執等級 3 運作可以正常運作。然而,在偏執等級 4 上,perf
無論文件功能如何,都會從問題中傳回錯誤。