OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 <errno.h> |
| 8 #include <sched.h> |
| 9 #include <sys/types.h> |
| 10 #include <sys/wait.h> |
| 11 |
| 12 #include "base/posix/eintr_wrapper.h" |
| 13 #include "base/process/process.h" |
| 14 #include "sandbox/linux/tests/unit_tests.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace sandbox { |
| 18 |
| 19 namespace { |
| 20 |
| 21 SANDBOX_TEST(NamespaceUtils, SupportsLinuxNamespace) { |
| 22 bool supports_user_ns = NamespaceUtils::SupportsLinuxNamespace(CLONE_NEWUSER); |
| 23 errno = 0; |
| 24 pid_t pid = base::ForkWithFlags(CLONE_NEWUSER, nullptr, nullptr); |
| 25 if (pid == 0) { |
| 26 _exit(0); |
| 27 } |
| 28 |
| 29 bool fork_succeeded = pid > 0; |
| 30 if (fork_succeeded) { |
| 31 int status; |
| 32 EXPECT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
| 33 EXPECT_EQ(0, status); |
| 34 } else { |
| 35 EXPECT_EQ(EINTR, errno); |
| 36 } |
| 37 |
| 38 EXPECT_EQ(supports_user_ns, fork_succeeded); |
| 39 } |
| 40 |
| 41 SANDBOX_TEST(NamespaceUtils, WriteToIdMapFile) { |
| 42 if (!NamespaceUtils::SupportsLinuxNamespace(CLONE_NEWUSER)) { |
| 43 return; |
| 44 } |
| 45 |
| 46 pid_t pid = base::ForkWithFlags(CLONE_NEWUSER, nullptr, nullptr); |
| 47 ASSERT_NE(-1, pid); |
| 48 |
| 49 uid_t uid = getuid(); |
| 50 gid_t gid = getgid(); |
| 51 if (pid == 0) { |
| 52 EXPECT_NE(uid, getuid()); |
| 53 NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid); |
| 54 EXPECT_EQ(uid, getuid()); |
| 55 |
| 56 EXPECT_NE(gid, getgid()); |
| 57 NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid); |
| 58 EXPECT_EQ(gid, getgid()); |
| 59 |
| 60 _exit(0); |
| 61 } |
| 62 |
| 63 int status; |
| 64 EXPECT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
| 65 EXPECT_EQ(0, status); |
| 66 } |
| 67 |
| 68 } // namespace. |
| 69 |
| 70 } // namespace sandbox. |
OLD | NEW |