S.A.R.A. a new stacked LSM
Subsystems (10)
v4 cover S.A.R.A. a new stacked LSM
S.A.R.A. (S.A.R.A. is Another Recursive Acronym) is a stacked Linux Security Module that aims to collect heterogeneous security measures, providing a common interface to manage them.
It can be useful to allow minor security features to use advanced management options, like user-space configuration files and tools, without too much overhead.
Some submodules that use this framework are also introduced. The code is quite long, I apologize for this. Thank you in advance to anyone who will take the time to review this patchset.
S.A.R.A. is meant to be stacked but it needs cred blobs and the procattr interface, so I temporarily implemented those parts in a way that won't be acceptable for upstream, but it works for now. I know that there is some ongoing work to make cred blobs and procattr stackable, as soon as the new interfaces will be available I'll reimplement the involved parts.
At the moment I've been able to test it only on x86.
The only submodule introduced in this patchset is WX Protection.
The kernel-space part is complemented by its user-space counterpart:
saractl [1].
A test suite for WX Protection, called sara-test [2], is also available.
WX Protection aims to improve user-space programs security by applying:
- W^X enforcement: program can't have a page of memory that is marked, at the same time, writable and executable.
- W!->X restriction: any page that could have been marked as writable in the past won't ever be allowed to be marked as executable.
- Executable MMAP prevention: prevents the creation of new executable mmaps after the dynamic libraries have been loaded.
All of the above features can be enabled or disabled both system wide or on a per executable basis through the use of configuration files managed by "saractl".
It is important to note that some programs may have issues working with WX Protection. In particular:
- W^X enforcement will cause problems to any programs that needs memory pages mapped both as writable and executable at the same time e.g. programs with executable stack markings in the PT_GNU_STACK segment.
- W!->X restriction will cause problems to any program that needs to generate executable code at run time or to modify executable pages e.g. programs with a JIT compiler built-in or linked against a non-PIC library.
- Executable MMAP prevention can work only with programs that have at least partial RELRO support. It's disabled automatically for programs that lack this feature. It will cause problems to any program that uses dlopen or tries to do an executable mmap. Unfortunately this feature is the one that could create most problems and should be enabled only after careful evaluation.
To extend the scope of the above features, despite the issues that they may cause, they are complemented by:
- procattr interface: can be used by a program to discover which WX Protection features are enabled and/or to tighten them.
- Trampoline emulation: emulates the execution of well-known "trampolines" even when they are placed in non-executable memory.
Parts of WX Protection are inspired by some of the features available in PaX.
Thanks to the addition of extended attributes support, it's now possible to use S.A.R.A. without being forced to rely on any special userspace tool.
More information can be found in the documentation introduced in the first patch and in the "commit message" of the following emails.
Changes in v2:
- Removed USB filtering submodule and relative hook
- s/saralib/libsara/ typo
- STR macro renamed to avoid conflicts
- check_vmflags hook now returns an error code instead of just 1 or 0. (suggested by Casey Schaufler)
- pr_wxp macro rewritten as function for readability
- Fixed i386 compilation warnings
- Documentation now states clearly that changes done via procattr interface only apply to current thread. (suggested by Jann Horn)
Changes in v3:
- Documentation has been moved to match the new directory structure.
- Kernel cmdline arguments are now accessed via module_param interface (suggested by Kees Cook).
- Created "sara_warn_or_return" macro to make WX Protection code more readable (suggested by Kees Cook).
- Added more comments, in the most important places, to clarify my intentions (suggested by Kees Cook).
- The "pagefault_handler" hook has been rewritten in a more "arch agnostic" way. Though it only support x86 at the moment (suggested by Kees Cook).
Changes in v4:
- Documentation improved and some mistakes have been fixed.
- Reduced dmesg verbosity.
- check_vmflags is now also used to decide whether to ignore GNU executable stack markings or not.
- Added the check_vmflags hook in setup_arg_pages too.
- Added support for extended attributes.
- Moved trampoline emulation to arch/x86/ (suggested by Kees Cook).
- SARA_WXP_MMAP now depends on SARA_WXP_OTHER.
- MAC_ADMIN capability is now required also for config read.
- Some other minor fixes not worth mentionig here.
[1] https://github.com/smeso/saractl
[2] https://github.com/smeso/sara-test
Salvatore Mesoraca (10):
S.A.R.A. Documentation
S.A.R.A. framework creation
Creation of "check_vmflags" LSM hook S.A.R.A. cred blob management
S.A.R.A. WX Protection
Creation of "pagefault_handler" LSM hook Trampoline emulation
Allowing for stacking procattr support in S.A.R.A. S.A.R.A. WX Protection procattr interface XATTRs support
Documentation/admin-guide/LSM/SARA.rst | 193 +++++ Documentation/admin-guide/LSM/index.rst | 1 + Documentation/admin-guide/kernel-parameters.txt | 40 + arch/Kconfig | 6 + arch/x86/Kbuild | 2 + arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 6 + arch/x86/security/Makefile | 2 + arch/x86/security/sara/Makefile | 1 + arch/x86/security/sara/emutramp.c | 55 ++ arch/x86/security/sara/trampolines32.h | 122 +++ arch/x86/security/sara/trampolines64.h | 148 ++++ fs/binfmt_elf.c | 3 +- fs/binfmt_elf_fdpic.c | 3 +- fs/exec.c | 4 + fs/proc/base.c | 38 + include/linux/cred.h | 3 + include/linux/lsm_hooks.h | 24 + include/linux/security.h | 17 + include/uapi/linux/xattr.h | 4 + mm/mmap.c | 13 + security/Kconfig | 1 + security/Makefile | 2 + security/sara/Kconfig | 154 ++++ security/sara/Makefile | 4 + security/sara/include/emutramp.h | 33 + security/sara/include/sara.h | 29 + security/sara/include/sara_data.h | 55 ++ security/sara/include/securityfs.h | 59 ++ security/sara/include/utils.h | 80 ++ security/sara/include/wxprot.h | 27 + security/sara/main.c | 117 +++ security/sara/sara_data.c | 79 ++ security/sara/securityfs.c | 563 +++++++++++++ security/sara/utils.c | 151 ++++ security/sara/wxprot.c | 1017 +++++++++++++++++++++++ security/security.c | 37 +- 37 files changed, 3090 insertions(+), 4 deletions(-) create mode 100644 Documentation/admin-guide/LSM/SARA.rst create mode 100644 arch/x86/security/Makefile create mode 100644 arch/x86/security/sara/Makefile create mode 100644 arch/x86/security/sara/emutramp.c create mode 100644 arch/x86/security/sara/trampolines32.h create mode 100644 arch/x86/security/sara/trampolines64.h create mode 100644 security/sara/Kconfig create mode 100644 security/sara/Makefile create mode 100644 security/sara/include/emutramp.h create mode 100644 security/sara/include/sara.h create mode 100644 security/sara/include/sara_data.h create mode 100644 security/sara/include/securityfs.h create mode 100644 security/sara/include/utils.h create mode 100644 security/sara/include/wxprot.h create mode 100644 security/sara/main.c create mode 100644 security/sara/sara_data.c create mode 100644 security/sara/securityfs.c create mode 100644 security/sara/utils.c create mode 100644 security/sara/wxprot.c
Signature · 1 line
1.9.1
RFC v4 1/10 S.A.R.A. Documentation
Adding documentation for S.A.R.A. LSM.
--- Documentation/admin-guide/LSM/SARA.rst | 173 ++++++++++++++++++++++++ Documentation/admin-guide/LSM/index.rst | 1 + Documentation/admin-guide/kernel-parameters.txt | 24 ++++ 3 files changed, 198 insertions(+) create mode 100644 Documentation/admin-guide/LSM/SARA.rst
Documentation/admin-guide/LSM/SARA.rst new file +173 -0
@@ -0,0 +1,173 @@ +======== +S.A.R.A. +======== + +S.A.R.A. (S.A.R.A. is Another Recursive Acronym) is a stacked Linux Security +Module that aims to collect heterogeneous security measures, providing a common +interface to manage them. +As of today it consists of one submodule: + +- WX Protection + + +The kernel-space part is complemented by its user-space counterpart: `saractl` +[2]_. +A test suite for WX Protection, called `sara-test` [4]_, is also available. +More information about where to find these tools and the full S.A.R.A. +documentation are in the `External Links and Documentation`_ section. + +------------------------------------------------------------------------------- + +S.A.R.A.'s Submodules +===================== + +WX Protection +------------- +WX Protection aims to improve user-space programs security by applying: + +- `W^X enforcement`_ +- `W!->X (once writable never executable) mprotect restriction`_ +- `Executable MMAP prevention`_ + +All of the above features can be enabled or disabled both system wide +or on a per executable basis through the use of configuration files managed by +`saractl` [2]_. + +It is important to note that some programs may have issues working with +WX Protection. In particular: + +- **W^X enforcement** will cause problems to any programs that needs + memory pages mapped both as writable and executable at the same time e.g. + programs with executable stack markings in the *PT_GNU_STACK* segment. +- **W!->X mprotect restriction** will cause problems to any program that + needs to generate executable code at run time or to modify executable + pages e.g. programs with a *JIT* compiler built-in or linked against a + *non-PIC* library. +- **Executable MMAP prevention** can work only with programs that have at least + partial *RELRO* support. It's disabled automatically for programs that + lack this feature. It will cause problems to any program that uses *dlopen* + or tries to do an executable mmap. Unfortunately this feature is the one + that could create most problems and should be enabled only after careful + evaluation. + +To extend the scope of the above features, despite the issues that they may +cause, they are complemented by **/proc/PID/attr/sara/wxprot** interface +and **trampoline emulation**. + +At the moment, WX Protection (unless specified otherwise) should work on +any architecture supporting the NX bit, including, but not limited to: +`x86_64`, `x86_32` (with PAE), `ARM` and `ARM64`. + +Parts of WX Protection are inspired by some of the features available in PaX. + +For further information about configuration file format and user-space +utilities please take a look at the full documentation [1]_. + +W^X enforcement +^^^^^^^^^^^^^^^ +W^X means that a program can't have a page of memory that is marked, at the +same time, writable and executable. This also allow to detect many bad +behaviours that make life much more easy for attackers. Programs running with +this feature enabled will be more difficult to exploit in the case they are +affected by some vulnerabilities, because the attacker will be forced +to make more steps in order to exploit them. + +W!->X (once writable never executable) mprotect restriction +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +"Once writable never executable" means that any page that could have been +marked as writable in the past won't ever be allowed to be marked (e.g. via +an mprotect syscall) as executable. +This goes on the same track as W^X, but is much stricter and prevents +the runtime creation of new executable code in memory. +Obviously, this feature does not prevent a program from creating a new file and +*mmapping* it as executable, however, it will be way more difficult for +attackers to exploit vulnerabilities if this feature is enabled. + +Executable MMAP prevention +^^^^^^^^^^^^^^^^^^^^^^^^^^ +This feature prevents the creation of new executable mmaps after the dynamic +libraries have been loaded. When used in combination with **W!->X mprotect +restriction** this feature will completely prevent the creation of new +executable code from the current thread. +Obviously, this feature does not prevent cases in which an attacker uses an +*execve* to start a completely new program. This kind of restriction, if +needed, can be applied using one of the other LSM that focuses on MAC. +Please be aware that this feature can break many programs and so it should be +enabled after careful evaluation. + +/proc/PID/attr/sara/wxprot interface +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The `procattr` interface can be used by a thread to discover which +WX Protection features are enabled and/or to tighten them: protection +can't be softened via procattr. +The interface is simple: it's a text file with an hexadecimal +number in it representing enabled features (more information can be +found in the `Flags values`_ section). Via this interface it is also +possible to perform a complete memory scan to remove the write permission +from pages that are both writable and executable, please note that this +change will also affect other threads of the same process. + +Protections that prevent the runtime creation of executable code +can be troublesome for all those programs that actually need to do it +e.g. programs shipping with a JIT compiler built-in. +This feature can be use to run the JIT compiler with few restrictions +while enforcing full WX Protection in the rest of the program. + +The preferred way to access this interface is via `libsara` [3]_. +If you don't want it as a dependency, you can just statically link it +in your project or copy/paste parts of it. +To make things simpler `libsara` is the only part of S.A.R.A. released under +*CC0 - No Rights Reserved* license. + +Trampoline emulation +^^^^^^^^^^^^^^^^^^^^ +Some programs need to generate part of their code at runtime. Luckily enough, +in some cases they only generate well-known code sequences (the +*trampolines*) that can be easily recognized and emulated by the kernel. +This way WX Protection can still be active, so a potential attacker won't be +able to generate arbitrary sequences of code, but just those that are +explicitly allowed. This is not ideal, but it's still better than having WX +Protection completely disabled. + +In particular S.A.R.A. is able to recognize trampolines used by GCC for nested +C functions and libffi's trampolines. +This feature is available only on `x86_32` and `x86_64`. + +Flags values +^^^^^^^^^^^^ +Flags are represented as a 16 bit unsigned integer in which every bit indicates +the status of a given feature: + ++------------------------------+----------+ +| Feature | Value | ++==============================+==========+ +| W!->X Heap | 0x0001 | ++------------------------------+----------+ +| W!->X Stack | 0x0002 | ++------------------------------+----------+ +| W!->X Other memory | 0x0004 | ++------------------------------+----------+ +| W^X | 0x0008 | ++------------------------------+----------+ +| Don't enforce, just complain | 0x0010 | ++------------------------------+----------+ +| Be Verbose | 0x0020 | ++------------------------------+----------+ +| Executable MMAP prevention | 0x0040 | ++------------------------------+----------+ +| Force W^X on setprocattr | 0x0080 | ++------------------------------+----------+ +| Trampoline emulation | 0x0100 | ++------------------------------+----------+ +| Children will inherit flags | 0x0200 | ++------------------------------+----------+ + +------------------------------------------------------------------------------- + +External Links and Documentation +================================ + +.. [1] `Documentation <https://sara.smeso.it>`_ +.. [2] `saractl <https://github.com/smeso/saractl>`_ +.. [3] `libsara <https://github.com/smeso/libsara>`_ +.. [4] `sara-test <https://github.com/smeso/sara-test>`_
Documentation/admin-guide/LSM/index.rst +1 -0
@@ -39,3 +39,4 @@ the one "major" module (e.g. SELinux) if there is one configured. Smack tomoyo Yama + SARA
Documentation/admin-guide/kernel-parameters.txt +24 -0
@@ -3817,6 +3817,30 @@ 1 -- enable. Default value is set via kernel config option. + sara.enabled= [SARA] Disable or enable S.A.R.A. at boot time. + If disabled this way S.A.R.A. can't be enabled + again. + Format: { "0" | "1" } + See security/sara/Kconfig help text + 0 -- disable. + 1 -- enable. + Default value is set via kernel config option. + + sara.wxprot_enabled= [SARA] + Disable or enable S.A.R.A. WX Protection + at boot time. + Format: { "0" | "1" } + See security/sara/Kconfig help text + 0 -- disable. + 1 -- enable. + Default value is set via kernel config option. + + sara.wxprot_default_flags= [SARA] + Set S.A.R.A. WX Protection default flags. + Format: <integer> + See S.A.R.A. documentation. + Default value is set via kernel config option. + serialnumber [BUGS=X86-32] shapers= [NET]
Signature · 1 line
1.9.1
RFC v4 2/10 S.A.R.A. framework creation
In reply to Salvatore MesoracaInitial S.A.R.A. framework setup.
Creation of a simplified interface to securityfs API to store and retrieve configurations and flags from user-space.
Creation of some generic functions and macros to handle concurrent access to configurations, memory allocation and path resolution.
--- include/linux/lsm_hooks.h | 5 + security/Kconfig | 1 + security/Makefile | 2 + security/sara/Kconfig | 40 +++ security/sara/Makefile | 3 + security/sara/include/sara.h | 29 ++ security/sara/include/securityfs.h | 59 ++++ security/sara/include/utils.h | 69 +++++ security/sara/main.c | 105 +++++++ security/sara/securityfs.c | 563 +++++++++++++++++++++++++++++++++++++ security/sara/utils.c | 151 ++++++++++ security/security.c | 1 + 12 files changed, 1028 insertions(+) create mode 100644 security/sara/Kconfig create mode 100644 security/sara/Makefile create mode 100644 security/sara/include/sara.h create mode 100644 security/sara/include/securityfs.h create mode 100644 security/sara/include/utils.h create mode 100644 security/sara/main.c create mode 100644 security/sara/securityfs.c create mode 100644 security/sara/utils.c
include/linux/lsm_hooks.h +5 -0
@@ -2025,5 +2025,10 @@ static inline void __init yama_add_hooks(void) { }#else static inline void loadpin_add_hooks (void ) { };#endif +#ifdef CONFIG_SECURITY_SARA +void __init sara_init (void ); +#else +static inline void __init sara_init (void ) { }; +#endif #endif /* ! __LINUX_LSM_HOOKS_H */
security/Kconfig +1 -0
@@ -211,6 +211,7 @@ source security/tomoyo/Kconfig source security/apparmor/Kconfig source security/loadpin/Kconfig source security/yama/Kconfig +source security/sara/Kconfig source security/integrity/Kconfig
security/Makefile +2 -0
@@ -10,6 +10,7 @@ subdir-$(CONFIG_SECURITY_TOMOYO) += tomoyo subdir-$(CONFIG_SECURITY_APPARMOR) += apparmor subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin +subdir-$(CONFIG_SECURITY_SARA) += sara # always enable default capabilities obj-y += commoncap.o
@@ -25,6 +26,7 @@ obj-$(CONFIG_SECURITY_TOMOYO) += tomoyo/ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ +obj-$(CONFIG_SECURITY_SARA) += sara/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file lists
security/sara/Kconfig new file +40 -0
@@ -0,0 +1,40 @@ +menuconfig SECURITY_SARA + bool "S.A.R.A." + depends on SECURITY + select SECURITYFS + default n + help + This selects S.A.R.A. LSM which aims to collect heterogeneous + security measures providing a common interface to manage them. + This LSM will always be stacked with the selected primary LSM and + other stacked LSMs. + Further information can be found in + Documentation/admin-guide/LSM/SARA.rst. + + If unsure, answer N. + +config SECURITY_SARA_DEFAULT_DISABLED + bool "S.A.R.A. will be disabled at boot." + depends on SECURITY_SARA + default n + help + If you say Y here, S.A.R.A. will not be enabled at startup. You can + override this option at boot time via "sara.enabled=[1|0]" kernel + parameter or via user-space utilities. + This option is useful for distro kernels. + + If unsure, answer N. + +config SECURITY_SARA_NO_RUNTIME_ENABLE + bool "S.A.R.A. can be turn on only at boot time." + depends on SECURITY_SARA_DEFAULT_DISABLED + default y + help + By enabling this option it won't be possible to turn on S.A.R.A. + at runtime via user-space utilities. However it can still be + turned on at boot time via the "sara.enabled=1" kernel parameter. + This option is functionally equivalent to "sara.enabled=0" kernel + parameter. This option is useful for distro kernels. + + If unsure, answer Y. +
security/sara/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_SARA) := sara.o + +sara-y := main.o securityfs.o utils.o
security/sara/include/sara.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* S.A.R.A. Linux Security Module +* +* Copyright (C) 2017 Salvatore Mesoraca <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +* +*/ + +#ifndef __SARA_H +#define __SARA_H + +#include <linux/types.h> +#include <uapi/linux/limits.h> + +#define SARA_VERSION 0 +#define SARA_PATH_MAX PATH_MAX + +#undef pr_fmt +#define pr_fmt (fmt) "SARA: " fmt + +extern bool sara_config_locked__read_mostly ; +extern bool sara_enabled__read_mostly ; + +void sara_init (void )__init ; + +#endif /* __SARA_H */
security/sara/include/securityfs.h new file +59 -0
59 lines not on this page · raw message
security/sara/include/utils.h new file +69 -0
69 lines not on this page · raw message
security/sara/main.c new file +105 -0
105 lines not on this page · raw message
security/sara/securityfs.c new file +563 -0
563 lines not on this page · raw message
security/sara/utils.c new file +151 -0
151 lines not on this page · raw message
security/security.c +1 -0
@@ -74,6 +74,7 @@ int __init security_init(void)capability_add_hooks ();yama_add_hooks ();loadpin_add_hooks (); +sara_init ();/* * Load all the remaining security modules.
Signature · 1 line
1.9.1
RFC v4 3/10 Creation of "check_vmflags" LSM hook
In reply to Salvatore MesoracaCreation of a new LSM hook to check if a given configuration of vmflags, for a new memory allocation request, should be allowed or not. It's placed in "do_mmap", "do_brk_flags", "__install_special_mapping" and "setup_arg_pages".
When loading an ELF, this hook is also used to determine what to do with an RWE PT_GNU_STACK header. This allows LSM to force the loader to silently ignore executable stack markings, which is useful a thing to do when trampoline emulation is available.
--- fs/binfmt_elf.c | 3 ++- fs/binfmt_elf_fdpic.c | 3 ++- fs/exec.c | 4 ++++ include/linux/lsm_hooks.h | 7 +++++++ include/linux/security.h | 6 ++++++ mm/mmap.c | 13 +++++++++++++ security/security.c | 5 +++++ 7 files changed, 39 insertions(+), 2 deletions(-)
fs/binfmt_elf.c +2 -1
@@ -803,7 +803,8 @@ static int load_elf_binary(struct linux_binprm *bprm)for (i =0 ; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)switch (elf_ppnt->p_type) {case PT_GNU_STACK : -if (elf_ppnt->p_flags &PF_X ) +if (elf_ppnt->p_flags &PF_X && + !security_check_vmflags (VM_EXEC |VM_READ |VM_WRITE )) executable_stack =EXSTACK_ENABLE_X ;else executable_stack =EXSTACK_DISABLE_X ;
fs/binfmt_elf_fdpic.c +2 -1
@@ -167,7 +167,8 @@ static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,if (phdr->p_type !=PT_GNU_STACK )continue ; -if (phdr->p_flags &PF_X ) +if (phdr->p_flags &PF_X && + !security_check_vmflags (VM_EXEC |VM_READ |VM_WRITE )) params->flags |=ELF_FDPIC_FLAG_EXEC_STACK ;else params->flags |=ELF_FDPIC_FLAG_NOEXEC_STACK ;
fs/exec.c +4 -0
@@ -748,6 +748,10 @@ int setup_arg_pages(struct linux_binprm *bprm, vm_flags |= mm->def_flags; vm_flags |=VM_STACK_INCOMPLETE_SETUP ; + ret =security_check_vmflags (vm_flags); +if (ret) + goto out_unlock; + ret =mprotect_fixup (vma, &prev, vma->vm_start, vma->vm_end, vm_flags);if (ret)
include/linux/lsm_hooks.h +7 -0
@@ -484,6 +484,11 @@* @reqprot contains the protection requested by the application. * @prot contains the protection that will be applied by the kernel. * Return 0 if permission is granted. +* @check_vmflags: +* Check if the requested @vmflags are allowed. +* @vmflags contains the requested vmflags. +* Return 0 if the operation is allowed to continue otherwise return +* the appropriate error code. * @file_lock: * Check permission before performing file locking operations. * Note: this hook mediates both flock and fcntl style locks.
@@ -1525,6 +1530,7 @@ unsigned long prot, unsigned long flags);int (*file_mprotect)(struct vm_area_struct *vma,unsigned long reqprot,unsigned long prot); +int (*check_vmflags)(vm_flags_t vmflags);int (*file_lock)(struct file *file,unsigned int cmd);int (*file_fcntl)(struct file *file,unsigned int cmd,unsigned long arg);
@@ -1812,6 +1818,7 @@ struct security_hook_heads {struct list_head mmap_addr;struct list_head mmap_file;struct list_head file_mprotect; +struct list_head check_vmflags;struct list_head file_lock;struct list_head file_fcntl;struct list_head file_set_fowner;
include/linux/security.h +6 -0
@@ -311,6 +311,7 @@ int security_mmap_file(struct file *file, unsigned long prot,int security_mmap_addr (unsigned long addr);int security_file_mprotect (struct vm_area_struct *vma,unsigned long reqprot,unsigned long prot); +int security_check_vmflags (vm_flags_t vmflags);int security_file_lock (struct file *file,unsigned int cmd);int security_file_fcntl (struct file *file,unsigned int cmd,unsigned long arg);void security_file_set_fowner (struct file *file);
@@ -845,6 +846,11 @@ static inline int security_file_mprotect(struct vm_area_struct *vma,return 0 ; } +static inline int security_check_vmflags (vm_flags_t vmflags) +{ +return 0 ; +} +static inline int security_file_lock (struct file *file,unsigned int cmd) {return 0 ;
mm/mmap.c +13 -0
@@ -1326,6 +1326,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, {struct mm_struct *mm = current->mm;int pkey =0 ; +int error; *populate =0 ;
@@ -1378,6 +1379,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr, vm_flags |=calc_vm_prot_bits (prot, pkey) |calc_vm_flag_bits (flags) | mm->def_flags |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC ; + error =security_check_vmflags (vm_flags); +if (error) +return error; +if (flags &MAP_LOCKED )if (!can_do_mlock ())return -EPERM ;
@@ -2888,6 +2893,10 @@ static int do_brk_flags(unsigned long addr, unsigned long request, unsigned longreturn -EINVAL ; flags |=VM_DATA_DEFAULT_FLAGS |VM_ACCOUNT | mm->def_flags; + error =security_check_vmflags (flags); +if (error) +return error; + error =get_unmapped_area (NULL , addr, len,0 ,MAP_FIXED );if (offset_in_page (error))return error;
@@ -3284,6 +3293,10 @@ static struct vm_area_struct *__install_special_mapping(int ret;struct vm_area_struct *vma; + ret =security_check_vmflags (vm_flags); +if (ret) +return ERR_PTR (ret); + vma =kmem_cache_zalloc (vm_area_cachep,GFP_KERNEL );if (unlikely (vma ==NULL ))return ERR_PTR (-ENOMEM );
security/security.c +5 -0
@@ -939,6 +939,11 @@ int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,return call_int_hook (file_mprotect,0 , vma, reqprot, prot); } +int security_check_vmflags (vm_flags_t vmflags) +{ +return call_int_hook (check_vmflags,0 , vmflags); +} +int security_file_lock (struct file *file,unsigned int cmd) {return call_int_hook (file_lock,0 , file, cmd);
Signature · 1 line
1.9.1
RFC v4 4/10 S.A.R.A. cred blob management
In reply to Salvatore MesoracaCreation of the S.A.R.A. cred blob management "API".
In order to allow S.A.R.A. to be stackable with other LSMs, it doesn't use the "security" field of struct cred, instead it uses an ad hoc field named security_sara.
This solution is probably not acceptable for upstream, so this part will be modified as soon as the LSM stackable cred blob management will be available.
--- include/linux/cred.h | 3 ++ security/sara/Makefile | 2 +- security/sara/include/sara_data.h | 55 +++++++++++++++++++++++++++ security/sara/main.c | 6 +++ security/sara/sara_data.c | 79 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 security/sara/include/sara_data.h create mode 100644 security/sara/sara_data.c
include/linux/cred.h +3 -0
@@ -141,6 +141,9 @@ struct cred {#ifdef CONFIG_SECURITY void *security;/* subjective LSM security */ #endif +#ifdef CONFIG_SECURITY_SARA +void *security_sara; +#endif struct user_struct *user;/* real user ID subscription */ struct user_namespace *user_ns;/* user_ns the caps and keyrings are relative to. */ struct group_info *group_info;/* supplementary groups for euid/fsgid */
security/sara/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_SECURITY_SARA) := sara.o -sara-y := main.o securityfs.o utils.o +sara-y := main.o securityfs.o utils.o sara_data.o
security/sara/include/sara_data.h new file +55 -0
55 lines not on this page · raw message
security/sara/main.c +6 -0
@@ -15,6 +15,7 @@#include <linux/module.h> #include "include/sara.h" +#include "include/sara_data.h" #include "include/securityfs.h" static const int sara_version =SARA_VERSION ;
@@ -90,6 +91,11 @@ void __init sara_init(void) goto error; } +if (sara_data_init ()) { +pr_crit ("impossible to initialize creds.\n" ); + goto error; + } +pr_debug ("initialized.\n" );if (sara_enabled)
security/sara/sara_data.c new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC v4 5/10 S.A.R.A. WX Protection
In reply to Salvatore MesoracaIntroduction of S.A.R.A. WX Protection.
It aims to improve user-space programs security by applying:
- W^X enforcement
- W!->X (once writable never executable) mprotect restriction
- Executable MMAP prevention
All of the above features can be enabled or disabled both system wide or on a per executable basis through the use of configuration. W^X enforcement works by blocking any memory allocation or mprotect invocation with both the WRITE and the EXEC flags enabled. W!->X restriction works by preventing any mprotect invocation that makes executable any page that is flagged VM_MAYWRITE.
This feature can be configured separately for stack, heap and other allocations.
Executable MMAP prevention works by preventing any new executable allocation after the dynamic libraries have been loaded. It works under the assumption that, when the dynamic libraries have been finished loading, the RELRO section will be marked read only.
Parts of WX Protection are inspired by some of the features available in PaX according to my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code.
--- security/sara/Kconfig | 74 ++++ security/sara/Makefile | 1 + security/sara/include/utils.h | 11 + security/sara/include/wxprot.h | 27 ++ security/sara/main.c | 6 + security/sara/wxprot.c | 741 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 860 insertions(+) create mode 100644 security/sara/include/wxprot.h create mode 100644 security/sara/wxprot.c
security/sara/Kconfig +74 -0
@@ -38,3 +38,77 @@ config SECURITY_SARA_NO_RUNTIME_ENABLE If unsure, answer Y. +config SECURITY_SARA_WXPROT + bool "WX Protection: W^X and W!->X protections" + depends on SECURITY_SARA + default y + help + WX Protection aims to improve user-space programs security by applying: + - W^X memory restriction + - W!->X (once writable never executable) mprotect restriction + - Executable MMAP prevention + See Documentation/admin-guide/LSM/SARA.rst. for further information. + + If unsure, answer Y. + +choice + prompt "Default action for W^X and W!->X protections" + depends on SECURITY_SARA + depends on SECURITY_SARA_WXPROT + default SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_COMPLAIN_VERBOSE + + help + Choose the default behaviour of WX Protection when no config + rule matches or no rule is loaded. + For further information on available flags and their meaning + see Documentation/admin-guide/LSM/SARA.rst. + + config SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_COMPLAIN_VERBOSE + bool "Protections enabled but not enforced." + help + All features enabled except "Executable MMAP prevention", + verbose reporting, but no actual enforce: it just complains. + Its numeric value is 0x3f, for more information see + Documentation/admin-guide/LSM/SARA.rst. + + config SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_ENFORCE_VERBOSE + bool "Full protection, verbose." + help + All features enabled except "Executable MMAP prevention". + The enabled features will be enforced with verbose reporting. + Its numeric value is 0x2f, for more information see + Documentation/admin-guide/LSM/SARA.rst. + + config SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_ENFORCE + bool "Full protection, quiet." + help + All features enabled except "Executable MMAP prevention". + The enabled features will be enforced quietly. + Its numeric value is 0xf, for more information see + Documentation/admin-guide/LSM/SARA.rst. + + config SECURITY_SARA_WXPROT_DEFAULT_FLAGS_NONE + bool "No protection at all." + help + All features disabled. + Its numeric value is 0, for more information see + Documentation/admin-guide/LSM/SARA.rst. +endchoice + +config SECURITY_SARA_WXPROT_DISABLED + bool "WX protection will be disabled at boot." + depends on SECURITY_SARA_WXPROT + default n + help + If you say Y here WX protection won't be enabled at startup. You can + override this option via user-space utilities or at boot time via + "sara.wxprot_enabled=[0|1]" kernel parameter. + + If unsure, answer N. + +config SECURITY_SARA_WXPROT_DEFAULT_FLAGS + hex + default "0x3f" if SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_COMPLAIN_VERBOSE + default "0x2f" if SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_ENFORCE_VERBOSE + default "0xf" if SECURITY_SARA_WXPROT_DEFAULT_FLAGS_ALL_ENFORCE + default "0" if SECURITY_SARA_WXPROT_DEFAULT_FLAGS_NONE
security/sara/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-$(CONFIG_SECURITY_SARA) := sara.o sara-y := main.o securityfs.o utils.o sara_data.o +sara-$(CONFIG_SECURITY_SARA_WXPROT) += wxprot.o
security/sara/include/utils.h +11 -0
@@ -26,6 +26,17 @@ static inline void release_entry(struct kref *ref)/* All work is done after the return from kref_put(). */ } + +/* +* The following macros must be used to access S.A.R.A. configuration +* structures. +* They are thread-safe under the assumption that a configuration +* won't ever be deleted but just replaced using SARA_CONFIG_REPLACE, +* possibly using an empty configuration. +* i.e. every call to SARA_CONFIG_PUT *must* be preceded by a matching +* SARA_CONFIG_GET invocation. +*/ +#define SARA_CONFIG_GET_RCU (DEST ,CONFIG ) do { \ rcu_read_lock(); \ DEST = rcu_dereference(CONFIG); \
security/sara/include/wxprot.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* S.A.R.A. Linux Security Module +* +* Copyright (C) 2017 Salvatore Mesoraca <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +* +*/ + +#ifndef __SARA_WXPROT_H +#define __SARA_WXPROT_H + +#ifdef CONFIG_SECURITY_SARA_WXPROT + +#include <linux/init.h> +int sara_wxprot_init (void )__init ; + +#else /* CONFIG_SECURITY_SARA_WXPROT */ +inline int sara_wxprot_init (void ) +{ +return 0 ; +} +#endif /* CONFIG_SECURITY_SARA_WXPROT */ + +#endif /* __SARA_WXPROT_H */
security/sara/main.c +6 -0
@@ -17,6 +17,7 @@#include "include/sara.h" #include "include/sara_data.h" #include "include/securityfs.h" +#include "include/wxprot.h" static const int sara_version =SARA_VERSION ;
@@ -96,6 +97,11 @@ void __init sara_init(void) goto error; } +if (sara_wxprot_init ()) { +pr_crit ("impossible to initialize WX protections.\n" ); + goto error; + } +pr_debug ("initialized.\n" );if (sara_enabled)
security/sara/wxprot.c new file +741 -0
741 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC v4 6/10 Creation of "pagefault_handler" LSM hook
In reply to Salvatore MesoracaCreation of a new hook to let LSM modules handle user-space pagefaults on x86.
It can be used to avoid segfaulting the originating process. If it's the case it can modify process registers before returning. This is not a security feature by itself, it's a way to soften some unwanted side-effects of restrictive security features.
In particular this is used by S.A.R.A. to implement what PaX call "trampoline emulation" that, in practice, allows for some specific code sequences to be executed even if they are in non executable memory. This may look like a bad thing at first, but you have to consider that:
- This allows for strict memory restrictions (e.g. W^X) to stay on even when they should be turned off. And, even if this emulation makes those features less effective, it's still better than having them turned off completely.
- The only code sequences emulated are trampolines used to make function calls. In many cases, when you have the chance to make arbitrary memory writes, you can already manipulate the control flow of the program by overwriting function pointers or return values. So, in many cases, "trampoline emulation" doesn't introduce new exploit vectors.
- It's a feature that can be turned on only if needed, on a per executable file basis.
--- arch/Kconfig | 6 ++++++ arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 6 ++++++ include/linux/lsm_hooks.h | 12 ++++++++++++ include/linux/security.h | 11 +++++++++++ security/security.c | 11 +++++++++++ 6 files changed, 47 insertions(+)
arch/Kconfig +6 -0
@@ -230,6 +230,12 @@ config ARCH_HAS_FORTIFY_SOURCE An architecture should select this when it can successfully build and run with CONFIG_FORTIFY_SOURCE. +config ARCH_HAS_LSM_PAGEFAULT + bool + help + An architecture should select this if it supports + "pagefault_handler" LSM hook. + # Select if arch has all set_memory_ro/rw/x/nx() functions in asm/cacheflush.h config ARCH_HAS_SET_MEMORY bool
arch/x86/Kconfig +1 -0
@@ -54,6 +54,7 @@ config X86 select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_KCOV if X86_64 + select ARCH_HAS_LSM_PAGEFAULT select ARCH_HAS_PMEM_API if X86_64 # Causing hangs/crashes, see the commit that added this change for details. select ARCH_HAS_REFCOUNT
arch/x86/mm/fault.c +6 -0
@@ -16,6 +16,7 @@#include <linux/prefetch.h> /* prefetchw */ #include <linux/context_tracking.h> /* exception_enter(), ... */ #include <linux/uaccess.h> /* faulthandler_disabled() */ +#include <linux/security.h> /* security_pagefault_handler */ #include <asm/cpufeature.h> /* boot_cpu_has, ... */ #include <asm/traps.h> /* dotraplinkage, ... */
@@ -1332,6 +1333,11 @@ static inline bool smap_violation(int error_code, struct pt_regs *regs)local_irq_enable (); } +if (unlikely (security_pagefault_handler (regs, + error_code, + address))) +return ; +perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);if (error_code &X86_PF_WRITE )
include/linux/lsm_hooks.h +12 -0
@@ -489,6 +489,14 @@* @vmflags contains the requested vmflags. * Return 0 if the operation is allowed to continue otherwise return * the appropriate error code. +* @pagefault_handler: +* Handle pagefaults on supported architectures, that is any architecture +* which defines CONFIG_ARCH_HAS_LSM_PAGEFAULT. +* @regs contains process' registers. +* @error_code contains error code for the pagefault. +* @address contains the address that caused the pagefault. +* Return 0 to let the kernel handle the pagefault as usually, any other +* value to let the process continue its execution. * @file_lock: * Check permission before performing file locking operations. * Note: this hook mediates both flock and fcntl style locks.
@@ -1531,6 +1539,9 @@int (*file_mprotect)(struct vm_area_struct *vma,unsigned long reqprot,unsigned long prot);int (*check_vmflags)(vm_flags_t vmflags); +int (*pagefault_handler)(struct pt_regs *regs, +unsigned long error_code, +unsigned long address);int (*file_lock)(struct file *file,unsigned int cmd);int (*file_fcntl)(struct file *file,unsigned int cmd,unsigned long arg);
@@ -1819,6 +1830,7 @@ struct security_hook_heads {struct list_head mmap_file;struct list_head file_mprotect;struct list_head check_vmflags; +struct list_head pagefault_handler;struct list_head file_lock;struct list_head file_fcntl;struct list_head file_set_fowner;
include/linux/security.h +11 -0
@@ -312,6 +312,9 @@ int security_mmap_file(struct file *file, unsigned long prot,int security_file_mprotect (struct vm_area_struct *vma,unsigned long reqprot,unsigned long prot);int security_check_vmflags (vm_flags_t vmflags); +int __maybe_unused security_pagefault_handler (struct pt_regs *regs, +unsigned long error_code, +unsigned long address);int security_file_lock (struct file *file,unsigned int cmd);int security_file_fcntl (struct file *file,unsigned int cmd,unsigned long arg);void security_file_set_fowner (struct file *file);
@@ -851,6 +854,14 @@ static inline int security_check_vmflags(vm_flags_t vmflags)return 0 ; } +static inline int __maybe_unused security_pagefault_handler ( +struct pt_regs *regs, +unsigned long error_code, +unsigned long address) +{ +return 0 ; +} +static inline int security_file_lock (struct file *file,unsigned int cmd) {return 0 ;
security/security.c +11 -0
@@ -944,6 +944,17 @@ int security_check_vmflags(vm_flags_t vmflags)return call_int_hook (check_vmflags,0 , vmflags); } +int __maybe_unused security_pagefault_handler (struct pt_regs *regs, +unsigned long error_code, +unsigned long address) +{ +return call_int_hook (pagefault_handler, +0 , + regs, + error_code, + address); +} +int security_file_lock (struct file *file,unsigned int cmd) {return call_int_hook (file_lock,0 , file, cmd);
Signature · 1 line
1.9.1
RFC v4 7/10 Trampoline emulation
In reply to Salvatore MesoracaSome programs need to generate part of their code at runtime. Luckily enough, in some cases they only generate well-known code sequences (the "trampolines") that can be easily recognized and emulated by the kernel. This way WX Protection can still be active, so a potential attacker won't be able to generate arbitrary sequences of code, but just those that are explicitly allowed. This is not ideal, but it's still better than having WX Protection completely disabled.
In particular S.A.R.A. is able to recognize trampolines used by GCC for nested C functions and libffi's trampolines.
This feature is implemented only on x86_32 and x86_64. Trampoline emulation is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code.
--- arch/x86/Kbuild | 2 + arch/x86/security/Makefile | 2 + arch/x86/security/sara/Makefile | 1 + arch/x86/security/sara/emutramp.c | 55 ++++++++++++ arch/x86/security/sara/trampolines32.h | 122 +++++++++++++++++++++++++++ arch/x86/security/sara/trampolines64.h | 148 +++++++++++++++++++++++++++++++++ security/sara/Kconfig | 18 ++++ security/sara/include/emutramp.h | 33 ++++++++ security/sara/wxprot.c | 29 +++++++ 9 files changed, 410 insertions(+) create mode 100644 arch/x86/security/Makefile create mode 100644 arch/x86/security/sara/Makefile create mode 100644 arch/x86/security/sara/emutramp.c create mode 100644 arch/x86/security/sara/trampolines32.h create mode 100644 arch/x86/security/sara/trampolines64.h create mode 100644 security/sara/include/emutramp.h
arch/x86/Kbuild +2 -0
@@ -22,3 +22,5 @@ obj-y += platform/ obj-y += net/ obj-$(CONFIG_KEXEC_FILE) += purgatory/ + +obj-y += security/
arch/x86/security/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +subdir-$(CONFIG_SECURITY_SARA) += sara +obj-$(CONFIG_SECURITY_SARA) += sara/
arch/x86/security/sara/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP) := emutramp.o
arch/x86/security/sara/emutramp.c new file +55 -0
55 lines not on this page · raw message
arch/x86/security/sara/trampolines32.h new file +122 -0
122 lines not on this page · raw message
arch/x86/security/sara/trampolines64.h new file +148 -0
148 lines not on this page · raw message
security/sara/Kconfig +18 -0
@@ -95,6 +95,24 @@ choice Documentation/admin-guide/LSM/SARA.rst. endchoice +config SECURITY_SARA_WXPROT_EMUTRAMP + bool "Enable emulation for some types of trampolines" + depends on SECURITY_SARA_WXPROT + depends on ARCH_HAS_LSM_PAGEFAULT + depends on X86 + default y + help + Some programs and libraries need to execute special small code + snippets from non-executable memory pages. + Most notable examples are the GCC and libffi trampolines. + This features make it possible to execute those trampolines even + if they reside in non-executable memory pages. + This features need to be enabled on a per-executable basis + via user-space utilities. + See Documentation/admin-guide/LSM/SARA.rst. for further information. + + If unsure, answer y. + config SECURITY_SARA_WXPROT_DISABLED bool "WX protection will be disabled at boot." depends on SECURITY_SARA_WXPROT
security/sara/include/emutramp.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* S.A.R.A. Linux Security Module +* +* Copyright (C) 2017 Salvatore Mesoraca <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +* +* Assembly sequences used here were copied from +* PaX patch by PaX Team <[email protected]> +* Being just hexadecimal constants, they are not subject to +* any copyright. +* +*/ + +#ifndef __EMUTRAMP_H +#define __EMUTRAMP_H + +#ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP +int sara_trampoline_emulator (struct pt_regs *regs, +unsigned long error_code, +unsigned long address); +#else +inline int sara_trampoline_emulator (struct pt_regs *regs, +unsigned long error_code, +unsigned long address) +{ +return 0 ; +} +#endif /* CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP */ + +#endif /* __EMUTRAMP_H */
security/sara/wxprot.c +29 -0
@@ -28,6 +28,7 @@#include "include/utils.h" #include "include/securityfs.h" #include "include/wxprot.h" +#include "include/emutramp.h" #define SARA_WXPROT_CONFIG_VERSION 0
@@ -38,6 +39,7 @@#define SARA_WXP_COMPLAIN 0x0010#define SARA_WXP_VERBOSE 0x0020#define SARA_WXP_MMAP 0x0040 +#define SARA_WXP_EMUTRAMP 0x0100#define SARA_WXP_TRANSFER 0x0200#define SARA_WXP_NONE 0x0000#define SARA_WXP_MPROTECT (SARA_WXP_HEAP | \
@@ -48,7 +50,12 @@ SARA_WXP_WXORX | \ SARA_WXP_COMPLAIN | \ SARA_WXP_VERBOSE) +#ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP +#define SARA_WXP_ALL (__SARA_WXP_ALL | \ + SARA_WXP_EMUTRAMP) +#else /* CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP */ #define SARA_WXP_ALL __SARA_WXP_ALL +#endif /* CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP */ struct wxprot_rule {char *path;
@@ -73,7 +80,11 @@ struct wxprot_config_container {static u16 default_flags__ro_after_init =CONFIG_SECURITY_SARA_WXPROT_DEFAULT_FLAGS ; +#ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP +static const bool wxprot_emutramp = true; +#else static const bool wxprot_emutramp; +#endif static void pr_wxp (char *msg) {
@@ -116,6 +127,9 @@ static bool are_flags_valid(u16 flags)if (unlikely (flags &SARA_WXP_MMAP && !(flags &SARA_WXP_OTHER )))return false; +if (unlikely (flags &SARA_WXP_EMUTRAMP && + ((flags &SARA_WXP_MPROTECT ) !=SARA_WXP_MPROTECT ))) +return false;return true; }
@@ -461,10 +475,25 @@ static int sara_file_mprotect(struct vm_area_struct *vma,return 0 ; } +#ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP +static int sara_pagefault_handler (struct pt_regs *regs, +unsigned long error_code, +unsigned long address) +{ +if (!sara_enabled || !wxprot_enabled || + !(get_current_sara_wxp_flags () &SARA_WXP_EMUTRAMP )) +return 0 ; +return sara_trampoline_emulator (regs, error_code, address); +} +#endif +static struct security_hook_list wxprot_hooks[]__ro_after_init = {LSM_HOOK_INIT (bprm_set_creds, sara_bprm_set_creds),LSM_HOOK_INIT (check_vmflags, sara_check_vmflags),LSM_HOOK_INIT (file_mprotect, sara_file_mprotect), +#ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP +LSM_HOOK_INIT (pagefault_handler, sara_pagefault_handler), +#endif };struct binary_config_header {
Signature · 1 line
1.9.1
RFC v4 8/10 Allowing for stacking procattr support in S.A.R.A.
In reply to Salvatore MesoracaThis allow S.A.R.A. to use the procattr interface without interfering with other LSMs.
This part should be reimplemented as soon as upstream procattr stacking support is available.
--- fs/proc/base.c | 38 ++++++++++++++++++++++++++++++++++++++ security/security.c | 20 ++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-)
fs/proc/base.c +38 -0
@@ -2560,6 +2560,40 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, .llseek = generic_file_llseek, }; +#ifdef CONFIG_SECURITY_SARA +static const struct pid_entry sara_attr_dir_stuff[] = { +REG ("wxprot" ,0666 , proc_pid_attr_operations), +}; + +static int proc_sara_attr_dir_readdir (struct file *file, +struct dir_context *ctx) +{ +return proc_pident_readdir (file, ctx, + sara_attr_dir_stuff, +ARRAY_SIZE (sara_attr_dir_stuff)); +} + +static const struct file_operations proc_sara_attr_dir_ops = { + .read = generic_read_dir, + .iterate_shared = proc_sara_attr_dir_readdir, + .llseek = generic_file_llseek, +}; + +static struct dentry *proc_sara_attr_dir_lookup (struct inode *dir, +struct dentry *dentry,unsigned int flags) +{ +return proc_pident_lookup (dir, dentry, + sara_attr_dir_stuff, +ARRAY_SIZE (sara_attr_dir_stuff)); +}; + +static const struct inode_operations proc_sara_attr_dir_inode_ops = { + .lookup = proc_sara_attr_dir_lookup, + .getattr = pid_getattr, + .setattr = proc_setattr, +}; +#endif /* CONFIG_SECURITY_SARA */ +static const struct pid_entry attr_dir_stuff[] = {REG ("current" ,S_IRUGO |S_IWUGO , proc_pid_attr_operations),REG ("prev" ,S_IRUGO , proc_pid_attr_operations),
@@ -2567,6 +2601,10 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,REG ("fscreate" ,S_IRUGO |S_IWUGO , proc_pid_attr_operations),REG ("keycreate" ,S_IRUGO |S_IWUGO , proc_pid_attr_operations),REG ("sockcreate" ,S_IRUGO |S_IWUGO , proc_pid_attr_operations), +#ifdef CONFIG_SECURITY_SARA +DIR ("sara" ,0555 , proc_sara_attr_dir_inode_ops, + proc_sara_attr_dir_ops), +#endif };static int proc_attr_dir_readdir (struct file *file,struct dir_context *ctx)
security/security.c +18 -2
@@ -1273,12 +1273,28 @@ void security_d_instantiate(struct dentry *dentry, struct inode *inode)int security_getprocattr (struct task_struct *p,char *name,char **value) { -return call_int_hook (getprocattr, -EINVAL , p, name, value); +struct security_hook_list *hp; +int rc; + +list_for_each_entry (hp, &security_hook_heads.getprocattr, list) { + rc = hp->hook.getprocattr (p, name, value); +if (rc != -EINVAL ) +return rc; + } +return -EINVAL ; }int security_setprocattr (const char *name,void *value,size_t size) { -return call_int_hook (setprocattr, -EINVAL , name, value, size); +struct security_hook_list *hp; +int rc; + +list_for_each_entry (hp, &security_hook_heads.setprocattr, list) { + rc = hp->hook.setprocattr (name, value, size); +if (rc != -EINVAL ) +return rc; + } +return -EINVAL ; }int security_netlink_send (struct sock *sk,struct sk_buff *skb)
Signature · 1 line
1.9.1
RFC v4 9/10 S.A.R.A. WX Protection procattr interface
In reply to Salvatore MesoracaThis allow threads to get current WX Protection flags for themselves or for other threads (if they have CAP_MAC_ADMIN).
It also allow a thread to set itself flags to a stricter set of rules than the current one.
Via a new wxprot flag (SARA_WXP_FORCE_WXORX) is it possible to ask the kernel to rescan the memory and remove the VM_WRITE flag from any area that is marked both writable and executable.
Protections that prevent the runtime creation of executable code can be troublesome for all those programs that actually need to do it e.g. programs shipping with a JIT compiler built-in.
This feature can be use to run the JIT compiler with few restrictions while enforcing full WX Protection in the rest of the program.
To simplify access to this interface a CC0 licensed library is available
here: https://github.com/smeso/libsara
--- security/sara/wxprot.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+)
security/sara/wxprot.c +150 -0
@@ -12,6 +12,7 @@#ifdef CONFIG_SECURITY_SARA_WXPROT #include <linux/binfmts.h> +#include <linux/capability.h> #include <linux/cred.h> #include <linux/elf.h> #include <linux/kref.h>
@@ -39,6 +40,7 @@#define SARA_WXP_COMPLAIN 0x0010#define SARA_WXP_VERBOSE 0x0020#define SARA_WXP_MMAP 0x0040 +#define SARA_WXP_FORCE_WXORX 0x0080#define SARA_WXP_EMUTRAMP 0x0100#define SARA_WXP_TRANSFER 0x0200#define SARA_WXP_NONE 0x0000
@@ -487,6 +489,152 @@ static int sara_pagefault_handler(struct pt_regs *regs, }#endif +static int sara_getprocattr (struct task_struct *p,char *name,char **value) +{ +int ret; +u16 flags; +char *buf; + + ret = -EINVAL ; +if (strcmp (name,"wxprot" ) !=0 ) + goto out; + + ret = -EACCES ; +if (unlikely (current != p && + !capable (CAP_MAC_ADMIN ))) + goto out; + + ret = -ENOMEM ; + buf =kzalloc (8 ,GFP_KERNEL ); +if (unlikely (buf ==NULL )) + goto out; + +if (!sara_enabled || !wxprot_enabled) { + flags =0x0 ; + }else { +rcu_read_lock (); + flags =get_sara_wxp_flags (__task_cred (p)); +rcu_read_unlock (); + } + +snprintf (buf,8 ,"0x%04x\n" , flags); + ret =strlen (buf); + *value = buf; + +out: +return ret; +} + +static int sara_setprocattr (const char *name,void *value,size_t size) +{ +int ret; +struct vm_area_struct *vma; +struct cred *new =prepare_creds (); +u16 cur_flags; +u16 req_flags; +char *buf =NULL ; + + ret = -EINVAL ; +if (!sara_enabled || !wxprot_enabled) + goto error; +if (unlikely (new ==NULL )) +return -ENOMEM ; +if (strcmp (name,"wxprot" ) !=0 ) + goto error; +if (unlikely (value ==NULL || size ==0 || size >7 )) + goto error; + ret = -ENOMEM ; + buf =kmalloc (size+1 ,GFP_KERNEL ); +if (unlikely (buf ==NULL )) + goto error; + buf[size] ='\0' ; +memcpy (buf, value, size); + ret = -EINVAL ; +if (unlikely (strlen (buf) != size)) + goto error; +if (unlikely (kstrtou16 (buf,0 , &req_flags) !=0 )) + goto error; +/* +* SARA_WXP_FORCE_WXORX is a procattr only flag with a special +* meaning and it isn't recognized by are_flags_valid +*/ +if (unlikely (!are_flags_valid (req_flags & ~SARA_WXP_FORCE_WXORX ))) + goto error; +/* +* Extra checks on requested flags: +* - SARA_WXP_FORCE_WXORX requires SARA_WXP_WXORX +* - SARA_WXP_MMAP can only be activated if the program +* has a relro section +* - COMPLAIN mode can only be requested if it was already +* on (procattr can only be used to make protection stricter) +* - EMUTRAMP can only be activated if it was already on or +* if MPROTECT and WXORX weren't already on (procattr can +* only be used to make protection stricter) +* - VERBOSITY request is ignored +*/ +if (unlikely (req_flags &SARA_WXP_FORCE_WXORX && + !(req_flags &SARA_WXP_WXORX ))) + goto error; +if (unlikely (!get_current_sara_relro_page_found () && + req_flags &SARA_WXP_MMAP )) + goto error; + cur_flags =get_current_sara_wxp_flags (); +if (unlikely ((req_flags &SARA_WXP_COMPLAIN ) && + !(cur_flags &SARA_WXP_COMPLAIN ))) + goto error; +if (unlikely ((req_flags &SARA_WXP_EMUTRAMP ) && + !(cur_flags &SARA_WXP_EMUTRAMP ) && + (cur_flags & (SARA_WXP_MPROTECT | +SARA_WXP_WXORX )))) + goto error; +if (cur_flags &SARA_WXP_VERBOSE ) + req_flags |=SARA_WXP_VERBOSE ; +else + req_flags &= ~SARA_WXP_VERBOSE ; +/* +* Except SARA_WXP_COMPLAIN and SARA_WXP_EMUTRAMP, +* any other flag can't be removed (procattr can +* only be used to make protection stricter). +*/ +if (unlikely (cur_flags & (req_flags ^ cur_flags) & + ~(SARA_WXP_COMPLAIN |SARA_WXP_EMUTRAMP ))) + goto error; + ret = -EINTR ; +/* +* When SARA_WXP_FORCE_WXORX is on we traverse all the +* memory and remove the write permission from any area +* that is both writable and executable. +*/ +if (req_flags &SARA_WXP_FORCE_WXORX ) { +if (down_write_killable (¤t->mm->mmap_sem)) + goto error; +for (vma = current->mm->mmap; vma; vma = vma->vm_next) { +if (vma->vm_flags &VM_EXEC && + vma->vm_flags &VM_WRITE ) { + vma->vm_flags &= ~VM_WRITE ; +vma_set_page_prot (vma); +change_protection (vma, + vma->vm_start, + vma->vm_end, + vma->vm_page_prot, +0 , +0 ); + } + } +up_write (¤t->mm->mmap_sem); + } +get_sara_wxp_flags (new) = req_flags & ~SARA_WXP_FORCE_WXORX ; +commit_creds (new); + ret = size; + goto out; + +error: +abort_creds (new); +out: +kfree (buf); +return ret; +} +static struct security_hook_list wxprot_hooks[]__ro_after_init = {LSM_HOOK_INIT (bprm_set_creds, sara_bprm_set_creds),LSM_HOOK_INIT (check_vmflags, sara_check_vmflags),
@@ -494,6 +642,8 @@ static int sara_pagefault_handler(struct pt_regs *regs, #ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP LSM_HOOK_INIT(pagefault_handler, sara_pagefault_handler), #endif + LSM_HOOK_INIT(getprocattr, sara_getprocattr), + LSM_HOOK_INIT(setprocattr, sara_setprocattr), }; struct binary_config_header {
Signature · 1 line
1.9.1
RFC v4 10/10 XATTRs support
In reply to Salvatore MesoracaAdds support for extended filesystem attributes in security and user namespaces. They can be used to override flags set via the centralized configuration, even when S.A.R.A. configuration is locked or saractl is not used at all.
--- Documentation/admin-guide/LSM/SARA.rst | 20 +++++ Documentation/admin-guide/kernel-parameters.txt | 16 ++++ include/uapi/linux/xattr.h | 4 + security/sara/Kconfig | 22 ++++++ security/sara/wxprot.c | 97 +++++++++++++++++++++++++ 5 files changed, 159 insertions(+)
Documentation/admin-guide/LSM/SARA.rst +20 -0
@@ -53,6 +53,8 @@ WX Protection. In particular: To extend the scope of the above features, despite the issues that they may cause, they are complemented by **/proc/PID/attr/sara/wxprot** interface and **trampoline emulation**. +It's also possible to override the centralized configuration via `Extended +filesystem attributes`_. At the moment, WX Protection (unless specified otherwise) should work on any architecture supporting the NX bit, including, but not limited to:
@@ -119,6 +121,24 @@ in your project or copy/paste parts of it. To make things simpler `libsara` is the only part of S.A.R.A. released under *CC0 - No Rights Reserved* license. +Extended filesystem attributes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +When this functionality is enabled, it's possible to override +WX Protection flags set in the main configuration via extended attributes, +even when S.A.R.A.'s configuration is in "locked" mode. +If the user namespace is also enabled, its attributes will override settings +configured via the security namespace. +The xattrs currently in use are: + +- security.sara.wxprot +- user.sara.wxprot + +They can be manually set to the desired value as a decimal, hexadecimal or +octal number. When this functionality is enabled, S.A.R.A. can be easily used +without the help of its userspace tools. Though the preferred way to change +these attributes is `sara-xattr` which is part of `saractl` [2]_. + + Trampoline emulation ^^^^^^^^^^^^^^^^^^^^ Some programs need to generate part of their code at runtime. Luckily enough,
Documentation/admin-guide/kernel-parameters.txt +16 -0
@@ -3841,6 +3841,22 @@ See S.A.R.A. documentation. Default value is set via kernel config option. + sara.wxprot_xattrs_enabled= [SARA] + Enable support for security xattrs. + Format: { "0" | "1" } + See security/sara/Kconfig help text + 0 -- disable. + 1 -- enable. + Default value is set via kernel config option. + + sara.wxprot_xattrs_user= [SARA] + Enable support for user xattrs. + Format: { "0" | "1" } + See security/sara/Kconfig help text + 0 -- disable. + 1 -- enable. + Default value is set via kernel config option. + serialnumber [BUGS=X86-32] shapers= [NET]
include/uapi/linux/xattr.h +4 -0
@@ -77,5 +77,9 @@#define XATTR_POSIX_ACL_DEFAULT "posix_acl_default"#define XATTR_NAME_POSIX_ACL_DEFAULT XATTR_SYSTEM_PREFIX XATTR_POSIX_ACL_DEFAULT +#define XATTR_SARA_SUFFIX "sara." +#define XATTR_SARA_WXP_SUFFIX XATTR_SARA_SUFFIX "wxp" +#define XATTR_NAME_SEC_SARA_WXP XATTR_SECURITY_PREFIX XATTR_SARA_WXP_SUFFIX +#define XATTR_NAME_USR_SARA_WXP XATTR_USER_PREFIX XATTR_SARA_WXP_SUFFIX#endif /* _UAPI_LINUX_XATTR_H */
security/sara/Kconfig +22 -0
@@ -113,6 +113,28 @@ config SECURITY_SARA_WXPROT_EMUTRAMP If unsure, answer y. +config SECURITY_SARA_WXPROT_XATTRS_ENABLED + bool "xattrs support enabled by default." + depends on SECURITY_SARA_WXPROT + default n + help + If you say Y here it will be possible to override WX protection + configuration via extended attributes in the security namespace. + Even when S.A.R.A.'s configuration has been locked. + + If unsure, answer N. + +config CONFIG_SECURITY_SARA_WXPROT_XATTRS_USER + bool "'user' namespace xattrs support enabled by default." + depends on SECURITY_SARA_WXPROT_XATTRS_ENABLED + default n + help + If you say Y here it will be possible to override WX protection + configuration via extended attributes in the user namespace. + Even when S.A.R.A.'s configuration has been locked. + + If unsure, answer N. + config SECURITY_SARA_WXPROT_DISABLED bool "WX protection will be disabled at boot." depends on SECURITY_SARA_WXPROT
security/sara/wxprot.c +97 -0
145 lines not on this page · raw message
Signature · 1 line
1.9.1