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

Side by Side Diff: sandbox/linux/services/namespace_sandbox.cc

Issue 1108173002: Roll //build, //native_client, and a few more targets of opportunity. Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Test fix Created 5 years, 7 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sandbox/linux/services/namespace_sandbox.h" 5 #include "sandbox/linux/services/namespace_sandbox.h"
6 6
7 #include <sched.h> 7 #include <sched.h>
8 #include <signal.h>
8 #include <stdlib.h> 9 #include <stdlib.h>
9 #include <sys/types.h> 10 #include <sys/types.h>
10 #include <unistd.h> 11 #include <unistd.h>
11 12
12 #include <string> 13 #include <string>
13 #include <utility> 14 #include <utility>
14 #include <vector> 15 #include <vector>
15 16
16 #include "base/command_line.h" 17 #include "base/command_line.h"
17 #include "base/environment.h" 18 #include "base/environment.h"
18 #include "base/files/scoped_file.h" 19 #include "base/files/scoped_file.h"
19 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/macros.h"
20 #include "base/posix/eintr_wrapper.h" 22 #include "base/posix/eintr_wrapper.h"
21 #include "base/process/launch.h" 23 #include "base/process/launch.h"
22 #include "base/process/process.h" 24 #include "base/process/process.h"
25 #include "sandbox/linux/services/credentials.h"
23 #include "sandbox/linux/services/namespace_utils.h" 26 #include "sandbox/linux/services/namespace_utils.h"
24 27
25 namespace sandbox { 28 namespace sandbox {
26 29
27 namespace { 30 namespace {
28 31
32 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS";
33 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS";
34 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS";
35
36 #if !defined(OS_NACL_NONSFI)
29 class WriteUidGidMapDelegate : public base::LaunchOptions::PreExecDelegate { 37 class WriteUidGidMapDelegate : public base::LaunchOptions::PreExecDelegate {
30 public: 38 public:
31 WriteUidGidMapDelegate() 39 WriteUidGidMapDelegate()
32 : uid_(getuid()), 40 : uid_(getuid()),
33 gid_(getgid()), 41 gid_(getgid()),
34 supports_deny_setgroups_( 42 supports_deny_setgroups_(
35 NamespaceUtils::KernelSupportsDenySetgroups()) {} 43 NamespaceUtils::KernelSupportsDenySetgroups()) {}
36 44
37 ~WriteUidGidMapDelegate() override {} 45 ~WriteUidGidMapDelegate() override {}
38 46
(...skipping 12 matching lines...) Expand all
51 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate); 59 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate);
52 }; 60 };
53 61
54 void SetEnvironForNamespaceType(base::EnvironmentMap* environ, 62 void SetEnvironForNamespaceType(base::EnvironmentMap* environ,
55 base::NativeEnvironmentString env_var, 63 base::NativeEnvironmentString env_var,
56 bool value) { 64 bool value) {
57 // An empty string causes the env var to be unset in the child process. 65 // An empty string causes the env var to be unset in the child process.
58 (*environ)[env_var] = value ? "1" : ""; 66 (*environ)[env_var] = value ? "1" : "";
59 } 67 }
60 68
61 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS"; 69 // Linux supports up to 64 signals. This should be updated if that ever changes.
62 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS"; 70 int g_signal_exit_codes[64];
63 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS"; 71
72 void TerminationSignalHandler(int sig) {
73 // Return a special exit code so that the process is detected as terminated by
74 // a signal.
75 const size_t sig_idx = static_cast<size_t>(sig);
76 if (sig_idx < arraysize(g_signal_exit_codes)) {
77 _exit(g_signal_exit_codes[sig_idx]);
78 }
79
80 _exit(NamespaceSandbox::kDefaultExitCode);
81 }
82 #endif // !defined(OS_NACL_NONSFI)
64 83
65 } // namespace 84 } // namespace
66 85
86 #if !defined(OS_NACL_NONSFI)
67 // static 87 // static
68 base::Process NamespaceSandbox::LaunchProcess( 88 base::Process NamespaceSandbox::LaunchProcess(
69 const base::CommandLine& cmdline, 89 const base::CommandLine& cmdline,
70 const base::LaunchOptions& options) { 90 const base::LaunchOptions& options) {
71 return LaunchProcess(cmdline.argv(), options); 91 return LaunchProcess(cmdline.argv(), options);
72 } 92 }
73 93
74 // static 94 // static
75 base::Process NamespaceSandbox::LaunchProcess( 95 base::Process NamespaceSandbox::LaunchProcess(
76 const std::vector<std::string>& argv, 96 const std::vector<std::string>& argv,
(...skipping 27 matching lines...) Expand all
104 for (const auto& entry : clone_flag_environ) { 124 for (const auto& entry : clone_flag_environ) {
105 const int flag = entry.first; 125 const int flag = entry.first;
106 const char* environ_name = entry.second; 126 const char* environ_name = entry.second;
107 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag); 127 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag);
108 } 128 }
109 129
110 return base::LaunchProcess(argv, launch_options); 130 return base::LaunchProcess(argv, launch_options);
111 } 131 }
112 132
113 // static 133 // static
134 pid_t NamespaceSandbox::ForkInNewPidNamespace(bool drop_capabilities_in_child) {
135 const pid_t pid =
136 base::ForkWithFlags(CLONE_NEWPID | SIGCHLD, nullptr, nullptr);
137 if (pid < 0) {
138 return pid;
139 }
140
141 if (pid == 0) {
142 DCHECK_EQ(1, getpid());
143 if (drop_capabilities_in_child) {
144 // Since we just forked, we are single-threaded, so this should be safe.
145 CHECK(Credentials::DropAllCapabilitiesOnCurrentThread());
146 }
147 return 0;
148 }
149
150 return pid;
151 }
152
153 // static
154 void NamespaceSandbox::InstallDefaultTerminationSignalHandlers() {
155 static const int kDefaultTermSignals[] = {
156 SIGHUP, SIGINT, SIGABRT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2,
157 };
158
159 for (const int sig : kDefaultTermSignals) {
160 InstallTerminationSignalHandler(sig, kDefaultExitCode);
161 }
162 }
163
164 // static
165 bool NamespaceSandbox::InstallTerminationSignalHandler(
166 int sig,
167 int exit_code) {
168 struct sigaction old_action;
169 PCHECK(sigaction(sig, nullptr, &old_action) == 0);
170
171 if (old_action.sa_flags & SA_SIGINFO &&
172 old_action.sa_sigaction != nullptr) {
173 return false;
174 } else if (old_action.sa_handler != SIG_DFL) {
175 return false;
176 }
177
178 const size_t sig_idx = static_cast<size_t>(sig);
179 CHECK_LT(sig_idx, arraysize(g_signal_exit_codes));
180
181 DCHECK_GE(exit_code, 0);
182 DCHECK_LT(exit_code, 256);
183
184 g_signal_exit_codes[sig_idx] = exit_code;
185
186 struct sigaction action = {};
187 action.sa_handler = &TerminationSignalHandler;
188 PCHECK(sigaction(sig, &action, nullptr) == 0);
189 return true;
190 }
191 #endif // !defined(OS_NACL_NONSFI)
192
193 // static
114 bool NamespaceSandbox::InNewUserNamespace() { 194 bool NamespaceSandbox::InNewUserNamespace() {
115 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr; 195 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr;
116 } 196 }
117 197
118 // static 198 // static
119 bool NamespaceSandbox::InNewPidNamespace() { 199 bool NamespaceSandbox::InNewPidNamespace() {
120 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr; 200 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr;
121 } 201 }
122 202
123 // static 203 // static
124 bool NamespaceSandbox::InNewNetNamespace() { 204 bool NamespaceSandbox::InNewNetNamespace() {
125 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr; 205 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr;
126 } 206 }
127 207
128 } // namespace sandbox 208 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/services/namespace_sandbox.h ('k') | sandbox/linux/services/namespace_sandbox_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698