OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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/strings/stringprintf.h" |
| 21 #include "base/third_party/valgrind/valgrind.h" |
| 22 |
| 23 namespace sandbox { |
| 24 |
| 25 namespace { |
| 26 bool IsRunningOnValgrind() { |
| 27 return RUNNING_ON_VALGRIND; |
| 28 } |
| 29 } // namespace |
| 30 |
| 31 // Write a uid or gid mapping from |id| to |id| in |map_file|. |
| 32 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { |
| 33 base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY))); |
| 34 if (!fd.is_valid()) { |
| 35 return false; |
| 36 } |
| 37 |
| 38 const generic_id_t inside_id = id; |
| 39 const generic_id_t outside_id = id; |
| 40 const std::string mapping = |
| 41 base::StringPrintf("%d %d 1\n", inside_id, outside_id); |
| 42 const size_t len = mapping.size(); |
| 43 const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping.c_str(), len)); |
| 44 return rc == static_cast<ssize_t>(len); |
| 45 } |
| 46 |
| 47 bool NamespaceUtils::SupportsLinuxNamespace(int type) { |
| 48 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), |
| 49 // so always consider namespaces unsupported there. |
| 50 if (IsRunningOnValgrind()) { |
| 51 return false; |
| 52 } |
| 53 |
| 54 const char* path; |
| 55 switch (type) { |
| 56 case CLONE_NEWIPC: |
| 57 path = "/proc/self/ns/ipc"; |
| 58 break; |
| 59 case CLONE_NEWNET: |
| 60 path = "/proc/self/ns/net"; |
| 61 break; |
| 62 case CLONE_NEWNS: |
| 63 path = "/proc/self/ns/mnt"; |
| 64 break; |
| 65 case CLONE_NEWPID: |
| 66 path = "/proc/self/ns/pid"; |
| 67 break; |
| 68 case CLONE_NEWUSER: |
| 69 path = "/proc/self/ns/user"; |
| 70 break; |
| 71 case CLONE_NEWUTS: |
| 72 path = "/proc/self/ns/uts"; |
| 73 break; |
| 74 default: |
| 75 NOTREACHED(); |
| 76 return false; |
| 77 } |
| 78 |
| 79 return base::PathExists(base::FilePath(path)); |
| 80 } |
| 81 |
| 82 } // namespace sandbox |
OLD | NEW |