blob: 470421db78b9dbc3a94acf927400a0cfc04ef467 [file] [log] [blame]
Mike Frysinger50e31fa2018-01-19 18:59:49 -05001/* Copyright 2017 The Chromium OS 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 * Test system.[ch] module code using gtest.
6 */
Mike Frysinger0b5cffa2017-08-15 18:06:18 -04007
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -07008#include <ftw.h>
yusukes76a9d742018-03-05 10:20:22 -08009#include <limits.h>
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040010#include <linux/securebits.h>
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
Mike Frysinger5fdba4e2018-01-17 15:39:48 -050014#include <sys/stat.h>
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070015#include <sys/statvfs.h>
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040016#include <unistd.h>
17
18#include <gtest/gtest.h>
19
Luis Hector Chavez6bdebb02018-07-10 19:31:27 -070020#include <string>
21
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040022#include "system.h"
23
24namespace {
25
26// A random path that really really should not exist on the host.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070027constexpr const char kNoSuchDir[] = "/.x/..x/...x/path/should/not/exist/";
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040028
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -040029// A random file that should exist on both Linux and Android.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070030constexpr const char kValidFile[] = "/etc/hosts";
Mike Frysingereaab4202017-08-14 14:57:21 -040031
32// A random directory that should exist.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070033constexpr const char kValidDir[] = "/";
Mike Frysingereaab4202017-08-14 14:57:21 -040034
35// A random character device that should exist.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070036constexpr const char kValidCharDev[] = "/dev/null";
Mike Frysingereaab4202017-08-14 14:57:21 -040037
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070038constexpr bool is_android() {
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -040039#if defined(__ANDROID__)
40 return true;
41#else
42 return false;
43#endif
44}
45
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070046// Returns a template path that can be used as an argument to mkstemp / mkdtemp.
47constexpr const char* temp_path_pattern() {
48 if (is_android())
49 return "/data/local/tmp/minijail.tests.XXXXXX";
50 else
51 return "minijail.tests.XXXXXX";
52}
53
54// Recursively deletes the subtree rooted at |path|.
55bool rmdir_recursive(const std::string& path) {
56 auto callback = [](const char* child, const struct stat*, int file_type,
57 struct FTW*) -> int {
58 if (file_type == FTW_DP) {
59 if (rmdir(child) == -1) {
60 fprintf(stderr, "rmdir(%s): %s", child, strerror(errno));
61 return -1;
62 }
63 } else if (file_type == FTW_F) {
64 if (unlink(child) == -1) {
65 fprintf(stderr, "unlink(%s): %s", child, strerror(errno));
66 return -1;
67 }
68 }
69 return 0;
70 };
71
72 return nftw(path.c_str(), callback, 128, FTW_DEPTH) == 0;
73}
74
75// Creates a temporary directory that will be cleaned up upon leaving scope.
76class TemporaryDir {
77 public:
78 TemporaryDir() : path(temp_path_pattern()) {
79 if (mkdtemp(const_cast<char*>(path.c_str())) == nullptr)
80 path.clear();
81 }
82 ~TemporaryDir() {
83 if (!is_valid())
84 return;
85 rmdir_recursive(path.c_str());
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -040086 }
87
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070088 bool is_valid() const { return !path.empty(); }
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040089
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070090 std::string path;
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040091
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070092 private:
93 TemporaryDir(const TemporaryDir&) = delete;
94 TemporaryDir& operator=(const TemporaryDir&) = delete;
95};
96
97// Creates a named temporary file that will be cleaned up upon leaving scope.
98class TemporaryFile {
99 public:
100 TemporaryFile() : path(temp_path_pattern()) {
101 int fd = mkstemp(const_cast<char*>(path.c_str()));
102 if (fd == -1) {
103 path.clear();
104 return;
105 }
106 close(fd);
107 }
108 ~TemporaryFile() {
109 if (!is_valid())
110 return;
111 unlink(path.c_str());
112 }
113
114 bool is_valid() const { return !path.empty(); }
115
116 std::string path;
117
118 private:
119 TemporaryFile(const TemporaryFile&) = delete;
120 TemporaryFile& operator=(const TemporaryFile&) = delete;
121};
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400122
123} // namespace
124
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400125TEST(secure_noroot_set_and_locked, zero_mask) {
126 ASSERT_EQ(secure_noroot_set_and_locked(0), 0);
127}
128
129TEST(secure_noroot_set_and_locked, set) {
130 ASSERT_EQ(secure_noroot_set_and_locked(issecure_mask(SECURE_NOROOT) |
131 issecure_mask(SECURE_NOROOT_LOCKED)),
132 1);
133}
134
135TEST(secure_noroot_set_and_locked, not_set) {
136 ASSERT_EQ(secure_noroot_set_and_locked(issecure_mask(SECURE_KEEP_CAPS) |
137 issecure_mask(SECURE_NOROOT_LOCKED)),
138 0);
139}
140
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400141// Sanity check for the cap range.
142TEST(get_last_valid_cap, basic) {
143 unsigned int cap = get_last_valid_cap();
144
145 // We pick 35 as it's been that since at least v3.0.
146 // If this test is run on older kernels, it might fail.
147 EXPECT_GE(cap, 35u);
148
149 // Pick a really large number that we probably won't hit for a long time.
150 // It helps that caps are bitfields.
151 EXPECT_LT(cap, 128u);
152}
153
154// Might be useful to figure out the return value, but for now,
155// just make sure it doesn't crash?
156TEST(cap_ambient_supported, smoke) {
157 cap_ambient_supported();
158}
159
160// Invalid indexes should return errors, not crash.
161TEST(setup_pipe_end, bad_index) {
162 EXPECT_LT(setup_pipe_end(nullptr, 2), 0);
163 EXPECT_LT(setup_pipe_end(nullptr, 3), 0);
164 EXPECT_LT(setup_pipe_end(nullptr, 4), 0);
165}
166
167// Verify getting the first fd works.
168TEST(setup_pipe_end, index0) {
169 int fds[2];
170 EXPECT_EQ(0, pipe(fds));
171 // This should close fds[1] and return fds[0].
172 EXPECT_EQ(fds[0], setup_pipe_end(fds, 0));
173 // Use close() to verify open/close state.
174 EXPECT_EQ(-1, close(fds[1]));
175 EXPECT_EQ(0, close(fds[0]));
176}
177
178// Verify getting the second fd works.
179TEST(setup_pipe_end, index1) {
180 int fds[2];
181 EXPECT_EQ(0, pipe(fds));
182 // This should close fds[0] and return fds[1].
183 EXPECT_EQ(fds[1], setup_pipe_end(fds, 1));
184 // Use close() to verify open/close state.
185 EXPECT_EQ(-1, close(fds[0]));
186 EXPECT_EQ(0, close(fds[1]));
187}
188
189// Invalid indexes should return errors, not crash.
190TEST(setup_and_dupe_pipe_end, bad_index) {
191 EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 2, -1), 0);
192 EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 3, -1), 0);
193 EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 4, -1), 0);
194}
195
196// An invalid path should return an error.
197TEST(write_pid_to_path, bad_path) {
198 EXPECT_NE(0, write_pid_to_path(0, kNoSuchDir));
199}
200
201// Make sure we can write a pid to the file.
202TEST(write_pid_to_path, basic) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700203 TemporaryFile tmp;
204 ASSERT_TRUE(tmp.is_valid());
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400205
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400206 EXPECT_EQ(0, write_pid_to_path(1234, tmp.path.c_str()));
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700207 FILE *fp = fopen(tmp.path.c_str(), "re");
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400208 EXPECT_NE(nullptr, fp);
lhchavez24b64c22017-09-01 03:52:13 +0000209 char data[6] = {};
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400210 EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp));
211 fclose(fp);
212 EXPECT_EQ(0, strcmp(data, "1234\n"));
Mike Frysingereaab4202017-08-14 14:57:21 -0400213}
214
215// If the destination exists, there's nothing to do.
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500216// Also check trailing slash handling.
217TEST(mkdir_p, dest_exists) {
218 EXPECT_EQ(0, mkdir_p("/", 0, true));
219 EXPECT_EQ(0, mkdir_p("///", 0, true));
220 EXPECT_EQ(0, mkdir_p("/proc", 0, true));
221 EXPECT_EQ(0, mkdir_p("/proc/", 0, true));
222 EXPECT_EQ(0, mkdir_p("/dev", 0, true));
223 EXPECT_EQ(0, mkdir_p("/dev/", 0, true));
224}
225
226// Create a directory tree that doesn't exist.
227TEST(mkdir_p, create_tree) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700228 TemporaryDir dir;
229 ASSERT_TRUE(dir.is_valid());
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500230
231 // Run `mkdir -p <path>/a/b/c`.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700232 std::string path_a = dir.path + "/a";
233 std::string path_a_b = path_a + "/b";
234 std::string path_a_b_c = path_a_b + "/c";
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500235
236 // First try creating it as a file.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700237 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, false));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500238
239 // Make sure the final path doesn't exist yet.
240 struct stat st;
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700241 EXPECT_EQ(0, stat(path_a_b.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500242 EXPECT_EQ(true, S_ISDIR(st.st_mode));
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700243 EXPECT_EQ(-1, stat(path_a_b_c.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500244
245 // Then create it as a complete dir.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700246 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, true));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500247
248 // Make sure the final dir actually exists.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700249 EXPECT_EQ(0, stat(path_a_b_c.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500250 EXPECT_EQ(true, S_ISDIR(st.st_mode));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500251}
252
253// If the destination exists, there's nothing to do.
Mike Frysingereaab4202017-08-14 14:57:21 -0400254TEST(setup_mount_destination, dest_exists) {
255 // Pick some paths that should always exist. We pass in invalid pointers
256 // for other args so we crash if the dest check doesn't short circuit.
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700257 EXPECT_EQ(0, setup_mount_destination(nullptr, kValidDir, 0, 0, false,
258 nullptr));
259 EXPECT_EQ(0, setup_mount_destination(nullptr, "/proc", 0, 0, true, nullptr));
260 EXPECT_EQ(0, setup_mount_destination(nullptr, "/dev", 0, 0, false, nullptr));
261}
262
263// Mount flags should be obtained for bind-mounts
264TEST(setup_mount_destination, mount_flags) {
265 struct statvfs stvfs_buf;
266 ASSERT_EQ(0, statvfs("/proc", &stvfs_buf));
267
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700268 TemporaryDir dir;
269 ASSERT_TRUE(dir.is_valid());
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700270
271 unsigned long mount_flags = -1;
272 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700273 std::string proc = dir.path + "/proc";
274 EXPECT_EQ(0, setup_mount_destination("/proc", proc.c_str(), -1, -1, true,
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700275 &mount_flags));
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400276 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700277 EXPECT_EQ(0, rmdir(proc.c_str()));
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700278
279 // Same thing holds for children of a mount.
280 mount_flags = -1;
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700281 std::string proc_self = dir.path + "/proc_self";
282 EXPECT_EQ(0, setup_mount_destination("/proc/self", proc_self.c_str(), -1, -1,
283 true, &mount_flags));
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400284 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700285 EXPECT_EQ(0, rmdir(proc_self.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400286}
287
288// When given a bind mount where the source is relative, reject it.
289TEST(setup_mount_destination, reject_relative_bind) {
290 // Pick a destination we know doesn't exist.
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700291 EXPECT_NE(0, setup_mount_destination("foo", kNoSuchDir, 0, 0, true, nullptr));
Mike Frysingereaab4202017-08-14 14:57:21 -0400292}
293
294// A mount of a pseudo filesystem should make the destination dir.
295TEST(setup_mount_destination, create_pseudo_fs) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700296 TemporaryDir dir;
297 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400298
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400299 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700300 std::string no_chmod = dir.path + "/no_chmod";
301 EXPECT_EQ(0, setup_mount_destination("none", no_chmod.c_str(), -1, -1, false,
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700302 nullptr));
Mike Frysingereaab4202017-08-14 14:57:21 -0400303 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700304 EXPECT_EQ(0, rmdir(no_chmod.c_str()));
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400305
306 // Confirm that a bad user ID/group ID fails the function as expected.
307 // On Android, Bionic manages user IDs directly: there is no /etc/passwd file.
308 // This results in most user IDs being valid. Instead of trying to find an
309 // invalid user ID, just skip this check.
310 if (!is_android()) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700311 std::string with_chmod = dir.path + "/with_chmod";
312 EXPECT_NE(0, setup_mount_destination("none", with_chmod.c_str(),
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700313 UINT_MAX / 2, UINT_MAX / 2, false,
314 nullptr));
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400315 }
Mike Frysingereaab4202017-08-14 14:57:21 -0400316}
317
318// If the source path does not exist, we should error out.
319TEST(setup_mount_destination, missing_source) {
320 // The missing dest path is so we can exercise the source logic.
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700321 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, false,
322 nullptr));
323 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, true,
324 nullptr));
Mike Frysingereaab4202017-08-14 14:57:21 -0400325}
326
327// A bind mount of a directory should create the destination dir.
328TEST(setup_mount_destination, create_bind_dir) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700329 TemporaryDir dir;
330 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400331
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400332 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700333 std::string child_dir = dir.path + "/child_dir";
334 EXPECT_EQ(0, setup_mount_destination(kValidDir, child_dir.c_str(), -1, -1,
335 true, nullptr));
Mike Frysingereaab4202017-08-14 14:57:21 -0400336 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700337 EXPECT_EQ(0, rmdir(child_dir.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400338}
339
340// A bind mount of a file should create the destination file.
341TEST(setup_mount_destination, create_bind_file) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700342 TemporaryDir dir;
343 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400344
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400345 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700346 std::string child_file = dir.path + "/child_file";
347 EXPECT_EQ(0, setup_mount_destination(kValidFile, child_file.c_str(), -1, -1,
348 true, nullptr));
Mike Frysingereaab4202017-08-14 14:57:21 -0400349 // We check it's a file by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700350 EXPECT_EQ(0, unlink(child_file.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400351}
352
353// A mount of a character device should create the destination char.
354TEST(setup_mount_destination, create_char_dev) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700355 TemporaryDir dir;
356 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400357
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400358 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700359 std::string child_dev = dir.path + "/child_dev";
360 EXPECT_EQ(0, setup_mount_destination(kValidCharDev, child_dev.c_str(), -1, -1,
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700361 false, nullptr));
Mike Frysingereaab4202017-08-14 14:57:21 -0400362 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700363 EXPECT_EQ(0, rmdir(child_dev.c_str()));
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400364}
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400365
366TEST(seccomp_actions_available, smoke) {
367 seccomp_ret_log_available();
368 seccomp_ret_kill_process_available();
369}