Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <dirent.h> | |
| 5 #include <fcntl.h> | 6 #include <fcntl.h> |
| 6 #include <sys/resource.h> | 7 #include <sys/resource.h> |
| 7 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 8 #include <sys/time.h> | 9 #include <sys/time.h> |
| 9 #include <sys/types.h> | 10 #include <sys/types.h> |
| 10 | 11 |
| 11 #include <limits> | 12 #include <limits> |
| 12 | 13 |
| 13 #include "base/bind.h" | 14 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" | 15 #include "base/callback_helpers.h" |
| 15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/memory/singleton.h" | 18 #include "base/memory/singleton.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/strings/string_number_conversions.h" | |
| 19 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 20 #include "content/common/sandbox_linux.h" | 22 #include "content/common/sandbox_linux.h" |
| 21 #include "content/common/sandbox_seccomp_bpf_linux.h" | 23 #include "content/common/sandbox_seccomp_bpf_linux.h" |
| 22 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 23 #include "content/public/common/sandbox_linux.h" | 25 #include "content/public/common/sandbox_linux.h" |
| 24 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 26 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 void LogSandboxStarted(const std::string& sandbox_name) { | 30 void LogSandboxStarted(const std::string& sandbox_name) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 53 } | 55 } |
| 54 | 56 |
| 55 bool IsRunningTSAN() { | 57 bool IsRunningTSAN() { |
| 56 #if defined(THREAD_SANITIZER) | 58 #if defined(THREAD_SANITIZER) |
| 57 return true; | 59 return true; |
| 58 #else | 60 #else |
| 59 return false; | 61 return false; |
| 60 #endif | 62 #endif |
| 61 } | 63 } |
| 62 | 64 |
| 65 struct DIRDeleter { | |
| 66 void operator()(DIR* d) { | |
| 67 CHECK(closedir(d) == 0); | |
|
jln (very slow on Chromium)
2013/11/02 00:10:56
You can even use PCHECK().
Mostyn Bramley-Moore
2013/11/02 07:30:07
Done.
| |
| 68 } | |
| 69 }; | |
| 70 | |
| 63 } // namespace | 71 } // namespace |
| 64 | 72 |
| 65 namespace content { | 73 namespace content { |
| 66 | 74 |
| 67 LinuxSandbox::LinuxSandbox() | 75 LinuxSandbox::LinuxSandbox() |
| 68 : proc_fd_(-1), | 76 : proc_fd_(-1), |
| 69 seccomp_bpf_started_(false), | 77 seccomp_bpf_started_(false), |
| 70 pre_initialized_(false), | 78 pre_initialized_(false), |
| 79 sealed_(false), | |
| 71 seccomp_bpf_supported_(false), | 80 seccomp_bpf_supported_(false), |
| 72 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { | 81 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { |
| 73 if (setuid_sandbox_client_ == NULL) { | 82 if (setuid_sandbox_client_ == NULL) { |
| 74 LOG(FATAL) << "Failed to instantiate the setuid sandbox client."; | 83 LOG(FATAL) << "Failed to instantiate the setuid sandbox client."; |
| 75 } | 84 } |
| 76 } | 85 } |
| 77 | 86 |
| 78 LinuxSandbox::~LinuxSandbox() { | 87 LinuxSandbox::~LinuxSandbox() { |
| 79 } | 88 } |
| 80 | 89 |
| 81 LinuxSandbox* LinuxSandbox::GetInstance() { | 90 LinuxSandbox* LinuxSandbox::GetInstance() { |
| 82 LinuxSandbox* instance = Singleton<LinuxSandbox>::get(); | 91 LinuxSandbox* instance = Singleton<LinuxSandbox>::get(); |
| 83 CHECK(instance); | 92 CHECK(instance); |
| 84 return instance; | 93 return instance; |
| 85 } | 94 } |
| 86 | 95 |
| 87 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX) | 96 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX) |
| 88 // ASan API call to notify the tool the sandbox is going to be turned on. | 97 // ASan API call to notify the tool the sandbox is going to be turned on. |
| 89 extern "C" void __sanitizer_sandbox_on_notify(void *reserved); | 98 extern "C" void __sanitizer_sandbox_on_notify(void *reserved); |
| 90 #endif | 99 #endif |
| 91 | 100 |
| 92 void LinuxSandbox::PreinitializeSandbox() { | 101 void LinuxSandbox::PreinitializeSandbox() { |
| 93 CHECK(!pre_initialized_); | 102 CHECK(!pre_initialized_); |
| 103 CHECK(!sealed_); | |
| 94 seccomp_bpf_supported_ = false; | 104 seccomp_bpf_supported_ = false; |
| 95 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX) | 105 #if defined(ADDRESS_SANITIZER) && defined(OS_LINUX) |
| 96 // ASan needs to open some resources before the sandbox is enabled. | 106 // ASan needs to open some resources before the sandbox is enabled. |
| 97 // This should not fork, not launch threads, not open a directory. | 107 // This should not fork, not launch threads, not open a directory. |
| 98 __sanitizer_sandbox_on_notify(/*reserved*/NULL); | 108 __sanitizer_sandbox_on_notify(/*reserved*/NULL); |
| 99 #endif | 109 #endif |
| 100 | 110 |
| 101 #if !defined(NDEBUG) | 111 #if !defined(NDEBUG) |
| 102 // Open proc_fd_ only in Debug mode so that forgetting to close it doesn't | 112 // Open proc_fd_ only in Debug mode so that forgetting to close it doesn't |
| 103 // produce a sandbox escape in Release mode. | 113 // produce a sandbox escape in Release mode. |
| 104 proc_fd_ = open("/proc", O_DIRECTORY | O_RDONLY); | 114 proc_fd_ = open("/proc", O_DIRECTORY | O_RDONLY); |
| 105 CHECK_GE(proc_fd_, 0); | 115 CHECK_GE(proc_fd_, 0); |
| 106 #endif // !defined(NDEBUG) | 116 #endif // !defined(NDEBUG) |
| 107 // We "pre-warm" the code that detects supports for seccomp BPF. | 117 // We "pre-warm" the code that detects supports for seccomp BPF. |
| 108 if (SandboxSeccompBpf::IsSeccompBpfDesired()) { | 118 if (SandboxSeccompBpf::IsSeccompBpfDesired()) { |
| 109 if (!SandboxSeccompBpf::SupportsSandbox()) { | 119 if (!SandboxSeccompBpf::SupportsSandbox()) { |
| 110 VLOG(1) << "Lacking support for seccomp-bpf sandbox."; | 120 VLOG(1) << "Lacking support for seccomp-bpf sandbox."; |
| 111 } else { | 121 } else { |
| 112 seccomp_bpf_supported_ = true; | 122 seccomp_bpf_supported_ = true; |
| 113 } | 123 } |
| 114 } | 124 } |
| 115 pre_initialized_ = true; | 125 pre_initialized_ = true; |
| 116 } | 126 } |
| 117 | 127 |
| 118 bool LinuxSandbox::InitializeSandbox() { | 128 bool LinuxSandbox::InitializeSandbox() { |
| 119 bool seccomp_bpf_started = false; | 129 bool seccomp_bpf_started = false; |
| 120 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); | 130 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
| 131 CHECK(!linux_sandbox->sealed_); | |
| 121 // We need to make absolutely sure that our sandbox is "sealed" before | 132 // We need to make absolutely sure that our sandbox is "sealed" before |
| 122 // InitializeSandbox does exit. | 133 // InitializeSandbox does exit. |
| 123 base::ScopedClosureRunner sandbox_sealer( | 134 base::ScopedClosureRunner sandbox_sealer( |
| 124 base::Bind(&LinuxSandbox::SealSandbox, base::Unretained(linux_sandbox))); | 135 base::Bind(&LinuxSandbox::SealSandbox, base::Unretained(linux_sandbox))); |
| 125 const std::string process_type = | 136 const std::string process_type = |
| 126 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 137 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 127 switches::kProcessType); | 138 switches::kProcessType); |
| 128 | 139 |
| 129 // No matter what, it's always an error to call InitializeSandbox() after | 140 // No matter what, it's always an error to call InitializeSandbox() after |
| 130 // threads have been created. | 141 // threads have been created. |
| 131 if (!linux_sandbox->IsSingleThreaded()) { | 142 if (!linux_sandbox->IsSingleThreaded()) { |
| 132 std::string error_message = "InitializeSandbox() called with multiple " | 143 std::string error_message = "InitializeSandbox() called with multiple " |
| 133 "threads in process " + process_type; | 144 "threads in process " + process_type; |
| 134 // TSAN starts a helper thread. So we don't start the sandbox and don't | 145 // TSAN starts a helper thread. So we don't start the sandbox and don't |
| 135 // even report an error about it. | 146 // even report an error about it. |
| 136 if (IsRunningTSAN()) | 147 if (IsRunningTSAN()) |
| 137 return false; | 148 return false; |
| 138 // The GPU process is allowed to call InitializeSandbox() with threads for | 149 // The GPU process is allowed to call InitializeSandbox() with threads for |
| 139 // now, because it loads third party libraries. | 150 // now, because it loads third party libraries. |
| 140 if (process_type != switches::kGpuProcess) | 151 if (process_type != switches::kGpuProcess) |
| 141 CHECK(false) << error_message; | 152 CHECK(false) << error_message; |
| 142 LOG(ERROR) << error_message; | 153 LOG(ERROR) << error_message; |
| 143 return false; | 154 return false; |
| 144 } | 155 } |
| 145 | 156 |
| 157 if (linux_sandbox->HasOpenDirectories()) { | |
| 158 LOG(FATAL) << "InitializeSandbox() called after unexpected directories " | |
| 159 "have been opened- the setuid sandbox may be at risk, if " | |
|
jln (very slow on Chromium)
2013/11/02 00:10:56
I would drop the detail about the BPF sandbox and
Mostyn Bramley-Moore
2013/11/02 07:30:07
Done.
| |
| 160 "the BPF sandbox is not running."; | |
| 161 } | |
| 162 | |
| 146 // Attempt to limit the future size of the address space of the process. | 163 // Attempt to limit the future size of the address space of the process. |
| 147 linux_sandbox->LimitAddressSpace(process_type); | 164 linux_sandbox->LimitAddressSpace(process_type); |
| 148 | 165 |
| 149 // First, try to enable seccomp-bpf. | 166 // First, try to enable seccomp-bpf. |
| 150 seccomp_bpf_started = linux_sandbox->StartSeccompBpf(process_type); | 167 seccomp_bpf_started = linux_sandbox->StartSeccompBpf(process_type); |
| 151 | 168 |
| 152 return seccomp_bpf_started; | 169 return seccomp_bpf_started; |
| 153 } | 170 } |
| 154 | 171 |
| 155 int LinuxSandbox::GetStatus() const { | 172 int LinuxSandbox::GetStatus() const { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 } | 218 } |
| 202 | 219 |
| 203 // At least "..", "." and the current thread should be present. | 220 // At least "..", "." and the current thread should be present. |
| 204 CHECK_LE(3UL, task_stat.st_nlink); | 221 CHECK_LE(3UL, task_stat.st_nlink); |
| 205 // Counting threads via /proc/self/task could be racy. For the purpose of | 222 // Counting threads via /proc/self/task could be racy. For the purpose of |
| 206 // determining if the current proces is monothreaded it works: if at any | 223 // determining if the current proces is monothreaded it works: if at any |
| 207 // time it becomes monothreaded, it'll stay so. | 224 // time it becomes monothreaded, it'll stay so. |
| 208 return task_stat.st_nlink == 3; | 225 return task_stat.st_nlink == 3; |
| 209 } | 226 } |
| 210 | 227 |
| 228 bool LinuxSandbox::HasOpenDirectories() { | |
| 229 CHECK(!sealed_); | |
| 230 | |
| 231 int proc_self_fd = -1; | |
| 232 if (proc_fd_ >= 0) { | |
| 233 proc_self_fd = openat(proc_fd_, "self/fd", O_DIRECTORY | O_RDONLY); | |
| 234 } else { | |
| 235 proc_self_fd = openat(AT_FDCWD, "/proc/self/fd", O_DIRECTORY | O_RDONLY); | |
| 236 if ((proc_self_fd < 0) && errno == ENOENT) { | |
| 237 // Guess false. | |
| 238 return false; | |
| 239 } | |
| 240 } | |
| 241 CHECK_GE(proc_self_fd, 0); | |
| 242 | |
| 243 // Ownership of proc_self_fd is transferred here, it must not be closed | |
| 244 // or modified afterwards except via dir. | |
| 245 scoped_ptr<DIR, DIRDeleter> dir(fdopendir(proc_self_fd)); | |
| 246 CHECK(dir); | |
| 247 | |
| 248 struct dirent e; | |
| 249 struct dirent* de; | |
| 250 while (!readdir_r(dir.get(), &e, &de) && de) { | |
| 251 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) | |
| 252 continue; | |
| 253 | |
| 254 int fd_num; | |
| 255 CHECK(base::StringToInt(e.d_name, &fd_num)); | |
| 256 if (fd_num == proc_fd_ || fd_num == proc_self_fd) { | |
| 257 continue; | |
| 258 } | |
| 259 | |
| 260 struct stat s; | |
| 261 // It's OK to use proc_self_fd here, fstatat won't modify it. | |
| 262 CHECK(fstatat(proc_self_fd, e.d_name, &s, 0) == 0); | |
| 263 if (S_ISDIR(s.st_mode)) { | |
| 264 return true; | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 // No open unmanaged directories found. \o/ | |
| 269 return false; | |
| 270 } | |
| 271 | |
| 211 bool LinuxSandbox::seccomp_bpf_started() const { | 272 bool LinuxSandbox::seccomp_bpf_started() const { |
| 212 return seccomp_bpf_started_; | 273 return seccomp_bpf_started_; |
| 213 } | 274 } |
| 214 | 275 |
| 215 sandbox::SetuidSandboxClient* | 276 sandbox::SetuidSandboxClient* |
| 216 LinuxSandbox::setuid_sandbox_client() const { | 277 LinuxSandbox::setuid_sandbox_client() const { |
| 217 return setuid_sandbox_client_.get(); | 278 return setuid_sandbox_client_.get(); |
| 218 } | 279 } |
| 219 | 280 |
| 220 // For seccomp-bpf, we use the SandboxSeccompBpf class. | 281 // For seccomp-bpf, we use the SandboxSeccompBpf class. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 return false; | 336 return false; |
| 276 #endif // !defined(ADDRESS_SANITIZER) | 337 #endif // !defined(ADDRESS_SANITIZER) |
| 277 } | 338 } |
| 278 | 339 |
| 279 void LinuxSandbox::SealSandbox() { | 340 void LinuxSandbox::SealSandbox() { |
| 280 if (proc_fd_ >= 0) { | 341 if (proc_fd_ >= 0) { |
| 281 int ret = HANDLE_EINTR(close(proc_fd_)); | 342 int ret = HANDLE_EINTR(close(proc_fd_)); |
| 282 CHECK_EQ(0, ret); | 343 CHECK_EQ(0, ret); |
| 283 proc_fd_ = -1; | 344 proc_fd_ = -1; |
| 284 } | 345 } |
| 346 sealed_ = true; | |
| 285 } | 347 } |
| 286 | 348 |
| 287 } // namespace content | 349 } // namespace content |
| 288 | 350 |
| OLD | NEW |