Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: sandbox/linux/services/credentials_unittest.cc

Issue 851503003: Update from https://crrev.com/311076 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sandbox/linux/services/credentials.cc ('k') | skia/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stdio.h> 9 #include <stdio.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
(...skipping 30 matching lines...) Expand all
41 struct stat parrent; 41 struct stat parrent;
42 CHECK_EQ(0, stat("..", &parrent)); 42 CHECK_EQ(0, stat("..", &parrent));
43 CHECK_EQ(current.st_dev, parrent.st_dev); 43 CHECK_EQ(current.st_dev, parrent.st_dev);
44 CHECK_EQ(current.st_ino, parrent.st_ino); 44 CHECK_EQ(current.st_ino, parrent.st_ino);
45 CHECK_EQ(current.st_mode, parrent.st_mode); 45 CHECK_EQ(current.st_mode, parrent.st_mode);
46 CHECK_EQ(current.st_uid, parrent.st_uid); 46 CHECK_EQ(current.st_uid, parrent.st_uid);
47 CHECK_EQ(current.st_gid, parrent.st_gid); 47 CHECK_EQ(current.st_gid, parrent.st_gid);
48 return true; 48 return true;
49 } 49 }
50 50
51 // Give dynamic tools a simple thing to test.
52 TEST(Credentials, CreateAndDestroy) {
53 {
54 Credentials cred1;
55 (void) cred1;
56 }
57 scoped_ptr<Credentials> cred2(new Credentials);
58 }
59
60 SANDBOX_TEST(Credentials, DropAllCaps) { 51 SANDBOX_TEST(Credentials, DropAllCaps) {
61 Credentials creds; 52 CHECK(Credentials::DropAllCapabilities());
62 CHECK(creds.DropAllCapabilities()); 53 CHECK(!Credentials::HasAnyCapability());
63 CHECK(!creds.HasAnyCapability());
64 } 54 }
65 55
66 SANDBOX_TEST(Credentials, GetCurrentCapString) { 56 SANDBOX_TEST(Credentials, GetCurrentCapString) {
67 Credentials creds; 57 CHECK(Credentials::DropAllCapabilities());
68 CHECK(creds.DropAllCapabilities());
69 const char kNoCapabilityText[] = "="; 58 const char kNoCapabilityText[] = "=";
70 CHECK(*creds.GetCurrentCapString() == kNoCapabilityText); 59 CHECK(*Credentials::GetCurrentCapString() == kNoCapabilityText);
71 } 60 }
72 61
73 SANDBOX_TEST(Credentials, MoveToNewUserNS) { 62 SANDBOX_TEST(Credentials, MoveToNewUserNS) {
74 Credentials creds; 63 CHECK(Credentials::DropAllCapabilities());
75 CHECK(creds.DropAllCapabilities()); 64 bool moved_to_new_ns = Credentials::MoveToNewUserNS();
76 bool moved_to_new_ns = creds.MoveToNewUserNS();
77 fprintf(stdout, 65 fprintf(stdout,
78 "Unprivileged CLONE_NEWUSER supported: %s\n", 66 "Unprivileged CLONE_NEWUSER supported: %s\n",
79 moved_to_new_ns ? "true." : "false."); 67 moved_to_new_ns ? "true." : "false.");
80 fflush(stdout); 68 fflush(stdout);
81 if (!moved_to_new_ns) { 69 if (!moved_to_new_ns) {
82 fprintf(stdout, "This kernel does not support unprivileged namespaces. " 70 fprintf(stdout, "This kernel does not support unprivileged namespaces. "
83 "USERNS tests will succeed without running.\n"); 71 "USERNS tests will succeed without running.\n");
84 fflush(stdout); 72 fflush(stdout);
85 return; 73 return;
86 } 74 }
87 CHECK(creds.HasAnyCapability()); 75 CHECK(Credentials::HasAnyCapability());
88 CHECK(creds.DropAllCapabilities()); 76 CHECK(Credentials::DropAllCapabilities());
89 CHECK(!creds.HasAnyCapability()); 77 CHECK(!Credentials::HasAnyCapability());
90 } 78 }
91 79
92 SANDBOX_TEST(Credentials, SupportsUserNS) { 80 SANDBOX_TEST(Credentials, SupportsUserNS) {
93 Credentials creds; 81 CHECK(Credentials::DropAllCapabilities());
94 CHECK(creds.DropAllCapabilities());
95 bool user_ns_supported = Credentials::SupportsNewUserNS(); 82 bool user_ns_supported = Credentials::SupportsNewUserNS();
96 bool moved_to_new_ns = creds.MoveToNewUserNS(); 83 bool moved_to_new_ns = Credentials::MoveToNewUserNS();
97 CHECK_EQ(user_ns_supported, moved_to_new_ns); 84 CHECK_EQ(user_ns_supported, moved_to_new_ns);
98 } 85 }
99 86
100 SANDBOX_TEST(Credentials, UidIsPreserved) { 87 SANDBOX_TEST(Credentials, UidIsPreserved) {
101 Credentials creds; 88 CHECK(Credentials::DropAllCapabilities());
102 CHECK(creds.DropAllCapabilities());
103 uid_t old_ruid, old_euid, old_suid; 89 uid_t old_ruid, old_euid, old_suid;
104 gid_t old_rgid, old_egid, old_sgid; 90 gid_t old_rgid, old_egid, old_sgid;
105 PCHECK(0 == getresuid(&old_ruid, &old_euid, &old_suid)); 91 PCHECK(0 == getresuid(&old_ruid, &old_euid, &old_suid));
106 PCHECK(0 == getresgid(&old_rgid, &old_egid, &old_sgid)); 92 PCHECK(0 == getresgid(&old_rgid, &old_egid, &old_sgid));
107 // Probably missing kernel support. 93 // Probably missing kernel support.
108 if (!creds.MoveToNewUserNS()) return; 94 if (!Credentials::MoveToNewUserNS()) return;
109 uid_t new_ruid, new_euid, new_suid; 95 uid_t new_ruid, new_euid, new_suid;
110 PCHECK(0 == getresuid(&new_ruid, &new_euid, &new_suid)); 96 PCHECK(0 == getresuid(&new_ruid, &new_euid, &new_suid));
111 CHECK(old_ruid == new_ruid); 97 CHECK(old_ruid == new_ruid);
112 CHECK(old_euid == new_euid); 98 CHECK(old_euid == new_euid);
113 CHECK(old_suid == new_suid); 99 CHECK(old_suid == new_suid);
114 100
115 gid_t new_rgid, new_egid, new_sgid; 101 gid_t new_rgid, new_egid, new_sgid;
116 PCHECK(0 == getresgid(&new_rgid, &new_egid, &new_sgid)); 102 PCHECK(0 == getresgid(&new_rgid, &new_egid, &new_sgid));
117 CHECK(old_rgid == new_rgid); 103 CHECK(old_rgid == new_rgid);
118 CHECK(old_egid == new_egid); 104 CHECK(old_egid == new_egid);
119 CHECK(old_sgid == new_sgid); 105 CHECK(old_sgid == new_sgid);
120 } 106 }
121 107
122 bool NewUserNSCycle(Credentials* creds) { 108 bool NewUserNSCycle() {
123 DCHECK(creds); 109 if (!Credentials::MoveToNewUserNS() ||
124 if (!creds->MoveToNewUserNS() || 110 !Credentials::HasAnyCapability() ||
125 !creds->HasAnyCapability() || 111 !Credentials::DropAllCapabilities() ||
126 !creds->DropAllCapabilities() || 112 Credentials::HasAnyCapability()) {
127 creds->HasAnyCapability()) {
128 return false; 113 return false;
129 } 114 }
130 return true; 115 return true;
131 } 116 }
132 117
133 SANDBOX_TEST(Credentials, NestedUserNS) { 118 SANDBOX_TEST(Credentials, NestedUserNS) {
134 Credentials creds; 119 CHECK(Credentials::DropAllCapabilities());
135 CHECK(creds.DropAllCapabilities());
136 // Probably missing kernel support. 120 // Probably missing kernel support.
137 if (!creds.MoveToNewUserNS()) return; 121 if (!Credentials::MoveToNewUserNS()) return;
138 CHECK(creds.DropAllCapabilities()); 122 CHECK(Credentials::DropAllCapabilities());
139 // As of 3.12, the kernel has a limit of 32. See create_user_ns(). 123 // As of 3.12, the kernel has a limit of 32. See create_user_ns().
140 const int kNestLevel = 10; 124 const int kNestLevel = 10;
141 for (int i = 0; i < kNestLevel; ++i) { 125 for (int i = 0; i < kNestLevel; ++i) {
142 CHECK(NewUserNSCycle(&creds)) << "Creating new user NS failed at iteration " 126 CHECK(NewUserNSCycle()) << "Creating new user NS failed at iteration "
143 << i << "."; 127 << i << ".";
144 } 128 }
145 } 129 }
146 130
147 // Test the WorkingDirectoryIsRoot() helper. 131 // Test the WorkingDirectoryIsRoot() helper.
148 TEST(Credentials, CanDetectRoot) { 132 TEST(Credentials, CanDetectRoot) {
149 ASSERT_EQ(0, chdir("/proc/")); 133 ASSERT_EQ(0, chdir("/proc/"));
150 ASSERT_FALSE(WorkingDirectoryIsRoot()); 134 ASSERT_FALSE(WorkingDirectoryIsRoot());
151 ASSERT_EQ(0, chdir("/")); 135 ASSERT_EQ(0, chdir("/"));
152 ASSERT_TRUE(WorkingDirectoryIsRoot()); 136 ASSERT_TRUE(WorkingDirectoryIsRoot());
153 } 137 }
154 138
155 SANDBOX_TEST(Credentials, DISABLE_ON_LSAN(DropFileSystemAccessIsSafe)) { 139 SANDBOX_TEST(Credentials, DISABLE_ON_LSAN(DropFileSystemAccessIsSafe)) {
156 Credentials creds; 140 CHECK(Credentials::DropAllCapabilities());
157 CHECK(creds.DropAllCapabilities());
158 // Probably missing kernel support. 141 // Probably missing kernel support.
159 if (!creds.MoveToNewUserNS()) return; 142 if (!Credentials::MoveToNewUserNS()) return;
160 CHECK(creds.DropFileSystemAccess()); 143 CHECK(Credentials::DropFileSystemAccess());
161 CHECK(!DirectoryExists("/proc")); 144 CHECK(!DirectoryExists("/proc"));
162 CHECK(WorkingDirectoryIsRoot()); 145 CHECK(WorkingDirectoryIsRoot());
163 // We want the chroot to never have a subdirectory. A subdirectory 146 // We want the chroot to never have a subdirectory. A subdirectory
164 // could allow a chroot escape. 147 // could allow a chroot escape.
165 CHECK_NE(0, mkdir("/test", 0700)); 148 CHECK_NE(0, mkdir("/test", 0700));
166 } 149 }
167 150
168 // Check that after dropping filesystem access and dropping privileges 151 // Check that after dropping filesystem access and dropping privileges
169 // it is not possible to regain capabilities. 152 // it is not possible to regain capabilities.
170 SANDBOX_TEST(Credentials, DISABLE_ON_LSAN(CannotRegainPrivileges)) { 153 SANDBOX_TEST(Credentials, DISABLE_ON_LSAN(CannotRegainPrivileges)) {
171 Credentials creds; 154 CHECK(Credentials::DropAllCapabilities());
172 CHECK(creds.DropAllCapabilities());
173 // Probably missing kernel support. 155 // Probably missing kernel support.
174 if (!creds.MoveToNewUserNS()) return; 156 if (!Credentials::MoveToNewUserNS()) return;
175 CHECK(creds.DropFileSystemAccess()); 157 CHECK(Credentials::DropFileSystemAccess());
176 CHECK(creds.DropAllCapabilities()); 158 CHECK(Credentials::DropAllCapabilities());
177 159
178 // The kernel should now prevent us from regaining capabilities because we 160 // The kernel should now prevent us from regaining capabilities because we
179 // are in a chroot. 161 // are in a chroot.
180 CHECK(!Credentials::SupportsNewUserNS()); 162 CHECK(!Credentials::SupportsNewUserNS());
181 CHECK(!creds.MoveToNewUserNS()); 163 CHECK(!Credentials::MoveToNewUserNS());
182 } 164 }
183 165
184 } // namespace. 166 } // namespace.
185 167
186 } // namespace sandbox. 168 } // namespace sandbox.
OLDNEW
« no previous file with comments | « sandbox/linux/services/credentials.cc ('k') | skia/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698