blob: 92b7ab98b356fad4fcc22bbfceff6fefaa467af2 [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
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400160// An invalid path should return an error.
161TEST(write_pid_to_path, bad_path) {
162 EXPECT_NE(0, write_pid_to_path(0, kNoSuchDir));
163}
164
165// Make sure we can write a pid to the file.
166TEST(write_pid_to_path, basic) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700167 TemporaryFile tmp;
168 ASSERT_TRUE(tmp.is_valid());
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400169
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400170 EXPECT_EQ(0, write_pid_to_path(1234, tmp.path.c_str()));
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700171 FILE *fp = fopen(tmp.path.c_str(), "re");
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400172 EXPECT_NE(nullptr, fp);
lhchavez24b64c22017-09-01 03:52:13 +0000173 char data[6] = {};
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400174 EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp));
175 fclose(fp);
Mike Frysinger22dc3522022-07-07 19:24:13 -0400176 EXPECT_STREQ(data, "1234\n");
Mike Frysingereaab4202017-08-14 14:57:21 -0400177}
178
179// If the destination exists, there's nothing to do.
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500180// Also check trailing slash handling.
181TEST(mkdir_p, dest_exists) {
182 EXPECT_EQ(0, mkdir_p("/", 0, true));
183 EXPECT_EQ(0, mkdir_p("///", 0, true));
184 EXPECT_EQ(0, mkdir_p("/proc", 0, true));
185 EXPECT_EQ(0, mkdir_p("/proc/", 0, true));
186 EXPECT_EQ(0, mkdir_p("/dev", 0, true));
187 EXPECT_EQ(0, mkdir_p("/dev/", 0, true));
188}
189
190// Create a directory tree that doesn't exist.
191TEST(mkdir_p, create_tree) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700192 TemporaryDir dir;
193 ASSERT_TRUE(dir.is_valid());
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500194
195 // Run `mkdir -p <path>/a/b/c`.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700196 std::string path_a = dir.path + "/a";
197 std::string path_a_b = path_a + "/b";
198 std::string path_a_b_c = path_a_b + "/c";
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500199
200 // First try creating it as a file.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700201 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, false));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500202
203 // Make sure the final path doesn't exist yet.
204 struct stat st;
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700205 EXPECT_EQ(0, stat(path_a_b.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500206 EXPECT_EQ(true, S_ISDIR(st.st_mode));
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700207 EXPECT_EQ(-1, stat(path_a_b_c.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500208
209 // Then create it as a complete dir.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700210 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, true));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500211
212 // Make sure the final dir actually exists.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700213 EXPECT_EQ(0, stat(path_a_b_c.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500214 EXPECT_EQ(true, S_ISDIR(st.st_mode));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500215}
216
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400217// Return success on NULL pointer.
218TEST(get_mount_flags, null_ptr) {
219 ASSERT_EQ(0, get_mount_flags("/proc", nullptr));
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700220}
221
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400222// Successfully obtain mount flags.
223TEST(get_mount_flags, mount_flags) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700224 struct statvfs stvfs_buf;
225 ASSERT_EQ(0, statvfs("/proc", &stvfs_buf));
226
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700227 TemporaryDir dir;
228 ASSERT_TRUE(dir.is_valid());
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700229
230 unsigned long mount_flags = -1;
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400231 ASSERT_EQ(0, get_mount_flags("/proc", &mount_flags));
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400232 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700233
234 // Same thing holds for children of a mount.
235 mount_flags = -1;
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400236 ASSERT_EQ(0, get_mount_flags("/proc/self", &mount_flags));
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400237 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400238}
239
240// Non-existent paths fail with the proper errno value.
241TEST(get_mount_flags, nonexistent_path) {
242 unsigned long mount_flags = -1;
243 ASSERT_EQ(-ENOENT, get_mount_flags("/does/not/exist", &mount_flags));
244}
245
246// If the destination exists, there's nothing to do.
247TEST(setup_mount_destination, dest_exists) {
248 // Pick some paths that should always exist. We pass in invalid pointers
249 // for other args so we crash if the dest check doesn't short circuit.
250 EXPECT_EQ(0, setup_mount_destination(nullptr, kValidDir, 0, 0, false));
251 EXPECT_EQ(0, setup_mount_destination(nullptr, "/proc", 0, 0, true));
252 EXPECT_EQ(0, setup_mount_destination(nullptr, "/dev", 0, 0, false));
Mike Frysingereaab4202017-08-14 14:57:21 -0400253}
254
255// When given a bind mount where the source is relative, reject it.
256TEST(setup_mount_destination, reject_relative_bind) {
257 // Pick a destination we know doesn't exist.
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400258 EXPECT_NE(0, setup_mount_destination("foo", kNoSuchDir, 0, 0, true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400259}
260
261// A mount of a pseudo filesystem should make the destination dir.
262TEST(setup_mount_destination, create_pseudo_fs) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700263 TemporaryDir dir;
264 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400265
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400266 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700267 std::string no_chmod = dir.path + "/no_chmod";
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400268 EXPECT_EQ(0, setup_mount_destination("none", no_chmod.c_str(), -1, -1,
269 false));
Mike Frysingereaab4202017-08-14 14:57:21 -0400270 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700271 EXPECT_EQ(0, rmdir(no_chmod.c_str()));
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400272
273 // Confirm that a bad user ID/group ID fails the function as expected.
274 // On Android, Bionic manages user IDs directly: there is no /etc/passwd file.
275 // This results in most user IDs being valid. Instead of trying to find an
276 // invalid user ID, just skip this check.
277 if (!is_android()) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700278 std::string with_chmod = dir.path + "/with_chmod";
279 EXPECT_NE(0, setup_mount_destination("none", with_chmod.c_str(),
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400280 UINT_MAX / 2, UINT_MAX / 2, false));
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400281 }
Mike Frysingereaab4202017-08-14 14:57:21 -0400282}
283
284// If the source path does not exist, we should error out.
285TEST(setup_mount_destination, missing_source) {
286 // The missing dest path is so we can exercise the source logic.
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400287 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, false));
288 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400289}
290
291// A bind mount of a directory should create the destination dir.
292TEST(setup_mount_destination, create_bind_dir) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700293 TemporaryDir dir;
294 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400295
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400296 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700297 std::string child_dir = dir.path + "/child_dir";
298 EXPECT_EQ(0, setup_mount_destination(kValidDir, child_dir.c_str(), -1, -1,
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400299 true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400300 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700301 EXPECT_EQ(0, rmdir(child_dir.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400302}
303
304// A bind mount of a file should create the destination file.
305TEST(setup_mount_destination, create_bind_file) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700306 TemporaryDir dir;
307 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400308
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400309 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700310 std::string child_file = dir.path + "/child_file";
311 EXPECT_EQ(0, setup_mount_destination(kValidFile, child_file.c_str(), -1, -1,
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400312 true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400313 // We check it's a file by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700314 EXPECT_EQ(0, unlink(child_file.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400315}
316
317// A mount of a character device should create the destination char.
318TEST(setup_mount_destination, create_char_dev) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700319 TemporaryDir dir;
320 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400321
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400322 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700323 std::string child_dev = dir.path + "/child_dev";
324 EXPECT_EQ(0, setup_mount_destination(kValidCharDev, child_dev.c_str(), -1, -1,
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400325 false));
Mike Frysingereaab4202017-08-14 14:57:21 -0400326 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700327 EXPECT_EQ(0, rmdir(child_dev.c_str()));
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400328}
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400329
330TEST(seccomp_actions_available, smoke) {
331 seccomp_ret_log_available();
332 seccomp_ret_kill_process_available();
333}