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>
|
2017-10-18 20:46:17 +08:00
|
|
|
#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
|
|
|
}
|
2018-02-11 00:49:15 +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) {
|
2018-02-11 06:46:15 +08:00
|
|
|
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
|
|
|
}
|
2018-02-01 21:19:01 +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-01-31 23:04:39 +08:00
|
|
|
|
2018-02-10 22:50:12 +08:00
|
|
|
bool preparePolicy(nsjconf_t* nsjconf) {
|
2018-02-11 06:46:15 +08:00
|
|
|
if (nsjconf->kafel_file_path.empty() && nsjconf->kafel_string.empty()) {
|
2018-01-31 23:04:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-12 10:05:21 +08:00
|
|
|
if (!nsjconf->kafel_file_path.empty() && !nsjconf->kafel_string.empty()) {
|
2018-02-12 10:06:28 +08:00
|
|
|
LOG_W(
|
2018-02-12 10:05:21 +08:00
|
|
|
"You specified both kafel seccomp policy, and kafel seccomp file. Specify one "
|
|
|
|
"only");
|
2018-01-31 23:04:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-01 21:19:01 +08:00
|
|
|
|
|
|
|
kafel_ctxt_t ctxt = kafel_ctxt_create();
|
|
|
|
|
2018-02-12 10:05:21 +08:00
|
|
|
if (!nsjconf->kafel_file_path.empty()) {
|
|
|
|
FILE* f = fopen(nsjconf->kafel_file_path.c_str(), "r");
|
|
|
|
if (!f) {
|
|
|
|
PLOG_W("Couldn't open the kafel seccomp policy file '%s'",
|
|
|
|
nsjconf->kafel_file_path.c_str());
|
|
|
|
kafel_ctxt_destroy(&ctxt);
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-11 06:54:36 +08:00
|
|
|
LOG_D("Compiling seccomp policy from file: '%s'", nsjconf->kafel_file_path.c_str());
|
2018-02-01 21:19:01 +08:00
|
|
|
kafel_set_input_file(ctxt, f);
|
2018-02-12 10:05:21 +08:00
|
|
|
}
|
|
|
|
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());
|
2018-02-11 06:46:15 +08:00
|
|
|
kafel_set_input_string(ctxt, nsjconf->kafel_string.c_str());
|
2018-02-01 21:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kafel_compile(ctxt, &nsjconf->seccomp_fprog) != 0) {
|
2018-02-12 10:06:28 +08:00
|
|
|
LOG_W("Could not compile policy: %s", kafel_error_msg(ctxt));
|
2018-02-01 21:19:01 +08:00
|
|
|
kafel_ctxt_destroy(&ctxt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
kafel_ctxt_destroy(&ctxt);
|
2018-01-31 23:04:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-10 00:16:41 +08:00
|
|
|
|
|
|
|
} // namespace sandbox
|