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
3 // found in the LICENSE file.
5 #include "sandbox/linux/services/credentials.h"
10 #include <sys/capability.h>
11 #include <sys/syscall.h>
12 #include <sys/types.h>
16 #include "base/basictypes.h"
17 #include "base/bind.h"
18 #include "base/files/file_path.h"
19 #include "base/files/file_util.h"
20 #include "base/logging.h"
21 #include "base/posix/eintr_wrapper.h"
22 #include "base/process/launch.h"
23 #include "base/template_util.h"
24 #include "base/third_party/valgrind/valgrind.h"
25 #include "sandbox/linux/services/namespace_utils.h"
26 #include "sandbox/linux/services/syscall_wrappers.h"
32 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND
; }
34 struct CapFreeDeleter
{
35 inline void operator()(cap_t cap
) const {
36 int ret
= cap_free(cap
);
41 // Wrapper to manage libcap2's cap_t type.
42 typedef scoped_ptr
<typeof(*((cap_t
)0)), CapFreeDeleter
> ScopedCap
;
44 struct CapTextFreeDeleter
{
45 inline void operator()(char* cap_text
) const {
46 int ret
= cap_free(cap_text
);
51 // Wrapper to manage the result from libcap2's cap_from_text().
52 typedef scoped_ptr
<char, CapTextFreeDeleter
> ScopedCapText
;
54 // Checks that the set of RES-uids and the set of RES-gids have
55 // one element each and return that element in |resuid| and |resgid|
56 // respectively. It's ok to pass NULL as one or both of the ids.
57 bool GetRESIds(uid_t
* resuid
, gid_t
* resgid
) {
58 uid_t ruid
, euid
, suid
;
59 gid_t rgid
, egid
, sgid
;
60 PCHECK(getresuid(&ruid
, &euid
, &suid
) == 0);
61 PCHECK(getresgid(&rgid
, &egid
, &sgid
) == 0);
62 const bool uids_are_equal
= (ruid
== euid
) && (ruid
== suid
);
63 const bool gids_are_equal
= (rgid
== egid
) && (rgid
== sgid
);
64 if (!uids_are_equal
|| !gids_are_equal
) return false;
65 if (resuid
) *resuid
= euid
;
66 if (resgid
) *resgid
= egid
;
70 const int kExitSuccess
= 0;
72 int ChrootToSelfFdinfo(void*) {
73 RAW_CHECK(chroot("/proc/self/fdinfo/") == 0);
75 // CWD is essentially an implicit file descriptor, so be careful to not
77 RAW_CHECK(chdir("/") == 0);
81 // chroot() to an empty dir that is "safe". To be safe, it must not contain
82 // any subdirectory (chroot-ing there would allow a chroot escape) and it must
83 // be impossible to create an empty directory there.
84 // We achieve this by doing the following:
85 // 1. We create a new process sharing file system information.
86 // 2. In the child, we chroot to /proc/self/fdinfo/
87 // This is already "safe", since fdinfo/ does not contain another directory and
88 // one cannot create another directory there.
89 // 3. The process dies
90 // After (3) happens, the directory is not available anymore in /proc.
91 bool ChrootToSafeEmptyDir() {
92 // We need to chroot to a fdinfo that is unique to a process and have that
94 // 1. We don't want to simply fork() because duplicating the page tables is
95 // slow with a big address space.
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.
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
);
107 #error "Unsupported architecture"
109 pid
= clone(ChrootToSelfFdinfo
, stack
,
110 CLONE_VM
| CLONE_VFORK
| CLONE_FS
| SIGCHLD
, nullptr, nullptr,
115 PCHECK(HANDLE_EINTR(waitpid(pid
, &status
, 0)) == pid
);
117 return WIFEXITED(status
) && WEXITSTATUS(status
) == kExitSuccess
;
120 // CHECK() that an attempt to move to a new user namespace raised an expected
122 void CheckCloneNewUserErrno(int error
) {
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.
125 // Valgrind will ENOSYS unshare().
126 PCHECK(error
== EPERM
|| error
== EUSERS
|| error
== EINVAL
||
132 bool Credentials::DropAllCapabilities() {
133 ScopedCap
cap(cap_init());
135 PCHECK(0 == cap_set_proc(cap
.get()));
136 CHECK(!HasAnyCapability());
137 // We never let this function fail.
141 bool Credentials::HasAnyCapability() {
142 ScopedCap
current_cap(cap_get_proc());
144 ScopedCap
empty_cap(cap_init());
146 return cap_compare(current_cap
.get(), empty_cap
.get()) != 0;
149 scoped_ptr
<std::string
> Credentials::GetCurrentCapString() {
150 ScopedCap
current_cap(cap_get_proc());
152 ScopedCapText
cap_text(cap_to_text(current_cap
.get(), NULL
));
154 return scoped_ptr
<std::string
> (new std::string(cap_text
.get()));
158 bool Credentials::CanCreateProcessInNewUserNS() {
159 // Valgrind will let clone(2) pass-through, but doesn't support unshare(),
160 // so always consider UserNS unsupported there.
161 if (IsRunningOnValgrind()) {
165 // This is roughly a fork().
166 const pid_t pid
= sys_clone(CLONE_NEWUSER
| SIGCHLD
, 0, 0, 0, 0);
169 CheckCloneNewUserErrno(errno
);
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
175 // fragile execution environment.
180 // Always reap the child.
182 PCHECK(HANDLE_EINTR(waitpid(pid
, &status
, 0)) == pid
);
183 CHECK(WIFEXITED(status
));
184 CHECK_EQ(kExitSuccess
, WEXITSTATUS(status
));
186 // clone(2) succeeded, we can use CLONE_NEWUSER.
190 bool Credentials::MoveToNewUserNS() {
193 if (!GetRESIds(&uid
, &gid
)) {
194 // If all the uids (or gids) are not equal to each other, the security
195 // model will most likely confuse the caller, abort.
196 DVLOG(1) << "uids or gids differ!";
199 int ret
= unshare(CLONE_NEWUSER
);
201 const int unshare_errno
= errno
;
202 VLOG(1) << "Looks like unprivileged CLONE_NEWUSER may not be available "
203 << "on this kernel.";
204 CheckCloneNewUserErrno(unshare_errno
);
208 if (NamespaceUtils::KernelSupportsDenySetgroups()) {
209 PCHECK(NamespaceUtils::DenySetgroups());
212 // The current {r,e,s}{u,g}id is now an overflow id (c.f.
213 // /proc/sys/kernel/overflowuid). Setup the uid and gid maps.
214 DCHECK(GetRESIds(NULL
, NULL
));
215 const char kGidMapFile
[] = "/proc/self/gid_map";
216 const char kUidMapFile
[] = "/proc/self/uid_map";
217 PCHECK(NamespaceUtils::WriteToIdMapFile(kGidMapFile
, gid
));
218 PCHECK(NamespaceUtils::WriteToIdMapFile(kUidMapFile
, uid
));
219 DCHECK(GetRESIds(NULL
, NULL
));
223 bool Credentials::DropFileSystemAccess() {
224 CHECK(ChrootToSafeEmptyDir());
225 CHECK(!base::DirectoryExists(base::FilePath("/proc")));
226 // We never let this function fail.
230 } // namespace sandbox.