| 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 <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 16 #include "base/environment.h" | 17 #include "base/environment.h" |
| 17 #include "base/files/scoped_file.h" | 18 #include "base/files/scoped_file.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/posix/eintr_wrapper.h" | 20 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/process/launch.h" | 21 #include "base/process/launch.h" |
| 21 #include "base/process/process.h" | 22 #include "base/process/process.h" |
| 22 #include "sandbox/linux/services/namespace_utils.h" | 23 #include "sandbox/linux/services/namespace_utils.h" |
| 23 | 24 |
| 24 namespace sandbox { | 25 namespace sandbox { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 class WriteUidGidMapDelegate : public base::LaunchOptions::PreExecDelegate { | 29 class WriteUidGidMapDelegate : public base::LaunchOptions::PreExecDelegate { |
| 29 public: | 30 public: |
| 30 WriteUidGidMapDelegate() : uid_(getuid()), gid_(getgid()) {} | 31 WriteUidGidMapDelegate() |
| 32 : uid_(getuid()), |
| 33 gid_(getgid()), |
| 34 supports_deny_setgroups_( |
| 35 NamespaceUtils::KernelSupportsDenySetgroups()) {} |
| 31 | 36 |
| 32 ~WriteUidGidMapDelegate() override {} | 37 ~WriteUidGidMapDelegate() override {} |
| 33 | 38 |
| 34 void RunAsyncSafe() override { | 39 void RunAsyncSafe() override { |
| 40 if (supports_deny_setgroups_) { |
| 41 RAW_CHECK(NamespaceUtils::DenySetgroups()); |
| 42 } |
| 35 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid_)); | 43 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid_)); |
| 36 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid_)); | 44 RAW_CHECK(NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid_)); |
| 37 } | 45 } |
| 38 | 46 |
| 39 private: | 47 private: |
| 40 uid_t uid_; | 48 const uid_t uid_; |
| 41 gid_t gid_; | 49 const gid_t gid_; |
| 50 const bool supports_deny_setgroups_; |
| 42 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate); | 51 DISALLOW_COPY_AND_ASSIGN(WriteUidGidMapDelegate); |
| 43 }; | 52 }; |
| 44 | 53 |
| 45 void SetEnvironForNamespaceType(base::EnvironmentMap* environ, | 54 void SetEnvironForNamespaceType(base::EnvironmentMap* environ, |
| 46 base::NativeEnvironmentString env_var, | 55 base::NativeEnvironmentString env_var, |
| 47 bool value) { | 56 bool value) { |
| 48 // An empty string causes the env var to be unset in the child process. | 57 // An empty string causes the env var to be unset in the child process. |
| 49 (*environ)[env_var] = value ? "1" : ""; | 58 (*environ)[env_var] = value ? "1" : ""; |
| 50 } | 59 } |
| 51 | 60 |
| 52 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS"; | 61 const char kSandboxUSERNSEnvironmentVarName[] = "SBX_USER_NS"; |
| 53 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS"; | 62 const char kSandboxPIDNSEnvironmentVarName[] = "SBX_PID_NS"; |
| 54 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS"; | 63 const char kSandboxNETNSEnvironmentVarName[] = "SBX_NET_NS"; |
| 55 | 64 |
| 56 } // namespace | 65 } // namespace |
| 57 | 66 |
| 58 // static | 67 // static |
| 59 base::Process NamespaceSandbox::LaunchProcess( | 68 base::Process NamespaceSandbox::LaunchProcess( |
| 60 const base::CommandLine& cmdline, | 69 const base::CommandLine& cmdline, |
| 61 const base::LaunchOptions& options) { | 70 const base::LaunchOptions& options) { |
| 71 return LaunchProcess(cmdline.argv(), options); |
| 72 } |
| 73 |
| 74 // static |
| 75 base::Process NamespaceSandbox::LaunchProcess( |
| 76 const std::vector<std::string>& argv, |
| 77 const base::LaunchOptions& options) { |
| 62 int clone_flags = 0; | 78 int clone_flags = 0; |
| 63 int ns_types[] = {CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET}; | 79 int ns_types[] = {CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET}; |
| 64 for (const int ns_type : ns_types) { | 80 for (const int ns_type : ns_types) { |
| 65 if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type)) { | 81 if (NamespaceUtils::KernelSupportsUnprivilegedNamespace(ns_type)) { |
| 66 clone_flags |= ns_type; | 82 clone_flags |= ns_type; |
| 67 } | 83 } |
| 68 } | 84 } |
| 69 CHECK(clone_flags & CLONE_NEWUSER); | 85 CHECK(clone_flags & CLONE_NEWUSER); |
| 70 | 86 |
| 71 // These fields may not be set by the caller. | 87 // These fields may not be set by the caller. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 std::make_pair(CLONE_NEWNET, kSandboxNETNSEnvironmentVarName), | 100 std::make_pair(CLONE_NEWNET, kSandboxNETNSEnvironmentVarName), |
| 85 }; | 101 }; |
| 86 | 102 |
| 87 base::EnvironmentMap* environ = &launch_options.environ; | 103 base::EnvironmentMap* environ = &launch_options.environ; |
| 88 for (const auto& entry : clone_flag_environ) { | 104 for (const auto& entry : clone_flag_environ) { |
| 89 const int flag = entry.first; | 105 const int flag = entry.first; |
| 90 const char* environ_name = entry.second; | 106 const char* environ_name = entry.second; |
| 91 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag); | 107 SetEnvironForNamespaceType(environ, environ_name, clone_flags & flag); |
| 92 } | 108 } |
| 93 | 109 |
| 94 return base::LaunchProcess(cmdline, launch_options); | 110 return base::LaunchProcess(argv, launch_options); |
| 95 } | 111 } |
| 96 | 112 |
| 97 // static | 113 // static |
| 98 bool NamespaceSandbox::InNewUserNamespace() { | 114 bool NamespaceSandbox::InNewUserNamespace() { |
| 99 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr; | 115 return getenv(kSandboxUSERNSEnvironmentVarName) != nullptr; |
| 100 } | 116 } |
| 101 | 117 |
| 102 // static | 118 // static |
| 103 bool NamespaceSandbox::InNewPidNamespace() { | 119 bool NamespaceSandbox::InNewPidNamespace() { |
| 104 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr; | 120 return getenv(kSandboxPIDNSEnvironmentVarName) != nullptr; |
| 105 } | 121 } |
| 106 | 122 |
| 107 // static | 123 // static |
| 108 bool NamespaceSandbox::InNewNetNamespace() { | 124 bool NamespaceSandbox::InNewNetNamespace() { |
| 109 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr; | 125 return getenv(kSandboxNETNSEnvironmentVarName) != nullptr; |
| 110 } | 126 } |
| 111 | 127 |
| 112 } // namespace sandbox | 128 } // namespace sandbox |
| OLD | NEW |