forked from RishavT/nsjail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.c
100 lines (84 loc) · 2.45 KB
/
sandbox.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
nsjail - seccomp-bpf sandboxing
-----------------------------------------
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "sandbox.h"
#include <errno.h>
#include <sys/prctl.h>
/* TBREMOVED */
#include <signal.h>
#include <unistd.h>
#include "common.h"
#include "log.h"
#include "seccomp/bpf-helper.h"
/*
* A demo policy, it disallows syslog and ptrace syscalls, both in 32 and 64
* modes
*/
static bool sandboxPrepareAndCommit(void)
{
#if defined(__x86_64__) || defined(__i386__)
struct bpf_labels l = {.count = 0 };
struct sock_filter filter[] = {
LOAD_ARCH,
JEQ32(AUDIT_ARCH_I386, JUMP(&l, label_i386)),
JEQ32(AUDIT_ARCH_X86_64, JUMP(&l, label_x86_64)),
/* I386 */
LABEL(&l, label_i386),
LOAD_SYSCALL_NR,
#define __NR_syslog_32 103
#define __NR_uselib_32 86
JEQ32(__NR_syslog_32, ERRNO(ENOENT)),
JEQ32(__NR_uselib_32, KILL),
ALLOW,
/* X86_64 */
LABEL(&l, label_x86_64),
LOAD_SYSCALL_NR,
#define __NR_syslog_64 103
#define __NR_uselib_64 134
JEQ32(__NR_syslog_64, ERRNO(ENOENT)),
JEQ32(__NR_uselib_64, KILL),
ALLOW,
};
struct sock_fprog prog = {
.filter = filter,
.len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
};
if (bpf_resolve_jumps(&l, filter, sizeof(filter) / sizeof(*filter)) != 0) {
LOG_W("bpf_resolve_jumps() failed");
return false;
}
#ifndef PR_SET_NO_NEW_PRIVS
#define PR_SET_NO_NEW_PRIVS 38
#endif /* PR_SET_NO_NEW_PRIVS */
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
PLOG_W("prctl(PR_SET_NO_NEW_PRIVS, 1) failed");
return false;
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0)) {
PLOG_W("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER) failed");
return false;
}
#endif /* defined(__x86_64__) || defined(__i386__) */
return true;
}
bool sandboxApply(struct nsjconf_t * nsjconf)
{
if (nsjconf->apply_sandbox == false) {
return true;
}
if (sandboxPrepareAndCommit() == false) {
return false;
}
return true;
}