-
Notifications
You must be signed in to change notification settings - Fork 2
/
seccomp.c
74 lines (68 loc) · 2.11 KB
/
seccomp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* seccomp.c: Helper functions for interacting with seccomp
* Copyright (C) 2018 Canonical LTD.
* Author: Tyler Hicks <tyhicks@canonical.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#define _GNU_SOURCE
#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
/* stddef.h must come before linux/signal.h to fix a build failure on Ubuntu
* 16.04 LTS
*/
#include <stddef.h>
#include <linux/signal.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/syscall.h>
#include <unistd.h>
/* seccomp(2) wrapper
*
* See the seccomp(2) man page for details.
*/
static int seccomp(unsigned int operation, unsigned int flags, void *args)
{
return syscall(SYS_seccomp, operation, flags, args);
}
/* Loads a permissive seccomp filter with the specificied filter flags
*
* Returns 0 on success. -1 on error.
*/
int load_seccomp_filter(unsigned int flags)
{
struct sock_filter filter[] = {
BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
offsetof(struct seccomp_data, nr)),
BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
};
struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
.filter = filter,
};
int rc;
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
fprintf(stderr, "ERROR: Couldn't set no new privs: %m\n");
return -1;
}
if (seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog) < 0) {
fprintf(stderr, "ERROR: Couldn't load the seccomp filter: %m\n");
return -1;
}
return 0;
}