Reduce overhead of LSMs with static calls
Subsystems (2)
v15 cover Reduce overhead of LSMs with static calls
# Background
LSM hooks (callbacks) are currently invoked as indirect function calls. These callbacks are registered into a linked list at boot time as the order of the LSMs can be configured on the kernel command line with the "lsm=" command line parameter.
Indirect function calls have a high overhead due to retpoline mitigation for various speculative execution attacks.
Retpolines remain relevant even with newer generation CPUs as recently discovered speculative attacks, like Spectre BHB need Retpolines to mitigate against branch history injection and still need to be used in combination with newer mitigation features like eIBRS.
This overhead is especially significant for the "bpf" LSM which allows the user to implement LSM functionality with eBPF program. In order to facilitate this the "bpf" LSM provides a default callback for all LSM hooks. When enabled, the "bpf" LSM incurs an unnecessary / avoidable indirect call. This is especially bad in OS hot paths (e.g. in the networking stack). This overhead prevents the adoption of bpf LSM on performance critical systems, and also, in general, slows down all LSMs.
Since we know the address of the enabled LSM callbacks at compile time and only the order is determined at boot time, the LSM framework can allocate static calls for each of the possible LSM callbacks and these calls can be updated once the order is determined at boot.
This series is a respin of the RFC proposed by Paul Renauld ([email protected]) and Brendan Jackman ([email protected]) [1]
# Performance improvement
With this patch-set some syscalls with lots of LSM hooks in their path benefitted at an average of ~3% and I/O and Pipe based system calls benefitting the most.
Here are the results of the relevant Unixbench system benchmarks with BPF LSM and SELinux enabled with default policies enabled with and without these patches.
Benchmark Delta(%): (+ is better)
===============================================================================
Execl Throughput +1.9356 File Write 1024 bufsize 2000 maxblocks +6.5953 Pipe Throughput +9.5499 Pipe-based Context Switching +3.0209 Process Creation +2.3246 Shell Scripts (1 concurrent) +1.4975 System Call Overhead +2.7815 System Benchmarks Index Score (Partial Only): +3.4859
In the best case, some syscalls like eventfd_create benefitted to about ~10%. The full analysis can be viewed at https://kpsingh.ch/lsm-perf
More context in the excellent LWN article by Jonathan Corbet [2]
[1] https://lore.kernel.org/linux-security-module/[email protected]/
[2] https://lwn.net/Articles/979683/
# v14 to v15
* Fixed early LSM init wuth Patch 1
* Made the static call table aligned to u64 and added a comment as to why this is needed.
# v13 to v14
* Dropped Patch 5 based on the ongoing discussion in
https://lore.kernel.org/linux-security-module/[email protected]/, BPF LSM will still have default callbacks enabled.
* Dropped Patch 4 as recommended by Paul, indirect calls will remain in some LSM hooks for now.
https://lore.kernel.org/linux-security-module/[email protected]/
* Fixed minor nits in Patch 3
# v12 to v13
* Fixed the GCC note when struct randomization is enabled
* Added missing link to BPF LSM side effects discussion in the commit message
# v11 to v12
* Casey's feedback on readability of patch 4
* Tetsuo's catch on the bug in Patch 5
* Changed LSM_HOOK_TOGGLEABLE to LSM_HOOK_RUNTIME, the bikeshed is blue now.
* Also, as security_toggle_hook relies on iterating over a
flat lsm_static_calls_table, I added the __packed attribute.
# v10 to v11
* bpf_lsm_toggle_hook to security_toggle_hook with LSM_HOOK_TOGGLEABLE limiting the hooks as the ones that can be toggled.
# v9 to v10
* Addressed Paul's comments for Patch 3. I did not remove the acks from this one as changes were minor.
* Moved BPF LSM specific hook toggling logic bpf_lsm_toggle_hook to s security_toggle_hook as a generic API. I removed the Ack's from this patch as it's worth another look.
* Refactored the non-standard hooks to use static calls.
# v8 to v9
Paul, I removed the 5th patch about CONFIG_SECURITY_HOOK_LIKELY and went through all the feedback. I believe it all should be addressed now.
But, please let me know if I missed anything.
The patches are based on https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm.git
(next branch as of 2024-02-07) and resolved a bunch of conflicts.
I also added Andrii's series ack to indidividual patches.
# v7 to v8
* Addressed Andrii's feedback
* Rebased (this seems to have removed the syscall changes). v7 has the required conflict resolution incase the conflicts need to be resolved again.
# v6 -> v7
* Rebased with latest LSM id changes merged
NOTE: The warning shown by the kernel test bot is spurious, there is no flex array and it seems to come from an older tool chain.
https://lore.kernel.org/bpf/[email protected]/
# v5 -> v6
* Fix a bug in BPF LSM hook toggle logic.
# v4 -> v5
* Rebase to linux-next/master
* Fixed the case where MAX_LSM_COUNT comes to zero when just CONFIG_SECURITY is compiled in without any other LSM enabled as reported here:
https://lore.kernel.org/bpf/[email protected]
# v3 -> v4
* Refactor LSM count macros to use COUNT_ARGS
* Change CONFIG_SECURITY_HOOK_LIKELY likely's default value to be based on the LSM enabled and have it depend on CONFIG_EXPERT. There are a lot of subtle options behind CONFIG_EXPERT and this should, hopefully alleviate concerns about yet another knob.
* __randomize_layout for struct lsm_static_call and, in addition to the cover letter add performance numbers to 3rd patch and some minor commit message updates.
* Rebase to linux-next.
# v2 -> v3
* Fixed a build issue on archs which don't have static calls and enable CONFIG_SECURITY.
* Updated the LSM_COUNT macros based on Andrii's suggestions.
* Changed the security_ prefix to lsm_prefix based on Casey's suggestion.
* Inlined static_branch_maybe into lsm_for_each_hook on Kees' feedback.
# v1 -> v2 (based on linux-next, next-20230614)
* Incorporated suggestions from Kees
* Changed the way MAX_LSMs are counted from a binary based generator to a clever header.
* Add CONFIG_SECURITY_HOOK_LIKELY to configure the likelihood of LSM hooks.
KP Singh (4):
init/main.c: Initialize early LSMs after arch code, static keys and
calls.
kernel: Add helper macros for loop unrolling
lsm: count the LSMs enabled at compile time
lsm: replace indirect LSM hook calls with static calls
include/linux/args.h | 6 +- include/linux/lsm_count.h | 128 ++++++++++++++++++++++ include/linux/lsm_hooks.h | 52 +++++++-- include/linux/unroll.h | 36 +++++++ init/main.c | 6 +- security/security.c | 219 +++++++++++++++++++++++++++----------- 6 files changed, 369 insertions(+), 78 deletions(-) create mode 100644 include/linux/lsm_count.h create mode 100644 include/linux/unroll.h
Signature · 1 line
2.46.0.184.g6999bdac58-goog
PATCH v15 1/4 init/main.c: Initialize early LSMs after arch code, static keys and calls.
With LSMs using static calls and static keys, early_lsm_init needs to wait for setup_arch for architecture specific functionality which includes jump tables and static calls to be initialized.
Since not all architectures call jump_table_init in setup_arch, explicitly call both jump_table_init and static_call_init before early_security_init.
This only affects "early LSMs" i.e. only lockdown when CONFIG_SECURITY_LOCKDOWN_LSM_EARLY is set.
--- init/main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
init/main.c +4 -2
@@ -922,8 +922,11 @@ void start_kernel(void)boot_cpu_init ();page_address_init ();pr_notice ("%s" , linux_banner); -early_security_init ();setup_arch (&command_line); +/* Static keys and static calls are needed by LSMs */ +jump_label_init (); +static_call_init (); +early_security_init ();setup_boot_config ();setup_command_line (command_line);setup_nr_cpu_ids ();
@@ -934,7 +937,6 @@ void start_kernel(void)pr_notice ("Kernel command line: %s\n" , saved_command_line);/* parameters may set static keys */ -jump_label_init ();parse_early_param (); after_dashes = parse_args("Booting kernel", static_command_line, __start___param,
Signature · 1 line
2.46.0.184.g6999bdac58-goog
PATCH v15 2/4 kernel: Add helper macros for loop unrolling
In reply to KP SinghThis helps in easily initializing blocks of code (e.g. static calls and keys).
UNROLL(N, MACRO, __VA_ARGS__) calls MACRO N times with the first argument as the index of the iteration. This allows string pasting to create unique tokens for variable names, function calls etc.
As an example:
#include <linux/unroll.h>
#define MACRO(N, a, b) \
int add_##N(int a, int b) \
{ \
return a + b + N; \
}
UNROLL(2, MACRO, x, y)expands to:
int add_0(int x, int y)
{
return x + y + 0;
}
int add_1(int x, int y)
{
return x + y + 1;
}--- include/linux/unroll.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 include/linux/unroll.h
include/linux/unroll.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2023 Google LLC. +*/ + +#ifndef __UNROLL_H +#define __UNROLL_H + +#include <linux/args.h> + +#define UNROLL(N, MACRO, args...) CONCATENATE(__UNROLL_, N)(MACRO, args) + +#define __UNROLL_0(MACRO, args...) +#define __UNROLL_1(MACRO, args...) __UNROLL_0(MACRO, args) MACRO(0, args) +#define __UNROLL_2(MACRO, args...) __UNROLL_1(MACRO, args) MACRO(1, args) +#define __UNROLL_3(MACRO, args...) __UNROLL_2(MACRO, args) MACRO(2, args) +#define __UNROLL_4(MACRO, args...) __UNROLL_3(MACRO, args) MACRO(3, args) +#define __UNROLL_5(MACRO, args...) __UNROLL_4(MACRO, args) MACRO(4, args) +#define __UNROLL_6(MACRO, args...) __UNROLL_5(MACRO, args) MACRO(5, args) +#define __UNROLL_7(MACRO, args...) __UNROLL_6(MACRO, args) MACRO(6, args) +#define __UNROLL_8(MACRO, args...) __UNROLL_7(MACRO, args) MACRO(7, args) +#define __UNROLL_9(MACRO, args...) __UNROLL_8(MACRO, args) MACRO(8, args) +#define __UNROLL_10(MACRO, args...) __UNROLL_9(MACRO, args) MACRO(9, args) +#define __UNROLL_11(MACRO, args...) __UNROLL_10(MACRO, args) MACRO(10, args) +#define __UNROLL_12(MACRO, args...) __UNROLL_11(MACRO, args) MACRO(11, args) +#define __UNROLL_13(MACRO, args...) __UNROLL_12(MACRO, args) MACRO(12, args) +#define __UNROLL_14(MACRO, args...) __UNROLL_13(MACRO, args) MACRO(13, args) +#define __UNROLL_15(MACRO, args...) __UNROLL_14(MACRO, args) MACRO(14, args) +#define __UNROLL_16(MACRO, args...) __UNROLL_15(MACRO, args) MACRO(15, args) +#define __UNROLL_17(MACRO, args...) __UNROLL_16(MACRO, args) MACRO(16, args) +#define __UNROLL_18(MACRO, args...) __UNROLL_17(MACRO, args) MACRO(17, args) +#define __UNROLL_19(MACRO, args...) __UNROLL_18(MACRO, args) MACRO(18, args) +#define __UNROLL_20(MACRO, args...) __UNROLL_19(MACRO, args) MACRO(19, args) + +#endif /* __UNROLL_H */
Signature · 1 line
2.46.0.184.g6999bdac58-goog
PATCH v15 3/4 lsm: count the LSMs enabled at compile time
In reply to KP SinghThese macros are a clever trick to determine a count of the number of LSMs that are enabled in the config to ascertain the maximum number of static calls that need to be configured per LSM hook.
Without this one would need to generate static calls for the total number of LSMs in the kernel (even if they are not compiled) times the number of LSM hooks which ends up being quite wasteful.
--- include/linux/args.h | 6 +- include/linux/lsm_count.h | 128 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 include/linux/lsm_count.h
include/linux/args.h +3 -3
@@ -17,9 +17,9 @@* that as _n. */ -/* This counts to 12. Any more, it will return 13th argument. */ -#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n -#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) +/* This counts to 15. Any more, it will return 16th argument. */ +#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _n, X...) _n +#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)/* Concatenate two parameters, but allow them to be expanded beforehand. */ #define __CONCAT (a, b) a ## b
include/linux/lsm_count.h new file +128 -0
128 lines not on this page · raw message
Signature · 1 line
2.46.0.184.g6999bdac58-goog
Paul Moore
maintainerRe: [PATCH v15 3/4] lsm: count the LSMs enabled at compile time
Quoted 9 lines · KP Singh
These macros are a clever trick to determine a count of the number of LSMs that are enabled in the config to ascertain the maximum number of static calls that need to be configured per LSM hook.
Without this one would need to generate static calls for the total number of LSMs in the kernel (even if they are not compiled) times the number of LSM hooks which ends up being quite wasteful.
Quoted 10 lines
Suggested-by Kui-Feng Lee <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Song Liu <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Reviewed-by Kees Cook <[email protected]>Reviewed-by Casey Schaufler <[email protected]>Signed-off-by KP Singh <[email protected]>Reviewed-by John Johansen <[email protected]>[PM: subj tweaks]Signed-off-by Paul Moore <[email protected]>
For future reference, it's fine to grab the commits that I previously merged into the lsm/dev branch to use as a base, but you should probably drop the merge edit notes (the stuff in the braces) when you (re)post the patches.
Quoted 4 lines
include/linux/args.h | 6 +- include/linux/lsm_count.h | 128 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 include/linux/lsm_count.h
Signature · 1 line
paul-moore.com
Paul Moore
maintainerRe: [PATCH v15 3/4] lsm: count the LSMs enabled at compile time
In reply to KP SinghQuoted 24 lines · KP Singh
These macros are a clever trick to determine a count of the number of LSMs that are enabled in the config to ascertain the maximum number of static calls that need to be configured per LSM hook.
Without this one would need to generate static calls for the total number of LSMs in the kernel (even if they are not compiled) times the number of LSM hooks which ends up being quite wasteful.
Suggested-by Kui-Feng Lee <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Song Liu <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Reviewed-by Kees Cook <[email protected]>Reviewed-by Casey Schaufler <[email protected]>Signed-off-by KP Singh <[email protected]>Reviewed-by John Johansen <[email protected]>[PM: subj tweaks]Signed-off-by Paul Moore <[email protected]>--- include/linux/args.h | 6 +- include/linux/lsm_count.h | 128 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 include/linux/lsm_count.h
...
Quoted 6 lines
include/linux/lsm_count.h new file +0
-0@@ -0,0 +1,128 @@
...
Quoted 12 lines
include/linux/lsm_count.h new file +12
-0+/* +* There is a trailing comma that we need to be accounted for. This is done by +* using a skipped argument in __COUNT_LSMS +*/ +#define __COUNT_LSMS(skipped_arg, args...) COUNT_ARGS(args...) +#define COUNT_LSMS(args...) __COUNT_LSMS(args) + +#define MAX_LSM_COUNT \ + COUNT_LSMS( \ + CAPABILITIES_ENABLED \ + SELINUX_ENABLED \ + SMACK_ENABLED \
include/linux/lsm_count.h line 120
Quoted 10 lines
include/linux/lsm_count.h new file +10
-0+APPARMOR_ENABLED \ + TOMOYO_ENABLED \ +YAMA_ENABLED \ + LOADPIN_ENABLED \ +LOCKDOWN_ENABLED \ + SAFESETID_ENABLED \ +BPF_LSM_ENABLED \ + LANDLOCK_ENABLED \ +IMA_ENABLED \ + EVM_ENABLED)
The above is missing an entry for IPE; I missed this during the merge, thanks to Fan for pointing it out. As the IPE patchset was merged into the lsm/dev tree only a few hours before this patchset, that isn't your fault, it's mine :)
Regardless, it should be fixed in lsm/dev now.
Signature · 1 line
paul-moore.com
Re: [PATCH v15 3/4] lsm: count the LSMs enabled at compile time
In reply to KP SinghQuoted 7 lines · KP Singh
These macros are a clever trick to determine a count of the number of LSMs that are enabled in the config to ascertain the maximum number of static calls that need to be configured per LSM hook.
Without this one would need to generate static calls for the total number of LSMs in the kernel (even if they are not compiled) times the number of LSM hooks which ends up being quite wasteful.
...
include/linux/args.h line 22
Quoted 6 lines
include/linux/args.h +3
-3-/* This counts to 12. Any more, it will return 13th argument. */ -#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n -#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) +/* This counts to 15. Any more, it will return 16th argument. */ +#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _n, X...) _n +#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
You forgot to update _12 in the upper comment.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Paul Moore
maintainerRe: [PATCH v15 3/4] lsm: count the LSMs enabled at compile time
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · KP Singh
These macros are a clever trick to determine a count of the number of LSMs that are enabled in the config to ascertain the maximum number of static calls that need to be configured per LSM hook.
Without this one would need to generate static calls for the total number of LSMs in the kernel (even if they are not compiled) times the number of LSM hooks which ends up being quite wasteful.
...
Quoted 12 lines
Quoted 10 lines
(quoted diff) +0
-2-/* This counts to 12. Any more, it will return 13th argument. */ -#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11,_12, _n, X...) _n
-#defineCOUNT_ARGS(X...)__COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)(quoted diff) +2
-0+/* This counts to 15. Any more, it will return 16th argument. */ +#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11,_12, _13, _14, _15, _n, X...) _n
+#defineCOUNT_ARGS(X...)__COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)You forgot to update _12 in the upper comment.
Do you plan to send a patch to fix this?
--
paul-moore.com
Re: [PATCH v15 3/4] lsm: count the LSMs enabled at compile time
Quoted 3 lines · Paul Moore
Quoted 1 line · Andy Shevchenko
On Fri, Aug 16, 2024 at 05:43:06PM +0200, KP Singh wrote:
...
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-2-/* This counts to 12. Any more, it will return 13th argument. */ -#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10,_11, _12, _n, X...) _n
-#defineCOUNT_ARGS(X...)__COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7,Quoted 8 lines
Quoted 6 lines
6, 5, 4, 3, 2, 1, 0)
(quoted diff) +2
-0+/* This counts to 15. Any more, it will return 16th argument. */ +#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10,_11, _12, _13, _14, _15, _n, X...) _n
+#defineCOUNT_ARGS(X...)__COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)You forgot to update _12 in the upper comment.
Quoted 1 line
Do you plan to send a patch to fix this?
Not really. Too lazy to write a commit message for it. Appreciate if you or the initial author or somebody else can do that.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v15 4/4 lsm: replace indirect LSM hook calls with static calls
In reply to KP SinghLSM hooks are currently invoked from a linked list as indirect calls which are invoked using retpolines as a mitigation for speculative attacks (Branch History / Target injection) and add extra overhead which is especially bad in kernel hot paths:
security_file_ioctl:
0xff...0320 <+0>: endbr64
0xff...0324 <+4>: push %rbp 0xff...0325 <+5>: push %r15 0xff...0327 <+7>: push %r14 0xff...0329 <+9>: push %rbx 0xff...032a <+10>: mov %rdx,%rbx 0xff...032d <+13>: mov %esi,%ebp 0xff...032f <+15>: mov %rdi,%r14 0xff...0332 <+18>: mov $0xff...7030,%r15 0xff...0339 <+25>: mov (%r15),%r15 0xff...033c <+28>: test %r15,%r15 0xff...033f <+31>: je 0xff...0358 <security_file_ioctl+56> 0xff...0341 <+33>: mov 0x18(%r15),%r11 0xff...0345 <+37>: mov %r14,%rdi 0xff...0348 <+40>: mov %ebp,%esi 0xff...034a <+42>: mov %rbx,%rdx
0xff...034d <+45>: call 0xff...2e0 <__x86_indirect_thunk_array+352>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Indirect calls that use retpolines leading to overhead, not just due to extra instruction but also branch misses.
0xff...0352 <+50>: test %eax,%eax 0xff...0354 <+52>: je 0xff...0339 <security_file_ioctl+25> 0xff...0356 <+54>: jmp 0xff...035a <security_file_ioctl+58> 0xff...0358 <+56>: xor %eax,%eax 0xff...035a <+58>: pop %rbx 0xff...035b <+59>: pop %r14 0xff...035d <+61>: pop %r15 0xff...035f <+63>: pop %rbp 0xff...0360 <+64>: jmp 0xff...47c4 <__x86_return_thunk>
The indirect calls are not really needed as one knows the addresses of enabled LSM callbacks at boot time and only the order can possibly change at boot time with the lsm= kernel command line parameter.
An array of static calls is defined per LSM hook and the static calls are updated at boot time once the order has been determined.
With the hook now exposed as a static call, one can see that the retpolines are no longer there and the LSM callbacks are invoked directly:
security_file_ioctl:
0xff...0ca0 <+0>: endbr64
0xff...0ca4 <+4>: nopl 0x0(%rax,%rax,1)
0xff...0ca9 <+9>: push %rbp
0xff...0caa <+10>: push %r14 0xff...0cac <+12>: push %rbx 0xff...0cad <+13>: mov %rdx,%rbx 0xff...0cb0 <+16>: mov %esi,%ebp 0xff...0cb2 <+18>: mov %rdi,%r14 0xff...0cb5 <+21>: jmp 0xff...0cc7 <security_file_ioctl+39>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Static key enabled for SELinux
0xffffffff818f0cb7 <+23>: jmp 0xff...0cde <security_file_ioctl+62>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Static key enabled for BPF LSM. This is something that is changed to default to false to avoid the existing side effect issues of BPF LSM
[1] in a subsequent patch.
0xff...0cb9 <+25>: xor %eax,%eax 0xff...0cbb <+27>: xchg %ax,%ax 0xff...0cbd <+29>: pop %rbx 0xff...0cbe <+30>: pop %r14 0xff...0cc0 <+32>: pop %rbp 0xff...0cc1 <+33>: cs jmp 0xff...0000 <__x86_return_thunk> 0xff...0cc7 <+39>: endbr64 0xff...0ccb <+43>: mov %r14,%rdi 0xff...0cce <+46>: mov %ebp,%esi 0xff...0cd0 <+48>: mov %rbx,%rdx 0xff...0cd3 <+51>: call 0xff...3230 <selinux_file_ioctl>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Direct call to SELinux.
0xff...0cd8 <+56>: test %eax,%eax 0xff...0cda <+58>: jne 0xff...0cbd <security_file_ioctl+29> 0xff...0cdc <+60>: jmp 0xff...0cb7 <security_file_ioctl+23> 0xff...0cde <+62>: endbr64 0xff...0ce2 <+66>: mov %r14,%rdi 0xff...0ce5 <+69>: mov %ebp,%esi 0xff...0ce7 <+71>: mov %rbx,%rdx 0xff...0cea <+74>: call 0xff...e220 <bpf_lsm_file_ioctl>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Direct call to BPF LSM.
0xff...0cef <+79>: test %eax,%eax 0xff...0cf1 <+81>: jne 0xff...0cbd <security_file_ioctl+29> 0xff...0cf3 <+83>: jmp 0xff...0cb9 <security_file_ioctl+25> 0xff...0cf5 <+85>: endbr64 0xff...0cf9 <+89>: mov %r14,%rdi 0xff...0cfc <+92>: mov %ebp,%esi 0xff...0cfe <+94>: mov %rbx,%rdx 0xff...0d01 <+97>: pop %rbx 0xff...0d02 <+98>: pop %r14
0xff...0d04 <+100>: pop %rbp
0xff...0d05 <+101>: ret
0xff...0d06 <+102>: int3
0xff...0d07 <+103>: int3
0xff...0d08 <+104>: int3
0xff...0d09 <+105>: int3
While this patch uses static_branch_unlikely indicating that an LSM hook is likely to be not present. In most cases this is still a better choice as even when an LSM with one hook is added, empty slots are created for all LSM hooks (especially when many LSMs that do not initialize most hooks are present on the system).
There are some hooks that don't use the call_int_hook or call_void_hook. These hooks are updated to use a new macro called lsm_for_each_hook where the lsm_callback is directly invoked as an indirect call.
Below are results of the relevant Unixbench system benchmarks with BPF LSM and SELinux enabled with default policies enabled with and without these patches.
Benchmark Delta(%): (+ is better)
==========================================================================
Execl Throughput +1.9356 File Write 1024 bufsize 2000 maxblocks +6.5953 Pipe Throughput +9.5499 Pipe-based Context Switching +3.0209 Process Creation +2.3246 Shell Scripts (1 concurrent) +1.4975 System Call Overhead +2.7815 System Benchmarks Index Score (Partial Only): +3.4859
In the best case, some syscalls like eventfd_create benefitted to about ~10%.
--- include/linux/lsm_hooks.h | 52 +++++++-- security/security.c | 219 +++++++++++++++++++++++++++----------- 2 files changed, 198 insertions(+), 73 deletions(-)
include/linux/lsm_hooks.h +43 -9
@@ -30,19 +30,47 @@#include <linux/init.h> #include <linux/rculist.h> #include <linux/xattr.h> +#include <linux/static_call.h> +#include <linux/unroll.h> +#include <linux/jump_label.h> +#include <linux/lsm_count.h> union security_list_options {#define LSM_HOOK (RET ,DEFAULT ,NAME , ...) RET (*NAME)(__VA_ARGS__);#include "lsm_hook_defs.h"#undef LSM_HOOK +void *lsm_func_addr; }; -struct security_hook_heads { -#define LSM_HOOK (RET ,DEFAULT ,NAME , ...) struct hlist_head NAME; -#include "lsm_hook_defs.h" -#undef LSM_HOOK +/* +* @key: static call key as defined by STATIC_CALL_KEY +* @trampoline: static call trampoline as defined by STATIC_CALL_TRAMP +* @hl: The security_hook_list as initialized by the owning LSM. +* @active: Enabled when the static call has an LSM hook associated. +*/ +struct lsm_static_call { +struct static_call_key *key; +void *trampoline; +struct security_hook_list *hl; +/* this needs to be true or false based on what the key defaults to */ +struct static_key_false *active; } __randomize_layout; +/* +* Table of the static calls for each LSM hook. +* Once the LSMs are initialized, their callbacks will be copied to these +* tables such that the calls are filled backwards (from last to first). +* This way, we can jump directly to the first used static call, and execute +* all of them after. This essentially makes the entry point +* dynamic to adapt the number of static calls to the number of callbacks. +*/ +struct lsm_static_calls_table { +#define LSM_HOOK (RET ,DEFAULT ,NAME , ...) \ + struct lsm_static_call NAME[MAX_LSM_COUNT]; +#include <linux/lsm_hook_defs.h> +#undef LSM_HOOK +} __packed __randomize_layout; +/** * struct lsm_id - Identify a Linux Security Module. * @lsm: name of the LSM, must be approved by the LSM maintainers
@@ -58,10 +86,14 @@ struct lsm_id {/* * Security module hook list structure. * For use with generic list macros for common operations. +* +* struct security_hook_list - Contents of a cacheable, mappable object. +* @scalls: The beginning of the array of static calls assigned to this hook. +* @hook: The callback for the hook. +* @lsm: The name of the lsm that owns this hook. */ struct security_hook_list { -struct hlist_node list; -struct hlist_head *head; +struct lsm_static_call *scalls;union security_list_options hook;const struct lsm_id *lsmid; } __randomize_layout;
@@ -97,8 +129,11 @@ struct lsm_blob_sizes {* care of the common case and reduces the amount of * text involved. */ -#define LSM_HOOK_INIT (HEAD ,HOOK ) \ - { .head = &security_hook_heads.HEAD, .hook = { .HEAD = HOOK } } +#define LSM_HOOK_INIT (NAME ,HOOK ) \ + { \ + .scalls = static_calls_table.NAME, \ + .hook = { .NAME = HOOK } \ + }extern void security_add_hooks (struct security_hook_list *hooks,int count,const struct lsm_id *lsmid);
@@ -133,7 +168,6 @@ struct lsm_info {/* DO NOT tamper with these variables outside of the LSM framework */ extern char *lsm_names; -extern struct security_hook_heads security_hook_heads;extern struct lsm_static_calls_table static_calls_table__ro_after_init ;extern struct lsm_info __start_lsm_info[], __end_lsm_info[];extern struct lsm_info __start_early_lsm_info[], __end_early_lsm_info[];
security/security.c +155 -64
376 lines not on this page · raw message
Signature · 1 line
2.46.0.184.g6999bdac58-goog
On 8/16/24 08:43, KP Singh wrote:
[ ... ]
Quoted 6 lines
# v14 to v15* Fixed early LSM init wuth Patch 1
* Made the static call table aligned to u64 and added a comment as to why this is needed.
Applied to v6.11-rc3, together with several other LSM patches from linux-next to avoid conflicts, this series passes all my qemu tests. Feel free to add
Thanks,
Guenter
Paul Moore
maintainerQuoted 9 lines · Guenter Roeck
On 8/16/24 08:43, KP Singh wrote:
[ ... ]Quoted 6 lines
# v14 to v15* Fixed early LSM init wuth Patch 1
* Made the static call table aligned to u64 and added a comment as to why this is needed.
Quoted 4 lines
Applied to v6.11-rc3, together with several other LSM patches from linux-next to avoid conflicts, this series passes all my qemu tests. Feel free to add
Tested-by Guenter Roeck <[email protected]>
Thanks Guenter, I appreciate your help testing and debugging all of the different arches.
Signature · 1 line
paul-moore.com
Quoted 5 lines · KP Singh
# v13 to v14* Dropped Patch 5 based on the ongoing discussion in
https://lore.kernel.org/linux-security-module/[email protected]/, BPF LSM will still have default callbacks enabled.
Why not use
struct lsm_callback {
struct list_head list;
struct static_call_key key;
}for each callback given that the latency is mostly caused by use of indirect function call?
Then, we don't need "lsm: count the LSMs enabled at compile time" (which I'm NACKing).
Quoted 3 lines
* Dropped Patch 4 as recommended by Paul, indirect calls will remain in some LSM hooks for now.
https://lore.kernel.org/linux-security-module/[email protected]/
* Fixed minor nits in Patch 3
Quoted 7 lines · KP Singh
# Background
LSM hooks (callbacks) are currently invoked as indirect function calls. These callbacks are registered into a linked list at boot time as the order of the LSMs can be configured on the kernel command line with the "lsm=" command line parameter ...
Merged into lsm/dev, thanks all.
Signature · 1 line
paul-moore.com