Perf events and tool security

Overview #

Usage of Performance Counters for Linux (perf_events) 1 , 2 , 3 can impose a considerable risk of leaking sensitive data accessed by monitored processes. The data leakage is possible both in scenarios of direct usage of perf_events system call API 2 and over data files generated by Perf tool user mode utility (Perf) 3 , 4 . The risk depends on the nature of data that perf_events performance monitoring units (PMU) 2 and Perf collect and expose for performance analysis. Collected system and performance data may be split into several categories:

  1. System hardware and software configuration data, for example: a CPU model and its cache configuration, an amount of available memory and its topology, used kernel and Perf versions, performance monitoring setup including experiment time, events configuration, Perf command line parameters, etc.

  2. User and kernel module paths and their load addresses with sizes, process and thread names with their PIDs and TIDs, timestamps for captured hardware and software events.

  3. Content of kernel software counters (e.g., for context switches, page faults, CPU migrations), architectural hardware performance counters (PMC) 8 and machine specific registers (MSR) 9 that provide execution metrics for various monitored parts of the system (e.g., memory controller (IMC), interconnect (QPI/UPI) or peripheral (PCIe) uncore counters) without direct attribution to any execution context state.

  4. Content of architectural execution context registers (e.g., RIP, RSP, RBP on x86_64), process user and kernel space memory addresses and data, content of various architectural MSRs that capture data from this category.

Data that belong to the fourth category can potentially contain sensitive process data. If PMUs in some monitoring modes capture values of execution context registers or data from process memory then access to such monitoring modes requires to be ordered and secured properly. So, perf_events performance monitoring and observability operations are the subject for security access control management 5 .

perf_events access control #

Peios authorizes performance monitoring through native token privileges and process access checks. UID 0 does not bypass these checks. setcap, capsh and set-user-ID wrappers do not grant native profiling authority.

  • Profiling the calling process needs no additional KACS profiling privilege, but Linux perf scope, event and resource checks still apply.
  • Profiling another process requires enabled SeProfileSingleProcessPrivilege, PROCESS_QUERY_INFORMATION access to that process, and PIP dominance.
  • System-wide profiling with perf_event_open(pid == -1) outside cgroup mode requires enabled SeSystemProfilePrivilege.

The compatibility CAP_PERFMON check passes for any of SeSystemProfilePrivilege, SeProfileSingleProcessPrivilege or SeLoadDriverPrivilege. Passing it does not bypass the operation-specific checks above. CAP_SYS_ADMIN maps to SeTcbPrivilege; CAP_SYS_PTRACE maps to SeDebugPrivilege. Debug privilege does not bypass process integrity protection. Nor does profiling authority grant unrestricted access to /proc/PID/mem.

Cgroup profiling has a different boundary in this implementation. With PERF_FLAG_PID_CGROUP, pid is a cgroup directory file descriptor and cpu selects a CPU. This path skips both the per-process KACS profiling call and the explicit system-wide SeSystemProfilePrivilege call. It still checks the cgroup handle and inherited perf scope, event and resource rules. In particular, CPU-event access uses perf_event_paranoid and perfmon_capable(); that is not the same as requiring SeSystemProfilePrivilege. There is no per-target process-query/PIP check from these skipped gates for each process observed in the cgroup. The Peios base configuration enables cgroup perf support. Treat this as an implementation boundary, not an assurance of equivalent protection across profiling modes.

Delegating profiling access #

Run the tool with a native token whose enabled privileges and target access permit the intended operation. The old Linux recipes that assigned file capabilities to perf or used capsh through sudo are not Peios delegation procedures. File execution access and any required PIP trust must also be established. See Peios kernel behaviour for the capability mapping and object-access rules.

Linux perf scope controls #

The following are the inherited Linux perf scope controls. They are additional to KACS: even perf_event_paranoid=-1 does not bypass the native cross-process or system-wide profiling gates above. Here, capability checks use the native mappings described earlier.

The Linux perf scope control is governed by perf_event_paranoid 2 setting:

  • -1:

    Disable these paranoid-level Linux scope restrictions; the native gates described above and other event checks remain. Per-user per-cpu perf_event_mlock_kb 2 locking limit is ignored when allocating memory buffers for storing performance data. This is the least secure mode since allowed monitored scope is maximized and the paranoid-level memory-lock exception is broadest. File-descriptor limits and other event/resource constraints still apply.

  • >=0:

    scope includes per-process and system wide performance monitoring but excludes raw tracepoints and ftrace function tracepoints monitoring. CPU and system events happened when executing either in user or in kernel space can be monitored and captured for later analysis. Per-user per-cpu perf_event_mlock_kb locking limit is imposed but ignored for unprivileged processes with CAP_IPC_LOCK 6 capability.

  • >=1:

    scope includes per-process performance monitoring only and excludes system wide performance monitoring. CPU and system events happened when executing either in user or in kernel space can be monitored and captured for later analysis. Per-user per-cpu perf_event_mlock_kb locking limit is imposed but ignored for unprivileged processes with CAP_IPC_LOCK capability.

  • >=2:

    scope includes per-process performance monitoring only. CPU and system events happened when executing in user space only can be monitored and captured for later analysis. Per-user per-cpu perf_event_mlock_kb locking limit is imposed but ignored for unprivileged processes with CAP_IPC_LOCK capability.

Resource control #

Open file descriptors #

The perf_events system call API 2 allocates file descriptors for every configured PMU event. Open file descriptors are a per-process accountable resource governed by the RLIMIT_NOFILE 11 limit (ulimit -n), which is usually derived from the login shell process. When configuring Perf collection for a long list of events on a large server system, this limit can be easily hit preventing required monitoring configuration. Configure the limit through the process launcher or an authorized resource-limit operation. Linux distributions may use limits.conf through PAM 12, but editing that file alone is not a Peios kernel configuration interface. Ordinarily, a Perf sampling session (perf record) requires an amount of open perf_event file descriptors that is not less than the number of monitored events multiplied by the number of monitored CPUs.

Memory allocation #

The amount of memory available to user processes for capturing performance monitoring data is governed by the perf_event_mlock_kb 2 setting. This perf_event specific resource setting defines overall per-cpu limits of memory allowed for mapping by the user processes to execute performance monitoring. The setting essentially extends the RLIMIT_MEMLOCK 11 limit, but only for memory regions mapped specifically for capturing monitored performance events and related data.

For example, if a machine has eight cores and perf_event_mlock_kb limit is set to 516 KiB, then a user process is provided with 516 KiB * 8 = 4128 KiB of memory above the RLIMIT_MEMLOCK limit (ulimit -l) for perf_event mmap buffers. In particular, this means that, if the user wants to start two or more performance monitoring processes, the user is required to manually distribute the available 4128 KiB between the monitoring processes, for example, using the --mmap-pages Perf record mode option. Otherwise, the first started performance monitoring process allocates all available 4128 KiB and the other processes will fail to proceed due to the lack of memory.

RLIMIT_MEMLOCK and perf_event_mlock_kb resource constraints are ignored for processes with the CAP_IPC_LOCK capability. In Peios this check requires enabled SeLockMemoryPrivilege in the subject token; assigning a file capability to the executable is not a grant mechanism.

Bibliography #

[1] https://lwn.net/Articles/337493/

[2] http://man7.org/linux/man-pages/man2/perf_event_open.2.html

[3] http://web.eece.maine.edu/~vweaver/projects/perf_events/

[4] https://perf.wiki.kernel.org/index.php/Main_Page

[5] https://www.kernel.org/doc/html/latest/security/credentials.html

[6] http://man7.org/linux/man-pages/man7/capabilities.7.html

[7] http://man7.org/linux/man-pages/man2/ptrace.2.html

[8] https://en.wikipedia.org/wiki/Hardware_performance_counter

[9] https://en.wikipedia.org/wiki/Model-specific_register

[10] http://man7.org/linux/man-pages/man5/acl.5.html

[11] http://man7.org/linux/man-pages/man2/getrlimit.2.html

[12] http://man7.org/linux/man-pages/man5/limits.conf.5.html

[13] https://sites.google.com/site/fullycapable

[14] http://man7.org/linux/man-pages/man8/auditd.8.html

[15] https://man7.org/linux/man-pages/man8/sudo.8.html

[16] https://git.kernel.org/pub/scm/libs/libcap/libcap.git/

Edit this page