This source file includes following definitions.
- LogSandboxStarted
- AddResourceLimit
- IsRunningTSAN
- OpenProcTaskFd
- setuid_sandbox_client_
- GetInstance
- PreinitializeSandbox
- InitializeSandbox
- StopThread
- GetStatus
- IsSingleThreaded
- seccomp_bpf_started
- setuid_sandbox_client
- StartSeccompBPF
- InitializeSandboxImpl
- StopThreadImpl
- seccomp_bpf_supported
- LimitAddressSpace
- HasOpenDirectories
- SealSandbox
- CheckForBrokenPromises
- StopThreadAndEnsureNotCounted
#include <dirent.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/debug/stack_trace.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/common/sandbox_linux/sandbox_linux.h"
#include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/sandbox_linux.h"
#include "sandbox/linux/services/credentials.h"
#include "sandbox/linux/services/thread_helpers.h"
#include "sandbox/linux/services/yama.h"
#include "sandbox/linux/suid/client/setuid_sandbox_client.h"
using sandbox::Yama;
namespace {
struct FDCloser {
inline void operator()(int* fd) const {
DCHECK(fd);
PCHECK(0 == IGNORE_EINTR(close(*fd)));
*fd = -1;
}
};
void LogSandboxStarted(const std::string& sandbox_name) {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
const std::string process_type =
command_line.GetSwitchValueASCII(switches::kProcessType);
const std::string activated_sandbox =
"Activated " + sandbox_name + " sandbox for process type: " +
process_type + ".";
#if defined(OS_CHROMEOS)
LOG(WARNING) << activated_sandbox;
#else
VLOG(1) << activated_sandbox;
#endif
}
bool AddResourceLimit(int resource, rlim_t limit) {
struct rlimit old_rlimit;
if (getrlimit(resource, &old_rlimit))
return false;
const struct rlimit new_rlimit = {
std::min(old_rlimit.rlim_cur, limit),
std::min(old_rlimit.rlim_max, limit)
};
int rc = setrlimit(resource, &new_rlimit);
return rc == 0;
}
bool IsRunningTSAN() {
#if defined(THREAD_SANITIZER)
return true;
#else
return false;
#endif
}
int OpenProcTaskFd(int proc_fd) {
int proc_self_task = -1;
if (proc_fd >= 0) {
proc_self_task = openat(proc_fd, "self/task/", O_RDONLY | O_DIRECTORY);
} else {
proc_self_task = open("/proc/self/task/", O_RDONLY | O_DIRECTORY);
}
return proc_self_task;
}
}
namespace content {
LinuxSandbox::LinuxSandbox()
: proc_fd_(-1),
seccomp_bpf_started_(false),
sandbox_status_flags_(kSandboxLinuxInvalid),
pre_initialized_(false),
seccomp_bpf_supported_(false),
yama_is_enforcing_(false),
setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) {
if (setuid_sandbox_client_ == NULL) {
LOG(FATAL) << "Failed to instantiate the setuid sandbox client.";
}
}
LinuxSandbox::~LinuxSandbox() {
}
LinuxSandbox* LinuxSandbox::GetInstance() {
LinuxSandbox* instance = Singleton<LinuxSandbox>::get();
CHECK(instance);
return instance;
}
#if defined(ADDRESS_SANITIZER) && defined(OS_LINUX)
extern "C" void __sanitizer_sandbox_on_notify(void *reserved);
#endif
void LinuxSandbox::PreinitializeSandbox() {
CHECK(!pre_initialized_);
seccomp_bpf_supported_ = false;
#if defined(ADDRESS_SANITIZER) && defined(OS_LINUX)
__sanitizer_sandbox_on_notify( NULL);
#endif
#if !defined(NDEBUG)
base::debug::EnableInProcessStackDumpingForSandbox();
proc_fd_ = open("/proc", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
CHECK_GE(proc_fd_, 0);
#endif
if (SandboxSeccompBPF::IsSeccompBPFDesired()) {
if (!SandboxSeccompBPF::SupportsSandbox()) {
VLOG(1) << "Lacking support for seccomp-bpf sandbox.";
} else {
seccomp_bpf_supported_ = true;
}
}
const int yama_status = Yama::GetStatus();
yama_is_enforcing_ = (yama_status & Yama::STATUS_PRESENT) &&
(yama_status & Yama::STATUS_ENFORCING);
pre_initialized_ = true;
}
bool LinuxSandbox::InitializeSandbox() {
LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
return linux_sandbox->InitializeSandboxImpl();
}
void LinuxSandbox::StopThread(base::Thread* thread) {
LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
linux_sandbox->StopThreadImpl(thread);
}
int LinuxSandbox::GetStatus() {
CHECK(pre_initialized_);
if (kSandboxLinuxInvalid == sandbox_status_flags_) {
sandbox_status_flags_ = 0;
if (setuid_sandbox_client_->IsSandboxed()) {
sandbox_status_flags_ |= kSandboxLinuxSUID;
if (setuid_sandbox_client_->IsInNewPIDNamespace())
sandbox_status_flags_ |= kSandboxLinuxPIDNS;
if (setuid_sandbox_client_->IsInNewNETNamespace())
sandbox_status_flags_ |= kSandboxLinuxNetNS;
}
if (seccomp_bpf_supported() &&
SandboxSeccompBPF::ShouldEnableSeccompBPF(switches::kRendererProcess)) {
sandbox_status_flags_ |= kSandboxLinuxSeccompBPF;
}
if (yama_is_enforcing_) {
sandbox_status_flags_ |= kSandboxLinuxYama;
}
}
return sandbox_status_flags_;
}
bool LinuxSandbox::IsSingleThreaded() const {
bool is_single_threaded = false;
base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_));
#if !defined(NDEBUG)
CHECK(proc_self_task.is_valid())
<< "Could not count threads, the sandbox was not "
<< "pre-initialized properly.";
#endif
if (!proc_self_task.is_valid()) {
is_single_threaded = true;
} else {
is_single_threaded =
sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task.get());
}
return is_single_threaded;
}
bool LinuxSandbox::seccomp_bpf_started() const {
return seccomp_bpf_started_;
}
sandbox::SetuidSandboxClient*
LinuxSandbox::setuid_sandbox_client() const {
return setuid_sandbox_client_.get();
}
bool LinuxSandbox::StartSeccompBPF(const std::string& process_type) {
CHECK(!seccomp_bpf_started_);
CHECK(pre_initialized_);
if (seccomp_bpf_supported())
seccomp_bpf_started_ = SandboxSeccompBPF::StartSandbox(process_type);
if (seccomp_bpf_started_)
LogSandboxStarted("seccomp-bpf");
return seccomp_bpf_started_;
}
bool LinuxSandbox::InitializeSandboxImpl() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
const std::string process_type =
command_line->GetSwitchValueASCII(switches::kProcessType);
base::ScopedClosureRunner sandbox_sealer(
base::Bind(&LinuxSandbox::SealSandbox, base::Unretained(this)));
base::ScopedClosureRunner sandbox_promise_keeper(
base::Bind(&LinuxSandbox::CheckForBrokenPromises,
base::Unretained(this),
process_type));
if (!IsSingleThreaded()) {
std::string error_message = "InitializeSandbox() called with multiple "
"threads in process " + process_type;
if (IsRunningTSAN())
return false;
bool sandbox_failure_fatal = process_type != switches::kGpuProcess;
if (process_type == switches::kGpuProcess &&
command_line->HasSwitch(switches::kGpuSandboxFailuresFatal)) {
const std::string switch_value =
command_line->GetSwitchValueASCII(switches::kGpuSandboxFailuresFatal);
sandbox_failure_fatal = switch_value != "no";
}
if (sandbox_failure_fatal)
LOG(FATAL) << error_message;
LOG(ERROR) << error_message;
return false;
}
if (!pre_initialized_)
PreinitializeSandbox();
DCHECK(!HasOpenDirectories()) <<
"InitializeSandbox() called after unexpected directories have been " <<
"opened. This breaks the security of the setuid sandbox.";
LimitAddressSpace(process_type);
bool seccomp_bpf_started = StartSeccompBPF(process_type);
return seccomp_bpf_started;
}
void LinuxSandbox::StopThreadImpl(base::Thread* thread) {
DCHECK(thread);
StopThreadAndEnsureNotCounted(thread);
}
bool LinuxSandbox::seccomp_bpf_supported() const {
CHECK(pre_initialized_);
return seccomp_bpf_supported_;
}
bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) {
(void) process_type;
#if !defined(ADDRESS_SANITIZER)
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kNoSandbox)) {
return false;
}
rlim_t address_space_limit = std::numeric_limits<uint32_t>::max();
#if defined(__LP64__)
if (process_type == switches::kRendererProcess ||
process_type == switches::kWorkerProcess ||
process_type == switches::kGpuProcess) {
address_space_limit = 1L << 34;
}
#endif
const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max();
bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit);
bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize);
return limited_as && limited_data;
#else
return false;
#endif
}
bool LinuxSandbox::HasOpenDirectories() const {
return sandbox::Credentials().HasOpenDirectory(proc_fd_);
}
void LinuxSandbox::SealSandbox() {
if (proc_fd_ >= 0) {
int ret = IGNORE_EINTR(close(proc_fd_));
CHECK_EQ(0, ret);
proc_fd_ = -1;
}
}
void LinuxSandbox::CheckForBrokenPromises(const std::string& process_type) {
bool promised_seccomp_bpf_would_start = false;
if (process_type == switches::kRendererProcess ||
process_type == switches::kWorkerProcess ||
process_type == switches::kPpapiPluginProcess) {
promised_seccomp_bpf_would_start =
(sandbox_status_flags_ != kSandboxLinuxInvalid) &&
(GetStatus() & kSandboxLinuxSeccompBPF);
}
if (promised_seccomp_bpf_would_start) {
CHECK(seccomp_bpf_started_);
}
}
void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const {
DCHECK(thread);
base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_));
PCHECK(proc_self_task.is_valid());
CHECK(sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task.get(),
thread));
}
}