Skip to content

Commit a0623b2

Browse files
l0kodkees
authored andcommitted
security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
The new SECBIT_EXEC_RESTRICT_FILE, SECBIT_EXEC_DENY_INTERACTIVE, and their *_LOCKED counterparts are designed to be set by processes setting up an execution environment, such as a user session, a container, or a security sandbox. Unlike other securebits, these ones can be set by unprivileged processes. Like seccomp filters or Landlock domains, the securebits are inherited across processes. When SECBIT_EXEC_RESTRICT_FILE is set, programs interpreting code should control executable resources according to execveat(2) + AT_EXECVE_CHECK (see previous commit). When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should deny execution of user interactive commands (which excludes executable regular files). Being able to configure each of these securebits enables system administrators or owner of image containers to gradually validate the related changes and to identify potential issues (e.g. with interpreter or audit logs). It should be noted that unlike other security bits, the SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE bits are dedicated to user space willing to restrict itself. Because of that, they only make sense in the context of a trusted environment (e.g. sandbox, container, user session, full system) where the process changing its behavior (according to these bits) and all its parent processes are trusted. Otherwise, any parent process could just execute its own malicious code (interpreting a script or not), or even enforce a seccomp filter to mask these bits. Such a secure environment can be achieved with an appropriate access control (e.g. mount's noexec option, file access rights, LSM policy) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of LD_PRELOAD. Ptrace restrictions according to these securebits would not make sense because of the processes' trust assumption. Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel. See chromeOS's documentation about script execution control and the related threat model: https://www.chromium.org/chromium-os/developer-library/guides/security/noexec-shell-scripts/ Cc: Al Viro <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Christian Brauner <[email protected]> Cc: Kees Cook <[email protected]> Cc: Paul Moore <[email protected]> Reviewed-by: Serge Hallyn <[email protected]> Reviewed-by: Jeff Xu <[email protected]> Tested-by: Jeff Xu <[email protected]> Signed-off-by: Mickaël Salaün <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent a5874fd commit a0623b2

File tree

3 files changed

+153
-7
lines changed

3 files changed

+153
-7
lines changed

Documentation/userspace-api/check_exec.rst

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@
55
Executability check
66
===================
77

8+
The ``AT_EXECVE_CHECK`` :manpage:`execveat(2)` flag, and the
9+
``SECBIT_EXEC_RESTRICT_FILE`` and ``SECBIT_EXEC_DENY_INTERACTIVE`` securebits
10+
are intended for script interpreters and dynamic linkers to enforce a
11+
consistent execution security policy handled by the kernel. See the
12+
`samples/check-exec/inc.c`_ example.
13+
14+
Whether an interpreter should check these securebits or not depends on the
15+
security risk of running malicious scripts with respect to the execution
16+
environment, and whether the kernel can check if a script is trustworthy or
17+
not. For instance, Python scripts running on a server can use arbitrary
18+
syscalls and access arbitrary files. Such interpreters should then be
19+
enlighten to use these securebits and let users define their security policy.
20+
However, a JavaScript engine running in a web browser should already be
21+
sandboxed and then should not be able to harm the user's environment.
22+
23+
Script interpreters or dynamic linkers built for tailored execution environments
24+
(e.g. hardened Linux distributions or hermetic container images) could use
25+
``AT_EXECVE_CHECK`` without checking the related securebits if backward
26+
compatibility is handled by something else (e.g. atomic update ensuring that
27+
all legitimate libraries are allowed to be executed). It is then recommended
28+
for script interpreters and dynamic linkers to check the securebits at run time
29+
by default, but also to provide the ability for custom builds to behave like if
30+
``SECBIT_EXEC_RESTRICT_FILE`` or ``SECBIT_EXEC_DENY_INTERACTIVE`` were always
31+
set to 1 (i.e. always enforce restrictions).
32+
833
AT_EXECVE_CHECK
934
===============
1035

@@ -35,3 +60,85 @@ be executable, which also requires integrity guarantees.
3560
To avoid race conditions leading to time-of-check to time-of-use issues,
3661
``AT_EXECVE_CHECK`` should be used with ``AT_EMPTY_PATH`` to check against a
3762
file descriptor instead of a path.
63+
64+
SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE
65+
==========================================================
66+
67+
When ``SECBIT_EXEC_RESTRICT_FILE`` is set, a process should only interpret or
68+
execute a file if a call to :manpage:`execveat(2)` with the related file
69+
descriptor and the ``AT_EXECVE_CHECK`` flag succeed.
70+
71+
This secure bit may be set by user session managers, service managers,
72+
container runtimes, sandboxer tools... Except for test environments, the
73+
related ``SECBIT_EXEC_RESTRICT_FILE_LOCKED`` bit should also be set.
74+
75+
Programs should only enforce consistent restrictions according to the
76+
securebits but without relying on any other user-controlled configuration.
77+
Indeed, the use case for these securebits is to only trust executable code
78+
vetted by the system configuration (through the kernel), so we should be
79+
careful to not let untrusted users control this configuration.
80+
81+
However, script interpreters may still use user configuration such as
82+
environment variables as long as it is not a way to disable the securebits
83+
checks. For instance, the ``PATH`` and ``LD_PRELOAD`` variables can be set by
84+
a script's caller. Changing these variables may lead to unintended code
85+
executions, but only from vetted executable programs, which is OK. For this to
86+
make sense, the system should provide a consistent security policy to avoid
87+
arbitrary code execution e.g., by enforcing a write xor execute policy.
88+
89+
When ``SECBIT_EXEC_DENY_INTERACTIVE`` is set, a process should never interpret
90+
interactive user commands (e.g. scripts). However, if such commands are passed
91+
through a file descriptor (e.g. stdin), its content should be interpreted if a
92+
call to :manpage:`execveat(2)` with the related file descriptor and the
93+
``AT_EXECVE_CHECK`` flag succeed.
94+
95+
For instance, script interpreters called with a script snippet as argument
96+
should always deny such execution if ``SECBIT_EXEC_DENY_INTERACTIVE`` is set.
97+
98+
This secure bit may be set by user session managers, service managers,
99+
container runtimes, sandboxer tools... Except for test environments, the
100+
related ``SECBIT_EXEC_DENY_INTERACTIVE_LOCKED`` bit should also be set.
101+
102+
Here is the expected behavior for a script interpreter according to combination
103+
of any exec securebits:
104+
105+
1. ``SECBIT_EXEC_RESTRICT_FILE=0`` and ``SECBIT_EXEC_DENY_INTERACTIVE=0``
106+
107+
Always interpret scripts, and allow arbitrary user commands (default).
108+
109+
No threat, everyone and everything is trusted, but we can get ahead of
110+
potential issues thanks to the call to :manpage:`execveat(2)` with
111+
``AT_EXECVE_CHECK`` which should always be performed but ignored by the
112+
script interpreter. Indeed, this check is still important to enable systems
113+
administrators to verify requests (e.g. with audit) and prepare for
114+
migration to a secure mode.
115+
116+
2. ``SECBIT_EXEC_RESTRICT_FILE=1`` and ``SECBIT_EXEC_DENY_INTERACTIVE=0``
117+
118+
Deny script interpretation if they are not executable, but allow
119+
arbitrary user commands.
120+
121+
The threat is (potential) malicious scripts run by trusted (and not fooled)
122+
users. That can protect against unintended script executions (e.g. ``sh
123+
/tmp/*.sh``). This makes sense for (semi-restricted) user sessions.
124+
125+
3. ``SECBIT_EXEC_RESTRICT_FILE=0`` and ``SECBIT_EXEC_DENY_INTERACTIVE=1``
126+
127+
Always interpret scripts, but deny arbitrary user commands.
128+
129+
This use case may be useful for secure services (i.e. without interactive
130+
user session) where scripts' integrity is verified (e.g. with IMA/EVM or
131+
dm-verity/IPE) but where access rights might not be ready yet. Indeed,
132+
arbitrary interactive commands would be much more difficult to check.
133+
134+
4. ``SECBIT_EXEC_RESTRICT_FILE=1`` and ``SECBIT_EXEC_DENY_INTERACTIVE=1``
135+
136+
Deny script interpretation if they are not executable, and also deny
137+
any arbitrary user commands.
138+
139+
The threat is malicious scripts run by untrusted users (but trusted code).
140+
This makes sense for system services that may only execute trusted scripts.
141+
142+
.. Links
143+
.. _samples/check-exec/inc.c:
144+
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/check-exec/inc.c

include/uapi/linux/securebits.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,32 @@
5252
#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \
5353
(issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED))
5454

55+
/* See Documentation/userspace-api/check_exec.rst */
56+
#define SECURE_EXEC_RESTRICT_FILE 8
57+
#define SECURE_EXEC_RESTRICT_FILE_LOCKED 9 /* make bit-8 immutable */
58+
59+
#define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE))
60+
#define SECBIT_EXEC_RESTRICT_FILE_LOCKED \
61+
(issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED))
62+
63+
/* See Documentation/userspace-api/check_exec.rst */
64+
#define SECURE_EXEC_DENY_INTERACTIVE 10
65+
#define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11 /* make bit-10 immutable */
66+
67+
#define SECBIT_EXEC_DENY_INTERACTIVE \
68+
(issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))
69+
#define SECBIT_EXEC_DENY_INTERACTIVE_LOCKED \
70+
(issecure_mask(SECURE_EXEC_DENY_INTERACTIVE_LOCKED))
71+
5572
#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \
5673
issecure_mask(SECURE_NO_SETUID_FIXUP) | \
5774
issecure_mask(SECURE_KEEP_CAPS) | \
58-
issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE))
75+
issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE) | \
76+
issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \
77+
issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))
5978
#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
6079

80+
#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \
81+
issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))
82+
6183
#endif /* _UAPI_LINUX_SECUREBITS_H */

security/commoncap.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,21 +1302,38 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
13021302
& (old->securebits ^ arg2)) /*[1]*/
13031303
|| ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
13041304
|| (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
1305-
|| (cap_capable(current_cred(),
1306-
current_cred()->user_ns,
1307-
CAP_SETPCAP,
1308-
CAP_OPT_NONE) != 0) /*[4]*/
13091305
/*
13101306
* [1] no changing of bits that are locked
13111307
* [2] no unlocking of locks
13121308
* [3] no setting of unsupported bits
1313-
* [4] doing anything requires privilege (go read about
1314-
* the "sendmail capabilities bug")
13151309
*/
13161310
)
13171311
/* cannot change a locked bit */
13181312
return -EPERM;
13191313

1314+
/*
1315+
* Doing anything requires privilege (go read about the
1316+
* "sendmail capabilities bug"), except for unprivileged bits.
1317+
* Indeed, the SECURE_ALL_UNPRIVILEGED bits are not
1318+
* restrictions enforced by the kernel but by user space on
1319+
* itself.
1320+
*/
1321+
if (cap_capable(current_cred(), current_cred()->user_ns,
1322+
CAP_SETPCAP, CAP_OPT_NONE) != 0) {
1323+
const unsigned long unpriv_and_locks =
1324+
SECURE_ALL_UNPRIVILEGED |
1325+
SECURE_ALL_UNPRIVILEGED << 1;
1326+
const unsigned long changed = old->securebits ^ arg2;
1327+
1328+
/* For legacy reason, denies non-change. */
1329+
if (!changed)
1330+
return -EPERM;
1331+
1332+
/* Denies privileged changes. */
1333+
if (changed & ~unpriv_and_locks)
1334+
return -EPERM;
1335+
}
1336+
13201337
new = prepare_creds();
13211338
if (!new)
13221339
return -ENOMEM;

0 commit comments

Comments
 (0)