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

Side by Side 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: Bunch of cleanups after the uid/gid map writing move Created 5 years, 10 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "sandbox/linux/services/namespace_sandbox.h"
6
7 #include <sched.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include <string>
13 #include <utility>
14
15 #include "base/command_line.h"
16 #include "base/environment.h"
17 #include "base/files/scoped_file.h"
18 #include "base/logging.h"
19 #include "base/posix/eintr_wrapper.h"
20 #include "base/process/launch.h"
21 #include "base/process/process.h"
22 #include "sandbox/linux/services/namespace_utils.h"
23
24 namespace sandbox {
25
26 namespace {
27
28 class WriteUidGidMapDelegate : public base::LaunchOptions::PreExecDelegate {
29 public:
30 explicit WriteUidGidMapDelegate() : uid_(getuid()), gid_(getgid()) {}
31
32 ~WriteUidGidMapDelegate() override {}
33
34 void RunAsyncSafe() override {
35 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid_));
36 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid_));
37 }
38
39 private:
40 uid_t uid_;
41 gid_t gid_;
42 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate);
43 };
44
45 void SetEnvironForNamespaceType(base::EnvironmentMap* environ,
46 base::NativeEnvironmentString env_var,
47 bool value) {
48 // An empty string causes the env var to be unset in the child process.
49 (*environ)[env_var] = value ? "1" : "";
50 }
51
52 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS";
53 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS";
54 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS";
55
56 } // namespace
57
58 // static
59 base::Process NamespaceSandbox::Launch(const base::CommandLine& cmdline,
60 const base::LaunchOptions& options) {
61 int clone_flags = 0;
62 int ns_types[] = {CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET};
63 for (const int ns_type : ns_types) {
64 if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type)) {
65 clone_flags |= ns_type;
66 }
67 }
68 CHECK(clone_flags & CLONE_NEWUSER);
69
70 // These fields may not be set by the caller.
71 CHECK(options.pre_exec_delegate == nullptr);
72 CHECK_EQ(0, options.clone_flags);
73
74 WriteUidGidMapDelegate write_uid_gid_map_delegate;
75
76 base::LaunchOptions launch_options = options;
77 launch_options.pre_exec_delegate = &write_uid_gid_map_delegate;
78 launch_options.clone_flags = clone_flags;
79
80 const std::pair<int, const char*> clone_flag_environ[] = {
81 std::make_pair(CLONE_NEWUSER, kSandboxUSERNSEnvironmentVarName),
82 std::make_pair(CLONE_NEWPID, kSandboxPIDNSEnvironmentVarName),
83 std::make_pair(CLONE_NEWNET, kSandboxNETNSEnvironmentVarName),
84 };
85
86 base::EnvironmentMap* environ = &launch_options.environ;
87 for (const auto& entry : clone_flag_environ) {
88 const int flag = entry.first;
89 const char* environ_name = entry.second;
90 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag);
91 }
92
93 return base::LaunchProcess(cmdline, launch_options);
94 }
95
96 // static
97 bool NamespaceSandbox::InNewUserNamespace() {
98 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr;
99 }
100
101 // static
102 bool NamespaceSandbox::InNewPidNamespace() {
103 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr;
104 }
105
106 // static
107 bool NamespaceSandbox::InNewNetNamespace() {
108 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr;
109 }
110
111 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698