perf-security.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. .. _perf_security:
  2. Perf events and tool security
  3. =============================
  4. Overview
  5. --------
  6. Usage of Performance Counters for Linux (perf_events) [1]_ , [2]_ , [3]_
  7. can impose a considerable risk of leaking sensitive data accessed by
  8. monitored processes. The data leakage is possible both in scenarios of
  9. direct usage of perf_events system call API [2]_ and over data files
  10. generated by Perf tool user mode utility (Perf) [3]_ , [4]_ . The risk
  11. depends on the nature of data that perf_events performance monitoring
  12. units (PMU) [2]_ and Perf collect and expose for performance analysis.
  13. Collected system and performance data may be split into several
  14. categories:
  15. 1. System hardware and software configuration data, for example: a CPU
  16. model and its cache configuration, an amount of available memory and
  17. its topology, used kernel and Perf versions, performance monitoring
  18. setup including experiment time, events configuration, Perf command
  19. line parameters, etc.
  20. 2. User and kernel module paths and their load addresses with sizes,
  21. process and thread names with their PIDs and TIDs, timestamps for
  22. captured hardware and software events.
  23. 3. Content of kernel software counters (e.g., for context switches, page
  24. faults, CPU migrations), architectural hardware performance counters
  25. (PMC) [8]_ and machine specific registers (MSR) [9]_ that provide
  26. execution metrics for various monitored parts of the system (e.g.,
  27. memory controller (IMC), interconnect (QPI/UPI) or peripheral (PCIe)
  28. uncore counters) without direct attribution to any execution context
  29. state.
  30. 4. Content of architectural execution context registers (e.g., RIP, RSP,
  31. RBP on x86_64), process user and kernel space memory addresses and
  32. data, content of various architectural MSRs that capture data from
  33. this category.
  34. Data that belong to the fourth category can potentially contain
  35. sensitive process data. If PMUs in some monitoring modes capture values
  36. of execution context registers or data from process memory then access
  37. to such monitoring modes requires to be ordered and secured properly.
  38. So, perf_events performance monitoring and observability operations are
  39. the subject for security access control management [5]_ .
  40. perf_events access control
  41. -------------------------------
  42. To perform security checks, the Linux implementation splits processes
  43. into two categories [6]_ : a) privileged processes (whose effective user
  44. ID is 0, referred to as superuser or root), and b) unprivileged
  45. processes (whose effective UID is nonzero). Privileged processes bypass
  46. all kernel security permission checks so perf_events performance
  47. monitoring is fully available to privileged processes without access,
  48. scope and resource restrictions.
  49. Unprivileged processes are subject to a full security permission check
  50. based on the process's credentials [5]_ (usually: effective UID,
  51. effective GID, and supplementary group list).
  52. Linux divides the privileges traditionally associated with superuser
  53. into distinct units, known as capabilities [6]_ , which can be
  54. independently enabled and disabled on per-thread basis for processes and
  55. files of unprivileged users.
  56. Unprivileged processes with enabled CAP_PERFMON capability are treated
  57. as privileged processes with respect to perf_events performance
  58. monitoring and observability operations, thus, bypass *scope* permissions
  59. checks in the kernel. CAP_PERFMON implements the principle of least
  60. privilege [13]_ (POSIX 1003.1e: 2.2.2.39) for performance monitoring and
  61. observability operations in the kernel and provides a secure approach to
  62. performance monitoring and observability in the system.
  63. For backward compatibility reasons the access to perf_events monitoring and
  64. observability operations is also open for CAP_SYS_ADMIN privileged
  65. processes but CAP_SYS_ADMIN usage for secure monitoring and observability
  66. use cases is discouraged with respect to the CAP_PERFMON capability.
  67. If system audit records [14]_ for a process using perf_events system call
  68. API contain denial records of acquiring both CAP_PERFMON and CAP_SYS_ADMIN
  69. capabilities then providing the process with CAP_PERFMON capability singly
  70. is recommended as the preferred secure approach to resolve double access
  71. denial logging related to usage of performance monitoring and observability.
  72. Prior Linux v5.9 unprivileged processes using perf_events system call
  73. are also subject for PTRACE_MODE_READ_REALCREDS ptrace access mode check
  74. [7]_ , whose outcome determines whether monitoring is permitted.
  75. So unprivileged processes provided with CAP_SYS_PTRACE capability are
  76. effectively permitted to pass the check. Starting from Linux v5.9
  77. CAP_SYS_PTRACE capability is not required and CAP_PERFMON is enough to
  78. be provided for processes to make performance monitoring and observability
  79. operations.
  80. Other capabilities being granted to unprivileged processes can
  81. effectively enable capturing of additional data required for later
  82. performance analysis of monitored processes or a system. For example,
  83. CAP_SYSLOG capability permits reading kernel space memory addresses from
  84. /proc/kallsyms file.
  85. Privileged Perf users groups
  86. ---------------------------------
  87. Mechanisms of capabilities, privileged capability-dumb files [6]_,
  88. file system ACLs [10]_ and sudo [15]_ utility can be used to create
  89. dedicated groups of privileged Perf users who are permitted to execute
  90. performance monitoring and observability without limits. The following
  91. steps can be taken to create such groups of privileged Perf users.
  92. 1. Create perf_users group of privileged Perf users, assign perf_users
  93. group to Perf tool executable and limit access to the executable for
  94. other users in the system who are not in the perf_users group:
  95. ::
  96. # groupadd perf_users
  97. # ls -alhF
  98. -rwxr-xr-x 2 root root 11M Oct 19 15:12 perf
  99. # chgrp perf_users perf
  100. # ls -alhF
  101. -rwxr-xr-x 2 root perf_users 11M Oct 19 15:12 perf
  102. # chmod o-rwx perf
  103. # ls -alhF
  104. -rwxr-x--- 2 root perf_users 11M Oct 19 15:12 perf
  105. 2. Assign the required capabilities to the Perf tool executable file and
  106. enable members of perf_users group with monitoring and observability
  107. privileges [6]_ :
  108. ::
  109. # setcap "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf
  110. # setcap -v "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf
  111. perf: OK
  112. # getcap perf
  113. perf = cap_sys_ptrace,cap_syslog,cap_perfmon+ep
  114. If the libcap [16]_ installed doesn't yet support "cap_perfmon", use "38" instead,
  115. i.e.:
  116. ::
  117. # setcap "38,cap_ipc_lock,cap_sys_ptrace,cap_syslog=ep" perf
  118. Note that you may need to have 'cap_ipc_lock' in the mix for tools such as
  119. 'perf top', alternatively use 'perf top -m N', to reduce the memory that
  120. it uses for the perf ring buffer, see the memory allocation section below.
  121. Using a libcap without support for CAP_PERFMON will make cap_get_flag(caps, 38,
  122. CAP_EFFECTIVE, &val) fail, which will lead the default event to be 'cycles:u',
  123. so as a workaround explicitly ask for the 'cycles' event, i.e.:
  124. ::
  125. # perf top -e cycles
  126. To get kernel and user samples with a perf binary with just CAP_PERFMON.
  127. As a result, members of perf_users group are capable of conducting
  128. performance monitoring and observability by using functionality of the
  129. configured Perf tool executable that, when executes, passes perf_events
  130. subsystem scope checks.
  131. In case Perf tool executable can't be assigned required capabilities (e.g.
  132. file system is mounted with nosuid option or extended attributes are
  133. not supported by the file system) then creation of the capabilities
  134. privileged environment, naturally shell, is possible. The shell provides
  135. inherent processes with CAP_PERFMON and other required capabilities so that
  136. performance monitoring and observability operations are available in the
  137. environment without limits. Access to the environment can be open via sudo
  138. utility for members of perf_users group only. In order to create such
  139. environment:
  140. 1. Create shell script that uses capsh utility [16]_ to assign CAP_PERFMON
  141. and other required capabilities into ambient capability set of the shell
  142. process, lock the process security bits after enabling SECBIT_NO_SETUID_FIXUP,
  143. SECBIT_NOROOT and SECBIT_NO_CAP_AMBIENT_RAISE bits and then change
  144. the process identity to sudo caller of the script who should essentially
  145. be a member of perf_users group:
  146. ::
  147. # ls -alh /usr/local/bin/perf.shell
  148. -rwxr-xr-x. 1 root root 83 Oct 13 23:57 /usr/local/bin/perf.shell
  149. # cat /usr/local/bin/perf.shell
  150. exec /usr/sbin/capsh --iab=^cap_perfmon --secbits=239 --user=$SUDO_USER -- -l
  151. 2. Extend sudo policy at /etc/sudoers file with a rule for perf_users group:
  152. ::
  153. # grep perf_users /etc/sudoers
  154. %perf_users ALL=/usr/local/bin/perf.shell
  155. 3. Check that members of perf_users group have access to the privileged
  156. shell and have CAP_PERFMON and other required capabilities enabled
  157. in permitted, effective and ambient capability sets of an inherent process:
  158. ::
  159. $ id
  160. 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
  161. $ sudo perf.shell
  162. [sudo] password for capsh_test:
  163. $ grep Cap /proc/self/status
  164. CapInh: 0000004000000000
  165. CapPrm: 0000004000000000
  166. CapEff: 0000004000000000
  167. CapBnd: 000000ffffffffff
  168. CapAmb: 0000004000000000
  169. $ capsh --decode=0000004000000000
  170. 0x0000004000000000=cap_perfmon
  171. As a result, members of perf_users group have access to the privileged
  172. environment where they can use tools employing performance monitoring APIs
  173. governed by CAP_PERFMON Linux capability.
  174. This specific access control management is only available to superuser
  175. or root running processes with CAP_SETPCAP, CAP_SETFCAP [6]_
  176. capabilities.
  177. Unprivileged users
  178. -----------------------------------
  179. perf_events *scope* and *access* control for unprivileged processes
  180. is governed by perf_event_paranoid [2]_ setting:
  181. -1:
  182. Impose no *scope* and *access* restrictions on using perf_events
  183. performance monitoring. Per-user per-cpu perf_event_mlock_kb [2]_
  184. locking limit is ignored when allocating memory buffers for storing
  185. performance data. This is the least secure mode since allowed
  186. monitored *scope* is maximized and no perf_events specific limits
  187. are imposed on *resources* allocated for performance monitoring.
  188. >=0:
  189. *scope* includes per-process and system wide performance monitoring
  190. but excludes raw tracepoints and ftrace function tracepoints
  191. monitoring. CPU and system events happened when executing either in
  192. user or in kernel space can be monitored and captured for later
  193. analysis. Per-user per-cpu perf_event_mlock_kb locking limit is
  194. imposed but ignored for unprivileged processes with CAP_IPC_LOCK
  195. [6]_ capability.
  196. >=1:
  197. *scope* includes per-process performance monitoring only and
  198. excludes system wide performance monitoring. CPU and system events
  199. happened when executing either in user or in kernel space can be
  200. monitored and captured for later analysis. Per-user per-cpu
  201. perf_event_mlock_kb locking limit is imposed but ignored for
  202. unprivileged processes with CAP_IPC_LOCK capability.
  203. >=2:
  204. *scope* includes per-process performance monitoring only. CPU and
  205. system events happened when executing in user space only can be
  206. monitored and captured for later analysis. Per-user per-cpu
  207. perf_event_mlock_kb locking limit is imposed but ignored for
  208. unprivileged processes with CAP_IPC_LOCK capability.
  209. Resource control
  210. ---------------------------------
  211. Open file descriptors
  212. +++++++++++++++++++++
  213. The perf_events system call API [2]_ allocates file descriptors for
  214. every configured PMU event. Open file descriptors are a per-process
  215. accountable resource governed by the RLIMIT_NOFILE [11]_ limit
  216. (ulimit -n), which is usually derived from the login shell process. When
  217. configuring Perf collection for a long list of events on a large server
  218. system, this limit can be easily hit preventing required monitoring
  219. configuration. RLIMIT_NOFILE limit can be increased on per-user basis
  220. modifying content of the limits.conf file [12]_ . Ordinarily, a Perf
  221. sampling session (perf record) requires an amount of open perf_event
  222. file descriptors that is not less than the number of monitored events
  223. multiplied by the number of monitored CPUs.
  224. Memory allocation
  225. +++++++++++++++++
  226. The amount of memory available to user processes for capturing
  227. performance monitoring data is governed by the perf_event_mlock_kb [2]_
  228. setting. This perf_event specific resource setting defines overall
  229. per-cpu limits of memory allowed for mapping by the user processes to
  230. execute performance monitoring. The setting essentially extends the
  231. RLIMIT_MEMLOCK [11]_ limit, but only for memory regions mapped
  232. specifically for capturing monitored performance events and related data.
  233. For example, if a machine has eight cores and perf_event_mlock_kb limit
  234. is set to 516 KiB, then a user process is provided with 516 KiB * 8 =
  235. 4128 KiB of memory above the RLIMIT_MEMLOCK limit (ulimit -l) for
  236. perf_event mmap buffers. In particular, this means that, if the user
  237. wants to start two or more performance monitoring processes, the user is
  238. required to manually distribute the available 4128 KiB between the
  239. monitoring processes, for example, using the --mmap-pages Perf record
  240. mode option. Otherwise, the first started performance monitoring process
  241. allocates all available 4128 KiB and the other processes will fail to
  242. proceed due to the lack of memory.
  243. RLIMIT_MEMLOCK and perf_event_mlock_kb resource constraints are ignored
  244. for processes with the CAP_IPC_LOCK capability. Thus, perf_events/Perf
  245. privileged users can be provided with memory above the constraints for
  246. perf_events/Perf performance monitoring purpose by providing the Perf
  247. executable with CAP_IPC_LOCK capability.
  248. Bibliography
  249. ------------
  250. .. [1] `<https://lwn.net/Articles/337493/>`_
  251. .. [2] `<http://man7.org/linux/man-pages/man2/perf_event_open.2.html>`_
  252. .. [3] `<http://web.eece.maine.edu/~vweaver/projects/perf_events/>`_
  253. .. [4] `<https://perf.wiki.kernel.org/index.php/Main_Page>`_
  254. .. [5] `<https://www.kernel.org/doc/html/latest/security/credentials.html>`_
  255. .. [6] `<http://man7.org/linux/man-pages/man7/capabilities.7.html>`_
  256. .. [7] `<http://man7.org/linux/man-pages/man2/ptrace.2.html>`_
  257. .. [8] `<https://en.wikipedia.org/wiki/Hardware_performance_counter>`_
  258. .. [9] `<https://en.wikipedia.org/wiki/Model-specific_register>`_
  259. .. [10] `<http://man7.org/linux/man-pages/man5/acl.5.html>`_
  260. .. [11] `<http://man7.org/linux/man-pages/man2/getrlimit.2.html>`_
  261. .. [12] `<http://man7.org/linux/man-pages/man5/limits.conf.5.html>`_
  262. .. [13] `<https://sites.google.com/site/fullycapable>`_
  263. .. [14] `<http://man7.org/linux/man-pages/man8/auditd.8.html>`_
  264. .. [15] `<https://man7.org/linux/man-pages/man8/sudo.8.html>`_
  265. .. [16] `<https://git.kernel.org/pub/scm/libs/libcap/libcap.git/>`_