| 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 <dirent.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/resource.h> | 7 #include <sys/resource.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 #include <sys/time.h> | 9 #include <sys/time.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <unistd.h> |
| 11 | 12 |
| 12 #include <limits> | 13 #include <limits> |
| 13 | 14 |
| 14 #include "base/bind.h" | 15 #include "base/bind.h" |
| 15 #include "base/callback_helpers.h" | 16 #include "base/callback_helpers.h" |
| 16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/singleton.h" | 20 #include "base/memory/singleton.h" |
| 19 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 22 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 23 #include "content/common/sandbox_linux/sandbox_linux.h" | 25 #include "content/common/sandbox_linux/sandbox_linux.h" |
| 24 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" | 26 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" |
| 25 #include "content/public/common/content_switches.h" | 27 #include "content/public/common/content_switches.h" |
| 26 #include "content/public/common/sandbox_linux.h" | 28 #include "content/public/common/sandbox_linux.h" |
| 27 #include "sandbox/linux/services/credentials.h" | 29 #include "sandbox/linux/services/credentials.h" |
| 30 #include "sandbox/linux/services/thread_helpers.h" |
| 28 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 31 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 35 struct FDCloser { |
| 36 inline void operator()(int* fd) const { |
| 37 DCHECK(fd); |
| 38 PCHECK(0 == IGNORE_EINTR(close(*fd))); |
| 39 *fd = -1; |
| 40 } |
| 41 }; |
| 42 |
| 43 // Don't use base::ScopedFD since it doesn't CHECK that the file descriptor was |
| 44 // closed. |
| 45 typedef scoped_ptr<int, FDCloser> SafeScopedFD; |
| 46 |
| 32 void LogSandboxStarted(const std::string& sandbox_name) { | 47 void LogSandboxStarted(const std::string& sandbox_name) { |
| 33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 48 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 34 const std::string process_type = | 49 const std::string process_type = |
| 35 command_line.GetSwitchValueASCII(switches::kProcessType); | 50 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 36 const std::string activated_sandbox = | 51 const std::string activated_sandbox = |
| 37 "Activated " + sandbox_name + " sandbox for process type: " + | 52 "Activated " + sandbox_name + " sandbox for process type: " + |
| 38 process_type + "."; | 53 process_type + "."; |
| 39 #if defined(OS_CHROMEOS) | 54 #if defined(OS_CHROMEOS) |
| 40 LOG(WARNING) << activated_sandbox; | 55 LOG(WARNING) << activated_sandbox; |
| 41 #else | 56 #else |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 } | 72 } |
| 58 | 73 |
| 59 bool IsRunningTSAN() { | 74 bool IsRunningTSAN() { |
| 60 #if defined(THREAD_SANITIZER) | 75 #if defined(THREAD_SANITIZER) |
| 61 return true; | 76 return true; |
| 62 #else | 77 #else |
| 63 return false; | 78 return false; |
| 64 #endif | 79 #endif |
| 65 } | 80 } |
| 66 | 81 |
| 82 // Try to open /proc/self/task/ with the help of |proc_fd|. |proc_fd| can be |
| 83 // -1. Will return -1 on error and set errno like open(2). |
| 84 int OpenProcTaskFd(int proc_fd) { |
| 85 int proc_self_task = -1; |
| 86 if (proc_fd >= 0) { |
| 87 // If a handle to /proc is available, use it. This allows to bypass file |
| 88 // system restrictions. |
| 89 proc_self_task = openat(proc_fd, "self/task/", O_RDONLY | O_DIRECTORY); |
| 90 } else { |
| 91 // Otherwise, make an attempt to access the file system directly. |
| 92 proc_self_task = open("/proc/self/task/", O_RDONLY | O_DIRECTORY); |
| 93 } |
| 94 return proc_self_task; |
| 95 } |
| 96 |
| 67 } // namespace | 97 } // namespace |
| 68 | 98 |
| 69 namespace content { | 99 namespace content { |
| 70 | 100 |
| 71 LinuxSandbox::LinuxSandbox() | 101 LinuxSandbox::LinuxSandbox() |
| 72 : proc_fd_(-1), | 102 : proc_fd_(-1), |
| 73 seccomp_bpf_started_(false), | 103 seccomp_bpf_started_(false), |
| 74 sandbox_status_flags_(kSandboxLinuxInvalid), | 104 sandbox_status_flags_(kSandboxLinuxInvalid), |
| 75 pre_initialized_(false), | 105 pre_initialized_(false), |
| 76 seccomp_bpf_supported_(false), | 106 seccomp_bpf_supported_(false), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 } | 148 } |
| 119 } | 149 } |
| 120 pre_initialized_ = true; | 150 pre_initialized_ = true; |
| 121 } | 151 } |
| 122 | 152 |
| 123 bool LinuxSandbox::InitializeSandbox() { | 153 bool LinuxSandbox::InitializeSandbox() { |
| 124 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); | 154 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
| 125 return linux_sandbox->InitializeSandboxImpl(); | 155 return linux_sandbox->InitializeSandboxImpl(); |
| 126 } | 156 } |
| 127 | 157 |
| 158 void LinuxSandbox::StopThread(base::Thread* thread) { |
| 159 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
| 160 linux_sandbox->StopThreadImpl(thread); |
| 161 } |
| 162 |
| 128 int LinuxSandbox::GetStatus() { | 163 int LinuxSandbox::GetStatus() { |
| 129 CHECK(pre_initialized_); | 164 CHECK(pre_initialized_); |
| 130 if (kSandboxLinuxInvalid == sandbox_status_flags_) { | 165 if (kSandboxLinuxInvalid == sandbox_status_flags_) { |
| 131 // Initialize sandbox_status_flags_. | 166 // Initialize sandbox_status_flags_. |
| 132 sandbox_status_flags_ = 0; | 167 sandbox_status_flags_ = 0; |
| 133 if (setuid_sandbox_client_->IsSandboxed()) { | 168 if (setuid_sandbox_client_->IsSandboxed()) { |
| 134 sandbox_status_flags_ |= kSandboxLinuxSUID; | 169 sandbox_status_flags_ |= kSandboxLinuxSUID; |
| 135 if (setuid_sandbox_client_->IsInNewPIDNamespace()) | 170 if (setuid_sandbox_client_->IsInNewPIDNamespace()) |
| 136 sandbox_status_flags_ |= kSandboxLinuxPIDNS; | 171 sandbox_status_flags_ |= kSandboxLinuxPIDNS; |
| 137 if (setuid_sandbox_client_->IsInNewNETNamespace()) | 172 if (setuid_sandbox_client_->IsInNewNETNamespace()) |
| 138 sandbox_status_flags_ |= kSandboxLinuxNetNS; | 173 sandbox_status_flags_ |= kSandboxLinuxNetNS; |
| 139 } | 174 } |
| 140 | 175 |
| 141 // We report whether the sandbox will be activated when renderers, workers | 176 // We report whether the sandbox will be activated when renderers, workers |
| 142 // and PPAPI plugins go through sandbox initialization. | 177 // and PPAPI plugins go through sandbox initialization. |
| 143 if (seccomp_bpf_supported() && | 178 if (seccomp_bpf_supported() && |
| 144 SandboxSeccompBPF::ShouldEnableSeccompBPF(switches::kRendererProcess)) { | 179 SandboxSeccompBPF::ShouldEnableSeccompBPF(switches::kRendererProcess)) { |
| 145 sandbox_status_flags_ |= kSandboxLinuxSeccompBPF; | 180 sandbox_status_flags_ |= kSandboxLinuxSeccompBPF; |
| 146 } | 181 } |
| 147 } | 182 } |
| 148 | 183 |
| 149 return sandbox_status_flags_; | 184 return sandbox_status_flags_; |
| 150 } | 185 } |
| 151 | 186 |
| 152 // Threads are counted via /proc/self/task. This is a little hairy because of | 187 // Threads are counted via /proc/self/task. This is a little hairy because of |
| 153 // PID namespaces and existing sandboxes, so "self" must really be used instead | 188 // PID namespaces and existing sandboxes, so "self" must really be used instead |
| 154 // of using the pid. | 189 // of using the pid. |
| 155 bool LinuxSandbox::IsSingleThreaded() const { | 190 bool LinuxSandbox::IsSingleThreaded() const { |
| 156 struct stat task_stat; | 191 bool is_single_threaded = false; |
| 157 int fstat_ret; | 192 int proc_self_task = OpenProcTaskFd(proc_fd_); |
| 158 if (proc_fd_ >= 0) { | 193 |
| 159 // If a handle to /proc is available, use it. This allows to bypass file | 194 // In Debug mode, it's mandatory to be able to count threads to catch bugs. |
| 160 // system restrictions. | |
| 161 fstat_ret = fstatat(proc_fd_, "self/task/", &task_stat, 0); | |
| 162 } else { | |
| 163 // Otherwise, make an attempt to access the file system directly. | |
| 164 fstat_ret = fstatat(AT_FDCWD, "/proc/self/task/", &task_stat, 0); | |
| 165 } | |
| 166 // In Debug mode, it's mandatory to be able to count threads to catch bugs. | |
| 167 #if !defined(NDEBUG) | 195 #if !defined(NDEBUG) |
| 168 // Using DCHECK here would be incorrect. DCHECK can be enabled in non | 196 // Using CHECK here since we want to check all the cases where |
| 169 // official release mode. | 197 // !defined(NDEBUG) |
| 170 CHECK_EQ(0, fstat_ret) << "Could not count threads, the sandbox was not " | 198 // gets built. |
| 171 << "pre-initialized properly."; | 199 CHECK_LE(0, proc_self_task) << "Could not count threads, the sandbox was not " |
| 200 << "pre-initialized properly."; |
| 172 #endif // !defined(NDEBUG) | 201 #endif // !defined(NDEBUG) |
| 173 if (fstat_ret) { | 202 |
| 203 if (proc_self_task < 0) { |
| 174 // Pretend to be monothreaded if it can't be determined (for instance the | 204 // Pretend to be monothreaded if it can't be determined (for instance the |
| 175 // setuid sandbox is already engaged but no proc_fd_ is available). | 205 // setuid sandbox is already engaged but no proc_fd_ is available). |
| 176 return true; | 206 is_single_threaded = true; |
| 207 } else { |
| 208 SafeScopedFD task_closer(&proc_self_task); |
| 209 is_single_threaded = |
| 210 sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task); |
| 177 } | 211 } |
| 178 | 212 |
| 179 // At least "..", "." and the current thread should be present. | 213 return is_single_threaded; |
| 180 CHECK_LE(3UL, task_stat.st_nlink); | |
| 181 // Counting threads via /proc/self/task could be racy. For the purpose of | |
| 182 // determining if the current proces is monothreaded it works: if at any | |
| 183 // time it becomes monothreaded, it'll stay so. | |
| 184 return task_stat.st_nlink == 3; | |
| 185 } | 214 } |
| 186 | 215 |
| 187 bool LinuxSandbox::seccomp_bpf_started() const { | 216 bool LinuxSandbox::seccomp_bpf_started() const { |
| 188 return seccomp_bpf_started_; | 217 return seccomp_bpf_started_; |
| 189 } | 218 } |
| 190 | 219 |
| 191 sandbox::SetuidSandboxClient* | 220 sandbox::SetuidSandboxClient* |
| 192 LinuxSandbox::setuid_sandbox_client() const { | 221 LinuxSandbox::setuid_sandbox_client() const { |
| 193 return setuid_sandbox_client_.get(); | 222 return setuid_sandbox_client_.get(); |
| 194 } | 223 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 279 |
| 251 // Attempt to limit the future size of the address space of the process. | 280 // Attempt to limit the future size of the address space of the process. |
| 252 LimitAddressSpace(process_type); | 281 LimitAddressSpace(process_type); |
| 253 | 282 |
| 254 // Try to enable seccomp-bpf. | 283 // Try to enable seccomp-bpf. |
| 255 bool seccomp_bpf_started = StartSeccompBPF(process_type); | 284 bool seccomp_bpf_started = StartSeccompBPF(process_type); |
| 256 | 285 |
| 257 return seccomp_bpf_started; | 286 return seccomp_bpf_started; |
| 258 } | 287 } |
| 259 | 288 |
| 289 void LinuxSandbox::StopThreadImpl(base::Thread* thread) { |
| 290 DCHECK(thread); |
| 291 StopThreadAndEnsureNotCounted(thread); |
| 292 } |
| 260 | 293 |
| 261 bool LinuxSandbox::seccomp_bpf_supported() const { | 294 bool LinuxSandbox::seccomp_bpf_supported() const { |
| 262 CHECK(pre_initialized_); | 295 CHECK(pre_initialized_); |
| 263 return seccomp_bpf_supported_; | 296 return seccomp_bpf_supported_; |
| 264 } | 297 } |
| 265 | 298 |
| 266 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) { | 299 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) { |
| 267 (void) process_type; | 300 (void) process_type; |
| 268 #if !defined(ADDRESS_SANITIZER) | 301 #if !defined(ADDRESS_SANITIZER) |
| 269 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 302 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 296 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); | 329 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); |
| 297 | 330 |
| 298 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit); | 331 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit); |
| 299 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize); | 332 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize); |
| 300 return limited_as && limited_data; | 333 return limited_as && limited_data; |
| 301 #else | 334 #else |
| 302 return false; | 335 return false; |
| 303 #endif // !defined(ADDRESS_SANITIZER) | 336 #endif // !defined(ADDRESS_SANITIZER) |
| 304 } | 337 } |
| 305 | 338 |
| 306 bool LinuxSandbox::HasOpenDirectories() { | 339 bool LinuxSandbox::HasOpenDirectories() const { |
| 307 return sandbox::Credentials().HasOpenDirectory(proc_fd_); | 340 return sandbox::Credentials().HasOpenDirectory(proc_fd_); |
| 308 } | 341 } |
| 309 | 342 |
| 310 void LinuxSandbox::SealSandbox() { | 343 void LinuxSandbox::SealSandbox() { |
| 311 if (proc_fd_ >= 0) { | 344 if (proc_fd_ >= 0) { |
| 312 int ret = IGNORE_EINTR(close(proc_fd_)); | 345 int ret = IGNORE_EINTR(close(proc_fd_)); |
| 313 CHECK_EQ(0, ret); | 346 CHECK_EQ(0, ret); |
| 314 proc_fd_ = -1; | 347 proc_fd_ = -1; |
| 315 } | 348 } |
| 316 } | 349 } |
| 317 | 350 |
| 318 void LinuxSandbox::CheckForBrokenPromises(const std::string& process_type) { | 351 void LinuxSandbox::CheckForBrokenPromises(const std::string& process_type) { |
| 319 // Make sure that any promise made with GetStatus() wasn't broken. | 352 // Make sure that any promise made with GetStatus() wasn't broken. |
| 320 bool promised_seccomp_bpf_would_start = false; | 353 bool promised_seccomp_bpf_would_start = false; |
| 321 if (process_type == switches::kRendererProcess || | 354 if (process_type == switches::kRendererProcess || |
| 322 process_type == switches::kWorkerProcess || | 355 process_type == switches::kWorkerProcess || |
| 323 process_type == switches::kPpapiPluginProcess) { | 356 process_type == switches::kPpapiPluginProcess) { |
| 324 promised_seccomp_bpf_would_start = | 357 promised_seccomp_bpf_would_start = |
| 325 (sandbox_status_flags_ != kSandboxLinuxInvalid) && | 358 (sandbox_status_flags_ != kSandboxLinuxInvalid) && |
| 326 (GetStatus() & kSandboxLinuxSeccompBPF); | 359 (GetStatus() & kSandboxLinuxSeccompBPF); |
| 327 } | 360 } |
| 328 if (promised_seccomp_bpf_would_start) { | 361 if (promised_seccomp_bpf_would_start) { |
| 329 CHECK(seccomp_bpf_started_); | 362 CHECK(seccomp_bpf_started_); |
| 330 } | 363 } |
| 331 } | 364 } |
| 332 | 365 |
| 366 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const { |
| 367 DCHECK(thread); |
| 368 int proc_self_task = OpenProcTaskFd(proc_fd_); |
| 369 PCHECK(proc_self_task >= 0); |
| 370 SafeScopedFD task_closer(&proc_self_task); |
| 371 CHECK( |
| 372 sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task, thread)); |
| 373 } |
| 374 |
| 333 } // namespace content | 375 } // namespace content |
| OLD | NEW |