nsjail/sandbox.c

106 lines
2.7 KiB
C
Raw Normal View History

2015-05-15 05:44:48 +08:00
/*
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
*/
2016-09-13 18:10:15 +08:00
static bool sandboxPrepareAndCommit(struct nsjconf_t *nsjconf)
2015-05-15 05:44:48 +08:00
{
2016-04-25 21:49:26 +08:00
#if defined(__x86_64__) || defined(__i386__)
2016-09-13 18:10:15 +08:00
if (nsjconf->seccomp_fprog.filter == NULL) {
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,
2015-05-15 05:44:48 +08:00
#define __NR_syslog_32 103
#define __NR_uselib_32 86
2016-09-13 18:10:15 +08:00
JEQ32(__NR_syslog_32, ERRNO(ENOENT)),
JEQ32(__NR_uselib_32, KILL),
ALLOW,
2015-05-15 05:44:48 +08:00
2016-09-13 18:10:15 +08:00
/* X86_64 */
LABEL(&l, label_x86_64),
LOAD_SYSCALL_NR,
2015-05-15 05:44:48 +08:00
#define __NR_syslog_64 103
#define __NR_uselib_64 134
2016-09-13 18:10:15 +08:00
JEQ32(__NR_syslog_64, ERRNO(ENOENT)),
JEQ32(__NR_uselib_64, KILL),
ALLOW,
};
/* *INDENT-OFF* */
nsjconf->seccomp_fprog = (struct sock_fprog) {
.filter = filter,
.len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
};
/* *INDENT-ON* */
if (bpf_resolve_jumps(&l, filter, sizeof(filter) / sizeof(*filter)) != 0) {
LOG_W("bpf_resolve_jumps() failed");
return false;
}
2015-05-15 05:44:48 +08:00
}
2016-09-13 18:10:15 +08:00
#endif /* defined(__x86_64__) || defined(__i386__) */
if (nsjconf->seccomp_fprog.filter != NULL) {
#ifndef PR_SET_NO_NEW_PRIVS
#define PR_SET_NO_NEW_PRIVS 38
#endif /* PR_SET_NO_NEW_PRIVS */
2016-09-13 18:10:15 +08:00
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, &nsjconf->seccomp_fprog, 0, 0)) {
PLOG_W("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER) failed");
return false;
}
2015-05-15 05:44:48 +08:00
}
return true;
}
bool sandboxApply(struct nsjconf_t * nsjconf)
{
if (nsjconf->apply_sandbox == false) {
return true;
}
2016-09-13 18:10:15 +08:00
if (sandboxPrepareAndCommit(nsjconf) == false) {
2015-05-15 05:44:48 +08:00
return false;
}
return true;
}