| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/namespace_utils.h" | 5 #include "sandbox/linux/services/namespace_utils.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sched.h> | 8 #include <sched.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_file.h" | 17 #include "base/files/scoped_file.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/process/launch.h" | 20 #include "base/process/launch.h" |
| 21 #include "base/strings/safe_sprintf.h" | 21 #include "base/strings/safe_sprintf.h" |
| 22 #include "base/third_party/valgrind/valgrind.h" | 22 #include "base/third_party/valgrind/valgrind.h" |
| 23 | 23 |
| 24 namespace sandbox { | 24 namespace sandbox { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 bool IsRunningOnValgrind() { | 27 bool IsRunningOnValgrind() { |
| 28 return RUNNING_ON_VALGRIND; | 28 return RUNNING_ON_VALGRIND; |
| 29 } | 29 } |
| 30 |
| 31 const char kProcSelfSetgroups[] = "/proc/self/setgroups"; |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 // static | 34 // static |
| 33 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { | 35 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { |
| 34 // This function needs to be async-signal-safe, as it may be called in between | 36 // This function needs to be async-signal-safe, as it may be called in between |
| 35 // fork and exec. | 37 // fork and exec. |
| 36 | 38 |
| 37 int fd = HANDLE_EINTR(open(map_file, O_WRONLY)); | 39 int fd = HANDLE_EINTR(open(map_file, O_WRONLY)); |
| 38 if (fd == -1) { | 40 if (fd == -1) { |
| 39 return false; | 41 return false; |
| 40 } | 42 } |
| 41 | 43 |
| 42 const generic_id_t inside_id = id; | 44 const generic_id_t inside_id = id; |
| 43 const generic_id_t outside_id = id; | 45 const generic_id_t outside_id = id; |
| 44 | 46 |
| 45 char mapping[64]; | 47 char mapping[64]; |
| 46 ssize_t len = | 48 const ssize_t len = |
| 47 base::strings::SafeSPrintf(mapping, "%d %d 1\n", inside_id, outside_id); | 49 base::strings::SafeSPrintf(mapping, "%d %d 1\n", inside_id, outside_id); |
| 48 const ssize_t rc = HANDLE_EINTR(write(fd, mapping, len)); | 50 const ssize_t rc = HANDLE_EINTR(write(fd, mapping, len)); |
| 49 RAW_CHECK(IGNORE_EINTR(close(fd)) == 0); | 51 RAW_CHECK(IGNORE_EINTR(close(fd)) == 0); |
| 50 return rc == len; | 52 return rc == len; |
| 51 } | 53 } |
| 52 | 54 |
| 53 // static | 55 // static |
| 54 bool NamespaceUtils::KernelSupportsUnprivilegedNamespace(int type) { | 56 bool NamespaceUtils::KernelSupportsUnprivilegedNamespace(int type) { |
| 55 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), | 57 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), |
| 56 // so always consider namespaces unsupported there. | 58 // so always consider namespaces unsupported there. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 85 path = "/proc/self/ns/uts"; | 87 path = "/proc/self/ns/uts"; |
| 86 break; | 88 break; |
| 87 default: | 89 default: |
| 88 NOTREACHED(); | 90 NOTREACHED(); |
| 89 return false; | 91 return false; |
| 90 } | 92 } |
| 91 | 93 |
| 92 return base::PathExists(base::FilePath(path)); | 94 return base::PathExists(base::FilePath(path)); |
| 93 } | 95 } |
| 94 | 96 |
| 97 // static |
| 98 bool NamespaceUtils::KernelSupportsDenySetgroups() { |
| 99 return base::PathExists(base::FilePath(kProcSelfSetgroups)); |
| 100 } |
| 101 |
| 102 // static |
| 103 bool NamespaceUtils::DenySetgroups() { |
| 104 // This function needs to be async-signal-safe. |
| 105 int fd = HANDLE_EINTR(open(kProcSelfSetgroups, O_WRONLY)); |
| 106 if (fd == -1) { |
| 107 return false; |
| 108 } |
| 109 |
| 110 static const char kDeny[] = "deny"; |
| 111 const ssize_t len = sizeof(kDeny) - 1; |
| 112 const ssize_t rc = HANDLE_EINTR(write(fd, kDeny, len)); |
| 113 RAW_CHECK(IGNORE_EINTR(close(fd)) == 0); |
| 114 return rc == len; |
| 115 } |
| 116 |
| 95 } // namespace sandbox | 117 } // namespace sandbox |
| OLD | NEW |