Skip to content
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 #532

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/packetgraph/seccomp-bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct seccomp_data {
};
#endif

extern int errno;

#define syscall_nr (offsetof(struct seccomp_data, nr))
#define arch_nr (offsetof(struct seccomp_data, arch))

Expand All @@ -49,7 +51,8 @@ struct seccomp_data {
#define VALIDATE_ARCHITECTURE \
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, arch_nr), \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARCH_NR, 1, 0), \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP), \
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)

#define EXAMINE_SYSCALL \
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr)
Expand All @@ -61,5 +64,8 @@ struct seccomp_data {
#define KILL_PROCESS \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)

#define TRAP_PROCESS \
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP)

#endif /* SECCOMP_BPF_H */

52 changes: 48 additions & 4 deletions src/seccomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,39 @@

#include <packetgraph/common.h>
#include <packetgraph/seccomp-bpf.h>
#include <errno.h>
#include <signal.h>
#include <string.h>

/*
* * Catch violations so we see, which system call caused the problems
* *
*/
static void catchViolation(int sig, siginfo_t *si, void *void_context)
{
fprintf(stderr,
"Catch sig [%d] when attemption to catch syscall: [%d]\n",
sig, si->si_syscall);
}
/*
* * Setup error handling
* *
*/
static void init_error_handling(void)
{
struct sigaction sa = { .sa_sigaction = catchViolation,
.sa_flags = SA_SIGINFO | SA_NODEFER };

if (sigaction(SIGSYS, &sa, NULL)) {
printf("Failed to configure SIGSYS handler [%s]\n",
strerror(errno));
}
}

int pg_init_seccomp(void)
{
init_error_handling();

struct sock_filter filter[] = {
VALIDATE_ARCHITECTURE,
EXAMINE_SYSCALL,
Expand Down Expand Up @@ -81,17 +111,31 @@ int pg_init_seccomp(void)
ALLOW_SYSCALL(gettimeofday),
ALLOW_SYSCALL(stat),
ALLOW_SYSCALL(clock_gettime),
ALLOW_SYSCALL(rt_sigreturn),
ALLOW_SYSCALL(epoll_create),
ALLOW_SYSCALL(epoll_ctl),
ALLOW_SYSCALL(epoll_wait),
ALLOW_SYSCALL(getsockopt),
ALLOW_SYSCALL(setsockopt),
ALLOW_SYSCALL(readlink),
ALLOW_SYSCALL(prlimit64),
ALLOW_SYSCALL(memfd_create),
ALLOW_SYSCALL(timerfd_create),
ALLOW_SYSCALL(uname),
ALLOW_SYSCALL(iopl),

KILL_PROCESS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be configurable, so take a bitmask in function parameter, and use that parameter here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is already defined in seccomp-bpf

define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but filter contain TRAP_PROCESS
Which doesn't work, if you want packetgraph to KILL process.
You can't put KILL_PROCESS into filter either, because that would mean packetgraph doesn't work if you want it to print process.
So you need to have a way to chose at runtime between KILL or TRAP.

TRAP_PROCESS,
};
struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter) / sizeof(*filter)),
.filter = filter,
};

if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
return -1;
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1) {
fprintf(stderr, "unknown PR_GET_SECCOMP error: %s\n",
strerror(errno));
return -1;
}
return 0;
}