-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
seccomp: emulate safe privileged system calls #61
Open
Snaipe
wants to merge
2
commits into
main
Choose a base branch
from
feature/seccomp-emulation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright © 2024 Arista Networks, Inc. All rights reserved. | ||
* | ||
* Use of this source code is governed by the MIT license that can be found | ||
* in the LICENSE file. | ||
*/ | ||
|
||
#ifndef ARCH_H_ | ||
# define ARCH_H_ | ||
|
||
# include "config.h" | ||
|
||
# define ARCH_STR_(x) #x | ||
# define ARCH_STR(x) ARCH_STR_(x) | ||
|
||
/* *INDENT-OFF* - formatters try to add spaces here */ | ||
# define ARCH_HEADER_BASE arch/ARCH | ||
/* *INDENT-ON* */ | ||
|
||
# include ARCH_STR(ARCH_HEADER_BASE/syscall.h) | ||
|
||
#endif /* !ARCH_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/bin/bash | ||
|
||
# This script generates the classic BPF program to intercept system calls | ||
# in AArch64 userspace. | ||
|
||
# From asm/unistd.h -- or you can use https://arm64.syscall.sh/ for new ones | ||
declare -A syscalls=( | ||
["mknodat"]="33" | ||
) | ||
|
||
prelude=( | ||
# Check that we're running on AArch64 | ||
'BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, arch)))' | ||
'BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_AARCH64, 1, 0)' | ||
'BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)' | ||
|
||
# Load syscall number | ||
'BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr)))' | ||
) | ||
|
||
syscall_jump=( | ||
'BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, $nr, 0, 1)' | ||
'BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF)' | ||
) | ||
|
||
# NOTE: indentation is done with tabs. Do not use spaces, do not remove tabs, | ||
# lest you break all HEREDOCs. | ||
|
||
gen_source() { | ||
cat <<-EOF | ||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
#include <stddef.h> | ||
#include <linux/audit.h> | ||
#include <linux/bpf_common.h> | ||
#include <linux/filter.h> | ||
#include <linux/seccomp.h> | ||
|
||
const struct sock_filter syscall_filter[] = { | ||
EOF | ||
|
||
for stmt in "${prelude[@]}"; do | ||
eval "echo $'\t'\"$stmt\"," | ||
done | ||
|
||
for syscall in "${!syscalls[@]}"; do | ||
nr=${syscalls[$syscall]} | ||
for stmt in "${syscall_jump[@]}"; do | ||
eval "echo $'\t'\"$stmt\"," | ||
done | ||
done | ||
|
||
echo $'\t''BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),' | ||
|
||
cat <<-EOF | ||
}; | ||
|
||
const size_t syscall_filter_length = sizeof (syscall_filter) / sizeof (struct sock_filter); | ||
|
||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ | ||
EOF | ||
} | ||
|
||
gen_header() { | ||
cat <<-EOF | ||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
extern const struct sock_filter syscall_filter[]; | ||
extern const size_t syscall_filter_length; | ||
|
||
EOF | ||
|
||
max=0 | ||
for syscall in "${!syscalls[@]}"; do | ||
echo "#define BST_NR_${syscall} ${syscalls[$syscall]}" | ||
(( ${syscalls[$syscall]} > max )) && max=${syscalls[$syscall]} | ||
done | ||
|
||
cat <<-EOF | ||
|
||
#define BST_NR_MAX $max | ||
|
||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ | ||
EOF | ||
} | ||
|
||
gen_source > arch/aarch64/syscall.c | ||
gen_header > arch/aarch64/syscall.h | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
#include <stddef.h> | ||
#include <linux/audit.h> | ||
#include <linux/bpf_common.h> | ||
#include <linux/filter.h> | ||
#include <linux/seccomp.h> | ||
|
||
const struct sock_filter syscall_filter[] = { | ||
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, arch))), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_AARCH64, 1, 0), | ||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS), | ||
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr))), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 33, 0, 1), | ||
BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF), | ||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), | ||
}; | ||
|
||
const size_t syscall_filter_length = sizeof (syscall_filter) / sizeof (struct sock_filter); | ||
|
||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
extern const struct sock_filter syscall_filter[]; | ||
extern const size_t syscall_filter_length; | ||
|
||
#define BST_NR_mknodat 33 | ||
|
||
#define BST_NR_MAX 33 | ||
|
||
/* THIS FILE WAS GENERATED BY arch/aarch64/gen-syscall.bash -- DO NOT EDIT */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/bin/bash | ||
|
||
# This script generates the classic BPF program to intercept system calls | ||
# in x86 userspace. | ||
|
||
# From asm/unistd_64.h | ||
declare -A x86_64_syscalls=( | ||
["mknod"]="133" | ||
["mknodat"]="259" | ||
) | ||
|
||
# From asm/unistd_32.h | ||
declare -A i386_syscalls=( | ||
["mknod"]="14" | ||
["mknodat"]="297" | ||
) | ||
|
||
prelude=( | ||
# Check that we're running on x86_64 or i386 | ||
'BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, arch)))' | ||
'BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_I386, $(($i386_offset-2)), 0)' | ||
'BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_X86_64, 1, 0)' | ||
'BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)' | ||
|
||
# The x32 ABI (not to be confused with the i386 ABI!) uses the | ||
# same system call numbers as x86_64, but set bit 30. Clear it so we share | ||
# the same table. | ||
'BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr)))' | ||
'BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, X32_SYSCALL_BIT, 0, 1)' | ||
'BPF_STMT(BPF_ALU | BPF_SUB | BPF_K, X32_SYSCALL_BIT)' | ||
) | ||
|
||
syscall_jump=( | ||
'BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, $nr, 0, 1)' | ||
'BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF)' | ||
) | ||
|
||
i386_offset=$((${#prelude[@]} + ${#syscall_jump[@]}*${#x86_64_syscalls[@]} + 1)) | ||
|
||
# NOTE: indentation is done with tabs. Do not use spaces, do not remove tabs, | ||
# lest you break all HEREDOCs. | ||
|
||
gen_source() { | ||
cat <<-EOF | ||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
#include <stddef.h> | ||
#include <linux/audit.h> | ||
#include <linux/bpf_common.h> | ||
#include <linux/filter.h> | ||
#include <linux/seccomp.h> | ||
|
||
/* For the x32 ABI, all system call numbers have bit 30 set */ | ||
#define X32_SYSCALL_BIT 0x40000000 | ||
|
||
const struct sock_filter syscall_filter[] = { | ||
EOF | ||
|
||
for stmt in "${prelude[@]}"; do | ||
eval "echo $'\t'\"$stmt\"," | ||
done | ||
|
||
for syscall in "${!x86_64_syscalls[@]}"; do | ||
nr=${x86_64_syscalls[$syscall]} | ||
for stmt in "${syscall_jump[@]}"; do | ||
eval "echo $'\t'\"$stmt\"," | ||
done | ||
done | ||
|
||
echo $'\t''BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),' | ||
echo $'\t''BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr))),' | ||
|
||
for syscall in "${!i386_syscalls[@]}"; do | ||
nr=${i386_syscalls[$syscall]} | ||
for stmt in "${syscall_jump[@]}"; do | ||
eval "echo $'\t'\"$stmt\"," | ||
done | ||
done | ||
|
||
echo $'\t''BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),' | ||
|
||
cat <<-EOF | ||
}; | ||
|
||
const size_t syscall_filter_length = sizeof (syscall_filter) / sizeof (struct sock_filter); | ||
|
||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ | ||
EOF | ||
} | ||
|
||
gen_header() { | ||
cat <<-EOF | ||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
extern const struct sock_filter syscall_filter[]; | ||
extern const size_t syscall_filter_length; | ||
|
||
EOF | ||
|
||
for syscall in "${!x86_64_syscalls[@]}"; do | ||
echo "#define BST_NR_${syscall} ${x86_64_syscalls[$syscall]}" | ||
done | ||
|
||
for syscall in "${!i386_syscalls[@]}"; do | ||
echo "#define BST_NR_${syscall}_32 ${i386_syscalls[$syscall]}" | ||
done | ||
|
||
max=0 | ||
for syscall in "${!x86_64_syscalls[@]}"; do | ||
(( ${x86_64_syscalls[$syscall]} > max )) && max=${x86_64_syscalls[$syscall]} | ||
done | ||
|
||
max32=0 | ||
for syscall in "${!i386_syscalls[@]}"; do | ||
(( ${i386_syscalls[$syscall]} > max32 )) && max32=${i386_syscalls[$syscall]} | ||
done | ||
|
||
cat <<-EOF | ||
|
||
#define BST_SECCOMP_32 1 | ||
|
||
#define BST_NR_MAX $max | ||
#define BST_NR_MAX32 $max32 | ||
|
||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ | ||
EOF | ||
} | ||
|
||
gen_source > arch/x86/syscall.c | ||
gen_header > arch/x86/syscall.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid checking this in, given we generate it, and you've gone to the trouble of not needing anything other than bash to do so? |
||
|
||
#include <stddef.h> | ||
#include <linux/audit.h> | ||
#include <linux/bpf_common.h> | ||
#include <linux/filter.h> | ||
#include <linux/seccomp.h> | ||
|
||
/* For the x32 ABI, all system call numbers have bit 30 set */ | ||
#define X32_SYSCALL_BIT 0x40000000 | ||
|
||
const struct sock_filter syscall_filter[] = { | ||
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, arch))), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_I386, 10, 0), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_X86_64, 1, 0), | ||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS), | ||
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr))), | ||
BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, X32_SYSCALL_BIT, 0, 1), | ||
BPF_STMT(BPF_ALU | BPF_SUB | BPF_K, X32_SYSCALL_BIT), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 133, 0, 1), | ||
BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 259, 0, 1), | ||
BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF), | ||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), | ||
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr))), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 14, 0, 1), | ||
BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF), | ||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 297, 0, 1), | ||
BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_USER_NOTIF), | ||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), | ||
}; | ||
|
||
const size_t syscall_filter_length = sizeof (syscall_filter) / sizeof (struct sock_filter); | ||
|
||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ | ||
|
||
extern const struct sock_filter syscall_filter[]; | ||
extern const size_t syscall_filter_length; | ||
|
||
#define BST_NR_mknod 133 | ||
#define BST_NR_mknodat 259 | ||
#define BST_NR_mknod_32 14 | ||
#define BST_NR_mknodat_32 297 | ||
|
||
#define BST_SECCOMP_32 1 | ||
|
||
#define BST_NR_MAX 259 | ||
#define BST_NR_MAX32 297 | ||
|
||
/* THIS FILE WAS GENERATED BY arch/x86/gen-syscall.bash -- DO NOT EDIT */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x86 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I love bash as the tool here, but hey, I guess it's better than pulling a templating engine into the bst build!