| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/credentials.h" | 5 #include "sandbox/linux/services/credentials.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <signal.h> | 8 #include <signal.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 const bool uids_are_equal = (ruid == euid) && (ruid == suid); | 46 const bool uids_are_equal = (ruid == euid) && (ruid == suid); |
| 47 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); | 47 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); |
| 48 if (!uids_are_equal || !gids_are_equal) return false; | 48 if (!uids_are_equal || !gids_are_equal) return false; |
| 49 if (resuid) *resuid = euid; | 49 if (resuid) *resuid = euid; |
| 50 if (resgid) *resgid = egid; | 50 if (resgid) *resgid = egid; |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 | 53 |
| 54 const int kExitSuccess = 0; | 54 const int kExitSuccess = 0; |
| 55 | 55 |
| 56 #if defined(__clang__) |
| 57 // Disable sanitizers that rely on TLS and may write to non-stack memory. |
| 58 __attribute__((no_sanitize_address)) |
| 59 __attribute__((no_sanitize_thread)) |
| 60 __attribute__((no_sanitize_memory)) |
| 61 #endif |
| 56 int ChrootToSelfFdinfo(void*) { | 62 int ChrootToSelfFdinfo(void*) { |
| 63 // This function can be run from a vforked child, so it should not write to |
| 64 // any memory other than the stack or errno. Reads from TLS may be different |
| 65 // from in the parent process. |
| 57 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); | 66 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); |
| 58 | 67 |
| 59 // CWD is essentially an implicit file descriptor, so be careful to not | 68 // CWD is essentially an implicit file descriptor, so be careful to not |
| 60 // leave it behind. | 69 // leave it behind. |
| 61 RAW_CHECK(chdir("/") == 0); | 70 RAW_CHECK(chdir("/") == 0); |
| 62 _exit(kExitSuccess); | 71 _exit(kExitSuccess); |
| 63 } | 72 } |
| 64 | 73 |
| 65 // chroot() to an empty dir that is "safe". To be safe, it must not contain | 74 // chroot() to an empty dir that is "safe". To be safe, it must not contain |
| 66 // any subdirectory (chroot-ing there would allow a chroot escape) and it must | 75 // any subdirectory (chroot-ing there would allow a chroot escape) and it must |
| (...skipping 17 matching lines...) Expand all Loading... |
| 84 pid_t pid = -1; | 93 pid_t pid = -1; |
| 85 char stack_buf[PTHREAD_STACK_MIN]; | 94 char stack_buf[PTHREAD_STACK_MIN]; |
| 86 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ | 95 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ |
| 87 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) | 96 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) |
| 88 // The stack grows downward. | 97 // The stack grows downward. |
| 89 void* stack = stack_buf + sizeof(stack_buf); | 98 void* stack = stack_buf + sizeof(stack_buf); |
| 90 #else | 99 #else |
| 91 #error "Unsupported architecture" | 100 #error "Unsupported architecture" |
| 92 #endif | 101 #endif |
| 93 | 102 |
| 94 pid = clone(ChrootToSelfFdinfo, stack, | 103 int clone_flags = CLONE_FS | LINUX_SIGCHLD; |
| 95 CLONE_VM | CLONE_VFORK | CLONE_FS | LINUX_SIGCHLD, nullptr, | 104 void* tls = nullptr; |
| 96 nullptr, nullptr, nullptr); | 105 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM_FAMILY) |
| 106 // Use CLONE_VM | CLONE_VFORK as an optimization to avoid copying page tables. |
| 107 // Since clone writes to the new child's TLS before returning, we must set a |
| 108 // new TLS to avoid corrupting the current process's TLS. On ARCH_CPU_X86, |
| 109 // glibc performs syscalls by calling a function pointer in TLS, so we do not |
| 110 // attempt this optimization. |
| 111 clone_flags |= CLONE_VM | CLONE_VFORK | CLONE_SETTLS; |
| 112 |
| 113 char tls_buf[PTHREAD_STACK_MIN] = {0}; |
| 114 tls = tls_buf; |
| 115 #endif |
| 116 |
| 117 pid = clone(ChrootToSelfFdinfo, stack, clone_flags, nullptr, nullptr, tls, |
| 118 nullptr); |
| 97 PCHECK(pid != -1); | 119 PCHECK(pid != -1); |
| 98 | 120 |
| 99 int status = -1; | 121 int status = -1; |
| 100 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); | 122 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); |
| 101 | 123 |
| 102 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; | 124 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; |
| 103 } | 125 } |
| 104 | 126 |
| 105 // CHECK() that an attempt to move to a new user namespace raised an expected | 127 // CHECK() that an attempt to move to a new user namespace raised an expected |
| 106 // errno. | 128 // errno. |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 bool Credentials::DropFileSystemAccess(int proc_fd) { | 311 bool Credentials::DropFileSystemAccess(int proc_fd) { |
| 290 CHECK_LE(0, proc_fd); | 312 CHECK_LE(0, proc_fd); |
| 291 | 313 |
| 292 CHECK(ChrootToSafeEmptyDir()); | 314 CHECK(ChrootToSafeEmptyDir()); |
| 293 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); | 315 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); |
| 294 CHECK(!ProcUtil::HasOpenDirectory(proc_fd)); | 316 CHECK(!ProcUtil::HasOpenDirectory(proc_fd)); |
| 295 // We never let this function fail. | 317 // We never let this function fail. |
| 296 return true; | 318 return true; |
| 297 } | 319 } |
| 298 | 320 |
| 321 pid_t Credentials::ForkAndDropCapabilitiesInChild() { |
| 322 pid_t pid = fork(); |
| 323 if (pid != 0) { |
| 324 return pid; |
| 325 } |
| 326 |
| 327 // Since we just forked, we are single threaded. |
| 328 PCHECK(DropAllCapabilitiesOnCurrentThread()); |
| 329 return 0; |
| 330 } |
| 331 |
| 299 } // namespace sandbox. | 332 } // namespace sandbox. |
| OLD | NEW |