Add support for O_MAYEXEC
Subsystems (10)
v2 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 userspace script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret/execute scripts or other files containing what can be seen as commands.
This second series mainly differ from the previous one [1] by moving the basic security policy from Yama to the filesystem subsystem. This policy can be enforced by the system administrator through a sysctl configuration consistent with the mount points.
Furthermore, the security policy can also be delegated to an LSM, either a MAC system or an integrity system. For instance, the new kernel MAY_OPENEXEC flag closes a major IMA measurement/appraisal interpreter integrity gap by bringing the ability to check the use of scripts [2]. Other uses are expected, such as for openat2(2) [3], SGX integration [4], and bpffs [5].
Userspace need to adapt to take advantage of this new feature. For example, the PEP 578 [6] (Runtime Audit Hooks) enables Python 3.8 to be extended with policy enforcement points related to code interpretation, which can be used to align with the PowerShell audit features. Additional Python security improvements (e.g. a limited interpreter withou -c, stdin piping of code) are on their way.
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 v5.3-rc7. This can be tested with CONFIG_SYSCTL. I would really appreciate constructive comments on this patch series.
# Changes since v1
* move code from Yama to the FS subsystem
* set __FMODE_EXEC when using O_MAYEXEC to make this information available through the new fanotify/FAN_OPEN_EXEC event
* only match regular files (not directories nor other types), which follows the same semantic as commit 73601ea5b7b1 ("fs/open.c: allow opening only regular files during execve()")
* improve tests
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/CALCETrVovr8XNZSroey7pHF46O=kj_c5D9K8h=z2T_cNrpvMig@mail.gmail.com/
[5] https://lore.kernel.org/lkml/CALCETrVeZ0eufFXwfhtaG_j+AdvbzEWE0M3wjXMWVEO7pj+xkw@mail.gmail.com/
[6] https://www.python.org/dev/peps/pep-0578/
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
fs: Enable to enforce noexec mounts or file exec through O_MAYEXEC selftest/exec: Add tests for O_MAYEXEC enforcing
doc: Add documentation for the fs.open_mayexec_enforce sysctl
Documentation/admin-guide/sysctl/fs.rst | 43 +++ fs/fcntl.c | 2 +- fs/namei.c | 70 +++++ fs/open.c | 6 + include/linux/fcntl.h | 2 +- include/linux/fs.h | 7 + include/uapi/asm-generic/fcntl.h | 3 + kernel/sysctl.c | 7 + tools/testing/selftests/exec/.gitignore | 1 + tools/testing/selftests/exec/Makefile | 4 +- tools/testing/selftests/exec/omayexec.c | 317 ++++++++++++++++++++ tools/testing/selftests/kselftest_harness.h | 3 + 12 files changed, 462 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/exec/omayexec.c
Signature · 1 line
2.23.0
PATCH v2 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
Changes since v1:
* set __FMODE_EXEC when using O_MAYEXEC to make this information available through the new fanotify/FAN_OPEN_EXEC event (suggested by Jan Kara and Matthew Bobrowski)
--- fs/fcntl.c | 2 +- fs/open.c | 6 ++++++ include/linux/fcntl.h | 2 +- include/linux/fs.h | 2 ++ include/uapi/asm-generic/fcntl.h | 3 +++ 5 files changed, 13 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 +6 -0
@@ -989,6 +989,12 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o acc_mode =0 ; } +/* Check execution permissions on open. */ +if (flags &O_MAYEXEC ) { + acc_mode |=MAY_OPENEXEC ; + flags |= __FMODE_EXEC; + } + op->open_flag = flags;/* O_TRUNC implies we need access checks for write permissions */
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 () (!IS_ENABLED(CONFIG_ARCH_32BIT_OFF_T))
include/linux/fs.h +2 -0
@@ -99,6 +99,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.23.0
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with O_MAYEXEC, and if that fails with EINVAL, try again without O_MAYEXEC?
Or do I risk disabling this security feature if I do that?
Do we need a different way for recognizing kernel support. (Note that we cannot probe paths in /proc for various reasons.)
Thanks,
Florian
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use O_MAYEXEC.
Quoted 2 lines
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
Quoted 3 lines
Do we need a different way for recognizing kernel support. (Note that we cannot probe paths in /proc for various reasons.)
There is no need to probe for kernel support.
Quoted 4 lines
Thanks,
Florian
--
Mickaël Salaün
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.
Jeff Layton
maintainerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 10 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.
Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
Maybe this should be a new flag that is only usable in the new openat2() syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.
Anyone that wants to use this will have to recompile anyway. If the kernel doesn't support openat2 or if the flag is rejected then you know that you have no O_MAYEXEC support and can decide what to do.
Quoted 5 lines
Quoted 1 line
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
With a security feature, I think we really want userland to aware of whether it works.
Quoted 5 lines
Quoted 2 lines
Do we need a different way for recognizing kernel support. (Note that we cannot probe paths in /proc for various reasons.)
There is no need to probe for kernel support.
Quoted 8 lines
Quoted 3 lines
Thanks,
Florian--
Mickaël SalaünLes 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 da
ta, please contact: [email protected]. If you have received this message in error, we thank you for informing the sender and destroying the message.
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 8 lines · Jeff Layton
Quoted 7 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?
Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair, O_TMPFILE actually does have a work-around with the O_DIRECTORY mask setup.
The openat2() set adds O_EMPTYPATH -- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) while O_EMPTYPATH is a no-op non-empty strings.
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.
I'm also interested in whether we could add an UPGRADE_NOEXEC flag to how->upgrade_mask for the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Jeff Layton
maintainerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 26 lines · Aleksa Sarai
Quoted 8 lines · Jeff Layton
Quoted 7 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.
Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).
I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via O_MAYEXEC (or some similar openat2 flag) if it's needed. Then you could add an UPGRADE_EXEC flag instead?
That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
Signature · 1 line
Jeff Layton <[email protected]>
Andy Lutomirski
reviewerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 41 lines · Jeff Layton
Quoted 28 lines · Aleksa Sarai
Quoted 9 lines · Jeff Layton
Quoted 8 lines · Mickaël Salaün
Quoted 6 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).
Quoted 9 lines
I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via
O_MAYEXEC(or some similar openat2 flag) if it's needed. Then you could add anUPGRADE_EXECflag instead?That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
There are at least four concepts of executability here:
- Just check the file mode and any other relevant permissions. Return a normal fd. Makes sense for script interpreters, perhaps.
- Make the fd fexecve-able.
- Make the resulting fd mappable PROT_EXEC.
- Make the resulting fd upgradable.
I’m not at all convinced that the kernel needs to distinguish all these, but at least upgradability should be its own thing IMO.
Jeff Layton
maintainerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 53 lines · Andy Lutomirski
Quoted 39 lines · Jeff Layton
Quoted 28 lines · Aleksa Sarai
Quoted 9 lines · Jeff Layton
Quoted 8 lines · Mickaël Salaün
Quoted 6 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).Quoted 9 lines
I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via
O_MAYEXEC(or some similar openat2 flag) if it's needed. Then you could add anUPGRADE_EXECflag instead?That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
There are at least four concepts of executability here:
- Just check the file mode and any other relevant permissions. Return a normal fd. Makes sense for script interpreters, perhaps.
Quoted 7 lines
- Make the fd fexecve-able.
- Make the resulting fd mappable
PROT_EXEC.- Make the resulting fd upgradable.
I’m not at all convinced that the kernel needs to distinguish all these, but at least upgradability should be its own thing IMO.
Good point. Upgradability is definitely orthogonal, though the idea there is to alter the default behavior. If the default is NOEXEC then UPGRADE_EXEC would make sense.
In any case, I was mostly thinking about the middle two in your list above. After more careful reading of the patches, I now get get that Mickaël is more interested in the first, and that's really a different sort of use-case.
Most opens never result in the fd being fed to fexecve or mmapped with PROT_EXEC, so having userland explicitly opt-in to allowing that during the open sounds like a reasonable thing to do.
But I get that preventing execution via script interpreters of files that are not executable might be something nice to have.
Perhaps we need two flags for openat2?
OA2_MAYEXEC : test that permissions allow execution and that the file doesn't reside on a noexec mount before allowing the open
OA2_EXECABLE : only allow fexecve or mmapping with PROT_EXEC if the fd was opened with this
Signature · 1 line
Jeff Layton <[email protected]>
Andy Lutomirski
reviewerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 22 lines · Jeff Layton
Quoted 1 line · Andy Lutomirski
I’m not at all convinced that the kernel needs to distinguish all these, but at least upgradability should be its own thing IMO.
Good point. Upgradability is definitely orthogonal, though the idea there is to alter the default behavior. If the default is NOEXEC then
UPGRADE_EXECwould make sense.In any case, I was mostly thinking about the middle two in your list above. After more careful reading of the patches, I now get get that Mickaël is more interested in the first, and that's really a different sort of use-case.
Most opens never result in the fd being fed to fexecve or mmapped with
PROT_EXEC, so having userland explicitly opt-in to allowing that during the open sounds like a reasonable thing to do.But I get that preventing execution via script interpreters of files that are not executable might be something nice to have.
Quoted 10 lines
Perhaps we need two flags for openat2?
OA2_MAYEXEC: test that permissions allow execution and that the file doesn't reside on a noexec mount before allowing the open
OA2_EXECABLE: only allow fexecve or mmapping withPROT_EXECif the fd was opened with this
We could go one step farther and have three masks: check_perms, fd_perms, and upgrade_perms. check_perms says “fail if I don’t have these perms”. fd_perms is the permissions on the returned fd, and upgrade_perms is the upgrade mask. (fd_perms & ~check_perms) != 0 is an error. This makes it possible to say "I want to make sure the file is writable, but I don't actually want to write to it", which could plausibly be useful.
I would argue that these things should have new, sane bits, e.g. FILE_READ, FILE_WRITE, and FILE_EXECUTE (or maybe FILE_MAP_EXEC and FILE_EXECVE). And maybe there should be at least 16 bits for each mask reserved. Windows has a lot more mode bits than Linux, and it's not entirely nuts. We do *not* need any direct equivalent of O_RDWR for openat2().
--Andy
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jeff LaytonQuoted 71 lines · Jeff Layton
Quoted 53 lines · Andy Lutomirski
Quoted 39 lines · Jeff Layton
Quoted 28 lines · Aleksa Sarai
Quoted 9 lines · Jeff Layton
Quoted 8 lines · Mickaël Salaün
Quoted 6 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).Quoted 9 lines
I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via
O_MAYEXEC(or some similar openat2 flag) if it's needed. Then you could add anUPGRADE_EXECflag instead?That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
There are at least four concepts of executability here:
- Just check the file mode and any other relevant permissions. Return a normal fd. Makes sense for script interpreters, perhaps.
Quoted 7 lines
- Make the fd fexecve-able.
- Make the resulting fd mappable
PROT_EXEC.- Make the resulting fd upgradable.
I’m not at all convinced that the kernel needs to distinguish all these, but at least upgradability should be its own thing IMO.
Good point. Upgradability is definitely orthogonal, though the idea there is to alter the default behavior. If the default is NOEXEC then
UPGRADE_EXECwould make sense.In any case, I was mostly thinking about the middle two in your list above. After more careful reading of the patches, I now get get that Mickaël is more interested in the first, and that's really a different sort of use-case.
Quoted 6 lines
Most opens never result in the fd being fed to fexecve or mmapped with
PROT_EXEC, so having userland explicitly opt-in to allowing that during the open sounds like a reasonable thing to do.But I get that preventing execution via script interpreters of files that are not executable might be something nice to have.
My first glance at the patch lead me to believe that this was about blocking at fexecve()-time (which was what my first attempt at this problem looked like) -- hence why I mentioned the upgrade_mask stuff (because of the dances you can do with O_PATH, if blocking at fexecve()-time was the goal then you seriously do need the upgrade_mask and "O_PATH mask" in order for it to be even slightly secure).
But I also agree this is useful, and we can always add FMODE_EXEC, FMODE_MAP_EXEC, and FMODE_UPGRADE_EXEC (and the related bits) at a later date.
Quoted 7 lines
Perhaps we need two flags for openat2?
OA2_MAYEXEC: test that permissions allow execution and that the file doesn't reside on a noexec mount before allowing the open
OA2_EXECABLE: only allow fexecve or mmapping withPROT_EXECif the fd was opened with this
That seems reasonable to me. The only thing is that there currently isn't any code to restrict fexecve() or PROT_EXEC in that fashion (doubly so when you consider binfmt_script). So if we want to make certain things default behaviour (such as disallowing exec by default) we'd need to get the PROT_EXEC restriction work done first.
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Andy LutomirskiQuoted 46 lines · Andy Lutomirski
Quoted 46 lines · Jeff Layton
Quoted 28 lines · Aleksa Sarai
Quoted 9 lines · Jeff Layton
Quoted 8 lines · Mickaël Salaün
Quoted 6 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via
O_MAYEXEC(or some similar openat2 flag) if it's needed. Then you could add anUPGRADE_EXECflag instead?
Quoted 8 lines
Quoted 4 lines
That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
There are at least four concepts of executability here:
- Just check the file mode and any other relevant permissions. Return a normal fd. Makes sense for script interpreters, perhaps.
This is the purpose of this patch series. It doesn't make sense to add memory restrictions nor constrain fexecve and such.
Quoted 9 lines
- Make the fd fexecve-able.
- Make the resulting fd mappable
PROT_EXEC.- Make the resulting fd upgradable.
I’m not at all convinced that the kernel needs to distinguish all these, but at least upgradability should be its own thing IMO.
--
Mickaël Salaün
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: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jeff LaytonQuoted 37 lines · Jeff Layton
Quoted 26 lines · Aleksa Sarai
Quoted 8 lines · Jeff Layton
Quoted 7 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).
Quoted 7 lines
I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via
O_MAYEXEC(or some similar openat2 flag) if it's needed. Then you could add anUPGRADE_EXECflag instead?That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
In that case, maybe openat2(2) should default to not allowing any upgrades by default? The reason I pitched UPGRADE_NOEXEC is because UPGRADE_NO{READ,WRITE} are the existing @how->upgrade_mask flags.
However, I just noticed something else about this series -- if you do O_PATH|O_MAYEXEC the new flag gets ignored. Given that you can do fexecve(2) on an O_PATH (and O_PATHs have some other benefits), is this something that we'd want to have?
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 43 lines · Aleksa Sarai
Quoted 42 lines · Jeff Layton
Quoted 26 lines · Aleksa Sarai
Quoted 8 lines · Jeff Layton
Quoted 7 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
It should be noted that this has been a valid concern for every new O_* flag introduced (and yet we still introduced new flags, despite the concern) -- though to be fair,
O_TMPFILEactually does have a work-around with theO_DIRECTORYmask setup.The
openat2()set addsO_EMPTYPATH-- though in fairness it's also backwards compatible because empty path strings have always given ENOENT (or EINVAL?) whileO_EMPTYPATHis a no-op non-empty strings.Quoted 9 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.I'm also interested in whether we could add an
UPGRADE_NOEXECflag tohow->upgrade_maskfor the openat2(2) patchset (I reserved a flag bit for it, since I'd heard about this work through the grape-vine).I rather like the idea of having openat2 fds be non-executable by default, and having userland request it specifically via
O_MAYEXEC(or some similar openat2 flag) if it's needed. Then you could add anUPGRADE_EXECflag instead?
Quoted 6 lines
Quoted 2 lines
That seems like something reasonable to do with a brand new API, and might be very helpful for preventing certain classes of attacks.
In that case, maybe openat2(2) should default to not allowing any upgrades by default? The reason I pitched
UPGRADE_NOEXECis because UPGRADE_NO{READ,WRITE} are the existing @how->upgrade_mask flags.
Sorry, another issue is that there isn't a current way to really restrict fexecve() permissions (from my [limited] understanding, __FMODE_EXEC isn't the right thing to use) -- so we can't blanket block exec through openat2() O_PATH descriptors and add UPGRADE_EXEC later.
We would have to implement FMODE_EXEC (and FMODE_MAP_EXEC as you suggested) in order to implement FMODE_UPGRADE_EXEC before we could even get a first version of openat2(2) in. Though, I do (a little begrudgingly) agree that we should have a safe default if possible (magical O_PATH reopening trickery is something that most people don't know about and probably wouldn't want to happen if they did).
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jeff LaytonQuoted 8 lines · Jeff Layton
Quoted 7 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?
Quoted 7 lines
Quoted 3 lines
The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
Well, I don't think we should nor could care about bogus programs that do not conform to the Linux ABI.
Quoted 5 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.
Here is a thread about a new syscall:
https://lore.kernel.org/lkml/[email protected]/
I don't think it fit well with auditing nor integrity. Moreover using the current open(2) behavior of ignoring unknown flags fit well with the usage of O_MAYEXEC (because it is only a hint to the kernel about the use of the *opened* file).
Quoted 4 lines
Anyone that wants to use this will have to recompile anyway. If the kernel doesn't support openat2 or if the flag is rejected then you know that you have no
O_MAYEXECsupport and can decide what to do.
If we want to enforce a security policy, we need to either be the system administrator or the distro developer. If a distro ship interpreters using this flag, we don't need to recompile anything, but we need to be able to control the enforcement according to the mount point configuration (or an advanced MAC, or an IMA config). I don't see why an userspace process should check if this flag is supported or not, it should simply use it, and the sysadmin will enable an enforcement if it makes sense for the whole system.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
With a security feature, I think we really want userland to aware of whether it works.
If userland would like to enforce something, it can already do it without any kernel modification. The goal of the O_MAYEXEC flag is to enable the kernel, hence sysadmins or system designers, to enforce a global security policy that makes sense.
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines
Do we need a different way for recognizing kernel support. (Note that we cannot probe paths in /proc for various reasons.)
There is no need to probe for kernel support.
Quoted 3 lines
Thanks,
Florian
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.
Jeff Layton
maintainerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 13 lines · Mickaël Salaün
Quoted 12 lines · Jeff Layton
Quoted 10 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.
Quoted 7 lines
Quoted 3 lines
Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
Well, I don't think we should nor could care about bogus programs that do not conform to the Linux ABI.
But they do conform. The ABI is just undefined here. Unknown flags are ignored so we never really know if $random_program may be setting them.
Quoted 5 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.
Quoted 8 lines
Here is a thread about a new syscall:
https://lore.kernel.org/lkml/[email protected]/I don't think it fit well with auditing nor integrity. Moreover using the current open(2) behavior of ignoring unknown flags fit well with the usage of
O_MAYEXEC(because it is only a hint to the kernel about the use of the *opened* file).
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Quoted 4 lines
Quoted 3 lines
Anyone that wants to use this will have to recompile anyway. If the kernel doesn't support openat2 or if the flag is rejected then you know that you have no
O_MAYEXECsupport and can decide what to do.
Quoted 9 lines
If we want to enforce a security policy, we need to either be the system administrator or the distro developer. If a distro ship interpreters using this flag, we don't need to recompile anything, but we need to be able to control the enforcement according to the mount point configuration (or an advanced MAC, or an IMA config). I don't see why an userspace process should check if this flag is supported or not, it should simply use it, and the sysadmin will enable an enforcement if it makes sense for the whole system.
A userland program may need to do other risk mitigation if it sets O_MAYEXEC and the kernel doesn't recognize it.
Personally, here's what I'd suggest:
- Base this on top of the openat2 set
- Change it that so that openat2() files are non-executable by default. Anyone wanting to do that needs to set O_MAYEXEC or upgrade the fd somehow.
- Only have the openat2 syscall pay attention to O_MAYEXEC. Let open and openat continue ignoring the new flag.
That works around a whole pile of potential ABI headaches. Note that we'd need to make that decision before the openat2 patches are merged.
Even better would be to declare the new flag in some openat2-only flag space, so there's no confusion about it being supported by legacy open calls.
If glibc wants to implement an open -> openat2 wrapper in userland later, it can set that flag in the wrapper implicitly to emulate the old behavior.
Given that you're going to have to recompile software to take advantage of this anyway, what's the benefit to changing legacy syscalls?
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
Quoted 8 lines
Quoted 2 lines
With a security feature, I think we really want userland to aware of whether it works.
If userland would like to enforce something, it can already do it without any kernel modification. The goal of the
O_MAYEXECflag is to enable the kernel, hence sysadmins or system designers, to enforce a global security policy that makes sense.
I don't see how this helps anything if you can't tell whether the kernel recognizes the damned thing. Also, our track record with global sysctl switches like this is pretty poor. They're an administrative headache as well as a potential attack vector.
I think your idea is a good one, but it could stand to have fewer moving parts.
Signature · 1 line
Jeff Layton <[email protected]>
Andy Lutomirski
reviewerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 104 lines · Jeff Layton
Quoted 15 lines · Mickaël Salaün
Quoted 13 lines · Jeff Layton
Quoted 11 lines · Mickaël Salaün
Quoted 6 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Quoted 7 lines
Quoted 3 lines
Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
Well, I don't think we should nor could care about bogus programs that do not conform to the Linux ABI.
But they do conform. The ABI is just undefined here. Unknown flags are ignored so we never really know if $random_program may be setting them.
Quoted 5 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.Quoted 8 lines
Here is a thread about a new syscall:
https://lore.kernel.org/lkml/[email protected]/I don't think it fit well with auditing nor integrity. Moreover using the current open(2) behavior of ignoring unknown flags fit well with the usage of
O_MAYEXEC(because it is only a hint to the kernel about the use of the *opened* file).The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Quoted 4 lines
Quoted 3 lines
Anyone that wants to use this will have to recompile anyway. If the kernel doesn't support openat2 or if the flag is rejected then you know that you have no
O_MAYEXECsupport and can decide what to do.Quoted 9 lines
If we want to enforce a security policy, we need to either be the system administrator or the distro developer. If a distro ship interpreters using this flag, we don't need to recompile anything, but we need to be able to control the enforcement according to the mount point configuration (or an advanced MAC, or an IMA config). I don't see why an userspace process should check if this flag is supported or not, it should simply use it, and the sysadmin will enable an enforcement if it makes sense for the whole system.
A userland program may need to do other risk mitigation if it sets
O_MAYEXECand the kernel doesn't recognize it.Personally, here's what I'd suggest:
- Base this on top of the openat2 set
- Change it that so thatopenat2()files are non-executable by default. Anyone wanting to do that needs to setO_MAYEXECor upgrade the fd somehow.
- Only have the openat2 syscall pay attention toO_MAYEXEC. Let open and openat continue ignoring the new flag.That works around a whole pile of potential ABI headaches. Note that we'd need to make that decision before the openat2 patches are merged.
Even better would be to declare the new flag in some openat2-only flag space, so there's no confusion about it being supported by legacy open calls.
If glibc wants to implement an open -> openat2 wrapper in userland later, it can set that flag in the wrapper implicitly to emulate the old behavior.
Given that you're going to have to recompile software to take advantage of this anyway, what's the benefit to changing legacy syscalls?
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
Quoted 8 lines
Quoted 2 lines
With a security feature, I think we really want userland to aware of whether it works.
If userland would like to enforce something, it can already do it without any kernel modification. The goal of the
O_MAYEXECflag is to enable the kernel, hence sysadmins or system designers, to enforce a global security policy that makes sense.
Quoted 4 lines
I don't see how this helps anything if you can't tell whether the kernel recognizes the damned thing. Also, our track record with global sysctl switches like this is pretty poor. They're an administrative headache as well as a potential attack vector.
I tend to agree. The sysctl seems like it’s asking for trouble. I can see an ld.so.conf option to turn this thing off making sense.
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 104 lines · Andy Lutomirski
Quoted 104 lines · Jeff Layton
Quoted 15 lines · Mickaël Salaün
Quoted 13 lines · Jeff Layton
Quoted 11 lines · Mickaël Salaün
Quoted 6 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Quoted 7 lines
Quoted 3 lines
Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
Well, I don't think we should nor could care about bogus programs that do not conform to the Linux ABI.
But they do conform. The ABI is just undefined here. Unknown flags are ignored so we never really know if $random_program may be setting them.
Quoted 5 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.Quoted 8 lines
Here is a thread about a new syscall:
https://lore.kernel.org/lkml/[email protected]/I don't think it fit well with auditing nor integrity. Moreover using the current open(2) behavior of ignoring unknown flags fit well with the usage of
O_MAYEXEC(because it is only a hint to the kernel about the use of the *opened* file).The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Quoted 4 lines
Quoted 3 lines
Anyone that wants to use this will have to recompile anyway. If the kernel doesn't support openat2 or if the flag is rejected then you know that you have no
O_MAYEXECsupport and can decide what to do.Quoted 9 lines
If we want to enforce a security policy, we need to either be the system administrator or the distro developer. If a distro ship interpreters using this flag, we don't need to recompile anything, but we need to be able to control the enforcement according to the mount point configuration (or an advanced MAC, or an IMA config). I don't see why an userspace process should check if this flag is supported or not, it should simply use it, and the sysadmin will enable an enforcement if it makes sense for the whole system.
A userland program may need to do other risk mitigation if it sets
O_MAYEXECand the kernel doesn't recognize it.Personally, here's what I'd suggest:
- Base this on top of the openat2 set
- Change it that so thatopenat2()files are non-executable by default. Anyone wanting to do that needs to setO_MAYEXECor upgrade the fd somehow.
- Only have the openat2 syscall pay attention toO_MAYEXEC. Let open and openat continue ignoring the new flag.That works around a whole pile of potential ABI headaches. Note that we'd need to make that decision before the openat2 patches are merged.
Even better would be to declare the new flag in some openat2-only flag space, so there's no confusion about it being supported by legacy open calls.
If glibc wants to implement an open -> openat2 wrapper in userland later, it can set that flag in the wrapper implicitly to emulate the old behavior.
Given that you're going to have to recompile software to take advantage of this anyway, what's the benefit to changing legacy syscalls?
Quoted 14 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
With a security feature, I think we really want userland to aware of whether it works.
If userland would like to enforce something, it can already do it without any kernel modification. The goal of the
O_MAYEXECflag is to enable the kernel, hence sysadmins or system designers, to enforce a global security policy that makes sense.
Quoted 6 lines
Quoted 4 lines
I don't see how this helps anything if you can't tell whether the kernel recognizes the damned thing. Also, our track record with global sysctl switches like this is pretty poor. They're an administrative headache as well as a potential attack vector.
I tend to agree. The sysctl seems like it’s asking for trouble. I can see an ld.so.conf option to turn this thing off making sense.
The sysctl is required to enable the adoption of this flag without breaking existing systems. Current systems may have "noexec" on mount points containing scripts. Without giving the ability to the sysadmin to control that behavior, updating to a newer version of an interpreter using O_MAYEXEC may break such systems.
How would you do this with ld.so.conf ?
--
Mickaël Salaün
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.
Andy Lutomirski
reviewerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 116 lines · Mickaël Salaün
Quoted 107 lines · Andy Lutomirski
Quoted 104 lines
Quoted 18 lines · Jeff Layton
Quoted 15 lines · Mickaël Salaün
Quoted 12 lines · Jeff Layton
Quoted 7 lines · Mickaël Salaün
On 06/09/2019 17:56, Florian Weimer wrote:
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Quoted 7 lines
Quoted 3 lines
Well...maybe. What about existing programs that are sending down bogus open flags? Once you turn this on, they may break...or provide a way to circumvent the protections this gives.
Well, I don't think we should nor could care about bogus programs that do not conform to the Linux ABI.
But they do conform. The ABI is just undefined here. Unknown flags are ignored so we never really know if $random_program may be setting them.
Quoted 5 lines
Quoted 4 lines
Maybe this should be a new flag that is only usable in the new
openat2()syscall that's still under discussion? That syscall will enforce that all flags are recognized. You presumably wouldn't need the sysctl if you went that route too.Quoted 8 lines
Here is a thread about a new syscall:
https://lore.kernel.org/lkml/[email protected]/I don't think it fit well with auditing nor integrity. Moreover using the current open(2) behavior of ignoring unknown flags fit well with the usage of
O_MAYEXEC(because it is only a hint to the kernel about the use of the *opened* file).The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Quoted 4 lines
Quoted 3 lines
Anyone that wants to use this will have to recompile anyway. If the kernel doesn't support openat2 or if the flag is rejected then you know that you have no
O_MAYEXECsupport and can decide what to do.Quoted 9 lines
If we want to enforce a security policy, we need to either be the system administrator or the distro developer. If a distro ship interpreters using this flag, we don't need to recompile anything, but we need to be able to control the enforcement according to the mount point configuration (or an advanced MAC, or an IMA config). I don't see why an userspace process should check if this flag is supported or not, it should simply use it, and the sysadmin will enable an enforcement if it makes sense for the whole system.
A userland program may need to do other risk mitigation if it sets
O_MAYEXECand the kernel doesn't recognize it.Personally, here's what I'd suggest:
- Base this on top of the openat2 set
- Change it that so thatopenat2()files are non-executable by default. Anyone wanting to do that needs to setO_MAYEXECor upgrade the fd somehow.
- Only have the openat2 syscall pay attention toO_MAYEXEC. Let open and openat continue ignoring the new flag.That works around a whole pile of potential ABI headaches. Note that we'd need to make that decision before the openat2 patches are merged.
Even better would be to declare the new flag in some openat2-only flag space, so there's no confusion about it being supported by legacy open calls.
If glibc wants to implement an open -> openat2 wrapper in userland later, it can set that flag in the wrapper implicitly to emulate the old behavior.
Given that you're going to have to recompile software to take advantage of this anyway, what's the benefit to changing legacy syscalls?
Quoted 14 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Or do I risk disabling this security feature if I do that?
It is only a security feature if the kernel support it, otherwise it is a no-op.
With a security feature, I think we really want userland to aware of whether it works.
If userland would like to enforce something, it can already do it without any kernel modification. The goal of the
O_MAYEXECflag is to enable the kernel, hence sysadmins or system designers, to enforce a global security policy that makes sense.Quoted 6 lines
Quoted 4 lines
I don't see how this helps anything if you can't tell whether the kernel recognizes the damned thing. Also, our track record with global sysctl switches like this is pretty poor. They're an administrative headache as well as a potential attack vector.
I tend to agree. The sysctl seems like it’s asking for trouble. I can see an ld.so.conf option to turn this thing off making sense.
Quoted 8 lines
The sysctl is required to enable the adoption of this flag without breaking existing systems. Current systems may have "noexec" on mount points containing scripts. Without giving the ability to the sysadmin to control that behavior, updating to a newer version of an interpreter using
O_MAYEXECmay break such systems.How would you do this with ld.so.conf ?
By telling user code not to use O_MAYEXEC?
Alternatively, you could allow O_MAYEXEC even on a noexec mount and have a strong_noexec option that blocks it.
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jeff Layton* Jeff Layton:
Quoted 3 lines
Even better would be to declare the new flag in some openat2-only flag space, so there's no confusion about it being supported by legacy open calls.
Isn't that desirable anyway because otherwise fcntl with F_GETFL will give really confusing results?
Quoted 3 lines
If glibc wants to implement an open -> openat2 wrapper in userland later, it can set that flag in the wrapper implicitly to emulate the old behavior.
I see us rather doing the opposite, i.e. implement openat2 with non-exotic flags using openat. But we've bitten by this in the past, so maybe that's not such a great idea. It's tempting to make the same mistake again for every new system call.
Thanks,
Florian
James Morris
maintainerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Jeff LaytonQuoted 8 lines · Jeff Layton
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Agree here. It's unfortunate but a reality and Linus will reject any such changes which break existing userspace.
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 9 lines · James Morris
Quoted 7 lines · Jeff Layton
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
Quoted 4 lines
Quoted 1 line
New open flags really have no place in the old syscalls, IMO.
Agree here. It's unfortunate but a reality and Linus will reject any such changes which break existing userspace.
Do you mean that adding new flags to open(2) is not possible?
Does it means that unspecified behaviors are definitely part of the Linux specification and can't be fixed?
As I said, O_MAYEXEC should be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).
--
Mickaël Salaün
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.
James Morris
maintainerRe: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 13 lines · Mickaël Salaün
Quoted 11 lines · James Morris
Quoted 8 lines · Jeff Layton
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Quoted 7 lines
Quoted 2 lines
Agree here. It's unfortunate but a reality and Linus will reject any such changes which break existing userspace.
Do you mean that adding new flags to open(2) is not possible?
Does it means that unspecified behaviors are definitely part of the Linux specification and can't be fixed?
This is my understanding.
Quoted 4 lines
As I said,
O_MAYEXECshould be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).
The problem here is programs which are already using the value of O_MAYEXEC, which will break. Hence, openat2(2).
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 26 lines · James Morris
Quoted 13 lines · Mickaël Salaün
Quoted 11 lines · James Morris
Quoted 8 lines · Jeff Layton
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
New open flags really have no place in the old syscalls, IMO.
Quoted 7 lines
Quoted 2 lines
Agree here. It's unfortunate but a reality and Linus will reject any such changes which break existing userspace.
Do you mean that adding new flags to open(2) is not possible?
Does it means that unspecified behaviors are definitely part of the Linux specification and can't be fixed?
This is my understanding.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
As I said,
O_MAYEXECshould be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).The problem here is programs which are already using the value of
O_MAYEXEC, which will break. Hence, openat2(2).
Well, it still depends on the sysctl, which doesn't enforce anything by default, hence doesn't break existing behavior, and this unused flags could be fixed/removed or reported by sysadmins or distro developers.
--
Mickaël Salaün
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: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 6 lines · Mickaël Salaün
Quoted 5 lines · James Morris
Quoted 3 lines · Mickaël Salaün
As I said,
O_MAYEXECshould be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).
Quoted 6 lines
Quoted 2 lines
The problem here is programs which are already using the value of
O_MAYEXEC, which will break. Hence, openat2(2).Well, it still depends on the sysctl, which doesn't enforce anything by default, hence doesn't break existing behavior, and this unused flags could be fixed/removed or reported by sysadmins or distro developers.
Okay, but then this means that new programs which really want to enforce O_MAYEXEC (and know that they really do want this feature) won't be able to unless an admin has set the relevant sysctl. Not to mention that the old-kernel fallback will not cover the "it's disabled by the sysctl" case -- so the fallback handling would need to be:
int fd = open("foo", O_MAYEXEC|O_RDONLY);
if (!(fcntl(fd, F_GETFL) & O_MAYEXEC))
fallback();
if (!sysctl_feature_is_enabled)
fallback();However, there is still a race here -- if an administrator enables O_MAYEXEC after the program gets the fd, then you still won't hit the fallback (and you can't tell that O_MAYEXEC checks weren't done).
You could fix the issue with the sysctl by clearing O_MAYEXEC from f_flags if the sysctl is disabled. You could also avoid some of the problems with it being a global setting by making it a prctl(2) which processes can opt-in to (though this has its own major problems).
Sorry, but I'm just really not a fan of this.
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 26 lines · Aleksa Sarai
Quoted 6 lines · Mickaël Salaün
Quoted 5 lines · James Morris
Quoted 3 lines · Mickaël Salaün
As I said,
O_MAYEXECshould be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).Quoted 6 lines
Quoted 2 lines
The problem here is programs which are already using the value of
O_MAYEXEC, which will break. Hence, openat2(2).Well, it still depends on the sysctl, which doesn't enforce anything by default, hence doesn't break existing behavior, and this unused flags could be fixed/removed or reported by sysadmins or distro developers.
Okay, but then this means that new programs which really want to enforce
O_MAYEXEC(and know that they really do want this feature) won't be able to unless an admin has set the relevant sysctl. Not to mention that the old-kernel fallback will not cover the "it's disabled by the sysctl" case -- so the fallback handling would need to be:int fd = open("foo", O_MAYEXEC|O_RDONLY); if (!(fcntl(fd, F_GETFL) & O_MAYEXEC)) fallback(); if (!sysctl_feature_is_enabled) fallback();
Quoted 3 lines
However, there is still a race here -- if an administrator enables
O_MAYEXECafter the program gets the fd, then you still won't hit the fallback (and you can't tell thatO_MAYEXECchecks weren't done).
I just replied to this concern here:
https://lore.kernel.org/lkml/[email protected]/
Quoted 5 lines
You could fix the issue with the sysctl by clearing
O_MAYEXECfromf_flagsif the sysctl is disabled. You could also avoid some of the problems with it being a global setting by making it a prctl(2) which processes can opt-in to (though this has its own major problems).
Security definition and enforcement should be manageable by sysadmins and distro developers.
Quoted 2 lines
Sorry, but I'm just really not a fan of this.
I guess there is some misunderstanding. I just replied to another thread and I think it should answer your concerns (especially about the PDP and PEP):
https://lore.kernel.org/lkml/[email protected]/
--
Mickaël Salaün
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: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Mickaël SalaünQuoted 10 lines · Mickaël Salaün
Quoted 9 lines · James Morris
Quoted 7 lines · Jeff Layton
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
New open flags really have no place in the old syscalls, IMO.
Agree here. It's unfortunate but a reality and Linus will reject any such changes which break existing userspace.
Do you mean that adding new flags to open(2) is not possible?
It is possible, as long as there is no case where a program that works today (and passes garbage to the unused bits in flags) works with the change.
O_TMPFILE was okay because it's actually two flags (one is O_DIRECTORY) and no working program does file IO to a directory (there are also some other tricky things done there, I'll admit I don't fully understand it).
O_EMPTYPATH works because it's a no-op with non-empty path strings, and empty path strings have always given an error (so no working program does it today).
However, O_MAYEXEC will result in programs that pass garbage bits to potentially get -EACCES that worked previously.
Quoted 3 lines
As I said,
O_MAYEXECshould be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).
NACK on having that behaviour with openat2(2). -EINVAL on unknown flags is how all other syscalls work (any new syscall proposed today that didn't do that would be rightly rejected), and is a quirk of open(2) which unfortunately cannot be fixed. The fact that *every new O_ flag needs to work around this problem* should be an indication that this interface mis-design should not be allowed to infect any more syscalls.
Note that this point is regardless of the fact that O_MAYEXEC is a *security* flag -- if userspace wants to have a secure fallback on old kernels (which is "the right thing" to do) they would have to do more work than necessary. And programs that don't care don't have to do anything special.
However with -EINVAL, the programs doing "the right thing" get an easy -EINVAL check. And programs that don't care can just un-set O_MAYEXEC and retry. You should be forced to deal with the case where a flag is not supported -- and this is doubly true of security flags!
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 37 lines · Aleksa Sarai
Quoted 10 lines · Mickaël Salaün
Quoted 9 lines · James Morris
Quoted 7 lines · Jeff Layton
The fact that open and openat didn't vet unknown flags is really a bug.
Too late to fix it now, of course, and as Aleksa points out, we've worked around that in the past. Now though, we have a new openat2 syscall on the horizon. There's little need to continue these sorts of hacks.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
New open flags really have no place in the old syscalls, IMO.
Agree here. It's unfortunate but a reality and Linus will reject any such changes which break existing userspace.
Do you mean that adding new flags to open(2) is not possible?
It is possible, as long as there is no case where a program that works today (and passes garbage to the unused bits in flags) works with the change.
O_TMPFILEwas okay because it's actually two flags (one isO_DIRECTORY) and no working program does file IO to a directory (there are also some other tricky things done there, I'll admit I don't fully understand it).
O_EMPTYPATHworks because it's a no-op with non-empty path strings, and empty path strings have always given an error (so no working program does it today).However,
O_MAYEXECwill result in programs that pass garbage bits to potentially get-EACCESthat worked previously.Quoted 3 lines
As I said,
O_MAYEXECshould be ignored if it is not supported by the kernel, which perfectly fit with the current open(2) flags behavior, and should also behave the same with openat2(2).
Quoted 6 lines
NACK on having that behaviour with openat2(2).
-EINVALon unknown flags is how all other syscalls work (any new syscall proposed today that didn't do that would be rightly rejected), and is a quirk of open(2) which unfortunately cannot be fixed. The fact that *every new O_ flag needs to work around this problem* should be an indication that this interface mis-design should not be allowed to infect any more syscalls.
It's definitely OK (and a sane interface) to always return -EINVAL for unknown flags with openat2(2) (and other new syscalls). With openat2(2), userland need to handle the case where some flags may be unknown to the kernel (and handling the fact that this syscall may be unknown too). So there is not an issue with openat2(2).
However, *userland* should not try to infer possible security restrictions from the O_MAYEXEC flag (then, my use of "ignore" above), which may return -EACCES or not, according to the current running system security policy.
Following this reasoning, the current behavior or open(2) is fine for O_MAYEXEC. The openat2(2) strict flag handling (i.e. -EINVAL) is fine too for O_MAYEXEC.
Quoted 6 lines
Note that this point is regardless of the fact that
O_MAYEXECis a *security* flag -- if userspace wants to have a secure fallback on old kernels (which is "the right thing" to do) they would have to do more work than necessary. And programs that don't care don't have to do anything special.
Most of the time this reasoning is good for most security stuff. However, the O_MAYEXEC flag is not a security feature on its own, it is an indication to the kernel to how this file would be used by userland. The *kernel* security policy may tell back to userland if the system security policy allow it or not. Most of the time, Policy Decision Points (PDP) and Policy Enforcement Points (PEP) are in the same software component (e.g. the kernel). Here the kernel is the PDP and userland interpreters are PDP. Obviously, it means that these interpreters must be (sub)part of your TCB (thanks to other security features).
Quoted 5 lines
However with
-EINVAL, the programs doing "the right thing" get an easy-EINVALcheck. And programs that don't care can just un-setO_MAYEXECand retry. You should be forced to deal with the case where a flag is not supported -- and this is doubly true of security flags!
I'm in favor of doing this for openat2(2) with O_MAYEXEC, but it is not because of the "security purposes" of this flag, as I said above, it is because it is a saner ABI that every syscall should follow. But again, it doesn't change my point about open(2). :)
--
Mickaël Salaün
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: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Mickaël SalaünQuoted 10 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.
Depends on your definition of "safe" -- a security feature that you will silently not enable on older kernels doesn't sound super safe to me. Unfortunately this is a limitation of open(2) that we cannot change -- which is why the openat2(2) proposal I've been posting gives -EINVAL for unknown O_* flags.
There is a way to probe for support (though unpleasant), by creating a test O_MAYEXEC fd and then checking if the flag is present in /proc/self/fdinfo/$n.
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 18 lines · Aleksa Sarai
Quoted 10 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Depends on your definition of "safe" -- a security feature that you will silently not enable on older kernels doesn't sound super safe to me. Unfortunately this is a limitation of open(2) that we cannot change -- which is why the openat2(2) proposal I've been posting gives
-EINVALfor unknown O_* flags.
Quoted 3 lines
There is a way to probe for support (though unpleasant), by creating a test
O_MAYEXECfd and then checking if the flag is present in/proc/self/fdinfo/$n.
Which Florian said they can't do for various reasons.
It is a major painpoint if there's no easy way for userspace to probe for support. Especially if it's security related which usually means that you want to know whether this feature works or not.
Christian
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 23 lines · Christian Brauner
Quoted 18 lines · Aleksa Sarai
Quoted 10 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Depends on your definition of "safe" -- a security feature that you will silently not enable on older kernels doesn't sound super safe to me. Unfortunately this is a limitation of open(2) that we cannot change -- which is why the openat2(2) proposal I've been posting gives
-EINVALfor unknown O_* flags.Quoted 3 lines
There is a way to probe for support (though unpleasant), by creating a test
O_MAYEXECfd and then checking if the flag is present in/proc/self/fdinfo/$n.
Quoted 5 lines
Which Florian said they can't do for various reasons.
It is a major painpoint if there's no easy way for userspace to probe for support. Especially if it's security related which usually means that you want to know whether this feature works or not.
I used "safe" deliberately (not "secure" which didn't make sense in this sentence). According to the threat model, if the kernel doesn't support the feature, it should be ignored by userland. In this case, it fit well with the current behavior of open(2). I agree that the openat2(2) behavior handling flags is the good way to do it (whitelisting), but the O_MAYEXEC flag should not change the userland behavior on its own, because it depend on a global policy. Even being able to probe for O_MAYEXEC support does not make sense because it would not be enough to know the system policy (either this flag is enforced or not…).
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: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
In reply to Christian BraunerQuoted 23 lines · Christian Brauner
Quoted 18 lines · Aleksa Sarai
Quoted 10 lines · Mickaël Salaün
Quoted 5 lines · Florian Weimer
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Depends on your definition of "safe" -- a security feature that you will silently not enable on older kernels doesn't sound super safe to me. Unfortunately this is a limitation of open(2) that we cannot change -- which is why the openat2(2) proposal I've been posting gives
-EINVALfor unknown O_* flags.Quoted 3 lines
There is a way to probe for support (though unpleasant), by creating a test
O_MAYEXECfd and then checking if the flag is present in/proc/self/fdinfo/$n.
Quoted 5 lines
Which Florian said they can't do for various reasons.
It is a major painpoint if there's no easy way for userspace to probe for support. Especially if it's security related which usually means that you want to know whether this feature works or not.
What about just trying to violate the policy via fexecve() instead of looking around in /proc? Still ugly, though.
Tycho
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
* Tycho Andersen:
Quoted 24 lines · Christian Brauner
Quoted 23 lines · Aleksa Sarai
Quoted 21 lines · Mickaël Salaün
Quoted 10 lines · Florian Weimer
Quoted 5 lines
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Depends on your definition of "safe" -- a security feature that you will silently not enable on older kernels doesn't sound super safe to me. Unfortunately this is a limitation of open(2) that we cannot change -- which is why the openat2(2) proposal I've been posting gives
-EINVALfor unknown O_* flags.There is a way to probe for support (though unpleasant), by creating a test
O_MAYEXECfd and then checking if the flag is present in/proc/self/fdinfo/$n.
Quoted 8 lines
Quoted 5 lines
Which Florian said they can't do for various reasons.
It is a major painpoint if there's no easy way for userspace to probe for support. Especially if it's security related which usually means that you want to know whether this feature works or not.
What about just trying to violate the policy via
fexecve()instead of looking around in /proc? Still ugly, though.
How would we do this? This is about opening the main executable as part of an explicit loader invocation. Typically, an fexecve will succeed and try to run the program, but with the wrong dynamic loader.
Thanks,
Florian
Re: [PATCH v2 1/5] fs: Add support for an O_MAYEXEC flag on sys_open()
Quoted 32 lines · Florian Weimer
* Tycho Andersen:
Quoted 30 lines · Christian Brauner
Quoted 28 lines · Aleksa Sarai
Quoted 18 lines · Mickaël Salaün
Quoted 10 lines · Florian Weimer
Quoted 5 lines
Let's assume I want to add support for this to the glibc dynamic loader, while still being able to run on older kernels.
Is it safe to try the open call first, with
O_MAYEXEC, and if that fails with EINVAL, try again withoutO_MAYEXEC?The kernel ignore unknown open(2) flags, so yes, it is safe even for older kernel to use
O_MAYEXEC.Depends on your definition of "safe" -- a security feature that you will silently not enable on older kernels doesn't sound super safe to me. Unfortunately this is a limitation of open(2) that we cannot change -- which is why the openat2(2) proposal I've been posting gives
-EINVALfor unknown O_* flags.Quoted 3 lines
There is a way to probe for support (though unpleasant), by creating a test
O_MAYEXECfd and then checking if the flag is present in/proc/self/fdinfo/$n.Which Florian said they can't do for various reasons.
It is a major painpoint if there's no easy way for userspace to probe for support. Especially if it's security related which usually means that you want to know whether this feature works or not.
Quoted 6 lines
Quoted 2 lines
What about just trying to violate the policy via
fexecve()instead of looking around in /proc? Still ugly, though.How would we do this? This is about opening the main executable as part of an explicit loader invocation. Typically, an fexecve will succeed and try to run the program, but with the wrong dynamic loader.
Yeah, fexecve() was a think-o, sorry, you don't need to go that far. I was thinking do what the tests in this series do: create a tmpfs with MS_NOEXEC, put an executable file in it, and try and open it with O_MAYEXEC. If that works, the kernel doesn't support the flag, and it should give you -EACCES if the kernel does support the flag.
Still a lot of work, though. Seems better to just use openat2.
Tycho
PATCH v2 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
@@ -2968,6 +2968,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
@@ -101,6 +101,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.23.0
PATCH v2 3/5 fs: Enable to enforce noexec mounts or file exec 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 fs.open_mayexec_enforce to control this behavior. A following patch adds documentation.
Changes since v1:
* move code from Yama to the FS subsystem (suggested by Kees Cook)
* make omayexec_inode_permission() static (suggested by Jann Horn)
* use mode 0600 for the sysctl
* only match regular files (not directories nor other types), which follows the same semantic as commit 73601ea5b7b1 ("fs/open.c: allow opening only regular files during execve()")
--- fs/namei.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 3 ++ kernel/sysctl.c | 7 +++++ 3 files changed, 78 insertions(+)
fs/namei.c +68 -0
@@ -39,6 +39,7 @@#include <linux/bitops.h> #include <linux/init_task.h> #include <linux/uaccess.h> +#include <linux/sysctl.h> #include "internal.h" #include "mount.h"
@@ -411,6 +412,34 @@ static int sb_permission(struct super_block *sb, struct inode *inode, int mask)return 0 ; } +#define OMAYEXEC_ENFORCE_NONE 0 +#define OMAYEXEC_ENFORCE_MOUNT (1 << 0) +#define OMAYEXEC_ENFORCE_FILE (1 << 1) +#define _OMAYEXEC_LAST OMAYEXEC_ENFORCE_FILE +#define _OMAYEXEC_MASK ((_OMAYEXEC_LAST << 1) - 1) + +/** +* omayexec_inode_permission - check O_MAYEXEC before accessing an inode +* @inode: inode structure to check +* @mask: permission mask +* +* Return 0 if access is permitted, -EACCES otherwise. +*/ +static int omayexec_inode_permission (struct inode *inode,int mask) +{ +if (!(mask &MAY_OPENEXEC )) +return 0 ; + +if ((sysctl_omayexec_enforce &OMAYEXEC_ENFORCE_MOUNT ) && + !(mask &MAY_EXECMOUNT )) +return -EACCES ; + +if (sysctl_omayexec_enforce &OMAYEXEC_ENFORCE_FILE ) +return generic_permission (inode,MAY_EXEC ); + +return 0 ; +} +/** * inode_permission - Check for access rights to a given inode * @inode: Inode to check permission on
@@ -454,10 +483,48 @@ int inode_permission(struct inode *inode, int mask)if (retval)return retval; + retval =omayexec_inode_permission (inode, mask); +if (retval) +return retval; +return security_inode_permission (inode, mask); }EXPORT_SYMBOL (inode_permission); +/* +* Handle open_mayexec_enforce sysctl +*/ +#ifdef CONFIG_SYSCTL +int proc_omayexec (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 sysctl_omayexec_enforce */ + table_copy.data = &tmp_mayexec_enforce; + error =proc_dointvec (&table_copy, write, buffer, lenp, ppos); +if (error) +return error; +if ((tmp_mayexec_enforce | _OMAYEXEC_MASK) != _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 ; +} +#endif +/** * path_get - get a reference to a path * @path: path to get the reference to
@@ -887,6 +954,7 @@ int sysctl_protected_symlinks __read_mostly = 0;int sysctl_protected_hardlinks__read_mostly =0 ;int sysctl_protected_fifos__read_mostly ;int sysctl_protected_regular__read_mostly ; +int sysctl_omayexec_enforce__read_mostly =OMAYEXEC_ENFORCE_NONE ;/** * may_follow_link - Check symlink following for unsafe situations
include/linux/fs.h +3 -0
@@ -81,6 +81,7 @@ extern int sysctl_protected_symlinks;extern int sysctl_protected_hardlinks;extern int sysctl_protected_fifos;extern int sysctl_protected_regular; +extern int sysctl_omayexec_enforce;typedef __kernel_rwf_t rwf_t ;
@@ -3452,6 +3453,8 @@ int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer, size_t *lenp, loff_t *ppos);int proc_nr_inodes (struct ctl_table *table,int write,void __user *buffer,size_t *lenp,loff_t *ppos); +int proc_omayexec (struct ctl_table *table,int write,void __user *buffer, +size_t *lenp,loff_t *ppos);int __init get_filesystem_list (char *buf);#define __FMODE_EXEC ((__force int) FMODE_EXEC)
kernel/sysctl.c +7 -0
@@ -1911,6 +1911,13 @@ static struct ctl_table fs_table[] = { .extra1 = SYSCTL_ZERO, .extra2 = &two, }, + { + .procname = "open_mayexec_enforce", + .data = &sysctl_omayexec_enforce, + .maxlen = sizeof(int), + .mode = 0600, + .proc_handler = proc_omayexec, + }, #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE) { .procname = "binfmt_misc",
Signature · 1 line
2.23.0
PATCH v2 4/5 selftest/exec: 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.
Changes since v1:
* move tests from yama to exec
* fix _GNU_SOURCE in kselftest_harness.h
* add a new test sysctl_access_write to check if CAP_MAC_ADMIN is taken into account
* test directory execution which is always forbidden since commit 73601ea5b7b1 ("fs/open.c: allow opening only regular files during execve()"), and also check that even the root user can not bypass file execution checks
* make sure delete_workspace() always as enough right to succeed
* cosmetic cleanup
--- tools/testing/selftests/exec/.gitignore | 1 + tools/testing/selftests/exec/Makefile | 4 +- tools/testing/selftests/exec/omayexec.c | 317 ++++++++++++++++++++ tools/testing/selftests/kselftest_harness.h | 3 + 4 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/exec/omayexec.c
tools/testing/selftests/exec/.gitignore +1 -0
@@ -1,3 +1,4 @@ +/omayexec subdir* script* execveat
tools/testing/selftests/exec/Makefile +3 -1
@@ -3,7 +3,7 @@ CFLAGS = -Wall CFLAGS += -Wno-nonnull CFLAGS += -D_GNU_SOURCE -TEST_GEN_PROGS := execveat +TEST_GEN_PROGS := execveat omayexec TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir # Makefile is a run-time dependency, since it's accessed by the execveat test TEST_FILES := Makefile
@@ -26,3 +26,5 @@ $(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat cp $< $@ chmod -x $@ +$(OUTPUT)/omayexec: omayexec.c ../kselftest_harness.h + $(CC) $(CFLAGS) -Wl,-no-as-needed $(LDFLAGS) -lcap $< -o $@
tools/testing/selftests/exec/omayexec.c new file +317 -0
317 lines not on this page · raw message
tools/testing/selftests/kselftest_harness.h +3 -0
@@ -50,7 +50,10 @@#ifndef __KSELFTEST_HARNESS_H#define __KSELFTEST_HARNESS_H +#ifndef _GNU_SOURCE#define _GNU_SOURCE +#endif +#include <asm/types.h> #include <errno.h> #include <stdbool.h>
Signature · 1 line
2.23.0
PATCH v2 5/5 doc: Add documentation for the fs.open_mayexec_enforce sysctl
In reply to Mickaël SalaünChanges since v1:
* move from LSM/Yama to sysctl/fs
--- Documentation/admin-guide/sysctl/fs.rst | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
Documentation/admin-guide/sysctl/fs.rst +43 -0
@@ -37,6 +37,7 @@ Currently, these files are in /proc/sys/fs: - inode-nr - inode-state - nr_open +- open_mayexec_enforce - overflowuid - overflowgid - pipe-user-pages-hard
@@ -165,6 +166,48 @@ system needs to prune the inode list instead of allocating more. +open_mayexec_enforce +-------------------- + +The ``O_MAYEXEC`` flag can be passed to :manpage:`open(2)` to only open regular +files that are expected to be 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, it is possible 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. + +There is two complementary security policies: enforce the ``noexec`` mount +option, or enforce executable file permission. These policies are handled by +the ``fs.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/exec/omayexec.c and +https://github.com/clipos-archive/clipos4_portage-overlay/search?q=O_MAYEXEC . + + overflowgid & overflowuid -------------------------
Signature · 1 line
2.23.0
Quoted 5 lines · EDT 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 userspace script interpreter to delegate to the kernel (and thus the system security policy) the permission to interpret/execute scripts or other files containing what can be seen as commands.
The problem is that this is only a gentleman's handshake. If I don't tell the kernel that what I'm opening is tantamount to executing it, then the security feature is never invoked. It is simple to strip the flags off of any system call without needing privileges. For example:
#define _GNU_SOURCE
#include <link.h>
#include <fcntl.h>
#include <string.h>
unsigned intla_version(unsigned int version)
{
return version;
}unsigned intla_objopen(struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
{
return LA_FLG_BINDTO | LA_FLG_BINDFROM;
}typedef int (*openat_t) (int dirfd, const char *pathname, int flags, mode_t mode); static openat_t real_openat = 0L;
int my_openat(int dirfd, const char *pathname, int flags, mode_t mode)
{
flags &= ~O_CLOEXEC;
return real_openat(dirfd, pathname, flags, mode);
}uintptr_t
la_symbind64(Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
uintptr_t *defcook, unsigned int *flags, const char *symname)
{
if (real_openat == 0L && strcmp(symname, "openat") == 0) {
real_openat = (openat_t) sym->st_value;
return (uintptr_t) my_openat;
}
return sym->st_value;
}gcc -c -g -Wno-unused-parameter -W -Wall -Wundef -O2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fPIC test.c gcc -o strip-flags.so.0 -shared -Wl,-soname,strip-flags.so.0 -ldl test.o
Now, let's make a test program:
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
int dir_fd, fd;
DIR *d = opendir("/etc");
dir_fd = dirfd(d);
fd = openat(dir_fd, "passwd", O_RDONLY|O_CLOEXEC);
close (fd);
closedir(d);
return 0;
}gcc -g -W -Wall -Wundef test.c -o test
OK, let's see what happens.
$ strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY|O_CLOEXEC) = 4
Now with LD_AUDIT
$ LD_AUDIT=/home/sgrubb/test/openflags/strip-flags.so.0 strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY) = 4
No O_CLOEXEC flag.
-Steve
* Steve Grubb:
Quoted 5 lines
Now with
LD_AUDIT$ LD_AUDIT=/home/sgrubb/test/openflags/strip-flags.so.0 strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY) = 4No
O_CLOEXECflag.
I think you need to explain in detail why you consider this a problem.
With LD_PRELOAD and LD_AUDIT, you can already do anything, including scanning other loaded objects for a system call instruction and jumping to that (in case a security module in the kernel performs a PC check to confer additional privileges).
Thanks,
Florian
Quoted 8 lines · EDT Florian Weimer
* Steve Grubb:
Quoted 5 lines
Now with
LD_AUDIT$ LD_AUDIT=/home/sgrubb/test/openflags/strip-flags.so.0 strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY) = 4No
O_CLOEXECflag.I think you need to explain in detail why you consider this a problem.
Because you can strip the O_MAYEXEC flag from being passed into the kernel. Once you do that, you defeat the security mechanism because it never gets invoked. The issue is that the only thing that knows _why_ something is being opened is user space. With this mechanism, you can attempt to pass this reason to the kernel so that it may see if policy permits this. But you can just remove the flag.
-Steve
Quoted 7 lines
With
LD_PRELOADandLD_AUDIT, you can already do anything, including scanning other loaded objects for a system call instruction and jumping to that (in case a security module in the kernel performs a PC check to confer additional privileges).Thanks,
Florian
Andy Lutomirski
reviewerQuoted 12 lines · Steve Grubb
Quoted 9 lines · EDT Florian Weimer
* Steve Grubb:
Quoted 5 lines
Now with
LD_AUDIT$ LD_AUDIT=/home/sgrubb/test/openflags/strip-flags.so.0 strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY) = 4No
O_CLOEXECflag.
Quoted 8 lines
Quoted 1 line
I think you need to explain in detail why you consider this a problem.
Because you can strip the
O_MAYEXECflag from being passed into the kernel. Once you do that, you defeat the security mechanism because it never gets invoked. The issue is that the only thing that knows _why_ something is being opened is user space. With this mechanism, you can attempt to pass this reason to the kernel so that it may see if policy permits this. But you can just remove the flag.
I’m with Florian here. Once you are executing code in a process, you could just emulate some other unapproved code. This series is not intended to provide the kind of absolute protection you’re imagining.
What the kernel *could* do is prevent mmapping a non-FMODE_EXEC file with PROT_EXEC, which would indeed have a real effect (in an iOS-like world, for example) but would break many, many things.
Quoted 19 lines · Andy Lutomirski
Quoted 10 lines · Steve Grubb
Quoted 9 lines · EDT Florian Weimer
* Steve Grubb:
Quoted 5 lines
Now with
LD_AUDIT$ LD_AUDIT=/home/sgrubb/test/openflags/strip-flags.so.0 strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY) = 4No
O_CLOEXECflag.Quoted 8 lines
Quoted 1 line
I think you need to explain in detail why you consider this a problem.
Because you can strip the
O_MAYEXECflag from being passed into the kernel. Once you do that, you defeat the security mechanism because it never gets invoked. The issue is that the only thing that knows _why_ something is being opened is user space. With this mechanism, you can attempt to pass this reason to the kernel so that it may see if policy permits this. But you can just remove the flag.
Quoted 3 lines
I’m with Florian here. Once you are executing code in a process, you could just emulate some other unapproved code. This series is not intended to provide the kind of absolute protection you’re imagining.
I also agree, though I think that there is a separate argument to be made that there are two possible problems with O_MAYEXEC (which might not be really big concerns):
* It's very footgun-prone if you didn't call O_MAYEXEC yourself and you pass the descriptor elsewhere. You need to check f_flags to see if it contains O_MAYEXEC. Maybe there is an argument to be made that passing O_MAYEXECs around isn't a valid use-case, but in that case there should be some warnings about that.
* There's effectively a TOCTOU flaw (even if you are sure O_MAYEXEC is in f_flags) -- if the filesystem becomes re-mounted noexec (or the file has a-x permissions) after you've done the check you won't get hit with an error when you go to use the file descriptor later.
To fix both you'd need to do what you mention later:
Quoted 3 lines
What the kernel *could* do is prevent mmapping a non-FMODE_EXEC file with
PROT_EXEC, which would indeed have a real effect (in an iOS-like world, for example) but would break many, many things.
And I think this would be useful (with the two possible ways of executing .text split into FMODE_EXEC and FMODE_MAP_EXEC, as mentioned in a sister subthread), but would have to be opt-in for the obvious reason you outlined. However, we could make it the default for openat2(2) -- assuming we can agree on what the semantics of a theoretical FMODE_EXEC should be.
And of course we'd need to do FMODE_UPGRADE_EXEC (which would need to also permit fexecve(2) though probably not PROT_EXEC -- I don't think you can mmap() an O_PATH descriptor).
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH <https://www.cyphar.com/>
Quoted 5 lines · Aleksa Sarai
Quoted 4 lines · Andy Lutomirski
Quoted 4 lines · Steve Grubb
Quoted 3 lines · EDT Florian Weimer
* Steve Grubb:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Now with
LD_AUDIT$ LD_AUDIT=/home/sgrubb/test/openflags/strip-flags.so.0 strace ./test 2>&1 | grep passwd openat(3, "passwd", O_RDONLY) = 4No
O_CLOEXECflag.I think you need to explain in detail why you consider this a problem.
Right, LD_PRELOAD and such things are definitely not part of the threat model for O_MAYEXEC, on purpose, because this must be addressed with other security mechanism (e.g. correct file system access-control, IMA policy, SELinux or other LSM security policies). This is a requirement for O_MAYEXEC to be useful.
An interpreter is just a flexible program which is generic and doesn't have other purpose other than behaving accordingly to external rules (i.e. scripts). If you don't trust your interpreter, it should not be executable in the first place. O_MAYEXEC enables to restrict the use of (some) interpreters accordingly to a *global* system security policy.
Quoted 16 lines
Quoted 11 lines
Quoted 7 lines
Because you can strip the
O_MAYEXECflag from being passed into the kernel. Once you do that, you defeat the security mechanism because it never gets invoked. The issue is that the only thing that knows _why_ something is being opened is user space. With this mechanism, you can attempt to pass this reason to the kernel so that it may see if policy permits this. But you can just remove the flag.I’m with Florian here. Once you are executing code in a process, you could just emulate some other unapproved code. This series is not intended to provide the kind of absolute protection you’re imagining.
I also agree, though I think that there is a separate argument to be made that there are two possible problems with
O_MAYEXEC(which might not be really big concerns):
Quoted 5 lines
* It's very footgun-prone if you didn't call
O_MAYEXECyourself and you pass the descriptor elsewhere. You need to checkf_flagsto see if it containsO_MAYEXEC. Maybe there is an argument to be made that passingO_MAYEXECsaround isn't a valid use-case, but in that case there should be some warnings about that.
That could be an issue if you don't trust your system, especially if the mount points (and the "noexec" option) can be changed by untrusted users. As I said above, there is a requirement for basic security properties as a meaningful file system access control, and obviously not letting any user change mount points (which can lead to much sever security issues anyway).
If a process A pass a FD to an interpreter B, then the interpreter B must trust the process A. Moreover, being able to tell if the FD was open with O_MAYEXEC and relying on it may create a wrong feeling of security. As I said in a previous email, being able to probe for O_MAYEXEC does not make sense because it would not be enough to know the system policy (either this flag is enforced or not, for mount points, based on xattr, time…). The main goal of O_MAYEXEC is to ask the kernel, on a trusted link (hence without LD_PRELOAD-like interfering), for a file which is allowed to be interpreted/executed by this interpreter.
To be able to correctly handle the case you pointed out (FD passing), either an existing or a new LSM should handle this behavior according to the origin of the FD and the chain of processes getting it.
Some advanced LSM rules could tie interpreters with scripts dedicated to them, and have different behavior for the same scripts but with different interpreters.
Quoted 5 lines
* There's effectively a TOCTOU flaw (even if you are sure
O_MAYEXECis inf_flags) -- if the filesystem becomes re-mounted noexec (or the file has a-x permissions) after you've done the check you won't get hit with an error when you go to use the file descriptor later.
Again, the threat model needs to be appropriate to make O_MAYEXEC useful. The security policies of the system need to be seen as a whole, and updated as such.
As for most file system access control on Linux, it may be possible to have TOCTOU, but the whole system should be designed to protect against that. For example, changing file access control (e.g. mount point options) without a reboot may lead to inconsistent security properties, which is why such thing are discouraged by some access control systems (e.g. SELinux).
Quoted 14 lines
To fix both you'd need to do what you mention later:
Quoted 3 lines
What the kernel *could* do is prevent mmapping a non-FMODE_EXEC file with
PROT_EXEC, which would indeed have a real effect (in an iOS-like world, for example) but would break many, many things.And I think this would be useful (with the two possible ways of executing .text split into
FMODE_EXECandFMODE_MAP_EXEC, as mentioned in a sister subthread), but would have to be opt-in for the obvious reason you outlined. However, we could make it the default for openat2(2) -- assuming we can agree on what the semantics of a theoreticalFMODE_EXECshould be.
Quoted 3 lines
And of course we'd need to do
FMODE_UPGRADE_EXEC(which would need to also permit fexecve(2) though probably notPROT_EXEC-- I don't think you canmmap()anO_PATHdescriptor).
The mmapping restriction may be interesting but it is a different use case. This series address the interpreter/script problem. Either the script may be mapped executable is the choice of the interpreter. In most cases, no script are mapped as such, exactly because they are interpreted by a process but not by the CPU.
--
Mickaël Salaün
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.
James Morris
maintainerQuoted 4 lines · Mickaël Salaün
Furthermore, the security policy can also be delegated to an LSM, either a MAC system or an integrity system. For instance, the new kernel
MAY_OPENEXECflag closes a major IMA measurement/appraisal interpreter integrity gap by bringing the ability to check the use of scripts [2].
To clarify, scripts are already covered by IMA if they're executed directly, and the gap is when invoking a script as a parameter to the interpreter (and for any sourced files). In that case only the interpreter is measured/appraised, unless there's a rule also covering the script file(s).
See:
https://en.opensuse.org/SDB:Ima_evm#script-behaviour
In theory you could probably also close the gap by modifying the interpreters to check for the execute bit on any file opened for interpretation (as earlier suggested by Steve Grubb), and then you could have IMA measure/appraise all files with +x. I suspect this could get messy in terms of unwanted files being included, and the MAY_OPENEXEC flag has cleaner semantics.
Signature · 2 lines
James Morris <[email protected]>