NAX (No Anonymous Execution) LSM
Subsystems (3)
v3 cover NAX (No Anonymous Execution) LSM
[Overview]
Fileless malware attacks are becoming more and more popular, and even ready-to-use frameworks are available [1], [2], [3]. They are based on running of the malware code from anonymous executable memory pages (which are not backed by an executable file or a library on a filesystem.) This allows effectively hiding malware presence in a system, making filesystem integrity checking tools unable to detect the intrusion.
Typically, the malware first needs to intercept the execution flow (e.g., by the means of ROP-based exploit). Then it needs to download the main part (in the form of normal executable or library) from its server, because it is hard to implement the entire exploit in ROP-based form. There are a number of security mechanisms that can ensure the integrity of the file-system, but we need to ensure the integrity of the code in memory too, to be sure, that only authorized code is running in the system.
The proposed LSM is preventing the creation of anonymous executable pages for the processes. The LSM intercepts mmap() and mprotect() system calls and handles it similarly to SELinux handlers.
The module allows to block the violating system call or to kill the violating process, depending on the settings, along with rate-limited logging.
Currently, the module restricts ether all processes or only the privileged processes, depending on the settings. The privileged process is a process for which any of the following is true:
+ uid == 0 && !issecure(SECURE_NOROOT) + euid == 0 && !issecure(SECURE_NOROOT) + suid == 0 && !issecure(SECURE_NOROOT)
+ cap_effective has any capability except of kernel.nax.allowed_caps
+ cap_permitted has any capability except of kernel.nax.allowed_caps
Checking of uid/euid/suid is important because a process may call seteuid(0) to gain privileges (if SECURE_NO_SETUID_FIXUP secure bit is not set).
The sysctl parameter kernel.nax.allowed_caps allows to define safe capabilities set for the privileged processes.
[JIT]
Because of blocked anonymous code execution, JIT-compiled code, some interpreters (which are using JIT) and libffi-based projects can be broken.
Our observation shows that such processes are typically running by a user, so they will not be privileged, so they will be allowed to use anonymous executable pages.
But for small embedded set-ups it could be possible to get rid of such processes at all, so the module could be enabled without further restrictions to protect both privileged and non-privileged processes.
In addition, libffi can be modified not to use anonymous executable pages.
[Similar implementations]
Although SELinux could be used to enable similar functionality, this LSM is simpler. It could be used in set-ups, where SELinux would be overkill.
There is also SARA LSM module, which solves similar task, but it is more complex.
[Cooperation with other security mechanisms]
NAX LSM is more useful in conjunction with IMA. IMA would be responsible for integrity checking of file-based executables and libraries, and NAX LSM would be responsible for preventing of anonymous code execution.
Alternatively, NAX LSM can be used with read-only root file system, protected by dm-verity/fs-verity.
[TODO]
- Implement xattrs support for marking privileged binaries on a per-file basis.
- Store NAX attributes in the per-task LSM blob to implement special launchers for the privileged processes, so all of the children processes of such a launcher would be allowed to have anonymous executable pages (but not to grandchildren).
[Links]
[1] https://blog.fbkcs.ru/elf-in-memory-execution/
[2] https://magisterquis.github.io/2018/03/31/in-memory-only-elf-execution.html
[3] https://www.prodefence.org/fireelf-fileless-linux-malware-framework/
[Credits]
Thanks to Mimi Zohar for consulting and to Simon Thoby for thorough review.
[Changelog]
V3
- Fix memory leak in allowed_caps assigning code.
- Protect allowed_caps updating with a spinlock.
- Fix Kconfig options description.
- Add example for allowed_caps value.
- Fix typo in documentation.
V2
- Fixed typo in Kconfig.
- Fixed "cap_effective" and "cap_permitted" parameters description in NAX.rst.
- Added "nax_allowed_caps" setup parameter. Factored out capabilities parsing logic.
- Added parameter for checking all processes (not only privileged).
- Added Kconfig parameter for setting allowed capabilities.
- Updated nax_file_mprotect() to avoid calling of nax_mmap_file() to avoid duplicated checks.
- Protect allowed_caps with RCU.
- Fixed all errors and most warning found by checkpatch.pl.
- Updated the module documentation. Added description of the boot parameters to kernel-parameters.
- Updated commit message.
V1:
- Initial implementation.
Igor Zhbanov (1):
NAX LSM: Add initial support
Documentation/admin-guide/LSM/NAX.rst | 72 +++ Documentation/admin-guide/LSM/index.rst | 1 + .../admin-guide/kernel-parameters.rst | 1 + .../admin-guide/kernel-parameters.txt | 32 ++ security/Kconfig | 11 +- security/Makefile | 2 + security/nax/Kconfig | 114 +++++ security/nax/Makefile | 4 + security/nax/nax-lsm.c | 472 ++++++++++++++++++ 9 files changed, 704 insertions(+), 5 deletions(-) create mode 100644 Documentation/admin-guide/LSM/NAX.rst create mode 100644 security/nax/Kconfig create mode 100644 security/nax/Makefile create mode 100644 security/nax/nax-lsm.c
Signature · 1 line
2.26.2
PATCH v3 1/1 NAX LSM: Add initial support
Add initial support for NAX (No Anonymous Execution), which is a Linux Security Module that extends DAC by making impossible to make anonymous and modified pages executable for privileged processes.
Intercepts anonymous executable pages created with mmap() and mprotect() system calls.
Log violations (in non-quiet mode) and block the action or kill the offending process, depending on the enabled settings.
See Documentation/admin-guide/LSM/NAX.rst.
--- Documentation/admin-guide/LSM/NAX.rst | 72 +++ Documentation/admin-guide/LSM/index.rst | 1 + .../admin-guide/kernel-parameters.rst | 1 + .../admin-guide/kernel-parameters.txt | 32 ++ security/Kconfig | 11 +- security/Makefile | 2 + security/nax/Kconfig | 114 +++++ security/nax/Makefile | 4 + security/nax/nax-lsm.c | 472 ++++++++++++++++++ 9 files changed, 704 insertions(+), 5 deletions(-) create mode 100644 Documentation/admin-guide/LSM/NAX.rst create mode 100644 security/nax/Kconfig create mode 100644 security/nax/Makefile create mode 100644 security/nax/nax-lsm.c
Documentation/admin-guide/LSM/NAX.rst new file +72 -0
@@ -0,0 +1,72 @@ +======= +NAX LSM +======= + +:Author: Igor Zhbanov + +NAX (No Anonymous Execution) is a Linux Security Module that extends DAC +by making impossible to make anonymous and modified pages executable for +processes. The module intercepts anonymous executable pages created with +mmap() and mprotect() system calls. + +To select it at boot time, add ``nax`` to ``security`` kernel command-line +parameter. + +The following sysctl parameters are available: + +* ``kernel.nax.check_all``: + - 0: Check all processes. + - 1: Check only privileged processes. The privileged process is a process + for which any of the following is true: + - ``uid == 0`` + - ``euid == 0`` + - ``suid == 0`` + - ``cap_effective`` has any capability except for the ones allowed + in ``kernel.nax.allowed_caps`` + - ``cap_permitted`` has any capability except for the ones allowed + in ``kernel.nax.allowed_caps`` + + Checking of uid/euid/suid is important because a process may call seteuid(0) + to gain privileges (if SECURE_NO_SETUID_FIXUP secure bit is not set). + +* ``kernel.nax.allowed_caps``: + + Hexadecimal number representing the set of capabilities a non-root + process can possess without being considered "privileged" by NAX LSM. + + For the meaning of the capabilities bits and their value, please check + ``include/uapi/linux/capability.h`` and ``capabilities(7)`` manual page. + + For example, ``CAP_SYS_PTRACE`` has a number 19. Therefore, to add it to + allowed capabilities list, we need to set 19'th bit (2^19 or 1 << 19) + or 80000 in hexadecimal form. Capabilities can be bitwise ORed. + +* ``kernel.nax.mode``: + + - 0: Only log errors (when enabled by ``kernel.nax.quiet``) (default mode) + - 1: Forbid unsafe pages mappings (and log when enabled) + - 2: Kill the violating process (and log when enabled) + +* ``kernel.nax.quiet``: + + - 0: Log violations (default) + - 1: Be quiet + +* ``kernel.nax.locked``: + + - 0: Changing of the module's sysctl parameters is allowed + - 1: Further changing of the module's sysctl parameters is forbidden + + Setting this parameter to ``1`` after initial setup during the system boot + will prevent the module disabling at the later time. + +There are matching kernel command-line parameters (with the same values): + +- ``nax_allowed_caps`` +- ``nax_check_all`` +- ``nax_mode`` +- ``nax_quiet`` +- ``nax_locked`` + +The ``nax_locked`` command-line parameter must be specified last to avoid +premature setting locking.
Documentation/admin-guide/LSM/index.rst +1 -0
@@ -42,6 +42,7 @@ subdirectories. apparmor LoadPin + NAX SELinux Smack tomoyo
Documentation/admin-guide/kernel-parameters.rst +1 -0
@@ -136,6 +136,7 @@ parameter is applicable:: MOUSE Appropriate mouse support is enabled. MSI Message Signaled Interrupts (PCI). MTD MTD (Memory Technology Device) support is enabled. + NAX NAX support is enabled. NET Appropriate network support is enabled. NUMA NUMA support is enabled. NFS Appropriate NFS support is enabled.
Documentation/admin-guide/kernel-parameters.txt +32 -0
@@ -3100,6 +3100,38 @@ n2= [NET] SDL Inc. RISCom/N2 synchronous serial card + nax_allowed_caps= [NAX] Hexadecimal number representing the set of + capabilities a non-root process can possess without + being considered "privileged" by NAX LSM. + + For the meaning of the capabilities bits and their + value, please check include/uapi/linux/capability.h + and capabilities(7) manual page. + + For example, `CAP_SYS_PTRACE` has a number 19. + Therefore, to add it to allowed capabilities list, + we need to set 19'th bit (2^19 or 1 << 19) or 80000 + in hexadecimal form. Capabilities can be bitwise ORed. + + nax_check_all= [NAX] NAX LSM processes checking mode: + 0 - Check only privileged processes (default). + 1 - Check all processes. + + nax_locked= [NAX] NAX LSM settings' locking mode: + 0 - Changing NAX sysctl parameters is allowed. + 1 - Changing NAX sysctl parameters is forbidden until + reboot. + + nax_mode= [NAX] NAX LSM violation reaction mode: + 0 - Only log errors (when not in quiet mode; default). + 1 - Forbid unsafe pages mappings (and log when + enabled). + 2 - Kill the violating process (and log when enabled). + + nax_quiet= [NAX] NAX LSM log verbosity: + 0 - Log messages to syslog. + 1 - Be quiet. + netdev= [NET] Network devices parameters Format: <irq>,<io>,<mem_start>,<mem_end>,<name> Note that mem_start is often overloaded to mean
security/Kconfig +6 -5
@@ -239,6 +239,7 @@ source "security/yama/Kconfig" source "security/safesetid/Kconfig" source "security/lockdown/Kconfig" source "security/landlock/Kconfig" +source "security/nax/Kconfig" source "security/integrity/Kconfig"
@@ -278,11 +279,11 @@ endchoice config LSM string "Ordered list of enabled LSMs" - default "landlock,lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK - default "landlock,lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR - default "landlock,lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO - default "landlock,lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC - default "landlock,lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" + default "nax,landlock,lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK + default "nax,landlock,lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR + default "nax,landlock,lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO + default "nax,landlock,lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC + default "nax,landlock,lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" 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
@@ -14,6 +14,7 @@ subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown subdir-$(CONFIG_BPF_LSM) += bpf subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock +subdir-$(CONFIG_SECURITY_NAX) += nax # always enable default capabilities obj-y += commoncap.o
@@ -34,6 +35,7 @@ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUPS) += device_cgroup.o obj-$(CONFIG_BPF_LSM) += bpf/ obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ +obj-$(CONFIG_SECURITY_NAX) += nax/ # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integrity
security/nax/Kconfig new file +114 -0
114 lines not on this page · raw message
security/nax/Makefile new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_SECURITY_NAX) := nax.o + +nax-y := nax-lsm.o
security/nax/nax-lsm.c new file +472 -0
472 lines not on this page · raw message
Signature · 1 line
2.26.2
Randy Dunlap
reviewerRe: [PATCH v3 1/1] NAX LSM: Add initial support
Hi--
security/nax/Kconfig line 5
Quoted 11 lines · Igor Zhbanov
security/nax/Kconfig new file +5
-0@@ -0,0 +1,114 @@ +# SPDX-License-Identifier: GPL-2.0-only +config SECURITY_NAX + bool "NAX support" + depends on SECURITY + default n
'default n' is the default value and hence it is redundant. We usually omit it.
Quoted 63 lines
security/nax/Kconfig new file +63
-0+ help + This selects NAX (No Anonymous Execution), which extends DAC + support with additional system-wide security settings beyond + regular Linux discretionary access controls. Currently, the only + available behavior is restricting the execution of anonymous and + modified pages. + + The module can restrict either privileged or all processes, + depending on the settings. It is possible to configure action, + performed when the violation is detected (log, log + block, + log + kill). + + Further information can be found in + Documentation/admin-guide/LSM/NAX.rst. + + If you are unsure how to answer this question, answer N. + +choice + prompt "NAX violation action mode" + default SECURITY_NAX_MODE_LOG + depends on SECURITY_NAX + help + Select the NAX violation action mode. + + In the default permissive mode the violations are only logged + (if logging is not suppressed). In the enforcing mode the violations + are prohibited. And in the kill mode the process is terminated. + + The value can be overridden at boot time with the kernel command-line + parameter "nax_mode=" (0, 1, 2) or "kernel.nax.mode=" (0, 1, 2) + sysctl parameter (if the settings are not locked). + + config SECURITY_NAX_MODE_LOG + bool "Permissive mode" + help + In this mode violations are only logged (if logging is not + suppressed by the "kernel.nax.quiet" parameter). The + violating system call will not be prohibited. + config SECURITY_NAX_MODE_ENFORCING + bool "Enforcing mode" + help + In this mode violations are prohibited and logged (if + logging is not suppressed by the "kernel.nax.quiet" + parameter). The violating system call will return -EACCES + error. + config SECURITY_NAX_MODE_KILL + bool "Kill mode" + help + In this mode the violating process is terminated on the + first violation system call. The violation event is logged + (if logging is not suppressed by the "kernel.nax.quiet" + parameter). +endchoice + +config SECURITY_NAX_MODE + int + depends on SECURITY_NAX + default 0 if SECURITY_NAX_MODE_LOG + default 1 if SECURITY_NAX_MODE_ENFORCING + default 2 if SECURITY_NAX_MODE_KILL + +config SECURITY_NAX_CHECK_ALL + bool "Check all processes"
security/nax/Kconfig line 78
Quoted 10 lines
security/nax/Kconfig new file +10
-0+ depends on SECURITY_NAX + help + If selected, NAX will check all processes. If not selected, NAX + will check only privileged processes (which is determined either + by having zero uid, euid, suid or fsuid; or by possessing + capabilities outside of allowed set). + + The value can also be overridden at boot time with the kernel + command-line parameter "nax_check_all=" (0, 1) or + "kernel.nax_check_all=" (0, 1) sysctl parameter (if the settings
kernel.nax.check_all ?
security/nax/Kconfig line 84
Quoted 6 lines
security/nax/Kconfig new file +6
-0+ are not locked). + +config SECURITY_NAX_ALLOWED_CAPS + hex "Process capabilities ignored by NAX" + default 0x0 + range 0x0 0xffffffffffff
Indent above line with tab + 2 spaces instead of all spaces.
Quoted 8 lines
security/nax/Kconfig new file +8
-0+ depends on SECURITY_NAX + help + Hexadecimal number representing the set of capabilities + a non-root process can possess without being considered + "privileged" by NAX LSM. + + The value can be overridden at boot time with the command-line + parameter "nax_allowed_caps=" or "kernel.nax.allowed_caps=" sysctl
security/nax/Kconfig line 102
Quoted 10 lines
security/nax/Kconfig new file +10
-0+ parameter (if the settings are not locked). + +config SECURITY_NAX_QUIET + bool "Silence NAX messages" + depends on SECURITY_NAX + help + If selected, NAX will not print violations. + + The value can be overridden at boot with the command-line + parameter "nax_quiet=" (0, 1) or "kernel.nax_quiet=" (0, 1) sysctl
kernel.nax.quiet
security/nax/Kconfig line 109
Quoted 7 lines
security/nax/Kconfig new file +7
-0+ parameter (if the settings are not locked). + +config SECURITY_NAX_LOCKED + bool "Lock NAX settings" + depends on SECURITY_NAX + help + Pevent any update to the settings of the NAX LSM. This applies to
Prevent
security/nax/Kconfig line 113
Quoted 4 lines
security/nax/Kconfig new file +4
-0+ both sysctl writes and the kernel command line. + + If not selected, it can be enabled at boot time with the kernel + command-line parameter "nax_locked=1" or "kernel.nax_locked=1"
kernel.nax.locked
Quoted 1 line
security/nax/Kconfig new file +1
-0+ sysctl parameter (if the settings are not locked).
Signature · 1 line
~Randy
Hi Igor,
Quoted 9 lines · Igor Zhbanov
[Overview]
Fileless malware attacks are becoming more and more popular, and even ready-to-use frameworks are available [1], [2], [3]. They are based on running of the malware code from anonymous executable memory pages (which are not backed by an executable file or a library on a filesystem.) This allows effectively hiding malware presence in a system, making filesystem integrity checking tools unable to detect the intrusion.
[snip]
Quoted 4 lines
[TODO]
- Implement xattrs support for marking privileged binaries on a per-file basis.
If/when you plan to add that, adding the new xattr to the list of EVM-protected xattrs may be worth discussing.
Quoted 5 lines
- Store NAX attributes in the per-task LSM blob to implement special launchers for the privileged processes, so all of the children processes of such a launcher would be allowed to have anonymous executable pages (but not to grandchildren).
[snip]
Overall I'm pleased to see this patch and I have no more remarks, outside of the few points Randy Dunlap raised.
Thanks,
Simon