# Perf events and tool security

_Peios Kernel / Administration / System administration / CPU, memory and workload controls_

> Native profiling authorization, Linux perf controls and the cgroup-mode boundary.

<a id="rst-perf-events-and-tool-security"></a>
<a id="rst-perf-security"></a>

<a id="rst-overview"></a>

## Overview

Usage of Performance Counters for Linux (perf_events) <a id="rst-footnote-reference-1"></a>[1](#rst-footnote-1) , <a id="rst-footnote-reference-2"></a>[2](#rst-footnote-2) , <a id="rst-footnote-reference-3"></a>[3](#rst-footnote-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 <a id="rst-footnote-reference-4"></a>[2](#rst-footnote-2) and over data files generated by Perf tool user mode utility (Perf) <a id="rst-footnote-reference-5"></a>[3](#rst-footnote-3) , <a id="rst-footnote-reference-6"></a>[4](#rst-footnote-4) . The risk depends on the nature of data that perf_events performance monitoring units (PMU) <a id="rst-footnote-reference-7"></a>[2](#rst-footnote-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) <a id="rst-footnote-reference-8"></a>[8](#rst-footnote-8) and machine specific registers (MSR) <a id="rst-footnote-reference-9"></a>[9](#rst-footnote-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 <a id="rst-footnote-reference-10"></a>[5](#rst-footnote-5) .

<a id="rst-perf-events-access-control"></a>

## 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.

<a id="rst-privileged-perf-users-groups"></a>

## 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](</kernel/introduction/peios-behaviour.md>) for the capability mapping and object-access rules.

<a id="rst-unprivileged-users"></a>

## 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 <a id="rst-footnote-reference-24"></a>[2](#rst-footnote-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 <a id="rst-footnote-reference-25"></a>[2](#rst-footnote-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.

- **&gt;=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 <a id="rst-footnote-reference-26"></a>[6](#rst-footnote-6) capability.

- **&gt;=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.

- **&gt;=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.

<a id="rst-resource-control"></a>

## Resource control

<a id="rst-open-file-descriptors"></a>

### Open file descriptors

The perf_events system call API <a id="rst-footnote-reference-27"></a>[2](#rst-footnote-2) allocates file descriptors for every configured PMU event. Open file descriptors are a per-process accountable resource governed by the RLIMIT_NOFILE <a id="rst-footnote-reference-28"></a>[11](#rst-footnote-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 <a id="rst-footnote-reference-29"></a>[12](#rst-footnote-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.

<a id="rst-memory-allocation"></a>

### Memory allocation

The amount of memory available to user processes for capturing performance monitoring data is governed by the perf_event_mlock_kb <a id="rst-footnote-reference-30"></a>[2](#rst-footnote-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 <a id="rst-footnote-reference-31"></a>[11](#rst-footnote-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.

<a id="rst-bibliography"></a>

## Bibliography

<a id="rst-footnote-1"></a>



**[1]** [https://lwn.net/Articles/337493/](<https://lwn.net/Articles/337493/>)<a id="rst-https-lwn-net-articles-337493"></a>

<a id="rst-footnote-2"></a>



**[2]** [http://man7.org/linux/man-pages/man2/perf_event_open.2.html](<http://man7.org/linux/man-pages/man2/perf_event_open.2.html>)<a id="rst-http-man7-org-linux-man-pages-man2-perf-event-open-2-html"></a>

<a id="rst-footnote-3"></a>



**[3]** [http://web.eece.maine.edu/\~vweaver/projects/perf_events/](<http://web.eece.maine.edu/~vweaver/projects/perf_events/>)<a id="rst-http-web-eece-maine-edu-vweaver-projects-perf-events"></a>

<a id="rst-footnote-4"></a>



**[4]** [https://perf.wiki.kernel.org/index.php/Main_Page](<https://perf.wiki.kernel.org/index.php/Main_Page>)<a id="rst-https-perf-wiki-kernel-org-index-php-main-page"></a>

<a id="rst-footnote-5"></a>



**[5]** [https://www.kernel.org/doc/html/latest/security/credentials.html](<https://www.kernel.org/doc/html/latest/security/credentials.html>)<a id="rst-https-www-kernel-org-doc-html-latest-security-credentials-html"></a>

<a id="rst-footnote-6"></a>



**[6]** [http://man7.org/linux/man-pages/man7/capabilities.7.html](<http://man7.org/linux/man-pages/man7/capabilities.7.html>)<a id="rst-http-man7-org-linux-man-pages-man7-capabilities-7-html"></a>

<a id="rst-footnote-7"></a>



**[7]** [http://man7.org/linux/man-pages/man2/ptrace.2.html](<http://man7.org/linux/man-pages/man2/ptrace.2.html>)<a id="rst-http-man7-org-linux-man-pages-man2-ptrace-2-html"></a>

<a id="rst-footnote-8"></a>



**[8]** [https://en.wikipedia.org/wiki/Hardware_performance_counter](<https://en.wikipedia.org/wiki/Hardware_performance_counter>)<a id="rst-https-en-wikipedia-org-wiki-hardware-performance-counter"></a>

<a id="rst-footnote-9"></a>



**[9]** [https://en.wikipedia.org/wiki/Model-specific_register](<https://en.wikipedia.org/wiki/Model-specific_register>)<a id="rst-https-en-wikipedia-org-wiki-model-specific-register"></a>

<a id="rst-footnote-10"></a>



**[10]** [http://man7.org/linux/man-pages/man5/acl.5.html](<http://man7.org/linux/man-pages/man5/acl.5.html>)<a id="rst-http-man7-org-linux-man-pages-man5-acl-5-html"></a>

<a id="rst-footnote-11"></a>



**[11]** [http://man7.org/linux/man-pages/man2/getrlimit.2.html](<http://man7.org/linux/man-pages/man2/getrlimit.2.html>)<a id="rst-http-man7-org-linux-man-pages-man2-getrlimit-2-html"></a>

<a id="rst-footnote-12"></a>



**[12]** [http://man7.org/linux/man-pages/man5/limits.conf.5.html](<http://man7.org/linux/man-pages/man5/limits.conf.5.html>)<a id="rst-http-man7-org-linux-man-pages-man5-limits-conf-5-html"></a>

<a id="rst-footnote-13"></a>



**[13]** [https://sites.google.com/site/fullycapable](<https://sites.google.com/site/fullycapable>)<a id="rst-https-sites-google-com-site-fullycapable"></a>

<a id="rst-footnote-14"></a>



**[14]** [http://man7.org/linux/man-pages/man8/auditd.8.html](<http://man7.org/linux/man-pages/man8/auditd.8.html>)<a id="rst-http-man7-org-linux-man-pages-man8-auditd-8-html"></a>

<a id="rst-footnote-15"></a>



**[15]** [https://man7.org/linux/man-pages/man8/sudo.8.html](<https://man7.org/linux/man-pages/man8/sudo.8.html>)<a id="rst-https-man7-org-linux-man-pages-man8-sudo-8-html"></a>

<a id="rst-footnote-16"></a>



**[16]** [https://git.kernel.org/pub/scm/libs/libcap/libcap.git/](<https://git.kernel.org/pub/scm/libs/libcap/libcap.git/>)<a id="rst-https-git-kernel-org-pub-scm-libs-libcap-libcap-git"></a>

<a id="rst-footnote-reference-11"></a>
<a id="rst-footnote-reference-12"></a>
<a id="rst-footnote-reference-13"></a>
<a id="rst-footnote-reference-14"></a>
<a id="rst-footnote-reference-15"></a>
<a id="rst-footnote-reference-16"></a>
<a id="rst-footnote-reference-17"></a>
<a id="rst-footnote-reference-18"></a>
<a id="rst-footnote-reference-19"></a>
<a id="rst-footnote-reference-20"></a>
<a id="rst-footnote-reference-21"></a>
<a id="rst-footnote-reference-22"></a>
<a id="rst-footnote-reference-23"></a>
