1 /* Copyright 2017 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Test system.[ch] module code using gtest.
6 */
7
8 #include <ftw.h>
9 #include <limits.h>
10 #include <linux/securebits.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/statvfs.h>
16 #include <unistd.h>
17
18 #include <gtest/gtest.h>
19
20 #include <string>
21
22 #include "system.h"
23 #include "unittest_util.h"
24
25 namespace {
26
27 // A random path that really really should not exist on the host.
28 constexpr const char kNoSuchDir[] = "/.x/..x/...x/path/should/not/exist/";
29
30 // A random file that should exist on both Linux and Android.
31 constexpr const char kValidFile[] = "/etc/hosts";
32
33 // A random directory that should exist.
34 constexpr const char kValidDir[] = "/";
35
36 // A random character device that should exist.
37 constexpr const char kValidCharDev[] = "/dev/null";
38
39 } // namespace
40
TEST(secure_noroot_set_and_locked,zero_mask)41 TEST(secure_noroot_set_and_locked, zero_mask) {
42 ASSERT_EQ(secure_noroot_set_and_locked(0), 0);
43 }
44
TEST(secure_noroot_set_and_locked,set)45 TEST(secure_noroot_set_and_locked, set) {
46 ASSERT_EQ(secure_noroot_set_and_locked(issecure_mask(SECURE_NOROOT) |
47 issecure_mask(SECURE_NOROOT_LOCKED)),
48 1);
49 }
50
TEST(secure_noroot_set_and_locked,not_set)51 TEST(secure_noroot_set_and_locked, not_set) {
52 ASSERT_EQ(secure_noroot_set_and_locked(issecure_mask(SECURE_KEEP_CAPS) |
53 issecure_mask(SECURE_NOROOT_LOCKED)),
54 0);
55 }
56
57 // Sanity check for the cap range.
TEST(get_last_valid_cap,basic)58 TEST(get_last_valid_cap, basic) {
59 unsigned int cap = get_last_valid_cap();
60
61 // We pick 35 as it's been that since at least v3.0.
62 // If this test is run on older kernels, it might fail.
63 EXPECT_GE(cap, 35u);
64
65 // Pick a really large number that we probably won't hit for a long time.
66 // It helps that caps are bitfields.
67 EXPECT_LT(cap, 128u);
68 }
69
70 // Might be useful to figure out the return value, but for now,
71 // just make sure it doesn't crash?
TEST(cap_ambient_supported,smoke)72 TEST(cap_ambient_supported, smoke) {
73 cap_ambient_supported();
74 }
75
76 // An invalid path should return an error.
TEST(write_pid_to_path,bad_path)77 TEST(write_pid_to_path, bad_path) {
78 EXPECT_NE(0, write_pid_to_path(0, kNoSuchDir));
79 }
80
81 // Make sure we can write a pid to the file.
TEST(write_pid_to_path,basic)82 TEST(write_pid_to_path, basic) {
83 TemporaryFile tmp;
84 ASSERT_TRUE(tmp.is_valid());
85
86 EXPECT_EQ(0, write_pid_to_path(1234, tmp.path.c_str()));
87 FILE *fp = fopen(tmp.path.c_str(), "re");
88 EXPECT_NE(nullptr, fp);
89 char data[6] = {};
90 EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp));
91 fclose(fp);
92 EXPECT_STREQ(data, "1234\n");
93 }
94
95 // If the destination exists, there's nothing to do.
96 // Also check trailing slash handling.
TEST(mkdir_p,dest_exists)97 TEST(mkdir_p, dest_exists) {
98 EXPECT_EQ(0, mkdir_p("/", 0, true));
99 EXPECT_EQ(0, mkdir_p("///", 0, true));
100 EXPECT_EQ(0, mkdir_p("/proc", 0, true));
101 EXPECT_EQ(0, mkdir_p("/proc/", 0, true));
102 EXPECT_EQ(0, mkdir_p("/dev", 0, true));
103 EXPECT_EQ(0, mkdir_p("/dev/", 0, true));
104 }
105
106 // Create a directory tree that doesn't exist.
TEST(mkdir_p,create_tree)107 TEST(mkdir_p, create_tree) {
108 TemporaryDir dir;
109 ASSERT_TRUE(dir.is_valid());
110
111 // Run `mkdir -p <path>/a/b/c`.
112 std::string path_a = dir.path + "/a";
113 std::string path_a_b = path_a + "/b";
114 std::string path_a_b_c = path_a_b + "/c";
115
116 // First try creating it as a file.
117 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, false));
118
119 // Make sure the final path doesn't exist yet.
120 struct stat st;
121 EXPECT_EQ(0, stat(path_a_b.c_str(), &st));
122 EXPECT_EQ(true, S_ISDIR(st.st_mode));
123 EXPECT_EQ(-1, stat(path_a_b_c.c_str(), &st));
124
125 // Then create it as a complete dir.
126 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, true));
127
128 // Make sure the final dir actually exists.
129 EXPECT_EQ(0, stat(path_a_b_c.c_str(), &st));
130 EXPECT_EQ(true, S_ISDIR(st.st_mode));
131 }
132
133 // Return success on NULL pointer.
TEST(get_mount_flags,null_ptr)134 TEST(get_mount_flags, null_ptr) {
135 ASSERT_EQ(0, get_mount_flags("/proc", nullptr));
136 }
137
138 // Successfully obtain mount flags.
TEST(get_mount_flags,mount_flags)139 TEST(get_mount_flags, mount_flags) {
140 struct statvfs stvfs_buf;
141 ASSERT_EQ(0, statvfs("/proc", &stvfs_buf));
142
143 TemporaryDir dir;
144 ASSERT_TRUE(dir.is_valid());
145
146 unsigned long mount_flags = -1;
147 ASSERT_EQ(0, get_mount_flags("/proc", &mount_flags));
148 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
149
150 // Same thing holds for children of a mount.
151 mount_flags = -1;
152 ASSERT_EQ(0, get_mount_flags("/proc/self", &mount_flags));
153 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
154 }
155
156 // Non-existent paths fail with the proper errno value.
TEST(get_mount_flags,nonexistent_path)157 TEST(get_mount_flags, nonexistent_path) {
158 unsigned long mount_flags = -1;
159 ASSERT_EQ(-ENOENT, get_mount_flags("/does/not/exist", &mount_flags));
160 }
161
162 // If the destination exists, there's nothing to do.
TEST(setup_mount_destination,dest_exists)163 TEST(setup_mount_destination, dest_exists) {
164 // Pick some paths that should always exist. We pass in invalid pointers
165 // for other args so we crash if the dest check doesn't short circuit.
166 EXPECT_EQ(0, setup_mount_destination(nullptr, kValidDir, 0, 0, false));
167 EXPECT_EQ(0, setup_mount_destination(nullptr, "/proc", 0, 0, true));
168 EXPECT_EQ(0, setup_mount_destination(nullptr, "/dev", 0, 0, false));
169 }
170
171 // When given a bind mount where the source is relative, reject it.
TEST(setup_mount_destination,reject_relative_bind)172 TEST(setup_mount_destination, reject_relative_bind) {
173 // Pick a destination we know doesn't exist.
174 EXPECT_NE(0, setup_mount_destination("foo", kNoSuchDir, 0, 0, true));
175 }
176
177 // A mount of a pseudo filesystem should make the destination dir.
TEST(setup_mount_destination,create_pseudo_fs)178 TEST(setup_mount_destination, create_pseudo_fs) {
179 TemporaryDir dir;
180 ASSERT_TRUE(dir.is_valid());
181
182 // Passing -1 for user ID/group ID tells chown to make no changes.
183 std::string no_chmod = dir.path + "/no_chmod";
184 EXPECT_EQ(0, setup_mount_destination("none", no_chmod.c_str(), -1, -1,
185 false));
186 // We check it's a directory by deleting it as such.
187 EXPECT_EQ(0, rmdir(no_chmod.c_str()));
188
189 // Confirm that a bad user ID/group ID fails the function as expected.
190 // On Android, Bionic manages user IDs directly: there is no /etc/passwd file.
191 // This results in most user IDs being valid. Instead of trying to find an
192 // invalid user ID, just skip this check.
193 if (!is_android()) {
194 std::string with_chmod = dir.path + "/with_chmod";
195 EXPECT_NE(0, setup_mount_destination("none", with_chmod.c_str(),
196 UINT_MAX / 2, UINT_MAX / 2, false));
197 }
198 }
199
200 // If the source path does not exist, we should error out.
TEST(setup_mount_destination,missing_source)201 TEST(setup_mount_destination, missing_source) {
202 // The missing dest path is so we can exercise the source logic.
203 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, false));
204 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, true));
205 }
206
207 // A bind mount of a directory should create the destination dir.
TEST(setup_mount_destination,create_bind_dir)208 TEST(setup_mount_destination, create_bind_dir) {
209 TemporaryDir dir;
210 ASSERT_TRUE(dir.is_valid());
211
212 // Passing -1 for user ID/group ID tells chown to make no changes.
213 std::string child_dir = dir.path + "/child_dir";
214 EXPECT_EQ(0, setup_mount_destination(kValidDir, child_dir.c_str(), -1, -1,
215 true));
216 // We check it's a directory by deleting it as such.
217 EXPECT_EQ(0, rmdir(child_dir.c_str()));
218 }
219
220 // A bind mount of a file should create the destination file.
TEST(setup_mount_destination,create_bind_file)221 TEST(setup_mount_destination, create_bind_file) {
222 TemporaryDir dir;
223 ASSERT_TRUE(dir.is_valid());
224
225 // Passing -1 for user ID/group ID tells chown to make no changes.
226 std::string child_file = dir.path + "/child_file";
227 EXPECT_EQ(0, setup_mount_destination(kValidFile, child_file.c_str(), -1, -1,
228 true));
229 // We check it's a file by deleting it as such.
230 EXPECT_EQ(0, unlink(child_file.c_str()));
231 }
232
233 // A mount of a character device should create the destination char.
TEST(setup_mount_destination,create_char_dev)234 TEST(setup_mount_destination, create_char_dev) {
235 TemporaryDir dir;
236 ASSERT_TRUE(dir.is_valid());
237
238 // Passing -1 for user ID/group ID tells chown to make no changes.
239 std::string child_dev = dir.path + "/child_dev";
240 EXPECT_EQ(0, setup_mount_destination(kValidCharDev, child_dev.c_str(), -1, -1,
241 false));
242 // We check it's a directory by deleting it as such.
243 EXPECT_EQ(0, rmdir(child_dev.c_str()));
244 }
245
TEST(seccomp_actions_available,smoke)246 TEST(seccomp_actions_available, smoke) {
247 seccomp_ret_log_available();
248 seccomp_ret_kill_process_available();
249 }
250
TEST(is_canonical_path,basic)251 TEST(is_canonical_path, basic) {
252 EXPECT_FALSE(is_canonical_path("/proc/self"));
253 EXPECT_FALSE(is_canonical_path("relative"));
254 EXPECT_FALSE(is_canonical_path("/proc/./1"));
255 EXPECT_FALSE(is_canonical_path("/proc/../proc/1"));
256
257 EXPECT_TRUE(is_canonical_path("/"));
258 EXPECT_TRUE(is_canonical_path("/proc"));
259 EXPECT_TRUE(is_canonical_path("/proc/1"));
260 }
261
TEST(is_canonical_path,trailing_slash)262 TEST(is_canonical_path, trailing_slash) {
263 EXPECT_TRUE(is_canonical_path("/proc/1/"));
264 EXPECT_FALSE(is_canonical_path("/proc/1//"));
265 }
266