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.
5 #include "sandbox/linux/services/namespace_sandbox.h"
10 #include <sys/types.h>
17 #include "base/command_line.h"
18 #include "base/environment.h"
19 #include "base/files/scoped_file.h"
20 #include "base/logging.h"
21 #include "base/macros.h"
22 #include "base/posix/eintr_wrapper.h"
23 #include "base/process/launch.h"
24 #include "base/process/process.h"
25 #include "sandbox/linux/services/credentials.h"
26 #include "sandbox/linux/services/namespace_utils.h"
27 #include "sandbox/linux/services/syscall_wrappers.h"
28 #include "sandbox/linux/system_headers/linux_signal.h"
34 const char kSandboxUSERNSEnvironmentVarName
[] = "SBX_USER_NS";
35 const char kSandboxPIDNSEnvironmentVarName
[] = "SBX_PID_NS";
36 const char kSandboxNETNSEnvironmentVarName
[] = "SBX_NET_NS";
38 #if !defined(OS_NACL_NONSFI)
39 class WriteUidGidMapDelegate
: public base::LaunchOptions::PreExecDelegate
{
41 WriteUidGidMapDelegate()
44 supports_deny_setgroups_(
45 NamespaceUtils::KernelSupportsDenySetgroups()) {}
47 ~WriteUidGidMapDelegate() override
{}
49 void RunAsyncSafe() override
{
50 if (supports_deny_setgroups_
) {
51 RAW_CHECK(NamespaceUtils::DenySetgroups());
53 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid_
));
54 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid_
));
60 const bool supports_deny_setgroups_
;
61 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate
);
64 void SetEnvironForNamespaceType(base::EnvironmentMap
* environ
,
65 base::NativeEnvironmentString env_var
,
67 // An empty string causes the env var to be unset in the child process.
68 (*environ
)[env_var
] = value
? "1" : "";
70 #endif // !defined(OS_NACL_NONSFI)
72 // Linux supports up to 64 signals. This should be updated if that ever changes.
73 int g_signal_exit_codes
[64];
75 void TerminationSignalHandler(int sig
) {
76 // Return a special exit code so that the process is detected as terminated by
78 const size_t sig_idx
= static_cast<size_t>(sig
);
79 if (sig_idx
< arraysize(g_signal_exit_codes
)) {
80 _exit(g_signal_exit_codes
[sig_idx
]);
83 _exit(NamespaceSandbox::SignalExitCode(sig
));
88 #if !defined(OS_NACL_NONSFI)
90 base::Process
NamespaceSandbox::LaunchProcess(
91 const base::CommandLine
& cmdline
,
92 const base::LaunchOptions
& options
) {
93 return LaunchProcess(cmdline
.argv(), options
);
97 base::Process
NamespaceSandbox::LaunchProcess(
98 const std::vector
<std::string
>& argv
,
99 const base::LaunchOptions
& options
) {
101 int ns_types
[] = {CLONE_NEWUSER
, CLONE_NEWPID
, CLONE_NEWNET
};
102 for (const int ns_type
: ns_types
) {
103 if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type
)) {
104 clone_flags
|= ns_type
;
107 CHECK(clone_flags
& CLONE_NEWUSER
);
109 // These fields may not be set by the caller.
110 CHECK(options
.pre_exec_delegate
== nullptr);
111 CHECK_EQ(0, options
.clone_flags
);
113 WriteUidGidMapDelegate write_uid_gid_map_delegate
;
115 base::LaunchOptions launch_options
= options
;
116 launch_options
.pre_exec_delegate
= &write_uid_gid_map_delegate
;
117 launch_options
.clone_flags
= clone_flags
;
119 const std::pair
<int, const char*> clone_flag_environ
[] = {
120 std::make_pair(CLONE_NEWUSER
, kSandboxUSERNSEnvironmentVarName
),
121 std::make_pair(CLONE_NEWPID
, kSandboxPIDNSEnvironmentVarName
),
122 std::make_pair(CLONE_NEWNET
, kSandboxNETNSEnvironmentVarName
),
125 base::EnvironmentMap
* environ
= &launch_options
.environ
;
126 for (const auto& entry
: clone_flag_environ
) {
127 const int flag
= entry
.first
;
128 const char* environ_name
= entry
.second
;
129 SetEnvironForNamespaceType(environ
, environ_name
, clone_flags
& flag
);
132 return base::LaunchProcess(argv
, launch_options
);
134 #endif // !defined(OS_NACL_NONSFI)
137 pid_t
NamespaceSandbox::ForkInNewPidNamespace(bool drop_capabilities_in_child
) {
139 base::ForkWithFlags(CLONE_NEWPID
| LINUX_SIGCHLD
, nullptr, nullptr);
145 DCHECK_EQ(1, getpid());
146 if (drop_capabilities_in_child
) {
147 // Since we just forked, we are single-threaded, so this should be safe.
148 CHECK(Credentials::DropAllCapabilitiesOnCurrentThread());
157 void NamespaceSandbox::InstallDefaultTerminationSignalHandlers() {
158 static const int kDefaultTermSignals
[] = {
159 LINUX_SIGHUP
, LINUX_SIGINT
, LINUX_SIGABRT
, LINUX_SIGQUIT
,
160 LINUX_SIGPIPE
, LINUX_SIGTERM
, LINUX_SIGUSR1
, LINUX_SIGUSR2
,
163 for (const int sig
: kDefaultTermSignals
) {
164 InstallTerminationSignalHandler(sig
, SignalExitCode(sig
));
169 bool NamespaceSandbox::InstallTerminationSignalHandler(
172 struct sigaction old_action
;
173 PCHECK(sys_sigaction(sig
, nullptr, &old_action
) == 0);
175 #if !defined(OS_NACL_NONSFI)
176 if (old_action
.sa_flags
& SA_SIGINFO
&&
177 old_action
.sa_sigaction
!= nullptr) {
182 if (old_action
.sa_handler
!= LINUX_SIG_DFL
) {
186 const size_t sig_idx
= static_cast<size_t>(sig
);
187 CHECK_LT(sig_idx
, arraysize(g_signal_exit_codes
));
189 DCHECK_GE(exit_code
, 0);
190 DCHECK_LT(exit_code
, 256);
192 g_signal_exit_codes
[sig_idx
] = exit_code
;
194 struct sigaction action
= {};
195 action
.sa_handler
= &TerminationSignalHandler
;
196 PCHECK(sys_sigaction(sig
, &action
, nullptr) == 0);
201 bool NamespaceSandbox::InNewUserNamespace() {
202 return getenv(kSandboxUSERNSEnvironmentVarName
) != nullptr;
206 bool NamespaceSandbox::InNewPidNamespace() {
207 return getenv(kSandboxPIDNSEnvironmentVarName
) != nullptr;
211 bool NamespaceSandbox::InNewNetNamespace() {
212 return getenv(kSandboxNETNSEnvironmentVarName
) != nullptr;
215 } // namespace sandbox