Mike Frysinger | 50e31fa | 2018-01-19 18:59:49 -0500 | [diff] [blame] | 1 | /* 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 Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 7 | |
yusukes | 76a9d74 | 2018-03-05 10:20:22 -0800 | [diff] [blame] | 8 | #include <limits.h> |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 12 | #include <sys/stat.h> |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | #include "system.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // A random path that really really should not exist on the host. |
| 22 | const char kNoSuchDir[] = "/.x/..x/...x/path/should/not/exist/"; |
| 23 | |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 24 | // A random file that should exist on both Linux and Android. |
| 25 | const char kValidFile[] = "/etc/hosts"; |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 26 | |
| 27 | // A random directory that should exist. |
| 28 | const char kValidDir[] = "/"; |
| 29 | |
| 30 | // A random character device that should exist. |
| 31 | const char kValidCharDev[] = "/dev/null"; |
| 32 | |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 33 | inline bool is_android() { |
| 34 | #if defined(__ANDROID__) |
| 35 | return true; |
| 36 | #else |
| 37 | return false; |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | // Return a temp filename that this test can manipulate. |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 42 | // It will not exist when it returns, and the user has to free the memory. |
| 43 | char *get_temp_path() { |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 44 | char *path = nullptr; |
| 45 | if (is_android()) { |
| 46 | path = strdup("/data/local/tmp/minijail.tests.XXXXXX"); |
| 47 | } else { |
| 48 | path = strdup("minijail.tests.XXXXXX"); |
| 49 | } |
| 50 | |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 51 | if (!path) |
| 52 | return nullptr; |
| 53 | |
| 54 | // Just create the temp path. |
| 55 | int fd = mkstemp(path); |
| 56 | if (fd < 0) |
| 57 | return nullptr; |
| 58 | close(fd); |
| 59 | unlink(path); |
| 60 | |
| 61 | return path; |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | // Sanity check for the cap range. |
| 67 | TEST(get_last_valid_cap, basic) { |
| 68 | unsigned int cap = get_last_valid_cap(); |
| 69 | |
| 70 | // We pick 35 as it's been that since at least v3.0. |
| 71 | // If this test is run on older kernels, it might fail. |
| 72 | EXPECT_GE(cap, 35u); |
| 73 | |
| 74 | // Pick a really large number that we probably won't hit for a long time. |
| 75 | // It helps that caps are bitfields. |
| 76 | EXPECT_LT(cap, 128u); |
| 77 | } |
| 78 | |
| 79 | // Might be useful to figure out the return value, but for now, |
| 80 | // just make sure it doesn't crash? |
| 81 | TEST(cap_ambient_supported, smoke) { |
| 82 | cap_ambient_supported(); |
| 83 | } |
| 84 | |
| 85 | // Invalid indexes should return errors, not crash. |
| 86 | TEST(setup_pipe_end, bad_index) { |
| 87 | EXPECT_LT(setup_pipe_end(nullptr, 2), 0); |
| 88 | EXPECT_LT(setup_pipe_end(nullptr, 3), 0); |
| 89 | EXPECT_LT(setup_pipe_end(nullptr, 4), 0); |
| 90 | } |
| 91 | |
| 92 | // Verify getting the first fd works. |
| 93 | TEST(setup_pipe_end, index0) { |
| 94 | int fds[2]; |
| 95 | EXPECT_EQ(0, pipe(fds)); |
| 96 | // This should close fds[1] and return fds[0]. |
| 97 | EXPECT_EQ(fds[0], setup_pipe_end(fds, 0)); |
| 98 | // Use close() to verify open/close state. |
| 99 | EXPECT_EQ(-1, close(fds[1])); |
| 100 | EXPECT_EQ(0, close(fds[0])); |
| 101 | } |
| 102 | |
| 103 | // Verify getting the second fd works. |
| 104 | TEST(setup_pipe_end, index1) { |
| 105 | int fds[2]; |
| 106 | EXPECT_EQ(0, pipe(fds)); |
| 107 | // This should close fds[0] and return fds[1]. |
| 108 | EXPECT_EQ(fds[1], setup_pipe_end(fds, 1)); |
| 109 | // Use close() to verify open/close state. |
| 110 | EXPECT_EQ(-1, close(fds[0])); |
| 111 | EXPECT_EQ(0, close(fds[1])); |
| 112 | } |
| 113 | |
| 114 | // Invalid indexes should return errors, not crash. |
| 115 | TEST(setup_and_dupe_pipe_end, bad_index) { |
| 116 | EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 2, -1), 0); |
| 117 | EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 3, -1), 0); |
| 118 | EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 4, -1), 0); |
| 119 | } |
| 120 | |
| 121 | // An invalid path should return an error. |
| 122 | TEST(write_pid_to_path, bad_path) { |
| 123 | EXPECT_NE(0, write_pid_to_path(0, kNoSuchDir)); |
| 124 | } |
| 125 | |
| 126 | // Make sure we can write a pid to the file. |
| 127 | TEST(write_pid_to_path, basic) { |
| 128 | char *path = get_temp_path(); |
| 129 | ASSERT_NE(nullptr, path); |
| 130 | |
| 131 | EXPECT_EQ(0, write_pid_to_path(1234, path)); |
| 132 | FILE *fp = fopen(path, "re"); |
| 133 | unlink(path); |
| 134 | EXPECT_NE(nullptr, fp); |
lhchavez | 24b64c2 | 2017-09-01 03:52:13 +0000 | [diff] [blame] | 135 | char data[6] = {}; |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 136 | EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp)); |
| 137 | fclose(fp); |
| 138 | EXPECT_EQ(0, strcmp(data, "1234\n")); |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 139 | |
| 140 | free(path); |
| 141 | } |
| 142 | |
| 143 | // If the destination exists, there's nothing to do. |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 144 | // Also check trailing slash handling. |
| 145 | TEST(mkdir_p, dest_exists) { |
| 146 | EXPECT_EQ(0, mkdir_p("/", 0, true)); |
| 147 | EXPECT_EQ(0, mkdir_p("///", 0, true)); |
| 148 | EXPECT_EQ(0, mkdir_p("/proc", 0, true)); |
| 149 | EXPECT_EQ(0, mkdir_p("/proc/", 0, true)); |
| 150 | EXPECT_EQ(0, mkdir_p("/dev", 0, true)); |
| 151 | EXPECT_EQ(0, mkdir_p("/dev/", 0, true)); |
| 152 | } |
| 153 | |
| 154 | // Create a directory tree that doesn't exist. |
| 155 | TEST(mkdir_p, create_tree) { |
| 156 | char *path = get_temp_path(); |
| 157 | ASSERT_NE(nullptr, path); |
| 158 | unlink(path); |
| 159 | |
| 160 | // Run `mkdir -p <path>/a/b/c`. |
| 161 | char *path_a, *path_a_b, *path_a_b_c; |
| 162 | ASSERT_NE(-1, asprintf(&path_a, "%s/a", path)); |
| 163 | ASSERT_NE(-1, asprintf(&path_a_b, "%s/b", path_a)); |
| 164 | ASSERT_NE(-1, asprintf(&path_a_b_c, "%s/c", path_a_b)); |
| 165 | |
| 166 | // First try creating it as a file. |
| 167 | EXPECT_EQ(0, mkdir_p(path_a_b_c, 0700, false)); |
| 168 | |
| 169 | // Make sure the final path doesn't exist yet. |
| 170 | struct stat st; |
| 171 | EXPECT_EQ(0, stat(path_a_b, &st)); |
| 172 | EXPECT_EQ(true, S_ISDIR(st.st_mode)); |
| 173 | EXPECT_EQ(-1, stat(path_a_b_c, &st)); |
| 174 | |
| 175 | // Then create it as a complete dir. |
| 176 | EXPECT_EQ(0, mkdir_p(path_a_b_c, 0700, true)); |
| 177 | |
| 178 | // Make sure the final dir actually exists. |
| 179 | EXPECT_EQ(0, stat(path_a_b_c, &st)); |
| 180 | EXPECT_EQ(true, S_ISDIR(st.st_mode)); |
| 181 | |
| 182 | // Clean up. |
| 183 | ASSERT_EQ(0, rmdir(path_a_b_c)); |
| 184 | ASSERT_EQ(0, rmdir(path_a_b)); |
| 185 | ASSERT_EQ(0, rmdir(path_a)); |
| 186 | ASSERT_EQ(0, rmdir(path)); |
| 187 | |
| 188 | free(path_a_b_c); |
| 189 | free(path_a_b); |
| 190 | free(path_a); |
| 191 | free(path); |
| 192 | } |
| 193 | |
| 194 | // If the destination exists, there's nothing to do. |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 195 | TEST(setup_mount_destination, dest_exists) { |
| 196 | // Pick some paths that should always exist. We pass in invalid pointers |
| 197 | // for other args so we crash if the dest check doesn't short circuit. |
| 198 | EXPECT_EQ(0, setup_mount_destination(nullptr, kValidDir, 0, 0, false)); |
| 199 | EXPECT_EQ(0, setup_mount_destination(nullptr, "/proc", 0, 0, true)); |
| 200 | EXPECT_EQ(0, setup_mount_destination(nullptr, "/dev", 0, 0, false)); |
| 201 | } |
| 202 | |
| 203 | // When given a bind mount where the source is relative, reject it. |
| 204 | TEST(setup_mount_destination, reject_relative_bind) { |
| 205 | // Pick a destination we know doesn't exist. |
| 206 | EXPECT_NE(0, setup_mount_destination("foo", kNoSuchDir, 0, 0, true)); |
| 207 | } |
| 208 | |
| 209 | // A mount of a pseudo filesystem should make the destination dir. |
| 210 | TEST(setup_mount_destination, create_pseudo_fs) { |
| 211 | char *path = get_temp_path(); |
| 212 | ASSERT_NE(nullptr, path); |
| 213 | |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 214 | // Passing -1 for user ID/group ID tells chown to make no changes. |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 215 | EXPECT_EQ(0, setup_mount_destination("none", path, -1, -1, false)); |
| 216 | // We check it's a directory by deleting it as such. |
| 217 | EXPECT_EQ(0, rmdir(path)); |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 218 | |
| 219 | // Confirm that a bad user ID/group ID fails the function as expected. |
| 220 | // On Android, Bionic manages user IDs directly: there is no /etc/passwd file. |
| 221 | // This results in most user IDs being valid. Instead of trying to find an |
| 222 | // invalid user ID, just skip this check. |
| 223 | if (!is_android()) { |
| 224 | EXPECT_NE(0, setup_mount_destination("none", path, |
| 225 | UINT_MAX / 2, UINT_MAX / 2, false)); |
| 226 | } |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 227 | |
| 228 | free(path); |
| 229 | } |
| 230 | |
| 231 | // If the source path does not exist, we should error out. |
| 232 | TEST(setup_mount_destination, missing_source) { |
| 233 | // The missing dest path is so we can exercise the source logic. |
| 234 | EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, false)); |
| 235 | EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, true)); |
| 236 | } |
| 237 | |
| 238 | // A bind mount of a directory should create the destination dir. |
| 239 | TEST(setup_mount_destination, create_bind_dir) { |
| 240 | char *path = get_temp_path(); |
| 241 | ASSERT_NE(nullptr, path); |
| 242 | |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 243 | // Passing -1 for user ID/group ID tells chown to make no changes. |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 244 | EXPECT_EQ(0, setup_mount_destination(kValidDir, path, -1, -1, true)); |
| 245 | // We check it's a directory by deleting it as such. |
| 246 | EXPECT_EQ(0, rmdir(path)); |
| 247 | |
| 248 | free(path); |
| 249 | } |
| 250 | |
| 251 | // A bind mount of a file should create the destination file. |
| 252 | TEST(setup_mount_destination, create_bind_file) { |
| 253 | char *path = get_temp_path(); |
| 254 | ASSERT_NE(nullptr, path); |
| 255 | |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 256 | // Passing -1 for user ID/group ID tells chown to make no changes. |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 257 | EXPECT_EQ(0, setup_mount_destination(kValidFile, path, -1, -1, true)); |
| 258 | // We check it's a file by deleting it as such. |
| 259 | EXPECT_EQ(0, unlink(path)); |
| 260 | |
| 261 | free(path); |
| 262 | } |
| 263 | |
| 264 | // A mount of a character device should create the destination char. |
| 265 | TEST(setup_mount_destination, create_char_dev) { |
| 266 | char *path = get_temp_path(); |
| 267 | ASSERT_NE(nullptr, path); |
| 268 | |
Jorge Lucangeli Obes | 1fc0518 | 2018-04-05 10:17:01 -0400 | [diff] [blame] | 269 | // Passing -1 for user ID/group ID tells chown to make no changes. |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 270 | EXPECT_EQ(0, setup_mount_destination(kValidCharDev, path, -1, -1, false)); |
| 271 | // We check it's a directory by deleting it as such. |
| 272 | EXPECT_EQ(0, rmdir(path)); |
| 273 | |
| 274 | free(path); |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 275 | } |