S.A.R.A. a new stacked LSM
Subsystems (9)
v5 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.
Changes in v5:
- Updated the code to use the new stacking interface.
- Path matching is now done using a DFA
Salvatore Mesoraca (12):
S.A.R.A.: add documentation
S.A.R.A.: create framework
S.A.R.A.: cred blob management
S.A.R.A.: generic DFA for string matching
LSM: creation of "check_vmflags" LSM hook
S.A.R.A.: WX protection
LSM: creation of "pagefault_handler" LSM hook
S.A.R.A.: trampoline emulation
S.A.R.A.: WX protection procattr interface
S.A.R.A.: XATTRs support
S.A.R.A.: /proc/*/mem write limitation
MAINTAINERS: take maintainership for S.A.R.A.
Documentation/admin-guide/LSM/SARA.rst | 197 +++++ Documentation/admin-guide/LSM/index.rst | 1 + Documentation/admin-guide/kernel-parameters.txt | 40 + MAINTAINERS | 9 + 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 | 57 ++ arch/x86/security/sara/trampolines32.h | 137 ++++ arch/x86/security/sara/trampolines64.h | 164 ++++ fs/binfmt_elf.c | 3 +- fs/binfmt_elf_fdpic.c | 3 +- fs/exec.c | 4 + fs/proc/base.c | 11 + include/linux/lsm_hooks.h | 19 + include/linux/security.h | 17 + include/uapi/linux/xattr.h | 4 + mm/mmap.c | 13 + security/Kconfig | 11 +- security/Makefile | 2 + security/sara/Kconfig | 176 +++++ security/sara/Makefile | 5 + security/sara/dfa.c | 335 ++++++++ security/sara/dfa_test.c | 135 ++++ security/sara/include/dfa.h | 52 ++ security/sara/include/dfa_test.h | 29 + security/sara/include/emutramp.h | 35 + security/sara/include/sara.h | 29 + security/sara/include/sara_data.h | 100 +++ security/sara/include/securityfs.h | 61 ++ security/sara/include/utils.h | 80 ++ security/sara/include/wxprot.h | 29 + security/sara/main.c | 134 ++++ security/sara/sara_data.c | 77 ++ security/sara/securityfs.c | 565 ++++++++++++++ security/sara/utils.c | 92 +++ security/sara/wxprot.c | 998 ++++++++++++++++++++++++ security/security.c | 16 + 41 files changed, 3651 insertions(+), 7 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/dfa.c create mode 100644 security/sara/dfa_test.c create mode 100644 security/sara/include/dfa.h create mode 100644 security/sara/include/dfa_test.h 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
PATCH v5 1/12 S.A.R.A.: add documentation
Adding documentation for S.A.R.A. LSM.
--- Documentation/admin-guide/LSM/SARA.rst | 177 ++++++++++++++++++++++++ Documentation/admin-guide/LSM/index.rst | 1 + Documentation/admin-guide/kernel-parameters.txt | 24 ++++ 3 files changed, 202 insertions(+) create mode 100644 Documentation/admin-guide/LSM/SARA.rst
Documentation/admin-guide/LSM/SARA.rst new file +177 -0
@@ -0,0 +1,177 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======== +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. +This feature also blocks accesses to /proc/*/mem files that would allow to +write the current process read-only memory, bypassing any protection. + +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
@@ -47,3 +47,4 @@ subdirectories. tomoyo Yama SafeSetID + SARA
Documentation/admin-guide/kernel-parameters.txt +24 -0
@@ -4230,6 +4230,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
Randy Dunlap
reviewerRe: [PATCH v5 01/12] S.A.R.A.: add documentation
Hi,
Just a few typo fixes (inline).
Quoted 77 lines · Salvatore Mesoraca
Adding documentation for S.A.R.A. LSM.
Signed-off-by Salvatore Mesoraca <[email protected]>--- Documentation/admin-guide/LSM/SARA.rst | 177 ++++++++++++++++++++++++ Documentation/admin-guide/LSM/index.rst | 1 + Documentation/admin-guide/kernel-parameters.txt | 24 ++++ 3 files changed, 202 insertions(+) create mode 100644 Documentation/admin-guide/LSM/SARA.rstDocumentation/admin-guide/LSM/SARA.rst new file +61
-0@@ -0,0 +1,177 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======== +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`.
Documentation/admin-guide/LSM/SARA.rst line 71
Quoted 10 lines
Documentation/admin-guide/LSM/SARA.rst new file +10
-0+ +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
allows
Quoted 17 lines
Documentation/admin-guide/LSM/SARA.rst new file +17
-0+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. +This feature also blocks accesses to /proc/*/mem files that would allow to +write the current process read-only memory, bypassing any protection. + +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.
Documentation/admin-guide/LSM/SARA.rst line 98
Quoted 10 lines
Documentation/admin-guide/LSM/SARA.rst new file +10
-0+ +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.
LSMs
Documentation/admin-guide/LSM/SARA.rst line 107
Quoted 9 lines
Documentation/admin-guide/LSM/SARA.rst new file +9
-0+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
with a
Documentation/admin-guide/LSM/SARA.rst line 112
Quoted 5 lines
Documentation/admin-guide/LSM/SARA.rst new file +5
-0+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.
[snip]
cheers.
Signature · 1 line
~Randy
Re: [PATCH v5 01/12] S.A.R.A.: add documentation
Quoted 4 lines · Randy Dunlap
Hi,
Just a few typo fixes (inline).
Hi Randy,
thank you for your help!
I'll address these and the other fixes in the next version of the patchset.
Best,
Salvatore
James Morris
maintainerRe: [PATCH v5 01/12] S.A.R.A.: add documentation
In reply to Salvatore MesoracaQuoted 1 line · Salvatore Mesoraca
Adding documentation for S.A.R.A. LSM.
It would be good if you could add an operational overview to help people understand how it works in practice, e.g. setting policies for binaries via sara-xattr and global config via saractl (IIUC). It's difficult to understand if you have to visit several links to piece things together.
Quoted 4 lines
Documentation/admin-guide/LSM/SARA.rst new file +4
-0+S.A.R.A.'s Submodules +===================== + +WX Protection
Documentation/admin-guide/LSM/SARA.rst line 36
Quoted 10 lines
Documentation/admin-guide/LSM/SARA.rst new file +10
-0+------------- +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]_.
How complete is the WX protection provided by this module? How does it compare with other implementations (such as PaX's restricted mprotect).
Quoted 1 line
+Parts of WX Protection are inspired by some of the features available in PaX.
Some critical aspects are copied (e.g. trampoline emulation), so it's more than just inspired. Could you include more information in the description about what's been ported from PaX to SARA?
Signature · 2 lines
James Morris <[email protected]>
PATCH v5 2/12 S.A.R.A.: create framework
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.
--- security/Kconfig | 11 +- security/Makefile | 2 + security/sara/Kconfig | 40 +++ security/sara/Makefile | 3 + security/sara/include/sara.h | 29 ++ security/sara/include/securityfs.h | 61 ++++ security/sara/include/utils.h | 80 ++++++ security/sara/main.c | 115 ++++++++ security/sara/securityfs.c | 565 +++++++++++++++++++++++++++++++++++++ security/sara/utils.c | 92 ++++++ 10 files changed, 993 insertions(+), 5 deletions(-) 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
security/Kconfig +6 -5
@@ -237,6 +237,7 @@ source "security/apparmor/Kconfig" source "security/loadpin/Kconfig" source "security/yama/Kconfig" source "security/safesetid/Kconfig" +source "security/sara/Kconfig" source "security/integrity/Kconfig"
@@ -276,11 +277,11 @@ endchoice config LSM string "Ordered list of enabled LSMs" - default "yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK - default "yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR - default "yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO - default "yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC - default "yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" + default "yama,loadpin,safesetid,integrity,sara,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK + default "yama,loadpin,safesetid,integrity,sara,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR + default "yama,loadpin,safesetid,integrity,sara,tomoyo" if DEFAULT_SECURITY_TOMOYO + default "yama,loadpin,safesetid,integrity,sara" if DEFAULT_SECURITY_DAC + default "yama,loadpin,safesetid,integrity,sara,selinux,smack,tomoyo,apparmor" help A comma-separated list of LSMs, in initialization order. Any LSMs left off this list will be ignored. This can be
security/Makefile +2 -0
@@ -11,6 +11,7 @@ subdir-$(CONFIG_SECURITY_APPARMOR) += apparmor subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid +subdir-$(CONFIG_SECURITY_SARA) += sara # always enable default capabilities obj-y += commoncap.o
@@ -27,6 +28,7 @@ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ +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 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* 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 int sara_config_locked__read_mostly ; +extern int sara_enabled__read_mostly ; + +#endif /* __SARA_H */
security/sara/include/securityfs.h new file +61 -0
61 lines not on this page · raw message
security/sara/include/utils.h new file +80 -0
80 lines not on this page · raw message
security/sara/main.c new file +115 -0
115 lines not on this page · raw message
security/sara/securityfs.c new file +565 -0
565 lines not on this page · raw message
security/sara/utils.c new file +92 -0
92 lines not on this page · raw message
Signature · 1 line
1.9.1
Randy Dunlap
reviewerRe: [PATCH v5 02/12] S.A.R.A.: create framework
Hi,
security/sara/Kconfig line 5
Quoted 11 lines · Salvatore Mesoraca
security/sara/Kconfig new file +5
-0@@ -0,0 +1,40 @@ +menuconfig SECURITY_SARA + bool "S.A.R.A." + depends on SECURITY + select SECURITYFS + default n
No need for "default n". Drop it, please.
Quoted 14 lines
security/sara/Kconfig new file +14
-0+ 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
security/sara/Kconfig line 29
Quoted 10 lines
security/sara/Kconfig new file +10
-0+ 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."
can be turned on
Quoted 11 lines
security/sara/Kconfig new file +11
-0+ 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. +
Signature · 1 line
~Randy
PATCH v5 3/12 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.
--- security/sara/Makefile | 2 +- security/sara/include/sara_data.h | 84 +++++++++++++++++++++++++++++++++++++++ security/sara/main.c | 7 ++++ security/sara/sara_data.c | 69 ++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 security/sara/include/sara_data.h create mode 100644 security/sara/sara_data.c
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 +84 -0
84 lines not on this page · raw message
security/sara/main.c +7 -0
@@ -18,6 +18,7 @@#include <linux/printk.h> #include "include/sara.h" +#include "include/sara_data.h" #include "include/securityfs.h" static const int sara_version =SARA_VERSION ;
@@ -93,6 +94,11 @@ static int __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)
@@ -112,4 +118,5 @@ static int __init sara_init(void) .name = "sara", .enabled = &sara_enabled, .init = sara_init, + .blobs = &sara_blob_sizes, };
security/sara/sara_data.c new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
1.9.1
James Morris
maintainerRe: [PATCH v5 03/12] S.A.R.A.: cred blob management
Quoted 7 lines · Salvatore Mesoraca
Creation 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 namedsecurity_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.
This description is out of date wrt cred blob sharing.
security/sara/sara_data.c line 58
Quoted 11 lines
security/sara/sara_data.c new file +10
-0+if (sara_data_init ()) { +pr_crit ("impossible to initialize creds.\n" ); + goto error; + } + +int __init sara_data_init (void ) +{ +security_add_hooks (data_hooks,ARRAY_SIZE (data_hooks),"sara" ); +return 0 ; +}
This can't fail so make it return void and simplify the caller.
Signature · 2 lines
James Morris <[email protected]>
PATCH v5 4/12 S.A.R.A.: generic DFA for string matching
In reply to Salvatore MesoracaCreation of a generic Discrete Finite Automata implementation for string matching. The transition tables have to be produced in user-space.
This allows us to possibly support advanced string matching patterns like regular expressions, but they need to be supported by user-space tools.
--- security/sara/Kconfig | 22 +++ security/sara/Makefile | 3 +- security/sara/dfa.c | 335 +++++++++++++++++++++++++++++++++++++++ security/sara/dfa_test.c | 135 ++++++++++++++++ security/sara/include/dfa.h | 52 ++++++ security/sara/include/dfa_test.h | 29 ++++ security/sara/main.c | 6 + 7 files changed, 581 insertions(+), 1 deletion(-) create mode 100644 security/sara/dfa.c create mode 100644 security/sara/dfa_test.c create mode 100644 security/sara/include/dfa.h create mode 100644 security/sara/include/dfa_test.h
security/sara/Kconfig +22 -0
@@ -13,6 +13,28 @@ menuconfig SECURITY_SARA If unsure, answer N. +config SECURITY_SARA_DFA_32BIT + bool "Use 32 bits instead of 16 bits for DFA states' id" + depends on SECURITY_SARA + default n + help + If you say Y here S.A.R.A. will use more memory, but you will be + able to configure more rules. + See Documentation/admin-guide/LSM/SARA.rst. for further information. + + If unsure, answer N. + +config SECURITY_SARA_DFA_TEST + bool "Enable test interface for the internal DFA engine" + depends on SECURITY_SARA + default n + help + If you say Y here S.A.R.A. will enable a user-space interface + that can be used to test the DFA engine (e.g. via `saractl test`). + See Documentation/admin-guide/LSM/SARA.rst. for further information. + + If unsure, answer N. + config SECURITY_SARA_DEFAULT_DISABLED bool "S.A.R.A. will be disabled at boot." depends on SECURITY_SARA
security/sara/Makefile +2 -1
@@ -1,3 +1,4 @@ obj-$(CONFIG_SECURITY_SARA) := sara.o -sara-y := main.o securityfs.o utils.o sara_data.o +sara-y := main.o securityfs.o utils.o sara_data.o dfa.o +sara-$(CONFIG_SECURITY_SARA_DFA_TEST) += dfa_test.o
security/sara/dfa.c new file +335 -0
335 lines not on this page · raw message
security/sara/dfa_test.c new file +135 -0
135 lines not on this page · raw message
security/sara/include/dfa.h new file +52 -0
52 lines not on this page · raw message
security/sara/include/dfa_test.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* 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_DFA_TEST_H +#define __SARA_DFA_TEST_H + +#ifdef CONFIG_SECURITY_SARA_DFA_TEST + +#include <linux/init.h> +int sara_dfa_test_init (void )__init ; + +#else /* CONFIG_SECURITY_SARA_DFA_TEST */ +inline int sara_dfa_test_init (void ) +{ +return 0 ; +} +#endif /* CONFIG_SECURITY_SARA_DFA_TEST */ + +#endif /* __SARA_DFA_TEST_H */
security/sara/main.c +6 -0
@@ -17,6 +17,7 @@#include <linux/module.h> #include <linux/printk.h> +#include "include/dfa_test.h" #include "include/sara.h" #include "include/sara_data.h" #include "include/securityfs.h"
@@ -99,6 +100,11 @@ static int __init sara_init(void) goto error; } +if (sara_dfa_test_init ()) { +pr_crit ("impossible to initialize DFA test interface.\n" ); + goto error; + } +pr_debug ("initialized.\n" );if (sara_enabled)
Signature · 1 line
1.9.1
Jann Horn
reviewerRe: [PATCH v5 04/12] S.A.R.A.: generic DFA for string matching
Quoted 6 lines · Salvatore Mesoraca
Creation of a generic Discrete Finite Automata implementation for string matching. The transition tables have to be produced in user-space.
This allows us to possibly support advanced string matching patterns like regular expressions, but they need to be supported by user-space tools.
AppArmor already has a DFA implementation that takes a DFA machine from userspace and runs it against file paths; see e.g. aa_dfa_match(). Did you look into whether you could move their DFA to some place like lib/ and reuse it instead of adding yet another generic rule interface to the kernel?
[...]
security/sara/dfa.c line 10
Quoted 12 lines
+++
b/security/sara/dfa.csecurity/sara/dfa.c new file +10
-0@@ -0,0 +1,335 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* 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.
Throughout the series, you are adding files that both add an SPDX identifier and have a description of the license in the comment block at the top. The SPDX identifier already identifies the license.
Re: [PATCH v5 04/12] S.A.R.A.: generic DFA for string matching
Quoted 10 lines · Jann Horn
Quoted 6 lines · Salvatore Mesoraca
Creation of a generic Discrete Finite Automata implementation for string matching. The transition tables have to be produced in user-space.
This allows us to possibly support advanced string matching patterns like regular expressions, but they need to be supported by user-space tools.
Quoted 5 lines
AppArmor already has a DFA implementation that takes a DFA machine from userspace and runs it against file paths; see e.g.
aa_dfa_match(). Did you look into whether you could move their DFA to some place likelib/and reuse it instead of adding yet another generic rule interface to the kernel?
Yes, using AppArmor DFA cloud be a possibility.
Though, I didn't know how AppArmor's maintainers feel about this. I thought that was easier to just implement my own.
Anyway I understand that re-using that code would be the optimal solution. I'm adding in CC AppArmor's maintainers, let's see what they think about this.
Quoted 8 lines
Quoted 8 lines
+++
b/security/sara/dfa.csecurity/sara/dfa.c new file +6
-0@@ -0,0 +1,335 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* S.A.R.A. Linux Security Module +* +* Copyright (C) 2017 Salvatore Mesoraca <[email protected]>
Quoted 8 lines
security/sara/dfa.c line 10
Quoted 4 lines
security/sara/dfa.c new file +4
-0+* +* 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. Throughout the series, you are adding files that both add an SPDX identifier and have a description of the license in the comment block at the top. The SPDX identifier already identifies the license.
I added the license description because I thought it was required anyway. IANAL, if you tell me that SPDX it's enough I'll remove the description.
Thank you for your comments.
Jann Horn
reviewerRe: [PATCH v5 04/12] S.A.R.A.: generic DFA for string matching
Quoted 7 lines · Salvatore Mesoraca
Quoted 3 lines · Jann Horn
Throughout the series, you are adding files that both add an SPDX identifier and have a description of the license in the comment block at the top. The SPDX identifier already identifies the license.
I added the license description because I thought it was required anyway. IANAL, if you tell me that SPDX it's enough I'll remove the description.
IANAL too, but Documentation/process/license-rules.rst says:
====
The common way of expressing the license of a source file is to add the matching boilerplate text into the top comment of the file. Due to formatting, typos etc. these "boilerplates" are hard to validate for tools which are used in the context of license compliance.
An alternative to boilerplate text is the use of Software Package Data Exchange (SPDX) license identifiers in each source file. SPDX license identifiers are machine parsable and precise shorthands for the license under which the content of the file is contributed. SPDX license identifiers are managed by the SPDX Workgroup at the Linux Foundation and have been agreed on by partners throughout the industry, tool vendors, and legal teams. For further information see https://spdx.org/
The Linux kernel requires the precise SPDX identifier in all source files. The valid identifiers used in the kernel are explained in the section License identifiers_ and have been retrieved from the official SPDX license list at https://spdx.org/licenses/ along with the license texts.
====
and there have been lots of conversion patches to replace license boilerplate headers with SPDX identifiers, see e.g. all the "treewide:
Replace GPLv2 boilerplate/reference with SPDX" patches.
Re: [PATCH v5 04/12] S.A.R.A.: generic DFA for string matching
In reply to Salvatore MesoracaQuoted 18 lines · Salvatore Mesoraca
Quoted 10 lines · Jann Horn
Quoted 6 lines · Salvatore Mesoraca
Creation of a generic Discrete Finite Automata implementation for string matching. The transition tables have to be produced in user-space.
This allows us to possibly support advanced string matching patterns like regular expressions, but they need to be supported by user-space tools.Quoted 5 lines
AppArmor already has a DFA implementation that takes a DFA machine from userspace and runs it against file paths; see e.g.
aa_dfa_match(). Did you look into whether you could move their DFA to some place likelib/and reuse it instead of adding yet another generic rule interface to the kernel?
Quoted 5 lines
Yes, using AppArmor DFA cloud be a possibility.
Though, I didn't know how AppArmor's maintainers feel about this. I thought that was easier to just implement my own.
Anyway I understand that re-using that code would be the optimal solution. I'm adding in CC AppArmor's maintainers, let's see what they think about this.
I don't want this to prevent SARA from being up-streamed. Do you think that having another DFA here could be acceptable anyway? Would it be better if I just drop the DFA an go back to simple string matching to speed up things?
Thank you,
Salvatore
Jann Horn
reviewerRe: [PATCH v5 04/12] S.A.R.A.: generic DFA for string matching
Quoted 23 lines · Salvatore Mesoraca
Quoted 16 lines · Salvatore Mesoraca
Quoted 9 lines · Jann Horn
Quoted 6 lines · Salvatore Mesoraca
Creation of a generic Discrete Finite Automata implementation for string matching. The transition tables have to be produced in user-space.
This allows us to possibly support advanced string matching patterns like regular expressions, but they need to be supported by user-space tools.Quoted 5 lines
AppArmor already has a DFA implementation that takes a DFA machine from userspace and runs it against file paths; see e.g.
aa_dfa_match(). Did you look into whether you could move their DFA to some place likelib/and reuse it instead of adding yet another generic rule interface to the kernel?Quoted 5 lines
Yes, using AppArmor DFA cloud be a possibility.
Though, I didn't know how AppArmor's maintainers feel about this. I thought that was easier to just implement my own.
Anyway I understand that re-using that code would be the optimal solution. I'm adding in CC AppArmor's maintainers, let's see what they think about this.
Quoted 4 lines
I don't want this to prevent SARA from being up-streamed. Do you think that having another DFA here could be acceptable anyway? Would it be better if I just drop the DFA an go back to simple string matching to speed up things?
While I think that it would be nicer not to have yet another implementation of the same thing, I don't feel strongly about it.
PATCH v5 5/12 LSM: 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
@@ -806,7 +806,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
@@ -163,7 +163,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
@@ -756,6 +756,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
@@ -513,6 +513,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 the hook mediates both flock and fcntl style locks.
@@ -1597,6 +1602,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);
@@ -1897,6 +1903,7 @@ struct security_hook_heads {struct hlist_head mmap_addr;struct hlist_head mmap_file;struct hlist_head file_mprotect; +struct hlist_head check_vmflags;struct hlist_head file_lock;struct hlist_head file_fcntl;struct hlist_head file_set_fowner;
include/linux/security.h +6 -0
@@ -312,6 +312,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);
@@ -859,6 +860,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
@@ -1390,6 +1390,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, {struct mm_struct *mm = current->mm;int pkey =0 ; +int error; *populate =0 ;
@@ -1453,6 +1454,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 ;
@@ -2996,6 +3001,10 @@ static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long flareturn -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;
@@ -3393,6 +3402,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 =vm_area_alloc (mm);if (unlikely (vma ==NULL ))return ERR_PTR (-ENOMEM );
security/security.c +5 -0
@@ -1421,6 +1421,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
PATCH v5 6/12 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.
Additional restrictions are in place for System V shared memory segments:
if a segment was attached as writable (executable) in the past it won't be allowed to be attached as executable (writable) in the future. 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/wxprot.h | 29 ++ security/sara/main.c | 6 + security/sara/wxprot.c | 679 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 789 insertions(+) create mode 100644 security/sara/include/wxprot.h create mode 100644 security/sara/wxprot.c
security/sara/Kconfig +74 -0
@@ -60,3 +60,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
@@ -2,3 +2,4 @@ obj-$(CONFIG_SECURITY_SARA) := sara.o sara-y := main.o securityfs.o utils.o sara_data.o dfa.o sara-$(CONFIG_SECURITY_SARA_DFA_TEST) += dfa_test.o +sara-$(CONFIG_SECURITY_SARA_WXPROT) += wxprot.o
security/sara/include/wxprot.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* 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
@@ -21,6 +21,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 ;
@@ -105,6 +106,11 @@ static int __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 +679 -0
679 lines not on this page · raw message
Signature · 1 line
1.9.1
Randy Dunlap
reviewerRe: [PATCH v5 06/12] S.A.R.A.: WX protection
Quoted 8 lines · Salvatore Mesoraca
security/sara/Kconfig +0
-0@@ -60,3 +60,77 @@ config SECURITY_SARA_NO_RUNTIME_ENABLE If unsure, answer Y.
security/sara/Kconfig config SECURITY_SARA_NO_RUNTIME_ENABLE · line 72
Quoted 10 lines
security/sara/Kconfig +10
-0+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.
.rst for further information.
security/sara/Kconfig config SECURITY_SARA_NO_RUNTIME_ENABLE · line 82
Quoted 10 lines
security/sara/Kconfig +10
-0+ + 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
Use tab instead of spaces for indentation above.
Quoted 31 lines
security/sara/Kconfig +31
-0+ 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."
security/sara/Kconfig config SECURITY_SARA_NO_RUNTIME_ENABLE · line 123
Quoted 10 lines
security/sara/Kconfig +10
-0+ 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
Omit "default n" please.
Quoted 13 lines
security/sara/Kconfig +13
-0+ 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
Signature · 1 line
~Randy
Al Viro
maintainerRe: [PATCH v5 06/12] S.A.R.A.: WX protection
In reply to Salvatore Mesoracasecurity/sara/wxprot.c line 325
Quoted 13 lines · Salvatore Mesoraca
security/sara/wxprot.c new file +13
-0+#define sara_warn_or_return (err, msg) do { \ + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ + pr_wxp(msg); \ + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ + return -err; \ +} while (0) + +#define sara_warn_or_goto (label, msg) do { \ + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ + pr_wxp(msg); \ + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ + goto label; \ +} while (0)
No. This kind of "style" has no place in the kernel.
Don't hide control flow. It's nasty enough to reviewers, but it's pure hell on anyone who strays into your code while chasing a bug or doing general code audit. In effect, you are creating your oh-so-private C dialect and assuming that everyone who ever looks at your code will start with learning that *AND* incorporating it into their mental C parser. I'm sorry, but you are not that important.
If it looks like a function call, a casual reader will assume that this is exactly what it is. And when one is scanning through a function (e.g. to tell if handling of some kind of refcounts is correct, with twentieth grep through the tree having brought something in your code into the view), the last thing one wants is to switch between the area-specific C dialects. Simply because looking at yours is sandwiched between digging through some crap in drivers/target/ and that weird thing in kernel/tracing/, hopefully staying limited to 20 seconds of glancing through several functions in your code.
Don't Do That. Really.
Re: [PATCH v5 06/12] S.A.R.A.: WX protection
Quoted 39 lines · Al Viro
security/sara/wxprot.c line 325
Quoted 13 lines · Salvatore Mesoraca
security/sara/wxprot.c new file +13
-0+#define sara_warn_or_return (err, msg) do { \ + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ + pr_wxp(msg); \ + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ + return -err; \ +} while (0) + +#define sara_warn_or_goto (label, msg) do { \ + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ + pr_wxp(msg); \ + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ + goto label; \ +} while (0)No. This kind of "style" has no place in the kernel.
Don't hide control flow. It's nasty enough to reviewers, but it's pure hell on anyone who strays into your code while chasing a bug or doing general code audit. In effect, you are creating your oh-so-private C dialect and assuming that everyone who ever looks at your code will start with learning that *AND* incorporating it into their mental C parser. I'm sorry, but you are not that important.
If it looks like a function call, a casual reader will assume that this is exactly what it is. And when one is scanning through a function (e.g. to tell if handling of some kind of refcounts is correct, with twentieth grep through the tree having brought something in your code into the view), the last thing one wants is to switch between the area-specific C dialects. Simply because looking at yours is sandwiched between digging through some crap in
drivers/target/and that weird thing inkernel/tracing/, hopefully staying limited to 20 seconds of glancing through several functions in your code.
Quoted 1 line
Don't Do That. Really.
I understand your concerns.
The first version of SARA didn't use these macros, they were added because I was asked[1] to do so.
I have absolutely no problems in reverting this change. I just want to make sure that there is agreement on this matter. Maybe Kees can clarify his stance.
Thank you for your suggestions.
[1] https://lkml.kernel.org/r/CAGXu5jJuQx2qOt_aDqDQDcqGOZ5kmr5rQ9Zjv=MRRCJ65ERfGw@mail.gmail.com
Re: [PATCH v5 06/12] S.A.R.A.: WX protection
Quoted 46 lines · Salvatore Mesoraca
Quoted 39 lines · Al Viro
security/sara/wxprot.c line 325
Quoted 13 lines · Salvatore Mesoraca
security/sara/wxprot.c new file +13
-0+#define sara_warn_or_return (err, msg) do { \ + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ + pr_wxp(msg); \ + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ + return -err; \ +} while (0) + +#define sara_warn_or_goto (label, msg) do { \ + if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \ + pr_wxp(msg); \ + if (!(sara_wxp_flags & SARA_WXP_COMPLAIN)) \ + goto label; \ +} while (0)No. This kind of "style" has no place in the kernel.
Don't hide control flow. It's nasty enough to reviewers, but it's pure hell on anyone who strays into your code while chasing a bug or doing general code audit. In effect, you are creating your oh-so-private C dialect and assuming that everyone who ever looks at your code will start with learning that *AND* incorporating it into their mental C parser. I'm sorry, but you are not that important.
If it looks like a function call, a casual reader will assume that this is exactly what it is. And when one is scanning through a function (e.g. to tell if handling of some kind of refcounts is correct, with twentieth grep through the tree having brought something in your code into the view), the last thing one wants is to switch between the area-specific C dialects. Simply because looking at yours is sandwiched between digging through some crap in
drivers/target/and that weird thing inkernel/tracing/, hopefully staying limited to 20 seconds of glancing through several functions in your code.Quoted 1 line
Don't Do That. Really.
I understand your concerns.
The first version of SARA didn't use these macros, they were added because I was asked[1] to do so.
Quoted 7 lines
I have absolutely no problems in reverting this change. I just want to make sure that there is agreement on this matter. Maybe Kees can clarify his stance.
Thank you for your suggestions.
[1] https://lkml.kernel.org/r/CAGXu5jJuQx2qOt_aDqDQDcqGOZ5kmr5rQ9Zjv=MRRCJ65ERfGw@mail.gmail.com
I just didn't like how difficult it was to review the repeated checking. I thought then (and still think now) it's worth the unusual style to improve the immediate readability. Obviously Al disagrees. I'm not against dropping my suggestion; it's just a pain to review it and it seems like an area that would be highly prone to subtle typos. Perhaps some middle ground:
#define sara_warn(msg) ({ \
if ((sara_wxp_flags & SARA_WXP_VERBOSE)) \
pr_wxp(msg); \
!(sara_wxp_flags & SARA_WXP_COMPLAIN); \
})...
if (unlikely(sara_wxp_flags & SARA_WXP_WXORX &&
vm_flags & VM_WRITE &&
vm_flags & VM_EXEC &&
sara_warn("W^X")))
return -EPERM;that way the copy/pasting isn't present but the control flow is visible?
Signature · 1 line
Kees Cook
RE: [PATCH v5 06/12] S.A.R.A.: WX protection
In reply to Salvatore MesoracaFrom: Salvatore Mesoraca
Quoted 1 line
Sent: 06 July 2019 11:55
...
Quoted 4 lines
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.
What about writing to the file of a dynamic library after it is loaded but before it is faulted it (or after evicting it from the I$).
...
Quoted 9 lines
security/sara/wxprot.c new file +9
-0+#define find_relro_section (ELFH ,ELFP ,FILE ,RELRO ,FOUND ) do { \ + unsigned long i; \ + int _tmp; \ + loff_t _pos = 0; \ + if (ELFH.e_type == ET_DYN || ELFH.e_type == ET_EXEC) { \ + for (i = 0; i < ELFH.e_phnum; ++i) { \ + _pos = ELFH.e_phoff + i*sizeof(ELFP); \ + _tmp = kernel_read(FILE, &ELFP, sizeof(ELFP), \ + &_pos); \
security/sara/wxprot.c line 173
Quoted 10 lines
security/sara/wxprot.c new file +10
-0+if (_tmp !=sizeof (ELFP )) \ +break ; \ +if (ELFP .p_type ==PT_GNU_RELRO ) { \ +RELRO =ELFP .p_offset >>PAGE_SHIFT ; \ +FOUND = true; \ +break ; \ + } \ + } \ + } \ +}while (0 )
This is big for a #define.
Since it contains kernel_read() it can't really matter if it is a real function.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
PATCH v5 7/12 LSM: 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
@@ -252,6 +252,12 @@ config ARCH_HAS_FORTIFY_SOURCE config ARCH_HAS_KEEPINITRD bool +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
@@ -67,6 +67,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_MEMBARRIER_SYNC_CORE select ARCH_HAS_PMEM_API if X86_64 select ARCH_HAS_PTE_SPECIAL
arch/x86/mm/fault.c +6 -0
@@ -18,6 +18,7 @@#include <linux/uaccess.h> /* faulthandler_disabled() */ #include <linux/efi.h> /* efi_recover_from_page_fault()*/ #include <linux/mm_types.h> +#include <linux/security.h> /* security_pagefault_handler */ #include <asm/cpufeature.h> /* boot_cpu_has, ... */ #include <asm/traps.h> /* dotraplinkage, ... */
@@ -1360,6 +1361,11 @@ void do_user_addr_fault(struct pt_regs *regs,local_irq_enable (); } +if (unlikely (security_pagefault_handler (regs, + hw_error_code, + address))) +return ; +perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);if (hw_error_code &X86_PF_WRITE )
include/linux/lsm_hooks.h +12 -0
@@ -518,6 +518,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 the hook mediates both flock and fcntl style locks.
@@ -1603,6 +1611,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);
@@ -1904,6 +1915,7 @@ struct security_hook_heads {struct hlist_head mmap_file;struct hlist_head file_mprotect;struct hlist_head check_vmflags; +struct hlist_head pagefault_handler;struct hlist_head file_lock;struct hlist_head file_fcntl;struct hlist_head file_set_fowner;
include/linux/security.h +11 -0
@@ -313,6 +313,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);
@@ -865,6 +868,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
@@ -1426,6 +1426,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
PATCH v5 8/12 S.A.R.A.: 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 | 57 ++++++++++++ arch/x86/security/sara/trampolines32.h | 137 +++++++++++++++++++++++++++ arch/x86/security/sara/trampolines64.h | 164 +++++++++++++++++++++++++++++++++ security/sara/Kconfig | 18 ++++ security/sara/include/emutramp.h | 35 +++++++ security/sara/wxprot.c | 29 ++++++ 9 files changed, 445 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
@@ -25,3 +25,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 +57 -0
57 lines not on this page · raw message
arch/x86/security/sara/trampolines32.h new file +137 -0
137 lines not on this page · raw message
arch/x86/security/sara/trampolines64.h new file +164 -0
164 lines not on this page · raw message
security/sara/Kconfig +18 -0
@@ -117,6 +117,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 +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* 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 __SARA_EMUTRAMP_H +#define __SARA_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 /* __SARA_EMUTRAMP_H */
security/sara/wxprot.c +29 -0
@@ -31,6 +31,7 @@#include "include/utils.h" #include "include/securityfs.h" #include "include/wxprot.h" +#include "include/emutramp.h" #define SARA_WXPROT_CONFIG_VERSION 0
@@ -41,6 +42,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 | \
@@ -51,7 +53,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_config_container {struct sara_dfa_tables *dfa;
@@ -67,7 +74,11 @@ struct wxprot_config_container { static u16 default_flags __lsm_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) {
@@ -110,6 +121,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; }
@@ -514,11 +528,26 @@ 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 || +likely (!(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[] __lsm_ro_after_init = {LSM_HOOK_INIT (bprm_set_creds, sara_bprm_set_creds),LSM_HOOK_INIT (check_vmflags, sara_check_vmflags),LSM_HOOK_INIT (shm_shmat, sara_shm_shmat),LSM_HOOK_INIT (file_mprotect, sara_file_mprotect), +#ifdef CONFIG_SECURITY_SARA_WXPROT_EMUTRAMP +LSM_HOOK_INIT (pagefault_handler, sara_pagefault_handler), +#endif };static void config_free (struct wxprot_config_container *data)
Signature · 1 line
1.9.1
Randy Dunlap
reviewerRe: [PATCH v5 08/12] S.A.R.A.: trampoline emulation
Quoted 9 lines · Salvatore Mesoraca
security/sara/Kconfig +1
-0@@ -117,6 +117,24 @@ choice Documentation/admin-guide/LSM/SARA.rst. endchoice +config SECURITY_SARA_WXPROT_EMUTRAMP
security/sara/Kconfig choice · line 130
Quoted 10 lines
security/sara/Kconfig +10
-0+ 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
This feature makes it possible
security/sara/Kconfig choice · line 132
Quoted 2 lines
security/sara/Kconfig +2
-0+ if they reside in non-executable memory pages. + This features need to be enabled on a per-executable basis
This feature needs to be
Quoted 8 lines
security/sara/Kconfig +5
-0+ 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
Signature · 1 line
~Randy
PATCH v5 9/12 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
--- fs/proc/base.c | 11 ++++ security/sara/wxprot.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+)
fs/proc/base.c +11 -0
@@ -2612,6 +2612,13 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,LSM_DIR_OPS (smack);#endif +#ifdef CONFIG_SECURITY_SARA +static const struct pid_entry sara_attr_dir_stuff[] = { +ATTR ("sara" ,"wxprot" ,0666 ), +}; +LSM_DIR_OPS (sara); +#endif +static const struct pid_entry attr_dir_stuff[] = {ATTR (NULL ,"current" ,0666 ),ATTR (NULL ,"prev" ,0444 ),
@@ -2623,6 +2630,10 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, DIR("smack", 0555, proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),#endif +#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/sara/wxprot.c +150 -0
174 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 10/12 S.A.R.A.: 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 | 99 +++++++++++++++++++++++++ 5 files changed, 161 insertions(+)
Documentation/admin-guide/LSM/SARA.rst +20 -0
@@ -55,6 +55,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:
@@ -123,6 +125,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
@@ -4254,6 +4254,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
@@ -135,6 +135,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 +99 -0
147 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 11/12 S.A.R.A.: /proc/*/mem write limitation
In reply to Salvatore MesoracaPrevent a task from opening, in "write" mode, any /proc/*/mem file that operates on the task's mm.
A process could use it to overwrite read-only memory, bypassing S.A.R.A. restrictions.
--- security/sara/include/sara_data.h | 18 ++++++++++++++++- security/sara/sara_data.c | 8 ++++++++ security/sara/wxprot.c | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-)
security/sara/include/sara_data.h +17 -1
@@ -15,6 +15,7 @@#define __SARA_DATA_H#include <linux/cred.h> +#include <linux/fs.h> #include <linux/init.h> #include <linux/lsm_hooks.h> #include <linux/spinlock.h>
@@ -40,6 +41,10 @@ struct sara_shm_data {spinlock_t lock; }; +struct sara_inode_data { +struct task_struct *task; +}; +static inline struct sara_data *get_sara_data (const struct cred *cred) {
@@ -79,6 +84,17 @@ static inline struct sara_shm_data *get_sara_shm_data(#define lock_sara_shm (X ) (spin_lock(&get_sara_shm_data((X))->lock))#define unlock_sara_shm (X ) (spin_unlock(&get_sara_shm_data((X))->lock)) -#endif + +static inline struct sara_inode_data *get_sara_inode_data ( +const struct inode *inode) +{ +if (unlikely (!inode->i_security)) +return NULL ; +return inode->i_security + sara_blob_sizes.lbs_inode; +} + +#define get_sara_inode_task (X ) (get_sara_inode_data((X))->task) + +#endif /* CONFIG_SECURITY_SARA_WXPROT */ #endif /* __SARA_H */
security/sara/sara_data.c +8 -0
@@ -17,6 +17,7 @@#include <linux/cred.h> #include <linux/lsm_hooks.h> #include <linux/mm.h> +#include <linux/slab.h> #include <linux/spinlock.h> static int sara_cred_prepare(struct cred *new, const struct cred *old,
@@ -40,15 +41,22 @@ static int sara_shm_alloc_security(struct kern_ipc_perm *shp)return 0 ; } +static void sara_task_to_inode (struct task_struct *t,struct inode *i) +{ +get_sara_inode_task (i) = t; +} + static struct security_hook_list data_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (cred_prepare, sara_cred_prepare),LSM_HOOK_INIT (cred_transfer, sara_cred_transfer),LSM_HOOK_INIT (shm_alloc_security, sara_shm_alloc_security), +LSM_HOOK_INIT (task_to_inode, sara_task_to_inode), }; struct lsm_blob_sizes sara_blob_sizes __lsm_ro_after_init = { .lbs_cred =sizeof (struct sara_data ), .lbs_ipc =sizeof (struct sara_shm_data ), + .lbs_inode =sizeof (struct sara_inode_data ), };int __init sara_data_init (void )
security/sara/wxprot.c +41 -0
61 lines not on this page · raw message
Signature · 1 line
1.9.1
Jann Horn
reviewerRe: [PATCH v5 11/12] S.A.R.A.: /proc/*/mem write limitation
Quoted 4 lines · Salvatore Mesoraca
Prevent a task from opening, in "write" mode, any
/proc/*/memfile that operates on the task's mm.
A process could use it to overwrite read-only memory, bypassing S.A.R.A. restrictions.
[...]
security/sara/sara_data.c static int sara_shm_alloc_security(struct kern_ipc_perm *shp) · line 46
Quoted 3 lines
security/sara/sara_data.c +3
-0+static void sara_task_to_inode (struct task_struct *t,struct inode *i) +{ +get_sara_inode_task (i) = t;
This looks bogus. Nothing is actually holding a reference to t here, right?
Quoted 8 lines
security/sara/sara_data.c +3
-0+} + static struct security_hook_list data_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (cred_prepare, sara_cred_prepare),LSM_HOOK_INIT (cred_transfer, sara_cred_transfer),LSM_HOOK_INIT (shm_alloc_security, sara_shm_alloc_security), +LSM_HOOK_INIT (task_to_inode, sara_task_to_inode), };
[...]
Quoted 13 lines
security/sara/wxprot.c +13
-0+static int sara_file_open (struct file *file) +{ +struct task_struct *t; +struct mm_struct *mm; +u16 sara_wxp_flags =get_current_sara_wxp_flags (); + +/* +* Prevent write access to /proc/.../mem +* if it operates on the mm_struct of the +* current process: it could be used to +* bypass W^X. +*/ +
security/sara/wxprot.c static int sara_file_mprotect(struct vm_area_struct *vma, · line 643
Quoted 10 lines
security/sara/wxprot.c +10
-0+if (!sara_enabled || + !wxprot_enabled || + !(sara_wxp_flags &SARA_WXP_WXORX ) || + !(file->f_mode &FMODE_WRITE )) +return 0 ; + + t =get_sara_inode_task (file_inode (file)); +if (unlikely (t !=NULL && +strcmp (file->f_path.dentry->d_name.name, +"mem" ) ==0 )) {
This should probably at least have a READ_ONCE() somewhere in case the file concurrently gets renamed?
security/sara/wxprot.c static int sara_file_mprotect(struct vm_area_struct *vma, · line 646
Quoted 3 lines
security/sara/wxprot.c +3
-0+get_task_struct (t); + mm =get_task_mm (t); +put_task_struct (t);
Getting and dropping a reference to the task_struct here is completely useless. Either you have a reference, in which case you don't need to take another one, or you don't have a reference, in which case you also can't take one.
security/sara/wxprot.c static int sara_file_mprotect(struct vm_area_struct *vma, · line 649
Quoted 3 lines
security/sara/wxprot.c +3
-0+if (unlikely (mm == current->mm)) +sara_warn_or_goto (error, +"write access to /proc/*/mem" );
Why is the current process so special that it must be protected more than other processes? Is the idea here to rely on other protections to protect all other tasks? This should probably come with a comment that explains this choice.
Quoted 7 lines
security/sara/wxprot.c +7
-0+mmput (mm); + } +return 0 ; +error: +mmput (mm); +return -EACCES ; +}
Re: [PATCH v5 11/12] S.A.R.A.: /proc/*/mem write limitation
Quoted 7 lines · Jann Horn
Quoted 4 lines · Salvatore Mesoraca
Prevent a task from opening, in "write" mode, any
/proc/*/memfile that operates on the task's mm.
A process could use it to overwrite read-only memory, bypassing S.A.R.A. restrictions.
Quoted 6 lines
[...]
security/sara/sara_data.c static int sara_shm_alloc_security(struct kern_ipc_perm *shp) · line 46
Quoted 3 lines
security/sara/sara_data.c +3
-0+static void sara_task_to_inode (struct task_struct *t,struct inode *i) +{ +get_sara_inode_task (i) = t;This looks bogus. Nothing is actually holding a reference to
there, right?
I think you are right, I should probably store the PID here.
Quoted 27 lines
Quoted 8 lines
security/sara/sara_data.c +3
-0+} + static struct security_hook_list data_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (cred_prepare, sara_cred_prepare),LSM_HOOK_INIT (cred_transfer, sara_cred_transfer),LSM_HOOK_INIT (shm_alloc_security, sara_shm_alloc_security), +LSM_HOOK_INIT (task_to_inode, sara_task_to_inode), };[...]
Quoted 18 lines
security/sara/wxprot.c +18
-0+static int sara_file_open (struct file *file) +{ +struct task_struct *t; +struct mm_struct *mm; +u16 sara_wxp_flags =get_current_sara_wxp_flags (); + +/* +* Prevent write access to /proc/.../mem +* if it operates on the mm_struct of the +* current process: it could be used to +* bypass W^X. +*/ + +if (!sara_enabled || + !wxprot_enabled || + !(sara_wxp_flags &SARA_WXP_WXORX ) || + !(file->f_mode &FMODE_WRITE )) +return 0 ;
Quoted 8 lines
security/sara/wxprot.c static int sara_file_mprotect(struct vm_area_struct *vma, · line 643
Quoted 5 lines
security/sara/wxprot.c +5
-0+ + t =get_sara_inode_task (file_inode (file)); +if (unlikely (t !=NULL && +strcmp (file->f_path.dentry->d_name.name, +"mem" ) ==0 )) {This should probably at least have a
READ_ONCE()somewhere in case the file concurrently gets renamed?
My understanding here is that /proc/$pid/mem files cannot be renamed. t != NULL implies we are in procfs.
Under these assumptions I think that that code is fine.
Am I wrong?
Quoted 8 lines
security/sara/wxprot.c static int sara_file_mprotect(struct vm_area_struct *vma, · line 646
Quoted 3 lines
security/sara/wxprot.c +3
-0+get_task_struct (t); + mm =get_task_mm (t); +put_task_struct (t);Getting and dropping a reference to the
task_structhere is completely useless. Either you have a reference, in which case you don't need to take another one, or you don't have a reference, in which case you also can't take one.
Absolutely agree.
Quoted 8 lines
security/sara/wxprot.c static int sara_file_mprotect(struct vm_area_struct *vma, · line 649
Quoted 3 lines
security/sara/wxprot.c +3
-0+if (unlikely (mm == current->mm)) +sara_warn_or_goto (error, +"write access to /proc/*/mem" );Why is the current process so special that it must be protected more than other processes? Is the idea here to rely on other protections to protect all other tasks? This should probably come with a comment that explains this choice.
Yes, I should have spent some more words here.
Access to /proc/$pid/mem from other processes is already regulated by security_ptrace_access_check (i.e. Yama).
Unfortunately, that hook is ignored when "mm == current->mm".
It seems that there is some user-space software that relies on /proc/self/mem being writable (cfr. commit f511c0b17b08).
Thank you for your suggestions.
PATCH v5 12/12 MAINTAINERS: take maintainership for S.A.R.A.
In reply to Salvatore Mesoraca--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -13925,6 +13925,15 @@ F: drivers/phy/samsung/phy-s5pv210-usb2.c F: drivers/phy/samsung/phy-samsung-usb2.c F: drivers/phy/samsung/phy-samsung-usb2.h +SARA SECURITY MODULE +M: Salvatore Mesoraca <[email protected]> +T: git git://github.com/smeso/sara.git lsm/sara/master +W: https://sara.smeso.it +S: Maintained +F: security/sara/ +F: arch/x86/security/sara/ +F: Documentation/admin-guide/LSM/SARA.rst + SC1200 WDT DRIVER M: Zwane Mwaikambo <[email protected]> S: Maintained
Signature · 1 line
1.9.1
Quoted 6 lines · Salvatore Mesoraca
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.
I thought all stacking pieces for minor LSM were merged in Linux 5.1. Is there still something missing or is this comment out-fo-date?
Jordan
You are right. I just forgot to remove that paragraph from the cover letter. My bad.
Thank you for noticing that :)
Il giorno sab 6 lug 2019 alle ore 16:33 Jordan Glover <[email protected]> ha scritto:
Quoted 14 lines · Salvatore Mesoraca
Quoted 7 lines
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.
I thought all stacking pieces for minor LSM were merged in Linux 5.1. Is there still something missing or is this comment out-fo-date?
Jordan
James Morris
maintainerQuoted 1 line · Salvatore Mesoraca
S.A.R.A. (S.A.R.A. is Another Recursive Acronym) is a stacked Linux
Please make this just SARA. Nobody wants to read or type S.A.R.A.
Signature · 2 lines
James Morris <[email protected]>
Quoted 6 lines · James Morris
Quoted 1 line · Salvatore Mesoraca
S.A.R.A. (S.A.R.A. is Another Recursive Acronym) is a stacked Linux
Please make this just SARA. Nobody wants to read or type S.A.R.A.
Agreed.
Thank you for your suggestion.