samples/seccomp: enforce an architecture for bpf-fancy seccomp sample
PATCH 1/1 samples/seccomp: enforce an architecture for bpf-fancy seccomp sample
- Enforce user specified architecture for bpf-fancy seccomp sample.
- Kills the thread if user specified architecture does not match system architecture.
Signed-off-by Mayank Gite <[email protected]>
--- samples/seccomp/bpf-fancy.c | 11 ++++++++++- samples/seccomp/bpf-helper.h | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-)
samples/seccomp/bpf-fancy.c +10 -1
@@ -14,6 +14,7 @@#include <linux/seccomp.h> #include <linux/unistd.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/prctl.h> #include <unistd.h>
@@ -26,6 +27,14 @@int main (int argc,char **argv) { +if (argc <2 ) { +fprintf (stderr,"Usage:\n" +"bpf-fancy <architecture>\n" +"Help: AUDIT_ARCH_I386: 0x%X\n" +" AUDIT_ARCH_X86_64: 0x%X\n" +"\n" ,AUDIT_ARCH_I386 ,AUDIT_ARCH_X86_64 ); +return -1 ; + }struct bpf_labels l = { .count =0 , };
@@ -34,7 +43,7 @@ int main(int argc, char **argv)char buf[256 ];struct sock_filter filter[] = {/* TODO: LOAD_SYSCALL_NR(arch) and enforce an arch */ -LOAD_SYSCALL_NR , +LOAD_SYSCALL_NR (strtol (argv[1 ],NULL ,0 )),SYSCALL (__NR_exit,ALLOW ),SYSCALL (__NR_exit_group,ALLOW ),SYSCALL (__NR_write,JUMP (&l, write_fd)),
samples/seccomp/bpf-helper.h +7 -1
@@ -17,6 +17,7 @@#include <asm/bitsperlong.h> /* for __BITS_PER_LONG */ #include <endian.h> +#include <linux/audit.h> #include <linux/filter.h> #include <linux/seccomp.h> /* for seccomp_data */ #include <linux/types.h>
@@ -256,7 +257,12 @@ union arg64 { jt, \ BPF_STMT(BPF_LD+BPF_MEM, 1) -#define LOAD_SYSCALL_NR \ +#define LOAD_SYSCALL_NR (_arch) \ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ + offsetof(struct seccomp_data, arch)), \ + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \ + _arch, 1, 0), \ + DENY, \ BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \ offsetof(struct seccomp_data, nr))
Signature · 1 line
2.53.0