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

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

Issue 849893004: Move a couple of utility functions to a new namespace_utils class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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
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/logging.h" 18 #include "base/logging.h"
19 #include "base/posix/eintr_wrapper.h" 19 #include "base/posix/eintr_wrapper.h"
20 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
21 #include "base/template_util.h" 21 #include "base/template_util.h"
22 #include "base/third_party/valgrind/valgrind.h" 22 #include "base/third_party/valgrind/valgrind.h"
23 #include "base/threading/thread.h" 23 #include "base/threading/thread.h"
24 #include "sandbox/linux/services/namespace_utils.h"
24 #include "sandbox/linux/services/syscall_wrappers.h" 25 #include "sandbox/linux/services/syscall_wrappers.h"
25 26
26 namespace { 27 namespace {
27 28
28 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; }
29
30 struct CapFreeDeleter { 29 struct CapFreeDeleter {
31 inline void operator()(cap_t cap) const { 30 inline void operator()(cap_t cap) const {
32 int ret = cap_free(cap); 31 int ret = cap_free(cap);
33 CHECK_EQ(0, ret); 32 CHECK_EQ(0, ret);
34 } 33 }
35 }; 34 };
36 35
37 // Wrapper to manage libcap2's cap_t type. 36 // Wrapper to manage libcap2's cap_t type.
38 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap; 37 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap;
39 38
(...skipping 11 matching lines...) Expand all
51 inline void operator()(FILE* f) const { 50 inline void operator()(FILE* f) const {
52 DCHECK(f); 51 DCHECK(f);
53 PCHECK(0 == fclose(f)); 52 PCHECK(0 == fclose(f));
54 } 53 }
55 }; 54 };
56 55
57 // Don't use ScopedFILE in base since it doesn't check fclose(). 56 // Don't use ScopedFILE in base since it doesn't check fclose().
58 // TODO(jln): fix base/. 57 // TODO(jln): fix base/.
59 typedef scoped_ptr<FILE, FILECloser> ScopedFILE; 58 typedef scoped_ptr<FILE, FILECloser> ScopedFILE;
60 59
61 static_assert((base::is_same<uid_t, gid_t>::value),
62 "uid_t and gid_t should be the same type");
63 // generic_id_t can be used for either uid_t or gid_t.
64 typedef uid_t generic_id_t;
65
66 // Write a uid or gid mapping from |id| to |id| in |map_file|.
67 bool WriteToIdMapFile(const char* map_file, generic_id_t id) {
68 ScopedFILE f(fopen(map_file, "w"));
69 PCHECK(f);
70 const uid_t inside_id = id;
71 const uid_t outside_id = id;
72 int num = fprintf(f.get(), "%d %d 1\n", inside_id, outside_id);
73 if (num < 0) return false;
74 // Manually call fflush() to catch permission failures.
75 int ret = fflush(f.get());
76 if (ret) {
77 VLOG(1) << "Could not write to id map file";
78 return false;
79 }
80 return true;
81 }
82
83 // Checks that the set of RES-uids and the set of RES-gids have 60 // Checks that the set of RES-uids and the set of RES-gids have
84 // one element each and return that element in |resuid| and |resgid| 61 // one element each and return that element in |resuid| and |resgid|
85 // respectively. It's ok to pass NULL as one or both of the ids. 62 // respectively. It's ok to pass NULL as one or both of the ids.
86 bool GetRESIds(uid_t* resuid, gid_t* resgid) { 63 bool GetRESIds(uid_t* resuid, gid_t* resgid) {
87 uid_t ruid, euid, suid; 64 uid_t ruid, euid, suid;
88 gid_t rgid, egid, sgid; 65 gid_t rgid, egid, sgid;
89 PCHECK(getresuid(&ruid, &euid, &suid) == 0); 66 PCHECK(getresuid(&ruid, &euid, &suid) == 0);
90 PCHECK(getresgid(&rgid, &egid, &sgid) == 0); 67 PCHECK(getresgid(&rgid, &egid, &sgid) == 0);
91 const bool uids_are_equal = (ruid == euid) && (ruid == suid); 68 const bool uids_are_equal = (ruid == euid) && (ruid == suid);
92 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); 69 const bool gids_are_equal = (rgid == egid) && (rgid == sgid);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } 152 }
176 153
177 scoped_ptr<std::string> Credentials::GetCurrentCapString() { 154 scoped_ptr<std::string> Credentials::GetCurrentCapString() {
178 ScopedCap current_cap(cap_get_proc()); 155 ScopedCap current_cap(cap_get_proc());
179 CHECK(current_cap); 156 CHECK(current_cap);
180 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL)); 157 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL));
181 CHECK(cap_text); 158 CHECK(cap_text);
182 return scoped_ptr<std::string> (new std::string(cap_text.get())); 159 return scoped_ptr<std::string> (new std::string(cap_text.get()));
183 } 160 }
184 161
185 // static
186 bool Credentials::SupportsNewUserNS() {
187 // Valgrind will let clone(2) pass-through, but doesn't support unshare(),
188 // so always consider UserNS unsupported there.
189 if (IsRunningOnValgrind()) {
190 return false;
191 }
192
193 // This is roughly a fork().
194 const pid_t pid = sys_clone(CLONE_NEWUSER | SIGCHLD, 0, 0, 0, 0);
195
196 if (pid == -1) {
197 CheckCloneNewUserErrno(errno);
198 return false;
199 }
200
201 // The parent process could have had threads. In the child, these threads
202 // have disappeared. Make sure to not do anything in the child, as this is a
203 // fragile execution environment.
204 if (pid == 0) {
205 _exit(0);
206 }
207
208 // Always reap the child.
209 siginfo_t infop;
210 PCHECK(0 == HANDLE_EINTR(waitid(P_PID, pid, &infop, WEXITED)));
211
212 // clone(2) succeeded, we can use CLONE_NEWUSER.
213 return true;
214 }
215
216 bool Credentials::MoveToNewUserNS() { 162 bool Credentials::MoveToNewUserNS() {
217 uid_t uid; 163 uid_t uid;
218 gid_t gid; 164 gid_t gid;
219 if (!GetRESIds(&uid, &gid)) { 165 if (!GetRESIds(&uid, &gid)) {
220 // If all the uids (or gids) are not equal to each other, the security 166 // If all the uids (or gids) are not equal to each other, the security
221 // model will most likely confuse the caller, abort. 167 // model will most likely confuse the caller, abort.
222 DVLOG(1) << "uids or gids differ!"; 168 DVLOG(1) << "uids or gids differ!";
223 return false; 169 return false;
224 } 170 }
225 int ret = unshare(CLONE_NEWUSER); 171 int ret = unshare(CLONE_NEWUSER);
226 if (ret) { 172 if (ret) {
227 const int unshare_errno = errno; 173 const int unshare_errno = errno;
228 VLOG(1) << "Looks like unprivileged CLONE_NEWUSER may not be available " 174 VLOG(1) << "Looks like unprivileged CLONE_NEWUSER may not be available "
229 << "on this kernel."; 175 << "on this kernel.";
230 CheckCloneNewUserErrno(unshare_errno); 176 CheckCloneNewUserErrno(unshare_errno);
231 return false; 177 return false;
232 } 178 }
233 179
234 // The current {r,e,s}{u,g}id is now an overflow id (c.f. 180 // The current {r,e,s}{u,g}id is now an overflow id (c.f.
235 // /proc/sys/kernel/overflowuid). Setup the uid and gid maps. 181 // /proc/sys/kernel/overflowuid). Setup the uid and gid maps.
236 DCHECK(GetRESIds(NULL, NULL)); 182 DCHECK(GetRESIds(NULL, NULL));
237 const char kGidMapFile[] = "/proc/self/gid_map"; 183 const char kGidMapFile[] = "/proc/self/gid_map";
238 const char kUidMapFile[] = "/proc/self/uid_map"; 184 const char kUidMapFile[] = "/proc/self/uid_map";
239 CHECK(WriteToIdMapFile(kGidMapFile, gid)); 185 CHECK(NamespaceUtils::WriteToIdMapFile(kGidMapFile, gid));
240 CHECK(WriteToIdMapFile(kUidMapFile, uid)); 186 CHECK(NamespaceUtils::WriteToIdMapFile(kUidMapFile, uid));
241 DCHECK(GetRESIds(NULL, NULL)); 187 DCHECK(GetRESIds(NULL, NULL));
242 return true; 188 return true;
243 } 189 }
244 190
245 bool Credentials::DropFileSystemAccess() { 191 bool Credentials::DropFileSystemAccess() {
246 return ChrootToSafeEmptyDir(); 192 return ChrootToSafeEmptyDir();
247 } 193 }
248 194
249 } // namespace sandbox. 195 } // namespace sandbox.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698