| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 int ChrootToSelfFdinfo(void*) { | 56 int ChrootToSelfFdinfo(void*) { |
| 57 // This function can be run from a vforked child, so it should not write to | |
| 58 // any memory other than the stack or errno. Reads from TLS may be different | |
| 59 // from in the parent process. | |
| 60 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); | 57 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); |
| 61 | 58 |
| 62 // CWD is essentially an implicit file descriptor, so be careful to not | 59 // CWD is essentially an implicit file descriptor, so be careful to not |
| 63 // leave it behind. | 60 // leave it behind. |
| 64 RAW_CHECK(chdir("/") == 0); | 61 RAW_CHECK(chdir("/") == 0); |
| 65 _exit(kExitSuccess); | 62 _exit(kExitSuccess); |
| 66 } | 63 } |
| 67 | 64 |
| 68 // chroot() to an empty dir that is "safe". To be safe, it must not contain | 65 // chroot() to an empty dir that is "safe". To be safe, it must not contain |
| 69 // any subdirectory (chroot-ing there would allow a chroot escape) and it must | 66 // any subdirectory (chroot-ing there would allow a chroot escape) and it must |
| (...skipping 17 matching lines...) Expand all Loading... |
| 87 pid_t pid = -1; | 84 pid_t pid = -1; |
| 88 char stack_buf[PTHREAD_STACK_MIN]; | 85 char stack_buf[PTHREAD_STACK_MIN]; |
| 89 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ | 86 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ |
| 90 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) | 87 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) |
| 91 // The stack grows downward. | 88 // The stack grows downward. |
| 92 void* stack = stack_buf + sizeof(stack_buf); | 89 void* stack = stack_buf + sizeof(stack_buf); |
| 93 #else | 90 #else |
| 94 #error "Unsupported architecture" | 91 #error "Unsupported architecture" |
| 95 #endif | 92 #endif |
| 96 | 93 |
| 97 int clone_flags = CLONE_FS | LINUX_SIGCHLD; | 94 pid = clone(ChrootToSelfFdinfo, stack, |
| 98 void* tls = nullptr; | 95 CLONE_VM | CLONE_VFORK | CLONE_FS | LINUX_SIGCHLD, nullptr, |
| 99 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM_FAMILY) | 96 nullptr, nullptr, nullptr); |
| 100 // Use CLONE_VM | CLONE_VFORK as an optimization to avoid copying page tables. | |
| 101 // Since clone writes to the new child's TLS before returning, we must set a | |
| 102 // new TLS to avoid corrupting the current process's TLS. On ARCH_CPU_X86, | |
| 103 // glibc performs syscalls by calling a function pointer in TLS, so we do not | |
| 104 // attempt this optimization. | |
| 105 clone_flags |= CLONE_VM | CLONE_VFORK | CLONE_SETTLS; | |
| 106 | |
| 107 char tls_buf[PTHREAD_STACK_MIN] = {0}; | |
| 108 tls = tls_buf; | |
| 109 #endif | |
| 110 | |
| 111 pid = clone(ChrootToSelfFdinfo, stack, clone_flags, nullptr, nullptr, tls, | |
| 112 nullptr); | |
| 113 PCHECK(pid != -1); | 97 PCHECK(pid != -1); |
| 114 | 98 |
| 115 int status = -1; | 99 int status = -1; |
| 116 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); | 100 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); |
| 117 | 101 |
| 118 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; | 102 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; |
| 119 } | 103 } |
| 120 | 104 |
| 121 // CHECK() that an attempt to move to a new user namespace raised an expected | 105 // CHECK() that an attempt to move to a new user namespace raised an expected |
| 122 // errno. | 106 // errno. |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 if (pid != 0) { | 301 if (pid != 0) { |
| 318 return pid; | 302 return pid; |
| 319 } | 303 } |
| 320 | 304 |
| 321 // Since we just forked, we are single threaded. | 305 // Since we just forked, we are single threaded. |
| 322 PCHECK(DropAllCapabilitiesOnCurrentThread()); | 306 PCHECK(DropAllCapabilitiesOnCurrentThread()); |
| 323 return 0; | 307 return 0; |
| 324 } | 308 } |
| 325 | 309 |
| 326 } // namespace sandbox. | 310 } // namespace sandbox. |
| OLD | NEW |