Chromium Code Reviews| 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/stringprintf.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 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 // static | 32 // static |
| 33 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { | 33 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) { |
| 34 base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY))); | 34 base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY))); |
|
jln (very slow on Chromium)
2015/02/03 01:14:30
I would rather not assume ScopedFD to be async-saf
rickyz (no longer on Chrome)
2015/02/03 01:27:22
Done.
| |
| 35 if (!fd.is_valid()) { | 35 if (!fd.is_valid()) { |
| 36 return false; | 36 return false; |
| 37 } | 37 } |
| 38 | 38 |
| 39 const generic_id_t inside_id = id; | 39 const generic_id_t inside_id = id; |
| 40 const generic_id_t outside_id = id; | 40 const generic_id_t outside_id = id; |
| 41 const std::string mapping = | 41 |
| 42 base::StringPrintf("%d %d 1\n", inside_id, outside_id); | 42 // This function needs to be async-signal-safe, as it is called in between |
|
jln (very slow on Chromium)
2015/02/03 01:14:30
Add this as a comment to the function instead.
rickyz (no longer on Chrome)
2015/02/03 01:27:22
Done.
| |
| 43 const size_t len = mapping.size(); | 43 // fork and exec. |
| 44 const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping.c_str(), len)); | 44 char mapping[64]; |
| 45 return rc == static_cast<ssize_t>(len); | 45 ssize_t len = |
| 46 base::strings::SafeSPrintf(mapping, "%d %d 1\n", inside_id, outside_id); | |
| 47 const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping, len)); | |
| 48 return rc == len; | |
| 46 } | 49 } |
| 47 | 50 |
| 48 // static | 51 // static |
| 49 bool NamespaceUtils::KernelSupportsUnprivilegedNamespace(int type) { | 52 bool NamespaceUtils::KernelSupportsUnprivilegedNamespace(int type) { |
| 50 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), | 53 // Valgrind will let clone(2) pass-through, but doesn't support unshare(), |
| 51 // so always consider namespaces unsupported there. | 54 // so always consider namespaces unsupported there. |
| 52 if (IsRunningOnValgrind()) { | 55 if (IsRunningOnValgrind()) { |
| 53 return false; | 56 return false; |
| 54 } | 57 } |
| 55 | 58 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 81 break; | 84 break; |
| 82 default: | 85 default: |
| 83 NOTREACHED(); | 86 NOTREACHED(); |
| 84 return false; | 87 return false; |
| 85 } | 88 } |
| 86 | 89 |
| 87 return base::PathExists(base::FilePath(path)); | 90 return base::PathExists(base::FilePath(path)); |
| 88 } | 91 } |
| 89 | 92 |
| 90 } // namespace sandbox | 93 } // namespace sandbox |
| OLD | NEW |