Chromium Code Reviews| 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 int ChrootToSelfFdinfo(void*) { | 56 int ChrootToSelfFdinfo(void*) { |
|
jln (very slow on Chromium)
2015/07/21 23:39:17
Maybe add a comment here saying that this function
rickyz (no longer on Chrome)
2015/07/22 04:00:32
Done.
| |
| 57 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); | 57 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); |
| 58 | 58 |
| 59 // 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 |
| 60 // leave it behind. | 60 // leave it behind. |
| 61 RAW_CHECK(chdir("/") == 0); | 61 RAW_CHECK(chdir("/") == 0); |
| 62 _exit(kExitSuccess); | 62 _exit(kExitSuccess); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // 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 |
| 66 // 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... | |
| 84 pid_t pid = -1; | 84 pid_t pid = -1; |
| 85 char stack_buf[PTHREAD_STACK_MIN]; | 85 char stack_buf[PTHREAD_STACK_MIN]; |
| 86 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ | 86 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ |
| 87 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) | 87 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) |
| 88 // The stack grows downward. | 88 // The stack grows downward. |
| 89 void* stack = stack_buf + sizeof(stack_buf); | 89 void* stack = stack_buf + sizeof(stack_buf); |
| 90 #else | 90 #else |
| 91 #error "Unsupported architecture" | 91 #error "Unsupported architecture" |
| 92 #endif | 92 #endif |
| 93 | 93 |
| 94 pid = clone(ChrootToSelfFdinfo, stack, | 94 int clone_flags = CLONE_FS | LINUX_SIGCHLD; |
| 95 CLONE_VM | CLONE_VFORK | CLONE_FS | LINUX_SIGCHLD, nullptr, | 95 void *tls = nullptr; |
| 96 nullptr, nullptr, nullptr); | 96 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_MIPS64_FAMILY) || \ |
| 97 defined(ARCH_CPU_MIPS_FAMILY) | |
|
jln (very slow on Chromium)
2015/07/22 00:04:20
Let's just exclude MIPS here. We've not tested the
rickyz (no longer on Chrome)
2015/07/22 04:00:32
Done - also noticed that I forgot to add ARM in th
| |
| 98 // Use CLONE_VM | CLONE_VFORK as an optimization to avoid copying page tables. | |
| 99 // Since clone writes to the new child's TLS before returning, we must set a | |
| 100 // new TLS to avoid corrupting the current process's TLS. On ARCH_CPU_X86, | |
| 101 // glibc performs syscalls by calling a function pointer in TLS, so we do not | |
| 102 // attempt this optimization. | |
| 103 clone_flags |= CLONE_VM | CLONE_VFORK | CLONE_SETTLS; | |
| 104 | |
| 105 // The stack grows down on these architectures. Place the TLS at the bottom of | |
|
jln (very slow on Chromium)
2015/07/21 23:39:17
"bottom of the stack" is very confusing here. I wo
rickyz (no longer on Chrome)
2015/07/22 04:00:32
Yeah, I ended up switching this to a zero-initiali
| |
| 106 // the stack. | |
| 107 tls = stack_buf; | |
| 108 #endif | |
| 109 | |
| 110 pid = clone(ChrootToSelfFdinfo, stack, clone_flags, nullptr, nullptr, tls, | |
| 111 nullptr); | |
| 97 PCHECK(pid != -1); | 112 PCHECK(pid != -1); |
| 98 | 113 |
| 99 int status = -1; | 114 int status = -1; |
| 100 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); | 115 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); |
| 101 | 116 |
| 102 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; | 117 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; |
| 103 } | 118 } |
| 104 | 119 |
| 105 // CHECK() that an attempt to move to a new user namespace raised an expected | 120 // CHECK() that an attempt to move to a new user namespace raised an expected |
| 106 // errno. | 121 // errno. |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 if (pid != 0) { | 316 if (pid != 0) { |
| 302 return pid; | 317 return pid; |
| 303 } | 318 } |
| 304 | 319 |
| 305 // Since we just forked, we are single threaded. | 320 // Since we just forked, we are single threaded. |
| 306 PCHECK(DropAllCapabilitiesOnCurrentThread()); | 321 PCHECK(DropAllCapabilitiesOnCurrentThread()); |
| 307 return 0; | 322 return 0; |
| 308 } | 323 } |
| 309 | 324 |
| 310 } // namespace sandbox. | 325 } // namespace sandbox. |
| OLD | NEW |