Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3299)

Unified Diff: sandbox/linux/services/namespace_sandbox.cc

Issue 881733002: Add namespace sandbox class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing comment, fix includes. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sandbox/linux/services/namespace_sandbox.cc
diff --git a/sandbox/linux/services/namespace_sandbox.cc b/sandbox/linux/services/namespace_sandbox.cc
new file mode 100644
index 0000000000000000000000000000000000000000..70d1f63f89d3e6eca243f119301cf1b5de19b28c
--- /dev/null
+++ b/sandbox/linux/services/namespace_sandbox.cc
@@ -0,0 +1,143 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sandbox/linux/services/namespace_sandbox.h"
+
+#include <sched.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <string>
+#include <utility>
+
+#include "base/bind.h"
+#include "base/command_line.h"
+#include "base/environment.h"
+#include "base/files/scoped_file.h"
+#include "base/logging.h"
+#include "base/posix/eintr_wrapper.h"
+#include "base/process/launch.h"
+#include "base/process/process.h"
+#include "base/strings/stringprintf.h"
+#include "sandbox/linux/services/namespace_utils.h"
+
+namespace sandbox {
+
+namespace {
+const char kPipeValue = '\xcc';
+
+class BlockOnPipeDelegate : public base::LaunchOptions::PreExecDelegate {
+ public:
+ explicit BlockOnPipeDelegate(int fd) : fd_(fd) {}
+
+ ~BlockOnPipeDelegate() override {}
+
+ void RunAsyncSafe() override {
+ char c;
+ RAW_CHECK(HANDLE_EINTR(read(fd_, &c, 1)) == 1);
+ RAW_CHECK(IGNORE_EINTR(close(fd_)) == 0);
+ RAW_CHECK(c == kPipeValue);
+ }
+
+ private:
+ int fd_;
+ DISALLOW_COPY_AND_ASSIGN(BlockOnPipeDelegate);
+};
+
+void SetEnvironForNamespaceType(base::EnvironmentMap* environ,
+ base::NativeEnvironmentString env_var,
+ bool value) {
+ // An empty string causes the env var to be unset in the child process.
+ (*environ)[env_var] = value ? "1" : "";
+}
+
+const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS";
+const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS";
+const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS";
+
+} // namespace
+
+NamespaceSandbox::NamespaceSandbox() : launch_called_(false) {
+}
+
+NamespaceSandbox::~NamespaceSandbox() {
+}
+
+void NamespaceSandbox::AddFdMapping(int from_fd, int to_fd) {
+ fds_to_remap_.push_back(std::make_pair(from_fd, to_fd));
+}
+
+base::Process NamespaceSandbox::Launch(const base::CommandLine& cmdline) {
+ CHECK(!launch_called_) << "NamespaceSandbox may only be used once.";
+ launch_called_ = true;
+
+ int clone_flags = 0;
+ int ns_types[] = {CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET};
+ for (const int ns_type : ns_types) {
+ if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type)) {
+ clone_flags |= ns_type;
+ }
+ }
+ CHECK(clone_flags & CLONE_NEWUSER);
+
+ int fds[2];
+ PCHECK(pipe(fds) == 0);
+ read_fd_.reset(fds[0]);
+ write_fd_.reset(fds[1]);
+ fds_to_remap_.push_back(std::make_pair(read_fd_.get(), read_fd_.get()));
+
+ BlockOnPipeDelegate block_on_pipe_delegate(read_fd_.get());
+ launch_options_.pre_exec_delegate = &block_on_pipe_delegate;
+ launch_options_.fds_to_remap = &fds_to_remap_;
+ launch_options_.clone_flags = clone_flags;
+
+ const std::pair<int, const char*> clone_flag_environ[] = {
+ std::make_pair(CLONE_NEWUSER, kSandboxUSERNSEnvironmentVarName),
+ std::make_pair(CLONE_NEWPID, kSandboxPIDNSEnvironmentVarName),
+ std::make_pair(CLONE_NEWNET, kSandboxNETNSEnvironmentVarName),
+ };
+
+ base::EnvironmentMap* environ = &launch_options_.environ;
+ for (const auto& entry : clone_flag_environ) {
+ const int flag = entry.first;
+ const char* environ_name = entry.second;
+ SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag);
+ }
+
+ base::Process process = base::LaunchProcess(cmdline, launch_options_);
+ read_fd_.reset();
+
+ if (!process.IsValid()) {
+ write_fd_.reset();
jln (very slow on Chromium) 2015/01/30 20:12:28 Having to "remember" to do that is problematic. r
rickyz (no longer on Chrome) 2015/01/31 02:33:53 Ah, that's much nicer, done
+ return process.Pass();
+ }
+
+ WriteUidGidMapAndUnblockProcess(process.Pid());
+ return process.Pass();
+}
+
+void NamespaceSandbox::WriteUidGidMapAndUnblockProcess(pid_t pid) {
+ const std::string uid_map_path = base::StringPrintf("/proc/%d/uid_map", pid);
+ const std::string gid_map_path = base::StringPrintf("/proc/%d/gid_map", pid);
+ NamespaceUtils::WriteToIdMapFile(uid_map_path.c_str(), getuid());
+ NamespaceUtils::WriteToIdMapFile(gid_map_path.c_str(), getgid());
+
+ PCHECK(HANDLE_EINTR(write(write_fd_.get(), &kPipeValue, 1)) == 1);
jln (very slow on Chromium) 2015/01/30 20:12:28 If the child process gets killed or disappears for
rickyz (no longer on Chrome) 2015/01/31 02:33:53 Done.
+ write_fd_.reset();
+}
+
+bool NamespaceSandbox::InNewUserNamespace() {
+ return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr;
+}
+
+bool NamespaceSandbox::InNewPidNamespace() {
+ return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr;
+}
+
+bool NamespaceSandbox::InNewNetNamespace() {
+ return getenv(kSandboxNETNSEnvironmentVarName) != nullptr;
+}
+
+} // namespace sandbox

Powered by Google App Engine
This is Rietveld 408576698