2019-08-11 20:10:38 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/capability.h>
|
2022-03-27 19:59:52 +02:00
|
|
|
#include <exile.hpp>
|
2019-08-11 20:10:38 +02:00
|
|
|
#include "../logger.h"
|
|
|
|
#include "../utils.h"
|
|
|
|
#include "../random.h"
|
|
|
|
|
|
|
|
#include "sandbox-linux.h"
|
|
|
|
|
|
|
|
bool SandboxLinux::supported()
|
|
|
|
{
|
2019-08-12 09:06:32 +02:00
|
|
|
std::fstream stream;
|
|
|
|
stream.open("/proc/sys/kernel/unprivileged_userns_clone");
|
|
|
|
if(stream.is_open())
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
stream >> str;
|
|
|
|
if(str[0] == '0')
|
|
|
|
{
|
|
|
|
Logger::error() << "Please write '1' to /proc/sys/kernel/unprivileged_userns_clone in order to enable "
|
|
|
|
"sandboxing support on this system";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 20:10:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-03 23:46:40 +02:00
|
|
|
bool SandboxLinux::enable(std::vector<std::string> fsPaths)
|
2019-08-11 20:10:38 +02:00
|
|
|
{
|
2021-10-03 23:46:40 +02:00
|
|
|
std::sort(fsPaths.begin(), fsPaths.end(),
|
|
|
|
[](const std::string &a, const std::string &b) { return a.length() < b.length(); });
|
|
|
|
|
2021-12-02 10:15:11 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-09-23 12:44:35 +02:00
|
|
|
if(policy == NULL)
|
|
|
|
{
|
|
|
|
Logger::error() << "Failed to init sandboxing policy (worker) ";
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-03 23:46:40 +02:00
|
|
|
for(unsigned int i = 0; i < fsPaths.size(); i++)
|
|
|
|
{
|
2022-03-27 19:59:52 +02:00
|
|
|
exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ | EXILE_FS_ALLOW_ALL_WRITE, fsPaths[i].c_str());
|
2021-10-03 23:46:40 +02:00
|
|
|
}
|
2021-09-23 12:44:35 +02:00
|
|
|
policy->drop_caps = 1;
|
|
|
|
policy->not_dumpable = 1;
|
|
|
|
policy->no_new_privs = 1;
|
2021-10-03 23:46:40 +02:00
|
|
|
policy->mount_path_policies_to_chroot = 1;
|
2021-12-29 11:13:47 +01:00
|
|
|
policy->vow_promises = EXILE_SYSCALL_VOW_STDIO | EXILE_SYSCALL_VOW_WPATH | EXILE_SYSCALL_VOW_CPATH |
|
2022-03-27 19:59:52 +02:00
|
|
|
EXILE_SYSCALL_VOW_RPATH | EXILE_SYSCALL_VOW_INET | EXILE_SYSCALL_VOW_UNIX |
|
|
|
|
EXILE_SYSCALL_VOW_THREAD;
|
2021-09-23 12:44:35 +02:00
|
|
|
|
2021-12-02 10:15:11 +01:00
|
|
|
if(exile_enable_policy(policy) != 0)
|
2019-08-11 20:10:38 +02:00
|
|
|
{
|
2022-03-27 19:59:52 +02:00
|
|
|
Logger::error() << "Sandbox: Activation of exile failed!";
|
2021-12-02 10:15:11 +01:00
|
|
|
exile_free_policy(policy);
|
2019-08-11 20:10:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-02 10:15:11 +01:00
|
|
|
exile_free_policy(policy);
|
2019-08-11 20:10:38 +02:00
|
|
|
return true;
|
|
|
|
}
|