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 <stdio.h> | 9 #include <stdio.h> |
10 #include <sys/capability.h> | 10 #include <sys/capability.h> |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 #error "Unsupported architecture" | 107 #error "Unsupported architecture" |
108 #endif | 108 #endif |
109 pid = clone(ChrootToSelfFdinfo, stack, | 109 pid = clone(ChrootToSelfFdinfo, stack, |
110 CLONE_VM | CLONE_VFORK | CLONE_FS | SIGCHLD, nullptr, nullptr, | 110 CLONE_VM | CLONE_VFORK | CLONE_FS | SIGCHLD, nullptr, nullptr, |
111 nullptr, nullptr); | 111 nullptr, nullptr); |
112 PCHECK(pid != -1); | 112 PCHECK(pid != -1); |
113 | 113 |
114 int status = -1; | 114 int status = -1; |
115 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); | 115 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); |
116 | 116 |
117 return kExitSuccess == status; | 117 return WIFEXITED(status) && WEXITSTATUS(status) == kExitSuccess; |
118 } | 118 } |
119 | 119 |
120 // 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 |
121 // errno. | 121 // errno. |
122 void CheckCloneNewUserErrno(int error) { | 122 void CheckCloneNewUserErrno(int error) { |
123 // EPERM can happen if already in a chroot. EUSERS if too many nested | 123 // EPERM can happen if already in a chroot. EUSERS if too many nested |
124 // namespaces are used. EINVAL for kernels that don't support the feature. | 124 // namespaces are used. EINVAL for kernels that don't support the feature. |
125 // Valgrind will ENOSYS unshare(). | 125 // Valgrind will ENOSYS unshare(). |
126 PCHECK(error == EPERM || error == EUSERS || error == EINVAL || | 126 PCHECK(error == EPERM || error == EUSERS || error == EINVAL || |
127 error == ENOSYS); | 127 error == ENOSYS); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 167 |
168 if (pid == -1) { | 168 if (pid == -1) { |
169 CheckCloneNewUserErrno(errno); | 169 CheckCloneNewUserErrno(errno); |
170 return false; | 170 return false; |
171 } | 171 } |
172 | 172 |
173 // The parent process could have had threads. In the child, these threads | 173 // The parent process could have had threads. In the child, these threads |
174 // have disappeared. Make sure to not do anything in the child, as this is a | 174 // have disappeared. Make sure to not do anything in the child, as this is a |
175 // fragile execution environment. | 175 // fragile execution environment. |
176 if (pid == 0) { | 176 if (pid == 0) { |
177 _exit(0); | 177 _exit(kExitSuccess); |
178 } | 178 } |
179 | 179 |
180 // Always reap the child. | 180 // Always reap the child. |
181 siginfo_t infop; | 181 int status = -1; |
182 PCHECK(0 == HANDLE_EINTR(waitid(P_PID, pid, &infop, WEXITED))); | 182 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); |
| 183 CHECK(WIFEXITED(status)); |
| 184 CHECK_EQ(kExitSuccess, WEXITSTATUS(status)); |
183 | 185 |
184 // clone(2) succeeded, we can use CLONE_NEWUSER. | 186 // clone(2) succeeded, we can use CLONE_NEWUSER. |
185 return true; | 187 return true; |
186 } | 188 } |
187 | 189 |
188 bool Credentials::MoveToNewUserNS() { | 190 bool Credentials::MoveToNewUserNS() { |
189 uid_t uid; | 191 uid_t uid; |
190 gid_t gid; | 192 gid_t gid; |
191 if (!GetRESIds(&uid, &gid)) { | 193 if (!GetRESIds(&uid, &gid)) { |
192 // If all the uids (or gids) are not equal to each other, the security | 194 // If all the uids (or gids) are not equal to each other, the security |
(...skipping 26 matching lines...) Expand all Loading... |
219 } | 221 } |
220 | 222 |
221 bool Credentials::DropFileSystemAccess() { | 223 bool Credentials::DropFileSystemAccess() { |
222 CHECK(ChrootToSafeEmptyDir()); | 224 CHECK(ChrootToSafeEmptyDir()); |
223 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); | 225 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); |
224 // We never let this function fail. | 226 // We never let this function fail. |
225 return true; | 227 return true; |
226 } | 228 } |
227 | 229 |
228 } // namespace sandbox. | 230 } // namespace sandbox. |
OLD | NEW |