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"
32 const char kSandboxUSERNSEnvironmentVarName
[] = "SBX_USER_NS";
33 const char kSandboxPIDNSEnvironmentVarName
[] = "SBX_PID_NS";
34 const char kSandboxNETNSEnvironmentVarName
[] = "SBX_NET_NS";
36 #if !defined(OS_NACL_NONSFI)
37 class WriteUidGidMapDelegate
: public base::LaunchOptions::PreExecDelegate
{
39 WriteUidGidMapDelegate()
42 supports_deny_setgroups_(
43 NamespaceUtils::KernelSupportsDenySetgroups()) {}
45 ~WriteUidGidMapDelegate() override
{}
47 void RunAsyncSafe() override
{
48 if (supports_deny_setgroups_
) {
49 RAW_CHECK(NamespaceUtils::DenySetgroups());
51 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid_
));
52 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid_
));
58 const bool supports_deny_setgroups_
;
59 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate
);
62 void SetEnvironForNamespaceType(base::EnvironmentMap
* environ
,
63 base::NativeEnvironmentString env_var
,
65 // An empty string causes the env var to be unset in the child process.
66 (*environ
)[env_var
] = value
? "1" : "";
69 // Linux supports up to 64 signals. This should be updated if that ever changes.
70 int g_signal_exit_codes
[64];
72 void TerminationSignalHandler(int sig
) {
73 // Return a special exit code so that the process is detected as terminated by
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
]);
80 _exit(NamespaceSandbox::kDefaultExitCode
);
82 #endif // !defined(OS_NACL_NONSFI)
86 #if !defined(OS_NACL_NONSFI)
88 base::Process
NamespaceSandbox::LaunchProcess(
89 const base::CommandLine
& cmdline
,
90 const base::LaunchOptions
& options
) {
91 return LaunchProcess(cmdline
.argv(), options
);
95 base::Process
NamespaceSandbox::LaunchProcess(
96 const std::vector
<std::string
>& argv
,
97 const base::LaunchOptions
& options
) {
99 int ns_types
[] = {CLONE_NEWUSER
, CLONE_NEWPID
, CLONE_NEWNET
};
100 for (const int ns_type
: ns_types
) {
101 if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type
)) {
102 clone_flags
|= ns_type
;
105 CHECK(clone_flags
& CLONE_NEWUSER
);
107 // These fields may not be set by the caller.
108 CHECK(options
.pre_exec_delegate
== nullptr);
109 CHECK_EQ(0, options
.clone_flags
);
111 WriteUidGidMapDelegate write_uid_gid_map_delegate
;
113 base::LaunchOptions launch_options
= options
;
114 launch_options
.pre_exec_delegate
= &write_uid_gid_map_delegate
;
115 launch_options
.clone_flags
= clone_flags
;
117 const std::pair
<int, const char*> clone_flag_environ
[] = {
118 std::make_pair(CLONE_NEWUSER
, kSandboxUSERNSEnvironmentVarName
),
119 std::make_pair(CLONE_NEWPID
, kSandboxPIDNSEnvironmentVarName
),
120 std::make_pair(CLONE_NEWNET
, kSandboxNETNSEnvironmentVarName
),
123 base::EnvironmentMap
* environ
= &launch_options
.environ
;
124 for (const auto& entry
: clone_flag_environ
) {
125 const int flag
= entry
.first
;
126 const char* environ_name
= entry
.second
;
127 SetEnvironForNamespaceType(environ
, environ_name
, clone_flags
& flag
);
130 return base::LaunchProcess(argv
, launch_options
);
134 pid_t
NamespaceSandbox::ForkInNewPidNamespace(bool drop_capabilities_in_child
) {
136 base::ForkWithFlags(CLONE_NEWPID
| SIGCHLD
, nullptr, nullptr);
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());
154 void NamespaceSandbox::InstallDefaultTerminationSignalHandlers() {
155 static const int kDefaultTermSignals
[] = {
156 SIGHUP
, SIGINT
, SIGABRT
, SIGQUIT
, SIGPIPE
, SIGTERM
, SIGUSR1
, SIGUSR2
,
159 for (const int sig
: kDefaultTermSignals
) {
160 InstallTerminationSignalHandler(sig
, kDefaultExitCode
);
165 bool NamespaceSandbox::InstallTerminationSignalHandler(
168 struct sigaction old_action
;
169 PCHECK(sigaction(sig
, nullptr, &old_action
) == 0);
171 if (old_action
.sa_flags
& SA_SIGINFO
&&
172 old_action
.sa_sigaction
!= nullptr) {
174 } else if (old_action
.sa_handler
!= SIG_DFL
) {
178 const size_t sig_idx
= static_cast<size_t>(sig
);
179 CHECK_LT(sig_idx
, arraysize(g_signal_exit_codes
));
181 DCHECK_GE(exit_code
, 0);
182 DCHECK_LT(exit_code
, 256);
184 g_signal_exit_codes
[sig_idx
] = exit_code
;
186 struct sigaction action
= {};
187 action
.sa_handler
= &TerminationSignalHandler
;
188 PCHECK(sigaction(sig
, &action
, nullptr) == 0);
191 #endif // !defined(OS_NACL_NONSFI)
194 bool NamespaceSandbox::InNewUserNamespace() {
195 return getenv(kSandboxUSERNSEnvironmentVarName
) != nullptr;
199 bool NamespaceSandbox::InNewPidNamespace() {
200 return getenv(kSandboxPIDNSEnvironmentVarName
) != nullptr;
204 bool NamespaceSandbox::InNewNetNamespace() {
205 return getenv(kSandboxNETNSEnvironmentVarName
) != nullptr;
208 } // namespace sandbox