xref: /linux/Documentation/userspace-api/check_exec.rst (revision 21266b8df5224c4f677acf9f353eecc9094731f0)
1a5874fdeSMickaël Salaün.. SPDX-License-Identifier: GPL-2.0
2a5874fdeSMickaël Salaün.. Copyright © 2024 Microsoft Corporation
3a5874fdeSMickaël Salaün
4a5874fdeSMickaël Salaün===================
5a5874fdeSMickaël SalaünExecutability check
6a5874fdeSMickaël Salaün===================
7a5874fdeSMickaël Salaün
8*a0623b2aSMickaël SalaünThe ``AT_EXECVE_CHECK`` :manpage:`execveat(2)` flag, and the
9*a0623b2aSMickaël Salaün``SECBIT_EXEC_RESTRICT_FILE`` and ``SECBIT_EXEC_DENY_INTERACTIVE`` securebits
10*a0623b2aSMickaël Salaünare intended for script interpreters and dynamic linkers to enforce a
11*a0623b2aSMickaël Salaünconsistent execution security policy handled by the kernel.  See the
12*a0623b2aSMickaël Salaün`samples/check-exec/inc.c`_ example.
13*a0623b2aSMickaël Salaün
14*a0623b2aSMickaël SalaünWhether an interpreter should check these securebits or not depends on the
15*a0623b2aSMickaël Salaünsecurity risk of running malicious scripts with respect to the execution
16*a0623b2aSMickaël Salaünenvironment, and whether the kernel can check if a script is trustworthy or
17*a0623b2aSMickaël Salaünnot.  For instance, Python scripts running on a server can use arbitrary
18*a0623b2aSMickaël Salaünsyscalls and access arbitrary files.  Such interpreters should then be
19*a0623b2aSMickaël Salaünenlighten to use these securebits and let users define their security policy.
20*a0623b2aSMickaël SalaünHowever, a JavaScript engine running in a web browser should already be
21*a0623b2aSMickaël Salaünsandboxed and then should not be able to harm the user's environment.
22*a0623b2aSMickaël Salaün
23*a0623b2aSMickaël SalaünScript interpreters or dynamic linkers built for tailored execution environments
24*a0623b2aSMickaël Salaün(e.g. hardened Linux distributions or hermetic container images) could use
25*a0623b2aSMickaël Salaün``AT_EXECVE_CHECK`` without checking the related securebits if backward
26*a0623b2aSMickaël Salaüncompatibility is handled by something else (e.g. atomic update ensuring that
27*a0623b2aSMickaël Salaünall legitimate libraries are allowed to be executed).  It is then recommended
28*a0623b2aSMickaël Salaünfor script interpreters and dynamic linkers to check the securebits at run time
29*a0623b2aSMickaël Salaünby default, but also to provide the ability for custom builds to behave like if
30*a0623b2aSMickaël Salaün``SECBIT_EXEC_RESTRICT_FILE`` or ``SECBIT_EXEC_DENY_INTERACTIVE`` were always
31*a0623b2aSMickaël Salaünset to 1 (i.e. always enforce restrictions).
32*a0623b2aSMickaël Salaün
33a5874fdeSMickaël SalaünAT_EXECVE_CHECK
34a5874fdeSMickaël Salaün===============
35a5874fdeSMickaël Salaün
36a5874fdeSMickaël SalaünPassing the ``AT_EXECVE_CHECK`` flag to :manpage:`execveat(2)` only performs a
37a5874fdeSMickaël Salaüncheck on a regular file and returns 0 if execution of this file would be
38a5874fdeSMickaël Salaünallowed, ignoring the file format and then the related interpreter dependencies
39a5874fdeSMickaël Salaün(e.g. ELF libraries, script's shebang).
40a5874fdeSMickaël Salaün
41a5874fdeSMickaël SalaünPrograms should always perform this check to apply kernel-level checks against
42a5874fdeSMickaël Salaünfiles that are not directly executed by the kernel but passed to a user space
43a5874fdeSMickaël Salaüninterpreter instead.  All files that contain executable code, from the point of
44a5874fdeSMickaël Salaünview of the interpreter, should be checked.  However the result of this check
45a5874fdeSMickaël Salaünshould only be enforced according to ``SECBIT_EXEC_RESTRICT_FILE`` or
46a5874fdeSMickaël Salaün``SECBIT_EXEC_DENY_INTERACTIVE.``.
47a5874fdeSMickaël Salaün
48a5874fdeSMickaël SalaünThe main purpose of this flag is to improve the security and consistency of an
49a5874fdeSMickaël Salaünexecution environment to ensure that direct file execution (e.g.
50a5874fdeSMickaël Salaün``./script.sh``) and indirect file execution (e.g. ``sh script.sh``) lead to
51a5874fdeSMickaël Salaünthe same result.  For instance, this can be used to check if a file is
52a5874fdeSMickaël Salaüntrustworthy according to the caller's environment.
53a5874fdeSMickaël Salaün
54a5874fdeSMickaël SalaünIn a secure environment, libraries and any executable dependencies should also
55a5874fdeSMickaël Salaünbe checked.  For instance, dynamic linking should make sure that all libraries
56a5874fdeSMickaël Salaünare allowed for execution to avoid trivial bypass (e.g. using ``LD_PRELOAD``).
57a5874fdeSMickaël SalaünFor such secure execution environment to make sense, only trusted code should
58a5874fdeSMickaël Salaünbe executable, which also requires integrity guarantees.
59a5874fdeSMickaël Salaün
60a5874fdeSMickaël SalaünTo avoid race conditions leading to time-of-check to time-of-use issues,
61a5874fdeSMickaël Salaün``AT_EXECVE_CHECK`` should be used with ``AT_EMPTY_PATH`` to check against a
62a5874fdeSMickaël Salaünfile descriptor instead of a path.
63*a0623b2aSMickaël Salaün
64*a0623b2aSMickaël SalaünSECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE
65*a0623b2aSMickaël Salaün==========================================================
66*a0623b2aSMickaël Salaün
67*a0623b2aSMickaël SalaünWhen ``SECBIT_EXEC_RESTRICT_FILE`` is set, a process should only interpret or
68*a0623b2aSMickaël Salaünexecute a file if a call to :manpage:`execveat(2)` with the related file
69*a0623b2aSMickaël Salaündescriptor and the ``AT_EXECVE_CHECK`` flag succeed.
70*a0623b2aSMickaël Salaün
71*a0623b2aSMickaël SalaünThis secure bit may be set by user session managers, service managers,
72*a0623b2aSMickaël Salaüncontainer runtimes, sandboxer tools...  Except for test environments, the
73*a0623b2aSMickaël Salaünrelated ``SECBIT_EXEC_RESTRICT_FILE_LOCKED`` bit should also be set.
74*a0623b2aSMickaël Salaün
75*a0623b2aSMickaël SalaünPrograms should only enforce consistent restrictions according to the
76*a0623b2aSMickaël Salaünsecurebits but without relying on any other user-controlled configuration.
77*a0623b2aSMickaël SalaünIndeed, the use case for these securebits is to only trust executable code
78*a0623b2aSMickaël Salaünvetted by the system configuration (through the kernel), so we should be
79*a0623b2aSMickaël Salaüncareful to not let untrusted users control this configuration.
80*a0623b2aSMickaël Salaün
81*a0623b2aSMickaël SalaünHowever, script interpreters may still use user configuration such as
82*a0623b2aSMickaël Salaünenvironment variables as long as it is not a way to disable the securebits
83*a0623b2aSMickaël Salaünchecks.  For instance, the ``PATH`` and ``LD_PRELOAD`` variables can be set by
84*a0623b2aSMickaël Salaüna script's caller.  Changing these variables may lead to unintended code
85*a0623b2aSMickaël Salaünexecutions, but only from vetted executable programs, which is OK.  For this to
86*a0623b2aSMickaël Salaünmake sense, the system should provide a consistent security policy to avoid
87*a0623b2aSMickaël Salaünarbitrary code execution e.g., by enforcing a write xor execute policy.
88*a0623b2aSMickaël Salaün
89*a0623b2aSMickaël SalaünWhen ``SECBIT_EXEC_DENY_INTERACTIVE`` is set, a process should never interpret
90*a0623b2aSMickaël Salaüninteractive user commands (e.g. scripts).  However, if such commands are passed
91*a0623b2aSMickaël Salaünthrough a file descriptor (e.g. stdin), its content should be interpreted if a
92*a0623b2aSMickaël Salaüncall to :manpage:`execveat(2)` with the related file descriptor and the
93*a0623b2aSMickaël Salaün``AT_EXECVE_CHECK`` flag succeed.
94*a0623b2aSMickaël Salaün
95*a0623b2aSMickaël SalaünFor instance, script interpreters called with a script snippet as argument
96*a0623b2aSMickaël Salaünshould always deny such execution if ``SECBIT_EXEC_DENY_INTERACTIVE`` is set.
97*a0623b2aSMickaël Salaün
98*a0623b2aSMickaël SalaünThis secure bit may be set by user session managers, service managers,
99*a0623b2aSMickaël Salaüncontainer runtimes, sandboxer tools...  Except for test environments, the
100*a0623b2aSMickaël Salaünrelated ``SECBIT_EXEC_DENY_INTERACTIVE_LOCKED`` bit should also be set.
101*a0623b2aSMickaël Salaün
102*a0623b2aSMickaël SalaünHere is the expected behavior for a script interpreter according to combination
103*a0623b2aSMickaël Salaünof any exec securebits:
104*a0623b2aSMickaël Salaün
105*a0623b2aSMickaël Salaün1. ``SECBIT_EXEC_RESTRICT_FILE=0`` and ``SECBIT_EXEC_DENY_INTERACTIVE=0``
106*a0623b2aSMickaël Salaün
107*a0623b2aSMickaël Salaün   Always interpret scripts, and allow arbitrary user commands (default).
108*a0623b2aSMickaël Salaün
109*a0623b2aSMickaël Salaün   No threat, everyone and everything is trusted, but we can get ahead of
110*a0623b2aSMickaël Salaün   potential issues thanks to the call to :manpage:`execveat(2)` with
111*a0623b2aSMickaël Salaün   ``AT_EXECVE_CHECK`` which should always be performed but ignored by the
112*a0623b2aSMickaël Salaün   script interpreter.  Indeed, this check is still important to enable systems
113*a0623b2aSMickaël Salaün   administrators to verify requests (e.g. with audit) and prepare for
114*a0623b2aSMickaël Salaün   migration to a secure mode.
115*a0623b2aSMickaël Salaün
116*a0623b2aSMickaël Salaün2. ``SECBIT_EXEC_RESTRICT_FILE=1`` and ``SECBIT_EXEC_DENY_INTERACTIVE=0``
117*a0623b2aSMickaël Salaün
118*a0623b2aSMickaël Salaün   Deny script interpretation if they are not executable, but allow
119*a0623b2aSMickaël Salaün   arbitrary user commands.
120*a0623b2aSMickaël Salaün
121*a0623b2aSMickaël Salaün   The threat is (potential) malicious scripts run by trusted (and not fooled)
122*a0623b2aSMickaël Salaün   users.  That can protect against unintended script executions (e.g. ``sh
123*a0623b2aSMickaël Salaün   /tmp/*.sh``).  This makes sense for (semi-restricted) user sessions.
124*a0623b2aSMickaël Salaün
125*a0623b2aSMickaël Salaün3. ``SECBIT_EXEC_RESTRICT_FILE=0`` and ``SECBIT_EXEC_DENY_INTERACTIVE=1``
126*a0623b2aSMickaël Salaün
127*a0623b2aSMickaël Salaün   Always interpret scripts, but deny arbitrary user commands.
128*a0623b2aSMickaël Salaün
129*a0623b2aSMickaël Salaün   This use case may be useful for secure services (i.e. without interactive
130*a0623b2aSMickaël Salaün   user session) where scripts' integrity is verified (e.g.  with IMA/EVM or
131*a0623b2aSMickaël Salaün   dm-verity/IPE) but where access rights might not be ready yet.  Indeed,
132*a0623b2aSMickaël Salaün   arbitrary interactive commands would be much more difficult to check.
133*a0623b2aSMickaël Salaün
134*a0623b2aSMickaël Salaün4. ``SECBIT_EXEC_RESTRICT_FILE=1`` and ``SECBIT_EXEC_DENY_INTERACTIVE=1``
135*a0623b2aSMickaël Salaün
136*a0623b2aSMickaël Salaün   Deny script interpretation if they are not executable, and also deny
137*a0623b2aSMickaël Salaün   any arbitrary user commands.
138*a0623b2aSMickaël Salaün
139*a0623b2aSMickaël Salaün   The threat is malicious scripts run by untrusted users (but trusted code).
140*a0623b2aSMickaël Salaün   This makes sense for system services that may only execute trusted scripts.
141*a0623b2aSMickaël Salaün
142*a0623b2aSMickaël Salaün.. Links
143*a0623b2aSMickaël Salaün.. _samples/check-exec/inc.c:
144*a0623b2aSMickaël Salaün   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/check-exec/inc.c
145