nsjail/sandbox.cc

96 lines
2.6 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"
2016-10-12 09:15:33 +08:00
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stddef.h>
2015-05-15 05:44:48 +08:00
#include <sys/prctl.h>
2018-02-10 00:16:41 +08:00
extern "C" {
2016-10-12 09:15:33 +08:00
#include "kafel.h"
2018-02-10 00:16:41 +08:00
}
#include "logs.h"
2018-02-10 00:16:41 +08:00
namespace sandbox {
2015-05-15 05:44:48 +08:00
2017-10-09 05:00:45 +08:00
#ifndef PR_SET_NO_NEW_PRIVS /* in prctl.h since Linux 3.5 */
2016-10-12 09:15:33 +08:00
#define PR_SET_NO_NEW_PRIVS 38
2017-10-09 05:00:45 +08:00
#endif /* PR_SET_NO_NEW_PRIVS */
2016-10-12 09:15:33 +08:00
2018-02-10 22:50:12 +08:00
static bool prepareAndCommit(nsjconf_t* nsjconf) {
if (nsjconf->kafel_file_path.empty() && nsjconf->kafel_string.empty()) {
2016-10-12 09:15:33 +08:00
return true;
}
2015-05-15 05:44:48 +08:00
2016-10-12 09:15:33 +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;
2015-05-15 05:44:48 +08:00
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &nsjconf->seccomp_fprog, 0, 0)) {
2016-10-12 09:15:33 +08:00
PLOG_W("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER) failed");
return false;
2015-05-15 05:44:48 +08:00
}
return true;
}
2018-02-11 03:32:04 +08:00
bool applyPolicy(nsjconf_t* nsjconf) {
return prepareAndCommit(nsjconf);
}
2018-02-10 22:50:12 +08:00
bool preparePolicy(nsjconf_t* nsjconf) {
if (nsjconf->kafel_file_path.empty() && nsjconf->kafel_string.empty()) {
return true;
}
FILE* f = NULL;
if (!nsjconf->kafel_file_path.empty() &&
!(f = fopen(nsjconf->kafel_file_path.c_str(), "r"))) {
PLOG_W("Couldn't open the kafel seccomp policy file '%s'",
nsjconf->kafel_file_path.c_str());
return false;
}
kafel_ctxt_t ctxt = kafel_ctxt_create();
if (f) {
2018-02-11 06:54:36 +08:00
LOG_D("Compiling seccomp policy from file: '%s'", nsjconf->kafel_file_path.c_str());
kafel_set_input_file(ctxt, f);
} else if (!nsjconf->kafel_string.empty()) {
2018-02-11 06:54:36 +08:00
LOG_D("Compiling seccomp policy from string: '%s'", nsjconf->kafel_string.c_str());
kafel_set_input_string(ctxt, nsjconf->kafel_string.c_str());
} else {
LOG_F(
"No kafel seccomp-bpf config file available, nor policy as a string was "
"defined");
}
if (kafel_compile(ctxt, &nsjconf->seccomp_fprog) != 0) {
LOG_E("Could not compile policy: %s", kafel_error_msg(ctxt));
kafel_ctxt_destroy(&ctxt);
return false;
}
kafel_ctxt_destroy(&ctxt);
return true;
}
2018-02-10 00:16:41 +08:00
} // namespace sandbox