OLD | NEW |
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 <signal.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
18 #include "base/environment.h" | 18 #include "base/environment.h" |
19 #include "base/files/scoped_file.h" | 19 #include "base/files/scoped_file.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/macros.h" | 21 #include "base/macros.h" |
22 #include "base/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
23 #include "base/process/launch.h" | 23 #include "base/process/launch.h" |
24 #include "base/process/process.h" | 24 #include "base/process/process.h" |
25 #include "sandbox/linux/services/credentials.h" | 25 #include "sandbox/linux/services/credentials.h" |
26 #include "sandbox/linux/services/namespace_utils.h" | 26 #include "sandbox/linux/services/namespace_utils.h" |
| 27 #include "sandbox/linux/system_headers/linux_signal.h" |
27 | 28 |
28 namespace sandbox { | 29 namespace sandbox { |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS"; | 33 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS"; |
33 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS"; | 34 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS"; |
34 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS"; | 35 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS"; |
35 | 36 |
36 #if !defined(OS_NACL_NONSFI) | 37 #if !defined(OS_NACL_NONSFI) |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 for (const auto& entry : clone_flag_environ) { | 125 for (const auto& entry : clone_flag_environ) { |
125 const int flag = entry.first; | 126 const int flag = entry.first; |
126 const char* environ_name = entry.second; | 127 const char* environ_name = entry.second; |
127 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag); | 128 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag); |
128 } | 129 } |
129 | 130 |
130 return base::LaunchProcess(argv, launch_options); | 131 return base::LaunchProcess(argv, launch_options); |
131 } | 132 } |
132 | 133 |
133 // static | 134 // 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() { | 135 void NamespaceSandbox::InstallDefaultTerminationSignalHandlers() { |
155 static const int kDefaultTermSignals[] = { | 136 static const int kDefaultTermSignals[] = { |
156 SIGHUP, SIGINT, SIGABRT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2, | 137 SIGHUP, SIGINT, SIGABRT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2, |
157 }; | 138 }; |
158 | 139 |
159 for (const int sig : kDefaultTermSignals) { | 140 for (const int sig : kDefaultTermSignals) { |
160 InstallTerminationSignalHandler(sig, kDefaultExitCode); | 141 InstallTerminationSignalHandler(sig, kDefaultExitCode); |
161 } | 142 } |
162 } | 143 } |
163 | 144 |
(...skipping 20 matching lines...) Expand all Loading... |
184 g_signal_exit_codes[sig_idx] = exit_code; | 165 g_signal_exit_codes[sig_idx] = exit_code; |
185 | 166 |
186 struct sigaction action = {}; | 167 struct sigaction action = {}; |
187 action.sa_handler = &TerminationSignalHandler; | 168 action.sa_handler = &TerminationSignalHandler; |
188 PCHECK(sigaction(sig, &action, nullptr) == 0); | 169 PCHECK(sigaction(sig, &action, nullptr) == 0); |
189 return true; | 170 return true; |
190 } | 171 } |
191 #endif // !defined(OS_NACL_NONSFI) | 172 #endif // !defined(OS_NACL_NONSFI) |
192 | 173 |
193 // static | 174 // static |
| 175 pid_t NamespaceSandbox::ForkInNewPidNamespace(bool drop_capabilities_in_child) { |
| 176 const pid_t pid = |
| 177 base::ForkWithFlags(CLONE_NEWPID | LINUX_SIGCHLD, nullptr, nullptr); |
| 178 if (pid < 0) { |
| 179 return pid; |
| 180 } |
| 181 |
| 182 if (pid == 0) { |
| 183 DCHECK_EQ(1, getpid()); |
| 184 if (drop_capabilities_in_child) { |
| 185 // Since we just forked, we are single-threaded, so this should be safe. |
| 186 CHECK(Credentials::DropAllCapabilitiesOnCurrentThread()); |
| 187 } |
| 188 return 0; |
| 189 } |
| 190 |
| 191 return pid; |
| 192 } |
| 193 |
| 194 // static |
194 bool NamespaceSandbox::InNewUserNamespace() { | 195 bool NamespaceSandbox::InNewUserNamespace() { |
195 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr; | 196 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr; |
196 } | 197 } |
197 | 198 |
198 // static | 199 // static |
199 bool NamespaceSandbox::InNewPidNamespace() { | 200 bool NamespaceSandbox::InNewPidNamespace() { |
200 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr; | 201 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr; |
201 } | 202 } |
202 | 203 |
203 // static | 204 // static |
204 bool NamespaceSandbox::InNewNetNamespace() { | 205 bool NamespaceSandbox::InNewNetNamespace() { |
205 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr; | 206 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr; |
206 } | 207 } |
207 | 208 |
208 } // namespace sandbox | 209 } // namespace sandbox |
OLD | NEW |