OLD | NEW |
(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 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::LaunchProcess( |
| 60 const base::CommandLine& cmdline, |
| 61 const base::LaunchOptions& options) { |
| 62 int clone_flags = 0; |
| 63 int ns_types[] = {CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET}; |
| 64 for (const int ns_type : ns_types) { |
| 65 if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type)) { |
| 66 clone_flags |= ns_type; |
| 67 } |
| 68 } |
| 69 CHECK(clone_flags & CLONE_NEWUSER); |
| 70 |
| 71 // These fields may not be set by the caller. |
| 72 CHECK(options.pre_exec_delegate == nullptr); |
| 73 CHECK_EQ(0, options.clone_flags); |
| 74 |
| 75 WriteUidGidMapDelegate write_uid_gid_map_delegate; |
| 76 |
| 77 base::LaunchOptions launch_options = options; |
| 78 launch_options.pre_exec_delegate = &write_uid_gid_map_delegate; |
| 79 launch_options.clone_flags = clone_flags; |
| 80 |
| 81 const std::pair<int, const char*> clone_flag_environ[] = { |
| 82 std::make_pair(CLONE_NEWUSER, kSandboxUSERNSEnvironmentVarName), |
| 83 std::make_pair(CLONE_NEWPID, kSandboxPIDNSEnvironmentVarName), |
| 84 std::make_pair(CLONE_NEWNET, kSandboxNETNSEnvironmentVarName), |
| 85 }; |
| 86 |
| 87 base::EnvironmentMap* environ = &launch_options.environ; |
| 88 for (const auto& entry : clone_flag_environ) { |
| 89 const int flag = entry.first; |
| 90 const char* environ_name = entry.second; |
| 91 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag); |
| 92 } |
| 93 |
| 94 return base::LaunchProcess(cmdline, launch_options); |
| 95 } |
| 96 |
| 97 // static |
| 98 bool NamespaceSandbox::InNewUserNamespace() { |
| 99 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr; |
| 100 } |
| 101 |
| 102 // static |
| 103 bool NamespaceSandbox::InNewPidNamespace() { |
| 104 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr; |
| 105 } |
| 106 |
| 107 // static |
| 108 bool NamespaceSandbox::InNewNetNamespace() { |
| 109 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr; |
| 110 } |
| 111 |
| 112 } // namespace sandbox |
OLD | NEW |