Skip to contentSkip to conversation

seccomp: allow BPF_MOD ALU instructions

PATCH In review Paul Chaignon 3 participants 3 messages Aug 9, 2019 – Aug 12, 2019 Secure computing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seccomp: allow BPF_MOD ALU instructions

We need BPF_MOD to match system calls against whitelists encoded as 32-bit bit arrays. The selection of the syscall's bit in the appropriate bit array requires a modulo operation such that X = 1 << nr % 32.

---
 kernel/seccomp.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -0
index 811b4a86cdf6..87de6532ff6d 100644
@@ -205,6 +205,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.17.1
#1 lore raw
Quoted 4 lines · Paul Chaignon

We need BPF_MOD to match system calls against whitelists encoded as 32-bit bit arrays. The selection of the syscall's bit in the appropriate bit array requires a modulo operation such that X = 1 << nr % 32.

Of course, X = 1 << nr & 0x1F, and we can do without BPF_MOD in our case. I'll put that on a lack of sleep...

Quoted 21 lines
---
 kernel/seccomp.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index 811b4a86cdf6..87de6532ff6d 100644
@@ -205,6 +205,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:
--

2.17.1

#2 lore raw
Quoted 8 lines · Paul Chaignon
Quoted 4 lines · Paul Chaignon

We need BPF_MOD to match system calls against whitelists encoded as 32-bit bit arrays. The selection of the syscall's bit in the appropriate bit array requires a modulo operation such that X = 1 << nr % 32.

Of course, X = 1 << nr & 0x1F, and we can do without BPF_MOD in our case. I'll put that on a lack of sleep...

No worries! Changing the dialect of seccomp BPF isn't something I'd like to do without really good reason since it creates a split in the filter correctness from userspace (i.e. a filter using BPF_MOD on an older kernel will fail). So there would need to be a distinct flag set somewhere, etc. So, if you do end up discovering later you really want BPF_MOD, we can figure that out, but for now if you can get by with "&", that would be best. :)

Thanks!

-Kees

Quoted 22 lines
Quoted 22 lines
---
 kernel/seccomp.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index 811b4a86cdf6..87de6532ff6d 100644
@@ -205,6 +205,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:
--

2.17.1

Signature · 1 line
Kees Cook