Script execution control (was O_MAYEXEC)
Subsystems (11)
Mickaël Salaün
maintainerv20 cover Script execution control (was O_MAYEXEC)
Hi,
The ultimate goal of this patch series is to be able to ensure that direct file execution (e.g. ./script.sh) and indirect file execution (e.g. sh script.sh) lead to the same result, especially from a security point of view.
The main changes from the previous version are new securebits with a better semantic, an improved documentation, new user space code examples including an enlighten interpreter and an extensive tests suite. As discussed, I also removed the ptrace checks which doesn't make sense anymore. You'll now find a quick FAQ at the end of this cover letter. The current status is summarized in this article:
https://lwn.net/Articles/982085/
I also gave a talk at LPC last month:
https://lpc.events/event/18/contributions/1692/
And here is a proof of concept for Python (for now, for the previous
version: v19): https://github.com/zooba/spython/pull/12
Overview
--------
This patch series is a new approach of the initial O_MAYEXEC feature, and a revamp of the previous patch series. Taking into account the last reviews [1], we now stick to the kernel semantic for file executability. One major change is the clear split between access check and policy management.
The first patch brings the AT_CHECK flag to execveat(2). The goal is to enable user space to check if a file could be executed (by the kernel). Unlike stat(2) that only checks file permissions, execveat2(2) + AT_CHECK take into account the full context, including mount points (noexec), caller's limits, and all potential LSM extra checks (e.g. argv, envp, credentials).
The second patch brings two new securebits used to set or get a security policy for a set of processes. For this to be meaningful, all executable code needs to be trusted. In practice, this means that (malicious) users can be restricted to only run scripts provided (and trusted) by the system.
[1] https://lore.kernel.org/r/CAHk-=wjPGNLyzeBMWdQu+kUdQLHQugznwY7CvWjmvNW47D5sog@mail.gmail.com
Script execution
----------------
One important thing to keep in mind is that the goal of this patch series is to get the same security restrictions with these commands:
* ./script.py
* python script.py
* python < script.py
* python -m script.py
However, on secure systems, we should be able to forbid these commands because there is no way to reliably identify the origin of the script:
* xargs -a script.py -d '\r' -- python -c
* cat script.py | python
* python
Background
----------
Compared to the previous patch series, there is no more dedicated syscall nor sysctl configuration. This new patch series only add new
flags: one for execveat(2) and four for prctl(2).
This kind of script interpreter restriction may already be used in hardened systems, which may need to fork interpreters and install different versions of the binaries. This mechanism should enable to avoid the use of duplicate binaries (and potential forked source code) for secure interpreters (e.g. secure Python [2]) by making it possible to dynamically enforce restrictions or not.
The ability to control script execution is also required to close a major IMA measurement/appraisal interpreter integrity [3].
This new execveat + AT_CHECK should not be confused with the O_EXEC flag (for open) which is intended for execute-only, which obviously doesn't work for scripts.
I gave a talk about controlling script execution where I explain the previous approaches [4]. The design of the WIP RFC I talked about changed quite a bit since then.
[2] https://github.com/zooba/spython
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lssna2023.sched.com/event/1K7bO
Execution policy
----------------
The "execution" usage means that the content of the file descriptor is trusted according to the system policy to be executed by user space, which means that it interprets the content or (try to) maps it as executable memory.
It is important to note that this can only enable to extend access control managed by the kernel. Hence it enables current access control mechanism to be extended and become a superset of what they can currently control. Indeed, the security policy could also be delegated to an LSM, either a MAC system or an integrity system.
Complementary W^X protections can be brought by SELinux or IPE [5].
Being able to restrict execution also enables to protect the kernel by restricting arbitrary syscalls that an attacker could perform with a crafted binary or certain script languages. It also improves multilevel isolation by reducing the ability of an attacker to use side channels with specific code. These restrictions can natively be enforced for ELF binaries (with the noexec mount option) but require this kernel extension to properly handle scripts (e.g. Python, Perl). To get a consistent execution policy, additional memory restrictions should also be enforced (e.g. thanks to SELinux).
[5] https://lore.kernel.org/lkml/[email protected]/
Prerequisite for security use
-----------------------------
Because scripts might not currently have the executable permission and still run well as is, or because we might want specific users to be allowed to run arbitrary scripts, we also need a configuration mechanism.
According to the threat model, to get a secure execution environment on top of these changes, it might be required to configure and enable existing security mechanisms such as secure boot, restrictive mount points (e.g. with rw AND noexec), correct file permissions (including executable libraries), IMA/EVM, SELinux policy...
The first thing to patch is the libc to check loaded libraries (e.g. see chromeOS changes). The second thing to patch are the script interpreters by checking direct scripts executability and by checking their own libraries (e.g. Python's imported files or argument-passed modules). For instance, the PEP 578 [6] (Runtime Audit Hooks) enables Python 3.8 to be extended with policy enforcement points related to code interpretation, which can be used to align with the PowerShell audit features. Additional Python security improvements (e.g. a limited interpreter without -c, stdin piping of code) are developed [2] [7].
[6] https://www.python.org/dev/peps/pep-0578/
[7] https://lore.kernel.org/lkml/[email protected]/
libc patch
----------
Dynamic linking needs still need to check the libraries the same way interpreters need to check scripts.
chromeOS patches glibc with a fstatvfs check [8] [9]. This enables to check against noexec mount points, which is OK but doesn't fit with execve semantics. Moreover, the kernel is not aware of such check, so all access control checks are not performed (e.g. file permission, LSMs security policies, integrity and authenticity checks), it is not handled with audit, and more importantly this would not work on generic distributions because of the strict requirement and chromeOS-specific assumptions.
[8] https://issuetracker.google.com/issues/40054993
[9] https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay/+/6abfc9e327241a5f684b8b941c899b7ca8b6dbc1/sys-libs/glibc/files/local/glibc-2.37/0007-Deny-LD_PRELOAD-of-files-in-NOEXEC-mount.patch
Examples
--------
The initial idea comes from CLIP OS 4 and the original implementation has been used for more than a decade:
https://github.com/clipos-archive/clipos4_doc
Chrome OS has a similar approach:
https://www.chromium.org/chromium-os/developer-library/guides/security/noexec-shell-scripts/
User space patches can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC
There is more than the O_MAYEXEC changes (which matches this search) e.g., to prevent Python interactive execution. There are patches for Bash, Wine, Java (Icedtea), Busybox's ash, Perl and Python. There are also some related patches which do not directly rely on O_MAYEXEC but which restrict the use of browser plugins and extensions, which may be seen as scripts too:
https://github.com/clipos-archive/clipos4_portage-overlay/tree/master/www-client
Past talks and articles
-----------------------
Closing the script execution control gap at Linux Plumbers Conference 2024: https://lpc.events/event/18/contributions/1692/
An introduction to O_MAYEXEC was given at the Linux Security Summit Europe 2018 - Linux Kernel Security Contributions by ANSSI:
https://www.youtube.com/watch?v=chNjCRtPKQY&t=17m15s
The "write xor execute" principle was explained at Kernel Recipes 2018 - CLIP OS: a defense-in-depth OS:
https://www.youtube.com/watch?v=PjRE0uBtkHU&t=11m14s
LWN articles:
* https://lwn.net/Articles/982085/
* https://lwn.net/Articles/832959/
* https://lwn.net/Articles/820000/
FAQ
---
Q: Why not extend open(2) or openat2(2) with a new flag like O_MAYEXEC?
A: Because it is not flexible enough:
https://lore.kernel.org/r/CAG48ez0NAV5gPgmbDaSjo=zzE=FgnYz=-OHuXwu0Vts=B5gesA@mail.gmail.com
Q: Why not only allowing file descriptor to avoid TOCTOU?
A: Because there are different use cases:
https://lore.kernel.org/r/CAHk-=whb=XuU=LGKnJWaa7LOYQz9VwHs8SLfgLbT5sf2VAbX1A@mail.gmail.com
Q: We can copy a script into a memfd and use it as an executable FD.
Wouldn't that bypass the purpose of this patch series?
A: If an attacker can create a memfd it means that a
malicious/compromised code is already running and it's too late for script execution control to help. This patch series makes it more difficult for an attacker to execute arbitrary code on a trusted system in the first place:
https://lore.kernel.org/all/[email protected]/
Q: What about ROP?
A: See previous answer. If ROP is exploited then the attacker already
controls some code:
https://lore.kernel.org/all/[email protected]/
Q: What about LD_PRELOAD environment variable?
A: The dynamic linker should be enlighten to check if libraries are
allowed to be loaded.
Q: What about The PATH environment variable?
A: All programs allowed to be executed are deemed trusted.
Q: Should we check seccomp filters too?
A: Yes, they should be considered as executable code because they can
change the behavior of processes, similarly to code injection:
https://lore.kernel.org/all/[email protected]/
Q: Could that be used for role transition?
A: That would be risky and difficult to implement correctly:
https://lore.kernel.org/all/[email protected]/
Previous versions
-----------------
v19: https://lore.kernel.org/r/[email protected]
v18: https://lore.kernel.org/r/[email protected]
v17: https://lore.kernel.org/r/[email protected]
v16: https://lore.kernel.org/r/[email protected]
v15: https://lore.kernel.org/r/[email protected]
v14: https://lore.kernel.org/r/[email protected]
v13: https://lore.kernel.org/r/[email protected]
v12: https://lore.kernel.org/r/[email protected]
v11: https://lore.kernel.org/r/[email protected]
v10: https://lore.kernel.org/r/[email protected]
v9: https://lore.kernel.org/r/[email protected]
v8: https://lore.kernel.org/r/[email protected]
v7: https://lore.kernel.org/r/[email protected]
v6: https://lore.kernel.org/r/[email protected]
v5: https://lore.kernel.org/r/[email protected]
v4: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
Regards,
Mickaël Salaün (6):
exec: Add a new AT_CHECK flag to execveat(2)
security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits selftests/exec: Add 32 tests for AT_CHECK and exec securebits selftests/landlock: Add tests for execveat + AT_CHECK samples/check-exec: Add set-exec
samples/check-exec: Add an enlighten "inc" interpreter and 28 tests
fs/exec.c | 18 +- include/linux/binfmts.h | 7 +- include/uapi/linux/fcntl.h | 31 ++ include/uapi/linux/securebits.h | 113 ++++- kernel/audit.h | 1 + kernel/auditsc.c | 1 + samples/Kconfig | 8 + samples/Makefile | 1 + samples/check-exec/.gitignore | 2 + samples/check-exec/Makefile | 15 + samples/check-exec/inc.c | 204 ++++++++ samples/check-exec/run-script-ask.inc | 8 + samples/check-exec/script-ask.inc | 4 + samples/check-exec/script-exec.inc | 3 + samples/check-exec/script-noexec.inc | 3 + samples/check-exec/set-exec.c | 85 ++++ security/commoncap.c | 29 +- security/security.c | 10 + tools/testing/selftests/exec/.gitignore | 4 + tools/testing/selftests/exec/Makefile | 19 +- .../selftests/exec/check-exec-tests.sh | 205 ++++++++ tools/testing/selftests/exec/check-exec.c | 446 ++++++++++++++++++ tools/testing/selftests/exec/config | 2 + tools/testing/selftests/exec/false.c | 5 + .../selftests/kselftest/ktap_helpers.sh | 2 +- tools/testing/selftests/landlock/fs_test.c | 26 + 26 files changed, 1240 insertions(+), 12 deletions(-) create mode 100644 samples/check-exec/.gitignore create mode 100644 samples/check-exec/Makefile create mode 100644 samples/check-exec/inc.c create mode 100755 samples/check-exec/run-script-ask.inc create mode 100755 samples/check-exec/script-ask.inc create mode 100755 samples/check-exec/script-exec.inc create mode 100644 samples/check-exec/script-noexec.inc create mode 100644 samples/check-exec/set-exec.c create mode 100755 tools/testing/selftests/exec/check-exec-tests.sh create mode 100644 tools/testing/selftests/exec/check-exec.c create mode 100644 tools/testing/selftests/exec/config create mode 100644 tools/testing/selftests/exec/false.c
base-commit: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b
Signature · 1 line
2.46.1
Mickaël Salaün
maintainerPATCH v20 1/6 exec: Add a new AT_CHECK flag to execveat(2)
Add a new AT_CHECK flag to execveat(2) to check if a file would be allowed for execution. The main use case is for script interpreters and dynamic linkers to check execution permission according to the kernel's security policy. Another use case is to add context to access logs e.g., which script (instead of interpreter) accessed a file. As any executable code, scripts could also use this check [1].
This is different from faccessat(2) + X_OK which only checks a subset of access rights (i.e. inode permission and mount options for regular files), but not the full context (e.g. all LSM access checks). The main use case for access(2) is for SUID processes to (partially) check access on behalf of their caller. The main use case for execveat(2) + AT_CHECK is to check if a script execution would be allowed, according to all the different restrictions in place. Because the use of AT_CHECK follows the exact kernel semantic as for a real execution, user space gets the same error codes.
An interesting point of using execveat(2) instead of openat2(2) is that it decouples the check from the enforcement. Indeed, the security check can be logged (e.g. with audit) without blocking an execution environment not yet ready to enforce a strict security policy.
LSMs can control or log execution requests with security_bprm_creds_for_exec(). However, to enforce a consistent and complete access control (e.g. on binary's dependencies) LSMs should restrict file executability, or mesure executed files, with security_file_open() by checking file->f_flags & __FMODE_EXEC.
Because AT_CHECK is dedicated to user space interpreters, it doesn't make sense for the kernel to parse the checked files, look for interpreters known to the kernel (e.g. ELF, shebang), and return ENOEXEC if the format is unknown. Because of that, security_bprm_check() is never called when AT_CHECK is used.
It should be noted that script interpreters cannot directly use execveat(2) (without this new AT_CHECK flag) because this could lead to unexpected behaviors e.g., python script.sh could lead to Bash being executed to interpret the script. Unlike the kernel, script interpreters may just interpret the shebang as a simple comment, which should not change for backward compatibility reasons.
Because scripts or libraries files might not currently have the executable permission set, or because we might want specific users to be allowed to run arbitrary scripts, the following patch provides a dynamic configuration mechanism with the SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE securebits.
This is a redesign of the CLIP OS 4's O_MAYEXEC:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than a decade with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC
---
Changes since v19:
* Remove mention of "role transition" as suggested by Andy.
* Highlight the difference between security_bprm_creds_for_exec() and the __FMODE_EXEC check for LSMs (in commit message and LSM's hooks) as discussed with Jeff.
* Improve documentation both in UAPI comments and kernel comments (requested by Kees).
New design since v18:
https://lore.kernel.org/r/[email protected]
--- fs/exec.c | 18 ++++++++++++++++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 31 +++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + security/security.c | 10 ++++++++++ 6 files changed, 65 insertions(+), 3 deletions(-)
fs/exec.c +16 -2
@@ -891,7 +891,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags) .lookup_flags = LOOKUP_FOLLOW, }; -if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH )) !=0 ) +if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH |AT_CHECK )) !=0 )return ERR_PTR (-EINVAL );if (flags &AT_SYMLINK_NOFOLLOW ) open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW ;
@@ -1545,6 +1545,20 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl } bprm->interp = bprm->filename; +/* +* At this point, security_file_open() has already been called (with +* __FMODE_EXEC) and access control checks for AT_CHECK will stop just +* after the security_bprm_creds_for_exec() call in bprm_execve(). +* Indeed, the kernel should not try to parse the content of the file +* with exec_binprm() nor change the calling thread, which means that +* the following security functions will be not called: +* - security_bprm_check() +* - security_bprm_creds_from_file() +* - security_bprm_committing_creds() +* - security_bprm_committed_creds() +*/ + bprm->is_check = !!(flags &AT_CHECK ); + retval =bprm_mm_init (bprm);if (!retval)return bprm;
@@ -1839,7 +1853,7 @@ static int bprm_execve(struct linux_binprm *bprm)/* Set the unchanging part of bprm->cred */ retval =security_bprm_creds_for_exec (bprm); -if (retval) +if (retval || bprm->is_check) goto out; retval =exec_binprm (bprm);
include/linux/binfmts.h +6 -1
@@ -42,7 +42,12 @@ struct linux_binprm {* Set when errors can no longer be returned to the * original userspace. */ - point_of_no_return:1 ; + point_of_no_return:1 , +/* +* Set by user space to check executability according to the +* caller's environment. +*/ + is_check:1;struct file *executable;/* Executable to pass to the interpreter */ struct file *interpreter;struct file *file;
include/uapi/linux/fcntl.h +31 -0
@@ -154,6 +154,37 @@usable with open_by_handle_at(2). */ #define AT_HANDLE_MNT_ID_UNIQUE 0x001/* Return the u64 unique mount ID. */ +/* +* AT_CHECK only performs a check on a regular file and returns 0 if execution +* of this file would be allowed, ignoring the file format and then the related +* interpreter dependencies (e.g. ELF libraries, script's shebang). +* +* Programs should always perform this check to apply kernel-level checks +* against files that are not directly executed by the kernel but passed to a +* user space interpreter instead. All files that contain executable code, +* from the point of view of the interpreter, should be checked. However the +* result of this check should only be enforced according to +* SECBIT_EXEC_RESTRICT_FILE or SECBIT_EXEC_DENY_INTERACTIVE. See securebits.h +* documentation and the samples/check-exec/inc.c example. +* +* The main purpose of this flag is to improve the security and consistency of +* an execution environment to ensure that direct file execution (e.g. +* `./script.sh`) and indirect file execution (e.g. `sh script.sh`) lead to the +* same result. For instance, this can be used to check if a file is +* trustworthy according to the caller's environment. +* +* In a secure environment, libraries and any executable dependencies should +* also be checked. For instance, dynamic linking should make sure that all +* libraries are allowed for execution to avoid trivial bypass (e.g. using +* LD_PRELOAD). For such secure execution environment to make sense, only +* trusted code should be executable, which also requires integrity guarantees. +* +* To avoid race conditions leading to time-of-check to time-of-use issues, +* AT_CHECK should be used with AT_EMPTY_PATH to check against a file +* descriptor instead of a path. +*/ +#define AT_CHECK 0x10000 +#if defined(__KERNEL__)#define AT_GETATTR_NOSEC 0x80000000#endif
kernel/audit.h +1 -0
@@ -197,6 +197,7 @@ struct audit_context {struct open_how openat2;struct {int argc; +bool is_check; } execve;struct {char *name;
kernel/auditsc.c +1 -0
@@ -2662,6 +2662,7 @@ void __audit_bprm(struct linux_binprm *bprm) context->type =AUDIT_EXECVE ; context->execve.argc = bprm->argc; + context->execve.is_check = bprm->is_check; }
security/security.c +10 -0
@@ -1248,6 +1248,12 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)* to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm * contains the linux_binprm structure. * +* If execveat(2) is called with the AT_CHECK flag, bprm->is_check is set. The +* result must be the same as without this flag even if the execution will +* never really happen and @bprm will always be dropped. +* +* This hook must not change current->cred, only @bprm->cred. +* * Return: Returns 0 if the hook is successful and permission is granted. */ int security_bprm_creds_for_exec (struct linux_binprm *bprm)
@@ -3098,6 +3104,10 @@ int security_file_receive(struct file *file)* Save open-time permission checking state for later use upon file_permission, * and recheck access if anything has changed since inode_permission. * +* We can check if a file is opened for execution (e.g. execve(2) call), either +* directly or indirectly (e.g. ELF's ld.so) by checking file->f_flags & +* __FMODE_EXEC . +* * Return: Returns 0 if permission is granted. */ int security_file_open (struct file *file)
Signature · 1 line
2.46.1
Mickaël Salaün
maintainerPATCH v20 2/6 security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Mickaël SalaünThe 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_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/
---
Changes since v19:
* Replace SECBIT_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT with SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE:
https://lore.kernel.org/all/[email protected]/
* Remove the ptrace restrictions, suggested by Andy.
* Improve documentation according to the discussion with Jeff.
New design since v18:
https://lore.kernel.org/r/[email protected]
--- include/uapi/linux/securebits.h | 113 +++++++++++++++++++++++++++++++- security/commoncap.c | 29 ++++++-- 2 files changed, 135 insertions(+), 7 deletions(-)
include/uapi/linux/securebits.h +112 -1
@@ -52,10 +52,121 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* The SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE securebits +* are intended for script interpreters and dynamic linkers to enforce a +* consistent execution security policy handled by the kernel. +* +* Whether an interpreter should check these securebits or not depends on the +* security risk of running malicious scripts with respect to the execution +* environment, and whether the kernel can check if a script is trustworthy or +* not. For instance, Python scripts running on a server can use arbitrary +* syscalls and access arbitrary files. Such interpreters should then be +* enlighten to use these securebits and let users define their security +* policy. However, a JavaScript engine running in a web browser should +* already be sandboxed and then should not be able to harm the user's +* environment. +* +* When SECBIT_EXEC_RESTRICT_FILE is set, a process should only interpret or +* execute a file if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_RESTRICT_FILE_LOCKED bit should also be set. +* +* Programs should only enforce consistent restrictions according to the +* securebits but without relying on any other user-controlled configuration. +* Indeed, the use case for these securebits is to only trust executable code +* vetted by the system configuration (through the kernel), so we should be +* careful to not let untrusted users control this configuration. +* +* However, script interpreters may still use user configuration such as +* environment variables as long as it is not a way to disable the securebits +* checks. For instance, the PATH and LD_PRELOAD variables can be set by a +* script's caller. Changing these variables may lead to unintended code +* executions, but only from vetted executable programs, which is OK. For this +* to make sense, the system should provide a consistent security policy to +* avoid arbitrary code execution e.g., by enforcing a write xor execute +* policy. +* +* SECBIT_EXEC_RESTRICT_FILE is complementary and should also be checked. +*/ +#define SECURE_EXEC_RESTRICT_FILE 8 +#define SECURE_EXEC_RESTRICT_FILE_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE)) +#define SECBIT_EXEC_RESTRICT_FILE_LOCKED \ + (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED)) + +/* +* When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret +* interactive user commands (e.g. scripts). However, if such commands are +* passed through a file descriptor (e.g. stdin), its content should be +* interpreted if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* For instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set. +* +* See the SECBIT_EXEC_RESTRICT_FILE documentation. +* +* Here is the expected behavior for a script interpreter according to +* combination of any exec securebits: +* +* 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default) +* Always interpret scripts, and allow arbitrary user commands. +* => No threat, everyone and everything is trusted, but we can get ahead of +* potential issues thanks to the call to execveat with AT_CHECK which +* should always be performed but ignored by the script interpreter. +* Indeed, this check is still important to enable systems administrators +* to verify requests (e.g. with audit) and prepare for migration to a +* secure mode. +* +* 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0 +* Deny script interpretation if they are not executable, but allow +* arbitrary user commands. +* => The threat is (potential) malicious scripts run by trusted (and not +* fooled) users. That can protect against unintended script executions +* (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user +* sessions. +* +* 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1 +* Always interpret scripts, but deny arbitrary user commands. +* => This use case may be useful for secure services (i.e. without +* interactive user session) where scripts' integrity is verified (e.g. +* with IMA/EVM or dm-verity/IPE) but where access rights might not be +* ready yet. Indeed, arbitrary interactive commands would be much more +* difficult to check. +* +* 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1 +* Deny script interpretation if they are not executable, and also deny +* any arbitrary user commands. +* => The threat is malicious scripts run by untrusted users (but trusted +* code). This makes sense for system services that may only execute +* trusted scripts. +*/ +#define SECURE_EXEC_DENY_INTERACTIVE 10 +#define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11/* make bit-10 immutable */ + +#define SECBIT_EXEC_DENY_INTERACTIVE \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#define SECBIT_EXEC_DENY_INTERACTIVE_LOCKED \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE_LOCKED)) +#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \ issecure_mask(SECURE_NO_SETUID_FIXUP) | \ issecure_mask(SECURE_KEEP_CAPS) | \ - issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE)) + issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE) | \ + issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#endif /* _UAPI_LINUX_SECUREBITS_H */
security/commoncap.c +23 -6
@@ -1302,21 +1302,38 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, & (old->securebits ^ arg2)) /*[1]*/ || ((old->securebits &SECURE_ALL_LOCKS & ~arg2))/*[2]*/ || (arg2 & ~(SECURE_ALL_LOCKS |SECURE_ALL_BITS ))/*[3]*/ - || (cap_capable (current_cred (), -current_cred ()->user_ns, -CAP_SETPCAP , -CAP_OPT_NONE ) !=0 )/*[4]*/ /* * [1] no changing of bits that are locked * [2] no unlocking of locks * [3] no setting of unsupported bits -* [4] doing anything requires privilege (go read about -* the "sendmail capabilities bug") */ ) /* cannot change a locked bit */ return -EPERM; +/* +* Doing anything requires privilege (go read about the +* "sendmail capabilities bug"), except for unprivileged bits. +* Indeed, the SECURE_ALL_UNPRIVILEGED bits are not +* restrictions enforced by the kernel but by user space on +* itself. +*/ +if (cap_capable (current_cred (),current_cred ()->user_ns, +CAP_SETPCAP ,CAP_OPT_NONE ) !=0 ) { +const unsigned long unpriv_and_locks = +SECURE_ALL_UNPRIVILEGED | +SECURE_ALL_UNPRIVILEGED <<1 ; +const unsigned long changed = old->securebits ^ arg2; + +/* For legacy reason, denies non-change. */ +if (!changed) +return -EPERM ; + +/* Denies privileged changes. */ +if (changed & ~unpriv_and_locks) +return -EPERM ; + } + new =prepare_creds ();if (!new)return -ENOMEM ;
Signature · 1 line
2.46.1
Mickaël Salaün
maintainerPATCH v20 4/6 selftests/landlock: Add tests for execveat + AT_CHECK
In reply to Mickaël SalaünExtend layout1.execute with the new AT_CHECK flag. The semantic with AT_CHECK is the same as with a simple execve(2), LANDLOCK_ACCESS_FS_EXECUTE is enforced the same way.
--- tools/testing/selftests/landlock/fs_test.c | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+)
tools/testing/selftests/landlock/fs_test.c +26 -0
@@ -37,6 +37,10 @@#include <linux/fs.h> #include <linux/mount.h> +/* Defines AT_CHECK without type conflicts. */ +#define _ASM_GENERIC_FCNTL_H +#include <linux/fcntl.h> +#include "common.h" #ifndef renameat2
@@ -2008,6 +2012,21 @@ static void test_execute(struct __test_metadata *const _metadata, const int err, }; } +static void test_check_exec (struct __test_metadata *const _metadata, +const int err,const char *const path) +{ +int ret; +char *const argv[] = { (char *)path,NULL }; + + ret =execveat (AT_FDCWD , path, argv,NULL ,AT_EMPTY_PATH |AT_CHECK ); +if (err) { +EXPECT_EQ (-1 , ret); +EXPECT_EQ (errno, err); + }else { +EXPECT_EQ (0 , ret); + } +} + TEST_F_FORK(layout1, execute) {const struct rule rules[] = {
@@ -2025,20 +2044,27 @@ TEST_F_FORK(layout1, execute)copy_binary (_metadata, file1_s1d2);copy_binary (_metadata, file1_s1d3); +/* Checks before file1_s1d1 being denied. */ +test_execute (_metadata,0 , file1_s1d1); +test_check_exec (_metadata,0 , file1_s1d1); +enforce_ruleset (_metadata, ruleset_fd);ASSERT_EQ (0 ,close (ruleset_fd));ASSERT_EQ (0 ,test_open (dir_s1d1,O_RDONLY ));ASSERT_EQ (0 ,test_open (file1_s1d1,O_RDONLY ));test_execute (_metadata,EACCES , file1_s1d1); +test_check_exec (_metadata,EACCES , file1_s1d1);ASSERT_EQ (0 ,test_open (dir_s1d2,O_RDONLY ));ASSERT_EQ (0 ,test_open (file1_s1d2,O_RDONLY ));test_execute (_metadata,0 , file1_s1d2); +test_check_exec (_metadata,0 , file1_s1d2);ASSERT_EQ (0 ,test_open (dir_s1d3,O_RDONLY ));ASSERT_EQ (0 ,test_open (file1_s1d3,O_RDONLY ));test_execute (_metadata,0 , file1_s1d3); +test_check_exec (_metadata,0 , file1_s1d3); }TEST_F_FORK (layout1, link)
Signature · 1 line
2.46.1
Mickaël Salaün
maintainerPATCH v20 5/6 samples/check-exec: Add set-exec
In reply to Mickaël SalaünAdd a simple tool to set SECBIT_EXEC_RESTRICT_FILE or SECBIT_EXEC_DENY_INTERACTIVE before executing a command. This is useful to easily test against enlighten script interpreters.
---
Changes since v19:
* Rename file and directory.
* Update securebits and related arguments.
* Remove useless call to prctl() when securebits are unchanged.
--- samples/Kconfig | 7 +++ samples/Makefile | 1 + samples/check-exec/.gitignore | 1 + samples/check-exec/Makefile | 14 ++++++ samples/check-exec/set-exec.c | 85 +++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 samples/check-exec/.gitignore create mode 100644 samples/check-exec/Makefile create mode 100644 samples/check-exec/set-exec.c
samples/Kconfig +7 -0
@@ -291,6 +291,13 @@ config SAMPLE_CGROUP help Build samples that demonstrate the usage of the cgroup API. +config SAMPLE_CHECK_EXEC + bool "Exec secure bits examples" + depends on CC_CAN_LINK && HEADERS_INSTALL + help + Build a tool to easily configure SECBIT_EXEC_RESTRICT_FILE and + SECBIT_EXEC_DENY_INTERACTIVE. + source "samples/rust/Kconfig" endif # SAMPLES
samples/Makefile +1 -0
@@ -3,6 +3,7 @@ subdir-$(CONFIG_SAMPLE_AUXDISPLAY) += auxdisplay subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs +subdir-$(CONFIG_SAMPLE_CHECK_EXEC) += check-exec subdir-$(CONFIG_SAMPLE_CGROUP) += cgroup obj-$(CONFIG_SAMPLE_CONFIGFS) += configfs/ obj-$(CONFIG_SAMPLE_CONNECTOR) += connector/
samples/check-exec/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/set-exec
samples/check-exec/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +# SPDX-License-Identifier: BSD-3-Clause + +userprogs-always-y := \ + set-exec + +userccflags += -I usr/include + +.PHONY: all clean + +all: + $(MAKE) -C ../.. samples/check-exec/ + +clean: + $(MAKE) -C ../.. M=samples/check-exec/ clean
samples/check-exec/set-exec.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
2.46.1
Mickaël Salaün
maintainerPATCH v20 3/6 selftests/exec: Add 32 tests for AT_CHECK and exec securebits
In reply to Mickaël SalaünTest that checks performed by execveat(..., AT_CHECK) are consistent with noexec mount points and file execute permissions.
Test that SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE are inherited by child processes and that they can be pinned with the appropriate SECBIT_EXEC_RESTRICT_FILE_LOCKED and SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bits.
---
Changes since v19:
* Rename securebits.
* Rename test file.
Changes since v18:
* Rewrite tests with the new design: execveat/AT_CHECK and securebits.
* Simplify the capability dropping and improve it with the NOROOT securebits.
* Replace most ASSERT with EXPECT.
* Fix NULL execve's argv to avoid kernel warning.
* Move tests to exec/
* Build a "false" static binary to test full execution path.
Changes since v14:
* Add Reviewed-by Kees Cook.
Changes since v13:
* Move -I to CFLAGS (suggested by Kees Cook).
* Update sysctl name.
Changes since v12:
* Fix Makefile's license.
Changes since v10:
* Update selftest Makefile.
Changes since v9:
* Rename the syscall and the sysctl.
* Update tests for enum trusted_for_usage
Changes since v8:
* Update with the dedicated syscall introspect_access(2) and the renamed fs.introspection_policy sysctl.
* Remove check symlink which can't be use as is anymore.
* Use socketpair(2) to test UNIX socket.
Changes since v7:
* Update tests with faccessat2/AT_INTERPRETED, including new ones to check that setting R_OK or W_OK returns EINVAL.
* Add tests for memfd, pipefs and nsfs.
* Rename and move back tests to a standalone directory.
Changes since v6:
* Add full combination tests for all file types, including block devices, character devices, fifos, sockets and symlinks.
* Properly save and restore initial sysctl value for all tests.
Changes since v5:
* Refactor with FIXTURE_VARIANT, which make the tests much more easy to read and maintain.
* Save and restore initial sysctl value (suggested by Kees Cook).
* Test with a sysctl value of 0.
* Check errno in sysctl_access_write test.
* Update tests for the CAP_SYS_ADMIN switch.
* Update tests to check -EISDIR (replacing -EACCES).
* Replace FIXTURE_DATA() with FIXTURE() (spotted by Kees Cook).
* Use global const strings.
Changes since v3:
* Replace RESOLVE_MAYEXEC with O_MAYEXEC.
* Add tests to check that O_MAYEXEC is ignored by open(2) and openat(2).
Changes since v2:
* Move tests from exec/ to openat2/ .
* Replace O_MAYEXEC with RESOLVE_MAYEXEC from openat2(2).
* Cleanup tests.
Changes since v1:
* Move tests from yama/ to exec/ .
* Fix _GNU_SOURCE in kselftest_harness.h .
* Add a new test sysctl_access_write to check if CAP_MAC_ADMIN is taken into account.
* Test directory execution which is always forbidden since commit 73601ea5b7b1 ("fs/open.c: allow opening only regular files during execve()"), and also check that even the root user can not bypass file execution checks.
* Make sure delete_workspace() always as enough right to succeed.
* Cosmetic cleanup.
--- tools/testing/selftests/exec/.gitignore | 2 + tools/testing/selftests/exec/Makefile | 7 + tools/testing/selftests/exec/check-exec.c | 446 ++++++++++++++++++++++ tools/testing/selftests/exec/config | 2 + tools/testing/selftests/exec/false.c | 5 + 5 files changed, 462 insertions(+) create mode 100644 tools/testing/selftests/exec/check-exec.c create mode 100644 tools/testing/selftests/exec/config create mode 100644 tools/testing/selftests/exec/false.c
tools/testing/selftests/exec/.gitignore +2 -0
@@ -9,6 +9,8 @@ execveat.ephemeral execveat.denatured non-regular null-argv +/check-exec +/false /load_address.* !load_address.c /recursion-depth
tools/testing/selftests/exec/Makefile +7 -0
@@ -1,6 +1,9 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS = -Wall CFLAGS += -Wno-nonnull +CFLAGS += $(KHDR_INCLUDES) + +LDLIBS += -lcap ALIGNS := 0x1000 0x200000 0x1000000 ALIGN_PIES := $(patsubst %,load_address.%,$(ALIGNS))
@@ -9,12 +12,14 @@ ALIGNMENT_TESTS := $(ALIGN_PIES) $(ALIGN_STATIC_PIES) TEST_PROGS := binfmt_script.py TEST_GEN_PROGS := execveat non-regular $(ALIGNMENT_TESTS) +TEST_GEN_PROGS_EXTENDED := false TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir # Makefile is a run-time dependency, since it's accessed by the execveat test TEST_FILES := Makefile TEST_GEN_PROGS += recursion-depth TEST_GEN_PROGS += null-argv +TEST_GEN_PROGS += check-exec EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx* \ $(OUTPUT)/S_I*.test
@@ -38,3 +43,5 @@ $(OUTPUT)/load_address.0x%: load_address.c $(OUTPUT)/load_address.static.0x%: load_address.c $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \ -fPIE -static-pie $< -o $@ +$(OUTPUT)/false: false.c + $(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
tools/testing/selftests/exec/check-exec.c new file +446 -0
446 lines not on this page · raw message
tools/testing/selftests/exec/config new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_BLK_DEV=y +CONFIG_BLK_DEV_LOOP=y
tools/testing/selftests/exec/false.c new file +5 -0
@@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 +int main (void ) +{ +return 1 ; +}
Signature · 1 line
2.46.1
Mickaël Salaün
maintainerPATCH v20 6/6 samples/check-exec: Add an enlighten "inc" interpreter and 28 tests
In reply to Mickaël SalaünAdd a very simple script interpreter called "inc" that can evaluate two different commands (one per line):
- "?" to initialize a counter from user's input;
- "+" to increment the counter (which is set to 0 by default).
It is enlighten to only interpret executable files according to AT_CHECK and the related securebits:
# Executing a script with RESTRICT_FILE is only allowed if the script
# is exectuable: ./set-exec -f -- ./inc script-exec.inc # Allowed ./set-exec -f -- ./inc script-noexec.inc # Denied
# Executing stdin with DENY_INTERACTIVE is only allowed if stdin is an
# executable regular file: ./set-exec -i -- ./inc -i < script-exec.inc # Allowed ./set-exec -i -- ./inc -i < script-noexec.inc # Denied
# However, a pipe is not executable and it is then denied:
cat script-noexec.inc | ./set-exec -i -- ./inc -i # Denied
# Executing raw data (e.g. command argument) with DENY_INTERACTIVE is
# always denied.
./set-exec -i -- ./inc -c "+" # Denied
./inc -c "$(<script-ask.inc)" # Allowed
# To directly execute a script, we can update $PATH (used by `env`):
PATH="${PATH}:." ./script-exec.inc# To execute several commands passed as argument:
Add a complete test suite to check the script interpreter against all possible execution cases:
make TARGETS=exec kselftest-install ./tools/testing/selftests/kselftest_install/run_kselftest.sh
Fix ktap_helpers.sh to gracefully ignore optional argument.
---
Changes since v19:
* New patch.
--- samples/Kconfig | 3 +- samples/check-exec/.gitignore | 1 + samples/check-exec/Makefile | 1 + samples/check-exec/inc.c | 204 +++++++++++++++++ samples/check-exec/run-script-ask.inc | 8 + samples/check-exec/script-ask.inc | 4 + samples/check-exec/script-exec.inc | 3 + samples/check-exec/script-noexec.inc | 3 + tools/testing/selftests/exec/.gitignore | 2 + tools/testing/selftests/exec/Makefile | 14 +- .../selftests/exec/check-exec-tests.sh | 205 ++++++++++++++++++ .../selftests/kselftest/ktap_helpers.sh | 2 +- 12 files changed, 446 insertions(+), 4 deletions(-) create mode 100644 samples/check-exec/inc.c create mode 100755 samples/check-exec/run-script-ask.inc create mode 100755 samples/check-exec/script-ask.inc create mode 100755 samples/check-exec/script-exec.inc create mode 100644 samples/check-exec/script-noexec.inc create mode 100755 tools/testing/selftests/exec/check-exec-tests.sh
samples/Kconfig +2 -1
@@ -296,7 +296,8 @@ config SAMPLE_CHECK_EXEC depends on CC_CAN_LINK && HEADERS_INSTALL help Build a tool to easily configure SECBIT_EXEC_RESTRICT_FILE and - SECBIT_EXEC_DENY_INTERACTIVE. + SECBIT_EXEC_DENY_INTERACTIVE, and a simple script interpreter to + demonstrate how they should be used with execveat(2) + AT_CHECK. source "samples/rust/Kconfig"
samples/check-exec/.gitignore +1 -0
@@ -1 +1,2 @@ +/inc /set-exec
samples/check-exec/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause userprogs-always-y := \ + inc \ set-exec userccflags += -I usr/include
samples/check-exec/inc.c new file +204 -0
204 lines not on this page · raw message
samples/check-exec/run-script-ask.inc new file +8 -0
@@ -0,0 +1,8 @@ +#!/usr/bin/env sh + +DIR="$(dirname -- "$0")" + +PATH="${PATH}:${DIR}" + +set -x +"${DIR}/script-ask.inc"
samples/check-exec/script-ask.inc new file +4 -0
@@ -0,0 +1,4 @@ +#!/usr/bin/env inc + +? ++
samples/check-exec/script-exec.inc new file +3 -0
@@ -0,0 +1,3 @@ +#!/usr/bin/env inc + ++
samples/check-exec/script-noexec.inc new file +3 -0
@@ -0,0 +1,3 @@ +#!/usr/bin/env inc + ++
tools/testing/selftests/exec/.gitignore +2 -0
@@ -11,9 +11,11 @@ non-regular null-argv /check-exec /false +/inc /load_address.* !load_address.c /recursion-depth +/set-exec xxxxxxxx* pipe S_I*.test
tools/testing/selftests/exec/Makefile +12 -2
@@ -10,9 +10,9 @@ ALIGN_PIES := $(patsubst %,load_address.%,$(ALIGNS)) ALIGN_STATIC_PIES := $(patsubst %,load_address.static.%,$(ALIGNS)) ALIGNMENT_TESTS := $(ALIGN_PIES) $(ALIGN_STATIC_PIES) -TEST_PROGS := binfmt_script.py +TEST_PROGS := binfmt_script.py check-exec-tests.sh TEST_GEN_PROGS := execveat non-regular $(ALIGNMENT_TESTS) -TEST_GEN_PROGS_EXTENDED := false +TEST_GEN_PROGS_EXTENDED := false inc set-exec script-exec.inc script-noexec.inc TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir # Makefile is a run-time dependency, since it's accessed by the execveat test TEST_FILES := Makefile
@@ -26,6 +26,8 @@ EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx* include ../lib.mk +CHECK_EXEC_SAMPLES := $(top_srcdir)/samples/check-exec + $(OUTPUT)/subdir: mkdir -p $@ $(OUTPUT)/script: Makefile
@@ -45,3 +47,11 @@ $(OUTPUT)/load_address.static.0x%: load_address.c -fPIE -static-pie $< -o $@ $(OUTPUT)/false: false.c $(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@ +$(OUTPUT)/inc: $(CHECK_EXEC_SAMPLES)/inc.c + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ +$(OUTPUT)/set-exec: $(CHECK_EXEC_SAMPLES)/set-exec.c + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ +$(OUTPUT)/script-exec.inc: $(CHECK_EXEC_SAMPLES)/script-exec.inc + cp $< $@ +$(OUTPUT)/script-noexec.inc: $(CHECK_EXEC_SAMPLES)/script-noexec.inc + cp $< $@
tools/testing/selftests/exec/check-exec-tests.sh new file +205 -0
205 lines not on this page · raw message
tools/testing/selftests/kselftest/ktap_helpers.sh +1 -1
@@ -40,7 +40,7 @@ ktap_skip_all() { __ktap_test() { result="$1" description="$2" - directive="$3" # optional + directive="${3:-}" # optional local directive_str= [ ! -z "$directive" ] && directive_str="# $directive"
Signature · 1 line
2.46.1
Serge E. Hallyn
maintainerRe: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Mickaël SalaünQuoted 226 lines · Mickaël Salaün
The new
SECBIT_EXEC_RESTRICT_FILE,SECBIT_EXEC_DENY_INTERACTIVE, and their*_LOCKEDcounterparts 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_FILEis set, programs interpreting code should control executable resources according to execveat(2) +AT_CHECK(see previous commit).When
SECBIT_EXEC_DENY_INTERACTIVEis 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_FILEandSECBIT_EXEC_DENY_INTERACTIVEbits 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]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* ReplaceSECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTwithSECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVE:
https://lore.kernel.org/all/[email protected]/
* Remove the ptrace restrictions, suggested by Andy.
* Improve documentation according to the discussion with Jeff.New design since v18:
https://lore.kernel.org/r/[email protected]--- include/uapi/linux/securebits.h | 113 +++++++++++++++++++++++++++++++- security/commoncap.c | 29 ++++++-- 2 files changed, 135 insertions(+), 7 deletions(-)include/uapi/linux/securebits.h +112
-1@@ -52,10 +52,121 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* The SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE securebits +* are intended for script interpreters and dynamic linkers to enforce a +* consistent execution security policy handled by the kernel. +* +* Whether an interpreter should check these securebits or not depends on the +* security risk of running malicious scripts with respect to the execution +* environment, and whether the kernel can check if a script is trustworthy or +* not. For instance, Python scripts running on a server can use arbitrary +* syscalls and access arbitrary files. Such interpreters should then be +* enlighten to use these securebits and let users define their security +* policy. However, a JavaScript engine running in a web browser should +* already be sandboxed and then should not be able to harm the user's +* environment. +* +* When SECBIT_EXEC_RESTRICT_FILE is set, a process should only interpret or +* execute a file if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_RESTRICT_FILE_LOCKED bit should also be set. +* +* Programs should only enforce consistent restrictions according to the +* securebits but without relying on any other user-controlled configuration. +* Indeed, the use case for these securebits is to only trust executable code +* vetted by the system configuration (through the kernel), so we should be +* careful to not let untrusted users control this configuration. +* +* However, script interpreters may still use user configuration such as +* environment variables as long as it is not a way to disable the securebits +* checks. For instance, the PATH and LD_PRELOAD variables can be set by a +* script's caller. Changing these variables may lead to unintended code +* executions, but only from vetted executable programs, which is OK. For this +* to make sense, the system should provide a consistent security policy to +* avoid arbitrary code execution e.g., by enforcing a write xor execute +* policy. +* +* SECBIT_EXEC_RESTRICT_FILE is complementary and should also be checked. +*/ +#define SECURE_EXEC_RESTRICT_FILE 8 +#define SECURE_EXEC_RESTRICT_FILE_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE)) +#define SECBIT_EXEC_RESTRICT_FILE_LOCKED \ + (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED)) + +/* +* When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret +* interactive user commands (e.g. scripts). However, if such commands are +* passed through a file descriptor (e.g. stdin), its content should be +* interpreted if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* For instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set. +* +* See the SECBIT_EXEC_RESTRICT_FILE documentation. +* +* Here is the expected behavior for a script interpreter according to +* combination of any exec securebits: +* +* 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default) +* Always interpret scripts, and allow arbitrary user commands. +* => No threat, everyone and everything is trusted, but we can get ahead of +* potential issues thanks to the call to execveat with AT_CHECK which +* should always be performed but ignored by the script interpreter. +* Indeed, this check is still important to enable systems administrators +* to verify requests (e.g. with audit) and prepare for migration to a +* secure mode. +* +* 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0 +* Deny script interpretation if they are not executable, but allow +* arbitrary user commands. +* => The threat is (potential) malicious scripts run by trusted (and not +* fooled) users. That can protect against unintended script executions +* (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user +* sessions. +* +* 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1 +* Always interpret scripts, but deny arbitrary user commands. +* => This use case may be useful for secure services (i.e. without +* interactive user session) where scripts' integrity is verified (e.g. +* with IMA/EVM or dm-verity/IPE) but where access rights might not be +* ready yet. Indeed, arbitrary interactive commands would be much more +* difficult to check. +* +* 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1 +* Deny script interpretation if they are not executable, and also deny +* any arbitrary user commands. +* => The threat is malicious scripts run by untrusted users (but trusted +* code). This makes sense for system services that may only execute +* trusted scripts. +*/ +#define SECURE_EXEC_DENY_INTERACTIVE 10 +#define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11/* make bit-10 immutable */ + +#define SECBIT_EXEC_DENY_INTERACTIVE \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#define SECBIT_EXEC_DENY_INTERACTIVE_LOCKED \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE_LOCKED)) +#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \ issecure_mask(SECURE_NO_SETUID_FIXUP) | \ issecure_mask(SECURE_KEEP_CAPS) | \ - issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE)) + issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE) | \ + issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#endif /* _UAPI_LINUX_SECUREBITS_H */ security/commoncap.c +7
-6@@ -1302,21 +1302,38 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, & (old->securebits ^ arg2)) /*[1]*/ || ((old->securebits &SECURE_ALL_LOCKS & ~arg2))/*[2]*/ || (arg2 & ~(SECURE_ALL_LOCKS |SECURE_ALL_BITS ))/*[3]*/ - || (cap_capable (current_cred (), -current_cred ()->user_ns, -CAP_SETPCAP , -CAP_OPT_NONE ) !=0 )/*[4]*/ /* * [1] no changing of bits that are locked * [2] no unlocking of locks * [3] no setting of unsupported bits -* [4] doing anything requires privilege (go read about -* the "sendmail capabilities bug") */ ) /* cannot change a locked bit */ return -EPERM; +/* +* Doing anything requires privilege (go read about the +* "sendmail capabilities bug"), except for unprivileged bits. +* Indeed, the SECURE_ALL_UNPRIVILEGED bits are not +* restrictions enforced by the kernel but by user space on +* itself. +*/
security/commoncap.c int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, · line 1330
Quoted 10 lines
security/commoncap.c +10
-0+if (cap_capable (current_cred (),current_cred ()->user_ns, +CAP_SETPCAP ,CAP_OPT_NONE ) !=0 ) { +const unsigned long unpriv_and_locks = +SECURE_ALL_UNPRIVILEGED | +SECURE_ALL_UNPRIVILEGED <<1 ; +const unsigned long changed = old->securebits ^ arg2; + +/* For legacy reason, denies non-change. */ +if (!changed) +return -EPERM ;
This is odd to me. You say for legacy reasons, but, currently, calling PR_SET_SECUREBITS with no changes returns 0. So you may be breaking a lot of programs here, unless I'm mistaken.
Quoted 11 lines
security/commoncap.c +6
-0+ +/* Denies privileged changes. */ +if (changed & ~unpriv_and_locks) +return -EPERM ; + } + new =prepare_creds ();if (!new)return -ENOMEM ;Signature · 1 line
2.46.1
Serge E. Hallyn
maintainerRe: [PATCH v20 1/6] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 1 line · Mickaël Salaün
Add a new
AT_CHECKflag to execveat(2) to check if a file would be
Apologies for both bikeshedding and missing earlier discussions.
But AT_CHECK sounds quite generic. How about AT_EXEC_CHECK, or AT_CHECK_EXEC_CREDS? (I would suggest just AT_CHECK_CREDS since it's for use in execveat(2), but as it's an AT_ flag, it's probably worth being more precise).
Re: [PATCH v20 1/6] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 171 lines · Mickaël Salaün
Add a new
AT_CHECKflag to execveat(2) to check if a file would be allowed for execution. The main use case is for script interpreters and dynamic linkers to check execution permission according to the kernel's security policy. Another use case is to add context to access logs e.g., which script (instead of interpreter) accessed a file. As any executable code, scripts could also use this check [1].This is different from faccessat(2) +
X_OKwhich only checks a subset of access rights (i.e. inode permission and mount options for regular files), but not the full context (e.g. all LSM access checks). The main use case for access(2) is for SUID processes to (partially) check access on behalf of their caller. The main use case for execveat(2) +AT_CHECKis to check if a script execution would be allowed, according to all the different restrictions in place. Because the use ofAT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.An interesting point of using execveat(2) instead of openat2(2) is that it decouples the check from the enforcement. Indeed, the security check can be logged (e.g. with audit) without blocking an execution environment not yet ready to enforce a strict security policy.
LSMs can control or log execution requests with
security_bprm_creds_for_exec(). However, to enforce a consistent and complete access control (e.g. on binary's dependencies) LSMs should restrict file executability, or mesure executed files, withsecurity_file_open()by checkingfile->f_flags&__FMODE_EXEC.Because
AT_CHECKis dedicated to user space interpreters, it doesn't make sense for the kernel to parse the checked files, look for interpreters known to the kernel (e.g. ELF, shebang), and return ENOEXEC if the format is unknown. Because of that,security_bprm_check()is never called whenAT_CHECKis used.It should be noted that script interpreters cannot directly use execveat(2) (without this new
AT_CHECKflag) because this could lead to unexpected behaviors e.g.,python script.shcould lead to Bash being executed to interpret the script. Unlike the kernel, script interpreters may just interpret the shebang as a simple comment, which should not change for backward compatibility reasons.Because scripts or libraries files might not currently have the executable permission set, or because we might want specific users to be allowed to run arbitrary scripts, the following patch provides a dynamic configuration mechanism with the
SECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVEsecurebits.This is a redesign of the CLIP OS 4's
O_MAYEXEC:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than a decade with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECCc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* Remove mention of "role transition" as suggested by Andy.
* Highlight the difference betweensecurity_bprm_creds_for_exec()and the__FMODE_EXECcheck for LSMs (in commit message and LSM's hooks) as discussed with Jeff.
* Improve documentation both in UAPI comments and kernel comments (requested by Kees).New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 18 ++++++++++++++++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 31 +++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + security/security.c | 10 ++++++++++ 6 files changed, 65 insertions(+), 3 deletions(-)fs/exec.c +16
-2@@ -891,7 +891,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags) .lookup_flags = LOOKUP_FOLLOW, }; -if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH )) !=0 ) +if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH |AT_CHECK )) !=0 )return ERR_PTR (-EINVAL );if (flags &AT_SYMLINK_NOFOLLOW ) open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW ;@@ -1545,6 +1545,20 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl } bprm->interp = bprm->filename; +/* +* At this point, security_file_open() has already been called (with +* __FMODE_EXEC) and access control checks for AT_CHECK will stop just +* after the security_bprm_creds_for_exec() call in bprm_execve(). +* Indeed, the kernel should not try to parse the content of the file +* with exec_binprm() nor change the calling thread, which means that +* the following security functions will be not called: +* - security_bprm_check() +* - security_bprm_creds_from_file() +* - security_bprm_committing_creds() +* - security_bprm_committed_creds() +*/ + bprm->is_check = !!(flags &AT_CHECK ); + retval =bprm_mm_init (bprm);if (!retval)return bprm;@@ -1839,7 +1853,7 @@ static int bprm_execve(struct linux_binprm *bprm)/* Set the unchanging part of bprm->cred */ retval =security_bprm_creds_for_exec (bprm); -if (retval) +if (retval || bprm->is_check) goto out; retval =exec_binprm (bprm);include/linux/binfmts.h +6
-1@@ -42,7 +42,12 @@ struct linux_binprm {* Set when errors can no longer be returned to the * original userspace. */ - point_of_no_return:1 ; + point_of_no_return:1 , +/* +* Set by user space to check executability according to the +* caller's environment. +*/ + is_check:1;struct file *executable;/* Executable to pass to the interpreter */ struct file *interpreter;struct file *file;include/uapi/linux/fcntl.h +19
-0@@ -154,6 +154,37 @@usable with open_by_handle_at(2). */ #define AT_HANDLE_MNT_ID_UNIQUE 0x001/* Return the u64 unique mount ID. */ +/* +* AT_CHECK only performs a check on a regular file and returns 0 if execution +* of this file would be allowed, ignoring the file format and then the related +* interpreter dependencies (e.g. ELF libraries, script's shebang). +* +* Programs should always perform this check to apply kernel-level checks +* against files that are not directly executed by the kernel but passed to a +* user space interpreter instead. All files that contain executable code, +* from the point of view of the interpreter, should be checked. However the +* result of this check should only be enforced according to +* SECBIT_EXEC_RESTRICT_FILE or SECBIT_EXEC_DENY_INTERACTIVE. See securebits.h +* documentation and the samples/check-exec/inc.c example. +* +* The main purpose of this flag is to improve the security and consistency of +* an execution environment to ensure that direct file execution (e.g. +* `./script.sh`) and indirect file execution (e.g. `sh script.sh`) lead to the +* same result. For instance, this can be used to check if a file is +* trustworthy according to the caller's environment. +*
include/uapi/linux/fcntl.h line 185
Quoted 10 lines
include/uapi/linux/fcntl.h +10
-0+* In a secure environment, libraries and any executable dependencies should +* also be checked. For instance, dynamic linking should make sure that all +* libraries are allowed for execution to avoid trivial bypass (e.g. using +* LD_PRELOAD). For such secure execution environment to make sense, only +* trusted code should be executable, which also requires integrity guarantees. +* +* To avoid race conditions leading to time-of-check to time-of-use issues, +* AT_CHECK should be used with AT_EMPTY_PATH to check against a file +* descriptor instead of a path. +*/
If you ask me, the very elaborate comment above belongs to execveat(2) man page and is way too verbose for a uapi header.
include/uapi/linux/fcntl.h line 186
Quoted 1 line
include/uapi/linux/fcntl.h +1
-0+#define AT_CHECK 0x10000
Please see the comment "Per-syscall flags for the *at(2) family of syscalls." above. If this is a per-syscall flag please use one of the per-syscall flags, e.g.:
/* Flags for execveat2(2) */
#define AT_EXECVE_CHECK 0x0001 /* Only perform a check if
execution would be allowed */
Thanks,
Amir.
Mickaël Salaün
maintainerRe: [PATCH v20 1/6] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 177 lines · Amir Goldstein
Quoted 176 lines · Mickaël Salaün
Add a new
AT_CHECKflag to execveat(2) to check if a file would be allowed for execution. The main use case is for script interpreters and dynamic linkers to check execution permission according to the kernel's security policy. Another use case is to add context to access logs e.g., which script (instead of interpreter) accessed a file. As any executable code, scripts could also use this check [1].This is different from faccessat(2) +
X_OKwhich only checks a subset of access rights (i.e. inode permission and mount options for regular files), but not the full context (e.g. all LSM access checks). The main use case for access(2) is for SUID processes to (partially) check access on behalf of their caller. The main use case for execveat(2) +AT_CHECKis to check if a script execution would be allowed, according to all the different restrictions in place. Because the use ofAT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.An interesting point of using execveat(2) instead of openat2(2) is that it decouples the check from the enforcement. Indeed, the security check can be logged (e.g. with audit) without blocking an execution environment not yet ready to enforce a strict security policy.
LSMs can control or log execution requests with
security_bprm_creds_for_exec(). However, to enforce a consistent and complete access control (e.g. on binary's dependencies) LSMs should restrict file executability, or mesure executed files, withsecurity_file_open()by checkingfile->f_flags&__FMODE_EXEC.Because
AT_CHECKis dedicated to user space interpreters, it doesn't make sense for the kernel to parse the checked files, look for interpreters known to the kernel (e.g. ELF, shebang), and return ENOEXEC if the format is unknown. Because of that,security_bprm_check()is never called whenAT_CHECKis used.It should be noted that script interpreters cannot directly use execveat(2) (without this new
AT_CHECKflag) because this could lead to unexpected behaviors e.g.,python script.shcould lead to Bash being executed to interpret the script. Unlike the kernel, script interpreters may just interpret the shebang as a simple comment, which should not change for backward compatibility reasons.Because scripts or libraries files might not currently have the executable permission set, or because we might want specific users to be allowed to run arbitrary scripts, the following patch provides a dynamic configuration mechanism with the
SECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVEsecurebits.This is a redesign of the CLIP OS 4's
O_MAYEXEC:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than a decade with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECCc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* Remove mention of "role transition" as suggested by Andy.
* Highlight the difference betweensecurity_bprm_creds_for_exec()and the__FMODE_EXECcheck for LSMs (in commit message and LSM's hooks) as discussed with Jeff.
* Improve documentation both in UAPI comments and kernel comments (requested by Kees).New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 18 ++++++++++++++++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 31 +++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + security/security.c | 10 ++++++++++ 6 files changed, 65 insertions(+), 3 deletions(-)fs/exec.c +16
-2@@ -891,7 +891,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags) .lookup_flags = LOOKUP_FOLLOW, }; -if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH )) !=0 ) +if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH |AT_CHECK )) !=0 )return ERR_PTR (-EINVAL );if (flags &AT_SYMLINK_NOFOLLOW ) open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW ;@@ -1545,6 +1545,20 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl } bprm->interp = bprm->filename; +/* +* At this point, security_file_open() has already been called (with +* __FMODE_EXEC) and access control checks for AT_CHECK will stop just +* after the security_bprm_creds_for_exec() call in bprm_execve(). +* Indeed, the kernel should not try to parse the content of the file +* with exec_binprm() nor change the calling thread, which means that +* the following security functions will be not called: +* - security_bprm_check() +* - security_bprm_creds_from_file() +* - security_bprm_committing_creds() +* - security_bprm_committed_creds() +*/ + bprm->is_check = !!(flags &AT_CHECK ); + retval =bprm_mm_init (bprm);if (!retval)return bprm;@@ -1839,7 +1853,7 @@ static int bprm_execve(struct linux_binprm *bprm)/* Set the unchanging part of bprm->cred */ retval =security_bprm_creds_for_exec (bprm); -if (retval) +if (retval || bprm->is_check) goto out; retval =exec_binprm (bprm);include/linux/binfmts.h +6
-1@@ -42,7 +42,12 @@ struct linux_binprm {* Set when errors can no longer be returned to the * original userspace. */ - point_of_no_return:1 ; + point_of_no_return:1 , +/* +* Set by user space to check executability according to the +* caller's environment. +*/ + is_check:1;struct file *executable;/* Executable to pass to the interpreter */ struct file *interpreter;struct file *file;include/uapi/linux/fcntl.h +24
-0@@ -154,6 +154,37 @@usable with open_by_handle_at(2). */ #define AT_HANDLE_MNT_ID_UNIQUE 0x001/* Return the u64 unique mount ID. */ +/* +* AT_CHECK only performs a check on a regular file and returns 0 if execution +* of this file would be allowed, ignoring the file format and then the related +* interpreter dependencies (e.g. ELF libraries, script's shebang). +* +* Programs should always perform this check to apply kernel-level checks +* against files that are not directly executed by the kernel but passed to a +* user space interpreter instead. All files that contain executable code, +* from the point of view of the interpreter, should be checked. However the +* result of this check should only be enforced according to +* SECBIT_EXEC_RESTRICT_FILE or SECBIT_EXEC_DENY_INTERACTIVE. See securebits.h +* documentation and the samples/check-exec/inc.c example. +* +* The main purpose of this flag is to improve the security and consistency of +* an execution environment to ensure that direct file execution (e.g. +* `./script.sh`) and indirect file execution (e.g. `sh script.sh`) lead to the +* same result. For instance, this can be used to check if a file is +* trustworthy according to the caller's environment. +* +* In a secure environment, libraries and any executable dependencies should +* also be checked. For instance, dynamic linking should make sure that all +* libraries are allowed for execution to avoid trivial bypass (e.g. using +* LD_PRELOAD). For such secure execution environment to make sense, only +* trusted code should be executable, which also requires integrity guarantees.
Quoted 8 lines
include/uapi/linux/fcntl.h line 185
Quoted 5 lines
include/uapi/linux/fcntl.h +5
-0+* +* To avoid race conditions leading to time-of-check to time-of-use issues, +* AT_CHECK should be used with AT_EMPTY_PATH to check against a file +* descriptor instead of a path. +*/ If you ask me, the very elaborate comment above belongs to execveat(2) man page and is way too verbose for a uapi header.
OK, but since this new flags raised a lot of questions, I guess a dedicated Documentation/userspace-api/check-exec.rst file with thit AT*_CHECK and the related securebits would be useful instead of the related inlined documentation.
Quoted 10 lines
include/uapi/linux/fcntl.h line 186
Quoted 2 lines
include/uapi/linux/fcntl.h +1
-0+#define AT_CHECK 0x10000Please see the comment "Per-syscall flags for the *at(2) family of syscalls." above. If this is a per-syscall flag please use one of the per-syscall flags, e.g.:
/* Flags for execveat2(2) */
#defineAT_EXECVE_CHECK0x0001/* Only perform a check if
execution would be allowed */
I missed this part, this prefix makes sense, thanks.
Quoted 4 lines
Thanks,
Amir.
Mickaël Salaün
maintainerRe: [PATCH v20 1/6] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Serge E. HallynQuoted 9 lines · Serge E. Hallyn
Quoted 1 line · Mickaël Salaün
Add a new
AT_CHECKflag to execveat(2) to check if a file would beApologies for both bikeshedding and missing earlier discussions.
But
AT_CHECKsounds quite generic. How aboutAT_EXEC_CHECK, orAT_CHECK_EXEC_CREDS? (I would suggest justAT_CHECK_CREDSsince it's for use in execveat(2), but as it's anAT_flag, it's probably worth being more precise).
As Amir pointed out, we need at least to use the AT_EXECVE_CHECK_ prefix, and I agree with the AT_EXECVE_CHECK name because it's about checking the whole execve request, not sepcifically a "creds" part.
Mickaël Salaün
maintainerRe: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Serge E. HallynQuoted 233 lines · Serge E. Hallyn
Quoted 232 lines · Mickaël Salaün
The new
SECBIT_EXEC_RESTRICT_FILE,SECBIT_EXEC_DENY_INTERACTIVE, and their*_LOCKEDcounterparts 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_FILEis set, programs interpreting code should control executable resources according to execveat(2) +AT_CHECK(see previous commit).When
SECBIT_EXEC_DENY_INTERACTIVEis 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_FILEandSECBIT_EXEC_DENY_INTERACTIVEbits 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]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* ReplaceSECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTwithSECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVE:
https://lore.kernel.org/all/[email protected]/
* Remove the ptrace restrictions, suggested by Andy.
* Improve documentation according to the discussion with Jeff.New design since v18:
https://lore.kernel.org/r/[email protected]--- include/uapi/linux/securebits.h | 113 +++++++++++++++++++++++++++++++- security/commoncap.c | 29 ++++++-- 2 files changed, 135 insertions(+), 7 deletions(-)include/uapi/linux/securebits.h +112
-1@@ -52,10 +52,121 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* The SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE securebits +* are intended for script interpreters and dynamic linkers to enforce a +* consistent execution security policy handled by the kernel. +* +* Whether an interpreter should check these securebits or not depends on the +* security risk of running malicious scripts with respect to the execution +* environment, and whether the kernel can check if a script is trustworthy or +* not. For instance, Python scripts running on a server can use arbitrary +* syscalls and access arbitrary files. Such interpreters should then be +* enlighten to use these securebits and let users define their security +* policy. However, a JavaScript engine running in a web browser should +* already be sandboxed and then should not be able to harm the user's +* environment. +* +* When SECBIT_EXEC_RESTRICT_FILE is set, a process should only interpret or +* execute a file if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_RESTRICT_FILE_LOCKED bit should also be set. +* +* Programs should only enforce consistent restrictions according to the +* securebits but without relying on any other user-controlled configuration. +* Indeed, the use case for these securebits is to only trust executable code +* vetted by the system configuration (through the kernel), so we should be +* careful to not let untrusted users control this configuration. +* +* However, script interpreters may still use user configuration such as +* environment variables as long as it is not a way to disable the securebits +* checks. For instance, the PATH and LD_PRELOAD variables can be set by a +* script's caller. Changing these variables may lead to unintended code +* executions, but only from vetted executable programs, which is OK. For this +* to make sense, the system should provide a consistent security policy to +* avoid arbitrary code execution e.g., by enforcing a write xor execute +* policy. +* +* SECBIT_EXEC_RESTRICT_FILE is complementary and should also be checked. +*/ +#define SECURE_EXEC_RESTRICT_FILE 8 +#define SECURE_EXEC_RESTRICT_FILE_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE)) +#define SECBIT_EXEC_RESTRICT_FILE_LOCKED \ + (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED)) + +/* +* When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret +* interactive user commands (e.g. scripts). However, if such commands are +* passed through a file descriptor (e.g. stdin), its content should be +* interpreted if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* For instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set. +* +* See the SECBIT_EXEC_RESTRICT_FILE documentation. +* +* Here is the expected behavior for a script interpreter according to +* combination of any exec securebits: +* +* 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default) +* Always interpret scripts, and allow arbitrary user commands. +* => No threat, everyone and everything is trusted, but we can get ahead of +* potential issues thanks to the call to execveat with AT_CHECK which +* should always be performed but ignored by the script interpreter. +* Indeed, this check is still important to enable systems administrators +* to verify requests (e.g. with audit) and prepare for migration to a +* secure mode. +* +* 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0 +* Deny script interpretation if they are not executable, but allow +* arbitrary user commands. +* => The threat is (potential) malicious scripts run by trusted (and not +* fooled) users. That can protect against unintended script executions +* (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user +* sessions. +* +* 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1 +* Always interpret scripts, but deny arbitrary user commands. +* => This use case may be useful for secure services (i.e. without +* interactive user session) where scripts' integrity is verified (e.g. +* with IMA/EVM or dm-verity/IPE) but where access rights might not be +* ready yet. Indeed, arbitrary interactive commands would be much more +* difficult to check. +* +* 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1 +* Deny script interpretation if they are not executable, and also deny +* any arbitrary user commands. +* => The threat is malicious scripts run by untrusted users (but trusted +* code). This makes sense for system services that may only execute +* trusted scripts. +*/ +#define SECURE_EXEC_DENY_INTERACTIVE 10 +#define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11/* make bit-10 immutable */ + +#define SECBIT_EXEC_DENY_INTERACTIVE \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#define SECBIT_EXEC_DENY_INTERACTIVE_LOCKED \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE_LOCKED)) +#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \ issecure_mask(SECURE_NO_SETUID_FIXUP) | \ issecure_mask(SECURE_KEEP_CAPS) | \ - issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE)) + issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE) | \ + issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#endif /* _UAPI_LINUX_SECUREBITS_H */ security/commoncap.c +13
-6@@ -1302,21 +1302,38 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, & (old->securebits ^ arg2)) /*[1]*/ || ((old->securebits &SECURE_ALL_LOCKS & ~arg2))/*[2]*/ || (arg2 & ~(SECURE_ALL_LOCKS |SECURE_ALL_BITS ))/*[3]*/ - || (cap_capable (current_cred (), -current_cred ()->user_ns, -CAP_SETPCAP , -CAP_OPT_NONE ) !=0 )/*[4]*/ /* * [1] no changing of bits that are locked * [2] no unlocking of locks * [3] no setting of unsupported bits -* [4] doing anything requires privilege (go read about -* the "sendmail capabilities bug") */ ) /* cannot change a locked bit */ return -EPERM; +/* +* Doing anything requires privilege (go read about the +* "sendmail capabilities bug"), except for unprivileged bits. +* Indeed, the SECURE_ALL_UNPRIVILEGED bits are not +* restrictions enforced by the kernel but by user space on +* itself. +*/ +if (cap_capable (current_cred (),current_cred ()->user_ns, +CAP_SETPCAP ,CAP_OPT_NONE ) !=0 ) { +const unsigned long unpriv_and_locks = +SECURE_ALL_UNPRIVILEGED | +SECURE_ALL_UNPRIVILEGED <<1 ; +const unsigned long changed = old->securebits ^ arg2;
Quoted 8 lines
security/commoncap.c int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, · line 1330
Quoted 4 lines
security/commoncap.c +4
-0+ +/* For legacy reason, denies non-change. */ +if (!changed) +return -EPERM ;This is odd to me. You say for legacy reasons, but, currently, calling
PR_SET_SECUREBITSwith no changes returns 0. So you may be breaking a lot of programs here, unless I'm mistaken.
When we call PR_SET_SECUREBITS with 0 (and if it was 0 too), it currently goes through the capability check and return -EPERM if the caller doesn't have CAP_SETCAP. This is tested with TEST_F(secbits, legacy) in tools/testing/selftests/exec/check-exec.c (patch 3/6).
Quoted 13 lines
Quoted 12 lines
security/commoncap.c +6
-0+ +/* Denies privileged changes. */ +if (changed & ~unpriv_and_locks) +return -EPERM ; + } + new =prepare_creds ();if (!new)return -ENOMEM ;Signature · 1 line
2.46.1
Re: [PATCH v20 1/6] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 200 lines · Mickaël Salaün
Quoted 177 lines · Amir Goldstein
Quoted 176 lines · Mickaël Salaün
Add a new
AT_CHECKflag to execveat(2) to check if a file would be allowed for execution. The main use case is for script interpreters and dynamic linkers to check execution permission according to the kernel's security policy. Another use case is to add context to access logs e.g., which script (instead of interpreter) accessed a file. As any executable code, scripts could also use this check [1].This is different from faccessat(2) +
X_OKwhich only checks a subset of access rights (i.e. inode permission and mount options for regular files), but not the full context (e.g. all LSM access checks). The main use case for access(2) is for SUID processes to (partially) check access on behalf of their caller. The main use case for execveat(2) +AT_CHECKis to check if a script execution would be allowed, according to all the different restrictions in place. Because the use ofAT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.An interesting point of using execveat(2) instead of openat2(2) is that it decouples the check from the enforcement. Indeed, the security check can be logged (e.g. with audit) without blocking an execution environment not yet ready to enforce a strict security policy.
LSMs can control or log execution requests with
security_bprm_creds_for_exec(). However, to enforce a consistent and complete access control (e.g. on binary's dependencies) LSMs should restrict file executability, or mesure executed files, withsecurity_file_open()by checkingfile->f_flags&__FMODE_EXEC.Because
AT_CHECKis dedicated to user space interpreters, it doesn't make sense for the kernel to parse the checked files, look for interpreters known to the kernel (e.g. ELF, shebang), and return ENOEXEC if the format is unknown. Because of that,security_bprm_check()is never called whenAT_CHECKis used.It should be noted that script interpreters cannot directly use execveat(2) (without this new
AT_CHECKflag) because this could lead to unexpected behaviors e.g.,python script.shcould lead to Bash being executed to interpret the script. Unlike the kernel, script interpreters may just interpret the shebang as a simple comment, which should not change for backward compatibility reasons.Because scripts or libraries files might not currently have the executable permission set, or because we might want specific users to be allowed to run arbitrary scripts, the following patch provides a dynamic configuration mechanism with the
SECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVEsecurebits.This is a redesign of the CLIP OS 4's
O_MAYEXEC:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than a decade with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECCc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* Remove mention of "role transition" as suggested by Andy.
* Highlight the difference betweensecurity_bprm_creds_for_exec()and the__FMODE_EXECcheck for LSMs (in commit message and LSM's hooks) as discussed with Jeff.
* Improve documentation both in UAPI comments and kernel comments (requested by Kees).New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 18 ++++++++++++++++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 31 +++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + security/security.c | 10 ++++++++++ 6 files changed, 65 insertions(+), 3 deletions(-)fs/exec.c +16
-2@@ -891,7 +891,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags) .lookup_flags = LOOKUP_FOLLOW, }; -if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH )) !=0 ) +if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH |AT_CHECK )) !=0 )return ERR_PTR (-EINVAL );if (flags &AT_SYMLINK_NOFOLLOW ) open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW ;@@ -1545,6 +1545,20 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl } bprm->interp = bprm->filename; +/* +* At this point, security_file_open() has already been called (with +* __FMODE_EXEC) and access control checks for AT_CHECK will stop just +* after the security_bprm_creds_for_exec() call in bprm_execve(). +* Indeed, the kernel should not try to parse the content of the file +* with exec_binprm() nor change the calling thread, which means that +* the following security functions will be not called: +* - security_bprm_check() +* - security_bprm_creds_from_file() +* - security_bprm_committing_creds() +* - security_bprm_committed_creds() +*/ + bprm->is_check = !!(flags &AT_CHECK ); + retval =bprm_mm_init (bprm);if (!retval)return bprm;@@ -1839,7 +1853,7 @@ static int bprm_execve(struct linux_binprm *bprm)/* Set the unchanging part of bprm->cred */ retval =security_bprm_creds_for_exec (bprm); -if (retval) +if (retval || bprm->is_check) goto out; retval =exec_binprm (bprm);include/linux/binfmts.h +6
-1@@ -42,7 +42,12 @@ struct linux_binprm {* Set when errors can no longer be returned to the * original userspace. */ - point_of_no_return:1 ; + point_of_no_return:1 , +/* +* Set by user space to check executability according to the +* caller's environment. +*/ + is_check:1;struct file *executable;/* Executable to pass to the interpreter */ struct file *interpreter;struct file *file;include/uapi/linux/fcntl.h +24
-0@@ -154,6 +154,37 @@usable with open_by_handle_at(2). */ #define AT_HANDLE_MNT_ID_UNIQUE 0x001/* Return the u64 unique mount ID. */ +/* +* AT_CHECK only performs a check on a regular file and returns 0 if execution +* of this file would be allowed, ignoring the file format and then the related +* interpreter dependencies (e.g. ELF libraries, script's shebang). +* +* Programs should always perform this check to apply kernel-level checks +* against files that are not directly executed by the kernel but passed to a +* user space interpreter instead. All files that contain executable code, +* from the point of view of the interpreter, should be checked. However the +* result of this check should only be enforced according to +* SECBIT_EXEC_RESTRICT_FILE or SECBIT_EXEC_DENY_INTERACTIVE. See securebits.h +* documentation and the samples/check-exec/inc.c example. +* +* The main purpose of this flag is to improve the security and consistency of +* an execution environment to ensure that direct file execution (e.g. +* `./script.sh`) and indirect file execution (e.g. `sh script.sh`) lead to the +* same result. For instance, this can be used to check if a file is +* trustworthy according to the caller's environment. +* +* In a secure environment, libraries and any executable dependencies should +* also be checked. For instance, dynamic linking should make sure that all +* libraries are allowed for execution to avoid trivial bypass (e.g. using +* LD_PRELOAD). For such secure execution environment to make sense, only +* trusted code should be executable, which also requires integrity guarantees. Quoted 8 lines
include/uapi/linux/fcntl.h line 185
Quoted 5 lines
include/uapi/linux/fcntl.h +5
-0+* +* To avoid race conditions leading to time-of-check to time-of-use issues, +* AT_CHECK should be used with AT_EMPTY_PATH to check against a file +* descriptor instead of a path. +*/ If you ask me, the very elaborate comment above belongs to execveat(2) man page and is way too verbose for a uapi header.
OK, but since this new flags raised a lot of questions, I guess a dedicated
Documentation/userspace-api/check-exec.rstfile with thit AT*_CHECK and the related securebits would be useful instead of the related inlined documentation.Quoted 7 lines
include/uapi/linux/fcntl.h line 186
Quoted 2 lines
include/uapi/linux/fcntl.h +1
-0+#define AT_CHECK 0x10000Please see the comment "Per-syscall flags for the *at(2) family of syscalls." above. If this is a per-syscall flag please use one of the per-syscall flags, e.g.:
Quoted 6 lines
Quoted 3 lines
/* Flags for execveat2(2) */
#defineAT_EXECVE_CHECK0x0001/* Only perform a check if
execution would be allowed */I missed this part, this prefix makes sense, thanks.
Not only the prefix, also the overloaded value.
Thanks,
Amir.
kernel test robot
build botRe: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Mickaël Salaünkernel test robot noticed the following build warnings:
Hi Mickaël,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b]
url: https://github.com/intel-lab-lkp/linux/commits/Micka-l-Sala-n/exec-Add-a-new-AT_CHECK-flag-to-execveat-2/20241012-024801 base: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b patch link: https://lore.kernel.org/r/20241011184422.977903-3-mic%40digikod.net patch subject: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20241015/[email protected]/config) compiler: alpha-linux-gcc (GCC) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241015/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from include/linux/securebits.h:5,
from include/linux/init_task.h:13, from init/init_task.c:2:
>> include/uapi/linux/securebits.h:135:23: warning: "/*" within comment [-Wcomment]
135 | * (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user
|
vim +135 include/uapi/linux/securebits.h
97
98 #define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE))
99 #define SECBIT_EXEC_RESTRICT_FILE_LOCKED \
100 (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED))
101
102 /*
103 * When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret
104 * interactive user commands (e.g. scripts). However, if such commands are
105 * passed through a file descriptor (e.g. stdin), its content should be
106 * interpreted if a call to execveat(2) with the related file descriptor and
107 * the AT_CHECK flag succeed.
108 *
109 * For instance, script interpreters called with a script snippet as argument
110 * should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set.
111 *
112 * This secure bit may be set by user session managers, service managers,
113 * container runtimes, sandboxer tools... Except for test environments, the
114 * related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set.
115 *
116 * See the SECBIT_EXEC_RESTRICT_FILE documentation.
117 *
118 * Here is the expected behavior for a script interpreter according to
119 * combination of any exec securebits:
120 *
121 * 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default)
122 * Always interpret scripts, and allow arbitrary user commands.
123 * => No threat, everyone and everything is trusted, but we can get ahead of
124 * potential issues thanks to the call to execveat with AT_CHECK which
125 * should always be performed but ignored by the script interpreter.
126 * Indeed, this check is still important to enable systems administrators
127 * to verify requests (e.g. with audit) and prepare for migration to a
128 * secure mode.
129 *
130 * 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0
131 * Deny script interpretation if they are not executable, but allow
132 * arbitrary user commands.
133 * => The threat is (potential) malicious scripts run by trusted (and not
134 * fooled) users. That can protect against unintended script executions
> 135 * (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user
136 * sessions.
137 *
138 * 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1
139 * Always interpret scripts, but deny arbitrary user commands.
140 * => This use case may be useful for secure services (i.e. without
141 * interactive user session) where scripts' integrity is verified (e.g.
142 * with IMA/EVM or dm-verity/IPE) but where access rights might not be
143 * ready yet. Indeed, arbitrary interactive commands would be much more
144 * difficult to check.
145 *
146 * 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1
147 * Deny script interpretation if they are not executable, and also deny
148 * any arbitrary user commands.
149 * => The threat is malicious scripts run by untrusted users (but trusted 150 * code). This makes sense for system services that may only execute
151 * trusted scripts.
152 */
153 #define SECURE_EXEC_DENY_INTERACTIVE 10
154 #define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11 /* make bit-10 immutable */
155
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Mickaël Salaünkernel test robot noticed the following build warnings:
Hi Mickaël,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b]
url: https://github.com/intel-lab-lkp/linux/commits/Micka-l-Sala-n/exec-Add-a-new-AT_CHECK-flag-to-execveat-2/20241012-024801 base: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b patch link: https://lore.kernel.org/r/20241011184422.977903-3-mic%40digikod.net patch subject: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20241015/[email protected]/config) compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 70e0a7e7e6a8541bcc46908c592eed561850e416) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241015/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from init/init_task.c:2:
In file included from include/linux/init_task.h:9:
In file included from include/linux/ftrace.h:13:
In file included from include/linux/kallsyms.h:13:
In file included from include/linux/mm.h:2213:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
518 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
In file included from init/init_task.c:2:
In file included from include/linux/init_task.h:13:
In file included from include/linux/securebits.h:5:
>> include/uapi/linux/securebits.h:135:23: warning: '/*' within block comment [-Wcomment] 135 | * (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user
| ^
2 warnings generated.
vim +135 include/uapi/linux/securebits.h
97
98 #define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE))
99 #define SECBIT_EXEC_RESTRICT_FILE_LOCKED \
100 (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED))
101
102 /*
103 * When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret
104 * interactive user commands (e.g. scripts). However, if such commands are
105 * passed through a file descriptor (e.g. stdin), its content should be
106 * interpreted if a call to execveat(2) with the related file descriptor and
107 * the AT_CHECK flag succeed.
108 *
109 * For instance, script interpreters called with a script snippet as argument
110 * should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set.
111 *
112 * This secure bit may be set by user session managers, service managers,
113 * container runtimes, sandboxer tools... Except for test environments, the
114 * related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set.
115 *
116 * See the SECBIT_EXEC_RESTRICT_FILE documentation.
117 *
118 * Here is the expected behavior for a script interpreter according to
119 * combination of any exec securebits:
120 *
121 * 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default)
122 * Always interpret scripts, and allow arbitrary user commands.
123 * => No threat, everyone and everything is trusted, but we can get ahead of
124 * potential issues thanks to the call to execveat with AT_CHECK which
125 * should always be performed but ignored by the script interpreter.
126 * Indeed, this check is still important to enable systems administrators
127 * to verify requests (e.g. with audit) and prepare for migration to a
128 * secure mode.
129 *
130 * 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0
131 * Deny script interpretation if they are not executable, but allow
132 * arbitrary user commands.
133 * => The threat is (potential) malicious scripts run by trusted (and not
134 * fooled) users. That can protect against unintended script executions
> 135 * (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user
136 * sessions.
137 *
138 * 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1
139 * Always interpret scripts, but deny arbitrary user commands.
140 * => This use case may be useful for secure services (i.e. without
141 * interactive user session) where scripts' integrity is verified (e.g.
142 * with IMA/EVM or dm-verity/IPE) but where access rights might not be
143 * ready yet. Indeed, arbitrary interactive commands would be much more
144 * difficult to check.
145 *
146 * 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1
147 * Deny script interpretation if they are not executable, and also deny
148 * any arbitrary user commands.
149 * => The threat is malicious scripts run by untrusted users (but trusted 150 * code). This makes sense for system services that may only execute
151 * trusted scripts.
152 */
153 #define SECURE_EXEC_DENY_INTERACTIVE 10
154 #define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11 /* make bit-10 immutable */
155
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Mickaël SalaünQuoted 243 lines · Mickaël Salaün
Quoted 233 lines · Serge E. Hallyn
Quoted 232 lines · Mickaël Salaün
The new
SECBIT_EXEC_RESTRICT_FILE,SECBIT_EXEC_DENY_INTERACTIVE, and their*_LOCKEDcounterparts 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_FILEis set, programs interpreting code should control executable resources according to execveat(2) +AT_CHECK(see previous commit).When
SECBIT_EXEC_DENY_INTERACTIVEis 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_FILEandSECBIT_EXEC_DENY_INTERACTIVEbits 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]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* ReplaceSECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTwithSECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVE:
https://lore.kernel.org/all/[email protected]/
* Remove the ptrace restrictions, suggested by Andy.
* Improve documentation according to the discussion with Jeff.New design since v18:
https://lore.kernel.org/r/[email protected]--- include/uapi/linux/securebits.h | 113 +++++++++++++++++++++++++++++++- security/commoncap.c | 29 ++++++-- 2 files changed, 135 insertions(+), 7 deletions(-)include/uapi/linux/securebits.h +112
-1@@ -52,10 +52,121 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* The SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE securebits +* are intended for script interpreters and dynamic linkers to enforce a +* consistent execution security policy handled by the kernel. +* +* Whether an interpreter should check these securebits or not depends on the +* security risk of running malicious scripts with respect to the execution +* environment, and whether the kernel can check if a script is trustworthy or +* not. For instance, Python scripts running on a server can use arbitrary +* syscalls and access arbitrary files. Such interpreters should then be +* enlighten to use these securebits and let users define their security +* policy. However, a JavaScript engine running in a web browser should +* already be sandboxed and then should not be able to harm the user's +* environment. +* +* When SECBIT_EXEC_RESTRICT_FILE is set, a process should only interpret or +* execute a file if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_RESTRICT_FILE_LOCKED bit should also be set. +* +* Programs should only enforce consistent restrictions according to the +* securebits but without relying on any other user-controlled configuration. +* Indeed, the use case for these securebits is to only trust executable code +* vetted by the system configuration (through the kernel), so we should be +* careful to not let untrusted users control this configuration. +* +* However, script interpreters may still use user configuration such as +* environment variables as long as it is not a way to disable the securebits +* checks. For instance, the PATH and LD_PRELOAD variables can be set by a +* script's caller. Changing these variables may lead to unintended code +* executions, but only from vetted executable programs, which is OK. For this +* to make sense, the system should provide a consistent security policy to +* avoid arbitrary code execution e.g., by enforcing a write xor execute +* policy. +* +* SECBIT_EXEC_RESTRICT_FILE is complementary and should also be checked. +*/ +#define SECURE_EXEC_RESTRICT_FILE 8 +#define SECURE_EXEC_RESTRICT_FILE_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE)) +#define SECBIT_EXEC_RESTRICT_FILE_LOCKED \ + (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED)) + +/* +* When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret +* interactive user commands (e.g. scripts). However, if such commands are +* passed through a file descriptor (e.g. stdin), its content should be +* interpreted if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* For instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set. +* +* See the SECBIT_EXEC_RESTRICT_FILE documentation. +* +* Here is the expected behavior for a script interpreter according to +* combination of any exec securebits: +* +* 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default) +* Always interpret scripts, and allow arbitrary user commands. +* => No threat, everyone and everything is trusted, but we can get ahead of +* potential issues thanks to the call to execveat with AT_CHECK which +* should always be performed but ignored by the script interpreter. +* Indeed, this check is still important to enable systems administrators +* to verify requests (e.g. with audit) and prepare for migration to a +* secure mode. +* +* 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0 +* Deny script interpretation if they are not executable, but allow +* arbitrary user commands. +* => The threat is (potential) malicious scripts run by trusted (and not +* fooled) users. That can protect against unintended script executions +* (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user +* sessions. +* +* 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1 +* Always interpret scripts, but deny arbitrary user commands. +* => This use case may be useful for secure services (i.e. without +* interactive user session) where scripts' integrity is verified (e.g. +* with IMA/EVM or dm-verity/IPE) but where access rights might not be +* ready yet. Indeed, arbitrary interactive commands would be much more +* difficult to check. +* +* 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1 +* Deny script interpretation if they are not executable, and also deny +* any arbitrary user commands. +* => The threat is malicious scripts run by untrusted users (but trusted +* code). This makes sense for system services that may only execute +* trusted scripts. +*/ +#define SECURE_EXEC_DENY_INTERACTIVE 10 +#define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11/* make bit-10 immutable */ + +#define SECBIT_EXEC_DENY_INTERACTIVE \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#define SECBIT_EXEC_DENY_INTERACTIVE_LOCKED \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE_LOCKED)) +#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \ issecure_mask(SECURE_NO_SETUID_FIXUP) | \ issecure_mask(SECURE_KEEP_CAPS) | \ - issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE)) + issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE) | \ + issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#endif /* _UAPI_LINUX_SECUREBITS_H */ security/commoncap.c +13
-6@@ -1302,21 +1302,38 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, & (old->securebits ^ arg2)) /*[1]*/ || ((old->securebits &SECURE_ALL_LOCKS & ~arg2))/*[2]*/ || (arg2 & ~(SECURE_ALL_LOCKS |SECURE_ALL_BITS ))/*[3]*/ - || (cap_capable (current_cred (), -current_cred ()->user_ns, -CAP_SETPCAP , -CAP_OPT_NONE ) !=0 )/*[4]*/ /* * [1] no changing of bits that are locked * [2] no unlocking of locks * [3] no setting of unsupported bits -* [4] doing anything requires privilege (go read about -* the "sendmail capabilities bug") */ ) /* cannot change a locked bit */ return -EPERM; +/* +* Doing anything requires privilege (go read about the +* "sendmail capabilities bug"), except for unprivileged bits. +* Indeed, the SECURE_ALL_UNPRIVILEGED bits are not +* restrictions enforced by the kernel but by user space on +* itself. +*/ +if (cap_capable (current_cred (),current_cred ()->user_ns, +CAP_SETPCAP ,CAP_OPT_NONE ) !=0 ) { +const unsigned long unpriv_and_locks = +SECURE_ALL_UNPRIVILEGED | +SECURE_ALL_UNPRIVILEGED <<1 ; +const unsigned long changed = old->securebits ^ arg2;Quoted 8 lines
security/commoncap.c int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, · line 1330
Quoted 4 lines
security/commoncap.c +4
-0+ +/* For legacy reason, denies non-change. */ +if (!changed) +return -EPERM ;This is odd to me. You say for legacy reasons, but, currently, calling
PR_SET_SECUREBITSwith no changes returns 0. So you may be breaking a lot of programs here, unless I'm mistaken.
Quoted 5 lines
When we call
PR_SET_SECUREBITSwith 0 (and if it was 0 too), it currently goes through the capability check and return-EPERMif the caller doesn't haveCAP_SETCAP. This is tested withTEST_F(secbits, legacy)intools/testing/selftests/exec/check-exec.c(patch 3/6).
Drat, my manual test case had a typo. Right you are - it fails now.
thanks,
-serge
[email protected]
Reviewed-byRe: [PATCH v20 1/6] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 6 lines · Mickaël Salaün
Quoted 5 lines · Serge E. Hallyn
Quoted 1 line · Mickaël Salaün
Add a new
AT_CHECKflag to execveat(2) to check if a file would beApologies for both bikeshedding and missing earlier discussions.
Quoted 8 lines
Quoted 4 lines
But
AT_CHECKsounds quite generic. How aboutAT_EXEC_CHECK, orAT_CHECK_EXEC_CREDS? (I would suggest justAT_CHECK_CREDSsince it's for use in execveat(2), but as it's anAT_flag, it's probably worth being more precise).As Amir pointed out, we need at least to use the
AT_EXECVE_CHECK_prefix, and I agree with theAT_EXECVE_CHECKname because it's about checking the whole execve request, not sepcifically a "creds" part.
Well, not the whole. You are explicitly not checking the validity of the files.
But ok. With that,
thanks,
-serge
Re: [PATCH v20 2/6] security: Add EXEC_RESTRICT_FILE and EXEC_DENY_INTERACTIVE securebits
In reply to Mickaël SalaünQuoted 45 lines · Mickaël Salaün
The new
SECBIT_EXEC_RESTRICT_FILE,SECBIT_EXEC_DENY_INTERACTIVE, and their*_LOCKEDcounterparts 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_FILEis set, programs interpreting code should control executable resources according to execveat(2) +AT_CHECK(see previous commit).When
SECBIT_EXEC_DENY_INTERACTIVEis 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_FILEandSECBIT_EXEC_DENY_INTERACTIVEbits 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/
Quoted 6 lines
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]>Cc Serge Hallyn <[email protected]>
thanks,
-serge
Quoted 197 lines
Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v19:
* ReplaceSECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTwithSECBIT_EXEC_RESTRICT_FILEandSECBIT_EXEC_DENY_INTERACTIVE:
https://lore.kernel.org/all/[email protected]/
* Remove the ptrace restrictions, suggested by Andy.
* Improve documentation according to the discussion with Jeff.New design since v18:
https://lore.kernel.org/r/[email protected]--- include/uapi/linux/securebits.h | 113 +++++++++++++++++++++++++++++++- security/commoncap.c | 29 ++++++-- 2 files changed, 135 insertions(+), 7 deletions(-)include/uapi/linux/securebits.h +112
-1@@ -52,10 +52,121 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* The SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE securebits +* are intended for script interpreters and dynamic linkers to enforce a +* consistent execution security policy handled by the kernel. +* +* Whether an interpreter should check these securebits or not depends on the +* security risk of running malicious scripts with respect to the execution +* environment, and whether the kernel can check if a script is trustworthy or +* not. For instance, Python scripts running on a server can use arbitrary +* syscalls and access arbitrary files. Such interpreters should then be +* enlighten to use these securebits and let users define their security +* policy. However, a JavaScript engine running in a web browser should +* already be sandboxed and then should not be able to harm the user's +* environment. +* +* When SECBIT_EXEC_RESTRICT_FILE is set, a process should only interpret or +* execute a file if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_RESTRICT_FILE_LOCKED bit should also be set. +* +* Programs should only enforce consistent restrictions according to the +* securebits but without relying on any other user-controlled configuration. +* Indeed, the use case for these securebits is to only trust executable code +* vetted by the system configuration (through the kernel), so we should be +* careful to not let untrusted users control this configuration. +* +* However, script interpreters may still use user configuration such as +* environment variables as long as it is not a way to disable the securebits +* checks. For instance, the PATH and LD_PRELOAD variables can be set by a +* script's caller. Changing these variables may lead to unintended code +* executions, but only from vetted executable programs, which is OK. For this +* to make sense, the system should provide a consistent security policy to +* avoid arbitrary code execution e.g., by enforcing a write xor execute +* policy. +* +* SECBIT_EXEC_RESTRICT_FILE is complementary and should also be checked. +*/ +#define SECURE_EXEC_RESTRICT_FILE 8 +#define SECURE_EXEC_RESTRICT_FILE_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_EXEC_RESTRICT_FILE (issecure_mask(SECURE_EXEC_RESTRICT_FILE)) +#define SECBIT_EXEC_RESTRICT_FILE_LOCKED \ + (issecure_mask(SECURE_EXEC_RESTRICT_FILE_LOCKED)) + +/* +* When SECBIT_EXEC_DENY_INTERACTIVE is set, a process should never interpret +* interactive user commands (e.g. scripts). However, if such commands are +* passed through a file descriptor (e.g. stdin), its content should be +* interpreted if a call to execveat(2) with the related file descriptor and +* the AT_CHECK flag succeed. +* +* For instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_EXEC_DENY_INTERACTIVE is set. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_EXEC_DENY_INTERACTIVE_LOCKED bit should also be set. +* +* See the SECBIT_EXEC_RESTRICT_FILE documentation. +* +* Here is the expected behavior for a script interpreter according to +* combination of any exec securebits: +* +* 1. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=0 (default) +* Always interpret scripts, and allow arbitrary user commands. +* => No threat, everyone and everything is trusted, but we can get ahead of +* potential issues thanks to the call to execveat with AT_CHECK which +* should always be performed but ignored by the script interpreter. +* Indeed, this check is still important to enable systems administrators +* to verify requests (e.g. with audit) and prepare for migration to a +* secure mode. +* +* 2. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=0 +* Deny script interpretation if they are not executable, but allow +* arbitrary user commands. +* => The threat is (potential) malicious scripts run by trusted (and not +* fooled) users. That can protect against unintended script executions +* (e.g. sh /tmp/*.sh). This makes sense for (semi-restricted) user +* sessions. +* +* 3. SECURE_EXEC_RESTRICT_FILE=0 SECURE_EXEC_DENY_INTERACTIVE=1 +* Always interpret scripts, but deny arbitrary user commands. +* => This use case may be useful for secure services (i.e. without +* interactive user session) where scripts' integrity is verified (e.g. +* with IMA/EVM or dm-verity/IPE) but where access rights might not be +* ready yet. Indeed, arbitrary interactive commands would be much more +* difficult to check. +* +* 4. SECURE_EXEC_RESTRICT_FILE=1 SECURE_EXEC_DENY_INTERACTIVE=1 +* Deny script interpretation if they are not executable, and also deny +* any arbitrary user commands. +* => The threat is malicious scripts run by untrusted users (but trusted +* code). This makes sense for system services that may only execute +* trusted scripts. +*/ +#define SECURE_EXEC_DENY_INTERACTIVE 10 +#define SECURE_EXEC_DENY_INTERACTIVE_LOCKED 11/* make bit-10 immutable */ + +#define SECBIT_EXEC_DENY_INTERACTIVE \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#define SECBIT_EXEC_DENY_INTERACTIVE_LOCKED \ + (issecure_mask(SECURE_EXEC_DENY_INTERACTIVE_LOCKED)) +#define SECURE_ALL_BITS (issecure_mask(SECURE_NOROOT) | \ issecure_mask(SECURE_NO_SETUID_FIXUP) | \ issecure_mask(SECURE_KEEP_CAPS) | \ - issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE)) + issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE) | \ + issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_EXEC_RESTRICT_FILE) | \ + issecure_mask(SECURE_EXEC_DENY_INTERACTIVE)) +#endif /* _UAPI_LINUX_SECUREBITS_H */ security/commoncap.c +23
-6@@ -1302,21 +1302,38 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, & (old->securebits ^ arg2)) /*[1]*/ || ((old->securebits &SECURE_ALL_LOCKS & ~arg2))/*[2]*/ || (arg2 & ~(SECURE_ALL_LOCKS |SECURE_ALL_BITS ))/*[3]*/ - || (cap_capable (current_cred (), -current_cred ()->user_ns, -CAP_SETPCAP , -CAP_OPT_NONE ) !=0 )/*[4]*/ /* * [1] no changing of bits that are locked * [2] no unlocking of locks * [3] no setting of unsupported bits -* [4] doing anything requires privilege (go read about -* the "sendmail capabilities bug") */ ) /* cannot change a locked bit */ return -EPERM; +/* +* Doing anything requires privilege (go read about the +* "sendmail capabilities bug"), except for unprivileged bits. +* Indeed, the SECURE_ALL_UNPRIVILEGED bits are not +* restrictions enforced by the kernel but by user space on +* itself. +*/ +if (cap_capable (current_cred (),current_cred ()->user_ns, +CAP_SETPCAP ,CAP_OPT_NONE ) !=0 ) { +const unsigned long unpriv_and_locks = +SECURE_ALL_UNPRIVILEGED | +SECURE_ALL_UNPRIVILEGED <<1 ; +const unsigned long changed = old->securebits ^ arg2; + +/* For legacy reason, denies non-change. */ +if (!changed) +return -EPERM ; + +/* Denies privileged changes. */ +if (changed & ~unpriv_and_locks) +return -EPERM ; + } + new =prepare_creds ();if (!new)return -ENOMEM ;Signature · 1 line
2.46.1