Script execution control (was O_MAYEXEC)
Subsystems (13)
Mickaël Salaün
maintainerv19 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.
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
-----------------------
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
See also a first LWN article about O_MAYEXEC and a new one about trusted_for(2) and its background:
* https://lwn.net/Articles/820000/
* https://lwn.net/Articles/832959/
Previous versions:
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 (5):
exec: Add a new AT_CHECK flag to execveat(2)
security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT
securebits
selftests/exec: Add tests for AT_CHECK and related securebits selftests/landlock: Add tests for execveat + AT_CHECK samples/should-exec: Add set-should-exec
fs/exec.c | 5 +- include/linux/binfmts.h | 7 +- include/uapi/linux/fcntl.h | 30 ++ include/uapi/linux/securebits.h | 56 ++- kernel/audit.h | 1 + kernel/auditsc.c | 1 + samples/Kconfig | 7 + samples/Makefile | 1 + samples/should-exec/.gitignore | 1 + samples/should-exec/Makefile | 13 + samples/should-exec/set-should-exec.c | 88 ++++ security/commoncap.c | 63 ++- tools/testing/selftests/exec/.gitignore | 2 + tools/testing/selftests/exec/Makefile | 8 + tools/testing/selftests/exec/config | 2 + tools/testing/selftests/exec/false.c | 5 + tools/testing/selftests/exec/should-exec.c | 449 +++++++++++++++++++++ tools/testing/selftests/landlock/fs_test.c | 26 ++ 18 files changed, 753 insertions(+), 12 deletions(-) create mode 100644 samples/should-exec/.gitignore create mode 100644 samples/should-exec/Makefile create mode 100644 samples/should-exec/set-should-exec.c create mode 100644 tools/testing/selftests/exec/config create mode 100644 tools/testing/selftests/exec/false.c create mode 100644 tools/testing/selftests/exec/should-exec.c
base-commit: f2661062f16b2de5d7b6a5c42a9a5c96326b8454
Signature · 1 line
2.45.2
Mickaël Salaün
maintainerRFC PATCH v19 1/5 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of AT_CHECK follows the exact kernel semantic as for a real execution, user space gets the same error codes.
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to security_bprm_creds_for_exec().
Because LSMs may only change bprm's credentials, use of AT_CHECK with current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so when bprm->is_check is set. Of course, such policy change should be in line with the new user space code.
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_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT 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
---
New design since v18:
https://lore.kernel.org/r/[email protected]
--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)
fs/exec.c +3 -2
@@ -931,7 +931,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 ;
@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)
@@ -1885,7 +1886,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 +30 -0
@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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; }
Signature · 1 line
2.45.2
Kees Cook
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 7 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].
Quoted 5 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.
Nice! I much prefer this method of going through the exec machinery so we always have a single code path for these kinds of checks.
Quoted 5 lines
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.
I'd like some additional comments in the code that reminds us that access control checks have finished past a certain point.
[...]
Quoted 13 lines
fs/exec.c +1
-1@@ -931,7 +931,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 ;
[...]
Quoted 3 lines
+ * To avoid race conditions leading to time-of-check to time-of-use issues,
+ *AT_CHECKshould be used withAT_EMPTY_PATHto check against a file
+ * descriptor instead of a path.
I want this enforced by the kernel. Let's not leave trivial ToCToU foot-guns around. i.e.:
if ((flags & AT_CHECK) == AT_CHECK && (flags & AT_EMPTY_PATH) == 0)
return ERR_PTR(-EBADF);
Signature · 1 line
Kees Cook
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 17 lines · Kees Cook
Quoted 7 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].Quoted 5 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Nice! I much prefer this method of going through the exec machinery so we always have a single code path for these kinds of checks.
Quoted 8 lines
Quoted 5 lines
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.I'd like some additional comments in the code that reminds us that access control checks have finished past a certain point.
Where in the code? Just before the bprm->is_check assignment?
Quoted 20 lines
[...]
Quoted 13 lines
fs/exec.c +1
-1@@ -931,7 +931,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 ;[...]
Quoted 3 lines
+ * To avoid race conditions leading to time-of-check to time-of-use issues,
+ *AT_CHECKshould be used withAT_EMPTY_PATHto check against a file
+ * descriptor instead of a path.
Quoted 5 lines
I want this enforced by the kernel. Let's not leave trivial ToCToU foot-guns around. i.e.:
if ((flags & AT_CHECK) == AT_CHECK && (flags & AT_EMPTY_PATH) == 0)return
ERR_PTR(-EBADF);
There are valid use cases relying on pathnames. See Linus's comment:
https://lore.kernel.org/r/CAHk-=whb=XuU=LGKnJWaa7LOYQz9VwHs8SLfgLbT5sf2VAbX1A@mail.gmail.com
Kees Cook
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 24 lines · Mickaël Salaün
Quoted 23 lines · Kees Cook
Quoted 7 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].Quoted 5 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Nice! I much prefer this method of going through the exec machinery so we always have a single code path for these kinds of checks.
Quoted 5 lines
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.
Quoted 4 lines
Quoted 2 lines
I'd like some additional comments in the code that reminds us that access control checks have finished past a certain point.
Where in the code? Just before the
bprm->is_checkassignment?
Yeah, that's what I was thinking.
Signature · 1 line
Kees Cook
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël Salaün* Mickaël Salaün:
Quoted 6 lines
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].
Some distributions no longer set executable bits on most shared objects, which I assume would interfere with AT_CHECK probing for shared objects. Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Thanks,
Florian
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 11 lines · Florian Weimer
* Mickaël Salaün:
Quoted 6 lines
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].Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects.
A file without the execute permission is not considered as executable by the kernel. The AT_CHECK flag doesn't change this semantic. Please note that this is just a check, not a restriction. See the next patch for the optional policy enforcement.
Anyway, we need to define the policy, and for Linux this is done with the file permission bits. So for systems willing to have a consistent execution policy, we need to rely on the same bits.
Quoted 8 lines
Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Interesting. Can you please point to the bug report and the fix? I don't see any ELIBEXEC in the kernel.
FYI, AT_CHECK doesn't check the content of the file (unlike a full execve call).
Anyway, I think we should not design a new kernel interface to work around a current user space bug.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
* Mickaël Salaün:
Quoted 13 lines · Florian Weimer
Quoted 11 lines
* Mickaël Salaün:
Quoted 6 lines
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].Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects.
Quoted 8 lines
A file without the execute permission is not considered as executable by the kernel. The
AT_CHECKflag doesn't change this semantic. Please note that this is just a check, not a restriction. See the next patch for the optional policy enforcement.Anyway, we need to define the policy, and for Linux this is done with the file permission bits. So for systems willing to have a consistent execution policy, we need to rely on the same bits.
Yes, that makes complete sense. I just wanted to point out the odd interaction with the old binutils bug and the (sadly still current) kernel bug.
Quoted 11 lines
Quoted 8 lines
Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Interesting. Can you please point to the bug report and the fix? I don't see any ELIBEXEC in the kernel.
The kernel hasn't been fixed yet. I do think this should be fixed, so that distributions can bring back the executable bit.
Thanks,
Florian
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 37 lines · Florian Weimer
* Mickaël Salaün:
Quoted 13 lines · Florian Weimer
Quoted 11 lines
* Mickaël Salaün:
Quoted 6 lines
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].Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects.Quoted 8 lines
A file without the execute permission is not considered as executable by the kernel. The
AT_CHECKflag doesn't change this semantic. Please note that this is just a check, not a restriction. See the next patch for the optional policy enforcement.Anyway, we need to define the policy, and for Linux this is done with the file permission bits. So for systems willing to have a consistent execution policy, we need to rely on the same bits.
Yes, that makes complete sense. I just wanted to point out the odd interaction with the old binutils bug and the (sadly still current) kernel bug.
Quoted 9 lines
Quoted 8 lines
Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Quoted 5 lines
Quoted 2 lines
Interesting. Can you please point to the bug report and the fix? I don't see any ELIBEXEC in the kernel.
The kernel hasn't been fixed yet. I do think this should be fixed, so that distributions can bring back the executable bit.
Can you please point to the mailing list discussion or the bug report?
PATCH 1/1 binfmt_elf: Fail execution of shared objects with ELIBEXEC (was: Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2))
* Mickaël Salaün:
Quoted 38 lines · Florian Weimer
Quoted 37 lines
* Mickaël Salaün:
Quoted 13 lines · Florian Weimer
Quoted 11 lines
* Mickaël Salaün:
Quoted 6 lines
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].Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects.Quoted 8 lines
A file without the execute permission is not considered as executable by the kernel. The
AT_CHECKflag doesn't change this semantic. Please note that this is just a check, not a restriction. See the next patch for the optional policy enforcement.Anyway, we need to define the policy, and for Linux this is done with the file permission bits. So for systems willing to have a consistent execution policy, we need to rely on the same bits.
Yes, that makes complete sense. I just wanted to point out the odd interaction with the old binutils bug and the (sadly still current) kernel bug.
Quoted 9 lines
Quoted 8 lines
Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Interesting. Can you please point to the bug report and the fix? I don't see any ELIBEXEC in the kernel.
The kernel hasn't been fixed yet. I do think this should be fixed, so that distributions can bring back the executable bit.
Can you please point to the mailing list discussion or the bug report?
I'm not sure if this was ever reported upstream as an RFE to fail with ELIBEXEC. We have downstream bug report:
Prevent executed .so files with e_entry == 0 from attempting to become a process.
<https://bugzilla.redhat.com/show_bug.cgi?id=2004942>
I've put together a patch which seems to work, see below.
I don't think there's any impact on AT_CHECK with execveat because that mode will never get to this point.
Thanks,
Florian
---8<-----------------------------------------------------------------
Subject: binfmt_elf: Fail execution of shared objects with ELIBEXEC
Historically, binutils has used the start of the text segment as the entry point if _start was not defined. Executing such files results in crashes with random effects, depending on what code resides there. However, starting with binutils 2.38, BFD ld uses a zero entry point, due to commit 5226a6a892f922ea672e5775c61776830aaf27b7 ("Change the linker's heuristic for computing the entry point for binaries so that shared libraries default to an entry point of 0."). This means that shared objects with zero entry points are becoming more common, and it makes sense for the kernel to recognize them and refuse to execute them.
For backwards compatibility, if a load segment does not map the ELF header at file offset zero, the kernel still proceeds as before, in case the file is very non-standard and can actually start executing at virtual offset zero.
fs/binfmt_elf.c +18 -0
@@ -830,6 +830,7 @@ static int load_elf_binary(struct linux_binprm *bprm)unsigned long e_entry;unsigned long interp_load_addr =0 ;unsigned long start_code, end_code, start_data, end_data; +bool elf_header_mapped = false;unsigned long reloc_func_desc__maybe_unused =0 ;int executable_stack =EXSTACK_DEFAULT ;struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;
@@ -865,6 +866,9 @@ static int load_elf_binary(struct linux_binprm *bprm)continue ; } +if (elf_ppnt->p_type ==PT_LOAD && !elf_ppnt->p_offset) + elf_header_mapped = true; +if (elf_ppnt->p_type !=PT_INTERP )continue ;
@@ -921,6 +925,20 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out_free_ph; } +/* +* A zero value for e_entry means that the ELF file has no +* entry point. If the ELF header is mapped, this is +* guaranteed to crash (often even on the first instruction), +* so fail the execve system call instead. (This is most +* likely to happen for a shared object.) If the object has a +* program interpreter, dealing with the situation is its +* responsibility. +*/ +if (elf_header_mapped && !elf_ex->e_entry && !interpreter) { + retval = -ELIBEXEC ; + goto out_free_dentry; + } + elf_ppnt = elf_phdata;for (i =0 ; i < elf_ex->e_phnum; i++, elf_ppnt++)switch (elf_ppnt->p_type) {
Re: [PATCH] binfmt_elf: Fail execution of shared objects with ELIBEXEC
Quoted 77 lines · Florian Weimer
* Mickaël Salaün:
Quoted 38 lines · Florian Weimer
Quoted 37 lines
* Mickaël Salaün:
Quoted 13 lines · Florian Weimer
Quoted 11 lines
* Mickaël Salaün:
Quoted 6 lines
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].Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects.Quoted 8 lines
A file without the execute permission is not considered as executable by the kernel. The
AT_CHECKflag doesn't change this semantic. Please note that this is just a check, not a restriction. See the next patch for the optional policy enforcement.Anyway, we need to define the policy, and for Linux this is done with the file permission bits. So for systems willing to have a consistent execution policy, we need to rely on the same bits.
Yes, that makes complete sense. I just wanted to point out the odd interaction with the old binutils bug and the (sadly still current) kernel bug.
Quoted 9 lines
Quoted 8 lines
Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Interesting. Can you please point to the bug report and the fix? I don't see any ELIBEXEC in the kernel.
The kernel hasn't been fixed yet. I do think this should be fixed, so that distributions can bring back the executable bit.
Can you please point to the mailing list discussion or the bug report?
I'm not sure if this was ever reported upstream as an RFE to fail with ELIBEXEC. We have downstream bug report:
Prevent executed .so files with
e_entry== 0 from attempting to become a process.
<https://bugzilla.redhat.com/show_bug.cgi?id=2004942>I've put together a patch which seems to work, see below.
I don't think there's any impact on
AT_CHECKwith execveat because that mode will never get to this point.Thanks,
Florian---8<-----------------------------------------------------------------
Subject:binfmt_elf: Fail execution of shared objects with ELIBEXECHistorically, binutils has used the start of the text segment as the entry point if
_startwas not defined. Executing such files results in crashes with random effects, depending on what code resides there. However, starting with binutils 2.38, BFD ld uses a zero entry point, due to commit5226a6a892f922ea672e5775c61776830aaf27b7("Change the linker's heuristic for computing the entry point for binaries so that shared libraries default to an entry point of 0."). This means that shared objects with zero entry points are becoming more common, and it makes sense for the kernel to recognize them and refuse to execute them.
Quoted 4 lines
For backwards compatibility, if a load segment does not map the ELF header at file offset zero, the kernel still proceeds as before, in case the file is very non-standard and can actually start executing at virtual offset zero.
As written I find the logic of the patch confusing, and slightly wrong.
The program header value e_entry is a virtual address, possibly adjusted by load_bias. Which makes testing it against the file offset of a PT_LOAD segment wrong. It needs to test against elf_ppnt->p_vaddr.
I think performing an early sanity check to avoid very confusing crashes seems sensible (as long as it is inexpensive). This appears inexpensive enough that we don't care. This code is also before begin_new_exec so it is early enough to be meaningful.
I think the check should simply test if e_entry is mapped. So a range check please to see if e_entry falls in a PT_LOAD segment.
Having code start at virtual address 0 is a perfectly fine semantically and might happen in embedded scenarios.
The program header is not required to be mapped or be first, (AKA p_offset and p_vaddr can have a somewhat arbitrary relationship) so any mention of the program header in your logic seems confusing to me.
I think your basic structure will work. Just the first check needs to check if e_entry is lands inside the virtual address of a PT_LOAD segment. The second check should just be checking a variable to see if e_entry was inside any PT_LOAD segment, and there is no interpreter.
Does that make sense?
Eric
Quoted 46 lines
Signed-off-by Florian Weimer <[email protected]>fs/binfmt_elf.c +18
-0@@ -830,6 +830,7 @@ static int load_elf_binary(struct linux_binprm *bprm)unsigned long e_entry;unsigned long interp_load_addr =0 ;unsigned long start_code, end_code, start_data, end_data; +bool elf_header_mapped = false;unsigned long reloc_func_desc__maybe_unused =0 ;int executable_stack =EXSTACK_DEFAULT ;struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;@@ -865,6 +866,9 @@ static int load_elf_binary(struct linux_binprm *bprm)continue ; } +if (elf_ppnt->p_type ==PT_LOAD && !elf_ppnt->p_offset) + elf_header_mapped = true; +if (elf_ppnt->p_type !=PT_INTERP )continue ;@@ -921,6 +925,20 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out_free_ph; } +/* +* A zero value for e_entry means that the ELF file has no +* entry point. If the ELF header is mapped, this is +* guaranteed to crash (often even on the first instruction), +* so fail the execve system call instead. (This is most +* likely to happen for a shared object.) If the object has a +* program interpreter, dealing with the situation is its +* responsibility. +*/ +if (elf_header_mapped && !elf_ex->e_entry && !interpreter) { + retval = -ELIBEXEC ; + goto out_free_dentry; + } + elf_ppnt = elf_phdata;for (i =0 ; i < elf_ex->e_phnum; i++, elf_ppnt++)switch (elf_ppnt->p_type) {
Re: [PATCH] binfmt_elf: Fail execution of shared objects with ELIBEXEC
* Eric W. Biederman:
Quoted 5 lines
As written I find the logic of the patch confusing, and slightly wrong.
The program header value
e_entryis a virtual address, possibly adjusted byload_bias. Which makes testing it against the file offset of aPT_LOADsegment wrong. It needs to test againstelf_ppnt->p_vaddr.
I think we need to test both against zero, or maybe invert the logic: if something is mapped at virtual address zero that doesn't come from a zero file offset, we disable the ELIBEXEC check.
Quoted 4 lines
I think performing an early sanity check to avoid very confusing crashes seems sensible (as long as it is inexpensive). This appears inexpensive enough that we don't care. This code is also before
begin_new_execso it is early enough to be meaningful.
Yeah, it was quite confusing when it was after begin_new_exec because the ELIBEXEC error is visible under strace, and then the SIGSEGV comes …
Quoted 2 lines
I think the check should simply test if
e_entryis mapped. So a range check please to see ife_entryfalls in aPT_LOADsegment.
It's usually mapped even with e_entry ==0 because the ELF header is loaded at virtual address zero for ET_DYN using the default linker flags (and this is the case we care about). With -z noseparate-code, it is even mapped executable.
Quoted 2 lines
Having code start at virtual address 0 is a perfectly fine semantically and might happen in embedded scenarios.
To keep supporting this case, we need to check that the ELF header is at address zero, because we make a leap of faith and assume it's not really executable even if it is mapped as such because due to its role in the file format, it does not contain executable instructions. That's why the patch is focused on the ELF header.
I could remove all these checks and just return ELIBEXEC for a zero entry point. I think this is valid based on the ELF specification, but it may have a backwards compatibility impact.
Quoted 3 lines
The program header is not required to be mapped or be first, (AKA
p_offsetandp_vaddrcan have a somewhat arbitrary relationship) so any mention of the program header in your logic seems confusing to me.
It's the ELF header.
Quoted 4 lines
I think your basic structure will work. Just the first check needs to check if
e_entryis lands inside the virtual address of aPT_LOADsegment. The second check should just be checking a variable to see ife_entrywas inside anyPT_LOADsegment, and there is no interpreter.
I think the range check doesn't help here. Just checking p_vaddr for zero in addition to p_offset should be sufficient. If you agree, can test and send an updated patch.
Thanks,
Florian
Mickaël Salaün
maintainerRe: [PATCH] binfmt_elf: Fail execution of shared objects with ELIBEXEC (was: Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2))
In reply to Florian WeimerQuoted 46 lines · Florian Weimer
* Mickaël Salaün:
Quoted 44 lines · Florian Weimer
Quoted 42 lines
* Mickaël Salaün:
Quoted 13 lines · Florian Weimer
Quoted 11 lines
* Mickaël Salaün:
Quoted 6 lines
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].Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects.Quoted 8 lines
A file without the execute permission is not considered as executable by the kernel. The
AT_CHECKflag doesn't change this semantic. Please note that this is just a check, not a restriction. See the next patch for the optional policy enforcement.Anyway, we need to define the policy, and for Linux this is done with the file permission bits. So for systems willing to have a consistent execution policy, we need to rely on the same bits.
Yes, that makes complete sense. I just wanted to point out the odd interaction with the old binutils bug and the (sadly still current) kernel bug.
Quoted 11 lines
Quoted 8 lines
Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Interesting. Can you please point to the bug report and the fix? I don't see any ELIBEXEC in the kernel.
The kernel hasn't been fixed yet. I do think this should be fixed, so that distributions can bring back the executable bit.
Quoted 8 lines
Quoted 1 line
Can you please point to the mailing list discussion or the bug report?
I'm not sure if this was ever reported upstream as an RFE to fail with ELIBEXEC. We have downstream bug report:
Prevent executed .so files with
e_entry== 0 from attempting to become a process.
<https://bugzilla.redhat.com/show_bug.cgi?id=2004942>
Thanks for the info.
Quoted 5 lines
I've put together a patch which seems to work, see below.
I don't think there's any impact on
AT_CHECKwith execveat because that mode will never get to this point.
Correct, that is not an issue for AT_CHECK use cases.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Florian WeimerHi
Quoted 10 lines · Florian Weimer
* Mickaël Salaün:
Quoted 6 lines
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].
Quoted 11 lines
Some distributions no longer set executable bits on most shared objects, which I assume would interfere with
AT_CHECKprobing for shared objects. Removing the executable bit is attractive because of a combination of two bugs: a binutils wart which until recently always set the entry point address in the ELF header to zero, and the kernel not checking for a zero entry point (maybe in combination with an absent program interpreter) and failing the execve with ELIBEXEC, instead of doing the execve and then faulting at virtual address zero. Removing the executable bit is currently the only way to avoid these confusing crashes, so I understand the temptation.
Will dynamic linkers use the execveat(AT_CHECK) to check shared libraries too ? or just the main executable itself.
Thanks.
-Jeff
Quoted 3 lines
Thanks,
Florian
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.
I expect that dynamic linkers will have to do this for everything they map. Usually, that does not include the maim program, but this can happen with explicit loader invocations (“ld.so /bin/true”).
Thanks,
Florian
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 8 lines · Florian Weimer
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to security_bprm_creds_for_exec(), some of them might be specific for the main executable ?
e.g. ChromeOS uses security_bprm_creds_for_exec to block executable memfd [1], applying this means automatically extending the block to the .so object.
I'm not sure if other LSMs need to be updated ? e.g. will SELINUX check for .so with its process transaction policy ?
[1] https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/3834992
-Jeff
Quoted 6 lines
Usually, that does not include the maim program, but this can happen with explicit loader invocations (“ld.so /bin/true”).
Thanks,
Florian
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 9 lines · Jeff Xu
Quoted 8 lines · Florian Weimer
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Correct, that would enable to safely handle LD_PRELOAD for instance.
Quoted 6 lines
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to
security_bprm_creds_for_exec(), some of them might be specific for the main executable ?
e.g. ChromeOS usessecurity_bprm_creds_for_execto block executable memfd [1], applying this means automatically extending the block to the .so object.
That's a good example of how this AT_CHECK check makes sense.
Landlock will probably get a similar (optional) restriction too:
https://github.com/landlock-lsm/linux/issues/37
Quoted 3 lines
I'm not sure if other LSMs need to be updated ? e.g. will SELINUX check for .so with its process transaction policy ?
LSM should not need to be updated with this patch series. However, systems/components/containers enabling this new check should make sure it works with their current policy.
Quoted 12 lines
[1] https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/3834992
-Jeff
Quoted 6 lines
Usually, that does not include the maim program, but this can happen with explicit loader invocations (“ld.so /bin/true”).
Thanks,
Florian
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Jeff Xu* Jeff Xu:
Quoted 12 lines · Florian Weimer
Quoted 8 lines
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to
security_bprm_creds_for_exec(), some of them might be specific for the main executable ?
If we want to avoid that, we could have an agreed-upon error code which the LSM can signal that it'll never fail AT_CHECK checks, so we only have to perform the extra system call once.
Thanks,
Florian
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 12 lines · Florian Weimer
* Jeff Xu:
Quoted 9 lines · Florian Weimer
Quoted 8 lines
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Quoted 8 lines
Quoted 3 lines
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to
security_bprm_creds_for_exec(), some of them might be specific for the main executable ?If we want to avoid that, we could have an agreed-upon error code which the LSM can signal that it'll never fail
AT_CHECKchecks, so we only have to perform the extra system call once.
Right, something like that.
I would prefer not having AT_CHECK specific code in LSM code as an initial goal, if that works, great.
-Jeff
Quoted 3 lines
Thanks,
Florian
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 8 lines · Jeff Xu
Quoted 7 lines · Florian Weimer
* Jeff Xu:
Quoted 4 lines · Florian Weimer
Quoted 3 lines
* Jeff Xu:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to
security_bprm_creds_for_exec(), some of them might be specific for the main executable ?
Yes, we should check every executable code (including seccomp filters) to get a consistent policy.
What do you mean by "specific for the main executable"?
Quoted 4 lines
Quoted 4 lines
If we want to avoid that, we could have an agreed-upon error code which the LSM can signal that it'll never fail
AT_CHECKchecks, so we only have to perform the extra system call once.
I'm not sure to follow. Either we check executable code or we don't, but it doesn't make sense to only check some parts (except for migration of user space code in a system, which is one purpose of the securebits added with the next patch).
The idea with AT_CHECK is to unconditionnaly check executable right the same way it is checked when a file is executed. User space can decide to check that or not according to its policy (i.e. securebits).
Quoted 4 lines
Quoted 1 line
Right, something like that.
I would prefer not havingAT_CHECKspecific code in LSM code as an initial goal, if that works, great.
LSMs should not need to change anything, but they are free to implement new access right according to AT_CHECK.
Quoted 6 lines
-Jeff
Quoted 3 lines
Thanks,
Florian
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
* Mickaël Salaün:
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
If we want to avoid that, we could have an agreed-upon error code which the LSM can signal that it'll never fail
AT_CHECKchecks, so we only have to perform the extra system call once.
Quoted 8 lines
I'm not sure to follow. Either we check executable code or we don't, but it doesn't make sense to only check some parts (except for migration of user space code in a system, which is one purpose of the securebits added with the next patch).
The idea with
AT_CHECKis to unconditionnaly check executable right the same way it is checked when a file is executed. User space can decide to check that or not according to its policy (i.e. securebits).
I meant it purely as a performance optimization, to skip future system calls if we know they won't provide any useful information for this process. In the grand scheme of things, the extra system call probably does not matter because we already have to do costly things like mmap.
Thanks,
Florian
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 11 lines · Florian Weimer
* Mickaël Salaün:
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
If we want to avoid that, we could have an agreed-upon error code which the LSM can signal that it'll never fail
AT_CHECKchecks, so we only have to perform the extra system call once.I'm not sure to follow. Either we check executable code or we don't, but it doesn't make sense to only check some parts (except for migration of user space code in a system, which is one purpose of the securebits added with the next patch).
Quoted 8 lines
Quoted 3 lines
The idea with
AT_CHECKis to unconditionnaly check executable right the same way it is checked when a file is executed. User space can decide to check that or not according to its policy (i.e. securebits).I meant it purely as a performance optimization, to skip future system calls if we know they won't provide any useful information for this process. In the grand scheme of things, the extra system call probably does not matter because we already have to do costly things like mmap.
Indeed, the performance impact of execveat+AT_CHECK should be negligible compared to everything else needed to interpret a script or spawn a process. Moreover, these checks should only be performed when SECBIT_SHOULD_EXEC_CHECK is set for the caller.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 15 lines · Mickaël Salaün
Quoted 13 lines · Jeff Xu
Quoted 12 lines · Florian Weimer
* Jeff Xu:
Quoted 9 lines · Florian Weimer
Quoted 8 lines
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to
security_bprm_creds_for_exec(), some of them might be specific for the main executable ?Yes, we should check every executable code (including seccomp filters) to get a consistent policy.
What do you mean by "specific for the main executable"?
I meant:
The check is for the exe itself, not .so, etc.
For example: /usr/bin/touch is checked.
not the shared objects:
ldd /usr/bin/touch
linux-vdso.so.1 (0x00007ffdc988f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f59b6757000) /lib64/ld-linux-x86-64.so.2 (0x00007f59b6986000)
Basically, I asked if the check can be extended to shared-objects, seccomp filters, etc, without modifying existing LSMs. you pointed out "LSM should not need to be updated with this patch series.", which already answered my question.
Thanks.
-Jeff
-Jeff
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 27 lines · Jeff Xu
Quoted 15 lines · Mickaël Salaün
Quoted 13 lines · Jeff Xu
Quoted 12 lines · Florian Weimer
* Jeff Xu:
Quoted 9 lines · Florian Weimer
Quoted 8 lines
* Jeff Xu:
Quoted 2 lines
Will dynamic linkers use the
execveat(AT_CHECK)to check shared libraries too ? or just the main executable itself.I expect that dynamic linkers will have to do this for everything they map.
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Then all the objects (.so, .sh, etc.) will go through the check from execveat's main to
security_bprm_creds_for_exec(), some of them might be specific for the main executable ?Yes, we should check every executable code (including seccomp filters) to get a consistent policy.
What do you mean by "specific for the main executable"?
I meant:
Quoted 8 lines
The check is for the exe itself, not .so, etc.
For example:
/usr/bin/touchis checked.
not the shared objects:
ldd/usr/bin/touch
linux-vdso.so.1 (0x00007ffdc988f000)
libc.so.6 =>/lib/x86_64-linux-gnu/libc.so.6(0x00007f59b6757000)/lib64/ld-linux-x86-64.so.2(0x00007f59b6986000)
ld.so should be patched to check shared-objects.
Quoted 3 lines
Basically, I asked if the check can be extended to shared-objects, seccomp filters, etc, without modifying existing LSMs.
Yes, the check should be used against any piece of code such as shared-objects, seccomp filters...
Quoted 7 lines
you pointed out "LSM should not need to be updated with this patch series.", which already answered my question.
Thanks.
(quoted diff) +0
-2-Jeff -Jeff
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 8 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].
Can you give a worked-out example of how this is useful?
I assume the idea is that a program could open a file, then pass the fd to execveat() to get the kernel's idea of whether it's permissible to execute it. And then the program would interpret the file, which is morally like executing it. And there would be a big warning in the manpage that passing a *path* is subject to a TOCTOU race.
This type of usage will do the wrong thing if LSM policy intends to lock down the task if the task were to actually exec the file. I personally think this is a mis-design (let the program doing the exec-ing lock itself down, possibly by querying a policy, but having magic happen on exec seems likely to do the wrong thing more often that it does the wright thing), but that ship sailed a long time ago.
So maybe what's actually needed is a rather different API: a way to check *and perform* the security transition for an exec without actually execing. This would need to be done NO_NEW_PRIVS style for reasons that are hopefully obvious, but it would permit:
fd = open(some script);
if (do_exec_transition_without_exec(fd) != 0)
return; // don't actually do it
// OK, we may have just lost privileges. But that's okay, because we meant to do that.
// Make sure we've munmapped anything sensitive and erased any secrets from memory,
// and then interpret the script!
I think this would actually be straightforward to implement in the kernel -- one would need to make sure that all the relevant no_new_privs checks are looking in the right place (as the task might not actually have no_new_privs set, but LSM_UNSAFE_NO_NEW_PRIVS would still be set), but I don't see any reason this would be insurmountable, nor do I expect there would be any fundamental problems.
Quoted 30 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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.
Quoted 5 lines
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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.
Can you explain what those bits do? And why they're useful?
Quoted 6 lines
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
This one at least returns an fd, so it looks less likely to get misused in a way that adds a TOCTOU race.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 11 lines · Andy Lutomirski
Quoted 8 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].Can you give a worked-out example of how this is useful?
Which part? Please take a look at CLIP OS, chromeOS, and PEP 578 use cases and related code (see cover letter).
Quoted 6 lines
I assume the idea is that a program could open a file, then pass the fd to
execveat()to get the kernel's idea of whether it's permissible to execute it. And then the program would interpret the file, which is morally like executing it. And there would be a big warning in the manpage that passing a *path* is subject to a TOCTOU race.
yes
Quoted 3 lines
This type of usage will do the wrong thing if LSM policy intends to lock down the task if the task were to actually exec the file. I
Why? LSMs should currently only change the bprm's credentials not the current's credentials. If needed, we can extend the current patch series with LSM specific patches for them to check bprm->is_check.
Quoted 4 lines
personally think this is a mis-design (let the program doing the exec-ing lock itself down, possibly by querying a policy, but having magic happen on exec seems likely to do the wrong thing more often that it does the wright thing), but that ship sailed a long time ago.
The execveat+AT_CHECK is only a check that doesn't impact the caller. Maybe you're talking about process transition with future LSM changes? In this case, we could add another flag, but I'm convinced it would be confusing for users. Anyway, let LSMs experiment with that and we'll come up with a new flag if needed. The current approach is a good and useful piece to fill a gap in Linux access control systems.
Quoted 5 lines
So maybe what's actually needed is a rather different API: a way to check *and perform* the security transition for an exec without actually execing. This would need to be done
NO_NEW_PRIVSstyle for reasons that are hopefully obvious, but it would permit:
NO_NEW_PRIVS is not that obvious in this case because the restrictions are enforced by user space, not the kernel. NO_NEW_PRIVS makes sense to avoid kernel restrictions be requested by a malicious/unprivileged process to change the behavior of a (child) privileged/trusted process. We are not in this configuration here. The only change would be for ptrace, which is a good thing either way and should not harm SUID processes but avoid confused deputy attack for them too.
If this is about an LSM changing the caller's credentials, then yes it might want to set additional flags, but that would be specific to their implementation, not part of this patch.
Quoted 11 lines
fd = open(some script);
if (do_exec_transition_without_exec(fd)!= 0)
return; // don't actually do it// OK, we may have just lost privileges. But that's okay, because we meant to do that.
// Make sure we've munmapped anything sensitive and erased any secrets from memory,
// and then interpret the script!
Quoted 7 lines
I think this would actually be straightforward to implement in the kernel -- one would need to make sure that all the relevant
no_new_privschecks are looking in the right place (as the task might not actually haveno_new_privsset, butLSM_UNSAFE_NO_NEW_PRIVSwould still be set), but I don't see any reason this would be insurmountable, nor do I expect there would be any fundamental problems.
OK, that's what is described below with security_bprm_creds_for_exec(). Each LSM can implement this change with the current patch series, but that should be part of a dedicated patch series per LSM, for those willing to leverage this new feature.
Quoted 32 lines
Quoted 32 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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.
Quoted 7 lines
Quoted 5 lines
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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.Can you explain what those bits do? And why they're useful?
I didn't want to duplicate the comments above their definition explaining their usage. Please let me know if it's not enough.
Quoted 10 lines
Quoted 7 lines
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_MAYEXECThis one at least returns an fd, so it looks less likely to get misused in a way that adds a TOCTOU race.
We can use both an FD or a path name with execveat(2). See discussion with Kees and comment from Linus.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 8 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].
Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.
So we concluded that execveat(AT_CHECK) will be used to check the exec, shared object, script and config file (such as seccomp config), I'm still thinking execveat(AT_CHECK) vs faccessat(AT_CHECK) in different use cases:
execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from do_execveat_common to security_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.
--------------------------
now looked at user cases (focus on elf for now)
1> ld.so /tmp/a.out, /tmp/a.out is on non-exec mount
dynamic linker will first call execveat(fd, AT_CHECK) then execveat(fd)
2> execve(/usr/bin/some.out) and some.out has dependency on /tmp/a.so /usr/bin/some.out will pass AT_CHECK
3> execve(usr/bin/some.out) and some.out uses custom /tmp/ld.so /usr/bin/some.out will pass AT_CHECK, however, it uses a custom /tmp/ld.so (I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, and binfmt_elf.c allocate memory for ld.so during execveat call)
4> dlopen(/tmp/a.so)
I assume dynamic linker will call execveat(AT_CHECK), before map a.so into memory.
For case 1>
Alternative solution: Because AT_CHECK is always called, I think we can avoid the first AT_CHECK call, and check during execveat(fd), this means the kernel will enforce SECBIT_EXEC_RESTRICT_FILE = 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesful execveat() case.
For case 2>
dynamic linker will call execve(AT_CHECK), then mmap(fd) into memory. However, the process can all open then mmap() directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.
Alternative solution:
dynamic linker call AT_CHECK for each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time of mmap(fd, executable memory) and enforce SECBIT_EXEC_RESTRICT_FILE = 1
Alternative solution 2:
a new syscall to load the .so and enforce the AT_CHECK in kernel
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ), unless the user space can harden the creation of executable anonymous memory in some way.
For case 3>
I think binfmt_elf.c in the kernel needs to check the ld.so to make sure it passes AT_CHECK, before loading it into memory.
For case 4>
same as case 2.
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to call open(), then mmap() it into executable memory.
b> if both user space and kernel need to call AT_CHECK, the faccessat seems to be a better place for AT_CHECK, e.g. kernel can call do_faccessat(AT_CHECK) and userspace can call faccessat(). This will avoid complicating the execveat() code path.
What do you think ?
Thanks
-Jeff
Quoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 4 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.
If there's a way to do this without running executable code that had to pass a previous execveat() check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforce execveat() is a trivial way to do it).
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Quoted 9 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.What do you think ?
Thanks
-Jeff
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 10 lines · Steve Dower
Quoted 4 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).
Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 5 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Quoted 5 lines
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Circling back to my previous comment (did that ever get noticed?), I don’t think this is quite right:
https://lore.kernel.org/all/CALCETrWYu=PYJSgyJ-vaa+3BGAry8Jo8xErZLiGR3U5h6+U0tA@mail.gmail.com/
On a basic system configuration, a given path either may or may not be executed. And maybe that path has some integrity check (dm-verity, etc). So the kernel should tell the interpreter/loader whether the target may be executed. All fine.
But I think the more complex cases are more interesting, and the
“execute a program” process IS NOT BINARY. An attempt to execute can be rejected outright, or it can be allowed *with a change to creds or security context*. It would be entirely reasonable to have a policy that allows execution of non-integrity-checked files but in a very locked down context only.
So… shouldn’t a patch series to this effect actually support this?
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 19 lines · Andy Lutomirski
Quoted 19 lines · Mickaël Salaün
Quoted 15 lines · Steve Dower
Quoted 5 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Quoted 7 lines
Quoted 5 lines
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Circling back to my previous comment (did that ever get noticed?), I
Yes, I replied to your comments. Did I miss something?
Quoted 9 lines
don’t think this is quite right:
https://lore.kernel.org/all/CALCETrWYu=PYJSgyJ-vaa+3BGAry8Jo8xErZLiGR3U5h6+U0tA@mail.gmail.com/
On a basic system configuration, a given path either may or may not be executed. And maybe that path has some integrity check (dm-verity, etc). So the kernel should tell the interpreter/loader whether the target may be executed. All fine.
Quoted 6 lines
But I think the more complex cases are more interesting, and the
“execute a program” process IS NOT BINARY. An attempt to execute can be rejected outright, or it can be allowed *with a change to creds or security context*. It would be entirely reasonable to have a policy that allows execution of non-integrity-checked files but in a very locked down context only.
I guess you mean to transition to a sandbox when executing an untrusted file. This is a good idea. I talked about role transition in the patch's description:
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to security_bprm_creds_for_exec().
Quoted 3 lines
So… shouldn’t a patch series to this effect actually support this?
This patch series brings the minimal building blocks to have a consistent execution environment. Role transitions for script execution are left to LSMs. For instance, we could extend Landlock to automatically sandbox untrusted scripts.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 28 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines
Quoted 1 line · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 5 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Quoted 3 lines
Quoted 1 line
Circling back to my previous comment (did that ever get noticed?), I
Yes, I replied to your comments. Did I miss something?
I missed that email in the pile, sorry. I’ll reply separately.
Quoted 25 lines
Quoted 10 lines
don’t think this is quite right:
https://lore.kernel.org/all/CALCETrWYu=PYJSgyJ-vaa+3BGAry8Jo8xErZLiGR3U5h6+U0tA@mail.gmail.com/
On a basic system configuration, a given path either may or may not be executed. And maybe that path has some integrity check (dm-verity, etc). So the kernel should tell the interpreter/loader whether the target may be executed. All fine.
Quoted 6 lines
But I think the more complex cases are more interesting, and the “execute a program” process IS NOT BINARY. An attempt to execute can be rejected outright, or it can be allowed *with a change to creds or security context*. It would be entirely reasonable to have a policy that allows execution of non-integrity-checked files but in a very locked down context only.
I guess you mean to transition to a sandbox when executing an untrusted file. This is a good idea. I talked about role transition in the patch's description:
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().
…
Quoted 4 lines
This patch series brings the minimal building blocks to have a consistent execution environment. Role transitions for script execution are left to LSMs. For instance, we could extend Landlock to automatically sandbox untrusted scripts.
I’m not really convinced. There’s more to building an API that enables LSM hooks than merely sticking the hook somewhere in kernel code. It needs to be a defined API. If you call an operation “check”, then people will expect it to check, not to change the caller’s credentials. And people will mess it up in both directions (e.g. callers will call it and then open try to load some library that they should have loaded first, or callers will call it and forget to close fds first.
And there should probably be some interaction with dumpable as well. If I “check” a file for executability, that should not suddenly allow someone to ptrace me?
And callers need to know to exit on failure, not carry on.
More concretely, a runtime that fully opts in to this may well "check" multiple things. For example, if I do:
$ ld.so ~/.local/bin/some_program (i.e. I literally execve ld.so)
then ld.so will load several things:
~/.local/bin/some_program
libc.so
other random DSOs, some of which may well be in my home directory
And for all ld.so knows, some_program is actually an interpreter and will "check" something else. And the LSMs have absolutely no clue what's what. So I think for this to work right, the APIs need to be a lot more expressive and explicit:
check_library(fd to libc.so); <-- does not transition or otherwise drop privs check_transition_main_program(fd to ~/.local/bin/some_program); <-- may drop privs
and if some_program is really an interpreter, then it will do:
check_library(fd to some thing imported by the script); check_transition_main_program(fd to the actual script);
And maybe that takes a parameter that gets run eval-style:
check_unsafe_user_script("actual contents of snippet");
The actual spelling of all this doesn't matter so much. But the user code and the kernel code need to be on the same page as to what the user program is doing and what it's asking the kernel program to do.
--Andy
Jarkko Sakkinen
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 86 lines · Andy Lutomirski
Quoted 28 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines
Quoted 1 line · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 5 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Quoted 3 lines
Quoted 1 line
Circling back to my previous comment (did that ever get noticed?), I
Yes, I replied to your comments. Did I miss something?
I missed that email in the pile, sorry. I’ll reply separately.
Quoted 25 lines
Quoted 10 lines
don’t think this is quite right:
https://lore.kernel.org/all/CALCETrWYu=PYJSgyJ-vaa+3BGAry8Jo8xErZLiGR3U5h6+U0tA@mail.gmail.com/
On a basic system configuration, a given path either may or may not be executed. And maybe that path has some integrity check (dm-verity, etc). So the kernel should tell the interpreter/loader whether the target may be executed. All fine.
Quoted 6 lines
But I think the more complex cases are more interesting, and the “execute a program” process IS NOT BINARY. An attempt to execute can be rejected outright, or it can be allowed *with a change to creds or security context*. It would be entirely reasonable to have a policy that allows execution of non-integrity-checked files but in a very locked down context only.
I guess you mean to transition to a sandbox when executing an untrusted file. This is a good idea. I talked about role transition in the patch's description:
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().…
Quoted 4 lines
This patch series brings the minimal building blocks to have a consistent execution environment. Role transitions for script execution are left to LSMs. For instance, we could extend Landlock to automatically sandbox untrusted scripts.
I’m not really convinced. There’s more to building an API that enables LSM hooks than merely sticking the hook somewhere in kernel code. It needs to be a defined API. If you call an operation “check”, then people will expect it to check, not to change the caller’s credentials. And people will mess it up in both directions (e.g. callers will call it and then open try to load some library that they should have loaded first, or callers will call it and forget to close fds first.
And there should probably be some interaction with dumpable as well. If I “check” a file for executability, that should not suddenly allow someone to ptrace me?
And callers need to know to exit on failure, not carry on.
More concretely, a runtime that fully opts in to this may well "check" multiple things. For example, if I do:
Quoted 7 lines
$ ld.so ~/.local/bin/some_program (i.e. I literally execve ld.so)then ld.so will load several things:
~/.local/bin/some_program
libc.so
other random DSOs, some of which may well be in my home directory
What would really help to comprehend this patch set would be a set of test scripts, preferably something that you can run easily with BuildRoot or similar.
Scripts would demonstrate the use cases for the patch set. Then it would be easier to develop scripts that would underline the corner cases. I would keep all this out of kselftest shenanigans for now.
I feel that the patch set is hovering in abstractions with examples that you cannot execute.
I added the patches to standard test CI hack:
https://codeberg.org/jarkko/linux-tpmdd-test
But after I booted up a kernel I had no idea what to do with it. And all this lenghty discussion makes it even more confusing.
Please find some connection to the real world before sending any new version of this (e.g. via test scripts). I think this should not be pulled before almost anyone doing kernel dev can comprehend the "gist" at least in some reasonable level.
BR, Jarkko
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 95 lines · Jarkko Sakkinen
Quoted 86 lines · Andy Lutomirski
Quoted 28 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines
Quoted 1 line · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 5 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Quoted 3 lines
Quoted 1 line
Circling back to my previous comment (did that ever get noticed?), I
Yes, I replied to your comments. Did I miss something?
I missed that email in the pile, sorry. I’ll reply separately.
Quoted 25 lines
Quoted 10 lines
don’t think this is quite right:
https://lore.kernel.org/all/CALCETrWYu=PYJSgyJ-vaa+3BGAry8Jo8xErZLiGR3U5h6+U0tA@mail.gmail.com/
On a basic system configuration, a given path either may or may not be executed. And maybe that path has some integrity check (dm-verity, etc). So the kernel should tell the interpreter/loader whether the target may be executed. All fine.
Quoted 6 lines
But I think the more complex cases are more interesting, and the “execute a program” process IS NOT BINARY. An attempt to execute can be rejected outright, or it can be allowed *with a change to creds or security context*. It would be entirely reasonable to have a policy that allows execution of non-integrity-checked files but in a very locked down context only.
I guess you mean to transition to a sandbox when executing an untrusted file. This is a good idea. I talked about role transition in the patch's description:
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().…
Quoted 4 lines
This patch series brings the minimal building blocks to have a consistent execution environment. Role transitions for script execution are left to LSMs. For instance, we could extend Landlock to automatically sandbox untrusted scripts.
I’m not really convinced. There’s more to building an API that enables LSM hooks than merely sticking the hook somewhere in kernel code. It needs to be a defined API. If you call an operation “check”, then people will expect it to check, not to change the caller’s credentials. And people will mess it up in both directions (e.g. callers will call it and then open try to load some library that they should have loaded first, or callers will call it and forget to close fds first.
And there should probably be some interaction with dumpable as well. If I “check” a file for executability, that should not suddenly allow someone to ptrace me?
And callers need to know to exit on failure, not carry on.
More concretely, a runtime that fully opts in to this may well "check" multiple things. For example, if I do:
Quoted 7 lines
$ ld.so ~/.local/bin/some_program (i.e. I literally execve ld.so)then ld.so will load several things:
~/.local/bin/some_program
libc.so
other random DSOs, some of which may well be in my home directory
Quoted 7 lines
What would really help to comprehend this patch set would be a set of test scripts, preferably something that you can run easily with BuildRoot or similar.
Scripts would demonstrate the use cases for the patch set. Then it would be easier to develop scripts that would underline the corner cases. I would keep all this out of kselftest shenanigans for now.
I'll include a toy script interpreter with the next patch series. This one was an RFC.
Quoted 10 lines
I feel that the patch set is hovering in abstractions with examples that you cannot execute.
I added the patches to standard test CI hack:
https://codeberg.org/jarkko/linux-tpmdd-test
But after I booted up a kernel I had no idea what to do with it. And all this lenghty discussion makes it even more confusing.
You can run the tests in the CI.
Quoted 5 lines
Please find some connection to the real world before sending any new version of this (e.g. via test scripts). I think this should not be pulled before almost anyone doing kernel dev can comprehend the "gist" at least in some reasonable level.
You'll find in this patch series (cover letter, patch description, and comments) connection to the real world. :)
The next patch series should take into account the current discussions.
Quoted 2 lines
BR, Jarkko
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Andy LutomirskiQuoted 108 lines · Andy Lutomirski
Quoted 28 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines
Quoted 1 line · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 5 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Quoted 3 lines
Quoted 1 line
Circling back to my previous comment (did that ever get noticed?), I
Yes, I replied to your comments. Did I miss something?
I missed that email in the pile, sorry. I’ll reply separately.
Quoted 25 lines
Quoted 10 lines
don’t think this is quite right:
https://lore.kernel.org/all/CALCETrWYu=PYJSgyJ-vaa+3BGAry8Jo8xErZLiGR3U5h6+U0tA@mail.gmail.com/
On a basic system configuration, a given path either may or may not be executed. And maybe that path has some integrity check (dm-verity, etc). So the kernel should tell the interpreter/loader whether the target may be executed. All fine.
Quoted 6 lines
But I think the more complex cases are more interesting, and the “execute a program” process IS NOT BINARY. An attempt to execute can be rejected outright, or it can be allowed *with a change to creds or security context*. It would be entirely reasonable to have a policy that allows execution of non-integrity-checked files but in a very locked down context only.
I guess you mean to transition to a sandbox when executing an untrusted file. This is a good idea. I talked about role transition in the patch's description:
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().…
Quoted 4 lines
This patch series brings the minimal building blocks to have a consistent execution environment. Role transitions for script execution are left to LSMs. For instance, we could extend Landlock to automatically sandbox untrusted scripts.
I’m not really convinced. There’s more to building an API that enables LSM hooks than merely sticking the hook somewhere in kernel code. It needs to be a defined API. If you call an operation “check”, then people will expect it to check, not to change the caller’s credentials. And people will mess it up in both directions (e.g. callers will call it and then open try to load some library that they should have loaded first, or callers will call it and forget to close fds first.
And there should probably be some interaction with dumpable as well. If I “check” a file for executability, that should not suddenly allow someone to ptrace me?
And callers need to know to exit on failure, not carry on.
More concretely, a runtime that fully opts in to this may well "check" multiple things. For example, if I do:
$ ld.so ~/.local/bin/some_program (i.e. I literally execve ld.so)then ld.so will load several things:
~/.local/bin/some_program
libc.so
other random DSOs, some of which may well be in my home directoryAnd for all ld.so knows,
some_programis actually an interpreter and will "check" something else. And the LSMs have absolutely no clue what's what. So I think for this to work right, the APIs need to be a lot more expressive and explicit:
check_library(fd to libc.so); <-- does not transition or otherwise drop privscheck_transition_main_program(fd to ~/.local/bin/some_program); <-- may drop privsand if
some_programis really an interpreter, then it will do:
check_library(fd to some thing imported by the script);check_transition_main_program(fd to the actual script);
Quoted 7 lines
And maybe that takes a parameter that gets run eval-style:
check_unsafe_user_script("actual contents of snippet");The actual spelling of all this doesn't matter so much. But the user code and the kernel code need to be on the same page as to what the user program is doing and what it's asking the kernel program to do.
I agree. I'll remove any references to "role transition". This kind of feature should come with something like getpeercon/setexeccon(3).
Quoted 3 lines
--Andy
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 12 lines · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 4 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).
Quoted 6 lines
Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
We will want to define what is considered as "arbitrary code is running"
Using an example of ROP, attackers change the return address in stack, e.g. direct the execution flow to a gauge to call "ld.so /tmp/a.out", do you consider "arbitrary code is running" when stack is overwritten ? or after execve() is called.
If it is later, this patch can prevent "ld.so /tmp/a.out".
Quoted 5 lines
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Even trusted executable can have a bug.
I'm thinking in the context of ChromeOS, where all its system services are from trusted partitions, and legit code won't load .so from a non-exec mount. But we want to sandbox those services, so even under some kind of ROP attack, the service still won't be able to load .so from /tmp. Of course, if an attacker can already write arbitrary length of data into the stack, it is probably already a game over.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 19 lines · Jeff Xu
Quoted 12 lines · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 4 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 6 lines
Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Quoted 6 lines
We will want to define what is considered as "arbitrary code is running"
Using an example of ROP, attackers change the return address in stack, e.g. direct the execution flow to a gauge to call "ld.so
/tmp/a.out", do you consider "arbitrary code is running" when stack is overwritten ? or afterexecve()is called.
Yes, ROP is arbitrary code execution (which can be mitigated with CFI). ROP could be enough to interpret custom commands and create a small interpreter/VM.
Quoted 9 lines
If it is later, this patch can prevent "ld.so
/tmp/a.out".Quoted 5 lines
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Even trusted executable can have a bug.
Definitely, but this patch series is dedicated to script execution control.
Quoted 8 lines
I'm thinking in the context of ChromeOS, where all its system services are from trusted partitions, and legit code won't load .so from a non-exec mount. But we want to sandbox those services, so even under some kind of ROP attack, the service still won't be able to load .so from /tmp. Of course, if an attacker can already write arbitrary length of data into the stack, it is probably already a game over.
OK, you want to tie executable file permission to mmap. That makes sense if you have a consistent execution model. This can be enforced by LSMs. Contrary to script interpretation which is a full user space implementation (and then controlled by user space), mmap restrictions should indeed be enforced by the kernel.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 54 lines · Mickaël Salaün
Quoted 19 lines · Jeff Xu
Quoted 12 lines · Mickaël Salaün
Quoted 10 lines · Steve Dower
Quoted 4 lines · Jeff Xu
Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.If there's a way to do this without running executable code that had to pass a previous
execveat()check, then yeah, it's not effective (e.g. a Python interpreter that *doesn't* enforceexecveat()is a trivial way to do it).Quoted 6 lines
Quoted 5 lines
Once arbitrary code is running, all bets are off. So long as all arbitrary code is being checked itself, it's allowed to do things that would bypass later checks (and it's up to whoever audited it in the first place to prevent this by not giving it the special mark that allows it to pass the check).
Quoted 6 lines
We will want to define what is considered as "arbitrary code is running"
Using an example of ROP, attackers change the return address in stack, e.g. direct the execution flow to a gauge to call "ld.so
/tmp/a.out", do you consider "arbitrary code is running" when stack is overwritten ? or afterexecve()is called.Yes, ROP is arbitrary code execution (which can be mitigated with CFI). ROP could be enough to interpret custom commands and create a small interpreter/VM.
Quoted 9 lines
If it is later, this patch can prevent "ld.so
/tmp/a.out".Quoted 5 lines
Exactly. As explained in the patches, one crucial prerequisite is that the executable code is trusted, and the system must provide integrity guarantees. We cannot do anything without that. This patches series is a building block to fix a blind spot on Linux systems to be able to fully control executability.
Even trusted executable can have a bug.
Definitely, but this patch series is dedicated to script execution control.
Quoted 8 lines
I'm thinking in the context of ChromeOS, where all its system services are from trusted partitions, and legit code won't load .so from a non-exec mount. But we want to sandbox those services, so even under some kind of ROP attack, the service still won't be able to load .so from /tmp. Of course, if an attacker can already write arbitrary length of data into the stack, it is probably already a game over.
Quoted 5 lines
OK, you want to tie executable file permission to mmap. That makes sense if you have a consistent execution model. This can be enforced by LSMs. Contrary to script interpretation which is a full user space implementation (and then controlled by user space), mmap restrictions should indeed be enforced by the kernel.
Ya, that is what I meant. it can be out of scope for this patch. Indeed, as you point out, this patch is dedicated to script execution control, and fixing ld.so /tmp/a.out is an extra bonus in addition to script.
Thanks
-Jeff
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Jeff XuQuoted 9 lines · Jeff Xu
Quoted 8 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].
Quoted 8 lines
Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),
"config file" that contains executable code.
Quoted 8 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
If we want other checks, we'll need to clearly define their semantic and align with the kernel. faccessat2(2) might be used to check other file properties, but the executable property is not only defined by the file attributes.
Quoted 16 lines
--------------------------
now looked at user cases (focus on elf for now)1> ld.so
/tmp/a.out,/tmp/a.outis on non-exec mount
dynamic linker will first callexecveat(fd, AT_CHECK)then execveat(fd)2> execve(/usr/bin/some.out) and some.out has dependency on
/tmp/a.so/usr/bin/some.outwill passAT_CHECK3> execve(usr/bin/some.out) and some.out uses custom
/tmp/ld.so/usr/bin/some.outwill passAT_CHECK, however, it uses a custom/tmp/ld.so(I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, andbinfmt_elf.callocate memory for ld.so during execveat call)
Quoted 7 lines
4> dlopen(/tmp/a.so)
I assume dynamic linker will callexecveat(AT_CHECK), before map a.so into memory.For case 1>
Alternative solution: BecauseAT_CHECKis always called, I think we can avoid the firstAT_CHECKcall, and check during execveat(fd),
There is no need to use AT_CHECK if we're going to call execveat(2) on the same file descriptor. By design, AT_CHECK is implicit for any execve(2).
Quoted 3 lines
this means the kernel will enforce
SECBIT_EXEC_RESTRICT_FILE= 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesfulexecveat()case.
As long as user space uses the same file descriptor, there is no TOCTOU.
SECBIT_EXEC_RESTRICT_FILE only makes sense for user space: it defines the user space security policy. The kernel already enforces the same security policy for any execve(2), whatever are the calling process's securebits.
Quoted 6 lines
For case 2>
dynamic linker will callexecve(AT_CHECK), then mmap(fd) into memory. However, the process can all open thenmmap()directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.
Which process? What do you mean by "can all open then mmap() directly"?
In this context the dynamic linker (like its parent processes) is trusted (guaranteed by the system).
For case 2, the dynamic linker must check with AT_CHECK all files that will be mapped, which include /usr/bin/some.out and /tmp/a.so
Quoted 6 lines
Alternative solution:
dynamic linker callAT_CHECKfor each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time ofmmap(fd, executable memory)and enforceSECBIT_EXEC_RESTRICT_FILE= 1
The idea with AT_CHECK is that there is no kernel side effect, no extra kernel state, and the semantic is the same as with execve(2).
This also enables us to check file's executable permission and ignore it, which is useful in a "permissive mode" when preparing for a migration without breaking a system, or to do extra integrity checks. BTW, this use case would also be more complex with a new openat2(2) flag like the original O_MAYEXEC.
Quoted 3 lines
Alternative solution 2:
a new syscall to load the .so and enforce theAT_CHECKin kernel
A new syscall would be overkill for this feature. Please see Linus's comment.
Quoted 3 lines
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ),
How seccomp could create anonymous memory in user space? seccomp filters should be treated (and checked with AT_CHECK) as executable code anyway.
Quoted 2 lines
unless the user space can harden the creation of executable anonymous memory in some way.
User space is already in charge of mmapping its own memory. I don't see what is missing.
Quoted 4 lines
For case 3>
I thinkbinfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.
All ELF dependencies are opened and checked with open_exec(), which perform the main executability checks (with the __FMODE_EXEC flag). Did I miss something?
However, we must be careful with programs using the (deprecated) uselib(2). They should also check with AT_CHECK because this syscall opens the shared library without __FMODE_EXEC (similar to a simple file open). See
https://lore.kernel.org/all/CAHk-=wiUwRG7LuR=z5sbkFVGQh+7qVB6_1NM0Ny9SVNL1Un4Sw@mail.gmail.com/
Quoted 8 lines
For case 4>
same as case 2.Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.
As Steve explained (and is also explained in the patches), it is trivial if the attacker can already execute its own code, which is too late to enforce any script execution control.
Quoted 4 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.
A previous version of this patches series already patched faccessat(2), but this is not the right place. faccessat2(2) is dedicated to check file permissions, not executability (e.g. with mount's noexec).
Quoted 2 lines
What do you think ?
I think there are some misunderstandings. Please let me know if it's clearer now.
Quoted 176 lines
Thanks
-JeffQuoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.
Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Quoted 9 lines
Quoted 8 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.
Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope of execveat(AT_CHECK)
Quoted 5 lines
If we want other checks, we'll need to clearly define their semantic and align with the kernel. faccessat2(2) might be used to check other file properties, but the executable property is not only defined by the file attributes.
Agreed.
Quoted 20 lines
Quoted 20 lines
--------------------------
now looked at user cases (focus on elf for now)1> ld.so
/tmp/a.out,/tmp/a.outis on non-exec mount
dynamic linker will first callexecveat(fd, AT_CHECK)then execveat(fd)2> execve(/usr/bin/some.out) and some.out has dependency on
/tmp/a.so/usr/bin/some.outwill passAT_CHECK3> execve(usr/bin/some.out) and some.out uses custom
/tmp/ld.so/usr/bin/some.outwill passAT_CHECK, however, it uses a custom/tmp/ld.so(I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, andbinfmt_elf.callocate memory for ld.so during execveat call)4> dlopen(/tmp/a.so)
I assume dynamic linker will callexecveat(AT_CHECK), before map a.so into memory.
Quoted 8 lines
Quoted 3 lines
For case 1>
Alternative solution: BecauseAT_CHECKis always called, I think we can avoid the firstAT_CHECKcall, and check during execveat(fd),There is no need to use
AT_CHECKif we're going to call execveat(2) on the same file descriptor. By design,AT_CHECKis implicit for any execve(2).
Yes. I realized I was wrong to say that ld.so will call execve() for /tmp/a.out, there is no execve() call, otherwise it would have been blocked already today.
The ld.so will mmap the /tmp/a.out directly. So case 1 is no different than case 2 and 4. ( the elf objects are mapped to memory by dynamic linker.)
I'm not familiar with dynamic linker, Florian is on this thread, and can help to correct me if my guess is wrong.
Quoted 18 lines
Quoted 3 lines
this means the kernel will enforce
SECBIT_EXEC_RESTRICT_FILE= 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesfulexecveat()case.As long as user space uses the same file descriptor, there is no TOCTOU.
SECBIT_EXEC_RESTRICT_FILEonly makes sense for user space: it defines the user space security policy. The kernel already enforces the same security policy for any execve(2), whatever are the calling process's securebits.Quoted 6 lines
For case 2>
dynamic linker will callexecve(AT_CHECK), then mmap(fd) into memory. However, the process can all open thenmmap()directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.
Quoted 8 lines
Which process? What do you mean by "can all open then
mmap()directly"?In this context the dynamic linker (like its parent processes) is trusted (guaranteed by the system).
For case 2, the dynamic linker must check with
AT_CHECKall files that will be mapped, which include/usr/bin/some.outand/tmp/a.so
My point is that the process can work around this by mmap() the file directly.
Quoted 7 lines
Quoted 6 lines
Alternative solution:
dynamic linker callAT_CHECKfor each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time ofmmap(fd, executable memory)and enforceSECBIT_EXEC_RESTRICT_FILE= 1
Quoted 6 lines
The idea with
AT_CHECKis that there is no kernel side effect, no extra kernel state, and the semantic is the same as with execve(2).This also enables us to check file's executable permission and ignore it, which is useful in a "permissive mode" when preparing for a migration without breaking a system, or to do extra integrity checks.
For preparing a migration (detect all violations), this is useful. But as a defense mechanism (SECBIT_EXEC_RESTRICT_FILE = 1) , this seems to be weak, at least for elf loading case.
Quoted 10 lines
BTW, this use case would also be more complex with a new openat2(2) flag like the original
O_MAYEXEC.Quoted 3 lines
Alternative solution 2:
a new syscall to load the .so and enforce theAT_CHECKin kernelA new syscall would be overkill for this feature. Please see Linus's comment.
maybe, I was thinking on how to prevent "/tmp/a.o" from getting mmap() to executable memory.
Quoted 15 lines
Quoted 3 lines
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ),
How seccomp could create anonymous memory in user space? seccomp filters should be treated (and checked with
AT_CHECK) as executable code anyway.Quoted 2 lines
unless the user space can harden the creation of executable anonymous memory in some way.
User space is already in charge of mmapping its own memory. I don't see what is missing.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
For case 3>
I thinkbinfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?
I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 29 lines
However, we must be careful with programs using the (deprecated) uselib(2). They should also check with
AT_CHECKbecause this syscall opens the shared library without__FMODE_EXEC(similar to a simple file open). See
https://lore.kernel.org/all/CAHk-=wiUwRG7LuR=z5sbkFVGQh+7qVB6_1NM0Ny9SVNL1Un4Sw@mail.gmail.com/Quoted 8 lines
For case 4>
same as case 2.Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.As Steve explained (and is also explained in the patches), it is trivial if the attacker can already execute its own code, which is too late to enforce any script execution control.
Quoted 4 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.A previous version of this patches series already patched faccessat(2), but this is not the right place. faccessat2(2) is dedicated to check file permissions, not executability (e.g. with mount's noexec).
Quoted 1 line
Quoted 5 lines
Quoted 1 line
What do you think ?
I think there are some misunderstandings. Please let me know if it's clearer now.
I'm still not sure about the user case for dynamic linker (elf loading) case. Maybe this patch is more suitable for scripts? A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from SECBIT_EXEC_RESTRICT_FILE = 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.
Quoted 176 lines
Quoted 176 lines
Thanks
-JeffQuoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 18 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Because seccomp filters alter syscalls, they are similar to code injection.
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)
The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
It might makes sense to follow the kernel and interpreter semantic: if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured with binfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable with binfmt_misc. So, again, where to draw the line?
I'd recommend to think about interaction with the outside, through function calls, IPCs, syscalls... For instance, "running" an image should not lead to reading or writing to arbitrary files, or accessing the network, but in practice it is legitimate for some file formats... PostScript is a programming language, but mostly used to draw pictures. So, again, where to draw the line?
We should follow the principle of least astonishment. What most users would expect? This should follow the *common usage* of executable files. At the end, the script interpreters will be patched by security folks for security reasons. I think the right question to ask should
be: could this file format be (ab)used to leak or modify arbitrary files, or to perform arbitrary syscalls? If the answer is yes, then it should be checked for executability. Of course, this excludes bugs exploited in the file format parser.
I'll extend the next patch series with this rationale.
Quoted 65 lines
Quoted 6 lines
If we want other checks, we'll need to clearly define their semantic and align with the kernel. faccessat2(2) might be used to check other file properties, but the executable property is not only defined by the file attributes.
Agreed.
Quoted 20 lines
Quoted 20 lines
--------------------------
now looked at user cases (focus on elf for now)1> ld.so
/tmp/a.out,/tmp/a.outis on non-exec mount
dynamic linker will first callexecveat(fd, AT_CHECK)then execveat(fd)2> execve(/usr/bin/some.out) and some.out has dependency on
/tmp/a.so/usr/bin/some.outwill passAT_CHECK3> execve(usr/bin/some.out) and some.out uses custom
/tmp/ld.so/usr/bin/some.outwill passAT_CHECK, however, it uses a custom/tmp/ld.so(I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, andbinfmt_elf.callocate memory for ld.so during execveat call)4> dlopen(/tmp/a.so)
I assume dynamic linker will callexecveat(AT_CHECK), before map a.so into memory.Quoted 8 lines
Quoted 3 lines
For case 1>
Alternative solution: BecauseAT_CHECKis always called, I think we can avoid the firstAT_CHECKcall, and check during execveat(fd),There is no need to use
AT_CHECKif we're going to call execveat(2) on the same file descriptor. By design,AT_CHECKis implicit for any execve(2).Yes. I realized I was wrong to say that ld.so will call
execve()for/tmp/a.out, there is noexecve()call, otherwise it would have been blocked already today.
The ld.so will mmap the/tmp/a.outdirectly. So case 1 is no different than case 2 and 4. ( the elf objects are mapped to memory by dynamic linker.)
I'm not familiar with dynamic linker, Florian is on this thread, and can help to correct me if my guess is wrong.Quoted 20 lines
Quoted 3 lines
this means the kernel will enforce
SECBIT_EXEC_RESTRICT_FILE= 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesfulexecveat()case.As long as user space uses the same file descriptor, there is no TOCTOU.
SECBIT_EXEC_RESTRICT_FILEonly makes sense for user space: it defines the user space security policy. The kernel already enforces the same security policy for any execve(2), whatever are the calling process's securebits.Quoted 6 lines
For case 2>
dynamic linker will callexecve(AT_CHECK), then mmap(fd) into memory. However, the process can all open thenmmap()directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.Which process? What do you mean by "can all open then
mmap()directly"?
Quoted 7 lines
Quoted 6 lines
In this context the dynamic linker (like its parent processes) is trusted (guaranteed by the system).
For case 2, the dynamic linker must check with
AT_CHECKall files that will be mapped, which include/usr/bin/some.outand/tmp/a.soMy point is that the process can work around this by
mmap()the file directly.
Yes, see my answer in the other email. The process is trusted.
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Alternative solution:
dynamic linker callAT_CHECKfor each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time ofmmap(fd, executable memory)and enforceSECBIT_EXEC_RESTRICT_FILE= 1The idea with
AT_CHECKis that there is no kernel side effect, no extra kernel state, and the semantic is the same as with execve(2).
Quoted 6 lines
Quoted 3 lines
This also enables us to check file's executable permission and ignore it, which is useful in a "permissive mode" when preparing for a migration without breaking a system, or to do extra integrity checks.
For preparing a migration (detect all violations), this is useful. But as a defense mechanism (
SECBIT_EXEC_RESTRICT_FILE= 1) , this seems to be weak, at least for elf loading case.
We could add more restrictions, but that is outside the scope of this patch series.
Quoted 5 lines
Quoted 5 lines
BTW, this use case would also be more complex with a new openat2(2) flag like the original
O_MAYEXEC.Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Alternative solution 2:
a new syscall to load the .so and enforce theAT_CHECKin kernelA new syscall would be overkill for this feature. Please see Linus's comment.
maybe, I was thinking on how to prevent "
/tmp/a.o" from gettingmmap()to executable memory.
OK, this is another story.
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ),
How seccomp could create anonymous memory in user space? seccomp filters should be treated (and checked with
AT_CHECK) as executable code anyway.Quoted 2 lines
unless the user space can harden the creation of executable anonymous memory in some way.
User space is already in charge of mmapping its own memory. I don't see what is missing.
Quoted 4 lines
For case 3>
I thinkbinfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.
Quoted 8 lines
Quoted 4 lines
All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
The kernel calls open_exec() on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.
Quoted 30 lines
Quoted 30 lines
However, we must be careful with programs using the (deprecated) uselib(2). They should also check with
AT_CHECKbecause this syscall opens the shared library without__FMODE_EXEC(similar to a simple file open). See
https://lore.kernel.org/all/CAHk-=wiUwRG7LuR=z5sbkFVGQh+7qVB6_1NM0Ny9SVNL1Un4Sw@mail.gmail.com/Quoted 8 lines
For case 4>
same as case 2.Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.As Steve explained (and is also explained in the patches), it is trivial if the attacker can already execute its own code, which is too late to enforce any script execution control.
Quoted 4 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.A previous version of this patches series already patched faccessat(2), but this is not the right place. faccessat2(2) is dedicated to check file permissions, not executability (e.g. with mount's noexec).
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What do you think ?
I think there are some misunderstandings. Please let me know if it's clearer now.
I'm still not sure about the user case for dynamic linker (elf loading) case. Maybe this patch is more suitable for scripts?
It's suitable for both, but we could add more restriction on mmap with an (existing) LSM. The kernel already checks for mount's noexec when mapping a file, but not for the file permission, which is OK because it could be bypassed by coping the content of the file and mprotecting it anyway. For a consistent memory execution control, all memory mapping need to be restricted, which is out of scope for this patch series.
Quoted 4 lines
A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from
SECBIT_EXEC_RESTRICT_FILE= 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.
I explained that in the patches and in the description of these new securebits. Please point which part is not clear. The full threat model is simple: the TCB includes the kernel and system's files, which are integrity-protected, but we don't trust arbitrary data/scripts that can be written to user-owned files or directly provided to script interpreters. As for the ptrace restrictions, the dynamic linker restrictions helps to avoid trivial bypasses (e.g. with LD_PRELOAD) with consistent executability checks.
Quoted 178 lines
Quoted 177 lines
Quoted 177 lines
Thanks
-JeffQuoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
James Bottomley
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 4 lines · Mickaël Salaün
Quoted 3 lines · Jeff Xu
Quoted 1 line · Mickaël Salaün
On Tue, Jul 16, 2024 at 11:33:55PM -0700, Jeff Xu wrote:
[...]
Quoted 20 lines
Quoted 14 lines
Quoted 9 lines
Quoted 8 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more. We will want to clearly define the scope of
execveat(AT_CHECK)
Quoted 3 lines
The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
Having a way to have config files part of the trusted envelope, either by signing or measurement would be really useful. The current standard distro IMA deployment is signed executables, but not signed config because it's hard to construct a policy that doesn't force the signing of too many extraneous files (and files which might change often).
Quoted 7 lines
It might makes sense to follow the kernel and interpreter semantic:
if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured withbinfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable with binfmt_misc. So, again, where to draw the line?
Possibly by making open for config an indication executables can give? I'm not advocating doing it in this patch, but if we had an open for config indication, the LSMs could do much finer grained policy, especially if they knew which executable was trying to open the config file. It would allow things like an IMA policy saying if a signed executable is opening a config file, then that file must also be signed.
James
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 44 lines · James Bottomley
Quoted 4 lines · Mickaël Salaün
Quoted 3 lines · Jeff Xu
Quoted 1 line · Mickaël Salaün
On Tue, Jul 16, 2024 at 11:33:55PM -0700, Jeff Xu wrote:
[...]
Quoted 20 lines
Quoted 14 lines
Quoted 9 lines
Quoted 8 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more. We will want to clearly define the scope of
execveat(AT_CHECK)Quoted 3 lines
The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
Having a way to have config files part of the trusted envelope, either by signing or measurement would be really useful. The current standard distro IMA deployment is signed executables, but not signed config because it's hard to construct a policy that doesn't force the signing of too many extraneous files (and files which might change often).
Quoted 7 lines
It might makes sense to follow the kernel and interpreter semantic:
if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured withbinfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable with binfmt_misc. So, again, where to draw the line?
Quoted 7 lines
Possibly by making open for config an indication executables can give? I'm not advocating doing it in this patch, but if we had an open for config indication, the LSMs could do much finer grained policy, especially if they knew which executable was trying to open the config file. It would allow things like an IMA policy saying if a signed executable is opening a config file, then that file must also be signed.
Checking configuration could be a next step, but not with this patch series. FYI, the previous version was a (too) generic syscall:
https://lore.kernel.org/all/[email protected]/
One of the main concern was alignment with kernel semantic. For now, let's focus on script execution control.
Quoted 3 lines
James
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 23 lines · Mickaël Salaün
Quoted 21 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Because seccomp filters alter syscalls, they are similar to code injection.
that makes sense.
Quoted 118 lines
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
It might makes sense to follow the kernel and interpreter semantic: if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured with
binfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable withbinfmt_misc. So, again, where to draw the line?I'd recommend to think about interaction with the outside, through function calls, IPCs, syscalls... For instance, "running" an image should not lead to reading or writing to arbitrary files, or accessing the network, but in practice it is legitimate for some file formats... PostScript is a programming language, but mostly used to draw pictures. So, again, where to draw the line?
We should follow the principle of least astonishment. What most users would expect? This should follow the *common usage* of executable files. At the end, the script interpreters will be patched by security folks for security reasons. I think the right question to ask should
be: could this file format be (ab)used to leak or modify arbitrary files, or to perform arbitrary syscalls? If the answer is yes, then it should be checked for executability. Of course, this excludes bugs exploited in the file format parser.I'll extend the next patch series with this rationale.
Quoted 68 lines
Quoted 6 lines
If we want other checks, we'll need to clearly define their semantic and align with the kernel. faccessat2(2) might be used to check other file properties, but the executable property is not only defined by the file attributes.
Agreed.
Quoted 20 lines
Quoted 20 lines
--------------------------
now looked at user cases (focus on elf for now)1> ld.so
/tmp/a.out,/tmp/a.outis on non-exec mount
dynamic linker will first callexecveat(fd, AT_CHECK)then execveat(fd)2> execve(/usr/bin/some.out) and some.out has dependency on
/tmp/a.so/usr/bin/some.outwill passAT_CHECK3> execve(usr/bin/some.out) and some.out uses custom
/tmp/ld.so/usr/bin/some.outwill passAT_CHECK, however, it uses a custom/tmp/ld.so(I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, andbinfmt_elf.callocate memory for ld.so during execveat call)4> dlopen(/tmp/a.so)
I assume dynamic linker will callexecveat(AT_CHECK), before map a.so into memory.Quoted 8 lines
Quoted 3 lines
For case 1>
Alternative solution: BecauseAT_CHECKis always called, I think we can avoid the firstAT_CHECKcall, and check during execveat(fd),There is no need to use
AT_CHECKif we're going to call execveat(2) on the same file descriptor. By design,AT_CHECKis implicit for any execve(2).Yes. I realized I was wrong to say that ld.so will call
execve()for/tmp/a.out, there is noexecve()call, otherwise it would have been blocked already today.
The ld.so will mmap the/tmp/a.outdirectly. So case 1 is no different than case 2 and 4. ( the elf objects are mapped to memory by dynamic linker.)
I'm not familiar with dynamic linker, Florian is on this thread, and can help to correct me if my guess is wrong.Quoted 23 lines
Quoted 3 lines
this means the kernel will enforce
SECBIT_EXEC_RESTRICT_FILE= 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesfulexecveat()case.As long as user space uses the same file descriptor, there is no TOCTOU.
SECBIT_EXEC_RESTRICT_FILEonly makes sense for user space: it defines the user space security policy. The kernel already enforces the same security policy for any execve(2), whatever are the calling process's securebits.Quoted 6 lines
For case 2>
dynamic linker will callexecve(AT_CHECK), then mmap(fd) into memory. However, the process can all open thenmmap()directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.Which process? What do you mean by "can all open then
mmap()directly"?In this context the dynamic linker (like its parent processes) is trusted (guaranteed by the system).
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
For case 2, the dynamic linker must check with
AT_CHECKall files that will be mapped, which include/usr/bin/some.outand/tmp/a.soMy point is that the process can work around this by
mmap()the file directly.Yes, see my answer in the other email. The process is trusted.
OK. Let's agree that this is out of scope for this patch series.
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 7 lines
Alternative solution:
dynamic linker callAT_CHECKfor each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time ofmmap(fd, executable memory)and enforceSECBIT_EXEC_RESTRICT_FILE= 1The idea with
AT_CHECKis that there is no kernel side effect, no extra kernel state, and the semantic is the same as with execve(2).This also enables us to check file's executable permission and ignore it, which is useful in a "permissive mode" when preparing for a migration without breaking a system, or to do extra integrity checks.
Quoted 7 lines
Quoted 3 lines
For preparing a migration (detect all violations), this is useful. But as a defense mechanism (
SECBIT_EXEC_RESTRICT_FILE= 1) , this seems to be weak, at least for elf loading case.We could add more restrictions, but that is outside the scope of this patch series.
Agreed.
Quoted 40 lines
Quoted 5 lines
Quoted 5 lines
BTW, this use case would also be more complex with a new openat2(2) flag like the original
O_MAYEXEC.Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Alternative solution 2:
a new syscall to load the .so and enforce theAT_CHECKin kernelA new syscall would be overkill for this feature. Please see Linus's comment.
maybe, I was thinking on how to prevent "
/tmp/a.o" from gettingmmap()to executable memory.OK, this is another story.
Quoted 24 lines
Quoted 24 lines
Quoted 4 lines
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ),
How seccomp could create anonymous memory in user space? seccomp filters should be treated (and checked with
AT_CHECK) as executable code anyway.Quoted 2 lines
unless the user space can harden the creation of executable anonymous memory in some way.
User space is already in charge of mmapping its own memory. I don't see what is missing.
Quoted 4 lines
For case 3>
I thinkbinfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?
Quoted 8 lines
Quoted 4 lines
I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.
This might not be entirely true. iiuc, kernel calls open_exec for open_exec for interpreter, but not all its dependency (e.g. libc.so.6) load_elf_binary() {
interpreter = open_exec(elf_interpreter);
}
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
- security_bprm_creds(a.out)
- open the exec(ld.so)
- call open_exec() for interruptor (ld.so)
- call execveat(AT_CHECK, ld.so) <-- do we want ld.so going through
the same check and code path as libc.so below ?
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so call execveat(AT_CHECK,libc.so) <-- proposed by this patch,
require dynamic linker change.
- ld.so mmap(libc.so,rx)
Quoted 38 lines
Quoted 30 lines
Quoted 30 lines
However, we must be careful with programs using the (deprecated) uselib(2). They should also check with
AT_CHECKbecause this syscall opens the shared library without__FMODE_EXEC(similar to a simple file open). See
https://lore.kernel.org/all/CAHk-=wiUwRG7LuR=z5sbkFVGQh+7qVB6_1NM0Ny9SVNL1Un4Sw@mail.gmail.com/Quoted 8 lines
For case 4>
same as case 2.Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.As Steve explained (and is also explained in the patches), it is trivial if the attacker can already execute its own code, which is too late to enforce any script execution control.
Quoted 4 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.A previous version of this patches series already patched faccessat(2), but this is not the right place. faccessat2(2) is dedicated to check file permissions, not executability (e.g. with mount's noexec).
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What do you think ?
I think there are some misunderstandings. Please let me know if it's clearer now.
I'm still not sure about the user case for dynamic linker (elf loading) case. Maybe this patch is more suitable for scripts?
Quoted 8 lines
It's suitable for both, but we could add more restriction on mmap with an (existing) LSM. The kernel already checks for mount's noexec when mapping a file, but not for the file permission, which is OK because it could be bypassed by coping the content of the file and mprotecting it anyway. For a consistent memory execution control, all memory mapping need to be restricted, which is out of scope for this patch series.
Ok.
Quoted 5 lines
Quoted 4 lines
A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from
SECBIT_EXEC_RESTRICT_FILE= 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.
Quoted 9 lines
I explained that in the patches and in the description of these new securebits. Please point which part is not clear. The full threat model is simple: the TCB includes the kernel and system's files, which are integrity-protected, but we don't trust arbitrary data/scripts that can be written to user-owned files or directly provided to script interpreters. As for the ptrace restrictions, the dynamic linker restrictions helps to avoid trivial bypasses (e.g. with
LD_PRELOAD) with consistent executability checks.
On elf loading case, I'm clear after your last email. However, I'm not sure if everyone else follows, I will try to summarize here:
- Problem: ld.so /tmp/a.out will happily pass, even /tmp/a.out is mounted as non-exec.
Solution: ld.so call execveat(AT_CHECK) for a.out before mmap a.out
into memory.
- Problem: a poorly built application (a.out) can have a dependency on /tmp/a.o, when /tmp/a.o is on non-exec mount,
Solution: ld.so call execveat(AT_CHECK) for a.o, before mmap a.o into memory.
- Problem: application can call mmap (/tmp/a.out, rx), where /tmp is on non-exec mount
This is out of scope, i.e. will require enforcement on mmap(), maybe
through LSM
Thanks
Best regards
-Jeff
-Jeff
Quoted 178 lines
Quoted 178 lines
Quoted 177 lines
Quoted 177 lines
Thanks
-JeffQuoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)
Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 4 lines
load_elf_binary() {
interpreter = open_exec(elf_interpreter);
}Quoted 9 lines
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?
open_exec() checks are enough. LSMs can use this information (open + __FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.
Quoted 5 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)
Explaining these steps is useful. I'll include that in the next patch series.
Quoted 21 lines
Quoted 5 lines
Quoted 4 lines
A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from
SECBIT_EXEC_RESTRICT_FILE= 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.Quoted 9 lines
I explained that in the patches and in the description of these new securebits. Please point which part is not clear. The full threat model is simple: the TCB includes the kernel and system's files, which are integrity-protected, but we don't trust arbitrary data/scripts that can be written to user-owned files or directly provided to script interpreters. As for the ptrace restrictions, the dynamic linker restrictions helps to avoid trivial bypasses (e.g. with
LD_PRELOAD) with consistent executability checks.On elf loading case, I'm clear after your last email. However, I'm not sure if everyone else follows, I will try to summarize here:
- Problem: ld.so/tmp/a.outwill happily pass, even/tmp/a.outis mounted as non-exec.
Solution: ld.so callexecveat(AT_CHECK)for a.out before mmap a.out
into memory.
Quoted 6 lines
- Problem: a poorly built application (a.out) can have a dependency on
/tmp/a.o, when/tmp/a.ois on non-exec mount,
Solution: ld.so callexecveat(AT_CHECK)for a.o, before mmap a.o into memory.- Problem: application can call mmap (
/tmp/a.out, rx), where /tmp is on non-exec mount
I'd say "malicious or non-enlightened processes" can call mmap without execveat+AT_CHECK...
Quoted 2 lines
This is out of scope, i.e. will require enforcement on
mmap(), maybe
through LSM
Cool, I'll include that as well. Thanks.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 4 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }Quoted 9 lines
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?
Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.
Then the ld.so doesn't go through the same security_bprm_creds() check as other .so.
As my previous email, the ChromeOS LSM restricts executable mfd through security_bprm_creds(), the end result is that ld.so can still be executable memfd, but not other .so.
One way to address this is to refactor the necessary code from execveat() code patch, and make it available to call from both kernel and execveat() code paths., but if we do that, we might as well use faccessat2(AT_CHECK)
Quoted 44 lines
Quoted 5 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)Explaining these steps is useful. I'll include that in the next patch series.
Quoted 21 lines
Quoted 5 lines
Quoted 4 lines
A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from
SECBIT_EXEC_RESTRICT_FILE= 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.Quoted 9 lines
I explained that in the patches and in the description of these new securebits. Please point which part is not clear. The full threat model is simple: the TCB includes the kernel and system's files, which are integrity-protected, but we don't trust arbitrary data/scripts that can be written to user-owned files or directly provided to script interpreters. As for the ptrace restrictions, the dynamic linker restrictions helps to avoid trivial bypasses (e.g. with
LD_PRELOAD) with consistent executability checks.On elf loading case, I'm clear after your last email. However, I'm not sure if everyone else follows, I will try to summarize here:
- Problem: ld.so/tmp/a.outwill happily pass, even/tmp/a.outis mounted as non-exec.
Solution: ld.so callexecveat(AT_CHECK)for a.out before mmap a.out
into memory.Quoted 6 lines
- Problem: a poorly built application (a.out) can have a dependency on
/tmp/a.o, when/tmp/a.ois on non-exec mount,
Solution: ld.so callexecveat(AT_CHECK)for a.o, before mmap a.o into memory.- Problem: application can call mmap (
/tmp/a.out, rx), where /tmp is on non-exec mountI'd say "malicious or non-enlightened processes" can call mmap without execveat+AT_CHECK...
Quoted 2 lines
This is out of scope, i.e. will require enforcement on
mmap(), maybe
through LSMCool, I'll include that as well. Thanks.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 62 lines · Jeff Xu
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 13 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?
Quoted 6 lines
Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.Then the ld.so doesn't go through the same
security_bprm_creds()check as other .so.
Indeed, but...
Quoted 4 lines
As my previous email, the ChromeOS LSM restricts executable mfd through
security_bprm_creds(), the end result is that ld.so can still be executable memfd, but not other .so.
The chromeOS LSM can check that with the security_file_open() hook and the __FMODE_EXEC flag, see Landlock's implementation. I think this should be the only hook implementation that chromeOS LSM needs to add.
Quoted 5 lines
One way to address this is to refactor the necessary code from
execveat()code patch, and make it available to call from both kernel andexecveat()code paths., but if we do that, we might as well usefaccessat2(AT_CHECK)
That's why I think it makes sense to rely on the existing __FMODE_EXEC information.
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)Explaining these steps is useful. I'll include that in the next patch series.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 64 lines · Mickaël Salaün
Quoted 62 lines · Jeff Xu
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 13 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.Then the ld.so doesn't go through the same
security_bprm_creds()check as other .so.Indeed, but...
My point is: we will want all the .so going through the same code path, so security_ functions are called consistently across all the objects, And in the future, if we want to develop additional LSM functionality based on AT_CHECK, it will be applied to all objects.
Another thing to consider is: we are asking userspace to make additional syscall before loading the file into memory/get executed, there is a possibility for future expansion of the mechanism, without asking user space to add another syscall again.
I m still not convinced yet that execveat(AT_CHECK) fits more than faccessat(AT_CHECK)
Quoted 28 lines
Quoted 4 lines
As my previous email, the ChromeOS LSM restricts executable mfd through
security_bprm_creds(), the end result is that ld.so can still be executable memfd, but not other .so.The chromeOS LSM can check that with the
security_file_open()hook and the__FMODE_EXECflag, see Landlock's implementation. I think this should be the only hook implementation that chromeOS LSM needs to add.Quoted 5 lines
One way to address this is to refactor the necessary code from
execveat()code patch, and make it available to call from both kernel andexecveat()code paths., but if we do that, we might as well usefaccessat2(AT_CHECK)That's why I think it makes sense to rely on the existing
__FMODE_EXECinformation.Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)Explaining these steps is useful. I'll include that in the next patch series.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 72 lines · Jeff Xu
Quoted 71 lines · Mickaël Salaün
Quoted 68 lines · Jeff Xu
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 4 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }Quoted 9 lines
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.Then the ld.so doesn't go through the same
security_bprm_creds()check as other .so.
Quoted 6 lines
Quoted 2 lines
Indeed, but...
My point is: we will want all the .so going through the same code path, so
security_functions are called consistently across all the objects, And in the future, if we want to develop additional LSM functionality based onAT_CHECK, it will be applied to all objects.
I'll extend the doc to encourage LSMs to check for __FMODE_EXEC, which already is the common security check for all executable dependencies. As extra information, they can get explicit requests by looking at execveat+AT_CHECK call.
Quoted 5 lines
Another thing to consider is: we are asking userspace to make additional syscall before loading the file into memory/get executed, there is a possibility for future expansion of the mechanism, without asking user space to add another syscall again.
AT_CHECK is defined with a specific semantic. Other mechanisms (e.g. LSM policies) could enforce other restrictions following the same semantic. We need to keep in mind backward compatibility.
Quoted 3 lines
I m still not convinced yet that
execveat(AT_CHECK)fits more thanfaccessat(AT_CHECK)
faccessat2(2) is dedicated to file permission/attribute check. execveat(2) is dedicated to execution, which is a superset of file permission for executability, plus other checks (e.g. noexec).
Quoted 31 lines
Quoted 30 lines
Quoted 6 lines
As my previous email, the ChromeOS LSM restricts executable mfd through
security_bprm_creds(), the end result is that ld.so can still be executable memfd, but not other .so.The chromeOS LSM can check that with the
security_file_open()hook and the__FMODE_EXECflag, see Landlock's implementation. I think this should be the only hook implementation that chromeOS LSM needs to add.Quoted 5 lines
One way to address this is to refactor the necessary code from
execveat()code patch, and make it available to call from both kernel andexecveat()code paths., but if we do that, we might as well usefaccessat2(AT_CHECK)That's why I think it makes sense to rely on the existing
__FMODE_EXECinformation.Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)Explaining these steps is useful. I'll include that in the next patch series.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 81 lines · Mickaël Salaün
Quoted 72 lines · Jeff Xu
Quoted 71 lines · Mickaël Salaün
Quoted 68 lines · Jeff Xu
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 4 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }Quoted 9 lines
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.Then the ld.so doesn't go through the same
security_bprm_creds()check as other .so.Quoted 6 lines
Quoted 2 lines
Indeed, but...
My point is: we will want all the .so going through the same code path, so
security_functions are called consistently across all the objects, And in the future, if we want to develop additional LSM functionality based onAT_CHECK, it will be applied to all objects.
Quoted 5 lines
I'll extend the doc to encourage LSMs to check for
__FMODE_EXEC, which already is the common security check for all executable dependencies. As extra information, they can get explicit requests by looking at execveat+AT_CHECK call.
I agree that security_file_open + __FMODE_EXEC for checking all the .so (e.g for executable memfd) is a better option than checking at security_bprm_creds_for_exec.
But then maybe execveat( AT_CHECK) can return after calling alloc_bprm ? See below call graph:
do_execveat_common (AT_CHECK)
-> alloc_bprm
->->do_open_execat
->->-> do_filp_open (__FMODE_EXEC)
->->->->->->> security_file_open
-> bprm_execve
->-> prepare_exec_creds
->->-> prepare_creds
->->->-> security_prepare_creds
->-> security_bprm_creds_for_exec
What is the consideration to mark the end at security_bprm_creds_for_exec ? i.e. including brpm_execve, prepare_creds, security_prepare_creds, security_bprm_creds_for_exec.
Since dynamic linker doesn't load ld.so (it is by kernel), ld.so won't go through those security_prepare_creds and security_bprm_creds_for_exec checks like other .so do.
Quoted 11 lines
Quoted 5 lines
Another thing to consider is: we are asking userspace to make additional syscall before loading the file into memory/get executed, there is a possibility for future expansion of the mechanism, without asking user space to add another syscall again.
AT_CHECKis defined with a specific semantic. Other mechanisms (e.g. LSM policies) could enforce other restrictions following the same semantic. We need to keep in mind backward compatibility.Quoted 1 line
Quoted 7 lines
Quoted 2 lines
I m still not convinced yet that
execveat(AT_CHECK)fits more thanfaccessat(AT_CHECK)faccessat2(2) is dedicated to file permission/attribute check. execveat(2) is dedicated to execution, which is a superset of file permission for executability, plus other checks (e.g. noexec).
That sounds reasonable, but if execveat(AT_CHECK) changes behavior of execveat(), someone might argue that faccessat2(EXEC_CHECK) can be made for the executability.
I think the decision might depend on what this PATCH intended to check, i.e. where we draw the line.
do_open_execat() seems to cover lots of checks for executability, if we are ok with the thing that do_open_execat() checks, then faccessat(AT_CHECK) calling do_open_execat() is an option, it won't have those "unrelated" calls in execve path, e.g. bprm_stack_limits, copy argc/env .
However, you mentioned superset of file permission for executability, can you elaborate on that ? Is there something not included in do_open_execat() but still necessary for execveat(AT_CHECK)? maybe security_bprm_creds_for_exec? (this goes back to my question above)
Thanks
Best regards,
-Jeff
Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 6 lines
As my previous email, the ChromeOS LSM restricts executable mfd through
security_bprm_creds(), the end result is that ld.so can still be executable memfd, but not other .so.The chromeOS LSM can check that with the
security_file_open()hook and the__FMODE_EXECflag, see Landlock's implementation. I think this should be the only hook implementation that chromeOS LSM needs to add.Quoted 5 lines
One way to address this is to refactor the necessary code from
execveat()code patch, and make it available to call from both kernel andexecveat()code paths., but if we do that, we might as well usefaccessat2(AT_CHECK)That's why I think it makes sense to rely on the existing
__FMODE_EXECinformation.Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)Explaining these steps is useful. I'll include that in the next patch series.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 105 lines · Jeff Xu
Quoted 81 lines · Mickaël Salaün
Quoted 72 lines · Jeff Xu
Quoted 71 lines · Mickaël Salaün
Quoted 68 lines · Jeff Xu
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 8 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].Quoted 6 lines
This is different than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 4 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }Quoted 9 lines
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.Then the ld.so doesn't go through the same
security_bprm_creds()check as other .so.Quoted 6 lines
Quoted 2 lines
Indeed, but...
My point is: we will want all the .so going through the same code path, so
security_functions are called consistently across all the objects, And in the future, if we want to develop additional LSM functionality based onAT_CHECK, it will be applied to all objects.Quoted 5 lines
I'll extend the doc to encourage LSMs to check for
__FMODE_EXEC, which already is the common security check for all executable dependencies. As extra information, they can get explicit requests by looking at execveat+AT_CHECK call.I agree that
security_file_open+__FMODE_EXECfor checking all the .so (e.g for executable memfd) is a better option than checking atsecurity_bprm_creds_for_exec.But then maybe
execveat( AT_CHECK)can return after callingalloc_bprm? See below call graph:
do_execveat_common(AT_CHECK)
->alloc_bprm
->->do_open_execat
->->->do_filp_open(__FMODE_EXEC)
->->->->->->>security_file_open
->bprm_execve
->->prepare_exec_creds
->->->prepare_creds
->->->->security_prepare_creds
->->security_bprm_creds_for_exec
Quoted 3 lines
What is the consideration to mark the end at
security_bprm_creds_for_exec? i.e. includingbrpm_execve,prepare_creds,security_prepare_creds,security_bprm_creds_for_exec.
This enables LSMs to know/log an explicit execution request, including context with argv and envp.
Quoted 4 lines
Since dynamic linker doesn't load ld.so (it is by kernel), ld.so won't go through those
security_prepare_credsandsecurity_bprm_creds_for_execchecks like other .so do.
Yes, but this is not an issue nor an explicit request. ld.so is only one case of this patch series.
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
Another thing to consider is: we are asking userspace to make additional syscall before loading the file into memory/get executed, there is a possibility for future expansion of the mechanism, without asking user space to add another syscall again.
AT_CHECKis defined with a specific semantic. Other mechanisms (e.g. LSM policies) could enforce other restrictions following the same semantic. We need to keep in mind backward compatibility.Quoted 3 lines
I m still not convinced yet that
execveat(AT_CHECK)fits more thanfaccessat(AT_CHECK)
Quoted 7 lines
Quoted 4 lines
faccessat2(2) is dedicated to file permission/attribute check. execveat(2) is dedicated to execution, which is a superset of file permission for executability, plus other checks (e.g. noexec).
That sounds reasonable, but if
execveat(AT_CHECK)changes behavior ofexecveat(), someone might argue thatfaccessat2(EXEC_CHECK)can be made for the executability.
AT_CHECK, as any other syscall flags, changes the behavior of execveat, but the overall semantic is clearly defined.
Again, faccessat2 is only dedicated to file attributes/permissions, not file executability.
Quoted 3 lines
I think the decision might depend on what this PATCH intended to check, i.e. where we draw the line.
The goal is clearly defined in the cover letter and patches: makes it possible to control (or log) script execution.
Quoted 6 lines
do_open_execat()seems to cover lots of checks for executability, if we are ok with the thing thatdo_open_execat()checks, thenfaccessat(AT_CHECK)callingdo_open_execat()is an option, it won't have those "unrelated" calls in execve path, e.g.bprm_stack_limits, copy argc/env .
I don't thing there is any unrelated calls in execve path, quite the contrary, it follows the same semantic as for a full execution, and that's another argument to use the execveat interface. Otherwise, we couldn't argue that ./script.sh can be the same as sh script.sh
The only difference is that user space is in charge of parsing and interpreting the file's content.
Quoted 5 lines
However, you mentioned superset of file permission for executability, can you elaborate on that ? Is there something not included in
do_open_execat()but still necessary forexecveat(AT_CHECK)? maybesecurity_bprm_creds_for_exec? (this goes back to my question above)
As explained above, the goal is to have the same semantic as a full execveat call, taking into account all the checks (e.g. stack limit, argv/envp...).
Quoted 47 lines
Thanks
Best regards,
-JeffQuoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 6 lines
As my previous email, the ChromeOS LSM restricts executable mfd through
security_bprm_creds(), the end result is that ld.so can still be executable memfd, but not other .so.The chromeOS LSM can check that with the
security_file_open()hook and the__FMODE_EXECflag, see Landlock's implementation. I think this should be the only hook implementation that chromeOS LSM needs to add.Quoted 5 lines
One way to address this is to refactor the necessary code from
execveat()code patch, and make it available to call from both kernel andexecveat()code paths., but if we do that, we might as well usefaccessat2(AT_CHECK)That's why I think it makes sense to rely on the existing
__FMODE_EXECinformation.Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
- transfer the control to ld.so)
- ld.so open (libc.so)
- ld.so callexecveat(AT_CHECK,libc.so)<-- proposed by this patch,
require dynamic linker change.
- ld.sommap(libc.so,rx)Explaining these steps is useful. I'll include that in the next patch series.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 165 lines · Mickaël Salaün
Quoted 105 lines · Jeff Xu
Quoted 81 lines · Mickaël Salaün
Quoted 72 lines · Jeff Xu
Quoted 71 lines · Mickaël Salaün
Quoted 68 lines · Jeff Xu
Quoted 61 lines · Mickaël Salaün
Quoted 36 lines · Jeff Xu
Quoted 35 lines · Mickaël Salaün
Quoted 32 lines · Jeff Xu
Quoted 20 lines · Mickaël Salaün
Quoted 17 lines · Jeff Xu
Quoted 14 lines · Mickaël Salaün
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 than faccessat(2) which only checks file access
rights, but not the full context e.g. mount point's noexec, stack limit,
and all potential LSM extra checks (e.g. argv, envp, credentials).
Since the use of AT_CHECK follows the exact kernel semantic as for a
real execution, user space gets the same error codes.So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),Quoted 7 lines
Quoted 2 lines
I think
binfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 5 lines
Quoted 3 lines
The kernel calls
open_exec()on all dependencies, including ld-linux-x86-64.so.2, so these files are checked for executability too.This might not be entirely true. iiuc, kernel calls
open_execforopen_execfor interpreter, but not all its dependency (e.g. libc.so.6)Correct, the dynamic linker is in charge of that, which is why it must be enlighten with execveat+AT_CHECK and securebits checks.
Quoted 4 lines
load_elf_binary() { interpreter = open_exec(elf_interpreter); }Quoted 9 lines
libc.so.6 is opened and mapped by dynamic linker.
so the call sequence is:
execve(a.out)
- open exec(a.out)
-security_bprm_creds(a.out)
- open the exec(ld.so)
- callopen_exec()for interruptor (ld.so)
- callexecveat(AT_CHECK, ld.so)<-- do we want ld.so going through
the same check and code path as libc.so below ?Quoted 4 lines
open_exec()checks are enough. LSMs can use this information (open +__FMODE_EXEC) if needed. execveat+AT_CHECK is only a user space request.Then the ld.so doesn't go through the same
security_bprm_creds()check as other .so.Quoted 6 lines
Quoted 2 lines
Indeed, but...
My point is: we will want all the .so going through the same code path, so
security_functions are called consistently across all the objects, And in the future, if we want to develop additional LSM functionality based onAT_CHECK, it will be applied to all objects.Quoted 5 lines
I'll extend the doc to encourage LSMs to check for
__FMODE_EXEC, which already is the common security check for all executable dependencies. As extra information, they can get explicit requests by looking at execveat+AT_CHECK call.I agree that
security_file_open+__FMODE_EXECfor checking all the .so (e.g for executable memfd) is a better option than checking atsecurity_bprm_creds_for_exec.But then maybe
execveat( AT_CHECK)can return after callingalloc_bprm? See below call graph:
do_execveat_common(AT_CHECK)
->alloc_bprm
->->do_open_execat
->->->do_filp_open(__FMODE_EXEC)
->->->->->->>security_file_open
->bprm_execve
->->prepare_exec_creds
->->->prepare_creds
->->->->security_prepare_creds
->->security_bprm_creds_for_execQuoted 3 lines
What is the consideration to mark the end at
security_bprm_creds_for_exec? i.e. includingbrpm_execve,prepare_creds,security_prepare_creds,security_bprm_creds_for_exec.This enables LSMs to know/log an explicit execution request, including context with argv and envp.
Quoted 4 lines
Since dynamic linker doesn't load ld.so (it is by kernel), ld.so won't go through those
security_prepare_credsandsecurity_bprm_creds_for_execchecks like other .so do.Yes, but this is not an issue nor an explicit request. ld.so is only one case of this patch series.
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
Another thing to consider is: we are asking userspace to make additional syscall before loading the file into memory/get executed, there is a possibility for future expansion of the mechanism, without asking user space to add another syscall again.
AT_CHECKis defined with a specific semantic. Other mechanisms (e.g. LSM policies) could enforce other restrictions following the same semantic. We need to keep in mind backward compatibility.Quoted 3 lines
I m still not convinced yet that
execveat(AT_CHECK)fits more thanfaccessat(AT_CHECK)Quoted 7 lines
Quoted 4 lines
faccessat2(2) is dedicated to file permission/attribute check. execveat(2) is dedicated to execution, which is a superset of file permission for executability, plus other checks (e.g. noexec).
That sounds reasonable, but if
execveat(AT_CHECK)changes behavior ofexecveat(), someone might argue thatfaccessat2(EXEC_CHECK)can be made for the executability.
AT_CHECK, as any other syscall flags, changes the behavior of execveat, but the overall semantic is clearly defined.Again, faccessat2 is only dedicated to file attributes/permissions, not file executability.
Quoted 3 lines
I think the decision might depend on what this PATCH intended to check, i.e. where we draw the line.
The goal is clearly defined in the cover letter and patches: makes it possible to control (or log) script execution.
Quoted 6 lines
do_open_execat()seems to cover lots of checks for executability, if we are ok with the thing thatdo_open_execat()checks, thenfaccessat(AT_CHECK)callingdo_open_execat()is an option, it won't have those "unrelated" calls in execve path, e.g.bprm_stack_limits, copy argc/env .
Quoted 5 lines
I don't thing there is any unrelated calls in execve path, quite the contrary, it follows the same semantic as for a full execution, and that's another argument to use the execveat interface. Otherwise, we couldn't argue that
./script.shcan be the same assh script.sh
It is a good point from the "scrip.sh/exec" perspective that we want it to go through the same execve path.
The reasoning is not obvious from the ".so" which doesn't go through stack/env check.
Since execveat(AT_CHECK) wants to cover both cases, it is fine.
Quoted 4 lines
The only difference is that user space is in charge of parsing and interpreting the file's content.
Quoted 1 line
Quoted 9 lines
Quoted 4 lines
However, you mentioned superset of file permission for executability, can you elaborate on that ? Is there something not included in
do_open_execat()but still necessary forexecveat(AT_CHECK)? maybesecurity_bprm_creds_for_exec? (this goes back to my question above)As explained above, the goal is to have the same semantic as a full execveat call, taking into account all the checks (e.g. stack limit, argv/envp...).
I'm fine with this, thanks for taking time to explain the design.
Regarding the future LSM based on this patch series:
For .so, security_file_open is recommended for LSM.
For scripts/exec (that needs a full exec code path), security_file_open and security_bprm_creds_for_exec can both be used.
Thanks
Best regards,
-Jeff
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Mickaël SalaünQuoted 64 lines · Mickaël Salaün
Quoted 18 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Because seccomp filters alter syscalls, they are similar to code injection.
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
It might makes sense to follow the kernel and interpreter semantic: if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured with
binfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable withbinfmt_misc. So, again, where to draw the line?
Quoted 7 lines
I'd recommend to think about interaction with the outside, through function calls, IPCs, syscalls... For instance, "running" an image should not lead to reading or writing to arbitrary files, or accessing the network, but in practice it is legitimate for some file formats... PostScript is a programming language, but mostly used to draw pictures. So, again, where to draw the line?
The javascript is run by browser and java code by java runtime, do they meet the criteria? they do not interact with the kernel directly, however they might have the same "executable" characteristics and the app might not want them to be put into non-exec mount.
If the answer is yes, they can also use execveat(AT_CHECK), the next question is: does it make sense for javacript/java code to go through execveat() code path, allocate bprm, etc ? (I don't have answer, maybe it is)
Quoted 11 lines
We should follow the principle of least astonishment. What most users would expect? This should follow the *common usage* of executable files. At the end, the script interpreters will be patched by security folks for security reasons. I think the right question to ask should
be: could this file format be (ab)used to leak or modify arbitrary files, or to perform arbitrary syscalls? If the answer is yes, then it should be checked for executability. Of course, this excludes bugs exploited in the file format parser.I'll extend the next patch series with this rationale.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 77 lines · Jeff Xu
Quoted 64 lines · Mickaël Salaün
Quoted 18 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Because seccomp filters alter syscalls, they are similar to code injection.
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
It might makes sense to follow the kernel and interpreter semantic: if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured with
binfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable withbinfmt_misc. So, again, where to draw the line?Quoted 7 lines
I'd recommend to think about interaction with the outside, through function calls, IPCs, syscalls... For instance, "running" an image should not lead to reading or writing to arbitrary files, or accessing the network, but in practice it is legitimate for some file formats... PostScript is a programming language, but mostly used to draw pictures. So, again, where to draw the line?
The javascript is run by browser and java code by java runtime, do they meet the criteria? they do not interact with the kernel directly, however they might have the same "executable" characteristics and the app might not want them to be put into non-exec mount.
Quoted 4 lines
If the answer is yes, they can also use
execveat(AT_CHECK), the next question is: does it make sense for javacript/java code to go throughexecveat()code path, allocate bprm, etc ? (I don't have answer, maybe it is)
Java and NodeJS can do arbitrary syscalls (through their runtime) and they can access arbitrary files, so according to my below comment, yes they should be managed as potentially dangerous executable code.
The question should be: is this code trusted? Most of the time it is not, hence the security model of web browser and their heavy use of sandboxing. So no, I don't think it would make sense to check this kind of code more than what the browser already do.
I'll talk about this use case in the next patch series.
Quoted 13 lines
Quoted 12 lines
We should follow the principle of least astonishment. What most users would expect? This should follow the *common usage* of executable files. At the end, the script interpreters will be patched by security folks for security reasons. I think the right question to ask should
be: could this file format be (ab)used to leak or modify arbitrary files, or to perform arbitrary syscalls? If the answer is yes, then it should be checked for executability. Of course, this excludes bugs exploited in the file format parser.I'll extend the next patch series with this rationale.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 84 lines · Mickaël Salaün
Quoted 77 lines · Jeff Xu
Quoted 64 lines · Mickaël Salaün
Quoted 18 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Because seccomp filters alter syscalls, they are similar to code injection.
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
It might makes sense to follow the kernel and interpreter semantic: if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured with
binfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable withbinfmt_misc. So, again, where to draw the line?Quoted 7 lines
I'd recommend to think about interaction with the outside, through function calls, IPCs, syscalls... For instance, "running" an image should not lead to reading or writing to arbitrary files, or accessing the network, but in practice it is legitimate for some file formats... PostScript is a programming language, but mostly used to draw pictures. So, again, where to draw the line?
The javascript is run by browser and java code by java runtime, do they meet the criteria? they do not interact with the kernel directly, however they might have the same "executable" characteristics and the app might not want them to be put into non-exec mount.
Quoted 4 lines
If the answer is yes, they can also use
execveat(AT_CHECK), the next question is: does it make sense for javacript/java code to go throughexecveat()code path, allocate bprm, etc ? (I don't have answer, maybe it is)
Quoted 9 lines
Java and NodeJS can do arbitrary syscalls (through their runtime) and they can access arbitrary files, so according to my below comment, yes they should be managed as potentially dangerous executable code.
The question should be: is this code trusted? Most of the time it is not, hence the security model of web browser and their heavy use of sandboxing. So no, I don't think it would make sense to check this kind of code more than what the browser already do.
If I understand you correctly, Java/NodeJS won't use execveat(AT_CHECK), we will leave that work to the web browser/java runtime's sandboxer.
This is good because the scope is more narrow/clear.
Thanks
-Jeff
Quoted 15 lines
I'll talk about this use case in the next patch series.
Quoted 13 lines
Quoted 12 lines
We should follow the principle of least astonishment. What most users would expect? This should follow the *common usage* of executable files. At the end, the script interpreters will be patched by security folks for security reasons. I think the right question to ask should
be: could this file format be (ab)used to leak or modify arbitrary files, or to perform arbitrary syscalls? If the answer is yes, then it should be checked for executability. Of course, this excludes bugs exploited in the file format parser.I'll extend the next patch series with this rationale.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 95 lines · Jeff Xu
Quoted 84 lines · Mickaël Salaün
Quoted 77 lines · Jeff Xu
Quoted 64 lines · Mickaël Salaün
Quoted 18 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Because seccomp filters alter syscalls, they are similar to code injection.
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
Quoted 5 lines
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)The line between data and code is blurry. For instance, a configuration file can impact the execution flow of a program. So, where to draw the line?
It might makes sense to follow the kernel and interpreter semantic: if a file can be executed by the kernel (e.g. ELF binary, file containing a shebang, or just configured with
binfmt_misc), then this should be considered as executable code. This applies to Bash, Python, Javascript, NodeJS, PE, PHP... However, we can also make a picture executable withbinfmt_misc. So, again, where to draw the line?Quoted 7 lines
I'd recommend to think about interaction with the outside, through function calls, IPCs, syscalls... For instance, "running" an image should not lead to reading or writing to arbitrary files, or accessing the network, but in practice it is legitimate for some file formats... PostScript is a programming language, but mostly used to draw pictures. So, again, where to draw the line?
The javascript is run by browser and java code by java runtime, do they meet the criteria? they do not interact with the kernel directly, however they might have the same "executable" characteristics and the app might not want them to be put into non-exec mount.
Quoted 4 lines
If the answer is yes, they can also use
execveat(AT_CHECK), the next question is: does it make sense for javacript/java code to go throughexecveat()code path, allocate bprm, etc ? (I don't have answer, maybe it is)Quoted 9 lines
Java and NodeJS can do arbitrary syscalls (through their runtime) and they can access arbitrary files, so according to my below comment, yes they should be managed as potentially dangerous executable code.
The question should be: is this code trusted? Most of the time it is not, hence the security model of web browser and their heavy use of sandboxing. So no, I don't think it would make sense to check this kind of code more than what the browser already do.
Quoted 4 lines
If I understand you correctly, Java/NodeJS won't use
execveat(AT_CHECK), we will leave that work to the web browser/java runtime's sandboxer.
This is good because the scope is more narrow/clear.
Yes for browser's sandboxes because the code comes from the network (i.e. not authenticated at the kernel level, and mostly untrusted).
For standalone Java applications (stored in the filesystem), the Java runtime(s) should be patched as other script interpreters.
Quoted 19 lines
Thanks
-JeffQuoted 15 lines
I'll talk about this use case in the next patch series.
Quoted 13 lines
Quoted 12 lines
We should follow the principle of least astonishment. What most users would expect? This should follow the *common usage* of executable files. At the end, the script interpreters will be patched by security folks for security reasons. I think the right question to ask should
be: could this file format be (ab)used to leak or modify arbitrary files, or to perform arbitrary syscalls? If the answer is yes, then it should be checked for executability. Of course, this excludes bugs exploited in the file format parser.I'll extend the next patch series with this rationale.
Re: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
In reply to Jeff XuQuoted 83 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Quoted 9 lines
Quoted 8 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)Quoted 5 lines
If we want other checks, we'll need to clearly define their semantic and align with the kernel. faccessat2(2) might be used to check other file properties, but the executable property is not only defined by the file attributes.
Agreed.
Quoted 20 lines
Quoted 20 lines
--------------------------
now looked at user cases (focus on elf for now)1> ld.so
/tmp/a.out,/tmp/a.outis on non-exec mount
dynamic linker will first callexecveat(fd, AT_CHECK)then execveat(fd)2> execve(/usr/bin/some.out) and some.out has dependency on
/tmp/a.so/usr/bin/some.outwill passAT_CHECK3> execve(usr/bin/some.out) and some.out uses custom
/tmp/ld.so/usr/bin/some.outwill passAT_CHECK, however, it uses a custom/tmp/ld.so(I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, andbinfmt_elf.callocate memory for ld.so during execveat call)4> dlopen(/tmp/a.so)
I assume dynamic linker will callexecveat(AT_CHECK), before map a.so into memory.Quoted 8 lines
Quoted 3 lines
For case 1>
Alternative solution: BecauseAT_CHECKis always called, I think we can avoid the firstAT_CHECKcall, and check during execveat(fd),There is no need to use
AT_CHECKif we're going to call execveat(2) on the same file descriptor. By design,AT_CHECKis implicit for any execve(2).
Quoted 8 lines
Yes. I realized I was wrong to say that ld.so will call
execve()for/tmp/a.out, there is noexecve()call, otherwise it would have been blocked already today.
The ld.so will mmap the/tmp/a.outdirectly. So case 1 is no different than case 2 and 4. ( the elf objects are mapped to memory by dynamic linker.)
I'm not familiar with dynamic linker, Florian is on this thread, and can help to correct me if my guess is wrong.
for Android, this has been the nail in the coffin of previous attempts to disallow running code from non-trusted filesystems --- instead of execing /tmp/a.out, the attacker just execs the linker with /tmp/a.out as an argument. people are doing this already in some cases, because we already have ineffectual "barriers" in place. [the usual argument for doing such things anyway is "it makes it harder to be doing this by _accident_".]
the other workaround for the attacker is to copy and paste the entire dynamic linker source and change the bits they don't like :-) (if you're thinking "is that a thing?", yes, so much so that the idea has been independently reinvented multiple times by several major legit apps and by basically every piece of DRM middleware. which is why -- although i'm excited by mseal(2) -- i expect to face significant challenges rolling it out in Android _especially_ in places like "dynamic linker internal data structures" where i've wanted it for years!)
this proposal feels like it _ought_ to let a defender tighten their seccomp filter to require a "safe" fd if i'm using mmap() with an fd, but in practice as long as JITs exist i can always just copy code into a non-fd-backed mmap() region. and -- from the perspective of Android, where all "apps" are code loaded into a Java runtime -- there's not much getting away from JITs. (and last i looked, ART -- Android's Java runtime -- uses memfd() for the JIT cache.)
Quoted 303 lines
Quoted 18 lines
Quoted 3 lines
this means the kernel will enforce
SECBIT_EXEC_RESTRICT_FILE= 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesfulexecveat()case.As long as user space uses the same file descriptor, there is no TOCTOU.
SECBIT_EXEC_RESTRICT_FILEonly makes sense for user space: it defines the user space security policy. The kernel already enforces the same security policy for any execve(2), whatever are the calling process's securebits.Quoted 6 lines
For case 2>
dynamic linker will callexecve(AT_CHECK), then mmap(fd) into memory. However, the process can all open thenmmap()directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.Quoted 8 lines
Which process? What do you mean by "can all open then
mmap()directly"?In this context the dynamic linker (like its parent processes) is trusted (guaranteed by the system).
For case 2, the dynamic linker must check with
AT_CHECKall files that will be mapped, which include/usr/bin/some.outand/tmp/a.soMy point is that the process can work around this by
mmap()the file directly.Quoted 7 lines
Quoted 6 lines
Alternative solution:
dynamic linker callAT_CHECKfor each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time ofmmap(fd, executable memory)and enforceSECBIT_EXEC_RESTRICT_FILE= 1Quoted 6 lines
The idea with
AT_CHECKis that there is no kernel side effect, no extra kernel state, and the semantic is the same as with execve(2).This also enables us to check file's executable permission and ignore it, which is useful in a "permissive mode" when preparing for a migration without breaking a system, or to do extra integrity checks.
For preparing a migration (detect all violations), this is useful. But as a defense mechanism (
SECBIT_EXEC_RESTRICT_FILE= 1) , this seems to be weak, at least for elf loading case.Quoted 10 lines
BTW, this use case would also be more complex with a new openat2(2) flag like the original
O_MAYEXEC.Quoted 3 lines
Alternative solution 2:
a new syscall to load the .so and enforce theAT_CHECKin kernelA new syscall would be overkill for this feature. Please see Linus's comment.
maybe, I was thinking on how to prevent "
/tmp/a.o" from gettingmmap()to executable memory.Quoted 15 lines
Quoted 3 lines
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ),
How seccomp could create anonymous memory in user space? seccomp filters should be treated (and checked with
AT_CHECK) as executable code anyway.Quoted 2 lines
unless the user space can harden the creation of executable anonymous memory in some way.
User space is already in charge of mmapping its own memory. I don't see what is missing.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
For case 3>
I thinkbinfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 29 lines
However, we must be careful with programs using the (deprecated) uselib(2). They should also check with
AT_CHECKbecause this syscall opens the shared library without__FMODE_EXEC(similar to a simple file open). See
https://lore.kernel.org/all/CAHk-=wiUwRG7LuR=z5sbkFVGQh+7qVB6_1NM0Ny9SVNL1Un4Sw@mail.gmail.com/Quoted 8 lines
For case 4>
same as case 2.Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.As Steve explained (and is also explained in the patches), it is trivial if the attacker can already execute its own code, which is too late to enforce any script execution control.
Quoted 4 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.A previous version of this patches series already patched faccessat(2), but this is not the right place. faccessat2(2) is dedicated to check file permissions, not executability (e.g. with mount's noexec).
Quoted 1 line
Quoted 5 lines
Quoted 1 line
What do you think ?
I think there are some misunderstandings. Please let me know if it's clearer now.
I'm still not sure about the user case for dynamic linker (elf loading) case. Maybe this patch is more suitable for scripts? A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from
SECBIT_EXEC_RESTRICT_FILE= 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.Quoted 176 lines
Quoted 176 lines
Thanks
-JeffQuoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
Mickaël Salaün
maintainerRe: [RFC PATCH v19 1/5] exec: Add a new AT_CHECK flag to execveat(2)
Quoted 93 lines · enh
Quoted 83 lines · Jeff Xu
Quoted 17 lines · Mickaël Salaün
Quoted 15 lines · Jeff Xu
Quoted 14 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 than faccessat(2) which only checks file access rights, but not the full context e.g. mount point's noexec, stack limit, and all potential LSM extra checks (e.g. argv, envp, credentials). Since the use of
AT_CHECKfollows the exact kernel semantic as for a real execution, user space gets the same error codes.Quoted 5 lines
Quoted 2 lines
So we concluded that
execveat(AT_CHECK)will be used to check the exec, shared object, script and config file (such as seccomp config),"config file" that contains executable code.
Is seccomp config considered as "contains executable code", seccomp config is translated into bpf, so maybe yes ? but bpf is running in the kernel.
Quoted 9 lines
Quoted 8 lines
I'm still thinking
execveat(AT_CHECK)vsfaccessat(AT_CHECK)in different use cases:execveat clearly has less code change, but that also means: we can't add logic specific to exec (i.e. logic that can't be applied to config) for this part (from
do_execveat_commontosecurity_bprm_creds_for_exec) in future. This would require some agreement/sign-off, I'm not sure from whom.Quoted 5 lines
I'm not sure to follow. We could still add new flags, but for now I don't see use cases. This patch series is not meant to handle all possible "trust checks", only executable code, which makes sense for the kernel.
I guess the "configfile" discussion is where I get confused, at one point, I think this would become a generic "trust checks" api for everything related to "generating executable code", e.g. javascript, java code, and more.
We will want to clearly define the scope ofexecveat(AT_CHECK)Quoted 5 lines
If we want other checks, we'll need to clearly define their semantic and align with the kernel. faccessat2(2) might be used to check other file properties, but the executable property is not only defined by the file attributes.
Agreed.
Quoted 20 lines
Quoted 20 lines
--------------------------
now looked at user cases (focus on elf for now)1> ld.so
/tmp/a.out,/tmp/a.outis on non-exec mount
dynamic linker will first callexecveat(fd, AT_CHECK)then execveat(fd)2> execve(/usr/bin/some.out) and some.out has dependency on
/tmp/a.so/usr/bin/some.outwill passAT_CHECK3> execve(usr/bin/some.out) and some.out uses custom
/tmp/ld.so/usr/bin/some.outwill passAT_CHECK, however, it uses a custom/tmp/ld.so(I assume this is possible for elf header will set the path for ld.so because kernel has no knowledge of that, andbinfmt_elf.callocate memory for ld.so during execveat call)4> dlopen(/tmp/a.so)
I assume dynamic linker will callexecveat(AT_CHECK), before map a.so into memory.Quoted 8 lines
Quoted 3 lines
For case 1>
Alternative solution: BecauseAT_CHECKis always called, I think we can avoid the firstAT_CHECKcall, and check during execveat(fd),There is no need to use
AT_CHECKif we're going to call execveat(2) on the same file descriptor. By design,AT_CHECKis implicit for any execve(2).Quoted 8 lines
Yes. I realized I was wrong to say that ld.so will call
execve()for/tmp/a.out, there is noexecve()call, otherwise it would have been blocked already today.
The ld.so will mmap the/tmp/a.outdirectly. So case 1 is no different than case 2 and 4. ( the elf objects are mapped to memory by dynamic linker.)
I'm not familiar with dynamic linker, Florian is on this thread, and can help to correct me if my guess is wrong.
Quoted 7 lines
for Android, this has been the nail in the coffin of previous attempts to disallow running code from non-trusted filesystems --- instead of execing
/tmp/a.out, the attacker just execs the linker with/tmp/a.outas an argument. people are doing this already in some cases, because we already have ineffectual "barriers" in place. [the usual argument for doing such things anyway is "it makes it harder to be doing this by _accident_".]
This AT_CHECK and related securebits should cover this case.
Quoted 11 lines
the other workaround for the attacker is to copy and paste the entire dynamic linker source and change the bits they don't like :-) (if you're thinking "is that a thing?", yes, so much so that the idea has been independently reinvented multiple times by several major legit apps and by basically every piece of DRM middleware. which is why -- although i'm excited by mseal(2) -- i expect to face significant challenges rolling it out in Android _especially_ in places like "dynamic linker internal data structures" where i've wanted it for years!)
Quoted 7 lines
this proposal feels like it _ought_ to let a defender tighten their seccomp filter to require a "safe" fd if i'm using
mmap()with an fd, but in practice as long as JITs exist i can always just copy code into a non-fd-backedmmap()region. and -- from the perspective of Android, where all "apps" are code loaded into a Java runtime -- there's not much getting away from JITs. (and last i looked, ART -- Android's Java runtime -- usesmemfd()for the JIT cache.)
Using the feature brought by this patch series makes sense for trusted executables willing to enforce a security policy. Untrusted ones should be sandboxed, which is the case for Android apps.
Quoted 305 lines
Quoted 304 lines
Quoted 19 lines
Quoted 4 lines
this means the kernel will enforce
SECBIT_EXEC_RESTRICT_FILE= 1, the benefit is that there is no TOCTOU and save one round trip of syscall for a succesfulexecveat()case.As long as user space uses the same file descriptor, there is no TOCTOU.
SECBIT_EXEC_RESTRICT_FILEonly makes sense for user space: it defines the user space security policy. The kernel already enforces the same security policy for any execve(2), whatever are the calling process's securebits.Quoted 6 lines
For case 2>
dynamic linker will callexecve(AT_CHECK), then mmap(fd) into memory. However, the process can all open thenmmap()directly, it seems minimal effort for an attacker to walk around such a defence from dynamic linker.Quoted 8 lines
Which process? What do you mean by "can all open then
mmap()directly"?In this context the dynamic linker (like its parent processes) is trusted (guaranteed by the system).
For case 2, the dynamic linker must check with
AT_CHECKall files that will be mapped, which include/usr/bin/some.outand/tmp/a.soMy point is that the process can work around this by
mmap()the file directly.Quoted 7 lines
Quoted 6 lines
Alternative solution:
dynamic linker callAT_CHECKfor each .so, kernel will save the state (associated with fd)
kernel will check fd state at the time ofmmap(fd, executable memory)and enforceSECBIT_EXEC_RESTRICT_FILE= 1Quoted 6 lines
The idea with
AT_CHECKis that there is no kernel side effect, no extra kernel state, and the semantic is the same as with execve(2).This also enables us to check file's executable permission and ignore it, which is useful in a "permissive mode" when preparing for a migration without breaking a system, or to do extra integrity checks.
For preparing a migration (detect all violations), this is useful. But as a defense mechanism (
SECBIT_EXEC_RESTRICT_FILE= 1) , this seems to be weak, at least for elf loading case.Quoted 10 lines
BTW, this use case would also be more complex with a new openat2(2) flag like the original
O_MAYEXEC.Quoted 3 lines
Alternative solution 2:
a new syscall to load the .so and enforce theAT_CHECKin kernelA new syscall would be overkill for this feature. Please see Linus's comment.
maybe, I was thinking on how to prevent "
/tmp/a.o" from gettingmmap()to executable memory.Quoted 15 lines
Quoted 3 lines
This also means, for the solution to be complete, we might want to block creation of executable anonymous memory (e.g. by seccomp, ),
How seccomp could create anonymous memory in user space? seccomp filters should be treated (and checked with
AT_CHECK) as executable code anyway.Quoted 2 lines
unless the user space can harden the creation of executable anonymous memory in some way.
User space is already in charge of mmapping its own memory. I don't see what is missing.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
For case 3>
I thinkbinfmt_elf.cin the kernel needs to check the ld.so to make sure it passesAT_CHECK, before loading it into memory.All ELF dependencies are opened and checked with
open_exec(), which perform the main executability checks (with the__FMODE_EXECflag). Did I miss something?I mean the ld-linux-x86-64.so.2 which is loaded by binfmt in the kernel. The app can choose its own dynamic linker path during build, (maybe even statically link one ?) This is another reason that relying on a userspace only is not enough.
Quoted 29 lines
However, we must be careful with programs using the (deprecated) uselib(2). They should also check with
AT_CHECKbecause this syscall opens the shared library without__FMODE_EXEC(similar to a simple file open). See
https://lore.kernel.org/all/CAHk-=wiUwRG7LuR=z5sbkFVGQh+7qVB6_1NM0Ny9SVNL1Un4Sw@mail.gmail.com/Quoted 8 lines
For case 4>
same as case 2.Consider those cases: I think:
a> relying purely on userspace for enforcement does't seem to be effective, e.g. it is trivial to callopen(), thenmmap()it into executable memory.As Steve explained (and is also explained in the patches), it is trivial if the attacker can already execute its own code, which is too late to enforce any script execution control.
Quoted 4 lines
b> if both user space and kernel need to call
AT_CHECK, the faccessat seems to be a better place forAT_CHECK, e.g. kernel can calldo_faccessat(AT_CHECK)and userspace can callfaccessat(). This will avoid complicating theexecveat()code path.A previous version of this patches series already patched faccessat(2), but this is not the right place. faccessat2(2) is dedicated to check file permissions, not executability (e.g. with mount's noexec).
Quoted 1 line
Quoted 5 lines
Quoted 1 line
What do you think ?
I think there are some misunderstandings. Please let me know if it's clearer now.
I'm still not sure about the user case for dynamic linker (elf loading) case. Maybe this patch is more suitable for scripts? A detailed user case will help demonstrate the use case for dynamic linker, e.g. what kind of app will benefit from
SECBIT_EXEC_RESTRICT_FILE= 1, what kind of threat model are we dealing with , what kind of attack chain we blocked as a result.Quoted 176 lines
Quoted 176 lines
Thanks
-JeffQuoted 171 lines
With the information that a script interpreter is about to interpret a script, an LSM security policy can adjust caller's access rights or log execution request as for native script execution (e.g. role transition). This is possible thanks to the call to
security_bprm_creds_for_exec().Because LSMs may only change bprm's credentials, use of
AT_CHECKwith current kernel code should not be a security issue (e.g. unexpected role transition). LSMs willing to update the caller's credential could now do so whenbprm->is_checkis set. Of course, such policy change should be in line with the new user space code.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_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTsecurebits.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]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- fs/exec.c | 5 +++-- include/linux/binfmts.h | 7 ++++++- include/uapi/linux/fcntl.h | 30 ++++++++++++++++++++++++++++++ kernel/audit.h | 1 + kernel/auditsc.c | 1 + 5 files changed, 41 insertions(+), 3 deletions(-)fs/exec.c +3
-2@@ -931,7 +931,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 ;@@ -1595,6 +1595,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl bprm->filename = bprm->fdpath; } bprm->interp = bprm->filename; + bprm->is_check = !!(flags &AT_CHECK ); retval =bprm_mm_init (bprm);if (!retval)@@ -1885,7 +1886,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 +30
-0@@ -118,6 +118,36 @@#define AT_HANDLE_FID AT_REMOVEDIR/* file handle is needed to compare object identity and may not be usable to open_by_handle_at(2) */ + +/* +* 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). AT_CHECK +* should only be used if SECBIT_SHOULD_EXEC_CHECK is set for the calling +* thread. See securebits.h documentation. +* +* Programs should use this check to apply kernel-level checks against files +* that are not directly executed by the kernel but directly passed to a user +* space interpreter instead. All files that contain executable code, from the +* point of view of the interpreter, should be checked. 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
-1@@ -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; } --2.45.2
Mickaël Salaün
maintainerRFC PATCH v19 2/5 security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
In reply to Mickaël SalaünThese new SECBIT_SHOULD_EXEC_CHECK, SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.
When SECBIT_SHOULD_EXEC_CHECK is set, programs interpreting code should check executable resources with execveat(2) + AT_CHECK (see previous patch).
When SECBIT_SHOULD_EXEC_RESTRICT is set, a process should only allow execution of approved resources, if any (see SECBIT_SHOULD_EXEC_CHECK).
For a secure environment, we might also want SECBIT_SHOULD_EXEC_CHECK_LOCKED and SECBIT_SHOULD_EXEC_RESTRICT_LOCKED to be set. For a test environment (e.g. testing on a fleet to identify potential issues), only the SECBIT_SHOULD_EXEC_CHECK* bits can be set to still be able to identify potential issues (e.g. with interpreters logs or LSMs audit entries).
It should be noted that unlike other security bits, the SECBIT_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT 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 policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of LD_PRELOAD.
Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.
---
New design since v18:
https://lore.kernel.org/r/[email protected]
--- include/uapi/linux/securebits.h | 56 ++++++++++++++++++++++++++++- security/commoncap.c | 63 ++++++++++++++++++++++++++++----- 2 files changed, 110 insertions(+), 9 deletions(-)
include/uapi/linux/securebits.h +55 -1
@@ -52,10 +52,64 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* When SECBIT_SHOULD_EXEC_CHECK is set, a process should check all executable +* files with execveat(2) + AT_CHECK. However, such check should only be +* performed if all to-be-executed code only comes from regular files. For +* instance, if a script interpreter is called with both a script snipped as +* argument and a regular file, the interpreter should not check any file. +* Doing otherwise would mislead the kernel to think that only the script file +* is being executed, which could for instance lead to unexpected permission +* change and break current use cases. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_SHOULD_EXEC_CHECK_LOCKED bit should also be set. +* +* Ptracing another process is deny if the tracer has SECBIT_SHOULD_EXEC_CHECK +* but not the tracee. SECBIT_SHOULD_EXEC_CHECK_LOCKED also checked. +*/ +#define SECURE_SHOULD_EXEC_CHECK 8 +#define SECURE_SHOULD_EXEC_CHECK_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_SHOULD_EXEC_CHECK (issecure_mask(SECURE_SHOULD_EXEC_CHECK)) +#define SECBIT_SHOULD_EXEC_CHECK_LOCKED \ + (issecure_mask(SECURE_SHOULD_EXEC_CHECK_LOCKED)) + +/* +* When SECBIT_SHOULD_EXEC_RESTRICT is set, a process should only allow +* execution of approved files, if any (see SECBIT_SHOULD_EXEC_CHECK). For +* instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_SHOULD_EXEC_RESTRICT is set. +* However, if a script interpreter is called with both +* SECBIT_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT, they should +* interpret the provided script files if no unchecked code is also provided +* (e.g. directly as argument). +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_SHOULD_EXEC_RESTRICT_LOCKED bit should also be set. +* +* Ptracing another process is deny if the tracer has +* SECBIT_SHOULD_EXEC_RESTRICT but not the tracee. +* SECBIT_SHOULD_EXEC_RESTRICT_LOCKED is also checked. +*/ +#define SECURE_SHOULD_EXEC_RESTRICT 10 +#define SECURE_SHOULD_EXEC_RESTRICT_LOCKED 11/* make bit-8 immutable */ + +#define SECBIT_SHOULD_EXEC_RESTRICT (issecure_mask(SECURE_SHOULD_EXEC_RESTRICT)) +#define SECBIT_SHOULD_EXEC_RESTRICT_LOCKED \ + (issecure_mask(SECURE_SHOULD_EXEC_RESTRICT_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_SHOULD_EXEC_CHECK) | \ + issecure_mask(SECURE_SHOULD_EXEC_RESTRICT))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_SHOULD_EXEC_CHECK) | \ + issecure_mask(SECURE_SHOULD_EXEC_RESTRICT)) +#endif /* _UAPI_LINUX_SECUREBITS_H */
security/commoncap.c +55 -8
96 lines not on this page · raw message
Signature · 1 line
2.45.2
Kees Cook
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 8 lines · Mickaël Salaün
Such a secure environment can be achieved with an appropriate access control policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
If the threat model includes an attacker sitting at a shell prompt, we need to be very careful about how process perform enforcement. E.g. even on a locked down system, if an attacker has access to LD_PRELOAD or a seccomp wrapper (which you both mention here), it would be possible to run commands where the resulting process is tricked into thinking it doesn't have the bits set.
But this would be exactly true for calling execveat(): LD_PRELOAD or seccomp policy could have it just return 0.
While I like AT_CHECK, I do wonder if it's better to do the checks via open(), as was originally designed with O_MAYEXEC. Because then enforcement is gated by the kernel -- the process does not get a file descriptor _at all_, no matter what LD_PRELOAD or seccomp tricks it into doing.
And this thinking also applies to faccessat() too: if a process can be tricked into thinking the access check passed, it'll happily interpret whatever. :( But not being able to open the fd _at all_ when O_MAYEXEC is being checked seems substantially safer to me...
Signature · 1 line
Kees Cook
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 7 lines · Kees Cook
Quoted 6 lines · Mickaël Salaün
Such a secure environment can be achieved with an appropriate access control policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.
Quoted 6 lines
Quoted 2 lines
Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
If the threat model includes an attacker sitting at a shell prompt, we need to be very careful about how process perform enforcement. E.g. even on a locked down system, if an attacker has access to
LD_PRELOADor a
LD_PRELOAD should be OK once ld.so will be patched to check the libraries. We can still imagine a debug library used to bypass security checks, but in this case the issue would be that this library is executable in the first place.
Quoted 3 lines
seccomp wrapper (which you both mention here), it would be possible to run commands where the resulting process is tricked into thinking it doesn't have the bits set.
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
Quoted 3 lines
But this would be exactly true for calling
execveat():LD_PRELOADor seccomp policy could have it just return 0.
If an attacker is allowed/able to load an arbitrary seccomp filter on a process, we cannot trust this process.
Quoted 6 lines
While I like
AT_CHECK, I do wonder if it's better to do the checks viaopen(), as was originally designed withO_MAYEXEC. Because then enforcement is gated by the kernel -- the process does not get a file descriptor_atall_, no matter whatLD_PRELOADor seccomp tricks it into doing.
Being able to check a path name or a file descriptor (with the same syscall) is more flexible and cover more use cases. The execveat(2) interface, including current and future flags, is dedicated to file execution. I then think that using execveat(2) for this kind of check makes more sense, and will easily evolve with this syscall.
Quoted 5 lines
And this thinking also applies to
faccessat()too: if a process can be tricked into thinking the access check passed, it'll happily interpret whatever. :( But not being able to open the fd_atall_whenO_MAYEXECis being checked seems substantially safer to me...
If attackers can filter execveat(2), they can also filter open(2) and any other syscalls. In all cases, that would mean an issue in the security policy.
Kees Cook
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 11 lines · Mickaël Salaün
Quoted 10 lines · Kees Cook
Quoted 8 lines · Mickaël Salaün
Such a secure environment can be achieved with an appropriate access control policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
Quoted 8 lines
Quoted 3 lines
If the threat model includes an attacker sitting at a shell prompt, we need to be very careful about how process perform enforcement. E.g. even on a locked down system, if an attacker has access to
LD_PRELOADor a
LD_PRELOADshould be OK once ld.so will be patched to check the libraries. We can still imagine a debug library used to bypass security checks, but in this case the issue would be that this library is executable in the first place.
Ah yes, that's fair: the shell would discover the malicious library while using AT_CHECK during resolution of the LD_PRELOAD.
Quoted 8 lines
Quoted 3 lines
seccomp wrapper (which you both mention here), it would be possible to run commands where the resulting process is tricked into thinking it doesn't have the bits set.
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Quoted 7 lines
Quoted 2 lines
But this would be exactly true for calling
execveat():LD_PRELOADor seccomp policy could have it just return 0.If an attacker is allowed/able to load an arbitrary seccomp filter on a process, we cannot trust this process.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
While I like
AT_CHECK, I do wonder if it's better to do the checks viaopen(), as was originally designed withO_MAYEXEC. Because then enforcement is gated by the kernel -- the process does not get a file descriptor_atall_, no matter whatLD_PRELOADor seccomp tricks it into doing.Being able to check a path name or a file descriptor (with the same syscall) is more flexible and cover more use cases.
If flexibility costs us reliability, I think that flexibility is not a benefit.
Quoted 4 lines
The execveat(2)
interface, including current and future flags, is dedicated to file execution. I then think that using execveat(2) for this kind of check makes more sense, and will easily evolve with this syscall.
Yeah, I do recognize that is feels much more natural, but I remain unhappy about how difficult it will become to audit a system for safety when the check is strictly per-process opt-in, and not enforced by the kernel for a given process tree. But, I think this may have always been a fiction in my mind. :)
Quoted 8 lines
Quoted 4 lines
And this thinking also applies to
faccessat()too: if a process can be tricked into thinking the access check passed, it'll happily interpret whatever. :( But not being able to open the fd_atall_whenO_MAYEXECis being checked seems substantially safer to me...If attackers can filter execveat(2), they can also filter open(2) and any other syscalls. In all cases, that would mean an issue in the security policy.
Hm, as in, make a separate call to open(2) without O_MAYEXEC, and pass that fd back to the filtered open(2) that did have O_MAYEXEC. Yes, true.
I guess it does become morally equivalent.
Okay. Well, let me ask about usability. Right now, a process will need to do:
- should I use AT_CHECK? (check secbit)
- if yes: perform execveat(AT_CHECK)
Why not leave the secbit test up to the kernel, and then the program can just unconditionally call execveat(AT_CHECK)?
Though perhaps the issue here is that an execveat() EINVAL doesn't tell the program if AT_CHECK is unimplemented or if something else went wrong, and the secbit prctl() will give the correct signal about AT_CHECK availability?
Signature · 1 line
Kees Cook
Jarkko Sakkinen
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 8 lines · Kees Cook
Quoted 4 lines
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Or a regular web browser? AFAIK seccomp filtering was the tool to make secure browser tabs in the first place.
BR, Jarkko
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 6 lines · Jarkko Sakkinen
Quoted 5 lines · Kees Cook
Quoted 4 lines
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
Quoted 6 lines
Quoted 3 lines
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Or a regular web browser? AFAIK seccomp filtering was the tool to make secure browser tabs in the first place.
Yes, and that't OK. Web browsers embedded their own seccomp filters and they are then as trusted as the browser code.
Jarkko Sakkinen
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 11 lines · Mickaël Salaün
Quoted 10 lines · Jarkko Sakkinen
Quoted 8 lines · Kees Cook
Quoted 4 lines
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Quoted 5 lines
Quoted 2 lines
Or a regular web browser? AFAIK seccomp filtering was the tool to make secure browser tabs in the first place.
Yes, and that't OK. Web browsers embedded their own seccomp filters and they are then as trusted as the browser code.
I'd recommend to slice of tech detail from cover letter, as long as those details are in the commit messages.
Then, in the cover letter I'd go through maybe two familiar scenarios, with interactions to this functionality.
A desktop web browser could be perhaps one of them...
BR, Jarkko
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
In reply to Kees CookQuoted 16 lines · Kees Cook
Quoted 15 lines · Mickaël Salaün
Quoted 13 lines · Kees Cook
Quoted 8 lines · Mickaël Salaün
Such a secure environment can be achieved with an appropriate access control policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
If the threat model includes an attacker sitting at a shell prompt, we need to be very careful about how process perform enforcement. E.g. even on a locked down system, if an attacker has access to
LD_PRELOADor a
Quoted 7 lines
Quoted 4 lines
LD_PRELOADshould be OK once ld.so will be patched to check the libraries. We can still imagine a debug library used to bypass security checks, but in this case the issue would be that this library is executable in the first place.Ah yes, that's fair: the shell would discover the malicious library while using
AT_CHECKduring resolution of theLD_PRELOAD.
That's the idea, but it would be checked by ld.so, not the shell.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
seccomp wrapper (which you both mention here), it would be possible to run commands where the resulting process is tricked into thinking it doesn't have the bits set.
Quoted 8 lines
Quoted 4 lines
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Processes running with untrusted seccomp filter should be considered untrusted. It would then make sense for these seccomp filters/programs to be considered executable code, and then for minijail and systemd to check them with AT_CHECK (according to the securebits policy).
Quoted 14 lines
Quoted 14 lines
Quoted 3 lines
But this would be exactly true for calling
execveat():LD_PRELOADor seccomp policy could have it just return 0.If an attacker is allowed/able to load an arbitrary seccomp filter on a process, we cannot trust this process.
Quoted 6 lines
While I like
AT_CHECK, I do wonder if it's better to do the checks viaopen(), as was originally designed withO_MAYEXEC. Because then enforcement is gated by the kernel -- the process does not get a file descriptor_atall_, no matter whatLD_PRELOADor seccomp tricks it into doing.
Quoted 5 lines
Quoted 2 lines
Being able to check a path name or a file descriptor (with the same syscall) is more flexible and cover more use cases.
If flexibility costs us reliability, I think that flexibility is not a benefit.
Well, it's a matter of letting user space do what they think is best, and I think there are legitimate and safe uses of path names, even if I agree that this should not be used in most use cases. Would we want faccessat2(2) to only take file descriptor as argument and not file path? I don't think so but I'd defer to the VFS maintainers.
Christian, Al, Linus?
Steve, could you share a use case with file paths?
Quoted 11 lines
Quoted 5 lines
The execveat(2)
interface, including current and future flags, is dedicated to file execution. I then think that using execveat(2) for this kind of check makes more sense, and will easily evolve with this syscall.Yeah, I do recognize that is feels much more natural, but I remain unhappy about how difficult it will become to audit a system for safety when the check is strictly per-process opt-in, and not enforced by the kernel for a given process tree. But, I think this may have always been a fiction in my mind. :)
Hmm, I'm not sure to follow. Securebits are inherited, so process tree. And we need the parent processes to be trusted anyway.
Quoted 15 lines
Quoted 9 lines
Quoted 5 lines
And this thinking also applies to
faccessat()too: if a process can be tricked into thinking the access check passed, it'll happily interpret whatever. :( But not being able to open the fd_atall_whenO_MAYEXECis being checked seems substantially safer to me...If attackers can filter execveat(2), they can also filter open(2) and any other syscalls. In all cases, that would mean an issue in the security policy.
Hm, as in, make a separate call to open(2) without
O_MAYEXEC, and pass that fd back to the filtered open(2) that did haveO_MAYEXEC. Yes, true.I guess it does become morally equivalent.
Quoted 8 lines
Okay. Well, let me ask about usability. Right now, a process will need to do:
- should I use
AT_CHECK? (check secbit)
- if yes: performexecveat(AT_CHECK)Why not leave the secbit test up to the kernel, and then the program can just unconditionally call
execveat(AT_CHECK)?
That was kind of the approach of the previous patch series and Linus wanted the new interface to follow the kernel semantic. Enforcing this kind of restriction will always be the duty of user space anyway, so I think it's simpler (i.e. no mix of policy definition, access check, and policy enforcement, but a standalone execveat feature), more flexible, and it fully delegates the policy enforcement to user space instead of trying to enforce some part in the kernel which would only give the illusion of security/policy enforcement.
Quoted 5 lines
Though perhaps the issue here is that an
execveat()EINVAL doesn't tell the program ifAT_CHECKis unimplemented or if something else went wrong, and the secbitprctl()will give the correct signal aboutAT_CHECKavailability?
This kind of check could indeed help to identify the issue.
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 115 lines · Mickaël Salaün
Quoted 16 lines · Kees Cook
Quoted 15 lines · Mickaël Salaün
Quoted 13 lines · Kees Cook
Quoted 8 lines · Mickaël Salaün
Such a secure environment can be achieved with an appropriate access control policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
If the threat model includes an attacker sitting at a shell prompt, we need to be very careful about how process perform enforcement. E.g. even on a locked down system, if an attacker has access to
LD_PRELOADor aQuoted 7 lines
Quoted 4 lines
LD_PRELOADshould be OK once ld.so will be patched to check the libraries. We can still imagine a debug library used to bypass security checks, but in this case the issue would be that this library is executable in the first place.Ah yes, that's fair: the shell would discover the malicious library while using
AT_CHECKduring resolution of theLD_PRELOAD.That's the idea, but it would be checked by ld.so, not the shell.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
seccomp wrapper (which you both mention here), it would be possible to run commands where the resulting process is tricked into thinking it doesn't have the bits set.
Quoted 8 lines
Quoted 4 lines
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Processes running with untrusted seccomp filter should be considered untrusted. It would then make sense for these seccomp filters/programs to be considered executable code, and then for minijail and systemd to check them with
AT_CHECK(according to the securebits policy).Quoted 14 lines
Quoted 14 lines
Quoted 3 lines
But this would be exactly true for calling
execveat():LD_PRELOADor seccomp policy could have it just return 0.If an attacker is allowed/able to load an arbitrary seccomp filter on a process, we cannot trust this process.
Quoted 6 lines
While I like
AT_CHECK, I do wonder if it's better to do the checks viaopen(), as was originally designed withO_MAYEXEC. Because then enforcement is gated by the kernel -- the process does not get a file descriptor_atall_, no matter whatLD_PRELOADor seccomp tricks it into doing.Quoted 5 lines
Quoted 2 lines
Being able to check a path name or a file descriptor (with the same syscall) is more flexible and cover more use cases.
If flexibility costs us reliability, I think that flexibility is not a benefit.
Well, it's a matter of letting user space do what they think is best, and I think there are legitimate and safe uses of path names, even if I agree that this should not be used in most use cases. Would we want faccessat2(2) to only take file descriptor as argument and not file path? I don't think so but I'd defer to the VFS maintainers.
Christian, Al, Linus?
Steve, could you share a use case with file paths?
Quoted 11 lines
Quoted 5 lines
The execveat(2)
interface, including current and future flags, is dedicated to file execution. I then think that using execveat(2) for this kind of check makes more sense, and will easily evolve with this syscall.Yeah, I do recognize that is feels much more natural, but I remain unhappy about how difficult it will become to audit a system for safety when the check is strictly per-process opt-in, and not enforced by the kernel for a given process tree. But, I think this may have always been a fiction in my mind. :)
Hmm, I'm not sure to follow. Securebits are inherited, so process tree. And we need the parent processes to be trusted anyway.
Quoted 15 lines
Quoted 9 lines
Quoted 5 lines
And this thinking also applies to
faccessat()too: if a process can be tricked into thinking the access check passed, it'll happily interpret whatever. :( But not being able to open the fd_atall_whenO_MAYEXECis being checked seems substantially safer to me...If attackers can filter execveat(2), they can also filter open(2) and any other syscalls. In all cases, that would mean an issue in the security policy.
Hm, as in, make a separate call to open(2) without
O_MAYEXEC, and pass that fd back to the filtered open(2) that did haveO_MAYEXEC. Yes, true.I guess it does become morally equivalent.
Quoted 8 lines
Okay. Well, let me ask about usability. Right now, a process will need to do:
- should I use
AT_CHECK? (check secbit)
- if yes: performexecveat(AT_CHECK)Why not leave the secbit test up to the kernel, and then the program can just unconditionally call
execveat(AT_CHECK)?
Quoted 8 lines
That was kind of the approach of the previous patch series and Linus wanted the new interface to follow the kernel semantic. Enforcing this kind of restriction will always be the duty of user space anyway, so I think it's simpler (i.e. no mix of policy definition, access check, and policy enforcement, but a standalone execveat feature), more flexible, and it fully delegates the policy enforcement to user space instead of trying to enforce some part in the kernel which would only give the illusion of
security/policyenforcement.
A problem could be that from IMA perspective there is no indication on whether the interpreter executed or not execveat(). Sure, we can detect that the binary supports it, but if the enforcement was enabled/disabled that it is not recorded.
Maybe, setting the process flags should be influenced by the kernel, for example not allowing changes and enforcing when there is an IMA policy loaded requiring to measure/appraise scripts.
Roberto
Quoted 7 lines
Quoted 5 lines
Though perhaps the issue here is that an
execveat()EINVAL doesn't tell the program ifAT_CHECKis unimplemented or if something else went wrong, and the secbitprctl()will give the correct signal aboutAT_CHECKavailability?This kind of check could indeed help to identify the issue.
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 125 lines · Roberto Sassu
Quoted 115 lines · Mickaël Salaün
Quoted 16 lines · Kees Cook
Quoted 15 lines · Mickaël Salaün
Quoted 13 lines · Kees Cook
Quoted 8 lines · Mickaël Salaün
Such a secure environment can be achieved with an appropriate access control policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
If the threat model includes an attacker sitting at a shell prompt, we need to be very careful about how process perform enforcement. E.g. even on a locked down system, if an attacker has access to
LD_PRELOADor aQuoted 7 lines
Quoted 4 lines
LD_PRELOADshould be OK once ld.so will be patched to check the libraries. We can still imagine a debug library used to bypass security checks, but in this case the issue would be that this library is executable in the first place.Ah yes, that's fair: the shell would discover the malicious library while using
AT_CHECKduring resolution of theLD_PRELOAD.That's the idea, but it would be checked by ld.so, not the shell.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
seccomp wrapper (which you both mention here), it would be possible to run commands where the resulting process is tricked into thinking it doesn't have the bits set.
Quoted 8 lines
Quoted 4 lines
As explained in the UAPI comments, all parent processes need to be trusted. This meeans that their code is trusted, their seccomp filters are trusted, and that they are patched, if needed, to check file executability.
But we have launchers that apply arbitrary seccomp policy, e.g. minijail on Chrome OS, or even systemd on regular distros. In theory, this should be handled via other ACLs.
Processes running with untrusted seccomp filter should be considered untrusted. It would then make sense for these seccomp filters/programs to be considered executable code, and then for minijail and systemd to check them with
AT_CHECK(according to the securebits policy).Quoted 14 lines
Quoted 14 lines
Quoted 3 lines
But this would be exactly true for calling
execveat():LD_PRELOADor seccomp policy could have it just return 0.If an attacker is allowed/able to load an arbitrary seccomp filter on a process, we cannot trust this process.
Quoted 6 lines
While I like
AT_CHECK, I do wonder if it's better to do the checks viaopen(), as was originally designed withO_MAYEXEC. Because then enforcement is gated by the kernel -- the process does not get a file descriptor_atall_, no matter whatLD_PRELOADor seccomp tricks it into doing.Quoted 5 lines
Quoted 2 lines
Being able to check a path name or a file descriptor (with the same syscall) is more flexible and cover more use cases.
If flexibility costs us reliability, I think that flexibility is not a benefit.
Well, it's a matter of letting user space do what they think is best, and I think there are legitimate and safe uses of path names, even if I agree that this should not be used in most use cases. Would we want faccessat2(2) to only take file descriptor as argument and not file path? I don't think so but I'd defer to the VFS maintainers.
Christian, Al, Linus?
Steve, could you share a use case with file paths?
Quoted 11 lines
Quoted 5 lines
The execveat(2)
interface, including current and future flags, is dedicated to file execution. I then think that using execveat(2) for this kind of check makes more sense, and will easily evolve with this syscall.Yeah, I do recognize that is feels much more natural, but I remain unhappy about how difficult it will become to audit a system for safety when the check is strictly per-process opt-in, and not enforced by the kernel for a given process tree. But, I think this may have always been a fiction in my mind. :)
Hmm, I'm not sure to follow. Securebits are inherited, so process tree. And we need the parent processes to be trusted anyway.
Quoted 15 lines
Quoted 9 lines
Quoted 5 lines
And this thinking also applies to
faccessat()too: if a process can be tricked into thinking the access check passed, it'll happily interpret whatever. :( But not being able to open the fd_atall_whenO_MAYEXECis being checked seems substantially safer to me...If attackers can filter execveat(2), they can also filter open(2) and any other syscalls. In all cases, that would mean an issue in the security policy.
Hm, as in, make a separate call to open(2) without
O_MAYEXEC, and pass that fd back to the filtered open(2) that did haveO_MAYEXEC. Yes, true.I guess it does become morally equivalent.
Quoted 8 lines
Okay. Well, let me ask about usability. Right now, a process will need to do:
- should I use
AT_CHECK? (check secbit)
- if yes: performexecveat(AT_CHECK)Why not leave the secbit test up to the kernel, and then the program can just unconditionally call
execveat(AT_CHECK)?Quoted 8 lines
That was kind of the approach of the previous patch series and Linus wanted the new interface to follow the kernel semantic. Enforcing this kind of restriction will always be the duty of user space anyway, so I think it's simpler (i.e. no mix of policy definition, access check, and policy enforcement, but a standalone execveat feature), more flexible, and it fully delegates the policy enforcement to user space instead of trying to enforce some part in the kernel which would only give the illusion of
security/policyenforcement.
Quoted 4 lines
A problem could be that from IMA perspective there is no indication on whether the interpreter executed or not
execveat(). Sure, we can detect that the binary supports it, but if the enforcement was enabled/disabled that it is not recorded.
We should assume that if the interpreter call execveat+AT_CHECK, it will enforce restrictions according to its securebits.
Quoted 4 lines
Maybe, setting the process flags should be influenced by the kernel, for example not allowing changes and enforcing when there is an IMA policy loaded requiring to measure/appraise scripts.
LSMs can set the required securebits per task/interpreter according to their policies.
Quoted 12 lines
Roberto
Quoted 7 lines
Quoted 5 lines
Though perhaps the issue here is that an
execveat()EINVAL doesn't tell the program ifAT_CHECKis unimplemented or if something else went wrong, and the secbitprctl()will give the correct signal aboutAT_CHECKavailability?This kind of check could indeed help to identify the issue.
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
In reply to Mickaël SalaünHi
Quoted 7 lines · Mickaël Salaün
These new
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.
Quoted 7 lines
When
SECBIT_SHOULD_EXEC_CHECKis set, programs interpreting code should check executable resources with execveat(2) +AT_CHECK(see previous patch).When
SECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow execution of approved resources, if any (seeSECBIT_SHOULD_EXEC_CHECK).
Do we need both bits ?
When CHECK is set and RESTRICT is not, the "check fail" executable will still get executed, so CHECK is for logging ?
Does RESTRICT imply CHECK is set, e.g. What if CHECK=0 and RESTRICT = 1 ?
Quoted 224 lines
For a secure environment, we might also want
SECBIT_SHOULD_EXEC_CHECK_LOCKEDandSECBIT_SHOULD_EXEC_RESTRICT_LOCKEDto be set. For a test environment (e.g. testing on a fleet to identify potential issues), only the SECBIT_SHOULD_EXEC_CHECK* bits can be set to still be able to identify potential issues (e.g. with interpreters logs or LSMs audit entries).It should be noted that unlike other security bits, the
SECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTbits 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 policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has
CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.Cc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- include/uapi/linux/securebits.h | 56 ++++++++++++++++++++++++++++- security/commoncap.c | 63 ++++++++++++++++++++++++++++----- 2 files changed, 110 insertions(+), 9 deletions(-)include/uapi/linux/securebits.h +55
-1@@ -52,10 +52,64 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* When SECBIT_SHOULD_EXEC_CHECK is set, a process should check all executable +* files with execveat(2) + AT_CHECK. However, such check should only be +* performed if all to-be-executed code only comes from regular files. For +* instance, if a script interpreter is called with both a script snipped as +* argument and a regular file, the interpreter should not check any file. +* Doing otherwise would mislead the kernel to think that only the script file +* is being executed, which could for instance lead to unexpected permission +* change and break current use cases. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_SHOULD_EXEC_CHECK_LOCKED bit should also be set. +* +* Ptracing another process is deny if the tracer has SECBIT_SHOULD_EXEC_CHECK +* but not the tracee. SECBIT_SHOULD_EXEC_CHECK_LOCKED also checked. +*/ +#define SECURE_SHOULD_EXEC_CHECK 8 +#define SECURE_SHOULD_EXEC_CHECK_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_SHOULD_EXEC_CHECK (issecure_mask(SECURE_SHOULD_EXEC_CHECK)) +#define SECBIT_SHOULD_EXEC_CHECK_LOCKED \ + (issecure_mask(SECURE_SHOULD_EXEC_CHECK_LOCKED)) + +/* +* When SECBIT_SHOULD_EXEC_RESTRICT is set, a process should only allow +* execution of approved files, if any (see SECBIT_SHOULD_EXEC_CHECK). For +* instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_SHOULD_EXEC_RESTRICT is set. +* However, if a script interpreter is called with both +* SECBIT_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT, they should +* interpret the provided script files if no unchecked code is also provided +* (e.g. directly as argument). +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_SHOULD_EXEC_RESTRICT_LOCKED bit should also be set. +* +* Ptracing another process is deny if the tracer has +* SECBIT_SHOULD_EXEC_RESTRICT but not the tracee. +* SECBIT_SHOULD_EXEC_RESTRICT_LOCKED is also checked. +*/ +#define SECURE_SHOULD_EXEC_RESTRICT 10 +#define SECURE_SHOULD_EXEC_RESTRICT_LOCKED 11/* make bit-8 immutable */ + +#define SECBIT_SHOULD_EXEC_RESTRICT (issecure_mask(SECURE_SHOULD_EXEC_RESTRICT)) +#define SECBIT_SHOULD_EXEC_RESTRICT_LOCKED \ + (issecure_mask(SECURE_SHOULD_EXEC_RESTRICT_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_SHOULD_EXEC_CHECK) | \ + issecure_mask(SECURE_SHOULD_EXEC_RESTRICT))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_SHOULD_EXEC_CHECK) | \ + issecure_mask(SECURE_SHOULD_EXEC_RESTRICT)) +#endif /* _UAPI_LINUX_SECUREBITS_H */ security/commoncap.c +55
-9@@ -117,6 +117,33 @@ int cap_settime(const struct timespec64 *ts, const struct timezone *tz)return 0 ; } +static bool ptrace_secbits_allowed (const struct cred *tracer, +const struct cred *tracee) +{ +const unsigned long tracer_secbits =SECURE_ALL_UNPRIVILEGED & + tracer->securebits; +const unsigned long tracee_secbits =SECURE_ALL_UNPRIVILEGED & + tracee->securebits; +/* Ignores locking of unset secure bits (cf. SECURE_ALL_LOCKS). */ +const unsigned long tracer_locked = (tracer_secbits <<1 ) & + tracer->securebits; +const unsigned long tracee_locked = (tracee_secbits <<1 ) & + tracee->securebits; + +/* The tracee must not have less constraints than the tracer. */ +if ((tracer_secbits | tracee_secbits) != tracee_secbits) +return false; + +/* +* Makes sure that the tracer's locks for restrictions are the same for +* the tracee. +*/ +if ((tracer_locked | tracee_locked) != tracee_locked) +return false; + +return true; +} +/** * cap_ptrace_access_check - Determine whether the current process may access * another @@ -146,7 +173,8 @@ int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)else caller_caps = &cred->cap_permitted;if (cred->user_ns == child_cred->user_ns && -cap_issubset (child_cred->cap_permitted, *caller_caps)) +cap_issubset (child_cred->cap_permitted, *caller_caps) && +ptrace_secbits_allowed (cred, child_cred)) goto out;if (ns_capable (child_cred->user_ns,CAP_SYS_PTRACE )) goto out;@@ -178,7 +206,8 @@ int cap_ptrace_traceme(struct task_struct *parent) cred =__task_cred (parent); child_cred =current_cred ();if (cred->user_ns == child_cred->user_ns && -cap_issubset (child_cred->cap_permitted, cred->cap_permitted)) +cap_issubset (child_cred->cap_permitted, cred->cap_permitted) && +ptrace_secbits_allowed (cred, child_cred)) goto out;if (has_ns_capability (parent, child_cred->user_ns,CAP_SYS_PTRACE )) goto out;@@ -1302,21 +1331,39 @@ 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. The kernel is only in charge of protecting against +* privilege escalation with ptrace protections. +*/ +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 ; --2.45.2
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 15 lines · Jeff Xu
Hi
Quoted 11 lines · Mickaël Salaün
These new
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.When
SECBIT_SHOULD_EXEC_CHECKis set, programs interpreting code should check executable resources with execveat(2) +AT_CHECK(see previous patch).
Quoted 8 lines
Quoted 3 lines
When
SECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow execution of approved resources, if any (seeSECBIT_SHOULD_EXEC_CHECK).Do we need both bits ?
When CHECK is set and RESTRICT is not, the "check fail" executable will still get executed, so CHECK is for logging ?
Does RESTRICT imply CHECK is set, e.g. What if CHECK=0 and RESTRICT = 1 ?
The intention might be "permissive mode"? if so, consider reuse existing selinux's concept, and still with 2 bits:SECBIT_SHOULD_EXEC_RESTRICT SECBIT_SHOULD_EXEC_RESTRICT_PERMISSIVE
-Jeff
Quoted 224 lines
Quoted 224 lines
For a secure environment, we might also want
SECBIT_SHOULD_EXEC_CHECK_LOCKEDandSECBIT_SHOULD_EXEC_RESTRICT_LOCKEDto be set. For a test environment (e.g. testing on a fleet to identify potential issues), only the SECBIT_SHOULD_EXEC_CHECK* bits can be set to still be able to identify potential issues (e.g. with interpreters logs or LSMs audit entries).It should be noted that unlike other security bits, the
SECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTbits 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 policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has
CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.Cc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
New design since v18:
https://lore.kernel.org/r/[email protected]--- include/uapi/linux/securebits.h | 56 ++++++++++++++++++++++++++++- security/commoncap.c | 63 ++++++++++++++++++++++++++++----- 2 files changed, 110 insertions(+), 9 deletions(-)include/uapi/linux/securebits.h +55
-1@@ -52,10 +52,64 @@#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED \ (issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED)) +/* +* When SECBIT_SHOULD_EXEC_CHECK is set, a process should check all executable +* files with execveat(2) + AT_CHECK. However, such check should only be +* performed if all to-be-executed code only comes from regular files. For +* instance, if a script interpreter is called with both a script snipped as +* argument and a regular file, the interpreter should not check any file. +* Doing otherwise would mislead the kernel to think that only the script file +* is being executed, which could for instance lead to unexpected permission +* change and break current use cases. +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_SHOULD_EXEC_CHECK_LOCKED bit should also be set. +* +* Ptracing another process is deny if the tracer has SECBIT_SHOULD_EXEC_CHECK +* but not the tracee. SECBIT_SHOULD_EXEC_CHECK_LOCKED also checked. +*/ +#define SECURE_SHOULD_EXEC_CHECK 8 +#define SECURE_SHOULD_EXEC_CHECK_LOCKED 9/* make bit-8 immutable */ + +#define SECBIT_SHOULD_EXEC_CHECK (issecure_mask(SECURE_SHOULD_EXEC_CHECK)) +#define SECBIT_SHOULD_EXEC_CHECK_LOCKED \ + (issecure_mask(SECURE_SHOULD_EXEC_CHECK_LOCKED)) + +/* +* When SECBIT_SHOULD_EXEC_RESTRICT is set, a process should only allow +* execution of approved files, if any (see SECBIT_SHOULD_EXEC_CHECK). For +* instance, script interpreters called with a script snippet as argument +* should always deny such execution if SECBIT_SHOULD_EXEC_RESTRICT is set. +* However, if a script interpreter is called with both +* SECBIT_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT, they should +* interpret the provided script files if no unchecked code is also provided +* (e.g. directly as argument). +* +* This secure bit may be set by user session managers, service managers, +* container runtimes, sandboxer tools... Except for test environments, the +* related SECBIT_SHOULD_EXEC_RESTRICT_LOCKED bit should also be set. +* +* Ptracing another process is deny if the tracer has +* SECBIT_SHOULD_EXEC_RESTRICT but not the tracee. +* SECBIT_SHOULD_EXEC_RESTRICT_LOCKED is also checked. +*/ +#define SECURE_SHOULD_EXEC_RESTRICT 10 +#define SECURE_SHOULD_EXEC_RESTRICT_LOCKED 11/* make bit-8 immutable */ + +#define SECBIT_SHOULD_EXEC_RESTRICT (issecure_mask(SECURE_SHOULD_EXEC_RESTRICT)) +#define SECBIT_SHOULD_EXEC_RESTRICT_LOCKED \ + (issecure_mask(SECURE_SHOULD_EXEC_RESTRICT_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_SHOULD_EXEC_CHECK) | \ + issecure_mask(SECURE_SHOULD_EXEC_RESTRICT))#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) +#define SECURE_ALL_UNPRIVILEGED (issecure_mask(SECURE_SHOULD_EXEC_CHECK) | \ + issecure_mask(SECURE_SHOULD_EXEC_RESTRICT)) +#endif /* _UAPI_LINUX_SECUREBITS_H */ security/commoncap.c +55
-9@@ -117,6 +117,33 @@ int cap_settime(const struct timespec64 *ts, const struct timezone *tz)return 0 ; } +static bool ptrace_secbits_allowed (const struct cred *tracer, +const struct cred *tracee) +{ +const unsigned long tracer_secbits =SECURE_ALL_UNPRIVILEGED & + tracer->securebits; +const unsigned long tracee_secbits =SECURE_ALL_UNPRIVILEGED & + tracee->securebits; +/* Ignores locking of unset secure bits (cf. SECURE_ALL_LOCKS). */ +const unsigned long tracer_locked = (tracer_secbits <<1 ) & + tracer->securebits; +const unsigned long tracee_locked = (tracee_secbits <<1 ) & + tracee->securebits; + +/* The tracee must not have less constraints than the tracer. */ +if ((tracer_secbits | tracee_secbits) != tracee_secbits) +return false; + +/* +* Makes sure that the tracer's locks for restrictions are the same for +* the tracee. +*/ +if ((tracer_locked | tracee_locked) != tracee_locked) +return false; + +return true; +} +/** * cap_ptrace_access_check - Determine whether the current process may access * another @@ -146,7 +173,8 @@ int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)else caller_caps = &cred->cap_permitted;if (cred->user_ns == child_cred->user_ns && -cap_issubset (child_cred->cap_permitted, *caller_caps)) +cap_issubset (child_cred->cap_permitted, *caller_caps) && +ptrace_secbits_allowed (cred, child_cred)) goto out;if (ns_capable (child_cred->user_ns,CAP_SYS_PTRACE )) goto out;@@ -178,7 +206,8 @@ int cap_ptrace_traceme(struct task_struct *parent) cred =__task_cred (parent); child_cred =current_cred ();if (cred->user_ns == child_cred->user_ns && -cap_issubset (child_cred->cap_permitted, cred->cap_permitted)) +cap_issubset (child_cred->cap_permitted, cred->cap_permitted) && +ptrace_secbits_allowed (cred, child_cred)) goto out;if (has_ns_capability (parent, child_cred->user_ns,CAP_SYS_PTRACE )) goto out;@@ -1302,21 +1331,39 @@ 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. The kernel is only in charge of protecting against +* privilege escalation with ptrace protections. +*/ +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 ; --2.45.2
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 24 lines · Jeff Xu
Quoted 15 lines · Jeff Xu
Hi
Quoted 11 lines · Mickaël Salaün
These new
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.When
SECBIT_SHOULD_EXEC_CHECKis set, programs interpreting code should check executable resources with execveat(2) +AT_CHECK(see previous patch).Quoted 8 lines
Quoted 3 lines
When
SECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow execution of approved resources, if any (seeSECBIT_SHOULD_EXEC_CHECK).Do we need both bits ?
When CHECK is set and RESTRICT is not, the "check fail" executable will still get executed, so CHECK is for logging ?
Does RESTRICT imply CHECK is set, e.g. What if CHECK=0 and RESTRICT = 1 ?
Quoted 4 lines
The intention might be "permissive mode"? if so, consider reuse existing selinux's concept, and still with 2 bits:
SECBIT_SHOULD_EXEC_RESTRICTSECBIT_SHOULD_EXEC_RESTRICT_PERMISSIVE
SECBIT_SHOULD_EXEC_CHECK is for user space to check with execveat+AT_CHECK.
SECBIT_SHOULD_EXEC_RESTRICT is for user space to restrict execution by default, and potentially allow some exceptions from the list of checked-and-allowed files, if SECBIT_SHOULD_EXEC_CHECK is set.
Without SECBIT_SHOULD_EXEC_CHECK, SECBIT_SHOULD_EXEC_RESTRICT is to deny any kind of execution/interpretation.
With only SECBIT_SHOULD_EXEC_CHECK, user space should just check and log any denied access, but ignore them. So yes, it is similar to the SELinux's permissive mode.
This is explained in the next patch as comments.
The *_LOCKED variants are useful and part of the securebits concept.
Quoted 46 lines
-Jeff
Quoted 39 lines
Quoted 39 lines
For a secure environment, we might also want
SECBIT_SHOULD_EXEC_CHECK_LOCKEDandSECBIT_SHOULD_EXEC_RESTRICT_LOCKEDto be set. For a test environment (e.g. testing on a fleet to identify potential issues), only the SECBIT_SHOULD_EXEC_CHECK* bits can be set to still be able to identify potential issues (e.g. with interpreters logs or LSMs audit entries).It should be noted that unlike other security bits, the
SECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTbits 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 policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has
CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.Cc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 31 lines · Mickaël Salaün
Quoted 24 lines · Jeff Xu
Quoted 15 lines · Jeff Xu
Hi
Quoted 11 lines · Mickaël Salaün
These new
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.When
SECBIT_SHOULD_EXEC_CHECKis set, programs interpreting code should check executable resources with execveat(2) +AT_CHECK(see previous patch).Quoted 8 lines
Quoted 3 lines
When
SECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow execution of approved resources, if any (seeSECBIT_SHOULD_EXEC_CHECK).Do we need both bits ?
When CHECK is set and RESTRICT is not, the "check fail" executable will still get executed, so CHECK is for logging ?
Does RESTRICT imply CHECK is set, e.g. What if CHECK=0 and RESTRICT = 1 ?Quoted 4 lines
The intention might be "permissive mode"? if so, consider reuse existing selinux's concept, and still with 2 bits:
SECBIT_SHOULD_EXEC_RESTRICTSECBIT_SHOULD_EXEC_RESTRICT_PERMISSIVE
Quoted 9 lines
SECBIT_SHOULD_EXEC_CHECKis for user space to check with execveat+AT_CHECK.
SECBIT_SHOULD_EXEC_RESTRICTis for user space to restrict execution by default, and potentially allow some exceptions from the list of checked-and-allowed files, ifSECBIT_SHOULD_EXEC_CHECKis set.Without
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICTis to deny any kind of execution/interpretation.
Do you mean "deny any kinds of executable/interpretation" or just those that failed with "AT_CHECK" ( I assume this)?
Quoted 4 lines
With only
SECBIT_SHOULD_EXEC_CHECK, user space should just check and log any denied access, but ignore them. So yes, it is similar to the SELinux's permissive mode.
IIUC:
CHECK=0, RESTRICT=0: do nothing, current behavior
CHECK=1, RESTRICT=0: permissive mode - ignore AT_CHECK results. CHECK=0, RESTRICT=1: call AT_CHECK, deny if AT_CHECK failed, no exception. CHECK=1, RESTRICT=1: call AT_CHECK, deny if AT_CHECK failed, except those in the "checked-and-allowed" list.
So CHECK is basically trying to form a allowlist?
If there is a need for a allowlist, that is the task of "interruptor or dynamic linker" to maintain this list, and the list is known in advance, i.e. not something from execveat(AT_CHECK), and kernel shouldn't have the knowledge of this allowlist.
Secondly, the concept of allow-list seems to be an attack factor for me, I would rather it be fully enforced, or permissive mode. And Check=1 and RESTRICT=1 is less secure than CHECK=0, RESTRICT=1, this might also be not obvious to dev.
Unless I understood the CHECK wrong.
Quoted 2 lines
This is explained in the next patch as comments.
The next patch is a selftest patch, it is better to define them in the current commit and in the securebits.h.
Quoted 2 lines
The
*_LOCKEDvariants are useful and part of the securebits concept.
The locked state is easy to understand.
Thanks
Best regards
-Jeff
Quoted 46 lines
Quoted 46 lines
-Jeff
Quoted 39 lines
Quoted 39 lines
For a secure environment, we might also want
SECBIT_SHOULD_EXEC_CHECK_LOCKEDandSECBIT_SHOULD_EXEC_RESTRICT_LOCKEDto be set. For a test environment (e.g. testing on a fleet to identify potential issues), only the SECBIT_SHOULD_EXEC_CHECK* bits can be set to still be able to identify potential issues (e.g. with interpreters logs or LSMs audit entries).It should be noted that unlike other security bits, the
SECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICTbits 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 policy (e.g. mount's noexec option, file access rights, LSM configuration) and an enlighten ld.so checking that libraries are allowed for execution e.g., to protect against illegitimate use of
LD_PRELOAD.Scripts may need some changes to deal with untrusted data (e.g. stdin, environment variables), but that is outside the scope of the kernel.
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has
CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.Cc Al Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Kees Cook <[email protected]>Cc Paul Moore <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 6 lines · Jeff Xu
IIUC:
CHECK=0, RESTRICT=0: do nothing, current behavior
CHECK=1, RESTRICT=0: permissive mode - ignoreAT_CHECKresults. CHECK=0, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, no exception. CHECK=1, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, except those in the "checked-and-allowed" list.
I had much the same question for Mickaël while working on this.
Essentially, "CHECK=0, RESTRICT=1" means to restrict without checking. In the context of a script or macro interpreter, this just means it will never interpret any scripts. Non-binary code execution is fully disabled in any part of the process that respects these bits.
"CHECK=1, RESTRICT=1" means to restrict unless AT_CHECK passes. This case is the allow list (or whatever mechanism is being used to determine the result of an AT_CHECK check). The actual mechanism isn't the business of the script interpreter at all, it just has to refuse to execute anything that doesn't pass the check. So a generic interpreter can implement a generic mechanism and leave the specifics to whoever configures the machine.
The other two case are more obvious. "CHECK=0, RESTRICT=0" is the zero-overhead case, while "CHECK=1, RESTRICT=0" might log, warn, or otherwise audit the result of the check, but it won't restrict execution.
Cheers,
Steve
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 9 lines · Steve Dower
Quoted 6 lines · Jeff Xu
IIUC:
CHECK=0, RESTRICT=0: do nothing, current behavior
CHECK=1, RESTRICT=0: permissive mode - ignoreAT_CHECKresults. CHECK=0, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, no exception. CHECK=1, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, except those in the "checked-and-allowed" list.
Quoted 7 lines
I had much the same question for Mickaël while working on this.
Essentially, "CHECK=0, RESTRICT=1" means to restrict without checking. In the context of a script or macro interpreter, this just means it will never interpret any scripts. Non-binary code execution is fully disabled in any part of the process that respects these bits.
I see, so Mickaël does mean this will block all scripts. I guess, in the context of dynamic linker, this means: no more .so loading, even "dlopen" is called by an app ? But this will make the execve() fail.
Quoted 8 lines
"CHECK=1, RESTRICT=1" means to restrict unless
AT_CHECKpasses. This case is the allow list (or whatever mechanism is being used to determine the result of anAT_CHECKcheck). The actual mechanism isn't the business of the script interpreter at all, it just has to refuse to execute anything that doesn't pass the check. So a generic interpreter can implement a generic mechanism and leave the specifics to whoever configures the machine.
In the context of dynamic linker. this means:
if .so passed the AT_CHECK, ldopen() can still load it. If .so fails the AT_CHECK, ldopen() will fail too.
Thanks
-Jeff
Quoted 6 lines
The other two case are more obvious. "CHECK=0, RESTRICT=0" is the zero-overhead case, while "CHECK=1, RESTRICT=0" might log, warn, or otherwise audit the result of the check, but it won't restrict execution.
Cheers,
Steve
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 10 lines · Jeff Xu
Quoted 9 lines · Steve Dower
Quoted 6 lines · Jeff Xu
IIUC:
CHECK=0, RESTRICT=0: do nothing, current behavior
CHECK=1, RESTRICT=0: permissive mode - ignoreAT_CHECKresults. CHECK=0, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, no exception. CHECK=1, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, except those in the "checked-and-allowed" list.
Quoted 8 lines
Quoted 7 lines
I had much the same question for Mickaël while working on this.
Essentially, "CHECK=0, RESTRICT=1" means to restrict without checking. In the context of a script or macro interpreter, this just means it will never interpret any scripts. Non-binary code execution is fully disabled in any part of the process that respects these bits.
I see, so Mickaël does mean this will block all scripts.
That is the initial idea.
Quoted 3 lines
I guess, in the context of dynamic linker, this means: no more .so loading, even "dlopen" is called by an app ? But this will make the
execve()fail.
Hmm, I'm not sure this "CHECK=0, RESTRICT=1" configuration would make sense for a dynamic linker except maybe if we want to only allow static binaries?
The CHECK and RESTRICT securebits are designed to make it possible a "permissive mode" and an enforcement mode with the related locked securebits. This is why this "CHECK=0, RESTRICT=1" combination looks a bit weird. We can replace these securebits with others but I didn't find a better (and simple) option. I don't think this is an issue because with any security policy we can create unusable combinations. The three other combinations makes a lot of sense though.
Quoted 9 lines
Quoted 9 lines
"CHECK=1, RESTRICT=1" means to restrict unless
AT_CHECKpasses. This case is the allow list (or whatever mechanism is being used to determine the result of anAT_CHECKcheck). The actual mechanism isn't the business of the script interpreter at all, it just has to refuse to execute anything that doesn't pass the check. So a generic interpreter can implement a generic mechanism and leave the specifics to whoever configures the machine.
Quoted 3 lines
In the context of dynamic linker. this means:
if .so passed theAT_CHECK,ldopen()can still load it. If .so fails theAT_CHECK,ldopen()will fail too.
Correct
Quoted 10 lines
Thanks
-JeffQuoted 6 lines
The other two case are more obvious. "CHECK=0, RESTRICT=0" is the zero-overhead case, while "CHECK=1, RESTRICT=0" might log, warn, or otherwise audit the result of the check, but it won't restrict execution.
Cheers,
Steve
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 31 lines · Mickaël Salaün
Quoted 10 lines · Jeff Xu
Quoted 9 lines · Steve Dower
Quoted 6 lines · Jeff Xu
IIUC:
CHECK=0, RESTRICT=0: do nothing, current behavior
CHECK=1, RESTRICT=0: permissive mode - ignoreAT_CHECKresults. CHECK=0, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, no exception. CHECK=1, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, except those in the "checked-and-allowed" list.Quoted 8 lines
Quoted 7 lines
I had much the same question for Mickaël while working on this.
Essentially, "CHECK=0, RESTRICT=1" means to restrict without checking. In the context of a script or macro interpreter, this just means it will never interpret any scripts. Non-binary code execution is fully disabled in any part of the process that respects these bits.
I see, so Mickaël does mean this will block all scripts.
That is the initial idea.
Quoted 3 lines
I guess, in the context of dynamic linker, this means: no more .so loading, even "dlopen" is called by an app ? But this will make the
execve()fail.Hmm, I'm not sure this "CHECK=0, RESTRICT=1" configuration would make sense for a dynamic linker except maybe if we want to only allow static binaries?
Quoted 8 lines
The CHECK and RESTRICT securebits are designed to make it possible a "permissive mode" and an enforcement mode with the related locked securebits. This is why this "CHECK=0, RESTRICT=1" combination looks a bit weird. We can replace these securebits with others but I didn't find a better (and simple) option. I don't think this is an issue because with any security policy we can create unusable combinations. The three other combinations makes a lot of sense though.
If we need only handle 3 combinations, I would think something like below is easier to understand, and don't have wield state like CHECK=0, RESTRICT=1
XX_RESTRICT: when true: Perform the AT_CHECK, and deny the executable after AT_CHECK fails.XX_RESTRICT_PERMISSIVE: take effect when XX_RESTRICT is true. True means Ignoring the AT_CHECK result.
Or
XX_CHECK: when true: Perform the AT_CHECK.XX_CHECK_ENFORCE takes effect only when XX_CHECK is true. True means
restrict the executable when AT_CHECK failed; false means ignore the AT_CHECK failure.
Of course, we can replace XX_CHECK_ENFORCE with XX_RESTRICT. Personally I think having _CHECK_ in the name implies the XX_CHECK needs to be true as a prerequisite for this flag , but that is my opinion only. As long as the semantics are clear as part of the comments of definition in code, it is fine.
Thanks
-Jeff
Quoted 25 lines
Quoted 9 lines
Quoted 9 lines
"CHECK=1, RESTRICT=1" means to restrict unless
AT_CHECKpasses. This case is the allow list (or whatever mechanism is being used to determine the result of anAT_CHECKcheck). The actual mechanism isn't the business of the script interpreter at all, it just has to refuse to execute anything that doesn't pass the check. So a generic interpreter can implement a generic mechanism and leave the specifics to whoever configures the machine.Quoted 3 lines
In the context of dynamic linker. this means:
if .so passed theAT_CHECK,ldopen()can still load it. If .so fails theAT_CHECK,ldopen()will fail too.Correct
Quoted 10 lines
Thanks
-JeffQuoted 6 lines
The other two case are more obvious. "CHECK=0, RESTRICT=0" is the zero-overhead case, while "CHECK=1, RESTRICT=0" might log, warn, or otherwise audit the result of the check, but it won't restrict execution.
Cheers,
Steve
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 40 lines · Jeff Xu
Quoted 31 lines · Mickaël Salaün
Quoted 10 lines · Jeff Xu
Quoted 9 lines · Steve Dower
Quoted 6 lines · Jeff Xu
IIUC:
CHECK=0, RESTRICT=0: do nothing, current behavior
CHECK=1, RESTRICT=0: permissive mode - ignoreAT_CHECKresults. CHECK=0, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, no exception. CHECK=1, RESTRICT=1: callAT_CHECK, deny ifAT_CHECKfailed, except those in the "checked-and-allowed" list.Quoted 8 lines
Quoted 7 lines
I had much the same question for Mickaël while working on this.
Essentially, "CHECK=0, RESTRICT=1" means to restrict without checking. In the context of a script or macro interpreter, this just means it will never interpret any scripts. Non-binary code execution is fully disabled in any part of the process that respects these bits.
I see, so Mickaël does mean this will block all scripts.
That is the initial idea.
Quoted 3 lines
I guess, in the context of dynamic linker, this means: no more .so loading, even "dlopen" is called by an app ? But this will make the
execve()fail.Hmm, I'm not sure this "CHECK=0, RESTRICT=1" configuration would make sense for a dynamic linker except maybe if we want to only allow static binaries?
Quoted 8 lines
The CHECK and RESTRICT securebits are designed to make it possible a "permissive mode" and an enforcement mode with the related locked securebits. This is why this "CHECK=0, RESTRICT=1" combination looks a bit weird. We can replace these securebits with others but I didn't find a better (and simple) option. I don't think this is an issue because with any security policy we can create unusable combinations. The three other combinations makes a lot of sense though.
Quoted 3 lines
If we need only handle 3 combinations, I would think something like below is easier to understand, and don't have wield state like CHECK=0, RESTRICT=1
The "CHECK=0, RESTRICT=1" is useful for script interpreter instances that should not interpret any command from users e.g., but only execute script files.
Quoted 6 lines
XX_RESTRICT: when true: Perform theAT_CHECK, and deny the executable afterAT_CHECKfails.
XX_RESTRICT_PERMISSIVE: take effect whenXX_RESTRICTis true. True means Ignoring theAT_CHECKresult.
We get a similar weird state with XX_RESTRICT_PERMISSIVE=1 and XX_RESTRICT=0
As a side note, for compatibility reasons, by default all securebits must be 0, and this must translate to no restriction.
Quoted 7 lines
Or
XX_CHECK: when true: Perform theAT_CHECK.XX_CHECK_ENFORCEtakes effect only whenXX_CHECKis true. True means
restrict the executable whenAT_CHECKfailed; false means ignore theAT_CHECKfailure.
We get a similar weird state with XX_CHECK_ENFORCE=1 and XX_CHECK=0
Quoted 6 lines
Of course, we can replace
XX_CHECK_ENFORCEwithXX_RESTRICT. Personally I think having _CHECK_ in the name implies theXX_CHECKneeds to be true as a prerequisite for this flag , but that is my opinion only. As long as the semantics are clear as part of the comments of definition in code, it is fine.
Here is another proposal:
We can change a bit the semantic by making it the norm to always check file executability with AT_CHECK, and using the securebits to restrict file interpretation and/or command injection (e.g. user supplied shell commands). Non-executable checked files can be reported/logged at the kernel level, with audit, configured by sysadmins.
New securebits (feel free to propose better names):
- SECBIT_EXEC_RESTRICT_FILE: requires AT_CHECK to pass.
- SECBIT_EXEC_DENY_INTERACTIVE: deny any command injection via command line arguments, environment variables, or configuration files. This should be ignored by dynamic linkers. We could also have an allow-list of shells for which this bit is not set, managed by an LSM's policy, if the native securebits scoping approach is not enough.
Different modes for script interpreters:
1. RESTRICT_FILE=0 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 with logs to prepare for a migration to a restrictive mode.
2. RESTRICT_FILE=1 DENY_INTERACTIVE=0
Deny script interpretation if they are not executable, and allow arbitrary user commands.
=> Threat: (potential) malicious scripts run by trusted (and not
fooled) users. That could protect against unintended script executions (e.g. sh /tmp/*.sh).
==> Makes sense for (semi-restricted) user sessions.
3. RESTRICT_FILE=1 DENY_INTERACTIVE=1
Deny script interpretation if they are not executable, and also deny any arbitrary user commands.
=> Threat: malicious scripts run by untrusted users.
==> Makes sense for system services executing scripts.
4. RESTRICT_FILE=0 DENY_INTERACTIVE=1
Always interpret scripts, but deny arbitrary user commands. => Goal: monitor/measure/assess script content (e.g. with IMA/EVM) in
a system where the access rights are not (yet) ready. Arbitrary user commands would be much more difficult to monitor.
==> First step of restricting system services that should not
directly pass arbitrary commands to shells.
Quoted 30 lines
Thanks
-JeffQuoted 25 lines
Quoted 9 lines
Quoted 9 lines
"CHECK=1, RESTRICT=1" means to restrict unless
AT_CHECKpasses. This case is the allow list (or whatever mechanism is being used to determine the result of anAT_CHECKcheck). The actual mechanism isn't the business of the script interpreter at all, it just has to refuse to execute anything that doesn't pass the check. So a generic interpreter can implement a generic mechanism and leave the specifics to whoever configures the machine.Quoted 3 lines
In the context of dynamic linker. this means:
if .so passed theAT_CHECK,ldopen()can still load it. If .so fails theAT_CHECK,ldopen()will fail too.Correct
Quoted 10 lines
Thanks
-JeffQuoted 6 lines
The other two case are more obvious. "CHECK=0, RESTRICT=0" is the zero-overhead case, while "CHECK=1, RESTRICT=0" might log, warn, or otherwise audit the result of the check, but it won't restrict execution.
Cheers,
Steve
Kees Cook
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 11 lines · Mickaël Salaün
Here is another proposal:
We can change a bit the semantic by making it the norm to always check file executability with
AT_CHECK, and using the securebits to restrict file interpretation and/or command injection (e.g. user supplied shell commands). Non-executable checked files can be reported/logged at the kernel level, with audit, configured by sysadmins.New securebits (feel free to propose better names):
-
SECBIT_EXEC_RESTRICT_FILE: requiresAT_CHECKto pass.
Would you want the enforcement of this bit done by userspace or the kernel?
IIUC, userspace would always perform AT_CHECK regardless of SECBIT_EXEC_RESTRICT_FILE, and then which would happen?
1) userspace would ignore errors from AT_CHECK when SECBIT_EXEC_RESTRICT_FILE is unset
or
2) kernel would allow all AT_CHECK when SECBIT_EXEC_RESTRICT_FILE is unset
I suspect 1 is best and what you intend, given that SECBIT_EXEC_DENY_INTERACTIVE can only be enforced by userspace.
Quoted 28 lines
-
SECBIT_EXEC_DENY_INTERACTIVE: deny any command injection via command line arguments, environment variables, or configuration files. This should be ignored by dynamic linkers. We could also have an allow-list of shells for which this bit is not set, managed by an LSM's policy, if the native securebits scoping approach is not enough.Different modes for script interpreters:
1. RESTRICT_FILE=0 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 with logs to prepare for a migration to a restrictive mode.2. RESTRICT_FILE=1 DENY_INTERACTIVE=0
Deny script interpretation if they are not executable, and allow arbitrary user commands.
=> Threat: (potential) malicious scripts run by trusted (and not
fooled) users. That could protect against unintended script executions (e.g. sh/tmp/*.sh).
==> Makes sense for (semi-restricted) user sessions.3. RESTRICT_FILE=1 DENY_INTERACTIVE=1
Deny script interpretation if they are not executable, and also deny any arbitrary user commands.
=> Threat: malicious scripts run by untrusted users.
==> Makes sense for system services executing scripts.
Quoted 7 lines
4. RESTRICT_FILE=0 DENY_INTERACTIVE=1
Always interpret scripts, but deny arbitrary user commands. => Goal: monitor/measure/assess script content (e.g. with IMA/EVM) in
a system where the access rights are not (yet) ready. Arbitrary user commands would be much more difficult to monitor.
==> First step of restricting system services that should not
directly pass arbitrary commands to shells.
I like these bits!
Signature · 1 line
Kees Cook
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 13 lines · Kees Cook
Quoted 11 lines · Mickaël Salaün
Here is another proposal:
We can change a bit the semantic by making it the norm to always check file executability with
AT_CHECK, and using the securebits to restrict file interpretation and/or command injection (e.g. user supplied shell commands). Non-executable checked files can be reported/logged at the kernel level, with audit, configured by sysadmins.New securebits (feel free to propose better names):
-
SECBIT_EXEC_RESTRICT_FILE: requiresAT_CHECKto pass.
Quoted 8 lines
Would you want the enforcement of this bit done by userspace or the kernel?
IIUC, userspace would always perform
AT_CHECKregardless ofSECBIT_EXEC_RESTRICT_FILE, and then which would happen?1) userspace would ignore errors from
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unset
Yes, that's the idea.
Quoted 8 lines
or
2) kernel would allow all
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetI suspect 1 is best and what you intend, given that
SECBIT_EXEC_DENY_INTERACTIVEcan only be enforced by userspace.
Indeed. We don't want AT_CHECK's behavior to change according to securebits.
Quoted 37 lines
Quoted 29 lines
-
SECBIT_EXEC_DENY_INTERACTIVE: deny any command injection via command line arguments, environment variables, or configuration files. This should be ignored by dynamic linkers. We could also have an allow-list of shells for which this bit is not set, managed by an LSM's policy, if the native securebits scoping approach is not enough.Different modes for script interpreters:
1. RESTRICT_FILE=0 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 with logs to prepare for a migration to a restrictive mode.2. RESTRICT_FILE=1 DENY_INTERACTIVE=0
Deny script interpretation if they are not executable, and allow arbitrary user commands.
=> Threat: (potential) malicious scripts run by trusted (and not
fooled) users. That could protect against unintended script executions (e.g. sh/tmp/*.sh).
==> Makes sense for (semi-restricted) user sessions.3. RESTRICT_FILE=1 DENY_INTERACTIVE=1
Deny script interpretation if they are not executable, and also deny any arbitrary user commands.
=> Threat: malicious scripts run by untrusted users.
==> Makes sense for system services executing scripts.Quoted 7 lines
4. RESTRICT_FILE=0 DENY_INTERACTIVE=1
Always interpret scripts, but deny arbitrary user commands. => Goal: monitor/measure/assess script content (e.g. with IMA/EVM) in
a system where the access rights are not (yet) ready. Arbitrary user commands would be much more difficult to monitor.
==> First step of restricting system services that should not
directly pass arbitrary commands to shells.
Quoted 1 line
I like these bits!
Good! Jeff, Steve, Florian, Matt, others, what do you think?
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 29 lines · Mickaël Salaün
Quoted 13 lines · Kees Cook
Quoted 11 lines · Mickaël Salaün
Here is another proposal:
We can change a bit the semantic by making it the norm to always check file executability with
AT_CHECK, and using the securebits to restrict file interpretation and/or command injection (e.g. user supplied shell commands). Non-executable checked files can be reported/logged at the kernel level, with audit, configured by sysadmins.New securebits (feel free to propose better names):
-
SECBIT_EXEC_RESTRICT_FILE: requiresAT_CHECKto pass.Quoted 8 lines
Would you want the enforcement of this bit done by userspace or the kernel?
IIUC, userspace would always perform
AT_CHECKregardless ofSECBIT_EXEC_RESTRICT_FILE, and then which would happen?1) userspace would ignore errors from
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetYes, that's the idea.
Quoted 3 lines
or
Quoted 9 lines
Quoted 5 lines
2) kernel would allow all
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetI suspect 1 is best and what you intend, given that
SECBIT_EXEC_DENY_INTERACTIVEcan only be enforced by userspace.Indeed. We don't want AT_CHECK's behavior to change according to securebits.
One bit is good.
Quoted 37 lines
Quoted 37 lines
Quoted 36 lines
-
SECBIT_EXEC_DENY_INTERACTIVE: deny any command injection via command line arguments, environment variables, or configuration files. This should be ignored by dynamic linkers. We could also have an allow-list of shells for which this bit is not set, managed by an LSM's policy, if the native securebits scoping approach is not enough.Different modes for script interpreters:
1. RESTRICT_FILE=0 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 with logs to prepare for a migration to a restrictive mode.2. RESTRICT_FILE=1 DENY_INTERACTIVE=0
Deny script interpretation if they are not executable, and allow arbitrary user commands.
=> Threat: (potential) malicious scripts run by trusted (and not
fooled) users. That could protect against unintended script executions (e.g. sh/tmp/*.sh).
==> Makes sense for (semi-restricted) user sessions.3. RESTRICT_FILE=1 DENY_INTERACTIVE=1
Deny script interpretation if they are not executable, and also deny any arbitrary user commands.
=> Threat: malicious scripts run by untrusted users.
==> Makes sense for system services executing scripts.4. RESTRICT_FILE=0 DENY_INTERACTIVE=1
Always interpret scripts, but deny arbitrary user commands. => Goal: monitor/measure/assess script content (e.g. with IMA/EVM) in
a system where the access rights are not (yet) ready. Arbitrary user commands would be much more difficult to monitor.
==> First step of restricting system services that should not
directly pass arbitrary commands to shells.
Quoted 3 lines
Quoted 1 line
I like these bits!
Good! Jeff, Steve, Florian, Matt, others, what do you think?
For below two cases: will they be restricted by one (or some) mode above ?
1> cat /tmp/a.sh | sh
2> sh -c "$(cat /tmp/a.sh)"
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 5 lines · Jeff Xu
For below two cases: will they be restricted by one (or some) mode above ?
1> cat
/tmp/a.sh| sh2> sh -c "$(cat
/tmp/a.sh)"
It will almost certainly depend on your context, but to properly lock down a system, they must be restricted. "We were unable to check the file" ought to be treated the same as "the file failed the check".
If your goal is to only execute files that have been pre-approved in some manner, you're implying that you don't want interactive execution at all (since that is not a file that's been pre-approved). So a mere "sh" or "sh -c ..." would be restricted without checking anything other than the secure bit.
Cheers,
Steve
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
In reply to Jeff XuQuoted 80 lines · Jeff Xu
Quoted 29 lines · Mickaël Salaün
Quoted 13 lines · Kees Cook
Quoted 11 lines · Mickaël Salaün
Here is another proposal:
We can change a bit the semantic by making it the norm to always check file executability with
AT_CHECK, and using the securebits to restrict file interpretation and/or command injection (e.g. user supplied shell commands). Non-executable checked files can be reported/logged at the kernel level, with audit, configured by sysadmins.New securebits (feel free to propose better names):
-
SECBIT_EXEC_RESTRICT_FILE: requiresAT_CHECKto pass.Quoted 8 lines
Would you want the enforcement of this bit done by userspace or the kernel?
IIUC, userspace would always perform
AT_CHECKregardless ofSECBIT_EXEC_RESTRICT_FILE, and then which would happen?1) userspace would ignore errors from
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetYes, that's the idea.
Quoted 3 lines
or
Quoted 9 lines
Quoted 5 lines
2) kernel would allow all
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetI suspect 1 is best and what you intend, given that
SECBIT_EXEC_DENY_INTERACTIVEcan only be enforced by userspace.Indeed. We don't want AT_CHECK's behavior to change according to securebits.
One bit is good.
Quoted 39 lines
Quoted 38 lines
Quoted 29 lines
-
SECBIT_EXEC_DENY_INTERACTIVE: deny any command injection via command line arguments, environment variables, or configuration files. This should be ignored by dynamic linkers. We could also have an allow-list of shells for which this bit is not set, managed by an LSM's policy, if the native securebits scoping approach is not enough.Different modes for script interpreters:
1. RESTRICT_FILE=0 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 with logs to prepare for a migration to a restrictive mode.2. RESTRICT_FILE=1 DENY_INTERACTIVE=0
Deny script interpretation if they are not executable, and allow arbitrary user commands.
=> Threat: (potential) malicious scripts run by trusted (and not
fooled) users. That could protect against unintended script executions (e.g. sh/tmp/*.sh).
==> Makes sense for (semi-restricted) user sessions.3. RESTRICT_FILE=1 DENY_INTERACTIVE=1
Deny script interpretation if they are not executable, and also deny any arbitrary user commands.
=> Threat: malicious scripts run by untrusted users.
==> Makes sense for system services executing scripts.Quoted 7 lines
4. RESTRICT_FILE=0 DENY_INTERACTIVE=1
Always interpret scripts, but deny arbitrary user commands. => Goal: monitor/measure/assess script content (e.g. with IMA/EVM) in
a system where the access rights are not (yet) ready. Arbitrary user commands would be much more difficult to monitor.
==> First step of restricting system services that should not
directly pass arbitrary commands to shells.I like these bits!
Quoted 7 lines
Quoted 1 line
Good! Jeff, Steve, Florian, Matt, others, what do you think?
For below two cases: will they be restricted by one (or some) mode above ?
1> cat
/tmp/a.sh| sh2> sh -c "$(cat
/tmp/a.sh)"
Yes, DENY_INTERACTIVE=1 is to deny both of these cases (i.e. arbitrary user command).
These other examples should be allowed with AT_CHECK and RESTRICT_FILE=1 if a.sh is executable though:
* sh /tmp/a.sh
* sh < /tmp/a.sh
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 90 lines · Mickaël Salaün
Quoted 80 lines · Jeff Xu
Quoted 29 lines · Mickaël Salaün
Quoted 13 lines · Kees Cook
Quoted 11 lines · Mickaël Salaün
Here is another proposal:
We can change a bit the semantic by making it the norm to always check file executability with
AT_CHECK, and using the securebits to restrict file interpretation and/or command injection (e.g. user supplied shell commands). Non-executable checked files can be reported/logged at the kernel level, with audit, configured by sysadmins.New securebits (feel free to propose better names):
-
SECBIT_EXEC_RESTRICT_FILE: requiresAT_CHECKto pass.Quoted 8 lines
Would you want the enforcement of this bit done by userspace or the kernel?
IIUC, userspace would always perform
AT_CHECKregardless ofSECBIT_EXEC_RESTRICT_FILE, and then which would happen?1) userspace would ignore errors from
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetYes, that's the idea.
Quoted 3 lines
or
Quoted 9 lines
Quoted 5 lines
2) kernel would allow all
AT_CHECKwhenSECBIT_EXEC_RESTRICT_FILEis unsetI suspect 1 is best and what you intend, given that
SECBIT_EXEC_DENY_INTERACTIVEcan only be enforced by userspace.Indeed. We don't want AT_CHECK's behavior to change according to securebits.
One bit is good.
Quoted 39 lines
Quoted 38 lines
Quoted 29 lines
-
SECBIT_EXEC_DENY_INTERACTIVE: deny any command injection via command line arguments, environment variables, or configuration files. This should be ignored by dynamic linkers. We could also have an allow-list of shells for which this bit is not set, managed by an LSM's policy, if the native securebits scoping approach is not enough.Different modes for script interpreters:
1. RESTRICT_FILE=0 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 with logs to prepare for a migration to a restrictive mode.2. RESTRICT_FILE=1 DENY_INTERACTIVE=0
Deny script interpretation if they are not executable, and allow arbitrary user commands.
=> Threat: (potential) malicious scripts run by trusted (and not
fooled) users. That could protect against unintended script executions (e.g. sh/tmp/*.sh).
==> Makes sense for (semi-restricted) user sessions.3. RESTRICT_FILE=1 DENY_INTERACTIVE=1
Deny script interpretation if they are not executable, and also deny any arbitrary user commands.
=> Threat: malicious scripts run by untrusted users.
==> Makes sense for system services executing scripts.Quoted 7 lines
4. RESTRICT_FILE=0 DENY_INTERACTIVE=1
Always interpret scripts, but deny arbitrary user commands. => Goal: monitor/measure/assess script content (e.g. with IMA/EVM) in
a system where the access rights are not (yet) ready. Arbitrary user commands would be much more difficult to monitor.
==> First step of restricting system services that should not
directly pass arbitrary commands to shells.I like these bits!
Quoted 7 lines
Quoted 1 line
Good! Jeff, Steve, Florian, Matt, others, what do you think?
For below two cases: will they be restricted by one (or some) mode above ?
1> cat
/tmp/a.sh| sh2> sh -c "$(cat
/tmp/a.sh)"
Quoted 7 lines
Yes, DENY_INTERACTIVE=1 is to deny both of these cases (i.e. arbitrary user command).
These other examples should be allowed with
AT_CHECKand RESTRICT_FILE=1 ifa.shis executable though:
* sh/tmp/a.sh
* sh </tmp/a.sh
That looks good. Thanks for clarifying.
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
In reply to Mickaël SalaünQuoted 13 lines · Mickaël Salaün
Quoted 12 lines · Jeff Xu
Quoted 12 lines
Hmm, I'm not sure this "CHECK=0, RESTRICT=1" configuration would make sense for a dynamic linker except maybe if we want to only allow static binaries?
The CHECK and RESTRICT securebits are designed to make it possible a "permissive mode" and an enforcement mode with the related locked securebits. This is why this "CHECK=0, RESTRICT=1" combination looks a bit weird. We can replace these securebits with others but I didn't find a better (and simple) option. I don't think this is an issue because with any security policy we can create unusable combinations. The three other combinations makes a lot of sense though.
Quoted 7 lines
Quoted 3 lines
If we need only handle 3 combinations, I would think something like below is easier to understand, and don't have wield state like CHECK=0, RESTRICT=1
The "CHECK=0, RESTRICT=1" is useful for script interpreter instances that should not interpret any command from users e.g., but only execute script files.
I see this case as being most relevant to something that doesn't usually need any custom scripts, but may have it. For example, macros in a document, or pre/post-install scripts for a package manager.
For something whose sole purpose is to execute scripts, it doesn't make much sense. But there are other cases that can be reasonably controlled with this option.
Cheers,
Steve
Re: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
In reply to Mickaël SalaünQuoted 7 lines · Mickaël Salaün
These new
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.
Quoted 6 lines
When
SECBIT_SHOULD_EXEC_CHECKis set, programs interpreting code should check executable resources with execveat(2) +AT_CHECK(see previous patch).When
SECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow execution of approved resources, if any (seeSECBIT_SHOULD_EXEC_CHECK).
I read this twice, slept on it, read them again, and I *still* can't understand it. See below...
Quoted 5 lines
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has
CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.
What's the actual issue? And why can't I, as root, do, in a carefully checked, CHECK'd and RESTRICT'd environment, # gdb -p <pid>? Adding weird restrictions to ptrace can substantially *weaken* security because it forces people to do utterly daft things to work around the restrictions.
...
Quoted 5 lines
+/*
+ * WhenSECBIT_SHOULD_EXEC_CHECKis set, a process should check all executable
+ * files with execveat(2) +AT_CHECK. However, such check should only be
+ * performed if all to-be-executed code only comes from regular files. For
+ * instance, if a script interpreter is called with both a script snipped as
s/snipped/snippet/
Quoted 4 lines
+ * argument and a regular file, the interpreter should not check any file.
+ * Doing otherwise would mislead the kernel to think that only the script file
+ * is being executed, which could for instance lead to unexpected permission
+ * change and break current use cases.
This is IMO not nearly clear enough to result in multiple user implementations and a kernel implementation and multiple LSM implementations and LSM policy authors actually agreeing as to what this means.
I also think it's wrong to give user code instructions about what kernel checks it should do. Have the user code call the kernel and have the kernel implement the policy.
Quoted 9 lines
+/*
+ * WhenSECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow
+ * execution of approved files, if any (seeSECBIT_SHOULD_EXEC_CHECK). For
+ * instance, script interpreters called with a script snippet as argument
+ * should always deny such execution ifSECBIT_SHOULD_EXEC_RESTRICTis set.
+ * However, if a script interpreter is called with both
+ *SECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICT, they should
+ * interpret the provided script files if no unchecked code is also provided
+ * (e.g. directly as argument).
I think you're trying to say that this is like (the inverse of)
Content-Security-Policy: unsafe-inline. In other words, you're saying that, if RESTRICT is set, then programs should not execute code-like text that didn't come from a file. Is that right?
I feel like it would be worth looking at the state of the art of Content-Security-Policy and all the lessons people have learned from it. Whatever the result is should be at least as comprehensible and at least as carefully engineered as Content-Security-Policy.
--Andy
Mickaël Salaün
maintainerRe: [RFC PATCH v19 2/5] security: Add new SHOULD_EXEC_CHECK and SHOULD_EXEC_RESTRICT securebits
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Mickaël Salaün
These new
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, 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. Like seccomp filters or Landlock domains, the securebits are inherited across proceses.When
SECBIT_SHOULD_EXEC_CHECKis set, programs interpreting code should check executable resources with execveat(2) +AT_CHECK(see previous patch).
Quoted 5 lines
Quoted 2 lines
When
SECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow execution of approved resources, if any (seeSECBIT_SHOULD_EXEC_CHECK).I read this twice, slept on it, read them again, and I *still* can't understand it. See below...
There is a new proposal:
https://lore.kernel.org/all/[email protected]/
The new securebits will be SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE. I'll send a new patch series with that.
Quoted 7 lines
Quoted 6 lines
The only restriction enforced by the kernel is the right to ptrace another process. Processes are denied to ptrace less restricted ones, unless the tracer has
CAP_SYS_PTRACE. This is mainly a safeguard to avoid trivial privilege escalations e.g., by a debugging process being abused with a confused deputy attack.
Quoted 5 lines
What's the actual issue? And why can't I, as root, do, in a carefully checked, CHECK'd and RESTRICT'd environment, # gdb -p <pid>? Adding weird restrictions to ptrace can substantially *weaken* security because it forces people to do utterly daft things to work around the restrictions.
Restricting ptrace was a cautious approach, but I get you point and I agree. I'll remove the ptrace restrictions in the next patch series.
Quoted 16 lines
...
Quoted 5 lines
+/*
+ * WhenSECBIT_SHOULD_EXEC_CHECKis set, a process should check all executable
+ * files with execveat(2) +AT_CHECK. However, such check should only be
+ * performed if all to-be-executed code only comes from regular files. For
+ * instance, if a script interpreter is called with both a script snipped ass/snipped/snippet/
Quoted 4 lines
+ * argument and a regular file, the interpreter should not check any file.
+ * Doing otherwise would mislead the kernel to think that only the script file
+ * is being executed, which could for instance lead to unexpected permission
+ * change and break current use cases.
Quoted 4 lines
This is IMO not nearly clear enough to result in multiple user implementations and a kernel implementation and multiple LSM implementations and LSM policy authors actually agreeing as to what this means.
Right, no kernel parts (e.g. LSMs) should try to infer anything other than an executability check. We should handle things such as role transitions with something else (e.g. a complementary dedicated flag), and that should be decorrelated from this patch series.
Quoted 4 lines
I also think it's wrong to give user code instructions about what kernel checks it should do. Have the user code call the kernel and have the kernel implement the policy.
Call the kernel for what? Script interpreter is a user space thing, and restrictions enforced on interpreters need to be a user space thing. The kernel cannot restrict user space according to a semantic only defined by user space, such as Python interpretation, CLI arguments, content of environment variables... If a process wants to interpret some data and turn than into code, there is no way for the kernel to know about that.
Quoted 11 lines
Quoted 10 lines
+/*
+ * WhenSECBIT_SHOULD_EXEC_RESTRICTis set, a process should only allow
+ * execution of approved files, if any (seeSECBIT_SHOULD_EXEC_CHECK). For
+ * instance, script interpreters called with a script snippet as argument
+ * should always deny such execution ifSECBIT_SHOULD_EXEC_RESTRICTis set.
+ * However, if a script interpreter is called with both
+ *SECBIT_SHOULD_EXEC_CHECKandSECBIT_SHOULD_EXEC_RESTRICT, they should
+ * interpret the provided script files if no unchecked code is also provided
+ * (e.g. directly as argument).
Quoted 4 lines
I think you're trying to say that this is like (the inverse of)
Content-Security-Policy: unsafe-inline. In other words, you're saying that, if RESTRICT is set, then programs should not execute code-like text that didn't come from a file. Is that right?
That is the definition of the new SECBIT_EXEC_DENY_INTERACTIVE, which should be clearer.
Quoted 5 lines
I feel like it would be worth looking at the state of the art of Content-Security-Policy and all the lessons people have learned from it. Whatever the result is should be at least as comprehensible and at least as carefully engineered as Content-Security-Policy.
That's a good idea, but I guess Content-Security-Policy cannot be directly applied here. My understanding is that CSP enables web servers to request restrictions on code they provide. In the AT_CHECK+securebits case, the policy is defined and enforced by the interpreter, not necessarily the script provider. One big difference is that web servers (should) know the scripts they provide, and can then request the browser to ensure that they do what they should do, while the script interpreter trusts the kernel to check security properties of a script. In other words, something like CSP could be implemented with AT_CHECK+securebits and a LSM policy (e.g. according to file's xattr).
Quoted 2 lines
--Andy
Mickaël Salaün
maintainerRFC PATCH v19 3/5 selftests/exec: Add tests for AT_CHECK and related 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_SHOULD_EXEC_CHECK and SECBIT_SHOULD_EXEC_RESTRICT are inherited by child processes and that they can be pinned with the appropriate SECBIT_SHOULD_EXEC_CHECK_LOCKED and SECBIT_SHOULD_EXEC_RESTRICT_LOCKED bits.
---
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 | 8 + tools/testing/selftests/exec/config | 2 + tools/testing/selftests/exec/false.c | 5 + tools/testing/selftests/exec/should-exec.c | 449 +++++++++++++++++++++ 5 files changed, 466 insertions(+) create mode 100644 tools/testing/selftests/exec/config create mode 100644 tools/testing/selftests/exec/false.c create mode 100644 tools/testing/selftests/exec/should-exec.c
tools/testing/selftests/exec/.gitignore +2 -0
@@ -9,8 +9,10 @@ execveat.ephemeral execveat.denatured non-regular null-argv +/false /load_address_* /recursion-depth +/should-exec xxxxxxxx* pipe S_I*.test
tools/testing/selftests/exec/Makefile +8 -0
@@ -2,15 +2,20 @@ CFLAGS = -Wall CFLAGS += -Wno-nonnull CFLAGS += -D_GNU_SOURCE +CFLAGS += $(KHDR_INCLUDES) + +LDLIBS += -lcap TEST_PROGS := binfmt_script.py TEST_GEN_PROGS := execveat load_address_4096 load_address_2097152 load_address_16777216 non-regular +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 += should-exec EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx* \ $(OUTPUT)/S_I*.test
@@ -34,3 +39,6 @@ $(OUTPUT)/load_address_2097152: load_address.c $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x200000 -pie -static $< -o $@ $(OUTPUT)/load_address_16777216: load_address.c $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x1000000 -pie -static $< -o $@ + +$(OUTPUT)/false: false.c + $(CC) $(CFLAGS) $(LDFLAGS) -static $< -o $@
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 ; +}
tools/testing/selftests/exec/should-exec.c new file +449 -0
449 lines not on this page · raw message
Signature · 1 line
2.45.2
Mickaël Salaün
maintainerRFC PATCH v19 4/5 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
@@ -2009,6 +2013,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[] = {
@@ -2026,20 +2045,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.45.2
Mickaël Salaün
maintainerRFC PATCH v19 5/5 samples/should-exec: Add set-should-exec
In reply to Mickaël SalaünAdd a simple tool to set SECBIT_SHOULD_EXEC_CHECK, SECBIT_SHOULD_EXEC_RESTRICT, and their lock counterparts before executing a command. This should be useful to easily test against script interpreters.
--- samples/Kconfig | 7 +++ samples/Makefile | 1 + samples/should-exec/.gitignore | 1 + samples/should-exec/Makefile | 13 ++++ samples/should-exec/set-should-exec.c | 88 +++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 samples/should-exec/.gitignore create mode 100644 samples/should-exec/Makefile create mode 100644 samples/should-exec/set-should-exec.c
samples/Kconfig +7 -0
@@ -180,6 +180,13 @@ config SAMPLE_SECCOMP Build samples of seccomp filters using various methods of BPF filter construction. +config SAMPLE_SHOULD_EXEC + bool "Should-exec secure bits examples" + depends on CC_CAN_LINK && HEADERS_INSTALL + help + Build a tool to easily configure SECBIT_SHOULD_EXEC_CHECK, + SECBIT_SHOULD_EXEC_RESTRICT and their lock counterparts. + config SAMPLE_TIMER bool "Timer sample" depends on CC_CAN_LINK && HEADERS_INSTALL
samples/Makefile +1 -0
@@ -19,6 +19,7 @@ subdir-$(CONFIG_SAMPLE_PIDFD) += pidfd obj-$(CONFIG_SAMPLE_QMI_CLIENT) += qmi/ obj-$(CONFIG_SAMPLE_RPMSG_CLIENT) += rpmsg/ subdir-$(CONFIG_SAMPLE_SECCOMP) += seccomp +subdir-$(CONFIG_SAMPLE_SHOULD_EXEC) += should-exec subdir-$(CONFIG_SAMPLE_TIMER) += timers obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace_events/ obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_events/
samples/should-exec/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/set-should-exec
samples/should-exec/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause + +userprogs-always-y := set-should-exec + +userccflags += -I usr/include + +.PHONY: all clean + +all: + $(MAKE) -C ../.. samples/should-exec/ + +clean: + $(MAKE) -C ../.. M=samples/should-exec/ clean
samples/should-exec/set-should-exec.c new file +88 -0
88 lines not on this page · raw message
Signature · 1 line
2.45.2
Mimi Zohar
maintainerRe: [RFC PATCH v19 5/5] samples/should-exec: Add set-should-exec
Hi Mickaël,
Quoted 4 lines · Mickaël Salaün
Add a simple tool to set
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, and their lock counterparts before executing a command. This should be useful to easily test against script interpreters.
The print_usage() provides the calling syntax. Could you provide an example of how to use it and what to expect?
thanks,
Mimi
Mickaël Salaün
maintainerRe: [RFC PATCH v19 5/5] samples/should-exec: Add set-should-exec
Quoted 10 lines · Mimi Zohar
Hi Mickaël,
Quoted 4 lines · Mickaël Salaün
Add a simple tool to set
SECBIT_SHOULD_EXEC_CHECK,SECBIT_SHOULD_EXEC_RESTRICT, and their lock counterparts before executing a command. This should be useful to easily test against script interpreters.The
print_usage()provides the calling syntax. Could you provide an example of how to use it and what to expect?
To set SECBIT_SHOULD_EXEC_CHECK, SECBIT_SHOULD_EXEC_RESTRICT, and lock them on a new shell (session) we can use this:
./set-should-exec -crl -- bash -i
This would have no impact unless Bash, ld.so, or one of its child code is patched to restrict execution (e.g. with execveat+AT_CHECK check). Script interpreters and dynamic linkers need to be patch on a secure sysetm. Steve is enlightening Python, and we'll need more similar changes for common user space code. This can be an incremental work and only enforced on some user sessions or containers for instance.
Quoted 6 lines
thanks,
Mimi
Mimi Zohar
maintainerHi Mickaël,
Quoted 34 lines · Mickaël Salaün
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.Overview
--------This patch series is a new approach of the initial
O_MAYEXECfeature, 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_CHECKflag 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_CHECKtake 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
----------------
Quoted 6 lines
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
* pythonscript.py
* python <script.py
* python -m script.pyT
This is really needed, but is it the "only" purpose of this patch set or can it be used to also monitor files the script opens (for read) with the intention of executing.
Quoted 21 lines
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 -ascript.py-d '\r' -- python -c
* catscript.py| python
* pythonBackground
----------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.
Quoted 2 lines
The ability to control script execution is also required to close a major IMA measurement/appraisal interpreter integrity [3].
Definitely. But it isn't limited to controlling script execution, but also measuring the script. Will it be possible to measure and appraise the indirect script calls with this patch set?
Mimi
Quoted 12 lines
This new execveat +
AT_CHECKshould not be confused with theO_EXECflag (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
Mickaël Salaün
maintainerQuoted 44 lines · Mimi Zohar
Hi Mickaël,
Quoted 34 lines · Mickaël Salaün
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.Overview
--------This patch series is a new approach of the initial
O_MAYEXECfeature, 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_CHECKflag 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_CHECKtake 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
----------------Quoted 6 lines
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
* pythonscript.py
* python <script.py
* python -m script.pyT
Quoted 3 lines
This is really needed, but is it the "only" purpose of this patch set or can it be used to also monitor files the script opens (for read) with the intention of executing.
This feature can indeed also be used to monitor files requested by scripts to be executed e.g. using
https://docs.python.org/3/library/io.html#io.open_code
IMA/EVM can include this check in its logs.
Quoted 22 lines
Quoted 22 lines
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 -ascript.py-d '\r' -- python -c
* catscript.py| python
* pythonBackground
----------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.
Quoted 6 lines
Quoted 2 lines
The ability to control script execution is also required to close a major IMA measurement/appraisal interpreter integrity [3].
Definitely. But it isn't limited to controlling script execution, but also measuring the script. Will it be possible to measure and appraise the indirect script calls with this patch set?
Yes. You should only need to implement security_bprm_creds_for_exec() for IMA/EVM.
BTW, I noticed that IMA only uses the security_bprm_check() hook (which can be called several times for one execve), but security_bprm_creds_for_exec() might be more appropriate.
Quoted 17 lines
Mimi
Quoted 12 lines
This new execveat +
AT_CHECKshould not be confused with theO_EXECflag (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
Quoted 84 lines · Mickaël Salaün
Quoted 44 lines · Mimi Zohar
Hi Mickaël,
Quoted 34 lines · Mickaël Salaün
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.Overview
--------This patch series is a new approach of the initial
O_MAYEXECfeature, 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_CHECKflag 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_CHECKtake 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
----------------Quoted 6 lines
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
* pythonscript.py
* python <script.py
* python -m script.pyTQuoted 3 lines
This is really needed, but is it the "only" purpose of this patch set or can it be used to also monitor files the script opens (for read) with the intention of executing.
This feature can indeed also be used to monitor files requested by scripts to be executed e.g. using
https://docs.python.org/3/library/io.html#io.open_codeIMA/EVM can include this check in its logs.
Quoted 22 lines
Quoted 22 lines
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 -ascript.py-d '\r' -- python -c
* catscript.py| python
* pythonBackground
----------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.
Quoted 6 lines
Quoted 2 lines
The ability to control script execution is also required to close a major IMA measurement/appraisal interpreter integrity [3].
Definitely. But it isn't limited to controlling script execution, but also measuring the script. Will it be possible to measure and appraise the indirect script calls with this patch set?
Quoted 6 lines
Yes. You should only need to implement
security_bprm_creds_for_exec()for IMA/EVM.BTW, I noticed that IMA only uses the
security_bprm_check()hook (which can be called several times for one execve), butsecurity_bprm_creds_for_exec()might be more appropriate.
Ok, I tried a trivial modification to have this working:
security/integrity/ima/ima_main.c +9 -0
@@ -554,6 +554,14 @@ static int ima_bprm_check(struct linux_binprm *bprm) MAY_EXEC, CREDS_CHECK); } +static int ima_bprm_creds_for_exec (struct linux_binprm *bprm) +{ +if (!bprm->is_check) +return 0 ; + +return ima_bprm_check (bprm); +} +/** * ima_file_check - based on policy, collect/store measurement. * @file: pointer to the file to be measured
@@ -1177,6 +1185,7 @@ static int __init init_ima(void)static struct security_hook_list ima_hooks[]__ro_after_init = {LSM_HOOK_INIT (bprm_check_security, ima_bprm_check), +LSM_HOOK_INIT (bprm_creds_for_exec, ima_bprm_creds_for_exec),LSM_HOOK_INIT (file_post_open, ima_file_check),LSM_HOOK_INIT (inode_post_create_tmpfile, ima_post_create_tmpfile),LSM_HOOK_INIT (file_release, ima_file_free),
I also adapted the Clip OS 4 patch for bash.
The result seems good so far:
# echo "measure fowner=2000 func=BPRM_CHECK" > /sys/kernel/security/ima/policy
# ./bash /root/test.sh Hello World
# cat /sys/kernel/security/ima/ascii_runtime_measurements 10 35435d0858d895b90097306171a2e5fcc7f5da9e ima-ng sha256:0e4acf326a82c6bded9d86f48d272d7a036b6490081bb6466ecc2a0e416b244a boot_aggregate 10 4cd9df168a2cf8d18be46543e66c76a53ca6a03d ima-ng sha256:e7f3c2dab66f56fef963fbab55fc6d64bc22a5f900c29042e6ecd87e08f2b535 /root/test.sh
So, it is there.
It works only with +x permission. If not, I get:
# ./bash /root/test.sh ./bash: /root/test.sh: Permission denied
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello World
Do you have a more recent patch for that?
Thanks
Roberto
Quoted 17 lines
Quoted 17 lines
Mimi
Quoted 12 lines
This new execveat +
AT_CHECKshould not be confused with theO_EXECflag (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
James Bottomley
maintainerQuoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
How far down the rabbit hole do you want to go? You can't forbid a shell from executing commands from stdin because logging in then won't work. It may be possible to allow from a tty backed file and not from a file backed one, but you still have the problem of the attacker manually typing in the script.
The saving grace for this for shells is that they pretty much do nothing on their own (unlike python) so you can still measure all the executables they call out to, which provides reasonable safety.
James
Wouldn't count those shell chickens - awk alone is enough and we can use ssh and openssl clients (all in metasploit public code). As one of the people who makes novel shell types, I can assure you that this effort is only going to slow skiddies and only until the rest of us publish mitigations for this mitigation :)
-Boris (RageLtMan)
Quoted 20 lines · James Bottomley
Quoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
How far down the rabbit hole do you want to go? You can't forbid a shell from executing commands from stdin because logging in then won't work. It may be possible to allow from a tty backed file and not from a file backed one, but you still have the problem of the attacker manually typing in the script.
The saving grace for this for shells is that they pretty much do nothing on their own (unlike python) so you can still measure all the executables they call out to, which provides reasonable safety.
James
Mickaël Salaün
maintainer(adding back other people in Cc)
Quoted 5 lines · Boris Lukashev
Wouldn't count those shell chickens - awk alone is enough and we can use ssh and openssl clients (all in metasploit public code). As one of the people who makes novel shell types, I can assure you that this effort is only going to slow skiddies and only until the rest of us publish mitigations for this mitigation :)
Security is not binary. :)
Not all Linux systems are equals. Some hardened systems need this kind of feature and they can get guarantees because they fully control and trust their executable binaries (e.g. CLIP OS, chromeOS) or they properly sandbox them. See context in the cover letter.
awk is a script interpreter that should be patched too, like other Linux tools.
Quoted 24 lines
-Boris (RageLtMan)
Quoted 20 lines · James Bottomley
Quoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
How far down the rabbit hole do you want to go? You can't forbid a shell from executing commands from stdin because logging in then won't work. It may be possible to allow from a tty backed file and not from a file backed one, but you still have the problem of the attacker manually typing in the script.
The saving grace for this for shells is that they pretty much do nothing on their own (unlike python) so you can still measure all the executables they call out to, which provides reasonable safety.
James
Apologies, sent from phone so plain-text wasn't flying.
To elaborate a bit on the quick commentary there - i'm the happy camper behind most of the SSL shells, SSH stuff, AWS shells, and so on in Metasploit. So please take the following with a grain of tinfoil-hat salt as i'm well aware that there is no perfect defense against these things which covers all bases while permitting any level of sane operation in a general-purpose linux system (also work w/ GrapheneOS which is a far more suitable context for this sort of thing). Having loosely followed the discussion thread, my offsec-brain $0.02 are:
Shells are the provenance of the post-exploitation world - it's what we want to get as a result of the exploit succeeding. So i think we want to keep clear delineation between exploit and post-exp mitigation as they're actually separate concerns of the killchain.
1. Command shells tend to differentiate from interpreted or binary execution environments in their use of POSIX file descriptor primitives such as pipes. How those are marshalled, chained, and maintained (in a loop or whatever, hiding args, etc) are the only real IOCs available at this tier for interdiction as observation of data flow through the pipes is too onerous and complex. Target systems vary in the post-exp surfaces exposed (/dev/tcp for example) with the mechanics of that exposure necessitating adaptation of marshalling, chaining, and maintenance to fit the environment; but the basic premise of what forms a command shell cannot be mitigated without breaking POSIX mechanics themselves - offsec devs are no different from anyone else, we want our code to utilize architectural primitives instead of undefined behavior for longevity and ecosystem persistence/relevance.
2. The conversation about interpreted languages is probably a dead-end unless you want to neuter the interpreter - check out Spencer McIntyre's work re Python meterpreter or HDs/mine/etc on the PHP side. The stagers, loaded contexts, execution patterns, etc are all trivially modified to avoid detection (private versions not submitted for free ripping by lazy commercial entities to the FOSS ecosystem, yet). Dynamic code loading of interpreted languages is trivial and requires no syscalls, just text/serialized IL/etc. The complexity of loaded context available permits much more advanced functionality than we get in most basic command interpreter shells - <advanced evasions go here before doing something that'll get you caught> sort of thing.
3. Lastly, binary payloads such as Mettle have their own advantages re portability, skipping over libc, etc but need to be "harnessed-in" from say a command-injection exploit via memfd or similar. We haven't published our memfd stagers while the relevant sysctl gets adopted more widely, but we've had them for a long time (meaning real bad guys have as well) and have other ways to get binary content into executable memory or make memory containing it executable (to-the-gills Grsec/PaX systems notwithstanding). IMO, interdiction of the harnessed injection from a command context is the last time when anything of use can be done at this layer unless we're sure that we can trace all related and potentially async (not within the process tree anyway) syscalls emanating from what happens next. Subsequent actions are separate "remedial" workflows which is a wholly separate philosophical discussion about how to handle having been compromised already.
Security is very much not binary and in that vein of logic i think that we should probably define our shades of gray as ranges of what we want to protect/how and at what operational cost to then permit "dial-in" knobs to actually garner adoption from a broad range of systems outside the "real hardened efforts." At some point this turns into "limit users to sftp or git shells" which is a perfectly valid approach when the context permits that level of draconian restriction but the architectural breakdown of "native command, interpreted context, fully binary" shell types is pretty universal with new ones being API access into runtimes of clouds (SSM/serial/etc) which have their own set of limitations at execution and interface layers. Organizing defensive functions to handle the primitives necessary for each of these shell classes would likely help stratify/simplify this conversation and allow for more granular tasking toward those specific objectives.
Thanks,
-Boris
Quoted 44 lines · Mickaël Salaün
(adding back other people in Cc)
Quoted 5 lines · Boris Lukashev
Wouldn't count those shell chickens - awk alone is enough and we can use ssh and openssl clients (all in metasploit public code). As one of the people who makes novel shell types, I can assure you that this effort is only going to slow skiddies and only until the rest of us publish mitigations for this mitigation :)
Security is not binary. :)
Not all Linux systems are equals. Some hardened systems need this kind of feature and they can get guarantees because they fully control and trust their executable binaries (e.g. CLIP OS, chromeOS) or they properly sandbox them. See context in the cover letter.
awk is a script interpreter that should be patched too, like other Linux tools.
Quoted 24 lines
-Boris (RageLtMan)
Quoted 20 lines · James Bottomley
Quoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
How far down the rabbit hole do you want to go? You can't forbid a shell from executing commands from stdin because logging in then won't work. It may be possible to allow from a tty backed file and not from a file backed one, but you still have the problem of the attacker manually typing in the script.
The saving grace for this for shells is that they pretty much do nothing on their own (unlike python) so you can still measure all the executables they call out to, which provides reasonable safety.
James
Mickaël Salaün
maintainerQuoted 11 lines · Boris Lukashev
Apologies, sent from phone so plain-text wasn't flying.
To elaborate a bit on the quick commentary there - i'm the happy camper behind most of the SSL shells, SSH stuff, AWS shells, and so on in Metasploit. So please take the following with a grain of tinfoil-hat salt as i'm well aware that there is no perfect defense against these things which covers all bases while permitting any level of sane operation in a general-purpose linux system (also work w/ GrapheneOS which is a far more suitable context for this sort of thing). Having loosely followed the discussion thread, my offsec-brain $0.02 are:
Quoted 4 lines
Shells are the provenance of the post-exploitation world - it's what we want to get as a result of the exploit succeeding. So i think we want to keep clear delineation between exploit and post-exp mitigation as they're actually separate concerns of the killchain.
Indeed. The goal of this patch series is to control executable code, so mostly to make exploitation more difficult. When an attacker can execute code (e.g. with ROP), execution control is already bypassed.
Quoted 6 lines
1. Command shells tend to differentiate from interpreted or binary execution environments in their use of POSIX file descriptor primitives such as pipes. How those are marshalled, chained, and maintained (in a loop or whatever, hiding args, etc) are the only real IOCs available at this tier for interdiction as observation of data flow through the pipes is too onerous and complex.
I agree. Only files can reliably be inspected.
Quoted 12 lines
Target systems vary
in the post-exp surfaces exposed (/dev/tcpfor example) with the mechanics of that exposure necessitating adaptation of marshalling, chaining, and maintenance to fit the environment; but the basic premise of what forms a command shell cannot be mitigated without breaking POSIX mechanics themselves - offsec devs are no different from anyone else, we want our code to utilize architectural primitives instead of undefined behavior for longevity and ecosystem persistence/relevance.
2. The conversation about interpreted languages is probably a dead-end unless you want to neuter the interpreter - check out Spencer McIntyre's work re Python meterpreter or HDs/mine/etc on the PHP side.
Quoted 8 lines
The stagers, loaded contexts, execution patterns, etc are all trivially modified to avoid detection (private versions not submitted for free ripping by lazy commercial entities to the FOSS ecosystem, yet). Dynamic code loading of interpreted languages is trivial and requires no syscalls, just text/serialized IL/etc. The complexity of loaded context available permits much more advanced functionality than we get in most basic command interpreter shells - <advanced evasions go here before doing something that'll get you caught> sort of thing.
Right, if attackers can bring their own code (or even do ROP), it doesn't matter what it interprets, its arbitrary code execution.
Quoted 7 lines
3. Lastly, binary payloads such as Mettle have their own advantages re portability, skipping over libc, etc but need to be "harnessed-in" from say a command-injection exploit via memfd or similar. We haven't published our memfd stagers while the relevant sysctl gets adopted more widely, but we've had them for a long time (meaning real bad guys have as well) and have other ways to get binary content into executable memory or make memory containing it executable
Quoted 8 lines
(to-the-gills Grsec/PaX systems notwithstanding). IMO, interdiction of the harnessed injection from a command context is the last time when anything of use can be done at this layer unless we're sure that we can trace all related and potentially async (not within the process tree anyway) syscalls emanating from what happens next. Subsequent actions are separate "remedial" workflows which is a wholly separate philosophical discussion about how to handle having been compromised already.
Indeed, there are some prerequisites for a secure system. In this case we trust all the system-installed executable code. If attackers can fill a memfd with arbitrary code, it means that they already have code execution. This patch series will help mitigate some ways to get code execution.
Quoted 16 lines
Security is very much not binary and in that vein of logic i think that we should probably define our shades of gray as ranges of what we want to protect/how and at what operational cost to then permit "dial-in" knobs to actually garner adoption from a broad range of systems outside the "real hardened efforts." At some point this turns into "limit users to sftp or git shells" which is a perfectly valid approach when the context permits that level of draconian restriction but the architectural breakdown of "native command, interpreted context, fully binary" shell types is pretty universal with new ones being API access into runtimes of clouds (SSM/serial/etc) which have their own set of limitations at execution and interface layers. Organizing defensive functions to handle the primitives necessary for each of these shell classes would likely help stratify/simplify this conversation and allow for more granular tasking toward those specific objectives.
Thanks for the discussion. I agree, but the difficulty with this patch series is that it brings a simple *building block*. Of course, this will definitely not be enough to secure any systems, but it will fill a gap in some secure systems, and it could also harden more generic systems (e.g. restricted system services which should not need shell access). I listed some examples with the new securebits proposal:
https://lore.kernel.org/all/[email protected]/
Quoted 50 lines
Thanks,
-BorisQuoted 44 lines · Mickaël Salaün
(adding back other people in Cc)
Quoted 5 lines · Boris Lukashev
Wouldn't count those shell chickens - awk alone is enough and we can use ssh and openssl clients (all in metasploit public code). As one of the people who makes novel shell types, I can assure you that this effort is only going to slow skiddies and only until the rest of us publish mitigations for this mitigation :)
Security is not binary. :)
Not all Linux systems are equals. Some hardened systems need this kind of feature and they can get guarantees because they fully control and trust their executable binaries (e.g. CLIP OS, chromeOS) or they properly sandbox them. See context in the cover letter.
awk is a script interpreter that should be patched too, like other Linux tools.
Quoted 24 lines
-Boris (RageLtMan)
Quoted 20 lines · James Bottomley
Quoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
How far down the rabbit hole do you want to go? You can't forbid a shell from executing commands from stdin because logging in then won't work. It may be possible to allow from a tty backed file and not from a file backed one, but you still have the problem of the attacker manually typing in the script.
The saving grace for this for shells is that they pretty much do nothing on their own (unlike python) so you can still measure all the executables they call out to, which provides reasonable safety.
James
Mickaël Salaün
maintainerQuoted 7 lines · James Bottomley
Quoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
Bash was only partially restricted for CLIP OS because it was used for administrative tasks (interactive shell).
Python was also restricted for user commands though:
https://github.com/clipos-archive/clipos4_portage-overlay/blob/master/dev-lang/python/files/python-2.7.9-clip-mayexec.patch
Steve and Christian could help with a better Python implementation.
Quoted 6 lines
How far down the rabbit hole do you want to go? You can't forbid a shell from executing commands from stdin because logging in then won't work. It may be possible to allow from a tty backed file and not from a file backed one, but you still have the problem of the attacker manually typing in the script.
Yes, that's why we'll have the (optional) SECBIT_EXEC_DENY_INTERACTIVE:
https://lore.kernel.org/all/[email protected]/
Quoted 4 lines
The saving grace for this for shells is that they pretty much do nothing on their own (unlike python) so you can still measure all the executables they call out to, which provides reasonable safety.
Exactly. Python is a much more interesting target for attacker because it opens the door for arbitrary syscalls (see the cover letter).
If we want to have a more advanced access control (e.g. allow Bash but not Python), we should extend existing LSMs to manage the appropriate securebits according to programs/subjects.
Mickaël Salaün
maintainerQuoted 9 lines · Mickaël Salaün
Quoted 7 lines · James Bottomley
Quoted 6 lines · Roberto Sassu
But the Clip OS 4 patch does not cover the redirection case:
# ./bash < /root/test.sh Hello WorldDo you have a more recent patch for that?
Quoted 7 lines
Bash was only partially restricted for CLIP OS because it was used for administrative tasks (interactive shell).
Python was also restricted for user commands though:
https://github.com/clipos-archive/clipos4_portage-overlay/blob/master/dev-lang/python/files/python-2.7.9-clip-mayexec.patchSteve and Christian could help with a better Python implementation.
I'll include a toy interpreter in the next patch series. That should help for experiments.
Mickaël Salaün <[email protected]> writes:
FYI:
Quoted 2 lines
User space patches can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC
That link appears to be broken.
Thanks,
jon
Mickaël Salaün
maintainerQuoted 8 lines · Jonathan Corbet
Mickaël Salaün <[email protected]> writes:
FYI:
Quoted 2 lines
User space patches can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECThat link appears to be broken.
Unfortunately, GitHub's code search links only work with an account. git grep prints a similar output though.
Quoted 4 lines
Thanks,
jon