OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/credentials.h" | 5 #include "sandbox/linux/services/credentials.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sched.h> | |
9 #include <stdio.h> | 10 #include <stdio.h> |
10 #include <sys/stat.h> | 11 #include <sys/stat.h> |
11 #include <sys/types.h> | 12 #include <sys/types.h> |
12 #include <unistd.h> | 13 #include <unistd.h> |
13 | 14 |
14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
16 #include "base/files/scoped_file.h" | 17 #include "base/files/scoped_file.h" |
17 #include "base/logging.h" | 18 #include "base/logging.h" |
18 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/process/launch.h" | |
19 #include "sandbox/linux/tests/unit_tests.h" | 21 #include "sandbox/linux/tests/unit_tests.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 namespace sandbox { | 24 namespace sandbox { |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 bool WorkingDirectoryIsRoot() { | 28 bool WorkingDirectoryIsRoot() { |
27 char current_dir[PATH_MAX]; | 29 char current_dir[PATH_MAX]; |
28 char* cwd = getcwd(current_dir, sizeof(current_dir)); | 30 char* cwd = getcwd(current_dir, sizeof(current_dir)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 fprintf(stdout, "This kernel does not support unprivileged namespaces. " | 66 fprintf(stdout, "This kernel does not support unprivileged namespaces. " |
65 "USERNS tests will succeed without running.\n"); | 67 "USERNS tests will succeed without running.\n"); |
66 fflush(stdout); | 68 fflush(stdout); |
67 return; | 69 return; |
68 } | 70 } |
69 CHECK(Credentials::HasAnyCapability()); | 71 CHECK(Credentials::HasAnyCapability()); |
70 CHECK(Credentials::DropAllCapabilities()); | 72 CHECK(Credentials::DropAllCapabilities()); |
71 CHECK(!Credentials::HasAnyCapability()); | 73 CHECK(!Credentials::HasAnyCapability()); |
72 } | 74 } |
73 | 75 |
74 SANDBOX_TEST(Credentials, SupportsUserNS) { | |
75 CHECK(Credentials::DropAllCapabilities()); | |
76 bool user_ns_supported = Credentials::SupportsNewUserNS(); | |
77 bool moved_to_new_ns = Credentials::MoveToNewUserNS(); | |
78 CHECK_EQ(user_ns_supported, moved_to_new_ns); | |
79 } | |
80 | |
81 SANDBOX_TEST(Credentials, UidIsPreserved) { | 76 SANDBOX_TEST(Credentials, UidIsPreserved) { |
82 CHECK(Credentials::DropAllCapabilities()); | 77 CHECK(Credentials::DropAllCapabilities()); |
83 uid_t old_ruid, old_euid, old_suid; | 78 uid_t old_ruid, old_euid, old_suid; |
84 gid_t old_rgid, old_egid, old_sgid; | 79 gid_t old_rgid, old_egid, old_sgid; |
85 PCHECK(0 == getresuid(&old_ruid, &old_euid, &old_suid)); | 80 PCHECK(0 == getresuid(&old_ruid, &old_euid, &old_suid)); |
86 PCHECK(0 == getresgid(&old_rgid, &old_egid, &old_sgid)); | 81 PCHECK(0 == getresgid(&old_rgid, &old_egid, &old_sgid)); |
87 // Probably missing kernel support. | 82 // Probably missing kernel support. |
88 if (!Credentials::MoveToNewUserNS()) return; | 83 if (!Credentials::MoveToNewUserNS()) return; |
89 uid_t new_ruid, new_euid, new_suid; | 84 uid_t new_ruid, new_euid, new_suid; |
90 PCHECK(0 == getresuid(&new_ruid, &new_euid, &new_suid)); | 85 PCHECK(0 == getresuid(&new_ruid, &new_euid, &new_suid)); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 // it is not possible to regain capabilities. | 141 // it is not possible to regain capabilities. |
147 SANDBOX_TEST(Credentials, DISABLE_ON_LSAN(CannotRegainPrivileges)) { | 142 SANDBOX_TEST(Credentials, DISABLE_ON_LSAN(CannotRegainPrivileges)) { |
148 CHECK(Credentials::DropAllCapabilities()); | 143 CHECK(Credentials::DropAllCapabilities()); |
149 // Probably missing kernel support. | 144 // Probably missing kernel support. |
150 if (!Credentials::MoveToNewUserNS()) return; | 145 if (!Credentials::MoveToNewUserNS()) return; |
151 CHECK(Credentials::DropFileSystemAccess()); | 146 CHECK(Credentials::DropFileSystemAccess()); |
152 CHECK(Credentials::DropAllCapabilities()); | 147 CHECK(Credentials::DropAllCapabilities()); |
153 | 148 |
154 // The kernel should now prevent us from regaining capabilities because we | 149 // The kernel should now prevent us from regaining capabilities because we |
155 // are in a chroot. | 150 // are in a chroot. |
156 CHECK(!Credentials::SupportsNewUserNS()); | 151 errno = 0; |
152 CHECK_EQ(-1, unshare(CLONE_NEWUSER)); | |
jln (very slow on Chromium)
2015/01/23 02:48:23
If we decide to not keep SupportsNewUserNS() under
rickyz (no longer on Chrome)
2015/01/23 23:59:37
Ended up keeping the function
| |
153 CHECK_EQ(EPERM, errno); | |
154 | |
155 errno = 0; | |
156 CHECK_EQ(-1, base::ForkWithFlags(CLONE_NEWUSER | SIGCHLD, nullptr, nullptr)); | |
157 CHECK_EQ(EPERM, errno); | |
158 | |
157 CHECK(!Credentials::MoveToNewUserNS()); | 159 CHECK(!Credentials::MoveToNewUserNS()); |
158 } | 160 } |
159 | 161 |
160 } // namespace. | 162 } // namespace. |
161 | 163 |
162 } // namespace sandbox. | 164 } // namespace sandbox. |
OLD | NEW |