seccomp: allow BPF_MOD ALU instructions
Subsystems (2)
PATCH 1/1 seccomp: allow BPF_MOD ALU instructions
The BPF_MOD ALU instructions could be utilized by seccomp classic BPF filters, but were missing from the explicit list of allowed calls since its introduction in the original e2cfabdfd075 ("seccomp: add system call filtering using BPF") commit. Add support for these instructions by adding them to the allowed list in the seccomp_check_filter function.
--- kernel/seccomp.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -0
@@ -206,6 +206,8 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU |BPF_DIV |BPF_K :case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_MOD |BPF_X :case BPF_ALU |BPF_AND |BPF_K :case BPF_ALU |BPF_AND |BPF_X :case BPF_ALU |BPF_OR |BPF_K :
Signature · 1 line
2.19.1
Quoted 7 lines · Anton Protopopov
The
BPF_MODALU instructions could be utilized by seccomp classic BPF filters, but were missing from the explicit list of allowed calls since its introduction in the originale2cfabdfd075("seccomp: add system call filtering using BPF") commit. Add support for these instructions by adding them to the allowed list in theseccomp_check_filterfunction.Signed-off-by Anton Protopopov <[email protected]>
This has been suggested in the past, but was deemed ultimately redundant:
https://lore.kernel.org/lkml/201908121035.06695C79F@keescook/
Is there a strong reason it's needed?
Thanks!
-Kees
Quoted 19 lines
--- kernel/seccomp.c | 2 ++ 1 file changed, 2 insertions(+)kernel/seccomp.c +2
-0@@ -206,6 +206,8 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU |BPF_DIV |BPF_K :case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_MOD |BPF_X :case BPF_ALU |BPF_AND |BPF_K :case BPF_ALU |BPF_AND |BPF_X :case BPF_ALU |BPF_OR |BPF_K :Signature · 1 line
2.19.1
Signature · 1 line
Kees Cook
пн, 16 мар. 2020 г. в 17:24, Kees Cook <[email protected]>:
Quoted 8 lines · Anton Protopopov
Quoted 6 lines
The
BPF_MODALU instructions could be utilized by seccomp classic BPF filters, but were missing from the explicit list of allowed calls since its introduction in the originale2cfabdfd075("seccomp: add system call filtering using BPF") commit. Add support for these instructions by adding them to the allowed list in theseccomp_check_filterfunction.
Quoted 4 lines
Quoted 1 line
Signed-off-by Anton Protopopov <[email protected]>This has been suggested in the past, but was deemed ultimately redundant:
https://lore.kernel.org/lkml/201908121035.06695C79F@keescook/
Yeah, Paul told me this right after I submitted the patch.
Quoted 1 line
Is there a strong reason it's needed?
I really don't have such a strong need in BPF_MOD, but let me tell why I wanted to use it in the first place.
I've used this operation to speedup processing linear blacklist filters. Namely, if you have a list of syscall numbers to blacklist, you can do, say,
ldw [0]
mod #4
jeq #1, case1
jeq #1, case2
jeq #1, case3
case0:
...
and in every case to walk only a corresponding factor-list. In my case I had a list of ~40 syscall numbers and after this change filter executed in 17.25 instructions on average per syscall vs. 45 instructions for the linear filter (so this removes about 30 instructions penalty per every syscall). To replace "mod #4" I actually used "and #3", but this obviously doesn't work for non-power-of-two divisors. If I would use "mod 5", then it would give me about 15.5 instructions on average.
Thanks,
Anton
Quoted 27 lines
Thanks!
-Kees
Quoted 18 lines
--- kernel/seccomp.c | 2 ++ 1 file changed, 2 insertions(+)kernel/seccomp.c +2
-1@@ -206,6 +206,8 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU |BPF_DIV |BPF_K :case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_MOD |BPF_X :case BPF_ALU |BPF_AND |BPF_K :case BPF_ALU |BPF_AND |BPF_X :case BPF_ALU |BPF_OR |BPF_K : --Quoted 1 line
2.19.1
--
Kees Cook
Quoted 8 lines · Anton Protopopov
and in every case to walk only a corresponding factor-list. In my case I had a list of ~40 syscall numbers and after this change filter executed in 17.25 instructions on average per syscall vs. 45 instructions for the linear filter (so this removes about 30 instructions penalty per every syscall). To replace "mod #4" I actually used "and #3", but this obviously doesn't work for non-power-of-two divisors. If I would use "mod 5", then it would give me about 15.5 instructions on average.
Gotcha. My real concern is with breaking the ABI here -- using BPF_MOD would mean a process couldn't run on older kernels without some tricks on the seccomp side.
Since the syscall list is static for a given filter, why not arrange it as a binary search? That should get even better average instructions as O(log n) instead of O(n).
Though frankly I've also been considering an ABI version bump for adding a syscall bitmap feature: the vast majority of seccomp filters are just binary yes/no across a list of syscalls. Only the special cases need special handling (arg inspection, fd notification, etc). Then these kinds of filters could run as O(1).
Signature · 1 line
Kees Cook
вт, 17 мар. 2020 г. в 16:21, Kees Cook <[email protected]>:
Quoted 11 lines · Anton Protopopov
Quoted 8 lines
and in every case to walk only a corresponding factor-list. In my case I had a list of ~40 syscall numbers and after this change filter executed in 17.25 instructions on average per syscall vs. 45 instructions for the linear filter (so this removes about 30 instructions penalty per every syscall). To replace "mod #4" I actually used "and #3", but this obviously doesn't work for non-power-of-two divisors. If I would use "mod 5", then it would give me about 15.5 instructions on average.
Quoted 3 lines
Gotcha. My real concern is with breaking the ABI here -- using
BPF_MODwould mean a process couldn't run on older kernels without some tricks on the seccomp side.
Yes, I understood. Could you tell what would you do exactly if there was a real need in a new instruction?
Quoted 3 lines
Since the syscall list is static for a given filter, why not arrange it as a binary search? That should get even better average instructions as O(log n) instead of O(n).
Right, thanks! This saves about 4 more instructions for my case and works 1-2 ns faster.
Quoted 8 lines
Though frankly I've also been considering an ABI version bump for adding a syscall bitmap feature: the vast majority of seccomp filters are just binary yes/no across a list of syscalls. Only the special cases need special handling (arg inspection, fd notification, etc). Then these kinds of filters could run as O(1).
--
Kees Cook
Thanks,
Anton
Quoted 12 lines · Anton Protopopov
вт, 17 мар. 2020 г. в 16:21, Kees Cook <[email protected]>:
Quoted 11 lines · Anton Protopopov
Quoted 8 lines
and in every case to walk only a corresponding factor-list. In my case I had a list of ~40 syscall numbers and after this change filter executed in 17.25 instructions on average per syscall vs. 45 instructions for the linear filter (so this removes about 30 instructions penalty per every syscall). To replace "mod #4" I actually used "and #3", but this obviously doesn't work for non-power-of-two divisors. If I would use "mod 5", then it would give me about 15.5 instructions on average.
Quoted 6 lines
Quoted 3 lines
Gotcha. My real concern is with breaking the ABI here -- using
BPF_MODwould mean a process couldn't run on older kernels without some tricks on the seccomp side.Yes, I understood. Could you tell what would you do exactly if there was a real need in a new instruction?
I'd likely need to introduce some kind of way to query (and declare) the "language version" of seccomp filters. New programs would need to declare the language level (EINVAL would mean the program must support the original "v1", ENOTSUPP would mean "kernel doesn't support that level"), and the program would have to build a filter based on the supported language features. The kernel would assume all undeclared seccomp users were "v1" and would need to reject BPF_MOD. All programs declaring "v2" would be allowed to use BPF_MOD.
It's really a lot for something that isn't really needed. :)
Quoted 6 lines
Quoted 3 lines
Since the syscall list is static for a given filter, why not arrange it as a binary search? That should get even better average instructions as O(log n) instead of O(n).
Right, thanks! This saves about 4 more instructions for my case and works 1-2 ns faster.
Excellent!
Quoted 5 lines
Quoted 5 lines
Though frankly I've also been considering an ABI version bump for adding a syscall bitmap feature: the vast majority of seccomp filters are just binary yes/no across a list of syscalls. Only the special cases need special handling (arg inspection, fd notification, etc). Then these kinds of filters could run as O(1).
*This* feature wouldn't need my crazy language version idea, but it _would_ still need to be detectable, much like how RET_USER_NOTIF was added.
Signature · 1 line
Kees Cook
ср, 18 мар. 2020 г. в 00:06, Kees Cook <[email protected]>:
Quoted 30 lines · Anton Protopopov
Quoted 12 lines
вт, 17 мар. 2020 г. в 16:21, Kees Cook <[email protected]>:
Quoted 11 lines · Anton Protopopov
Quoted 8 lines
and in every case to walk only a corresponding factor-list. In my case I had a list of ~40 syscall numbers and after this change filter executed in 17.25 instructions on average per syscall vs. 45 instructions for the linear filter (so this removes about 30 instructions penalty per every syscall). To replace "mod #4" I actually used "and #3", but this obviously doesn't work for non-power-of-two divisors. If I would use "mod 5", then it would give me about 15.5 instructions on average.
Quoted 6 lines
Quoted 3 lines
Gotcha. My real concern is with breaking the ABI here -- using
BPF_MODwould mean a process couldn't run on older kernels without some tricks on the seccomp side.Yes, I understood. Could you tell what would you do exactly if there was a real need in a new instruction?
I'd likely need to introduce some kind of way to query (and declare) the "language version" of seccomp filters. New programs would need to declare the language level (EINVAL would mean the program must support the original "v1", ENOTSUPP would mean "kernel doesn't support that level"), and the program would have to build a filter based on the supported language features. The kernel would assume all undeclared seccomp users were "v1" and would need to reject
BPF_MOD. All programs declaring "v2" would be allowed to useBPF_MOD.
Quoted 1 line
It's really a lot for something that isn't really needed. :)
Right :) Thanks for the explanations!
Quoted 21 lines
Quoted 6 lines
Quoted 3 lines
Since the syscall list is static for a given filter, why not arrange it as a binary search? That should get even better average instructions as O(log n) instead of O(n).
Right, thanks! This saves about 4 more instructions for my case and works 1-2 ns faster.
Excellent!
Quoted 5 lines
Quoted 5 lines
Though frankly I've also been considering an ABI version bump for adding a syscall bitmap feature: the vast majority of seccomp filters are just binary yes/no across a list of syscalls. Only the special cases need special handling (arg inspection, fd notification, etc). Then these kinds of filters could run as O(1).
*This* feature wouldn't need my crazy language version idea, but it _would_ still need to be detectable, much like how
RET_USER_NOTIFwas added.--
Kees Cook