| 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> | 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/proc_util.h" | |
| 25 #include "sandbox/linux/services/syscall_wrappers.h" | 24 #include "sandbox/linux/services/syscall_wrappers.h" |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } | 28 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } |
| 30 | 29 |
| 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); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 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 COMPILE_ASSERT((base::is_same<uid_t, gid_t>::value), UidAndGidAreSameType); | 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. | 63 // generic_id_t can be used for either uid_t or gid_t. |
| 64 typedef uid_t generic_id_t; | 64 typedef uid_t generic_id_t; |
| 65 | 65 |
| 66 // Write a uid or gid mapping from |id| to |id| in |map_file|. | 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) { | 67 bool WriteToIdMapFile(const char* map_file, generic_id_t id) { |
| 68 ScopedFILE f(fopen(map_file, "w")); | 68 ScopedFILE f(fopen(map_file, "w")); |
| 69 PCHECK(f); | 69 PCHECK(f); |
| 70 const uid_t inside_id = id; | 70 const uid_t inside_id = id; |
| 71 const uid_t outside_id = id; | 71 const uid_t outside_id = id; |
| 72 int num = fprintf(f.get(), "%d %d 1\n", inside_id, outside_id); | 72 int num = fprintf(f.get(), "%d %d 1\n", inside_id, outside_id); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 94 if (resuid) *resuid = euid; | 94 if (resuid) *resuid = euid; |
| 95 if (resgid) *resgid = egid; | 95 if (resgid) *resgid = egid; |
| 96 return true; | 96 return true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 // chroot() and chdir() to /proc/<tid>/fdinfo. | 99 // chroot() and chdir() to /proc/<tid>/fdinfo. |
| 100 void ChrootToThreadFdInfo(base::PlatformThreadId tid, bool* result) { | 100 void ChrootToThreadFdInfo(base::PlatformThreadId tid, bool* result) { |
| 101 DCHECK(result); | 101 DCHECK(result); |
| 102 *result = false; | 102 *result = false; |
| 103 | 103 |
| 104 COMPILE_ASSERT((base::is_same<base::PlatformThreadId, int>::value), | 104 static_assert((base::is_same<base::PlatformThreadId, int>::value), |
| 105 TidIsAnInt); | 105 "platform thread id should be an int"); |
| 106 const std::string current_thread_fdinfo = "/proc/" + | 106 const std::string current_thread_fdinfo = "/proc/" + |
| 107 base::IntToString(tid) + "/fdinfo/"; | 107 base::IntToString(tid) + "/fdinfo/"; |
| 108 | 108 |
| 109 // Make extra sure that /proc/<tid>/fdinfo is unique to the thread. | 109 // Make extra sure that /proc/<tid>/fdinfo is unique to the thread. |
| 110 CHECK(0 == unshare(CLONE_FILES)); | 110 CHECK(0 == unshare(CLONE_FILES)); |
| 111 int chroot_ret = chroot(current_thread_fdinfo.c_str()); | 111 int chroot_ret = chroot(current_thread_fdinfo.c_str()); |
| 112 if (chroot_ret) { | 112 if (chroot_ret) { |
| 113 PLOG(ERROR) << "Could not chroot"; | 113 PLOG(ERROR) << "Could not chroot"; |
| 114 return; | 114 return; |
| 115 } | 115 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 DCHECK(GetRESIds(NULL, NULL)); | 241 DCHECK(GetRESIds(NULL, NULL)); |
| 242 const char kGidMapFile[] = "/proc/self/gid_map"; | 242 const char kGidMapFile[] = "/proc/self/gid_map"; |
| 243 const char kUidMapFile[] = "/proc/self/uid_map"; | 243 const char kUidMapFile[] = "/proc/self/uid_map"; |
| 244 CHECK(WriteToIdMapFile(kGidMapFile, gid)); | 244 CHECK(WriteToIdMapFile(kGidMapFile, gid)); |
| 245 CHECK(WriteToIdMapFile(kUidMapFile, uid)); | 245 CHECK(WriteToIdMapFile(kUidMapFile, uid)); |
| 246 DCHECK(GetRESIds(NULL, NULL)); | 246 DCHECK(GetRESIds(NULL, NULL)); |
| 247 return true; | 247 return true; |
| 248 } | 248 } |
| 249 | 249 |
| 250 bool Credentials::DropFileSystemAccess() { | 250 bool Credentials::DropFileSystemAccess() { |
| 251 // Chrooting to a safe empty dir will only be safe if no directory file | |
| 252 // descriptor is available to the process. | |
| 253 DCHECK(!ProcUtil::HasOpenDirectory(-1)); | |
| 254 return ChrootToSafeEmptyDir(); | 251 return ChrootToSafeEmptyDir(); |
| 255 } | 252 } |
| 256 | 253 |
| 257 } // namespace sandbox. | 254 } // namespace sandbox. |
| OLD | NEW |