| 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> | |
| 11 #include <sys/syscall.h> | 10 #include <sys/syscall.h> |
| 12 #include <sys/types.h> | 11 #include <sys/types.h> |
| 13 #include <sys/wait.h> | 12 #include <sys/wait.h> |
| 14 #include <unistd.h> | 13 #include <unistd.h> |
| 15 | 14 |
| 16 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | 16 #include "base/bind.h" |
| 18 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 19 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 20 #include "base/logging.h" | 19 #include "base/logging.h" |
| 21 #include "base/posix/eintr_wrapper.h" | 20 #include "base/posix/eintr_wrapper.h" |
| 22 #include "base/process/launch.h" | 21 #include "base/process/launch.h" |
| 23 #include "base/template_util.h" | 22 #include "base/template_util.h" |
| 24 #include "base/third_party/valgrind/valgrind.h" | 23 #include "base/third_party/valgrind/valgrind.h" |
| 25 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 26 #include "sandbox/linux/services/namespace_utils.h" | 25 #include "sandbox/linux/services/namespace_utils.h" |
| 27 #include "sandbox/linux/services/proc_util.h" | 26 #include "sandbox/linux/services/proc_util.h" |
| 28 #include "sandbox/linux/services/syscall_wrappers.h" | 27 #include "sandbox/linux/services/syscall_wrappers.h" |
| 29 #include "sandbox/linux/services/thread_helpers.h" | 28 #include "sandbox/linux/services/thread_helpers.h" |
| 29 #include "sandbox/linux/system_headers/capability.h" |
| 30 | 30 |
| 31 namespace sandbox { | 31 namespace sandbox { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } | 35 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } |
| 36 | 36 |
| 37 struct CapFreeDeleter { | |
| 38 inline void operator()(cap_t cap) const { | |
| 39 int ret = cap_free(cap); | |
| 40 CHECK_EQ(0, ret); | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 // Wrapper to manage libcap2's cap_t type. | |
| 45 typedef scoped_ptr<typeof(*((cap_t)0)), CapFreeDeleter> ScopedCap; | |
| 46 | |
| 47 struct CapTextFreeDeleter { | |
| 48 inline void operator()(char* cap_text) const { | |
| 49 int ret = cap_free(cap_text); | |
| 50 CHECK_EQ(0, ret); | |
| 51 } | |
| 52 }; | |
| 53 | |
| 54 // Wrapper to manage the result from libcap2's cap_from_text(). | |
| 55 typedef scoped_ptr<char, CapTextFreeDeleter> ScopedCapText; | |
| 56 | |
| 57 // Checks that the set of RES-uids and the set of RES-gids have | 37 // Checks that the set of RES-uids and the set of RES-gids have |
| 58 // one element each and return that element in |resuid| and |resgid| | 38 // one element each and return that element in |resuid| and |resgid| |
| 59 // respectively. It's ok to pass NULL as one or both of the ids. | 39 // respectively. It's ok to pass NULL as one or both of the ids. |
| 60 bool GetRESIds(uid_t* resuid, gid_t* resgid) { | 40 bool GetRESIds(uid_t* resuid, gid_t* resgid) { |
| 61 uid_t ruid, euid, suid; | 41 uid_t ruid, euid, suid; |
| 62 gid_t rgid, egid, sgid; | 42 gid_t rgid, egid, sgid; |
| 63 PCHECK(getresuid(&ruid, &euid, &suid) == 0); | 43 PCHECK(getresuid(&ruid, &euid, &suid) == 0); |
| 64 PCHECK(getresgid(&rgid, &egid, &sgid) == 0); | 44 PCHECK(getresgid(&rgid, &egid, &sgid) == 0); |
| 65 const bool uids_are_equal = (ruid == euid) && (ruid == suid); | 45 const bool uids_are_equal = (ruid == euid) && (ruid == suid); |
| 66 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); | 46 const bool gids_are_equal = (rgid == egid) && (rgid == sgid); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // CHECK() that an attempt to move to a new user namespace raised an expected | 103 // CHECK() that an attempt to move to a new user namespace raised an expected |
| 124 // errno. | 104 // errno. |
| 125 void CheckCloneNewUserErrno(int error) { | 105 void CheckCloneNewUserErrno(int error) { |
| 126 // EPERM can happen if already in a chroot. EUSERS if too many nested | 106 // EPERM can happen if already in a chroot. EUSERS if too many nested |
| 127 // namespaces are used. EINVAL for kernels that don't support the feature. | 107 // namespaces are used. EINVAL for kernels that don't support the feature. |
| 128 // Valgrind will ENOSYS unshare(). | 108 // Valgrind will ENOSYS unshare(). |
| 129 PCHECK(error == EPERM || error == EUSERS || error == EINVAL || | 109 PCHECK(error == EPERM || error == EUSERS || error == EINVAL || |
| 130 error == ENOSYS); | 110 error == ENOSYS); |
| 131 } | 111 } |
| 132 | 112 |
| 113 // Converts a LinuxCapability to the corresponding Linux CAP_XXX value. |
| 114 int LinuxCapabilityToKernelValue(LinuxCapability cap) { |
| 115 switch (cap) { |
| 116 case LinuxCapability::kCapSysChroot: |
| 117 return CAP_SYS_CHROOT; |
| 118 case LinuxCapability::kCapSysAdmin: |
| 119 return CAP_SYS_ADMIN; |
| 120 } |
| 121 |
| 122 LOG(FATAL) << "Invalid LinuxCapability: " << static_cast<int>(cap); |
| 123 } |
| 124 |
| 133 } // namespace. | 125 } // namespace. |
| 134 | 126 |
| 135 bool Credentials::DropAllCapabilities(int proc_fd) { | 127 bool Credentials::DropAllCapabilities(int proc_fd) { |
| 136 DCHECK_LE(0, proc_fd); | 128 if (!SetCapabilities(proc_fd, std::vector<LinuxCapability>())) { |
| 137 #if !defined(THREAD_SANITIZER) | 129 return false; |
| 138 // With TSAN, accept to break the security model as it is a testing | 130 } |
| 139 // configuration. | |
| 140 CHECK(ThreadHelpers::IsSingleThreaded(proc_fd)); | |
| 141 #endif | |
| 142 | 131 |
| 143 ScopedCap cap(cap_init()); | |
| 144 CHECK(cap); | |
| 145 PCHECK(0 == cap_set_proc(cap.get())); | |
| 146 CHECK(!HasAnyCapability()); | 132 CHECK(!HasAnyCapability()); |
| 147 // We never let this function fail. | |
| 148 return true; | 133 return true; |
| 149 } | 134 } |
| 150 | 135 |
| 151 bool Credentials::DropAllCapabilities() { | 136 bool Credentials::DropAllCapabilities() { |
| 152 base::ScopedFD proc_fd(ProcUtil::OpenProc()); | 137 base::ScopedFD proc_fd(ProcUtil::OpenProc()); |
| 153 return Credentials::DropAllCapabilities(proc_fd.get()); | 138 return Credentials::DropAllCapabilities(proc_fd.get()); |
| 154 } | 139 } |
| 155 | 140 |
| 156 bool Credentials::HasAnyCapability() { | 141 // static |
| 157 ScopedCap current_cap(cap_get_proc()); | 142 bool Credentials::SetCapabilities(int proc_fd, |
| 158 CHECK(current_cap); | 143 const std::vector<LinuxCapability>& caps) { |
| 159 ScopedCap empty_cap(cap_init()); | 144 DCHECK_LE(0, proc_fd); |
| 160 CHECK(empty_cap); | 145 |
| 161 return cap_compare(current_cap.get(), empty_cap.get()) != 0; | 146 #if !defined(THREAD_SANITIZER) |
| 147 // With TSAN, accept to break the security model as it is a testing |
| 148 // configuration. |
| 149 CHECK(ThreadHelpers::IsSingleThreaded(proc_fd)); |
| 150 #endif |
| 151 |
| 152 struct cap_hdr hdr = {}; |
| 153 hdr.version = _LINUX_CAPABILITY_VERSION_3; |
| 154 struct cap_data data[_LINUX_CAPABILITY_U32S_3] = {{}}; |
| 155 |
| 156 // Initially, cap has no capability flags set. Enable the effective and |
| 157 // permitted flags only for the requested capabilities. |
| 158 for (const LinuxCapability cap : caps) { |
| 159 const int cap_num = LinuxCapabilityToKernelValue(cap); |
| 160 const size_t index = CAP_TO_INDEX(cap_num); |
| 161 const uint32_t mask = CAP_TO_MASK(cap_num); |
| 162 data[index].effective |= mask; |
| 163 data[index].permitted |= mask; |
| 164 } |
| 165 |
| 166 return sys_capset(&hdr, data) == 0; |
| 162 } | 167 } |
| 163 | 168 |
| 164 scoped_ptr<std::string> Credentials::GetCurrentCapString() { | 169 bool Credentials::HasAnyCapability() { |
| 165 ScopedCap current_cap(cap_get_proc()); | 170 struct cap_hdr hdr = {}; |
| 166 CHECK(current_cap); | 171 hdr.version = _LINUX_CAPABILITY_VERSION_3; |
| 167 ScopedCapText cap_text(cap_to_text(current_cap.get(), NULL)); | 172 struct cap_data data[_LINUX_CAPABILITY_U32S_3] = {{}}; |
| 168 CHECK(cap_text); | 173 |
| 169 return scoped_ptr<std::string> (new std::string(cap_text.get())); | 174 PCHECK(sys_capget(&hdr, data) == 0); |
| 175 |
| 176 for (size_t i = 0; i < arraysize(data); ++i) { |
| 177 if (data[i].effective || data[i].permitted || data[i].inheritable) { |
| 178 return true; |
| 179 } |
| 180 } |
| 181 |
| 182 return false; |
| 183 } |
| 184 |
| 185 bool Credentials::HasCapability(LinuxCapability cap) { |
| 186 struct cap_hdr hdr = {}; |
| 187 hdr.version = _LINUX_CAPABILITY_VERSION_3; |
| 188 struct cap_data data[_LINUX_CAPABILITY_U32S_3] = {{}}; |
| 189 |
| 190 PCHECK(sys_capget(&hdr, data) == 0); |
| 191 |
| 192 const int cap_num = LinuxCapabilityToKernelValue(cap); |
| 193 const size_t index = CAP_TO_INDEX(cap_num); |
| 194 const uint32_t mask = CAP_TO_MASK(cap_num); |
| 195 |
| 196 return (data[index].effective | data[index].permitted | |
| 197 data[index].inheritable) & |
| 198 mask; |
| 170 } | 199 } |
| 171 | 200 |
| 172 // static | 201 // static |
| 173 bool Credentials::CanCreateProcessInNewUserNS() { | 202 bool Credentials::CanCreateProcessInNewUserNS() { |
| 174 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), | 203 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), |
| 175 // so always consider UserNS unsupported there. | 204 // so always consider UserNS unsupported there. |
| 176 if (IsRunningOnValgrind()) { | 205 if (IsRunningOnValgrind()) { |
| 177 return false; | 206 return false; |
| 178 } | 207 } |
| 179 | 208 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 CHECK_LE(0, proc_fd); | 274 CHECK_LE(0, proc_fd); |
| 246 | 275 |
| 247 CHECK(ChrootToSafeEmptyDir()); | 276 CHECK(ChrootToSafeEmptyDir()); |
| 248 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); | 277 CHECK(!base::DirectoryExists(base::FilePath("/proc"))); |
| 249 CHECK(!ProcUtil::HasOpenDirectory(proc_fd)); | 278 CHECK(!ProcUtil::HasOpenDirectory(proc_fd)); |
| 250 // We never let this function fail. | 279 // We never let this function fail. |
| 251 return true; | 280 return true; |
| 252 } | 281 } |
| 253 | 282 |
| 254 } // namespace sandbox. | 283 } // namespace sandbox. |
| OLD | NEW |