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

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

Issue 885443002: Roll Chrome into Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase to ToT mojo Created 5 years, 10 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 | « sandbox/linux/services/credentials.h ('k') | 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 <stdio.h> 9 #include <stdio.h>
10 #include <sys/capability.h> 10 #include <sys/capability.h>
11 #include <sys/syscall.h> 11 #include <sys/syscall.h>
12 #include <sys/types.h> 12 #include <sys/types.h>
13 #include <sys/wait.h> 13 #include <sys/wait.h>
14 #include <unistd.h> 14 #include <unistd.h>
15 15
16 #include "base/basictypes.h" 16 #include "base/basictypes.h"
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/files/file_path.h" 18 #include "base/files/file_path.h"
19 #include "base/files/file_util.h" 19 #include "base/files/file_util.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/posix/eintr_wrapper.h" 21 #include "base/posix/eintr_wrapper.h"
22 #include "base/process/launch.h" 22 #include "base/process/launch.h"
23 #include "base/template_util.h" 23 #include "base/template_util.h"
24 #include "base/third_party/valgrind/valgrind.h" 24 #include "base/third_party/valgrind/valgrind.h"
25 #include "sandbox/linux/services/namespace_utils.h"
25 #include "sandbox/linux/services/syscall_wrappers.h" 26 #include "sandbox/linux/services/syscall_wrappers.h"
26 27
28 namespace sandbox {
29
27 namespace { 30 namespace {
28 31
29 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } 32 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; }
30 33
31 struct CapFreeDeleter { 34 struct CapFreeDeleter {
32 inline void operator()(cap_t cap) const { 35 inline void operator()(cap_t cap) const {
33 int ret = cap_free(cap); 36 int ret = cap_free(cap);
34 CHECK_EQ(0, ret); 37 CHECK_EQ(0, ret);
35 } 38 }
36 }; 39 };
37 40
38 // Wrapper to manage libcap2's cap_t type. 41 // Wrapper to manage libcap2's cap_t type.
39 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap; 42 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap;
40 43
41 struct CapTextFreeDeleter { 44 struct CapTextFreeDeleter {
42 inline void operator()(char* cap_text) const { 45 inline void operator()(char* cap_text) const {
43 int ret = cap_free(cap_text); 46 int ret = cap_free(cap_text);
44 CHECK_EQ(0, ret); 47 CHECK_EQ(0, ret);
45 } 48 }
46 }; 49 };
47 50
48 // Wrapper to manage the result from libcap2's cap_from_text(). 51 // Wrapper to manage the result from libcap2's cap_from_text().
49 typedef scoped_ptr<char, CapTextFreeDeleter> ScopedCapText; 52 typedef scoped_ptr<char, CapTextFreeDeleter> ScopedCapText;
50 53
51 struct FILECloser {
52 inline void operator()(FILE* f) const {
53 DCHECK(f);
54 PCHECK(0 == fclose(f));
55 }
56 };
57
58 // Don't use ScopedFILE in base since it doesn't check fclose().
59 // TODO(jln): fix base/.
60 typedef scoped_ptr<FILE, FILECloser> ScopedFILE;
61
62 static_assert((base::is_same<uid_t, gid_t>::value),
63 "uid_t and gid_t should be the same type");
64 // generic_id_t can be used for either uid_t or gid_t.
65 typedef uid_t generic_id_t;
66
67 // Write a uid or gid mapping from |id| to |id| in |map_file|.
68 bool WriteToIdMapFile(const char* map_file, generic_id_t id) {
69 ScopedFILE f(fopen(map_file, "w"));
70 PCHECK(f);
71 const uid_t inside_id = id;
72 const uid_t outside_id = id;
73 int num = fprintf(f.get(), "%d %d 1\n", inside_id, outside_id);
74 if (num < 0) return false;
75 // Manually call fflush() to catch permission failures.
76 int ret = fflush(f.get());
77 if (ret) {
78 VLOG(1) << "Could not write to id map file";
79 return false;
80 }
81 return true;
82 }
83
84 // Checks that the set of RES-uids and the set of RES-gids have 54 // Checks that the set of RES-uids and the set of RES-gids have
85 // one element each and return that element in |resuid| and |resgid| 55 // one element each and return that element in |resuid| and |resgid|
86 // respectively. It's ok to pass NULL as one or both of the ids. 56 // respectively. It's ok to pass NULL as one or both of the ids.
87 bool GetRESIds(uid_t* resuid, gid_t* resgid) { 57 bool GetRESIds(uid_t* resuid, gid_t* resgid) {
88 uid_t ruid, euid, suid; 58 uid_t ruid, euid, suid;
89 gid_t rgid, egid, sgid; 59 gid_t rgid, egid, sgid;
90 PCHECK(getresuid(&ruid, &euid, &suid) == 0); 60 PCHECK(getresuid(&ruid, &euid, &suid) == 0);
91 PCHECK(getresgid(&rgid, &egid, &sgid) == 0); 61 PCHECK(getresgid(&rgid, &egid, &sgid) == 0);
92 const bool uids_are_equal = (ruid == euid) && (ruid == suid); 62 const bool uids_are_equal = (ruid == euid) && (ruid == suid);
93 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); 63 const bool gids_are_equal = (rgid == egid) && (rgid == sgid);
94 if (!uids_are_equal || !gids_are_equal) return false; 64 if (!uids_are_equal || !gids_are_equal) return false;
95 if (resuid) *resuid = euid; 65 if (resuid) *resuid = euid;
96 if (resgid) *resgid = egid; 66 if (resgid) *resgid = egid;
97 return true; 67 return true;
98 } 68 }
99 69
70 const int kExitSuccess = 0;
71
72 int ChrootToSelfFdinfo(void*) {
73 RAW_CHECK(chroot("/proc/self/fdinfo/") == 0);
74
75 // CWD is essentially an implicit file descriptor, so be careful to not
76 // leave it behind.
77 RAW_CHECK(chdir("/") == 0);
78 _exit(kExitSuccess);
79 }
80
100 // chroot() to an empty dir that is "safe". To be safe, it must not contain 81 // chroot() to an empty dir that is "safe". To be safe, it must not contain
101 // any subdirectory (chroot-ing there would allow a chroot escape) and it must 82 // any subdirectory (chroot-ing there would allow a chroot escape) and it must
102 // be impossible to create an empty directory there. 83 // be impossible to create an empty directory there.
103 // We achieve this by doing the following: 84 // We achieve this by doing the following:
104 // 1. We create a new process sharing file system information. 85 // 1. We create a new process sharing file system information.
105 // 2. In the child, we chroot to /proc/self/fdinfo/ 86 // 2. In the child, we chroot to /proc/self/fdinfo/
106 // This is already "safe", since fdinfo/ does not contain another directory and 87 // This is already "safe", since fdinfo/ does not contain another directory and
107 // one cannot create another directory there. 88 // one cannot create another directory there.
108 // 3. The process dies 89 // 3. The process dies
109 // After (3) happens, the directory is not available anymore in /proc. 90 // After (3) happens, the directory is not available anymore in /proc.
110 bool ChrootToSafeEmptyDir() { 91 bool ChrootToSafeEmptyDir() {
111 // We do not use a thread because when we are in a PID namespace, we cannot 92 // We need to chroot to a fdinfo that is unique to a process and have that
112 // easily get a handle to the /proc/tid directory for the thread (since /proc 93 // process die.
113 // may not be aware of the PID namespace). With a process, we can just use 94 // 1. We don't want to simply fork() because duplicating the page tables is
114 // /proc/self. 95 // slow with a big address space.
115 pid_t pid = base::ForkWithFlags(SIGCHLD | CLONE_FS, nullptr, nullptr); 96 // 2. We do not use a regular thread (that would unshare CLONE_FILES) because
97 // when we are in a PID namespace, we cannot easily get a handle to the
98 // /proc/tid directory for the thread (since /proc may not be aware of the
99 // PID namespace). With a process, we can just use /proc/self.
100 pid_t pid = -1;
101 char stack_buf[PTHREAD_STACK_MIN];
102 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \
103 defined(ARCH_CPU_MIPS64_FAMILY) || defined(ARCH_CPU_MIPS_FAMILY)
104 // The stack grows downward.
105 void* stack = stack_buf + sizeof(stack_buf);
106 #else
107 #error "Unsupported architecture"
108 #endif
109 pid = clone(ChrootToSelfFdinfo, stack,
110 CLONE_VM | CLONE_VFORK | CLONE_FS | SIGCHLD, nullptr, nullptr,
111 nullptr, nullptr);
116 PCHECK(pid != -1); 112 PCHECK(pid != -1);
117 if (pid == 0) {
118 RAW_CHECK(chroot("/proc/self/fdinfo/") == 0);
119
120 // CWD is essentially an implicit file descriptor, so be careful to not
121 // leave it behind.
122 RAW_CHECK(chdir("/") == 0);
123 _exit(0);
124 }
125 113
126 int status = -1; 114 int status = -1;
127 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid); 115 PCHECK(HANDLE_EINTR(waitpid(pid, &status, 0)) == pid);
128 116
129 return status == 0; 117 return kExitSuccess == status;
130 } 118 }
131 119
132 // 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
133 // errno. 121 // errno.
134 void CheckCloneNewUserErrno(int error) { 122 void CheckCloneNewUserErrno(int error) {
135 // 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
136 // 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.
137 // Valgrind will ENOSYS unshare(). 125 // Valgrind will ENOSYS unshare().
138 PCHECK(error == EPERM || error == EUSERS || error == EINVAL || 126 PCHECK(error == EPERM || error == EUSERS || error == EINVAL ||
139 error == ENOSYS); 127 error == ENOSYS);
140 } 128 }
141 129
142 } // namespace. 130 } // namespace.
143 131
144 namespace sandbox {
145
146 bool Credentials::DropAllCapabilities() { 132 bool Credentials::DropAllCapabilities() {
147 ScopedCap cap(cap_init()); 133 ScopedCap cap(cap_init());
148 CHECK(cap); 134 CHECK(cap);
149 PCHECK(0 == cap_set_proc(cap.get())); 135 PCHECK(0 == cap_set_proc(cap.get()));
150 CHECK(!HasAnyCapability()); 136 CHECK(!HasAnyCapability());
151 // We never let this function fail. 137 // We never let this function fail.
152 return true; 138 return true;
153 } 139 }
154 140
155 bool Credentials::HasAnyCapability() { 141 bool Credentials::HasAnyCapability() {
156 ScopedCap current_cap(cap_get_proc()); 142 ScopedCap current_cap(cap_get_proc());
157 CHECK(current_cap); 143 CHECK(current_cap);
158 ScopedCap empty_cap(cap_init()); 144 ScopedCap empty_cap(cap_init());
159 CHECK(empty_cap); 145 CHECK(empty_cap);
160 return cap_compare(current_cap.get(), empty_cap.get()) != 0; 146 return cap_compare(current_cap.get(), empty_cap.get()) != 0;
161 } 147 }
162 148
163 scoped_ptr<std::string> Credentials::GetCurrentCapString() { 149 scoped_ptr<std::string> Credentials::GetCurrentCapString() {
164 ScopedCap current_cap(cap_get_proc()); 150 ScopedCap current_cap(cap_get_proc());
165 CHECK(current_cap); 151 CHECK(current_cap);
166 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL)); 152 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL));
167 CHECK(cap_text); 153 CHECK(cap_text);
168 return scoped_ptr<std::string> (new std::string(cap_text.get())); 154 return scoped_ptr<std::string> (new std::string(cap_text.get()));
169 } 155 }
170 156
171 // static 157 // static
172 bool Credentials::SupportsNewUserNS() { 158 bool Credentials::CanCreateProcessInNewUserNS() {
173 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), 159 // Valgrind will let clone(2) pass-through, but doesn't support unshare(),
174 // so always consider UserNS unsupported there. 160 // so always consider UserNS unsupported there.
175 if (IsRunningOnValgrind()) { 161 if (IsRunningOnValgrind()) {
176 return false; 162 return false;
177 } 163 }
178 164
179 // This is roughly a fork(). 165 // This is roughly a fork().
180 const pid_t pid = sys_clone(CLONE_NEWUSER | SIGCHLD, 0, 0, 0, 0); 166 const pid_t pid = sys_clone(CLONE_NEWUSER | SIGCHLD, 0, 0, 0, 0);
181 167
182 if (pid == -1) { 168 if (pid == -1) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 << "on this kernel."; 201 << "on this kernel.";
216 CheckCloneNewUserErrno(unshare_errno); 202 CheckCloneNewUserErrno(unshare_errno);
217 return false; 203 return false;
218 } 204 }
219 205
220 // The current {r,e,s}{u,g}id is now an overflow id (c.f. 206 // The current {r,e,s}{u,g}id is now an overflow id (c.f.
221 // /proc/sys/kernel/overflowuid). Setup the uid and gid maps. 207 // /proc/sys/kernel/overflowuid). Setup the uid and gid maps.
222 DCHECK(GetRESIds(NULL, NULL)); 208 DCHECK(GetRESIds(NULL, NULL));
223 const char kGidMapFile[] = "/proc/self/gid_map"; 209 const char kGidMapFile[] = "/proc/self/gid_map";
224 const char kUidMapFile[] = "/proc/self/uid_map"; 210 const char kUidMapFile[] = "/proc/self/uid_map";
225 CHECK(WriteToIdMapFile(kGidMapFile, gid)); 211 CHECK(NamespaceUtils::WriteToIdMapFile(kGidMapFile, gid));
226 CHECK(WriteToIdMapFile(kUidMapFile, uid)); 212 CHECK(NamespaceUtils::WriteToIdMapFile(kUidMapFile, uid));
227 DCHECK(GetRESIds(NULL, NULL)); 213 DCHECK(GetRESIds(NULL, NULL));
228 return true; 214 return true;
229 } 215 }
230 216
231 bool Credentials::DropFileSystemAccess() { 217 bool Credentials::DropFileSystemAccess() {
232 CHECK(ChrootToSafeEmptyDir()); 218 CHECK(ChrootToSafeEmptyDir());
233 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); 219 CHECK(!base::DirectoryExists(base::FilePath("/proc")));
234 // We never let this function fail. 220 // We never let this function fail.
235 return true; 221 return true;
236 } 222 }
237 223
238 } // namespace sandbox. 224 } // namespace sandbox.
OLDNEW
« no previous file with comments | « sandbox/linux/services/credentials.h ('k') | sandbox/linux/services/credentials_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698