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

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: Rename to SupportsUnprivilegedNamespace, which always checks for user namespace support 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/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
27 namespace { 28 namespace {
28 29
29 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; }
30
31 struct CapFreeDeleter { 30 struct CapFreeDeleter {
32 inline void operator()(cap_t cap) const { 31 inline void operator()(cap_t cap) const {
33 int ret = cap_free(cap); 32 int ret = cap_free(cap);
34 CHECK_EQ(0, ret); 33 CHECK_EQ(0, ret);
35 } 34 }
36 }; 35 };
37 36
38 // Wrapper to manage libcap2's cap_t type. 37 // Wrapper to manage libcap2's cap_t type.
39 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap; 38 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap;
40 39
41 struct CapTextFreeDeleter { 40 struct CapTextFreeDeleter {
42 inline void operator()(char* cap_text) const { 41 inline void operator()(char* cap_text) const {
43 int ret = cap_free(cap_text); 42 int ret = cap_free(cap_text);
44 CHECK_EQ(0, ret); 43 CHECK_EQ(0, ret);
45 } 44 }
46 }; 45 };
47 46
48 // Wrapper to manage the result from libcap2's cap_from_text(). 47 // Wrapper to manage the result from libcap2's cap_from_text().
49 typedef scoped_ptr<char, CapTextFreeDeleter> ScopedCapText; 48 typedef scoped_ptr<char, CapTextFreeDeleter> ScopedCapText;
50 49
51 struct FILECloser { 50 struct FILECloser {
jln (very slow on Chromium) 2015/01/23 02:48:23 Remove
rickyz (no longer on Chrome) 2015/01/23 23:59:37 Done.
52 inline void operator()(FILE* f) const { 51 inline void operator()(FILE* f) const {
53 DCHECK(f); 52 DCHECK(f);
54 PCHECK(0 == fclose(f)); 53 PCHECK(0 == fclose(f));
55 } 54 }
56 }; 55 };
57 56
58 // Don't use ScopedFILE in base since it doesn't check fclose(). 57 // Don't use ScopedFILE in base since it doesn't check fclose().
59 // TODO(jln): fix base/. 58 // TODO(jln): fix base/.
60 typedef scoped_ptr<FILE, FILECloser> ScopedFILE; 59 typedef scoped_ptr<FILE, FILECloser> ScopedFILE;
61 60
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 61 // 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| 62 // 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. 63 // respectively. It's ok to pass NULL as one or both of the ids.
87 bool GetRESIds(uid_t* resuid, gid_t* resgid) { 64 bool GetRESIds(uid_t* resuid, gid_t* resgid) {
88 uid_t ruid, euid, suid; 65 uid_t ruid, euid, suid;
89 gid_t rgid, egid, sgid; 66 gid_t rgid, egid, sgid;
90 PCHECK(getresuid(&ruid, &euid, &suid) == 0); 67 PCHECK(getresuid(&ruid, &euid, &suid) == 0);
91 PCHECK(getresgid(&rgid, &egid, &sgid) == 0); 68 PCHECK(getresgid(&rgid, &egid, &sgid) == 0);
92 const bool uids_are_equal = (ruid == euid) && (ruid == suid); 69 const bool uids_are_equal = (ruid == euid) && (ruid == suid);
93 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); 70 const bool gids_are_equal = (rgid == egid) && (rgid == sgid);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 } 138 }
162 139
163 scoped_ptr<std::string> Credentials::GetCurrentCapString() { 140 scoped_ptr<std::string> Credentials::GetCurrentCapString() {
164 ScopedCap current_cap(cap_get_proc()); 141 ScopedCap current_cap(cap_get_proc());
165 CHECK(current_cap); 142 CHECK(current_cap);
166 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL)); 143 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL));
167 CHECK(cap_text); 144 CHECK(cap_text);
168 return scoped_ptr<std::string> (new std::string(cap_text.get())); 145 return scoped_ptr<std::string> (new std::string(cap_text.get()));
169 } 146 }
170 147
171 // static
172 bool Credentials::SupportsNewUserNS() {
173 // Valgrind will let clone(2) pass-through, but doesn't support unshare(),
174 // so always consider UserNS unsupported there.
175 if (IsRunningOnValgrind()) {
176 return false;
177 }
178
179 // This is roughly a fork().
180 const pid_t pid = sys_clone(CLONE_NEWUSER | SIGCHLD, 0, 0, 0, 0);
181
182 if (pid == -1) {
183 CheckCloneNewUserErrno(errno);
184 return false;
185 }
186
187 // The parent process could have had threads. In the child, these threads
188 // have disappeared. Make sure to not do anything in the child, as this is a
189 // fragile execution environment.
190 if (pid == 0) {
191 _exit(0);
192 }
193
194 // Always reap the child.
195 siginfo_t infop;
196 PCHECK(0 == HANDLE_EINTR(waitid(P_PID, pid, &infop, WEXITED)));
197
198 // clone(2) succeeded, we can use CLONE_NEWUSER.
199 return true;
200 }
201
202 bool Credentials::MoveToNewUserNS() { 148 bool Credentials::MoveToNewUserNS() {
203 uid_t uid; 149 uid_t uid;
204 gid_t gid; 150 gid_t gid;
205 if (!GetRESIds(&uid, &gid)) { 151 if (!GetRESIds(&uid, &gid)) {
206 // If all the uids (or gids) are not equal to each other, the security 152 // If all the uids (or gids) are not equal to each other, the security
207 // model will most likely confuse the caller, abort. 153 // model will most likely confuse the caller, abort.
208 DVLOG(1) << "uids or gids differ!"; 154 DVLOG(1) << "uids or gids differ!";
209 return false; 155 return false;
210 } 156 }
211 int ret = unshare(CLONE_NEWUSER); 157 int ret = unshare(CLONE_NEWUSER);
212 if (ret) { 158 if (ret) {
213 const int unshare_errno = errno; 159 const int unshare_errno = errno;
214 VLOG(1) << "Looks like unprivileged CLONE_NEWUSER may not be available " 160 VLOG(1) << "Looks like unprivileged CLONE_NEWUSER may not be available "
215 << "on this kernel."; 161 << "on this kernel.";
216 CheckCloneNewUserErrno(unshare_errno); 162 CheckCloneNewUserErrno(unshare_errno);
217 return false; 163 return false;
218 } 164 }
219 165
220 // The current {r,e,s}{u,g}id is now an overflow id (c.f. 166 // 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. 167 // /proc/sys/kernel/overflowuid). Setup the uid and gid maps.
222 DCHECK(GetRESIds(NULL, NULL)); 168 DCHECK(GetRESIds(NULL, NULL));
223 const char kGidMapFile[] = "/proc/self/gid_map"; 169 const char kGidMapFile[] = "/proc/self/gid_map";
224 const char kUidMapFile[] = "/proc/self/uid_map"; 170 const char kUidMapFile[] = "/proc/self/uid_map";
225 CHECK(WriteToIdMapFile(kGidMapFile, gid)); 171 CHECK(NamespaceUtils::WriteToIdMapFile(kGidMapFile, gid));
226 CHECK(WriteToIdMapFile(kUidMapFile, uid)); 172 CHECK(NamespaceUtils::WriteToIdMapFile(kUidMapFile, uid));
227 DCHECK(GetRESIds(NULL, NULL)); 173 DCHECK(GetRESIds(NULL, NULL));
228 return true; 174 return true;
229 } 175 }
230 176
231 bool Credentials::DropFileSystemAccess() { 177 bool Credentials::DropFileSystemAccess() {
232 CHECK(ChrootToSafeEmptyDir()); 178 CHECK(ChrootToSafeEmptyDir());
233 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); 179 CHECK(!base::DirectoryExists(base::FilePath("/proc")));
234 // We never let this function fail. 180 // We never let this function fail.
235 return true; 181 return true;
236 } 182 }
237 183
238 } // namespace sandbox. 184 } // namespace sandbox.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698