OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "sandbox/linux/services/namespace_utils.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sched.h> |
| 9 #include <sys/types.h> |
| 10 #include <sys/stat.h> |
| 11 #include <unistd.h> |
| 12 |
| 13 #include <string> |
| 14 |
| 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_file.h" |
| 18 #include "base/logging.h" |
| 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/process/launch.h" |
| 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/third_party/valgrind/valgrind.h" |
| 23 |
| 24 namespace sandbox { |
| 25 |
| 26 namespace { |
| 27 bool IsRunningOnValgrind() { |
| 28 return RUNNING_ON_VALGRIND; |
| 29 } |
| 30 } // namespace |
| 31 |
| 32 // static |
| 33 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { |
| 34 base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY))); |
| 35 if (!fd.is_valid()) { |
| 36 return false; |
| 37 } |
| 38 |
| 39 const generic_id_t inside_id = id; |
| 40 const generic_id_t outside_id = id; |
| 41 const std::string mapping = |
| 42 base::StringPrintf("%d %d 1\n", inside_id, outside_id); |
| 43 const size_t len = mapping.size(); |
| 44 const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping.c_str(), len)); |
| 45 return rc == static_cast<ssize_t>(len); |
| 46 } |
| 47 |
| 48 // static |
| 49 bool NamespaceUtils::KernelSupportsUnprivilegedNamespace(int type) { |
| 50 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), |
| 51 // so always consider namespaces unsupported there. |
| 52 if (IsRunningOnValgrind()) { |
| 53 return false; |
| 54 } |
| 55 |
| 56 // As of Linux 3.8, /proc/self/ns/* files exist for all namespace types. Since |
| 57 // user namespaces were added in 3.8, it is OK to rely on the existence of |
| 58 // /proc/self/ns/*. |
| 59 if (!base::PathExists(base::FilePath("/proc/self/ns/user"))) { |
| 60 return false; |
| 61 } |
| 62 |
| 63 const char* path; |
| 64 switch (type) { |
| 65 case CLONE_NEWUSER: |
| 66 return true; |
| 67 case CLONE_NEWIPC: |
| 68 path = "/proc/self/ns/ipc"; |
| 69 break; |
| 70 case CLONE_NEWNET: |
| 71 path = "/proc/self/ns/net"; |
| 72 break; |
| 73 case CLONE_NEWNS: |
| 74 path = "/proc/self/ns/mnt"; |
| 75 break; |
| 76 case CLONE_NEWPID: |
| 77 path = "/proc/self/ns/pid"; |
| 78 break; |
| 79 case CLONE_NEWUTS: |
| 80 path = "/proc/self/ns/uts"; |
| 81 break; |
| 82 default: |
| 83 NOTREACHED(); |
| 84 return false; |
| 85 } |
| 86 |
| 87 return base::PathExists(base::FilePath(path)); |
| 88 } |
| 89 |
| 90 } // namespace sandbox |
OLD | NEW |