Add support for O_MAYEXEC
Subsystems (9)
cover Add support for O_MAYEXEC
Hi,
The goal of this patch series is to control script interpretation. A new O_MAYEXEC flag used by sys_open() is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.
The security policy is the responsibility of an LSM. A basic system-wide policy is implemented with Yama and configurable through a sysctl.
The initial idea come from CLIP OS and the original implementation has been used for more than 10 years:
https://github.com/clipos-archive/clipos4_doc
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
This patch series can be applied on top of v4.20-rc6. This can be tested with CONFIG_SECURITY_YAMA. I would really appreciate constructive comments on this RFC.
Regards,
Mickaël Salaün (5):
fs: Add support for an O_MAYEXEC flag on sys_open()
fs: Add a MAY_EXECMOUNT flag to infer the noexec mount propertie
Yama: Enforces noexec mounts or file executability through O_MAYEXEC selftest/yama: Add tests for O_MAYEXEC enforcing
doc: Add documentation for Yama's open_mayexec_enforce
Documentation/admin-guide/LSM/Yama.rst | 41 +++ MAINTAINERS | 1 + fs/fcntl.c | 2 +- fs/namei.c | 2 + fs/open.c | 4 + include/linux/fcntl.h | 2 +- include/linux/fs.h | 4 + include/uapi/asm-generic/fcntl.h | 3 + security/yama/Kconfig | 3 +- security/yama/yama_lsm.c | 82 +++++- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/yama/.gitignore | 1 + tools/testing/selftests/yama/Makefile | 19 ++ tools/testing/selftests/yama/config | 2 + tools/testing/selftests/yama/test_omayexec.c | 276 +++++++++++++++++++ 15 files changed, 439 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/yama/.gitignore create mode 100644 tools/testing/selftests/yama/Makefile create mode 100644 tools/testing/selftests/yama/config create mode 100644 tools/testing/selftests/yama/test_omayexec.c
Signature · 1 line
2.20.0.rc2
RFC PATCH 1/5 fs: Add support for an O_MAYEXEC flag on sys_open()
When the O_MAYEXEC flag is passed, sys_open() may be subject to additional restrictions depending on a security policy implemented by an LSM through the inode_permission hook.
The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the O_MAYEXEC flag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.
A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC
--- fs/fcntl.c | 2 +- fs/open.c | 4 ++++ include/linux/fcntl.h | 2 +- include/linux/fs.h | 2 ++ include/uapi/asm-generic/fcntl.h | 3 +++ 5 files changed, 11 insertions(+), 2 deletions(-)
fs/fcntl.c +1 -1
@@ -1031,7 +1031,7 @@ static int __init fcntl_init(void)* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ -BUILD_BUG_ON (21 -1 /* for O_RDONLY being 0 */ != +BUILD_BUG_ON (22 -1 /* for O_RDONLY being 0 */ !=HWEIGHT32 ( (VALID_OPEN_FLAGS & ~(O_NONBLOCK |O_NDELAY )) | __FMODE_EXEC | __FMODE_NONOTIFY));
fs/open.c +4 -0
@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode; op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;
include/linux/fcntl.h +1 -1
@@ -9,7 +9,7 @@ (O_RDONLY |O_WRONLY |O_RDWR |O_CREAT |O_EXCL |O_NOCTTY |O_TRUNC | \O_APPEND |O_NDELAY |O_NONBLOCK |O_NDELAY | __O_SYNC |O_DSYNC | \FASYNC |O_DIRECT |O_LARGEFILE |O_DIRECTORY |O_NOFOLLOW | \ - O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE) + O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_MAYEXEC)#ifndef force_o_largefile#define force_o_largefile () (BITS_PER_LONG != 32)
include/linux/fs.h +2 -0
@@ -94,6 +94,8 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,#define MAY_CHDIR 0x00000040/* called from RCU mode, don't block */ #define MAY_NOT_BLOCK 0x00000080 +/* the inode is opened with O_MAYEXEC */ +#define MAY_OPENEXEC 0x00000100/* * flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond
include/uapi/asm-generic/fcntl.h +3 -0
@@ -97,6 +97,9 @@#define O_NDELAY O_NONBLOCK#endif +/* command execution from file is intended, check exec permissions */ +#define O_MAYEXEC 040000000 +#define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */ #define F_SETFD 2/* set/clear close_on_exec */
Signature · 1 line
2.20.0.rc2
Jan Kara
reviewerRe: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 33 lines · Mickaël Salaün
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>
...
Quoted 5 lines
fs/open.c +0
-0
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 10 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode; op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;
I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set FMODE_EXEC on the resulting struct file? That way also security_file_open() can be used to arbitrate such executable opens and in particular fanotify permission event FAN_OPEN_EXEC will get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved in FAN_OPEN_EXEC to CC. Just an idea...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Le 12/12/2018 à 15:43, Jan Kara a écrit :
Quoted 51 lines · Mickaël Salaün
Quoted 33 lines
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 14 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;
Quoted 10 lines
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 1 line
fs/open.c +0
-0op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
Indeed, it may be useful for other LSM.
Mickaël
Mimi Zohar
maintainerRe: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jan KaraQuoted 14 lines · Jan Kara
Quoted 14 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;
Quoted 10 lines
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 1 line
fs/open.c +0
-0op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
Assuming the interpreters are properly modified (and signed), MAY_OPENEXEC closes a major IMA measurement/appraisal gap. The kernel has no insight into the files that the interpreter is opening. Having the interpreter annotate the file open, allows IMA to differentiate scripts opening data files from code.
IMA policy rules could then be written requiring code to be signed.
Example IMA policy rules:
measure func=FILE_CHECK mask=MAY_OPENEXEC
appraise func=FILE_CHECK mask=MAY_OPENEXEC appraise_type=imasig
Mimi
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jan KaraQuoted 50 lines · Jan Kara
Quoted 33 lines
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 14 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;
Quoted 10 lines
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 1 line
fs/open.c +0
-0op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
If I'm understanding this patch series correctly, without an enforced LSM policy there's realistically no added benefit from a security perspective, right? Also, I'm in agreement with what Jan has mentioned in regards to setting the __FMODE_EXEC flag when O_MAYEXEC has been specified. This is something that would work quite nicely in conjunction with some of the new file access notification events.
Rather than setting it on the resulting struct file, couldn't they simply incorporate it as part of op->open_flags when flags & O_MAYEXEC? Not entirely sure whether this is what you actually meant or not though? Pretty much the same as a call to exec(2)/execat(2) when it builds its open_flags.
Signature · 1 line
Matthew Bobrowski
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 62 lines · Matthew Bobrowski
Quoted 50 lines · Jan Kara
Quoted 33 lines
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 14 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;Quoted 10 lines
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 1 line
fs/open.c +0
-0op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
Quoted 3 lines
If I'm understanding this patch series correctly, without an enforced LSM policy there's realistically no added benefit from a security perspective, right?
That's correct. The kernel knows the semantic but the enforcement is delegated to an LSM and its policy.
Quoted 4 lines
Also, I'm in agreement with what Jan has mentioned in regards to setting the
__FMODE_EXECflag whenO_MAYEXEChas been specified. This is something that would work quite nicely in conjunction with some of the new file access notification events.
OK, I will add it in the next patch series (for the new FAN_OPEN_EXEC support).
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jan KaraHello,
Quoted 56 lines · EDT Jan Kara
Quoted 35 lines · Mickaël Salaün
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patchThis patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYE
XECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 17 lines
fs/open.c +0
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags,
umode_tmode, structopen_flags*o>fs/open.c +4
-0if (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;
Quoted 10 lines
Quoted 1 line
op->intent= flags &O_PATH? 0 :LOOKUP_OPEN;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
Late in replying. But I think it's important to have a deep look into the issue.
TL;DR - This is a gentle man's handshake. It won't _really_ solve the problem.
This flag that is being proposed means that you would have to patch all interpreters to use it. If you are sure that upstreams will accept that, why not just change the policy to interpreters shouldn't execute anything unless the execute bit is set? That is simpler and doesn't need a kernel change. And setting the execute bit is an auditable event.
The bottom line is that any interpreter has to become a security policy enforcement point whether by indicating it wants to execute by setting a flag or by refusing to use a file without execute bit set. But this just moves the problem to one that is harder to fix. Why in the world does any programming language allow programs to be loaded via stdin?
It is possible to wget a program and pipe it into python which subsequently pulls down an ELF shared object and runs it all without touching disk via memfd_create (e.g. SnakeEater). This is all direct to memory execution. And direct to memory bypasses anti-virus, selinux, IMA, application whitelisting, and other integrity schemes.
So, to fix this problem, you really need to not allow any programs to load via stdin so that everything that executes has to touch disk. This way you can get a fanotify event and see the application and vote yes/no on allowing it. And this will be particularly harder with the memfd_create fix for the runc container breakout. Prior to that, there were very few uses of that system call. Now it may be very common which means finding malicious use just got harder to spot.
But assuming these problems got fixed, then we have yet another place to look. Many interpreters allow you to specify a command to run via arguments. Some have a small buffer and some allow lengthy programs to be entered as an argument. One strategy might be that an attacker can bootstrap a lengthier program across the network. Python for example allows loading modules across a network. All you need to put in the commandline is the override for the module loader and a couple modules to import. It then loads the modules remotely. Getting rid of this hole will likely lead to some unhappy people - meaning it can't be fixed.
And even if we get that fixed, we have one last hole to plug. Shells. One can simply start a shell and paste their program into the shell and then execute it. You can easily do this with bash or python or any language that has a REPL (read–eval–print loop). To fix this means divorcing the notion of a language from a REPL. Production systems really do not need a Python shell, they need the interpreter. I doubt that this would be popular. But fixing each of these issues is what it would take to prevent unknown software from running. Not going this far leaves holes.
Best Regards,
-Steve
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
* Steve Grubb:
Quoted 5 lines
This flag that is being proposed means that you would have to patch all interpreters to use it. If you are sure that upstreams will accept that, why not just change the policy to interpreters shouldn't execute anything unless the execute bit is set? That is simpler and doesn't need a kernel change. And setting the execute bit is an auditable event.
I think we need something like O_MAYEXEC so that security policies can be enforced and noexec mounts can be detected. I don't think it's a good idea to do this in userspace, especially the latter.
Thanks,
Florian
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 9 lines · EDT Florian Weimer
* Steve Grubb:
Quoted 5 lines
This flag that is being proposed means that you would have to patch all interpreters to use it. If you are sure that upstreams will accept that, why not just change the policy to interpreters shouldn't execute anything unless the execute bit is set? That is simpler and doesn't need a kernel change. And setting the execute bit is an auditable event.
I think we need something like
O_MAYEXECso that security policies can be enforced and noexec mounts can be detected.
Application whitelisting can already today stop unknown software without needing O_MAYEXEC.
Quoted 2 lines
I don't think it's a good idea to do this in userspace, especially the latter.
The problem is that passing O_MAYEXEC is opt-in. You can use ptrace/seccomp/ bpf/LD_PRELOAD/LD_AUDIT to remove that bit from an otherwise normal program. This does not require privs to do so.
But let's consider that this comes to pass and every interpreter is updated and IMA can see the O_MAYEXEC flag. Attackers now simply pivot to running programs via stdin. It never touches disk and therefore nothing enforces security policy. This already is among the most common ways that malware runs today to evade detection.
-Steve
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
* Steve Grubb:
Quoted 8 lines · EDT Florian Weimer
Quoted 7 lines
* Steve Grubb:
Quoted 5 lines
This flag that is being proposed means that you would have to patch all interpreters to use it. If you are sure that upstreams will accept that, why not just change the policy to interpreters shouldn't execute anything unless the execute bit is set? That is simpler and doesn't need a kernel change. And setting the execute bit is an auditable event.
Quoted 5 lines
Quoted 2 lines
I think we need something like
O_MAYEXECso that security policies can be enforced and noexec mounts can be detected.Application whitelisting can already today stop unknown software without needing
O_MAYEXEC.
I'm somewhat interested in using this to add a proper check for executability to explicit dynamic loader invocations. In other words, this
/lib64/ld-linux-x86-64.so.2 /path/to/noexec/fs/program
should refuse to run the program if the program is located on a file system mounted with the noexec attribute.
Quoted 3 lines
The problem is that passing
O_MAYEXECis opt-in. You can use ptrace/seccomp/ bpf/LD_PRELOAD/LD_AUDIT to remove that bit from an otherwise normal program. This does not require privs to do so.
That doesn't really help with the above.
Quoted 5 lines
But let's consider that this comes to pass and every interpreter is updated and IMA can see the
O_MAYEXECflag. Attackers now simply pivot to running programs via stdin. It never touches disk and therefore nothing enforces security policy. This already is among the most common ways that malware runs today to evade detection.
Are you referring to Windows malware using Powershell?
I'm not sure this is applicable to Linux. We do not have much behavioral monitoring anyway.
Thanks,
Florian
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 15 lines · Florian Weimer
* Steve Grubb:
Quoted 13 lines · EDT Florian Weimer
Quoted 9 lines
* Steve Grubb:
Quoted 5 lines
This flag that is being proposed means that you would have to patch all interpreters to use it. If you are sure that upstreams will accept that, why not just change the policy to interpreters shouldn't execute anything unless the execute bit is set? That is simpler and doesn't need a kernel change. And setting the execute bit is an auditable event.
I think we need something like
O_MAYEXECso that security policies can be enforced and noexec mounts can be detected.Application whitelisting can already today stop unknown software without needing
O_MAYEXEC.
Whitelisting may be a lot of thing (path/TPE, signed binaries…), but being able to handle this with a global system configuration (instead of app-specific hardcoded configuration) is a good idea. ;)
Quoted 9 lines
I'm somewhat interested in using this to add a proper check for executability to explicit dynamic loader invocations. In other words, this
/lib64/ld-linux-x86-64.so.2/path/to/noexec/fs/programshould refuse to run the program if the program is located on a file system mounted with the noexec attribute.
What if a sysadmin need to do this on an executable mount point? Being able to enforce a security policy according to a configuration may fit to much more use cases.
Quoted 6 lines
Quoted 4 lines
The problem is that passing
O_MAYEXECis opt-in. You can use ptrace/seccomp/ bpf/LD_PRELOAD/LD_AUDIT to remove that bit from an otherwise normal program. This does not require privs to do so.That doesn't really help with the above.
Right, ptrace/LD_PRELOAD and so on must be addressed by something else than only O_MAYEXEC.
Quoted 6 lines
Quoted 6 lines
But let's consider that this comes to pass and every interpreter is updated and IMA can see the
O_MAYEXECflag. Attackers now simply pivot to running programs via stdin. It never touches disk and therefore nothing enforces security policy. This already is among the most common ways that malware runs today to evade detection.
As my previous reply, use cases like stdin may be restricted as well.
Quoted 9 lines
Are you referring to Windows malware using Powershell?
I'm not sure this is applicable to Linux. We do not have much behavioral monitoring anyway.
Thanks,
Florian
--
Mickaël Salaün
ANSSI/SDE/ST/LAM
Les données à caractère personnel recueillies et traitées dans le cadre de cet échange, le sont à seule fin d’exécution d’une relation professionnelle et s’opèrent dans cette seule finalité et pour la durée nécessaire à cette relation. Si vous souhaitez faire usage de vos droits de consultation, de rectification et de suppression de vos données, veuillez contacter [email protected]. Si vous avez reçu ce message par erreur, nous vous remercions d’en informer l’expéditeur et de détruire le message. The personal data collected and processed during this exchange aims solely at completing a business relationship and is limited to the necessary duration of that relationship. If you wish to use your rights of consultation, rectification and deletion of your data, please contact: [email protected]. If you have received this message in error, we thank you for informing the sender and destroying the message.
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Steve GrubbQuoted 70 lines · Steve Grubb
Hello,
Quoted 56 lines · EDT Jan Kara
Quoted 35 lines · Mickaël Salaün
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patchThis patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYE
XECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 17 lines
fs/open.c +0
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags,
umode_tmode, structopen_flags*o>
if (flags &O_APPEND)
acc_mode|=MAY_APPEND;fs/open.c +4
-0+/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;Quoted 10 lines
Quoted 1 line
op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
Quoted 5 lines
Late in replying. But I think it's important to have a deep look into the issue.
TL;DR - This is a gentle man's handshake. It won't _really_ solve the problem.
Thanks for your comments. You should find most answers in this thread:
https://lore.kernel.org/lkml/[email protected]/
The threat model targets persistent attacks. This O_MAYEXEC flag is not a silver bullet but it's a needed block to enforce a security policy on a trusted system. This means that every component executable on the system must be controlled, which means they may need some bit of customization. Today no userspace application use this flag (except in CLIP OS), but we need to first create a feature before it can be used.
It is very important to have in mind that a system security policy need to have a (central) security manager, in this case the kernel thanks to Yama's policy (but it could be SELinux, IMA or any other LSM). The goal is not to give to the developer the job of defining a security policy for the *system*; this job is for the system administrator (or the distro).
Quoted 6 lines
This flag that is being proposed means that you would have to patch all interpreters to use it. If you are sure that upstreams will accept that, why not just change the policy to interpreters shouldn't execute anything unless the execute bit is set? That is simpler and doesn't need a kernel change. And setting the execute bit is an auditable event.
As said above, the definition of a the security policy is the job of the system administrator. Moreover, the security policy may be defined by the mount point restrictions (i.e. noexec) but it should be definable with something else (e.g. a SELinux or IMA policy which may be agnostic to the mount points).
Quoted 13 lines
The bottom line is that any interpreter has to become a security policy enforcement point whether by indicating it wants to execute by setting a flag or by refusing to use a file without execute bit set. But this just moves the problem to one that is harder to fix. Why in the world does any programming language allow programs to be loaded via stdin?
It is possible to wget a program and pipe it into python which subsequently pulls down an ELF shared object and runs it all without touching disk via
memfd_create(e.g. SnakeEater). This is all direct to memory execution. And direct to memory bypasses anti-virus, selinux, IMA, application whitelisting, and other integrity schemes.
Quoted 7 lines
So, to fix this problem, you really need to not allow any programs to load via stdin so that everything that executes has to touch disk. This way you can get a fanotify event and see the application and vote yes/no on allowing it. And this will be particularly harder with the
memfd_createfix for the runc container breakout. Prior to that, there were very few uses of that system call. Now it may be very common which means finding malicious use just got harder to spot.
As said above, stdin must be restricted in some way. You may want to take a look at the CLIP OS patches (which doesn't only add the O_MAYEXEC flag but restrict other way to interpret code). It may be foolish to block or restrict stdin for interpreters on a developer workstation, but it may make sense for an embedded custom system.
The same apply for memfd_create. If you want to enforce a security policy on this kind of *file descriptor*, you should ask to the proper LSM to do so. The current Yama patch deal with this kind of FD if they are accessed through /proc/*/fd because the procfs is mounted with noexec. Anyway, the interpreter must *inform* the LSM that it wants to execute/interpret something from this FD, which is done thanks to the O_MAYEXEC flag.
Quoted 10 lines
But assuming these problems got fixed, then we have yet another place to look. Many interpreters allow you to specify a command to run via arguments. Some have a small buffer and some allow lengthy programs to be entered as an argument. One strategy might be that an attacker can bootstrap a lengthier program across the network. Python for example allows loading modules across a network. All you need to put in the commandline is the override for the module loader and a couple modules to import. It then loads the modules remotely. Getting rid of this hole will likely lead to some unhappy people - meaning it can't be fixed.
Again, this depend on the threat model and the corresponding product. If you want to handle everything on your system, then you may need some adjustments.
Quoted 9 lines
And even if we get that fixed, we have one last hole to plug. Shells. One can simply start a shell and paste their program into the shell and then execute it. You can easily do this with bash or python or any language that has a REPL (read–eval–print loop). To fix this means divorcing the notion of a language from a REPL. Production systems really do not need a Python shell, they need the interpreter. I doubt that this would be popular. But fixing each of these issues is what it would take to prevent unknown software from running. Not going this far leaves holes.
This is also covered by the threat model defined in the patch 3/5 (i.e. protect the kernel by restricting arbitrary syscalls).
Regards,
--
Mickaël Salaün
ANSSI/SDE/ST/LAM
Les données à caractère personnel recueillies et traitées dans le cadre de cet échange, le sont à seule fin d’exécution d’une relation professionnelle et s’opèrent dans cette seule finalité et pour la durée nécessaire à cette relation. Si vous souhaitez faire usage de vos droits de consultation, de rectification et de suppression de vos données, veuillez contacter [email protected]. Si vous avez reçu ce message par erreur, nous vous remercions d’en informer l’expéditeur et de détruire le message. The personal data collected and processed during this exchange aims solely at completing a business relationship and is limited to the necessary duration of that relationship. If you wish to use your rights of consultation, rectification and deletion of your data, please contact: [email protected]. If you have received this message in error, we thank you for informing the sender and destroying the message.
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jan KaraQuoted 52 lines · Jan Kara
Quoted 33 lines · Mickaël Salaün
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 14 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;
Quoted 11 lines
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 1 line
fs/open.c +0
-0op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
I would really like to land this patch. I'm fiddling with making bpffs handle permissions intelligently, and the lack of a way to say "hey, I want to open this bpf program so that I can run it" is annoying.
--Andy
Re: [RFC PATCH v1 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 65 lines · Andy Lutomirski
Quoted 52 lines · Jan Kara
Quoted 33 lines · Mickaël Salaün
When the
O_MAYEXECflag is passed,sys_open()may be subject to additional restrictions depending on a security policy implemented by an LSM through theinode_permissionhook.The underlying idea is to be able to restrict scripts interpretation according to a policy defined by the system administrator. For this to be possible, script interpreters must use the
O_MAYEXECflag appropriately. To be fully effective, these interpreters also need to handle the other ways to execute code (for which the kernel can't help):
command line parameters (e.g., option -e for Perl), module loading (e.g., option -m for Python), stdin, file sourcing, environment variables, configuration files... According to the threat model, it may be acceptable to allow some script interpreters (e.g. Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it may not be enough to (directly) perform syscalls.A simple security policy implementation is available in a following patch for Yama.
This is an updated subset of the patch initially written by Vincent Strubel for CLIP OS:
https://github.com/clipos-archive/src_platform_clip-patches/blob/f5cb330d6b684752e403b4e41b39f7004d88e561/1901_open_mayexec.patch
This patch has been used for more than 10 years with customized script interpreters. Some examples can be found here:
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXECSigned-off-by Mickaël Salaün <[email protected]>Signed-off-by Thibaut Sautereau <[email protected]>Signed-off-by Vincent Strubel <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Cc Al Viro <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>...
Quoted 14 lines
fs/open.c +4
-0@@ -974,6 +974,10 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *oif (flags &O_APPEND ) acc_mode |=MAY_APPEND ; +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) + acc_mode |=MAY_OPENEXEC ; + op->acc_mode = acc_mode;Quoted 11 lines
fs/open.c static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o · line 983
Quoted 1 line
fs/open.c +0
-0op->intent = flags &O_PATH ?0 :LOOKUP_OPEN ;I don't feel experienced enough in security to tell whether we want this functionality or not. But if we do this, shouldn't we also set
FMODE_EXECon the resulting struct file? That way alsosecurity_file_open()can be used to arbitrate such executable opens and in particular fanotify permission eventFAN_OPEN_EXECwill get properly generated which I guess is desirable (support for it is sitting in my tree waiting for the merge window) - adding some audit people involved inFAN_OPEN_EXECto CC. Just an idea...
Quoted 4 lines
I would really like to land this patch. I'm fiddling with making bpffs handle permissions intelligently, and the lack of a way to say "hey, I want to open this bpf program so that I can run it" is annoying.
Are you OK with this series? What about Aleksa's work on openat2(), and Sean's work on SGX/noexec? Is it time to send a new patch series (with a dedicated LSM instead of Yama)?
RFC PATCH 2/5 fs: Add a MAY_EXECMOUNT flag to infer the noexec mount propertie
In reply to Mickaël SalaünAn LSM doesn't get path information related to an access request to open an inode. This new (internal) MAY_EXECMOUNT flag enables an LSM to check if the underlying mount point of an inode is marked as executable. This is useful to implement a security policy taking advantage of the noexec mount option.
This flag is set according to path_noexec(), which checks if a mount point is mounted with MNT_NOEXEC or if the underlying superblock is SB_I_NOEXEC.
--- fs/namei.c | 2 ++ include/linux/fs.h | 2 ++ 2 files changed, 4 insertions(+)
fs/namei.c +2 -0
@@ -2970,6 +2970,8 @@ static int may_open(const struct path *path, int acc_mode, int flag)break ; } +/* Pass the mount point executability. */ + acc_mode |=path_noexec (path) ?0 :MAY_EXECMOUNT ; error =inode_permission (inode,MAY_OPEN | acc_mode);if (error)return error;
include/linux/fs.h +2 -0
@@ -96,6 +96,8 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,#define MAY_NOT_BLOCK 0x00000080/* the inode is opened with O_MAYEXEC */ #define MAY_OPENEXEC 0x00000100 +/* the mount point is marked as executable */ +#define MAY_EXECMOUNT 0x00000200/* * flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond
Signature · 1 line
2.20.0.rc2
RFC PATCH 3/5 Yama: Enforces noexec mounts or file executability through O_MAYEXEC
In reply to Mickaël SalaünEnable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
The main goal is to be able 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).
Add a new sysctl kernel.yama.open_mayexec_enforce to control this behavior. A following patch adds documentation.
--- security/yama/Kconfig | 3 +- security/yama/yama_lsm.c | 82 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-)
security/yama/Kconfig +2 -1
@@ -5,7 +5,8 @@ config SECURITY_YAMA help This selects Yama, which extends DAC support with additional system-wide security settings beyond regular Linux discretionary - access controls. Currently available is ptrace scope restriction. + access controls. Currently available are ptrace scope restriction and + enforcement of the O_MAYEXEC open flag. Like capabilities, this security module stacks with other LSMs. Further information can be found in Documentation/admin-guide/LSM/Yama.rst.
security/yama/yama_lsm.c +81 -1
117 lines not on this page · raw message
Signature · 1 line
2.20.0.rc2
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
Le 12/12/2018 à 09:17, Mickaël Salaün a écrit :
Quoted 84 lines
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
The main goal is to be able 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).
Add a new sysctl kernel.yama.open_mayexec_enforce to control this behavior. A following patch adds documentation.
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Reviewed-by Thibaut Sautereau <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>--- security/yama/Kconfig | 3 +- security/yama/yama_lsm.c | 82 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-)security/yama/Kconfig +2
-1@@ -5,7 +5,8 @@ config SECURITY_YAMA help This selects Yama, which extends DAC support with additional system-wide security settings beyond regular Linux discretionary - access controls. Currently available is ptrace scope restriction. + access controls. Currently available are ptrace scope restriction and + enforcement of the O_MAYEXEC open flag. Like capabilities, this security module stacks with other LSMs. Further information can be found in Documentation/admin-guide/LSM/Yama.rst.security/yama/yama_lsm.c +17
-1@@ -1,10 +1,12 @@/* * Yama Linux Security Module * -* Author: Kees Cook <[email protected]> +* Authors: Kees Cook <[email protected]> +* Mickaël Salaün <[email protected]> * * Copyright (C) 2010 Canonical, Ltd. * Copyright (C) 2011 The Chromium OS Authors. +* Copyright (C) 2018 ANSSI * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, as @@ -28,7 +30,14 @@#define YAMA_SCOPE_CAPABILITY 2#define YAMA_SCOPE_NO_ATTACH 3 +#define YAMA_OMAYEXEC_ENFORCE_NONE 0 +#define YAMA_OMAYEXEC_ENFORCE_MOUNT (1 << 0) +#define YAMA_OMAYEXEC_ENFORCE_FILE (1 << 1) +#define _YAMA_OMAYEXEC_LAST YAMA_OMAYEXEC_ENFORCE_FILE +#define _YAMA_OMAYEXEC_MASK ((_YAMA_OMAYEXEC_LAST << 1) - 1) +static int ptrace_scope =YAMA_SCOPE_RELATIONAL ; +static int open_mayexec_enforce =YAMA_OMAYEXEC_ENFORCE_NONE ;/* describe a ptrace relationship for potential exception */ struct ptrace_relation {@@ -423,7 +432,40 @@ int yama_ptrace_traceme(struct task_struct *parent)return rc; } +/** +* yama_inode_permission - check O_MAYEXEC permission before accessing an inode +* @inode: inode structure to check +* @mask: permission mask +* +* Return 0 if access is permitted, -EACCES otherwise. +*/
security/yama/yama_lsm.c int yama_ptrace_traceme(struct task_struct *parent) · line 451
Quoted 10 lines
security/yama/yama_lsm.c +10
-0+int yama_inode_permission (struct inode *inode,int mask) +{ +if (!(mask &MAY_OPENEXEC )) +return 0 ; +/* +* Match regular files and directories to make it easier to +* modify script interpreters. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISDIR (inode->i_mode)) +return 0 ;
I forgot to mention that these checks do not handle fifos. This is relevant in a threat model targeting persistent attacks (and with additional protections/restrictions).
Quoted 73 lines
security/yama/yama_lsm.c +54
-0+ +if ((open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_MOUNT ) && + !(mask &MAY_EXECMOUNT )) +return -EACCES ; + +/* +* May prefer acl_permission_check() instead of generic_permission(), +* to not be bypassable with CAP_DAC_READ_SEARCH. +*/ +if (open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_FILE ) +return generic_permission (inode,MAY_EXEC ); + +return 0 ; +} + static struct security_hook_list yama_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (inode_permission, yama_inode_permission),LSM_HOOK_INIT (ptrace_access_check, yama_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, yama_ptrace_traceme),LSM_HOOK_INIT (task_prctl, yama_task_prctl),@@ -447,6 +489,37 @@ static int yama_dointvec_minmax(struct ctl_table *table, int write,return proc_dointvec_minmax (&table_copy, write, buffer, lenp, ppos); } +static int yama_dointvec_bitmask_macadmin (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos) +{ +int error; + +if (write) { +struct ctl_table table_copy; +int tmp_mayexec_enforce; + +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; + tmp_mayexec_enforce = *((int *)table->data); + table_copy = *table; +/* do not erase open_mayexec_enforce */ + table_copy.data = &tmp_mayexec_enforce; + error =proc_dointvec (&table_copy, write, buffer, lenp, ppos); +if (error) +return error; +if ((tmp_mayexec_enforce | _YAMA_OMAYEXEC_MASK) != + _YAMA_OMAYEXEC_MASK) +return -EINVAL ; + *((int *)table->data) = tmp_mayexec_enforce; + }else { + error =proc_dointvec (table, write, buffer, lenp, ppos); +if (error) +return error; + } +return 0 ; +} +static int zero;static int max_scope =YAMA_SCOPE_NO_ATTACH ;@@ -466,6 +539,13 @@ static struct ctl_table yama_sysctl_table[] = { .extra1 = &zero, .extra2 = &max_scope, }, + { + .procname = "open_mayexec_enforce", + .data = &open_mayexec_enforce, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = yama_dointvec_bitmask_macadmin, + }, { } };static void __init yama_init_sysctl (void )
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
In reply to Mickaël SalaünQuoted 22 lines · Mickaël Salaün
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
The main goal is to be able 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).
Add a new sysctl kernel.yama.open_mayexec_enforce to control this behavior. A following patch adds documentation.
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Reviewed-by Thibaut Sautereau <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>---
[...]
security/yama/yama_lsm.c int yama_ptrace_traceme(struct task_struct *parent) · line 442
Quoted 8 lines
security/yama/yama_lsm.c +8
-0+/** +* yama_inode_permission - check O_MAYEXEC permission before accessing an inode +* @inode: inode structure to check +* @mask: permission mask +* +* Return 0 if access is permitted, -EACCES otherwise. +*/ +int yama_inode_permission (struct inode *inode,int mask)
This should be static, no?
security/yama/yama_lsm.c int yama_ptrace_traceme(struct task_struct *parent) · line 451
Quoted 9 lines
security/yama/yama_lsm.c +9
-0+{ +if (!(mask &MAY_OPENEXEC )) +return 0 ; +/* +* Match regular files and directories to make it easier to +* modify script interpreters. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISDIR (inode->i_mode)) +return 0 ;
So files are subject to checks, but loading code from things like sockets is always fine?
Quoted 25 lines
security/yama/yama_lsm.c +17
-0+if ((open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_MOUNT ) && + !(mask &MAY_EXECMOUNT )) +return -EACCES ; + +/* +* May prefer acl_permission_check() instead of generic_permission(), +* to not be bypassable with CAP_DAC_READ_SEARCH. +*/ +if (open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_FILE ) +return generic_permission (inode,MAY_EXEC ); + +return 0 ; +} + static struct security_hook_list yama_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (inode_permission, yama_inode_permission),LSM_HOOK_INIT (ptrace_access_check, yama_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, yama_ptrace_traceme),LSM_HOOK_INIT (task_prctl, yama_task_prctl),@@ -447,6 +489,37 @@ static int yama_dointvec_minmax(struct ctl_table *table, int write,return proc_dointvec_minmax (&table_copy, write, buffer, lenp, ppos); } +static int yama_dointvec_bitmask_macadmin(struct ctl_table *table, int write, + void __user *buffer, size_t *lenp,
security/yama/yama_lsm.c static int yama_dointvec_minmax(struct ctl_table *table, int write, · line 503
Quoted 10 lines
security/yama/yama_lsm.c +10
-0+ loff_t *ppos) +{ +int error; + +if (write) { +struct ctl_table table_copy; +int tmp_mayexec_enforce; + +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ;
Don't put capable() checks in sysctls, it doesn't work.
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
Quoted 24 lines · Jann Horn
Quoted 22 lines · Mickaël Salaün
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
The main goal is to be able 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).
Add a new sysctl kernel.yama.open_mayexec_enforce to control this behavior. A following patch adds documentation.
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Philippe Trébuchet <[email protected]>Reviewed-by Thibaut Sautereau <[email protected]>Cc Kees Cook <[email protected]>Cc Mickaël Salaün <[email protected]>---
[...]
Quoted 10 lines
security/yama/yama_lsm.c int yama_ptrace_traceme(struct task_struct *parent) · line 442
Quoted 8 lines
security/yama/yama_lsm.c +8
-0+/** +* yama_inode_permission - check O_MAYEXEC permission before accessing an inode +* @inode: inode structure to check +* @mask: permission mask +* +* Return 0 if access is permitted, -EACCES otherwise. +*/ +int yama_inode_permission (struct inode *inode,int mask)This should be static, no?
Right, it will be in the next series. The previous function (yama_ptrace_traceme) is not static though.
Quoted 11 lines
security/yama/yama_lsm.c int yama_ptrace_traceme(struct task_struct *parent) · line 451
Quoted 10 lines
security/yama/yama_lsm.c +9
-0+{ +if (!(mask &MAY_OPENEXEC )) +return 0 ; +/* +* Match regular files and directories to make it easier to +* modify script interpreters. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISDIR (inode->i_mode)) +return 0 ;
Quoted 2 lines
So files are subject to checks, but loading code from things like sockets is always fine?
As I said in a previous email, these checks do not handle fifo either. This is relevant in a threat model targeting persistent attacks (and with additional protections/restrictions). We may want to only whitelist fifo, but I don't get how a socket is relevant here. Can you please clarify?
Quoted 24 lines
Quoted 24 lines
security/yama/yama_lsm.c +15
-0+if ((open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_MOUNT ) && + !(mask &MAY_EXECMOUNT )) +return -EACCES ; + +/* +* May prefer acl_permission_check() instead of generic_permission(), +* to not be bypassable with CAP_DAC_READ_SEARCH. +*/ +if (open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_FILE ) +return generic_permission (inode,MAY_EXEC ); + +return 0 ; +} + static struct security_hook_list yama_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (inode_permission, yama_inode_permission),LSM_HOOK_INIT (ptrace_access_check, yama_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, yama_ptrace_traceme),LSM_HOOK_INIT (task_prctl, yama_task_prctl),@@ -447,6 +489,37 @@ static int yama_dointvec_minmax(struct ctl_table *table, int write,return proc_dointvec_minmax (&table_copy, write, buffer, lenp, ppos); }
Quoted 15 lines
security/yama/yama_lsm.c static int yama_dointvec_minmax(struct ctl_table *table, int write, · line 503
Quoted 12 lines
security/yama/yama_lsm.c +12
-0+static int yama_dointvec_bitmask_macadmin (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos) +{ +int error; + +if (write) { +struct ctl_table table_copy; +int tmp_mayexec_enforce; + +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ;Don't put
capable()checks in sysctls, it doesn't work.
I tested it and the root user can indeed open the file even if the process doesn't have CAP_MAC_ADMIN, however writing in the sysctl file is denied. Btw there is a similar check in the previous function (yama_dointvec_minmax).
Thanks
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
Quoted 17 lines · Mickaël Salaün
Quoted 16 lines · Jann Horn
Quoted 15 lines · Mickaël Salaün
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
The main goal is to be able 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).
Add a new sysctl kernel.yama.open_mayexec_enforce to control this behavior. A following patch adds documentation.
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
security/yama/yama_lsm.c +9
-0+{ +if (!(mask &MAY_OPENEXEC )) +return 0 ; +/* +* Match regular files and directories to make it easier to +* modify script interpreters. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISDIR (inode->i_mode)) +return 0 ;
Quoted 7 lines
Quoted 2 lines
So files are subject to checks, but loading code from things like sockets is always fine?
As I said in a previous email, these checks do not handle fifo either. This is relevant in a threat model targeting persistent attacks (and with additional protections/restrictions). We may want to only whitelist fifo, but I don't get how a socket is relevant here. Can you please clarify?
I don't think that there's a security problem here. I just think it's weird to have the extra check when it seems to me like it isn't really necessary - nobody is going to want to execute a socket or fifo anyway, right?
Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
security/yama/yama_lsm.c +15
-0+if ((open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_MOUNT ) && + !(mask &MAY_EXECMOUNT )) +return -EACCES ; + +/* +* May prefer acl_permission_check() instead of generic_permission(), +* to not be bypassable with CAP_DAC_READ_SEARCH. +*/ +if (open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_FILE ) +return generic_permission (inode,MAY_EXEC ); + +return 0 ; +} + static struct security_hook_list yama_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (inode_permission, yama_inode_permission),LSM_HOOK_INIT (ptrace_access_check, yama_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, yama_ptrace_traceme),LSM_HOOK_INIT (task_prctl, yama_task_prctl),@@ -447,6 +489,37 @@ static int yama_dointvec_minmax(struct ctl_table *table, int write,return proc_dointvec_minmax (&table_copy, write, buffer, lenp, ppos); }Quoted 15 lines
security/yama/yama_lsm.c static int yama_dointvec_minmax(struct ctl_table *table, int write, · line 503
Quoted 12 lines
security/yama/yama_lsm.c +12
-0+static int yama_dointvec_bitmask_macadmin (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos) +{ +int error; + +if (write) { +struct ctl_table table_copy; +int tmp_mayexec_enforce; + +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ;Don't put
capable()checks in sysctls, it doesn't work.
Quoted 4 lines
I tested it and the root user can indeed open the file even if the process doesn't have
CAP_MAC_ADMIN, however writing in the sysctl file is denied. Btw there is a similar check in the previous function (yama_dointvec_minmax).
It's still wrong. If an attacker without CAP_MAC_ADMIN opens the sysctl file, then passes the file descriptor to a setcap binary that has CAP_MAC_ADMIN as stdout/stderr, and the setcap binary writes to it, then the capable() check is bypassed. (But of course, to open the sysctl file in the first place, you'd need to be root (uid 0), so the check doesn't really matter.)
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
Quoted 38 lines · Jann Horn
Quoted 17 lines · Mickaël Salaün
Quoted 16 lines · Jann Horn
Quoted 15 lines · Mickaël Salaün
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
The main goal is to be able 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).
Add a new sysctl kernel.yama.open_mayexec_enforce to control this behavior. A following patch adds documentation.
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
security/yama/yama_lsm.c +9
-0+{ +if (!(mask &MAY_OPENEXEC )) +return 0 ; +/* +* Match regular files and directories to make it easier to +* modify script interpreters. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISDIR (inode->i_mode)) +return 0 ;Quoted 7 lines
Quoted 2 lines
So files are subject to checks, but loading code from things like sockets is always fine?
As I said in a previous email, these checks do not handle fifo either. This is relevant in a threat model targeting persistent attacks (and with additional protections/restrictions). We may want to only whitelist fifo, but I don't get how a socket is relevant here. Can you please clarify?
Quoted 4 lines
I don't think that there's a security problem here. I just think it's weird to have the extra check when it seems to me like it isn't really necessary - nobody is going to want to execute a socket or fifo anyway, right?
Right, the fifo whitelisting should answer your concern then.
Quoted 46 lines
Quoted 41 lines
Quoted 25 lines
Quoted 25 lines
security/yama/yama_lsm.c +15
-0+if ((open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_MOUNT ) && + !(mask &MAY_EXECMOUNT )) +return -EACCES ; + +/* +* May prefer acl_permission_check() instead of generic_permission(), +* to not be bypassable with CAP_DAC_READ_SEARCH. +*/ +if (open_mayexec_enforce &YAMA_OMAYEXEC_ENFORCE_FILE ) +return generic_permission (inode,MAY_EXEC ); + +return 0 ; +} + static struct security_hook_list yama_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (inode_permission, yama_inode_permission),LSM_HOOK_INIT (ptrace_access_check, yama_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, yama_ptrace_traceme),LSM_HOOK_INIT (task_prctl, yama_task_prctl),@@ -447,6 +489,37 @@ static int yama_dointvec_minmax(struct ctl_table *table, int write,return proc_dointvec_minmax (&table_copy, write, buffer, lenp, ppos); }Quoted 15 lines
security/yama/yama_lsm.c static int yama_dointvec_minmax(struct ctl_table *table, int write, · line 503
Quoted 12 lines
security/yama/yama_lsm.c +12
-0+static int yama_dointvec_bitmask_macadmin (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos) +{ +int error; + +if (write) { +struct ctl_table table_copy; +int tmp_mayexec_enforce; + +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ;Don't put
capable()checks in sysctls, it doesn't work.Quoted 4 lines
I tested it and the root user can indeed open the file even if the process doesn't have
CAP_MAC_ADMIN, however writing in the sysctl file is denied. Btw there is a similar check in the previous function (yama_dointvec_minmax).
Quoted 6 lines
It's still wrong. If an attacker without
CAP_MAC_ADMINopens the sysctl file, then passes the file descriptor to a setcap binary that hasCAP_MAC_ADMINas stdout/stderr, and the setcap binary writes to it, then thecapable()check is bypassed. (But of course, to open the sysctl file in the first place, you'd need to be root (uid 0), so the check doesn't really matter.)
I agree with you that a confused deputy attack may uses file descriptors, but I don't see how the current sysctl API may be used to check the process capability at open time. Anyway, on a properly configured system, especially one leveraging Linux capabilities (e.g. CLIP OS), root processes may not have CAP_SYS_ADMIN. Moreover, SUID or fcap binaries may not be available to an attacker (e.g. in a container).
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
Quoted 12 lines · Mickaël Salaün
Quoted 9 lines · Jann Horn
Quoted 7 lines · Mickaël Salaün
Quoted 6 lines · Jann Horn
Quoted 5 lines · Mickaël Salaün
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
The main goal is to be able 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).
I like this idea, but I think it shouldn't live in Yama (since it is currently intended to be a ptrace-policy-only LSM). It was _originally_ designed to do various DAC improvements, but the agreement was that those should live directly in the VFS instead (i.e. the symlink, hardlink and now fifo and regular file defenses).
This should likely go in similarly. (But if not, it could also be its own LSM.)
Signature · 1 line
Kees Cook
Re: [RFC PATCH v1 3/5] Yama: Enforces noexec mounts or file executability through O_MAYEXEC
Quoted 22 lines · Kees Cook
Quoted 12 lines · Mickaël Salaün
Quoted 9 lines · Jann Horn
Quoted 7 lines · Mickaël Salaün
Quoted 6 lines · Jann Horn
Quoted 5 lines · Mickaël Salaün
Enable to either propagate the mount options from the underlying VFS mount to prevent execution, or to propagate the file execute permission. This may allow a script interpreter to check execution permissions before reading commands from a file.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
The main goal is to be able 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).
Quoted 8 lines
I like this idea, but I think it shouldn't live in Yama (since it is currently intended to be a ptrace-policy-only LSM). It was _originally_ designed to do various DAC improvements, but the agreement was that those should live directly in the VFS instead (i.e. the symlink, hardlink and now fifo and regular file defenses).
This should likely go in similarly. (But if not, it could also be its own LSM.)
I think that Yama is quite handy and make sense here, but I'm fine putting this knob elsewhere. However, I was thinking, for a future patch series, to add another sysctl to lock this choice, i.e. generalizing the way Yama can lock the ptrace_scope.
What matter here is the ability for an LSM to use this O_MAYEXEC flag. Yama is a good place to showcase this feature and I think it is cleaner to leverage the LSM framework to put new (optional) security features. I can easily create a new LSM but it would be pretty similar to Yama... What do you think about it James and Al?
Side question: wouldn't it be better to use a 0600 mode (instead of 0644) for this kind of sysctl?
RFC PATCH 4/5 selftest/yama: Add tests for O_MAYEXEC enforcing
In reply to Mickaël SalaünTest propagation of noexec mount points or file executability through files open with or without O_MAYEXEC.
--- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/yama/.gitignore | 1 + tools/testing/selftests/yama/Makefile | 19 ++ tools/testing/selftests/yama/config | 2 + tools/testing/selftests/yama/test_omayexec.c | 276 +++++++++++++++++++ 6 files changed, 300 insertions(+) create mode 100644 tools/testing/selftests/yama/.gitignore create mode 100644 tools/testing/selftests/yama/Makefile create mode 100644 tools/testing/selftests/yama/config create mode 100644 tools/testing/selftests/yama/test_omayexec.c
MAINTAINERS +1 -0
@@ -16591,6 +16591,7 @@ M: Kees Cook <[email protected]> T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git yama/tip S: Supported F: security/yama/ +F: tools/testing/selftests/yama/ F: Documentation/admin-guide/LSM/Yama.rst YEALINK PHONE DRIVER
tools/testing/selftests/Makefile +1 -0
@@ -46,6 +46,7 @@ endif TARGETS += user TARGETS += vm TARGETS += x86 +TARGETS += yama TARGETS += zram #Please keep the TARGETS list alphabetically sorted # Run "make quicktest=1 run_tests" or
tools/testing/selftests/yama/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/test_omayexec
tools/testing/selftests/yama/Makefile new file +19 -0
@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0 + +all: + +include ../lib.mk + +.PHONY: all clean + +BINARIES := test_omayexec +CFLAGS += -Wl,-no-as-needed -Wall -Werror +LDFLAGS += -lcap + +test_omayexec: test_omayexec.c ../kselftest_harness.h + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ + +TEST_PROGS += $(BINARIES) +EXTRA_CLEAN := $(BINARIES) + +all: $(BINARIES)
tools/testing/selftests/yama/config new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_SECURITY=y +CONFIG_SECURITY_YAMA=y
tools/testing/selftests/yama/test_omayexec.c new file +276 -0
276 lines not on this page · raw message
Signature · 1 line
2.20.0.rc2
RFC PATCH 5/5 doc: Add documentation for Yama's open_mayexec_enforce
In reply to Mickaël Salaün--- Documentation/admin-guide/LSM/Yama.rst | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
Documentation/admin-guide/LSM/Yama.rst +41 -0
@@ -72,3 +72,44 @@ The sysctl settings (writable only with ``CAP_SYS_PTRACE``) are: ``PTRACE_TRACEME``. Once set, this sysctl value cannot be changed. The original children-only logic was based on the restrictions in grsecurity. + +open_mayexec_enforce +==================== + +The ``O_MAYEXEC`` flag can be passed to :manpage:`open(2)` to only open files +(or directories) that are executable. If the file is not identified as +executable, then the syscall returns -EACCES. This may allow a script +interpreter to check executable permission before reading commands from a file. +One interesting use case is to enforce a "write xor execute" policy through +interpreters. + +Thanks to this flag, Yama enables to enforce the ``noexec`` mount option (i.e. +the underlying mount point of the file is mounted with MNT_NOEXEC or its +underlying superblock is SB_I_NOEXEC) not only on ELF binaries but also on +scripts. This may be possible thanks to script interpreters using the +``O_MAYEXEC`` flag. The executable permission is then checked before reading +commands from a file, and thus can enforce the ``noexec`` at the interpreter +level by propagating this security policy to the scripts. To be fully +effective, these interpreters also need to handle the other ways to execute +code (for which the kernel can't help): command line parameters (e.g., option +``-e`` for Perl), module loading (e.g., option ``-m`` for Python), stdin, file +sourcing, environment variables, configuration files... According to the +threat model, it may be acceptable to allow some script interpreters (e.g. +Bash) to interpret commands from stdin, may it be a TTY or a pipe, because it +may not be enough to (directly) perform syscalls. + +Yama implements two complementary security policies to propagate the ``noexec`` +mount option or the executable file permission. These policies are handled by +the ``kernel.yama.open_mayexec_enforce`` sysctl (writable only with +``CAP_MAC_ADMIN``) as a bitmask: + +1 - mount restriction: + check that the mount options for the underlying VFS mount do not prevent + execution. + +2 - file permission restriction: + check that the to-be-opened file is marked as executable for the current + process (e.g., POSIX permissions). + +Code samples can be found in tools/testing/selftests/yama/test_omayexec.c and +https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC .
Signature · 1 line
2.20.0.rc2
Quoted 23 lines · Mickaël Salaün
Hi,
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.The security policy is the responsibility of an LSM. A basic system-wide policy is implemented with Yama and configurable through a sysctl.
The initial idea come from CLIP OS and the original implementation has been used for more than 10 years:
https://github.com/clipos-archive/clipos4_docAn introduction to
O_MAYEXECwas 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
Quoted 6 lines
This patch series can be applied on top of v4.20-rc6. This can be tested with
CONFIG_SECURITY_YAMA. I would really appreciate constructive comments on this RFC.Regards,
Are various interpreters upstreams interested in adding support for O_MAYEXEC if it land in kernel? Did you contacted them about this?
Jordan
Le 12/12/2018 à 17:29, Jordan Glover a écrit :
Quoted 29 lines · Mickaël Salaün
Quoted 28 lines
Hi,
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.The security policy is the responsibility of an LSM. A basic system-wide policy is implemented with Yama and configurable through a sysctl.
The initial idea come from CLIP OS and the original implementation has been used for more than 10 years:
https://github.com/clipos-archive/clipos4_docAn introduction to
O_MAYEXECwas 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=11m14sThis patch series can be applied on top of v4.20-rc6. This can be tested with
CONFIG_SECURITY_YAMA. I would really appreciate constructive comments on this RFC.
Quoted 5 lines
Quoted 2 lines
Regards,
Are various interpreters upstreams interested in adding support for
O_MAYEXECif it land in kernel? Did you contacted them about this?
I think the first step is to be OK on the kernel side. We will then be able to help upstream interpreters implement this feature. It should be OK because the behavior doesn't change by default, i.e. if the sysadmin doesn't configure (and test) the whole system. Some examples of modified interpreters can be found at
https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC
.
Mickaël
James Morris
maintainerQuoted 11 lines · Mickaël Salaün
Hi,
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.The security policy is the responsibility of an LSM. A basic system-wide policy is implemented with Yama and configurable through a sysctl.
If you're depending on the script interpreter to flag that the user may execute code, this seems to be equivalent in security terms to depending on the user. e.g. what if the user uses ptrace and clears O_MAYEXEC?
Signature · 2 lines
James Morris <[email protected]>
* James Morris:
Quoted 3 lines
If you're depending on the script interpreter to flag that the user may execute code, this seems to be equivalent in security terms to depending on the user. e.g. what if the user uses ptrace and clears
O_MAYEXEC?
The argument I've heard is this: Using ptrace (and adding the +x attribute) are auditable events.
Florian
James Morris
maintainerQuoted 8 lines · Florian Weimer
* James Morris:
Quoted 3 lines
If you're depending on the script interpreter to flag that the user may execute code, this seems to be equivalent in security terms to depending on the user. e.g. what if the user uses ptrace and clears
O_MAYEXEC?The argument I've heard is this: Using ptrace (and adding the +x attribute) are auditable events.
I guess you could also preload a modified libc which strips the flag.
Signature · 2 lines
James Morris <[email protected]>
* James Morris:
Quoted 12 lines · Florian Weimer
Quoted 9 lines
* James Morris:
Quoted 3 lines
If you're depending on the script interpreter to flag that the user may execute code, this seems to be equivalent in security terms to depending on the user. e.g. what if the user uses ptrace and clears
O_MAYEXEC?The argument I've heard is this: Using ptrace (and adding the +x attribute) are auditable events.
I guess you could also preload a modified libc which strips the flag.
My understanding is that this new libc would have to come somewhere, and making it executable would be an auditable even as well.
Thanks,
Florian
Quoted 9 lines · Florian Weimer
* James Morris:
Quoted 7 lines · Florian Weimer
Quoted 6 lines
* James Morris:
Quoted 3 lines
If you're depending on the script interpreter to flag that the user may execute code, this seems to be equivalent in security terms to depending on the user. e.g. what if the user uses ptrace and clears
O_MAYEXEC?
This security mechanism makes sense in an hardened system where the user is not allowed to import and execute new file (write xor execute policy). This can be enforced with appropriate mount points a more advanced access control policy.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
The argument I've heard is this: Using ptrace (and adding the +x attribute) are auditable events.
I guess you could also preload a modified libc which strips the flag.
My understanding is that this new libc would have to come somewhere, and making it executable would be an auditable even as well.
Auditing is a possible use case as well, but the W^X idea is to deny use of libraries which are not in an executable mount point, i.e. only execute trusted code.
Quoted 5 lines · Mickaël Salaün
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.
I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
int verify_for_exec(int fd)
instead of adding an O_MAYEXEC.
* Matthew Wilcox:
Quoted 13 lines · Mickaël Salaün
Quoted 5 lines
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
Quoted 3 lines
int
verify_for_exec(int fd)instead of adding an
O_MAYEXEC.
Will this work for auditing?
Maybe add an interface which explicitly upgrades O_PATH descriptors, and give that a separate flag argument? I suppose that would be more friendly to auditing.
Thanks,
Florian
Mimi Zohar
maintainerQuoted 13 lines · Matthew Wilcox
Quoted 5 lines · Mickaël Salaün
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
Quoted 3 lines
int
verify_for_exec(int fd)instead of adding an
O_MAYEXEC.
The indication needs to be set during file open, before the open returns to the caller. This is the point where ima_file_check() verifies the file's signature. On failure, access to the file is denied.
Mimi
* Mimi Zohar:
Quoted 4 lines
The indication needs to be set during file open, before the open returns to the caller. This is the point where
ima_file_check()verifies the file's signature. On failure, access to the file is denied.
Does this verification happen for open with O_PATH?
Thanks,
Florian
Mimi Zohar
maintainer[Cc'ing linux-integrity]
Quoted 8 lines · Florian Weimer
* Mimi Zohar:
Quoted 4 lines
The indication needs to be set during file open, before the open returns to the caller. This is the point where
ima_file_check()verifies the file's signature. On failure, access to the file is denied.Does this verification happen for open with
O_PATH?
Interesting! According to the manpage, userspace cannot read/write to the file. It looks like do_o_path() intentionally skips do_last(), with the call to ima_file_check(). If the file data isn't being accessed, does the file's integrity need to be verified?
Mimi
Quoted 6 lines · Mimi Zohar
Quoted 6 lines
I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
Quoted 8 lines
Quoted 3 lines
int
verify_for_exec(int fd)instead of adding an
O_MAYEXEC.The indication needs to be set during file open, before the open returns to the caller. This is the point where
ima_file_check()verifies the file's signature. On failure, access to the file is denied.
I understand that's what happens today, but do we need to do it that way? There's no harm in the interpreter having an fd to a file if it knows not to execute it. This is different from a program opening a file and having the LSM deny access to it because it violates the security model.
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Mickaël Salaün
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
Quoted 4 lines
int
verify_for_exec(int fd)instead of adding an
O_MAYEXEC.
Adding a new syscall for this simple use case seems excessive. I think that the open/openat syscall familly are the right place to do an atomic open and permission check, the same way the kernel does for other file access. Moreover, it will be easier to patch upstream interpreters without the burden of handling a (new) syscall that may not exist on the running system, whereas unknown open flags are ignored.
Quoted 14 lines · Mickaël Salaün
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Mickaël Salaün
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
Quoted 5 lines
Quoted 3 lines
int
verify_for_exec(int fd)instead of adding an
O_MAYEXEC.Adding a new syscall for this simple use case seems excessive. I think
We have somewhat less than 400 syscalls today. We have 20 O_ bits defined. Obviously there's a lower practical limit on syscalls, but in principle we could have up to 2^32 syscalls, and there are only 12 O_ bits remaining.
Quoted 5 lines
that the open/openat syscall familly are the right place to do an atomic open and permission check, the same way the kernel does for other file access. Moreover, it will be easier to patch upstream interpreters without the burden of handling a (new) syscall that may not exist on the running system, whereas unknown open flags are ignored.
Ah, but that's the problem. The interpreter can see an -ENOSYS response and handle it appropriately. If the flag is silently ignored, the interpreter has no idea whether it can do a racy check or whether to skip even trying to do the check.
Quoted 31 lines · Matthew Wilcox
Quoted 14 lines · Mickaël Salaün
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Mickaël Salaün
The goal of this patch series is to control script interpretation. A new
O_MAYEXECflag used bysys_open()is added to enable userland script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret scripts or other files containing what can be seen as commands.I don't have a problem with the concept, but we're running low on O_ bits. Does this have to be done before the process gets a file descriptor, or could we have a new syscall? Since we're going to be changing the interpreters anyway, it doesn't seem like too much of an imposition to ask them to use:
Quoted 5 lines
Quoted 3 lines
int
verify_for_exec(int fd)instead of adding an
O_MAYEXEC.Adding a new syscall for this simple use case seems excessive. I think
We have somewhat less than 400 syscalls today. We have 20 O_ bits defined. Obviously there's a lower practical limit on syscalls, but in principle we could have up to 2^32 syscalls, and there are only 12 O_ bits remaining.
Quoted 5 lines
that the open/openat syscall familly are the right place to do an atomic open and permission check, the same way the kernel does for other file access. Moreover, it will be easier to patch upstream interpreters without the burden of handling a (new) syscall that may not exist on the running system, whereas unknown open flags are ignored.
Quoted 4 lines
Ah, but that's the problem. The interpreter can see an
-ENOSYSresponse and handle it appropriately. If the flag is silently ignored, the interpreter has no idea whether it can do a racy check or whether to skip even trying to do the check.
Right, but the interpreter should interpret the script if the open with O_MAYEXEC succeed (but not otherwise): it may be because the flag is known by the kernel and the system policy allow this call, or because the (old) kernel doesn't known about this flag (which is fine and needed for backward compatibility). The script interpretation must not failed if the kernel doesn't support O_MAYEXEC, it is then useless for the interpreter to do any additional check.
Quoted 19 lines · Mickaël Salaün
Quoted 13 lines · Matthew Wilcox
Quoted 1 line · Mickaël Salaün
Adding a new syscall for this simple use case seems excessive. I think
We have somewhat less than 400 syscalls today. We have 20 O_ bits defined. Obviously there's a lower practical limit on syscalls, but in principle we could have up to 2^32 syscalls, and there are only 12 O_ bits remaining.
Quoted 5 lines
that the open/openat syscall familly are the right place to do an atomic open and permission check, the same way the kernel does for other file access. Moreover, it will be easier to patch upstream interpreters without the burden of handling a (new) syscall that may not exist on the running system, whereas unknown open flags are ignored.
Quoted 4 lines
Ah, but that's the problem. The interpreter can see an
-ENOSYSresponse and handle it appropriately. If the flag is silently ignored, the interpreter has no idea whether it can do a racy check or whether to skip even trying to do the check.
Quoted 7 lines
Right, but the interpreter should interpret the script if the open with
O_MAYEXECsucceed (but not otherwise): it may be because the flag is known by the kernel and the system policy allow this call, or because the (old) kernel doesn't known about this flag (which is fine and needed for backward compatibility). The script interpretation must not failed if the kernel doesn't supportO_MAYEXEC, it is then useless for the interpreter to do any additional check.
If that's the way interpreters want to work, then that's fine. They can just call the verify() syscall and ignore the -ENOSYS. Done.
Or somebody who cares very, very deeply can change the interpreter to decline to run any scripts if the kernel returns -ENOSYS.