Fork brute force attack mitigation
Subsystems (5)
v7 cover Fork brute force attack mitigation
Attacks against vulnerable userspace applications with the purpose to break ASLR or bypass canaries traditionally use some level of brute force with the help of the fork system call. This is possible since when creating a new process using fork its memory contents are the same as those of the parent process (the process that called the fork system call). So, the attacker can test the memory infinite times to find the correct memory values or the correct memory addresses without worrying about crashing the application.
Based on the above scenario it would be nice to have this detected and mitigated, and this is the goal of this patch serie. Specifically the following attacks are expected to be detected:
1.- Launching (fork()/exec()) a setuid/setgid process repeatedly until a
desirable memory layout is got (e.g. Stack Clash).
2.- Connecting to an exec()ing network daemon (e.g. xinetd) repeatedly
until a desirable memory layout is got (e.g. what CTFs do for simple network service).
3.- Launching processes without exec() (e.g. Android Zygote) and exposing
state to attack a sibling.
4.- Connecting to a fork()ing network daemon (e.g. apache) repeatedly until
the previously shared memory layout of all the other children is exposed (e.g. kind of related to HeartBleed).
In each case, a privilege boundary has been crossed:
Case 1: setuid/setgid process
Case 2: network to local
Case 3: privilege changes
Case 4: network to local
So, what will really be detected are fork/exec brute force attacks that cross any of the commented bounds.
The implementation details and comparison against other existing implementations can be found in the "Documentation" patch.
It is important to mention that this version has changed the method used to track the information related to the application crashes. Prior this version, a pointer per process (in the task_struct structure) held a reference to the shared statistical data. Or in other words, these stats were shared by all the fork hierarchy processes. But this has an important
drawback: a brute force attack that happens through the execve system call losts the faults info since these statistics are freed when the fork hierarchy disappears. So, the solution adopted in the v6 version was to use an upper fork hierarchy to track the info for this attack type. But, as Valdis Kletnieks pointed out during this discussion [1], this method can be easily bypassed using a double exec (well, this was the method used in the kselftest to avoid the detection ;) ). So, in this version, to track all the statistical data (info related with application crashes), the extended attributes feature for the executable files are used. The xattr is also used to mark the executables as "not allowed" when an attack is detected. Then, the execve system call rely on this flag to avoid following executions of this file.
[1] https://lore.kernel.org/kernelnewbies/20210330173459.GA3163@ubuntu/
Moreover, I think this solves another problem pointed out by Andi Kleen during the v5 review [2] related to the possibility that a supervisor respawns processes killed by the Brute LSM. He suggested adding some way so a supervisor can know that a process has been killed by Brute and then decide to respawn or not. So, now, the supervisor can read the brute xattr of one executable and know if it is blocked by Brute and why (using the statistical data).
[2] https://lore.kernel.org/kernel-hardening/[email protected]/
Knowing all this information I will explain now the different patches:
The 1/7 patch defines a new LSM hook to get the fatal signal of a task. This will be useful during the attack detection phase.
The 2/7 patch defines a new LSM and the necessary sysctl attributes to fine tuning the attack detection.
The 3/7 patch detects a fork/exec brute force attack and narrows the possible cases taken into account the privilege boundary crossing.
The 4/7 patch mitigates a brute force attack.
The 5/7 patch adds self-tests to validate the Brute LSM expectations.
The 6/7 patch adds the documentation to explain this implementation.
The 7/7 patch updates the maintainers file.
This patch serie is a task of the KSPP [3] and can also be accessed from my github tree [4] in the "brute_v7" branch.
[3] https://github.com/KSPP/linux/issues/39
[4] https://github.com/johwood/linux/
When I ran the "checkpatch" script I got the following errors, but I think they are false positives as I follow the same coding style for the others extended attributes suffixes.
----------------------------------------------------------------------------../patches/brute_v7/v7-0003-security-brute-Detect-a-brute-force-attack.patch
----------------------------------------------------------------------------
ERROR: Macros with complex values should be enclosed in parentheses 89: FILE: include/uapi/linux/xattr.h:80:
+#define XATTR_NAME_BRUTE XATTR_SECURITY_PREFIX XATTR_BRUTE_SUFFIX
-----------------------------------------------------------------------------../patches/brute_v7/v7-0005-selftests-brute-Add-tests-for-the-Brute-LSM.patch
-----------------------------------------------------------------------------
ERROR: Macros with complex values should be enclosed in parentheses 100: FILE: tools/testing/selftests/brute/rmxattr.c:18:
+#define XATTR_NAME_BRUTE XATTR_SECURITY_PREFIX XATTR_BRUTE_SUFFIX
When I ran the "kernel-doc" script with the following parameters:
./scripts/kernel-doc --none -v security/brute/brute.c
I got the following warning:
security/brute/brute.c:65: warning: contents before sections
But I don't understand why it is complaining. Could it be a false positive?
The previous versions can be found in:
RFC
https://lore.kernel.org/kernel-hardening/[email protected]/
Version 2
https://lore.kernel.org/kernel-hardening/[email protected]/
Version 3
https://lore.kernel.org/lkml/[email protected]/
Version 4
https://lore.kernel.org/lkml/[email protected]/
Version 5
https://lore.kernel.org/kernel-hardening/[email protected]/
Version 6
https://lore.kernel.org/kernel-hardening/[email protected]/
Changelog RFC -> v2
-------------------
- Rename this feature with a more suitable name (Jann Horn, Kees Cook).
- Convert the code to an LSM (Kees Cook).
- Add locking to avoid data races (Jann Horn).
- Add a new LSM hook to get the fatal signal of a task (Jann Horn, Kees Cook).
- Add the last crashes timestamps list to avoid false positives in the attack detection (Jann Horn).
- Use "period" instead of "rate" (Jann Horn).
- Other minor changes suggested (Jann Horn, Kees Cook).
Changelog v2 -> v3
------------------
- Compute the application crash period on an on-going basis (Kees Cook).
- Detect a brute force attack through the execve system call (Kees Cook).
- Detect an slow brute force attack (Randy Dunlap).
- Fine tuning the detection taken into account privilege boundary crossing (Kees Cook).
- Taken into account only fatal signals delivered by the kernel (Kees Cook).
- Remove the sysctl attributes to fine tuning the detection (Kees Cook).
- Remove the prctls to allow per process enabling/disabling (Kees Cook).
- Improve the documentation (Kees Cook).
- Fix some typos in the documentation (Randy Dunlap).
- Add self-test to validate the expectations (Kees Cook).
Changelog v3 -> v4
------------------
- Fix all the warnings shown by the tool "scripts/kernel-doc" (Randy Dunlap).
Changelog v4 -> v5
------------------
- Fix some typos (Randy Dunlap).
Changelog v5 -> v6
------------------
- Fix a reported deadlock (kernel test robot).
- Add high level details to the documentation (Andi Kleen).
Changelog v6 -> v7
------------------
- Add the "Reviewed-by:" tag to the first patch.
- Rearrange the brute LSM between lockdown and yama (Kees Cook).
- Split subdir and obj in security/Makefile (Kees Cook).
- Reduce the number of header files included (Kees Cook).
- Print the pid when an attack is detected (Kees Cook).
- Use the socket_accept LSM hook instead of socket_sock_rcv_skb hook to avoid running a hook on every incoming network packet (Kees Cook).
- Update the documentation and fix it to render it properly (Jonathan Corbet).
- Manage correctly an exec brute force attack avoiding the bypass (Valdis Kletnieks).
- Other minor changes and cleanups.
Any constructive comments are welcome.
Thanks in advance.
John Wood (7):
security: Add LSM hook at the point where a task gets a fatal signal security/brute: Define a LSM and add sysctl attributes security/brute: Detect a brute force attack security/brute: Mitigate a brute force attack selftests/brute: Add tests for the Brute LSM
Documentation: Add documentation for the Brute LSM
MAINTAINERS: Add a new entry for the Brute LSM
Documentation/admin-guide/LSM/Brute.rst | 334 +++++++++++ Documentation/admin-guide/LSM/index.rst | 1 + MAINTAINERS | 7 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 4 + include/linux/security.h | 4 + include/uapi/linux/xattr.h | 3 + kernel/signal.c | 1 + security/Kconfig | 11 +- security/Makefile | 2 + security/brute/Kconfig | 15 + security/brute/Makefile | 2 + security/brute/brute.c | 716 +++++++++++++++++++++++ security/security.c | 5 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/brute/.gitignore | 2 + tools/testing/selftests/brute/Makefile | 5 + tools/testing/selftests/brute/config | 1 + tools/testing/selftests/brute/rmxattr.c | 34 ++ tools/testing/selftests/brute/test.c | 507 ++++++++++++++++ tools/testing/selftests/brute/test.sh | 256 ++++++++ 21 files changed, 1907 insertions(+), 5 deletions(-) create mode 100644 Documentation/admin-guide/LSM/Brute.rst create mode 100644 security/brute/Kconfig create mode 100644 security/brute/Makefile create mode 100644 security/brute/brute.c create mode 100644 tools/testing/selftests/brute/.gitignore create mode 100644 tools/testing/selftests/brute/Makefile create mode 100644 tools/testing/selftests/brute/config create mode 100644 tools/testing/selftests/brute/rmxattr.c create mode 100644 tools/testing/selftests/brute/test.c create mode 100755 tools/testing/selftests/brute/test.sh
--
2.25.1
PATCH v7 1/7 security: Add LSM hook at the point where a task gets a fatal signal
Add a security hook that allows a LSM to be notified when a task gets a fatal signal. This patch is a previous step on the way to compute the task crash period by the "brute" LSM (linux security module to detect and mitigate fork brute force attack against vulnerable userspace processes).
--- include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 4 ++++ include/linux/security.h | 4 ++++ kernel/signal.c | 1 + security/security.c | 5 +++++ 5 files changed, 15 insertions(+)
include/linux/lsm_hook_defs.h +1 -0
@@ -225,6 +225,7 @@ LSM_HOOK(int, -ENOSYS, task_prctl, int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) LSM_HOOK(void, LSM_RET_VOID, task_to_inode, struct task_struct *p, struct inode *inode) +LSM_HOOK(void, LSM_RET_VOID, task_fatal_signal, const kernel_siginfo_t *siginfo) LSM_HOOK(int, 0, ipc_permission, struct kern_ipc_perm *ipcp, short flag) LSM_HOOK(void, LSM_RET_VOID, ipc_getsecid, struct kern_ipc_perm *ipcp, u32 *secid)
include/linux/lsm_hooks.h +4 -0
@@ -799,6 +799,10 @@* security attributes, e.g. for /proc/pid inodes. * @p contains the task_struct for the task. * @inode contains the inode structure for the inode. +* @task_fatal_signal: +* This hook allows security modules to be notified when a task gets a +* fatal signal. +* @siginfo contains the signal information. * * Security hooks for Netlink messaging. *
include/linux/security.h +4 -0
@@ -433,6 +433,7 @@ int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,int security_task_prctl (int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5);void security_task_to_inode (struct task_struct *p,struct inode *inode); +void security_task_fatal_signal (const kernel_siginfo_t *siginfo);int security_ipc_permission (struct kern_ipc_perm *ipcp,short flag);void security_ipc_getsecid (struct kern_ipc_perm *ipcp,u32 *secid);int security_msg_msg_alloc (struct msg_msg *msg);
@@ -1183,6 +1184,9 @@ static inline int security_task_prctl(int option, unsigned long arg2,static inline void security_task_to_inode (struct task_struct *p,struct inode *inode) { } +static inline void security_task_fatal_signal (const kernel_siginfo_t *siginfo) +{ } +static inline int security_ipc_permission (struct kern_ipc_perm *ipcp,short flag) {
kernel/signal.c +1 -0
@@ -2787,6 +2787,7 @@ bool get_signal(struct ksignal *ksig)/* * Anything else is fatal, maybe with a core dump. */ +security_task_fatal_signal (&ksig->info); current->flags |=PF_SIGNALED ;if (sig_kernel_coredump (signr)) {
security/security.c +5 -0
@@ -1891,6 +1891,11 @@ void security_task_to_inode(struct task_struct *p, struct inode *inode)call_void_hook (task_to_inode, p, inode); } +void security_task_fatal_signal (const kernel_siginfo_t *siginfo) +{ +call_void_hook (task_fatal_signal, siginfo); +} +int security_ipc_permission (struct kern_ipc_perm *ipcp,short flag) {return call_int_hook (ipc_permission,0 , ipcp, flag);
--
2.25.1
PATCH v7 2/7 security/brute: Define a LSM and add sysctl attributes
In reply to John WoodAdd a new Kconfig file to define a menu entry under "Security options" to enable the "Fork brute force attack detection and mitigation" feature.
The detection of a brute force attack can be based on the number of faults per application and its crash rate.
There are two types of brute force attacks that can be detected. The first one is a slow brute force attack that is detected if the maximum number of faults per fork hierarchy is reached. The second type is a fast brute force attack that is detected if the application crash period falls below a certain threshold.
The application crash period must be a value that is not prone to change due to spurious data and follows the real crash period. So, to compute it, the exponential moving average (EMA) will be used.
This kind of average defines a weight (between 0 and 1) for the new value to add and applies the remainder of the weight to the current average value. This way, some spurious data will not excessively modify the average and only if the new values are persistent, the moving average will tend towards them.
Mathematically the application crash period's EMA can be expressed as follows:
period_ema = period * weight + period_ema * (1 - weight)
Moreover, it is important to note that a minimum number of faults is needed to guarantee a trend in the crash period when the EMA is used.
So, based on all the previous information define a LSM with five sysctl attributes that will be used to fine tune the attack detection.
ema_weight_numeratorema_weight_denominatormax_faultsmin_faultscrash_period_threshold
This patch is a previous step on the way to fine tune the attack detection.
--- security/Kconfig | 11 +-- security/Makefile | 2 + security/brute/Kconfig | 14 ++++ security/brute/Makefile | 2 + security/brute/brute.c | 147 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 security/brute/Kconfig create mode 100644 security/brute/Makefile create mode 100644 security/brute/brute.c
security/Kconfig +6 -5
@@ -241,6 +241,7 @@ source "security/lockdown/Kconfig" source "security/landlock/Kconfig" source "security/integrity/Kconfig" +source "security/brute/Kconfig" choice prompt "First legacy 'major LSM' to be initialized"
@@ -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 "landlock,lockdown,brute,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK + default "landlock,lockdown,brute,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR + default "landlock,lockdown,brute,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO + default "landlock,lockdown,brute,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC + default "landlock,lockdown,brute,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_FORK_BRUTE) += brute # 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_FORK_BRUTE) += brute/ # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integrity
security/brute/Kconfig new file +14 -0
@@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0 +config SECURITY_FORK_BRUTE + bool "Fork brute force attack detection and mitigation" + depends on SECURITY + help + This is an LSM that stops any fork brute force attack against + vulnerable userspace processes. The detection method is based on + the application crash period and as a mitigation procedure all the + offending tasks are killed. Also, the executable file involved in the + attack will be marked as "not allowed" and new execve system calls + using this file will fail. Like capabilities, this security module + stacks with other LSMs. + + If you are unsure how to answer this question, answer N.
security/brute/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_SECURITY_FORK_BRUTE) += brute.o
security/brute/brute.c new file +147 -0
147 lines not on this page · raw message
--
2.25.1
PATCH v7 3/7 security/brute: Detect a brute force attack
In reply to John WoodFor a correct management of a fork brute force attack it is necessary to track all the information related to the application crashes. To do so, use the extended attributes (xattr) of the executable files and define a statistical data structure to hold all the necessary information shared by all the fork hierarchy processes. This info is the number of crashes, the last crash timestamp and the crash period's moving average.
The same can be achieved using a pointer to the fork hierarchy statistical data held by the task_struct structure. But this has an important drawback: a brute force attack that happens through the execve system call losts the faults info since these statistics are freed when the fork hierarchy disappears. Using this method makes not possible to manage this attack type that can be successfully treated using extended attributes.
Also, to avoid false positives during the attack detection it is necessary to narrow the possible cases. So, only the following scenarios are taken into account:
1.- Launching (fork()/exec()) a setuid/setgid process repeatedly until a
desirable memory layout is got (e.g. Stack Clash).
2.- Connecting to an exec()ing network daemon (e.g. xinetd) repeatedly
until a desirable memory layout is got (e.g. what CTFs do for simple network service).
3.- Launching processes without exec() (e.g. Android Zygote) and
exposing state to attack a sibling.
4.- Connecting to a fork()ing network daemon (e.g. apache) repeatedly
until the previously shared memory layout of all the other children is exposed (e.g. kind of related to HeartBleed).
In each case, a privilege boundary has been crossed:
Case 1: setuid/setgid process
Case 2: network to local
Case 3: privilege changes
Case 4: network to local
To mark that a privilege boundary has been crossed it is only necessary to create a new stats for the executable file via the extended attribute and only if it has no previous statistical data. This is done using four different LSM hooks, one per privilege boundary:
setuid/setgid process --> bprm_creds_from_file hook (based on secureexec
flag).
network to local -------> socket_accept hook (taking into account only
external connections).
privilege changes ------> task_fix_setuid and task_fix_setgid hooks.
To detect a brute force attack it is necessary that the executable file statistics be updated in every fatal crash and the most important data to update is the application crash period. To do so, use the new "task_fatal_signal" LSM hook added in a previous step.
The application crash period must be a value that is not prone to change due to spurious data and follows the real crash period. So, to compute it, the exponential moving average (EMA) is used.
Based on the updated statistics two different attacks can be handled. A slow brute force attack that is detected if the maximum number of faults per fork hierarchy is reached and a fast brute force attack that is detected if the application crash period falls below a certain threshold.
Moreover, only the signals delivered by the kernel are taken into account with the exception of the SIGABRT signal since the latter is used by glibc for stack canary, malloc, etc failures, which may indicate that a mitigation has been triggered.
--- include/uapi/linux/xattr.h | 3 + security/brute/brute.c | 478 +++++++++++++++++++++++++++++++++++++ 2 files changed, 481 insertions(+)
include/uapi/linux/xattr.h +3 -0
@@ -76,6 +76,9 @@#define XATTR_CAPS_SUFFIX "capability"#define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX +#define XATTR_BRUTE_SUFFIX "brute" +#define XATTR_NAME_BRUTE XATTR_SECURITY_PREFIX XATTR_BRUTE_SUFFIX +#define XATTR_POSIX_ACL_ACCESS "posix_acl_access"#define XATTR_NAME_POSIX_ACL_ACCESS XATTR_SYSTEM_PREFIX XATTR_POSIX_ACL_ACCESS#define XATTR_POSIX_ACL_DEFAULT "posix_acl_default"
security/brute/brute.c +478 -0
510 lines not on this page · raw message
--
2.25.1
kernel test robot
build botRe: [PATCH v7 3/7] security/brute: Detect a brute force attack
Thank you for the patch! Yet something to improve:
Hi John,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on security/next-testing]
[also build test ERROR on kselftest/next linus/master v5.13-rc2 next-20210521] [cannot apply to kees/for-next/pstore]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/John-Wood/Fork-brute-force-attack-mitigation/20210522-203100 base: https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git next-testing config: sh-randconfig-s031-20210522 (attached as .config) compiler: sh4-linux-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-341-g8af24329-dirty
# https://github.com/0day-ci/linux/commit/7dcc9f3a699718cccd18074ec2ebb56ca4b68287
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review John-Wood/Fork-brute-force-attack-mitigation/20210522-203100
git checkout 7dcc9f3a699718cccd18074ec2ebb56ca4b68287
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=sh
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/asm-generic/bug.h:5,
from arch/sh/include/asm/bug.h:112,
from include/linux/bug.h:5,
from include/linux/thread_info.h:12,
from include/asm-generic/current.h:5,
from ./arch/sh/include/generated/asm/current.h:1,
from include/linux/sched.h:12,
from include/linux/binfmts.h:5,
from security/brute/brute.c:5:
security/brute/brute.c: In function 'brute_socket_accept':
>> include/net/sock.h:383:37: error: 'struct sock_common' has no member named 'skc_v6_rcv_saddr'; did you mean 'skc_rcv_saddr'?
383 | #define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
| ^~~~~~~~~~~~~~~~
include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
58 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
| ^~~~
security/brute/brute.c:511:2: note: in expansion of macro 'if'
511 | if (sk->sk_family == AF_UNIX || sk->sk_family == AF_NETLINK ||
| ^~
security/brute/brute.c:513:27: note: in expansion of macro 'sk_v6_rcv_saddr'
513 | ipv6_addr_equal(&sk->sk_v6_rcv_saddr, &in6addr_loopback))
| ^~~~~~~~~~~~~~~
>> include/net/sock.h:383:37: error: 'struct sock_common' has no member named 'skc_v6_rcv_saddr'; did you mean 'skc_rcv_saddr'?
383 | #define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
| ^~~~~~~~~~~~~~~~
include/linux/compiler.h:58:61: note: in definition of macro '__trace_if_var'
58 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
| ^~~~
security/brute/brute.c:511:2: note: in expansion of macro 'if'
511 | if (sk->sk_family == AF_UNIX || sk->sk_family == AF_NETLINK ||
| ^~
security/brute/brute.c:513:27: note: in expansion of macro 'sk_v6_rcv_saddr'
513 | ipv6_addr_equal(&sk->sk_v6_rcv_saddr, &in6addr_loopback))
| ^~~~~~~~~~~~~~~
>> include/net/sock.h:383:37: error: 'struct sock_common' has no member named 'skc_v6_rcv_saddr'; did you mean 'skc_rcv_saddr'?
383 | #define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
| ^~~~~~~~~~~~~~~~
include/linux/compiler.h:69:3: note: in definition of macro '__trace_if_value'
69 | (cond) ? \
| ^~~~
include/linux/compiler.h:56:28: note: in expansion of macro '__trace_if_var'
56 | #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
| ^~~~~~~~~~~~~~
security/brute/brute.c:511:2: note: in expansion of macro 'if'
511 | if (sk->sk_family == AF_UNIX || sk->sk_family == AF_NETLINK ||
| ^~
security/brute/brute.c:513:27: note: in expansion of macro 'sk_v6_rcv_saddr'
513 | ipv6_addr_equal(&sk->sk_v6_rcv_saddr, &in6addr_loopback))
| ^~~~~~~~~~~~~~~
In file included from security/brute/brute.c:6:
security/brute/brute.c: At top level:
>> include/linux/lsm_hooks.h:1602:32: error: 'struct security_hook_heads' has no member named 'socket_accept'
1602 | { .head = &security_hook_heads.HEAD, .hook = { .HEAD = HOOK } }
| ^
security/brute/brute.c:527:2: note: in expansion of macro 'LSM_HOOK_INIT'
527 | LSM_HOOK_INIT(socket_accept, brute_socket_accept),
| ^~~~~~~~~~~~~
>> security/brute/brute.c:527:16: error: 'union security_list_options' has no member named 'socket_accept'
527 | LSM_HOOK_INIT(socket_accept, brute_socket_accept),
| ^~~~~~~~~~~~~
include/linux/lsm_hooks.h:1602:50: note: in definition of macro 'LSM_HOOK_INIT'
1602 | { .head = &security_hook_heads.HEAD, .hook = { .HEAD = HOOK } }
| ^~~~
>> security/brute/brute.c:527:31: error: initialization of 'int (*)(struct task_struct *)' from incompatible pointer type 'int (*)(struct socket *, struct socket *)' [-Werror=incompatible-pointer-types]
527 | LSM_HOOK_INIT(socket_accept, brute_socket_accept),
| ^~~~~~~~~~~~~~~~~~~
include/linux/lsm_hooks.h:1602:57: note: in definition of macro 'LSM_HOOK_INIT'
1602 | { .head = &security_hook_heads.HEAD, .hook = { .HEAD = HOOK } }
| ^~~~
security/brute/brute.c:527:31: note: (near initialization for 'brute_hooks[4].hook.binder_set_context_mgr')
527 | LSM_HOOK_INIT(socket_accept, brute_socket_accept),
| ^~~~~~~~~~~~~~~~~~~
include/linux/lsm_hooks.h:1602:57: note: in definition of macro 'LSM_HOOK_INIT'
1602 | { .head = &security_hook_heads.HEAD, .hook = { .HEAD = HOOK } }
| ^~~~cc1: some warnings being treated as errors
vim +383 include/net/sock.h
4dc6dc7162c08b Eric Dumazet 2009-07-15 362
68835aba4d9b74 Eric Dumazet 2010-11-30 363 #define sk_dontcopy_begin __sk_common.skc_dontcopy_begin
68835aba4d9b74 Eric Dumazet 2010-11-30 364 #define sk_dontcopy_end __sk_common.skc_dontcopy_end
4dc6dc7162c08b Eric Dumazet 2009-07-15 365 #define sk_hash __sk_common.skc_hash
5080546682bae3 Eric Dumazet 2013-10-02 366 #define sk_portpair __sk_common.skc_portpair
05dbc7b59481ca Eric Dumazet 2013-10-03 367 #define sk_num __sk_common.skc_num
05dbc7b59481ca Eric Dumazet 2013-10-03 368 #define sk_dport __sk_common.skc_dport
5080546682bae3 Eric Dumazet 2013-10-02 369 #define sk_addrpair __sk_common.skc_addrpair
5080546682bae3 Eric Dumazet 2013-10-02 370 #define sk_daddr __sk_common.skc_daddr
5080546682bae3 Eric Dumazet 2013-10-02 371 #define sk_rcv_saddr __sk_common.skc_rcv_saddr
^1da177e4c3f41 Linus Torvalds 2005-04-16 372 #define sk_family __sk_common.skc_family
^1da177e4c3f41 Linus Torvalds 2005-04-16 373 #define sk_state __sk_common.skc_state
^1da177e4c3f41 Linus Torvalds 2005-04-16 374 #define sk_reuse __sk_common.skc_reuse
055dc21a1d1d21 Tom Herbert 2013-01-22 375 #define sk_reuseport __sk_common.skc_reuseport
9fe516ba3fb29b Eric Dumazet 2014-06-27 376 #define sk_ipv6only __sk_common.skc_ipv6only
26abe14379f8e2 Eric W. Biederman 2015-05-08 377 #define sk_net_refcnt __sk_common.skc_net_refcnt
^1da177e4c3f41 Linus Torvalds 2005-04-16 378 #define sk_bound_dev_if __sk_common.skc_bound_dev_if
^1da177e4c3f41 Linus Torvalds 2005-04-16 379 #define sk_bind_node __sk_common.skc_bind_node
8feaf0c0a5488b Arnaldo Carvalho de Melo 2005-08-09 380 #define sk_prot __sk_common.skc_prot
07feaebfcc10cd Eric W. Biederman 2007-09-12 381 #define sk_net __sk_common.skc_net
efe4208f47f907 Eric Dumazet 2013-10-03 382 #define sk_v6_daddr __sk_common.skc_v6_daddr
efe4208f47f907 Eric Dumazet 2013-10-03 @383 #define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
33cf7c90fe2f97 Eric Dumazet 2015-03-11 384 #define sk_cookie __sk_common.skc_cookie
70da268b569d32 Eric Dumazet 2015-10-08 385 #define sk_incoming_cpu __sk_common.skc_incoming_cpu
8e5eb54d303b7c Eric Dumazet 2015-10-08 386 #define sk_flags __sk_common.skc_flags
ed53d0ab761f5c Eric Dumazet 2015-10-08 387 #define sk_rxhash __sk_common.skc_rxhash
efe4208f47f907 Eric Dumazet 2013-10-03 388
^1da177e4c3f41 Linus Torvalds 2005-04-16 389 socket_lock_t sk_lock;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 390 atomic_t sk_drops;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 391 int sk_rcvlowat;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 392 struct sk_buff_head sk_error_queue;
8b27dae5a2e89a Eric Dumazet 2019-03-22 393 struct sk_buff *sk_rx_skb_cache;
b178bb3dfc30d9 Eric Dumazet 2010-11-16 394 struct sk_buff_head sk_receive_queue;
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 395 /*
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 396 * The backlog queue is special, it is always used with
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 397 * the per-socket spinlock held and requires low latency
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 398 * access. Therefore we special case it's implementation.
b178bb3dfc30d9 Eric Dumazet 2010-11-16 399 * Note : rmem_alloc is in this structure to fill a hole
b178bb3dfc30d9 Eric Dumazet 2010-11-16 400 * on 64bit arches, not because its logically part of
b178bb3dfc30d9 Eric Dumazet 2010-11-16 401 * backlog.
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 402 */
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 403 struct {
b178bb3dfc30d9 Eric Dumazet 2010-11-16 404 atomic_t rmem_alloc;
b178bb3dfc30d9 Eric Dumazet 2010-11-16 405 int len;
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 406 struct sk_buff *head;
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 407 struct sk_buff *tail;
fa438ccfdfd3f6 Eric Dumazet 2007-03-04 408 } sk_backlog;
b178bb3dfc30d9 Eric Dumazet 2010-11-16 409 #define sk_rmem_alloc sk_backlog.rmem_alloc
2c8c56e15df3d4 Eric Dumazet 2014-11-11 410
9115e8cd2a0c6e Eric Dumazet 2016-12-03 411 int sk_forward_alloc;
e0d1095ae34054 Cong Wang 2013-08-01 412 #ifdef CONFIG_NET_RX_BUSY_POLL
dafcc4380deec2 Eliezer Tamir 2013-06-14 413 unsigned int sk_ll_usec;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 414 /* ===== mostly read cache line ===== */
9115e8cd2a0c6e Eric Dumazet 2016-12-03 415 unsigned int sk_napi_id;
b178bb3dfc30d9 Eric Dumazet 2010-11-16 416 #endif
b178bb3dfc30d9 Eric Dumazet 2010-11-16 417 int sk_rcvbuf;
b178bb3dfc30d9 Eric Dumazet 2010-11-16 418
b178bb3dfc30d9 Eric Dumazet 2010-11-16 419 struct sk_filter __rcu *sk_filter;
ceb5d58b217098 Eric Dumazet 2015-11-29 420 union {
eaefd1105bc431 Eric Dumazet 2011-02-18 421 struct socket_wq __rcu *sk_wq;
66256e0b15bd72 Randy Dunlap 2020-02-15 422 /* private: */
ceb5d58b217098 Eric Dumazet 2015-11-29 423 struct socket_wq *sk_wq_raw;
66256e0b15bd72 Randy Dunlap 2020-02-15 424 /* public: */
ceb5d58b217098 Eric Dumazet 2015-11-29 425 };
def8b4faff5ca3 Alexey Dobriyan 2008-10-28 426 #ifdef CONFIG_XFRM
d188ba86dd07a7 Eric Dumazet 2015-12-08 427 struct xfrm_policy __rcu *sk_policy[2];
def8b4faff5ca3 Alexey Dobriyan 2008-10-28 428 #endif
deaa58542b21d2 Eric Dumazet 2012-06-24 429 struct dst_entry *sk_rx_dst;
0e36cbb344575e Cong Wang 2013-01-22 430 struct dst_entry __rcu *sk_dst_cache;
^1da177e4c3f41 Linus Torvalds 2005-04-16 431 atomic_t sk_omem_alloc;
4e07a91c37c69e Arnaldo Carvalho de Melo 2007-05-29 432 int sk_sndbuf;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 433
9115e8cd2a0c6e Eric Dumazet 2016-12-03 434 /* ===== cache line for TX ===== */
9115e8cd2a0c6e Eric Dumazet 2016-12-03 435 int sk_wmem_queued;
14afee4b6092fd Reshetova, Elena 2017-06-30 436 refcount_t sk_wmem_alloc;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 437 unsigned long sk_tsq_flags;
75c119afe14f74 Eric Dumazet 2017-10-05 438 union {
9115e8cd2a0c6e Eric Dumazet 2016-12-03 439 struct sk_buff *sk_send_head;
75c119afe14f74 Eric Dumazet 2017-10-05 440 struct rb_root tcp_rtx_queue;
75c119afe14f74 Eric Dumazet 2017-10-05 441 };
472c2e07eef045 Eric Dumazet 2019-03-22 442 struct sk_buff *sk_tx_skb_cache;
^1da177e4c3f41 Linus Torvalds 2005-04-16 443 struct sk_buff_head sk_write_queue;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 444 __s32 sk_peek_off;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 445 int sk_write_pending;
9b8805a325591c Julian Anastasov 2017-02-06 446 __u32 sk_dst_pending_confirm;
218af599fa635b Eric Dumazet 2017-05-16 447 u32 sk_pacing_status; /* see enum sk_pacing */
9115e8cd2a0c6e Eric Dumazet 2016-12-03 448 long sk_sndtimeo;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 449 struct timer_list sk_timer;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 450 __u32 sk_priority;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 451 __u32 sk_mark;
76a9ebe811fb3d Eric Dumazet 2018-10-15 452 unsigned long sk_pacing_rate; /* bytes per second */
76a9ebe811fb3d Eric Dumazet 2018-10-15 453 unsigned long sk_max_pacing_rate;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 454 struct page_frag sk_frag;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 455 netdev_features_t sk_route_caps;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 456 netdev_features_t sk_route_nocaps;
0a6b2a1dc2a210 Eric Dumazet 2018-02-19 457 netdev_features_t sk_route_forced_caps;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 458 int sk_gso_type;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 459 unsigned int sk_gso_max_size;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 460 gfp_t sk_allocation;
9115e8cd2a0c6e Eric Dumazet 2016-12-03 461 __u32 sk_txhash;
fc64869c48494a Andrey Ryabinin 2016-05-18 462
fc64869c48494a Andrey Ryabinin 2016-05-18 463 /*
fc64869c48494a Andrey Ryabinin 2016-05-18 464 * Because of non atomicity rules, all
fc64869c48494a Andrey Ryabinin 2016-05-18 465 * changes are protected by socket lock.
fc64869c48494a Andrey Ryabinin 2016-05-18 466 */
bf9765145b856f Mat Martineau 2020-01-09 467 u8 sk_padding : 1,
cdfbabfb2f0ce9 David Howells 2017-03-09 468 sk_kern_sock : 1,
28448b80456fea Tom Herbert 2014-05-23 469 sk_no_check_tx : 1,
28448b80456fea Tom Herbert 2014-05-23 470 sk_no_check_rx : 1,
bf9765145b856f Mat Martineau 2020-01-09 471 sk_userlocks : 4;
3a9b76fd0db9f0 Eric Dumazet 2017-11-11 472 u8 sk_pacing_shift;
bf9765145b856f Mat Martineau 2020-01-09 473 u16 sk_type;
bf9765145b856f Mat Martineau 2020-01-09 474 u16 sk_protocol;
bf9765145b856f Mat Martineau 2020-01-09 475 u16 sk_gso_max_segs;
^1da177e4c3f41 Linus Torvalds 2005-04-16 476 unsigned long sk_lingertime;
476e19cfa131e2 Arnaldo Carvalho de Melo 2005-05-05 477 struct proto *sk_prot_creator;
^1da177e4c3f41 Linus Torvalds 2005-04-16 478 rwlock_t sk_callback_lock;
^1da177e4c3f41 Linus Torvalds 2005-04-16 479 int sk_err,
^1da177e4c3f41 Linus Torvalds 2005-04-16 480 sk_err_soft;
becb74f0acca19 Eric Dumazet 2015-03-19 481 u32 sk_ack_backlog;
becb74f0acca19 Eric Dumazet 2015-03-19 482 u32 sk_max_ack_backlog;
86741ec25462e4 Lorenzo Colitti 2016-11-04 483 kuid_t sk_uid;
7fd3253a7de6a3 Björn Töpel 2020-11-30 484 #ifdef CONFIG_NET_RX_BUSY_POLL
7fd3253a7de6a3 Björn Töpel 2020-11-30 485 u8 sk_prefer_busy_poll;
7c951cafc0cb2e Björn Töpel 2020-11-30 486 u16 sk_busy_poll_budget;
7fd3253a7de6a3 Björn Töpel 2020-11-30 487 #endif
109f6e39fa07c4 Eric W. Biederman 2010-06-13 488 struct pid *sk_peer_pid;
109f6e39fa07c4 Eric W. Biederman 2010-06-13 489 const struct cred *sk_peer_cred;
^1da177e4c3f41 Linus Torvalds 2005-04-16 490 long sk_rcvtimeo;
b7aa0bf70c4afb Eric Dumazet 2007-04-19 491 ktime_t sk_stamp;
3a0ed3e9619738 Deepa Dinamani 2018-12-27 492 #if BITS_PER_LONG==32
3a0ed3e9619738 Deepa Dinamani 2018-12-27 493 seqlock_t sk_stamp_seq;
3a0ed3e9619738 Deepa Dinamani 2018-12-27 494 #endif
b9f40e21ef4298 Willem de Bruijn 2014-08-04 495 u16 sk_tsflags;
fc64869c48494a Andrey Ryabinin 2016-05-18 496 u8 sk_shutdown;
09c2d251b70723 Willem de Bruijn 2014-08-04 497 u32 sk_tskey;
52267790ef52d7 Willem de Bruijn 2017-08-03 498 atomic_t sk_zckey;
80b14dee2bea12 Richard Cochran 2018-07-03 499
80b14dee2bea12 Richard Cochran 2018-07-03 500 u8 sk_clockid;
80b14dee2bea12 Richard Cochran 2018-07-03 501 u8 sk_txtime_deadline_mode : 1,
4b15c707535266 Jesus Sanchez-Palencia 2018-07-03 502 sk_txtime_report_errors : 1,
4b15c707535266 Jesus Sanchez-Palencia 2018-07-03 503 sk_txtime_unused : 6;
80b14dee2bea12 Richard Cochran 2018-07-03 504
^1da177e4c3f41 Linus Torvalds 2005-04-16 505 struct socket *sk_socket;
^1da177e4c3f41 Linus Torvalds 2005-04-16 506 void *sk_user_data;
d5f642384e9da7 Alexey Dobriyan 2008-11-04 507 #ifdef CONFIG_SECURITY
^1da177e4c3f41 Linus Torvalds 2005-04-16 508 void *sk_security;
d5f642384e9da7 Alexey Dobriyan 2008-11-04 509 #endif
2a56a1fec290bf Tejun Heo 2015-12-07 510 struct sock_cgroup_data sk_cgrp_data;
baac50bbc3cdfd Johannes Weiner 2016-01-14 511 struct mem_cgroup *sk_memcg;
^1da177e4c3f41 Linus Torvalds 2005-04-16 512 void (*sk_state_change)(struct sock *sk);
676d23690fb62b David S. Miller 2014-04-11 513 void (*sk_data_ready)(struct sock *sk);
^1da177e4c3f41 Linus Torvalds 2005-04-16 514 void (*sk_write_space)(struct sock *sk);
^1da177e4c3f41 Linus Torvalds 2005-04-16 515 void (*sk_error_report)(struct sock *sk);
^1da177e4c3f41 Linus Torvalds 2005-04-16 516 int (*sk_backlog_rcv)(struct sock *sk,
^1da177e4c3f41 Linus Torvalds 2005-04-16 517 struct sk_buff *skb);
ebf4e808fa0b22 Ilya Lesokhin 2018-04-30 518 #ifdef CONFIG_SOCK_VALIDATE_XMIT
ebf4e808fa0b22 Ilya Lesokhin 2018-04-30 519 struct sk_buff* (*sk_validate_xmit_skb)(struct sock *sk,
ebf4e808fa0b22 Ilya Lesokhin 2018-04-30 520 struct net_device *dev,
ebf4e808fa0b22 Ilya Lesokhin 2018-04-30 521 struct sk_buff *skb);
ebf4e808fa0b22 Ilya Lesokhin 2018-04-30 522 #endif
^1da177e4c3f41 Linus Torvalds 2005-04-16 523 void (*sk_destruct)(struct sock *sk);
ef456144da8ef5 Craig Gallek 2016-01-04 524 struct sock_reuseport __rcu *sk_reuseport_cb;
6ac99e8f23d4b1 Martin KaFai Lau 2019-04-26 525 #ifdef CONFIG_BPF_SYSCALL
1f00d375af84fb KP Singh 2020-08-25 526 struct bpf_local_storage __rcu *sk_bpf_storage;
6ac99e8f23d4b1 Martin KaFai Lau 2019-04-26 527 #endif
a4298e4522d687 Eric Dumazet 2016-04-01 528 struct rcu_head sk_rcu;
^1da177e4c3f41 Linus Torvalds 2005-04-16 529 };
^1da177e4c3f41 Linus Torvalds 2005-04-16 530 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 8 lines
Moreover, I think this solves another problem pointed out by Andi Kleen during the v5 review [2] related to the possibility that a supervisor respawns processes killed by the Brute LSM. He suggested adding some way so a supervisor can know that a process has been killed by Brute and then decide to respawn or not. So, now, the supervisor can read the brute xattr of one executable and know if it is blocked by Brute and why (using the statistical data).
It looks better now, Thank.
One potential problem is that the supervisor might see the executable directly, but run it through some wrapper. In fact I suspect that will be fairly common with complex daemons. So it couldn't directly look at the xattr. Might be useful to also pass this information through the wait* chain, so that the supervisor can directly collect it. That would need some extension to these system calls.
-Andi
Quoted 181 lines
[2] https://lore.kernel.org/kernel-hardening/[email protected]/
Knowing all this information I will explain now the different patches:
The 1/7 patch defines a new LSM hook to get the fatal signal of a task. This will be useful during the attack detection phase.
The 2/7 patch defines a new LSM and the necessary sysctl attributes to fine tuning the attack detection.
The 3/7 patch detects a fork/exec brute force attack and narrows the possible cases taken into account the privilege boundary crossing.
The 4/7 patch mitigates a brute force attack.
The 5/7 patch adds self-tests to validate the Brute LSM expectations.
The 6/7 patch adds the documentation to explain this implementation.
The 7/7 patch updates the maintainers file.
This patch serie is a task of the KSPP [3] and can also be accessed from my github tree [4] in the "
brute_v7" branch.[3] https://github.com/KSPP/linux/issues/39
[4] https://github.com/johwood/linux/When I ran the "checkpatch" script I got the following errors, but I think they are false positives as I follow the same coding style for the others extended attributes suffixes.
----------------------------------------------------------------------------
../patches/brute_v7/v7-0003-security-brute-Detect-a-brute-force-attack.patch
----------------------------------------------------------------------------
ERROR: Macros with complex values should be enclosed in parentheses 89: FILE:include/uapi/linux/xattr.h:80:
+#defineXATTR_NAME_BRUTEXATTR_SECURITY_PREFIXXATTR_BRUTE_SUFFIX-----------------------------------------------------------------------------
../patches/brute_v7/v7-0005-selftests-brute-Add-tests-for-the-Brute-LSM.patch
-----------------------------------------------------------------------------
ERROR: Macros with complex values should be enclosed in parentheses 100: FILE:tools/testing/selftests/brute/rmxattr.c:18:
+#defineXATTR_NAME_BRUTEXATTR_SECURITY_PREFIXXATTR_BRUTE_SUFFIXWhen I ran the "kernel-doc" script with the following parameters:
./scripts/kernel-doc--none -vsecurity/brute/brute.cI got the following warning:
security/brute/brute.c:65: warning: contents before sectionsBut I don't understand why it is complaining. Could it be a false positive?
The previous versions can be found in:
RFC
https://lore.kernel.org/kernel-hardening/[email protected]/Version 2
https://lore.kernel.org/kernel-hardening/[email protected]/Version 3
https://lore.kernel.org/lkml/[email protected]/Version 4
https://lore.kernel.org/lkml/[email protected]/Version 5
https://lore.kernel.org/kernel-hardening/[email protected]/Version 6
https://lore.kernel.org/kernel-hardening/[email protected]/Changelog RFC -> v2
-------------------
- Rename this feature with a more suitable name (Jann Horn, Kees Cook).
- Convert the code to an LSM (Kees Cook).
- Add locking to avoid data races (Jann Horn).
- Add a new LSM hook to get the fatal signal of a task (Jann Horn, Kees Cook).
- Add the last crashes timestamps list to avoid false positives in the attack detection (Jann Horn).
- Use "period" instead of "rate" (Jann Horn).
- Other minor changes suggested (Jann Horn, Kees Cook).Changelog v2 -> v3
------------------
- Compute the application crash period on an on-going basis (Kees Cook).
- Detect a brute force attack through the execve system call (Kees Cook).
- Detect an slow brute force attack (Randy Dunlap).
- Fine tuning the detection taken into account privilege boundary crossing (Kees Cook).
- Taken into account only fatal signals delivered by the kernel (Kees Cook).
- Remove the sysctl attributes to fine tuning the detection (Kees Cook).
- Remove the prctls to allow per process enabling/disabling (Kees Cook).
- Improve the documentation (Kees Cook).
- Fix some typos in the documentation (Randy Dunlap).
- Add self-test to validate the expectations (Kees Cook).Changelog v3 -> v4
------------------
- Fix all the warnings shown by the tool "scripts/kernel-doc" (Randy Dunlap).Changelog v4 -> v5
------------------
- Fix some typos (Randy Dunlap).Changelog v5 -> v6
------------------
- Fix a reported deadlock (kernel test robot).
- Add high level details to the documentation (Andi Kleen).Changelog v6 -> v7
------------------- Add the "Reviewed-by:" tag to the first patch.
- Rearrange the brute LSM between lockdown and yama (Kees Cook).
- Split subdir and obj insecurity/Makefile(Kees Cook).
- Reduce the number of header files included (Kees Cook).
- Print the pid when an attack is detected (Kees Cook).
- Use thesocket_acceptLSM hook instead ofsocket_sock_rcv_skbhook to avoid running a hook on every incoming network packet (Kees Cook).
- Update the documentation and fix it to render it properly (Jonathan Corbet).
- Manage correctly an exec brute force attack avoiding the bypass (Valdis Kletnieks).
- Other minor changes and cleanups.Any constructive comments are welcome.
Thanks in advance.John Wood (7):
security: Add LSM hook at the point where a task gets a fatal signalsecurity/brute: Define a LSM and add sysctl attributessecurity/brute: Detect a brute force attacksecurity/brute: Mitigate a brute force attack selftests/brute: Add tests for the Brute LSM
Documentation: Add documentation for the Brute LSM
MAINTAINERS: Add a new entry for the Brute LSMDocumentation/admin-guide/LSM/Brute.rst | 334 +++++++++++ Documentation/admin-guide/LSM/index.rst | 1 + MAINTAINERS | 7 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 4 + include/linux/security.h | 4 + include/uapi/linux/xattr.h | 3 + kernel/signal.c | 1 + security/Kconfig | 11 +- security/Makefile | 2 + security/brute/Kconfig | 15 + security/brute/Makefile | 2 + security/brute/brute.c | 716 +++++++++++++++++++++++ security/security.c | 5 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/brute/.gitignore | 2 + tools/testing/selftests/brute/Makefile | 5 + tools/testing/selftests/brute/config | 1 + tools/testing/selftests/brute/rmxattr.c | 34 ++ tools/testing/selftests/brute/test.c | 507 ++++++++++++++++ tools/testing/selftests/brute/test.sh | 256 ++++++++ 21 files changed, 1907 insertions(+), 5 deletions(-)create mode 100644
Documentation/admin-guide/LSM/Brute.rstcreate mode 100644security/brute/Kconfig
create mode 100644security/brute/Makefile
create mode 100644security/brute/brute.c
create mode 100644tools/testing/selftests/brute/.gitignorecreate mode 100644tools/testing/selftests/brute/Makefilecreate mode 100644tools/testing/selftests/brute/configcreate mode 100644tools/testing/selftests/brute/rmxattr.ccreate mode 100644tools/testing/selftests/brute/test.ccreate mode 100755tools/testing/selftests/brute/test.sh--
2.25.1
Hi,
Quoted 9 lines · Andi Kleen
Quoted 8 lines
Moreover, I think this solves another problem pointed out by Andi Kleen during the v5 review [2] related to the possibility that a supervisor respawns processes killed by the Brute LSM. He suggested adding some way so a supervisor can know that a process has been killed by Brute and then decide to respawn or not. So, now, the supervisor can read the brute xattr of one executable and know if it is blocked by Brute and why (using the statistical data).
Quoted 9 lines
It looks better now, Thank.
One potential problem is that the supervisor might see the executable directly, but run it through some wrapper. In fact I suspect that will be fairly common with complex daemons. So it couldn't directly look at the xattr. Might be useful to also pass this information through the wait* chain, so that the supervisor can directly collect it. That would need some extension to these system calls.
Could something like this help? (not tested)
arch/x86/kernel/signal_compat.c +1 -1
@@ -30,7 +30,7 @@ static inline void signal_compat_build_tests(void)BUILD_BUG_ON (NSIGSEGV !=9 );BUILD_BUG_ON (NSIGBUS !=5 );BUILD_BUG_ON (NSIGTRAP !=6 ); -BUILD_BUG_ON (NSIGCHLD !=6 ); +BUILD_BUG_ON (NSIGCHLD !=7 );BUILD_BUG_ON (NSIGSYS !=2 );/* This is part of the ABI and can never change in size: */
include/brute/brute.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _BRUTE_H_ +#define _BRUTE_H_ + +#include <linux/sched.h> + +#ifdef CONFIG_SECURITY_FORK_BRUTE +bool brute_task_killed (struct task_struct *task); +#else +static inline bool brute_task_killed (struct task_struct *task) {return false; } +#endif + +#endif /* _BRUTE_H_ */
include/uapi/asm-generic/siginfo.h +2 -1
@@ -273,7 +273,8 @@ typedef struct siginfo {#define CLD_TRAPPED 4/* traced child has trapped */ #define CLD_STOPPED 5/* child has stopped */ #define CLD_CONTINUED 6/* stopped child has continued */ -#define NSIGCHLD 6 +#define CLD_BRUTE 7/* child was killed by brute LSM */ +#define NSIGCHLD 7/* * SIGPOLL (or any other signal without signal specific si_codes) si_codes
kernel/exit.c +5 -1
@@ -69,6 +69,8 @@#include <asm/unistd.h> #include <asm/mmu_context.h> +#include <brute/brute.h> +static void __unhash_process (struct task_struct *p,bool group_dead) { nr_threads--;
@@ -1001,6 +1003,7 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)pid_t pid =task_pid_vnr (p);uid_t uid =from_kuid_munged (current_user_ns (),task_uid (p));struct waitid_info *infop; +bool killed_by_brute =brute_task_killed (p);if (!likely (wo->wo_flags &WEXITED ))return 0 ;
@@ -1114,7 +1117,8 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p) infop->cause =CLD_EXITED ; infop->status = status >>8 ; }else { - infop->cause = (status &0x80 ) ?CLD_DUMPED :CLD_KILLED ; + infop->cause = (status &0x80 ) ?CLD_DUMPED : + killed_by_brute ?CLD_BRUTE :CLD_KILLED ; infop->status = status &0x7f ; } infop->pid = pid;
kernel/signal.c +3 -1
@@ -55,6 +55,8 @@#include <asm/siginfo.h> #include <asm/cacheflush.h> +#include <brute/brute.h> +/* * SLAB caches for signal bits. */
@@ -1996,7 +1998,7 @@ bool do_notify_parent(struct task_struct *tsk, int sig)if (tsk->exit_code &0x80 ) info.si_code =CLD_DUMPED ;else if (tsk->exit_code &0x7f ) - info.si_code =CLD_KILLED ; + info.si_code =brute_task_killed (tsk) ?CLD_BRUTE :CLD_KILLED ;else { info.si_code =CLD_EXITED ; info.si_status = tsk->exit_code >>8 ;
Thanks,
John Wood
Quoted 12 lines · John Wood
Hi,
Quoted 9 lines · Andi Kleen
Quoted 7 lines
Moreover, I think this solves another problem pointed out by Andi Kleen during the v5 review [2] related to the possibility that a supervisor respawns processes killed by the Brute LSM. He suggested adding some way so a supervisor can know that a process has been killed by Brute and then decide to respawn or not. So, now, the supervisor can read the brute xattr of one executable and know if it is blocked by Brute and why (using the statistical data).
It looks better now, Thank.
Quoted 8 lines
Quoted 7 lines
One potential problem is that the supervisor might see the executable directly, but run it through some wrapper. In fact I suspect that will be fairly common with complex daemons. So it couldn't directly look at the xattr. Might be useful to also pass this information through the wait* chain, so that the supervisor can directly collect it. That would need some extension to these system calls.
Could something like this help? (not tested)
This works even when someone further down the chain died? Assuming it does, for SIGCHLD it seems reasonable.
I'm not fully sure how it will interact with cgroup release tracking though, that might need more research (my understanding is that modern supervisors often use cgroups)
-Andi
Quoted 21 lines · Andi Kleen
Quoted 19 lines · John Wood
Hi,
Quoted 16 lines · Andi Kleen
Quoted 7 lines
Moreover, I think this solves another problem pointed out by Andi Kleen during the v5 review [2] related to the possibility that a supervisor respawns processes killed by the Brute LSM. He suggested adding some way so a supervisor can know that a process has been killed by Brute and then decide to respawn or not. So, now, the supervisor can read the brute xattr of one executable and know if it is blocked by Brute and why (using the statistical data).
It looks better now, Thank.
One potential problem is that the supervisor might see the executable directly, but run it through some wrapper. In fact I suspect that will be fairly common with complex daemons. So it couldn't directly look at the xattr. Might be useful to also pass this information through the wait* chain, so that the supervisor can directly collect it. That would need some extension to these system calls.
Quoted 3 lines
Quoted 1 line
Could something like this help? (not tested)
This works even when someone further down the chain died?
Yes, this is the idea. (but now is a work in progress :) )
Quoted 1 line
Assuming it does, for SIGCHLD it seems reasonable.
So, if there are no objections I will work on it for the next version.
Quoted 4 lines
I'm not fully sure how it will interact with cgroup release tracking though, that might need more research (my understanding is that modern supervisors often use cgroups)
Yeah, a new topic to learn: cgroups. I will try to work on this too if there are no objections.
Thanks for the feedback.
John Wood
PATCH v7 4/7 security/brute: Mitigate a brute force attack
In reply to John WoodWhen a brute force attack is detected all the offending tasks involved in the attack must be killed. In other words, it is necessary to kill all the tasks that are executing the same file that is running during the brute force attack.
Also, to prevent the executable involved in the attack from being respawned by a supervisor, and thus prevent a brute force attack from being started again, test the "not_allowed" flag and avoid the file execution based on this.
--- security/brute/brute.c | 113 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 11 deletions(-)
security/brute/brute.c +102 -11
@@ -233,6 +233,88 @@ static inline void brute_print_attack_running(void) current->comm); } +/** +* brute_print_file_not_allowed() - Warn about a file not allowed. +* @dentry: The dentry of the file not allowed. +*/ +static void brute_print_file_not_allowed (struct dentry *dentry) +{ +char *buf, *path; + + buf =__getname (); +if (WARN_ON_ONCE (!buf)) +return ; + + path =dentry_path_raw (dentry, buf,PATH_MAX ); +if (WARN_ON_ONCE (IS_ERR (path))) + goto free; + +pr_warn_ratelimited ("%s not allowed\n" , path); +free: +__putname (buf); +} + +/** +* brute_is_same_file() - Test if two files are the same. +* @file1: First file to compare. Cannot be NULL. +* @file2: Second file to compare. Cannot be NULL. +* +* Two files are the same if they have the same inode number and the same block +* device. +* +* Return: True if the two files are the same. False otherwise. +*/ +static inline bool brute_is_same_file (const struct file *file1, +const struct file *file2) +{ +struct inode *inode1 =file_inode (file1); +struct inode *inode2 =file_inode (file2); + +return inode1->i_ino == inode2->i_ino && + inode1->i_sb->s_dev == inode2->i_sb->s_dev; +} + +/** +* brute_kill_offending_tasks() - Kill the offending tasks. +* @file: The file executed during a brute force attack. Cannot be NULL. +* +* When a brute force attack is detected all the offending tasks involved in the +* attack must be killed. In other words, it is necessary to kill all the tasks +* that are executing the same file that is running during the brute force +* attack. Moreover, the processes that have the same group_leader that the +* current task must be avoided since they are in the path to be killed. +* +* The for_each_process loop is protected by the tasklist_lock acquired in read +* mode instead of rcu_read_lock to avoid that the newly created processes +* escape this RCU read lock. +*/ +static void brute_kill_offending_tasks (const struct file *file) +{ +struct task_struct *task; +struct file *exe_file; +bool is_same_file; + +read_lock (&tasklist_lock); +for_each_process (task) { +if (task->group_leader == current->group_leader) +continue ; + + exe_file =get_task_exe_file (task); +if (!exe_file) +continue ; + + is_same_file =brute_is_same_file (exe_file, file); +fput (exe_file); +if (!is_same_file) +continue ; + +do_send_sig_info (SIGKILL ,SEND_SIG_PRIV , task,PIDTYPE_PID ); +pr_warn_ratelimited ("Offending process %d [%s] killed\n" , + task->pid, task->comm); + } +read_unlock (&tasklist_lock); +} +/** * brute_get_xattr_stats() - Get the stats from an extended attribute. * @dentry: The dentry of the file to get the extended attribute.
@@ -295,6 +377,10 @@ static int brute_set_xattr_stats(struct dentry *dentry, struct inode *inode,* created. This way, the scenario where an application has not crossed any * privilege boundary is avoided since the existence of the extended attribute * denotes the crossing of bounds. +* +* Also, do not update the statistics if the execution of the file is not +* allowed and kill all the offending tasks when a brute force attack is +* detected. */ static void brute_update_xattr_stats (const struct file *file) {
@@ -306,7 +392,7 @@ static void brute_update_xattr_stats(const struct file *file)inode_lock (inode); rc =brute_get_xattr_stats (dentry, inode, &stats);WARN_ON_ONCE (rc && rc != -ENODATA ); -if (rc) { +if (rc || (!rc && stats.not_allowed)) {inode_unlock (inode);return ; }
@@ -320,6 +406,9 @@ static void brute_update_xattr_stats(const struct file *file) rc =brute_set_xattr_stats (dentry, inode, &stats);WARN_ON_ONCE (rc);inode_unlock (inode); + +if (stats.not_allowed) +brute_kill_offending_tasks (file); }/**
@@ -433,21 +522,17 @@ static void brute_task_fatal_signal(const kernel_siginfo_t *siginfo)* @bprm: Contains the linux_binprm structure. * @file: Binary that will be executed without an interpreter. * -* This hook is useful to mark that a privilege boundary (setuid/setgid process) -* has been crossed. This is done based on the "secureexec" flag. +* If there are statistics, test the "not_allowed" flag and avoid the file +* execution based on this. Also, this hook is useful to mark that a privilege +* boundary (setuid/setgid process) has been crossed. This is done based on the +* "secureexec" flag. * * To be defensive return an error code if it is not possible to get or set the * stats using an extended attribute since this blocks the execution of the * file. This scenario is treated as an attack. * -* It is important to note that here the brute_new_xattr_stats function could be -* used with a previous test of the secureexec flag. However it is better to use -* the basic xattr functions since in a future commit a test if the execution is -* allowed (via the brute_stats::not_allowed flag) will be necessary. This way, -* the stats of the file will be get only once. -* -* Return: An error code if it is not possible to get or set the statistical -* data. Zero otherwise. +* Return: -EPERM if the execution of the file is not allowed. An error code if +* it is not possible to get or set the statistical data. Zero otherwise. */ static int brute_task_execve (struct linux_binprm *bprm,struct file *file) {
@@ -461,6 +546,12 @@ static int brute_task_execve(struct linux_binprm *bprm, struct file *file)if (WARN_ON_ONCE (rc && rc != -ENODATA )) goto unlock; +if (!rc && stats.not_allowed) { +brute_print_file_not_allowed (dentry); + rc = -EPERM ; + goto unlock; + } +if (rc == -ENODATA && bprm->secureexec) {brute_reset_stats (&stats); rc =brute_set_xattr_stats (dentry, inode, &stats);
--
2.25.1
PATCH v7 5/7 selftests/brute: Add tests for the Brute LSM
In reply to John WoodAdd tests to check the brute LSM functionality and cover fork/exec brute force attacks crossing the following privilege boundaries:
1.- setuid process
2.- privilege changes
3.- network to local
Also, as a first step check that fork/exec brute force attacks without crossing any privilege boundary already commented doesn't trigger the detection and mitigation stage.
Once a brute force attack is detected, the "test" executable is marked as "not allowed". To start again a new test, use the "rmxattr" app to revert this state. This way, all the tests can be run using the same binary.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/brute/.gitignore | 2 + tools/testing/selftests/brute/Makefile | 5 + tools/testing/selftests/brute/config | 1 + tools/testing/selftests/brute/rmxattr.c | 34 ++ tools/testing/selftests/brute/test.c | 507 +++++++++++++++++++++++ tools/testing/selftests/brute/test.sh | 256 ++++++++++++ 7 files changed, 806 insertions(+) create mode 100644 tools/testing/selftests/brute/.gitignore create mode 100644 tools/testing/selftests/brute/Makefile create mode 100644 tools/testing/selftests/brute/config create mode 100644 tools/testing/selftests/brute/rmxattr.c create mode 100644 tools/testing/selftests/brute/test.c create mode 100755 tools/testing/selftests/brute/test.sh
tools/testing/selftests/Makefile +1 -0
@@ -2,6 +2,7 @@ TARGETS = arm64 TARGETS += bpf TARGETS += breakpoints +TARGETS += brute TARGETS += capabilities TARGETS += cgroup TARGETS += clone3
tools/testing/selftests/brute/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +rmxattr +test
tools/testing/selftests/brute/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wall -O2 +TEST_PROGS := test.sh +TEST_GEN_FILES := rmxattr test +include ../lib.mk
tools/testing/selftests/brute/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_SECURITY_FORK_BRUTE=y
tools/testing/selftests/brute/rmxattr.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/xattr.h> + +static __attribute__((noreturn))void error_failure (const char *message) +{ +perror (message); +exit (EXIT_FAILURE ); +} + +#define PROG_NAME basename(argv[0]) + +#define XATTR_SECURITY_PREFIX "security." +#define XATTR_BRUTE_SUFFIX "brute" +#define XATTR_NAME_BRUTE XATTR_SECURITY_PREFIX XATTR_BRUTE_SUFFIX + +int main (int argc,char **argv) +{ +int rc; + +if (argc <2 ) { +printf ("Usage: %s <FILE>\n" ,PROG_NAME ); +exit (EXIT_FAILURE ); + } + + rc =removexattr (argv[1 ],XATTR_NAME_BRUTE ); +if (rc) +error_failure ("removexattr" ); + +return EXIT_SUCCESS ; +}
tools/testing/selftests/brute/test.c new file +507 -0
507 lines not on this page · raw message
tools/testing/selftests/brute/test.sh new file +256 -0
256 lines not on this page · raw message
--
2.25.1
PATCH v7 6/7 Documentation: Add documentation for the Brute LSM
In reply to John WoodAdd some info detailing what is the Brute LSM, its motivation, weak points of existing implementations, proposed solutions, enabling, disabling, configuration and self-tests.
--- Documentation/admin-guide/LSM/Brute.rst | 334 ++++++++++++++++++++++++ Documentation/admin-guide/LSM/index.rst | 1 + security/brute/Kconfig | 3 +- 3 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 Documentation/admin-guide/LSM/Brute.rst
Documentation/admin-guide/LSM/Brute.rst new file +334 -0
@@ -0,0 +1,334 @@ +.. SPDX-License-Identifier: GPL-2.0 + +===== +Brute +===== + +Brute is a Linux Security Module that detects and mitigates fork brute force +attacks against vulnerable userspace processes. + + +Motivation +========== + +Attacks against vulnerable userspace applications with the purpose to break ASLR +or bypass canaries traditionally use some level of brute force with the help of +the fork system call. This is possible since when creating a new process using +fork its memory contents are the same as those of the parent process (the +process that called the fork system call). So, the attacker can test the memory +infinite times to find the correct memory values or the correct memory addresses +without worrying about crashing the application. + +Based on the above scenario it would be nice to have this detected and +mitigated, and this is the goal of this implementation. Specifically the +following attacks are expected to be detected: + + 1. Launching (fork()/exec()) a setuid/setgid process repeatedly until a + desirable memory layout is got (e.g. Stack Clash). + 2. Connecting to an exec()ing network daemon (e.g. xinetd) repeatedly until a + desirable memory layout is got (e.g. what CTFs do for simple network + service). + 3. Launching processes without exec() (e.g. Android Zygote) and exposing state + to attack a sibling. + 4. Connecting to a fork()ing network daemon (e.g. apache) repeatedly until the + previously shared memory layout of all the other children is exposed (e.g. + kind of related to HeartBleed). + +In each case, a privilege boundary has been crossed: + + | Case 1: setuid/setgid process + | Case 2: network to local + | Case 3: privilege changes + | Case 4: network to local + +So, what really needs to be detected are fork/exec brute force attacks that +cross any of the commented bounds. + + +Other implementations +===================== + +The public version of grsecurity, as a summary, is based on the idea of delaying +the fork system call if a child died due to some fatal signal (``SIGSEGV``, +``SIGBUS``, ``SIGKILL`` or ``SIGILL``). This has some issues: + +Bad practices +------------- + +Adding delays to the kernel is, in general, a bad idea. + +Scenarios not detected (false negatives) +---------------------------------------- + +This protection acts only when the fork system call is called after a child has +crashed. So, it would still be possible for an attacker to fork a big amount of +children (in the order of thousands), then probe all of them, and finally wait +the protection time before repeating the steps. + +Moreover, this method is based on the idea that the protection doesn't act if +the parent crashes. So, it would still be possible for an attacker to fork a +process and probe itself. Then, fork the child process and probe itself again. +This way, these steps can be repeated infinite times without any mitigation. + +Scenarios detected (false positives) +------------------------------------ + +Scenarios where an application rarely fails for reasons unrelated to a real +attack. + + +This implementation +=================== + +The main idea behind this implementation is to improve the existing ones +focusing on the weak points annotated before. Basically, the adopted solution is +to detect a fast crash rate instead of only one simple crash and to detect both +the crash of parent and child processes. Also, fine tune the detection focusing +on privilege boundary crossing. And finally, as a mitigation method, kill all +the offending tasks involved in the attack and mark the executable as "not +allowed" (to block the following executions) instead of using delays. + +To achieve this goal, and going into more details, this implementation is based +on the use of some statistical data shared across all the processes that can +have the same memory contents. Or in other words, a statistical data shared +between all the fork hierarchy processes after an execve system call. + +The purpose of these statistics is, basically, collect all the necessary info to +compute the application crash period in order to detect an attack. To track all +this information, the extended attributes (xattr) of the executable files are +used. More specifically, the name of the attribute is "brute" and uses the +"security" namespace. So, the full xattr name for the Brute LSM is: + + ``security.brute`` + +The same can be achieved using a pointer to the fork hierarchy statistical data +held by the ``task_struct`` structure, but this has an important drawback: a +brute force attack that happens through the execve system call losts the faults +info since these statistics are freed when the fork hierarchy disappears. Using +the last method (pointer in the ``task_struct`` structure) makes not possible to +manage this attack type that can be successfully treated using extended +attributes. + +To detect a brute force attack it is necessary that the statistics shared by all +the fork hierarchy processes be updated in every fatal crash and the most +important data to update is the application crash period. + +The crash period is the time between two consecutive faults, but this also has a +drawback: if an application crashes twice in a short period of time for some +reason unrelated to a real attack, a false positive will be triggered. To avoid +this scenario the exponential moving average (EMA) is used. This way, the +application crash period will be a value that is not prone to change due to +spurious data and follows the real crash period. + +These statistics are stored in the executables using the extended attributes +feature. So, the detection and mitigation of brute force attacks using this LSM +it is only feasible in filesystems that support xattr. + +.. kernel-doc:: security/brute/brute.c + :identifiers: brute_raw_stats + +This is a fixed sized struct with a very small footprint. So, in reference to +memory usage, it is not expected to have problems storing it as an extended +attribute. + +Concerning to access rights to this statistical data, as stated above, the +"security" namespace is used. Since no custom policy, related to this extended +attribute, has been implemented for the Brute LSM, all processes have read +access to these statistics, and write access is limited to processes that have +the ``CAP_SYS_ADMIN`` capability. + +Attack detection +---------------- + +There are two types of brute force attacks that need to be detected. The first +one is an attack that happens through the fork system call and the second one is +an attack that happens through the execve system call. Moreover, these two +attack types have two variants. A slow brute force attack that is detected if a +maximum number of faults per fork hierarchy is reached and a fast brute force +attack that is detected if the application crash period falls below a certain +threshold. + +Attack mitigation +----------------- + +Once an attack has been detected, this is mitigated killing all the offending +tasks involved. Or in other words, once an attack has been detected, this is +mitigated killing all the processes that are executing the same file that is +running during the brute force attack. Also, to prevent the executable involved +in the attack from being respawned by a supervisor, and thus prevent a brute +force attack from being started again, the file is marked as "not allowed" and +the following executions are avoided based on this mark. This method allows +supervisors to implement their own policy: they can read the statistics, know if +the executable is blocked by the Brute LSM and why, and act based on this +information. If they want to respawn the offending executable it is only +necessary to remove the "``security.brute``" extended attribute and thus remove +the statistical data. + +Fine tuning the attack detection +-------------------------------- + +To avoid false positives during the attack detection it is necessary to narrow +the possible cases. To do so, and based on the threat scenarios that we want to +detect, this implementation also focuses on the crossing of privilege bounds. + +To be precise, only the following privilege bounds are taken into account: + + 1. setuid/setgid process + 2. network to local + 3. privilege changes + +Moreover, only the fatal signals delivered by the kernel are taken into account +avoiding the fatal signals sent by userspace applications (with the exception of +the ``SIGABRT`` user signal since this is used by glibc for stack canary, +malloc, etc. failures, which may indicate that a mitigation has been triggered). + +Exponential moving average (EMA) +-------------------------------- + +This kind of average defines a weight (between 0 and 1) for the new value to add +and applies the remainder of the weight to the current average value. This way, +some spurious data will not excessively modify the average and only if the new +values are persistent, the moving average will tend towards them. + +Mathematically the application crash period's EMA can be expressed as follows: + + period_ema = period * weight + period_ema * (1 - weight) + +Related to the attack detection, the EMA must guarantee that not many crashes +are needed. To demonstrate this, the scenario where an application has failed +and then has been running without any crashes for a month, will be used. + +The period's EMA can be written now as: + + period_ema[i] = period[i] * weight + period_ema[i - 1] * (1 - weight) + +If the new crash periods have insignificant values related to the first crash +period (a month in this case), the formula can be rewritten as: + + period_ema[i] = period_ema[i - 1] * (1 - weight) + +And by extension: + + | period_ema[i - 1] = period_ema[i - 2] * (1 - weight) + | period_ema[i - 2] = period_ema[i - 3] * (1 - weight) + | period_ema[i - 3] = period_ema[i - 4] * (1 - weight) + +So, if the substitution is made: + + | period_ema[i] = period_ema[i - 1] * (1 - weight) + | period_ema[i] = period_ema[i - 2] * (1 - weight)\ :sup:`2` + | period_ema[i] = period_ema[i - 3] * (1 - weight)\ :sup:`3` + | period_ema[i] = period_ema[i - 4] * (1 - weight)\ :sup:`4` + +And in a more generic form: + + period_ema[i] = period_ema[i - n] * (1 - weight)\ :sup:`n` + +Where "n" represents the number of iterations to obtain an EMA value. Or in +other words, the number of crashes to detect an attack. + +So, if we isolate the number of crashes: + + | period_ema[i] / period_ema[i - n] = (1 - weight)\ :sup:`n` + | log(period_ema[i] / period_ema[i - n]) = log((1 - weight)\ :sup:`n`) + | log(period_ema[i] / period_ema[i - n]) = n * log(1 - weight) + | n = log(period_ema[i] / period_ema[i - n]) / log(1 - weight) + +Then, in the commented scenario (an application has failed and then has been +running without any crashes for a month), the approximate number of crashes to +detect an attack (using the default implementation values for the weight and the +crash period threshold) is: + + | weight = 7 / 10 + | crash_period_threshold = 30 seconds + + | n = log(crash_period_threshold / seconds_per_month) / log(1 - weight) + | n = log(30 / (30 * 24 * 3600)) / log(1 - 0.7) + | n = 9.44 + +So, with 10 crashes for this scenario an attack will be detected. If these steps +are repeated for different scenarios and the results are collected: + + ======================== ===================================== + time without any crashes number of crashes to detect an attack + ======================== ===================================== + 1 month 9.44 + 1 year 11.50 + 10 years 13.42 + ======================== ===================================== + +However, this computation has a drawback. The first data added to the EMA not +obtains a real average showing a trend. So the solution is simple, the EMA needs +a minimum number of data to be able to be interpreted. This way, the case where +a few first faults are fast enough followed by no crashes is avoided. + +Per system enabling/disabling +----------------------------- + +This feature can be enabled at build time using the +``CONFIG_SECURITY_FORK_BRUTE`` option or using the visual config application +under the following menu: + + Security options ``--->`` Fork brute force attack detection and mitigation + +Also, at boot time, this feature can be disable too, by changing the "``lsm=``" +boot parameter. + +Per system configuration +------------------------ + +To customize the detection's sensibility there are five new sysctl attributes +for the Brute LSM that are accessible through the following path: + + ``/proc/sys/kernel/brute/`` + +More specifically, the files and their description are: + +**ema_weight_numerator** + + .. kernel-doc:: security/brute/brute.c + :doc: brute_ema_weight_numerator + +**ema_weight_denominator** + + .. kernel-doc:: security/brute/brute.c + :doc: brute_ema_weight_denominator + +**max_faults** + + .. kernel-doc:: security/brute/brute.c + :doc: brute_max_faults + +**min_faults** + + .. kernel-doc:: security/brute/brute.c + :doc: brute_min_faults + +**crash_period_threshold** + + .. kernel-doc:: security/brute/brute.c + :doc: brute_crash_period_threshold + +Kernel selftests +---------------- + +To validate all the expectations about this implementation, there is a set of +selftests. This tests cover fork/exec brute force attacks crossing the following +privilege boundaries: + + 1. setuid process + 2. privilege changes + 3. network to local + +Also, there are some tests to check that fork/exec brute force attacks without +crossing any privilege boundariy already commented doesn't trigger the detection +and mitigation stage. + +To build the tests: + ``make -C tools/testing/selftests/ TARGETS=brute`` + +To run the tests: + ``make -C tools/testing/selftests TARGETS=brute run_tests`` + +To package the tests: + ``make -C tools/testing/selftests TARGETS=brute gen_tar``
Documentation/admin-guide/LSM/index.rst +1 -0
@@ -41,6 +41,7 @@ subdirectories. :maxdepth: 1 apparmor + Brute LoadPin SELinux Smack
security/brute/Kconfig +2 -1
@@ -9,6 +9,7 @@ config SECURITY_FORK_BRUTE offending tasks are killed. Also, the executable file involved in the attack will be marked as "not allowed" and new execve system calls using this file will fail. Like capabilities, this security module - stacks with other LSMs. + stacks with other LSMs. Further information can be found in + Documentation/admin-guide/LSM/Brute.rst. If you are unsure how to answer this question, answer N.
--
2.25.1
PATCH v7 7/7 MAINTAINERS: Add a new entry for the Brute LSM
In reply to John WoodIn order to maintain the code for the Brute LSM add a new entry to the maintainers list.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -3847,6 +3847,13 @@ L: [email protected] S: Supported F: drivers/net/ethernet/brocade/bna/ +BRUTE SECURITY MODULE +M: John Wood <[email protected]> +S: Maintained +F: Documentation/admin-guide/LSM/Brute.rst +F: security/brute/ +F: tools/testing/selftests/brute/ + BSG (block layer generic sg v4 driver) M: FUJITA Tomonori <[email protected]> L: [email protected]
--
2.25.1