0001 .. _perf_security:
0002
0003 Perf events and tool security
0004 =============================
0005
0006 Overview
0007 --------
0008
0009 Usage of Performance Counters for Linux (perf_events) [1]_ , [2]_ , [3]_
0010 can impose a considerable risk of leaking sensitive data accessed by
0011 monitored processes. The data leakage is possible both in scenarios of
0012 direct usage of perf_events system call API [2]_ and over data files
0013 generated by Perf tool user mode utility (Perf) [3]_ , [4]_ . The risk
0014 depends on the nature of data that perf_events performance monitoring
0015 units (PMU) [2]_ and Perf collect and expose for performance analysis.
0016 Collected system and performance data may be split into several
0017 categories:
0018
0019 1. System hardware and software configuration data, for example: a CPU
0020 model and its cache configuration, an amount of available memory and
0021 its topology, used kernel and Perf versions, performance monitoring
0022 setup including experiment time, events configuration, Perf command
0023 line parameters, etc.
0024
0025 2. User and kernel module paths and their load addresses with sizes,
0026 process and thread names with their PIDs and TIDs, timestamps for
0027 captured hardware and software events.
0028
0029 3. Content of kernel software counters (e.g., for context switches, page
0030 faults, CPU migrations), architectural hardware performance counters
0031 (PMC) [8]_ and machine specific registers (MSR) [9]_ that provide
0032 execution metrics for various monitored parts of the system (e.g.,
0033 memory controller (IMC), interconnect (QPI/UPI) or peripheral (PCIe)
0034 uncore counters) without direct attribution to any execution context
0035 state.
0036
0037 4. Content of architectural execution context registers (e.g., RIP, RSP,
0038 RBP on x86_64), process user and kernel space memory addresses and
0039 data, content of various architectural MSRs that capture data from
0040 this category.
0041
0042 Data that belong to the fourth category can potentially contain
0043 sensitive process data. If PMUs in some monitoring modes capture values
0044 of execution context registers or data from process memory then access
0045 to such monitoring modes requires to be ordered and secured properly.
0046 So, perf_events performance monitoring and observability operations are
0047 the subject for security access control management [5]_ .
0048
0049 perf_events access control
0050 -------------------------------
0051
0052 To perform security checks, the Linux implementation splits processes
0053 into two categories [6]_ : a) privileged processes (whose effective user
0054 ID is 0, referred to as superuser or root), and b) unprivileged
0055 processes (whose effective UID is nonzero). Privileged processes bypass
0056 all kernel security permission checks so perf_events performance
0057 monitoring is fully available to privileged processes without access,
0058 scope and resource restrictions.
0059
0060 Unprivileged processes are subject to a full security permission check
0061 based on the process's credentials [5]_ (usually: effective UID,
0062 effective GID, and supplementary group list).
0063
0064 Linux divides the privileges traditionally associated with superuser
0065 into distinct units, known as capabilities [6]_ , which can be
0066 independently enabled and disabled on per-thread basis for processes and
0067 files of unprivileged users.
0068
0069 Unprivileged processes with enabled CAP_PERFMON capability are treated
0070 as privileged processes with respect to perf_events performance
0071 monitoring and observability operations, thus, bypass *scope* permissions
0072 checks in the kernel. CAP_PERFMON implements the principle of least
0073 privilege [13]_ (POSIX 1003.1e: 2.2.2.39) for performance monitoring and
0074 observability operations in the kernel and provides a secure approach to
0075 performance monitoring and observability in the system.
0076
0077 For backward compatibility reasons the access to perf_events monitoring and
0078 observability operations is also open for CAP_SYS_ADMIN privileged
0079 processes but CAP_SYS_ADMIN usage for secure monitoring and observability
0080 use cases is discouraged with respect to the CAP_PERFMON capability.
0081 If system audit records [14]_ for a process using perf_events system call
0082 API contain denial records of acquiring both CAP_PERFMON and CAP_SYS_ADMIN
0083 capabilities then providing the process with CAP_PERFMON capability singly
0084 is recommended as the preferred secure approach to resolve double access
0085 denial logging related to usage of performance monitoring and observability.
0086
0087 Prior Linux v5.9 unprivileged processes using perf_events system call
0088 are also subject for PTRACE_MODE_READ_REALCREDS ptrace access mode check
0089 [7]_ , whose outcome determines whether monitoring is permitted.
0090 So unprivileged processes provided with CAP_SYS_PTRACE capability are
0091 effectively permitted to pass the check. Starting from Linux v5.9
0092 CAP_SYS_PTRACE capability is not required and CAP_PERFMON is enough to
0093 be provided for processes to make performance monitoring and observability
0094 operations.
0095
0096 Other capabilities being granted to unprivileged processes can
0097 effectively enable capturing of additional data required for later
0098 performance analysis of monitored processes or a system. For example,
0099 CAP_SYSLOG capability permits reading kernel space memory addresses from
0100 /proc/kallsyms file.
0101
0102 Privileged Perf users groups
0103 ---------------------------------
0104
0105 Mechanisms of capabilities, privileged capability-dumb files [6]_,
0106 file system ACLs [10]_ and sudo [15]_ utility can be used to create
0107 dedicated groups of privileged Perf users who are permitted to execute
0108 performance monitoring and observability without limits. The following
0109 steps can be taken to create such groups of privileged Perf users.
0110
0111 1. Create perf_users group of privileged Perf users, assign perf_users
0112 group to Perf tool executable and limit access to the executable for
0113 other users in the system who are not in the perf_users group:
0114
0115 ::
0116
0117 # groupadd perf_users
0118 # ls -alhF
0119 -rwxr-xr-x 2 root root 11M Oct 19 15:12 perf
0120 # chgrp perf_users perf
0121 # ls -alhF
0122 -rwxr-xr-x 2 root perf_users 11M Oct 19 15:12 perf
0123 # chmod o-rwx perf
0124 # ls -alhF
0125 -rwxr-x--- 2 root perf_users 11M Oct 19 15:12 perf
0126
0127 2. Assign the required capabilities to the Perf tool executable file and
0128 enable members of perf_users group with monitoring and observability
0129 privileges [6]_ :
0130
0131 ::
0132
0133 # setcap "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf
0134 # setcap -v "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf
0135 perf: OK
0136 # getcap perf
0137 perf = cap_sys_ptrace,cap_syslog,cap_perfmon+ep
0138
0139 If the libcap [16]_ installed doesn't yet support "cap_perfmon", use "38" instead,
0140 i.e.:
0141
0142 ::
0143
0144 # setcap "38,cap_ipc_lock,cap_sys_ptrace,cap_syslog=ep" perf
0145
0146 Note that you may need to have 'cap_ipc_lock' in the mix for tools such as
0147 'perf top', alternatively use 'perf top -m N', to reduce the memory that
0148 it uses for the perf ring buffer, see the memory allocation section below.
0149
0150 Using a libcap without support for CAP_PERFMON will make cap_get_flag(caps, 38,
0151 CAP_EFFECTIVE, &val) fail, which will lead the default event to be 'cycles:u',
0152 so as a workaround explicitly ask for the 'cycles' event, i.e.:
0153
0154 ::
0155
0156 # perf top -e cycles
0157
0158 To get kernel and user samples with a perf binary with just CAP_PERFMON.
0159
0160 As a result, members of perf_users group are capable of conducting
0161 performance monitoring and observability by using functionality of the
0162 configured Perf tool executable that, when executes, passes perf_events
0163 subsystem scope checks.
0164
0165 In case Perf tool executable can't be assigned required capabilities (e.g.
0166 file system is mounted with nosuid option or extended attributes are
0167 not supported by the file system) then creation of the capabilities
0168 privileged environment, naturally shell, is possible. The shell provides
0169 inherent processes with CAP_PERFMON and other required capabilities so that
0170 performance monitoring and observability operations are available in the
0171 environment without limits. Access to the environment can be open via sudo
0172 utility for members of perf_users group only. In order to create such
0173 environment:
0174
0175 1. Create shell script that uses capsh utility [16]_ to assign CAP_PERFMON
0176 and other required capabilities into ambient capability set of the shell
0177 process, lock the process security bits after enabling SECBIT_NO_SETUID_FIXUP,
0178 SECBIT_NOROOT and SECBIT_NO_CAP_AMBIENT_RAISE bits and then change
0179 the process identity to sudo caller of the script who should essentially
0180 be a member of perf_users group:
0181
0182 ::
0183
0184 # ls -alh /usr/local/bin/perf.shell
0185 -rwxr-xr-x. 1 root root 83 Oct 13 23:57 /usr/local/bin/perf.shell
0186 # cat /usr/local/bin/perf.shell
0187 exec /usr/sbin/capsh --iab=^cap_perfmon --secbits=239 --user=$SUDO_USER -- -l
0188
0189 2. Extend sudo policy at /etc/sudoers file with a rule for perf_users group:
0190
0191 ::
0192
0193 # grep perf_users /etc/sudoers
0194 %perf_users ALL=/usr/local/bin/perf.shell
0195
0196 3. Check that members of perf_users group have access to the privileged
0197 shell and have CAP_PERFMON and other required capabilities enabled
0198 in permitted, effective and ambient capability sets of an inherent process:
0199
0200 ::
0201
0202 $ id
0203 uid=1003(capsh_test) gid=1004(capsh_test) groups=1004(capsh_test),1000(perf_users) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
0204 $ sudo perf.shell
0205 [sudo] password for capsh_test:
0206 $ grep Cap /proc/self/status
0207 CapInh: 0000004000000000
0208 CapPrm: 0000004000000000
0209 CapEff: 0000004000000000
0210 CapBnd: 000000ffffffffff
0211 CapAmb: 0000004000000000
0212 $ capsh --decode=0000004000000000
0213 0x0000004000000000=cap_perfmon
0214
0215 As a result, members of perf_users group have access to the privileged
0216 environment where they can use tools employing performance monitoring APIs
0217 governed by CAP_PERFMON Linux capability.
0218
0219 This specific access control management is only available to superuser
0220 or root running processes with CAP_SETPCAP, CAP_SETFCAP [6]_
0221 capabilities.
0222
0223 Unprivileged users
0224 -----------------------------------
0225
0226 perf_events *scope* and *access* control for unprivileged processes
0227 is governed by perf_event_paranoid [2]_ setting:
0228
0229 -1:
0230 Impose no *scope* and *access* restrictions on using perf_events
0231 performance monitoring. Per-user per-cpu perf_event_mlock_kb [2]_
0232 locking limit is ignored when allocating memory buffers for storing
0233 performance data. This is the least secure mode since allowed
0234 monitored *scope* is maximized and no perf_events specific limits
0235 are imposed on *resources* allocated for performance monitoring.
0236
0237 >=0:
0238 *scope* includes per-process and system wide performance monitoring
0239 but excludes raw tracepoints and ftrace function tracepoints
0240 monitoring. CPU and system events happened when executing either in
0241 user or in kernel space can be monitored and captured for later
0242 analysis. Per-user per-cpu perf_event_mlock_kb locking limit is
0243 imposed but ignored for unprivileged processes with CAP_IPC_LOCK
0244 [6]_ capability.
0245
0246 >=1:
0247 *scope* includes per-process performance monitoring only and
0248 excludes system wide performance monitoring. CPU and system events
0249 happened when executing either in user or in kernel space can be
0250 monitored and captured for later analysis. Per-user per-cpu
0251 perf_event_mlock_kb locking limit is imposed but ignored for
0252 unprivileged processes with CAP_IPC_LOCK capability.
0253
0254 >=2:
0255 *scope* includes per-process performance monitoring only. CPU and
0256 system events happened when executing in user space only can be
0257 monitored and captured for later analysis. Per-user per-cpu
0258 perf_event_mlock_kb locking limit is imposed but ignored for
0259 unprivileged processes with CAP_IPC_LOCK capability.
0260
0261 Resource control
0262 ---------------------------------
0263
0264 Open file descriptors
0265 +++++++++++++++++++++
0266
0267 The perf_events system call API [2]_ allocates file descriptors for
0268 every configured PMU event. Open file descriptors are a per-process
0269 accountable resource governed by the RLIMIT_NOFILE [11]_ limit
0270 (ulimit -n), which is usually derived from the login shell process. When
0271 configuring Perf collection for a long list of events on a large server
0272 system, this limit can be easily hit preventing required monitoring
0273 configuration. RLIMIT_NOFILE limit can be increased on per-user basis
0274 modifying content of the limits.conf file [12]_ . Ordinarily, a Perf
0275 sampling session (perf record) requires an amount of open perf_event
0276 file descriptors that is not less than the number of monitored events
0277 multiplied by the number of monitored CPUs.
0278
0279 Memory allocation
0280 +++++++++++++++++
0281
0282 The amount of memory available to user processes for capturing
0283 performance monitoring data is governed by the perf_event_mlock_kb [2]_
0284 setting. This perf_event specific resource setting defines overall
0285 per-cpu limits of memory allowed for mapping by the user processes to
0286 execute performance monitoring. The setting essentially extends the
0287 RLIMIT_MEMLOCK [11]_ limit, but only for memory regions mapped
0288 specifically for capturing monitored performance events and related data.
0289
0290 For example, if a machine has eight cores and perf_event_mlock_kb limit
0291 is set to 516 KiB, then a user process is provided with 516 KiB * 8 =
0292 4128 KiB of memory above the RLIMIT_MEMLOCK limit (ulimit -l) for
0293 perf_event mmap buffers. In particular, this means that, if the user
0294 wants to start two or more performance monitoring processes, the user is
0295 required to manually distribute the available 4128 KiB between the
0296 monitoring processes, for example, using the --mmap-pages Perf record
0297 mode option. Otherwise, the first started performance monitoring process
0298 allocates all available 4128 KiB and the other processes will fail to
0299 proceed due to the lack of memory.
0300
0301 RLIMIT_MEMLOCK and perf_event_mlock_kb resource constraints are ignored
0302 for processes with the CAP_IPC_LOCK capability. Thus, perf_events/Perf
0303 privileged users can be provided with memory above the constraints for
0304 perf_events/Perf performance monitoring purpose by providing the Perf
0305 executable with CAP_IPC_LOCK capability.
0306
0307 Bibliography
0308 ------------
0309
0310 .. [1] `<https://lwn.net/Articles/337493/>`_
0311 .. [2] `<http://man7.org/linux/man-pages/man2/perf_event_open.2.html>`_
0312 .. [3] `<http://web.eece.maine.edu/~vweaver/projects/perf_events/>`_
0313 .. [4] `<https://perf.wiki.kernel.org/index.php/Main_Page>`_
0314 .. [5] `<https://www.kernel.org/doc/html/latest/security/credentials.html>`_
0315 .. [6] `<http://man7.org/linux/man-pages/man7/capabilities.7.html>`_
0316 .. [7] `<http://man7.org/linux/man-pages/man2/ptrace.2.html>`_
0317 .. [8] `<https://en.wikipedia.org/wiki/Hardware_performance_counter>`_
0318 .. [9] `<https://en.wikipedia.org/wiki/Model-specific_register>`_
0319 .. [10] `<http://man7.org/linux/man-pages/man5/acl.5.html>`_
0320 .. [11] `<http://man7.org/linux/man-pages/man2/getrlimit.2.html>`_
0321 .. [12] `<http://man7.org/linux/man-pages/man5/limits.conf.5.html>`_
0322 .. [13] `<https://sites.google.com/site/fullycapable>`_
0323 .. [14] `<http://man7.org/linux/man-pages/man8/auditd.8.html>`_
0324 .. [15] `<https://man7.org/linux/man-pages/man8/sudo.8.html>`_
0325 .. [16] `<https://git.kernel.org/pub/scm/libs/libcap/libcap.git/>`_