Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: sandbox/linux/services/credentials.cc

Issue 1248673004: Set a new TLS when using CLONE_VM. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to comments. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sandbox/linux/services/credentials_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
57 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0); 60 RAW_CHECK(sys_chroot("/proc/self/fdinfo/") == 0);
58 61
59 // CWD is essentially an implicit file descriptor, so be careful to not 62 // CWD is essentially an implicit file descriptor, so be careful to not
60 // leave it behind. 63 // leave it behind.
61 RAW_CHECK(chdir("/") == 0); 64 RAW_CHECK(chdir("/") == 0);
62 _exit(kExitSuccess); 65 _exit(kExitSuccess);
63 } 66 }
64 67
65 // chroot() to an empty dir that is "safe". To be safe, it must not contain 68 // 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 69 // any subdirectory (chroot-ing there would allow a chroot escape) and it must
(...skipping 17 matching lines...) Expand all
84 pid_t pid = -1; 87 pid_t pid = -1;
85 char stack_buf[PTHREAD_STACK_MIN]; 88 char stack_buf[PTHREAD_STACK_MIN];
86 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \ 89 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \
87 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY) 90 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY)
88 // The stack grows downward. 91 // The stack grows downward.
89 void* stack = stack_buf + sizeof(stack_buf); 92 void* stack = stack_buf + sizeof(stack_buf);
90 #else 93 #else
91 #error "Unsupported architecture" 94 #error "Unsupported architecture"
92 #endif 95 #endif
93 96
94 pid = clone(ChrootToSelfFdinfo, stack, 97 int clone_flags = CLONE_FS | LINUX_SIGCHLD;
95 CLONE_VM | CLONE_VFORK | CLONE_FS | LINUX_SIGCHLD, nullptr, 98 void* tls = nullptr;
96 nullptr, nullptr, nullptr); 99 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM_FAMILY)
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);
97 PCHECK(pid != -1); 113 PCHECK(pid != -1);
98 114
99 int status = -1; 115 int status = -1;
100 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); 116 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid);
101 117
102 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; 118 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess;
103 } 119 }
104 120
105 // CHECK() that an attempt to move to a new user namespace raised an expected 121 // CHECK() that an attempt to move to a new user namespace raised an expected
106 // errno. 122 // errno.
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 if (pid != 0) { 317 if (pid != 0) {
302 return pid; 318 return pid;
303 } 319 }
304 320
305 // Since we just forked, we are single threaded. 321 // Since we just forked, we are single threaded.
306 PCHECK(DropAllCapabilitiesOnCurrentThread()); 322 PCHECK(DropAllCapabilitiesOnCurrentThread());
307 return 0; 323 return 0;
308 } 324 }
309 325
310 } // namespace sandbox. 326 } // namespace sandbox.
OLDNEW
« no previous file with comments | « no previous file | sandbox/linux/services/credentials_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698