blob: 19b9099bca49c6d29ebda8942cc3eaae69743b2a [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"
Jorge Lucangeli Obes22388b92022-07-13 14:34:47 -040023#include "unittest_util.h"
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040024
25namespace {
26
27// A random path that really really should not exist on the host.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070028constexpr const char kNoSuchDir[] = "/.x/..x/...x/path/should/not/exist/";
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040029
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -040030// A random file that should exist on both Linux and Android.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070031constexpr const char kValidFile[] = "/etc/hosts";
Mike Frysingereaab4202017-08-14 14:57:21 -040032
33// A random directory that should exist.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070034constexpr const char kValidDir[] = "/";
Mike Frysingereaab4202017-08-14 14:57:21 -040035
36// A random character device that should exist.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070037constexpr const char kValidCharDev[] = "/dev/null";
Mike Frysingereaab4202017-08-14 14:57:21 -040038
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040039} // namespace
40
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040041TEST(secure_noroot_set_and_locked, zero_mask) {
42 ASSERT_EQ(secure_noroot_set_and_locked(0), 0);
43}
44
45TEST(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
51TEST(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
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040057// Sanity check for the cap range.
58TEST(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?
72TEST(cap_ambient_supported, smoke) {
73 cap_ambient_supported();
74}
75
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040076// An invalid path should return an error.
77TEST(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.
82TEST(write_pid_to_path, basic) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070083 TemporaryFile tmp;
84 ASSERT_TRUE(tmp.is_valid());
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040085
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -040086 EXPECT_EQ(0, write_pid_to_path(1234, tmp.path.c_str()));
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -070087 FILE *fp = fopen(tmp.path.c_str(), "re");
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040088 EXPECT_NE(nullptr, fp);
lhchavez24b64c22017-09-01 03:52:13 +000089 char data[6] = {};
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040090 EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp));
91 fclose(fp);
Mike Frysinger22dc3522022-07-07 19:24:13 -040092 EXPECT_STREQ(data, "1234\n");
Mike Frysingereaab4202017-08-14 14:57:21 -040093}
94
95// If the destination exists, there's nothing to do.
Mike Frysinger5fdba4e2018-01-17 15:39:48 -050096// Also check trailing slash handling.
97TEST(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.
107TEST(mkdir_p, create_tree) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700108 TemporaryDir dir;
109 ASSERT_TRUE(dir.is_valid());
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500110
111 // Run `mkdir -p <path>/a/b/c`.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700112 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";
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500115
116 // First try creating it as a file.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700117 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, false));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500118
119 // Make sure the final path doesn't exist yet.
120 struct stat st;
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700121 EXPECT_EQ(0, stat(path_a_b.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500122 EXPECT_EQ(true, S_ISDIR(st.st_mode));
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700123 EXPECT_EQ(-1, stat(path_a_b_c.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500124
125 // Then create it as a complete dir.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700126 EXPECT_EQ(0, mkdir_p(path_a_b_c.c_str(), 0700, true));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500127
128 // Make sure the final dir actually exists.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700129 EXPECT_EQ(0, stat(path_a_b_c.c_str(), &st));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500130 EXPECT_EQ(true, S_ISDIR(st.st_mode));
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500131}
132
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400133// Return success on NULL pointer.
134TEST(get_mount_flags, null_ptr) {
135 ASSERT_EQ(0, get_mount_flags("/proc", nullptr));
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700136}
137
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400138// Successfully obtain mount flags.
139TEST(get_mount_flags, mount_flags) {
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700140 struct statvfs stvfs_buf;
141 ASSERT_EQ(0, statvfs("/proc", &stvfs_buf));
142
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700143 TemporaryDir dir;
144 ASSERT_TRUE(dir.is_valid());
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700145
146 unsigned long mount_flags = -1;
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400147 ASSERT_EQ(0, get_mount_flags("/proc", &mount_flags));
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400148 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700149
150 // Same thing holds for children of a mount.
151 mount_flags = -1;
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400152 ASSERT_EQ(0, get_mount_flags("/proc/self", &mount_flags));
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400153 EXPECT_EQ(stvfs_buf.f_flag, mount_flags);
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400154}
155
156// Non-existent paths fail with the proper errno value.
157TEST(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.
163TEST(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));
Mike Frysingereaab4202017-08-14 14:57:21 -0400169}
170
171// When given a bind mount where the source is relative, reject it.
172TEST(setup_mount_destination, reject_relative_bind) {
173 // Pick a destination we know doesn't exist.
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400174 EXPECT_NE(0, setup_mount_destination("foo", kNoSuchDir, 0, 0, true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400175}
176
177// A mount of a pseudo filesystem should make the destination dir.
178TEST(setup_mount_destination, create_pseudo_fs) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700179 TemporaryDir dir;
180 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400181
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400182 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700183 std::string no_chmod = dir.path + "/no_chmod";
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400184 EXPECT_EQ(0, setup_mount_destination("none", no_chmod.c_str(), -1, -1,
185 false));
Mike Frysingereaab4202017-08-14 14:57:21 -0400186 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700187 EXPECT_EQ(0, rmdir(no_chmod.c_str()));
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400188
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()) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700194 std::string with_chmod = dir.path + "/with_chmod";
195 EXPECT_NE(0, setup_mount_destination("none", with_chmod.c_str(),
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400196 UINT_MAX / 2, UINT_MAX / 2, false));
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400197 }
Mike Frysingereaab4202017-08-14 14:57:21 -0400198}
199
200// If the source path does not exist, we should error out.
201TEST(setup_mount_destination, missing_source) {
202 // The missing dest path is so we can exercise the source logic.
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400203 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, false));
204 EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400205}
206
207// A bind mount of a directory should create the destination dir.
208TEST(setup_mount_destination, create_bind_dir) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700209 TemporaryDir dir;
210 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400211
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400212 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700213 std::string child_dir = dir.path + "/child_dir";
214 EXPECT_EQ(0, setup_mount_destination(kValidDir, child_dir.c_str(), -1, -1,
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400215 true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400216 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700217 EXPECT_EQ(0, rmdir(child_dir.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400218}
219
220// A bind mount of a file should create the destination file.
221TEST(setup_mount_destination, create_bind_file) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700222 TemporaryDir dir;
223 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400224
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400225 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700226 std::string child_file = dir.path + "/child_file";
227 EXPECT_EQ(0, setup_mount_destination(kValidFile, child_file.c_str(), -1, -1,
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400228 true));
Mike Frysingereaab4202017-08-14 14:57:21 -0400229 // We check it's a file by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700230 EXPECT_EQ(0, unlink(child_file.c_str()));
Mike Frysingereaab4202017-08-14 14:57:21 -0400231}
232
233// A mount of a character device should create the destination char.
234TEST(setup_mount_destination, create_char_dev) {
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700235 TemporaryDir dir;
236 ASSERT_TRUE(dir.is_valid());
Mike Frysingereaab4202017-08-14 14:57:21 -0400237
Jorge Lucangeli Obes1fc05182018-04-05 10:17:01 -0400238 // Passing -1 for user ID/group ID tells chown to make no changes.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700239 std::string child_dev = dir.path + "/child_dev";
240 EXPECT_EQ(0, setup_mount_destination(kValidCharDev, child_dev.c_str(), -1, -1,
Jorge Lucangeli Obes3ce72e02022-06-07 19:41:11 -0400241 false));
Mike Frysingereaab4202017-08-14 14:57:21 -0400242 // We check it's a directory by deleting it as such.
Luis Hector Chavezc5f4f472018-07-11 12:27:18 -0700243 EXPECT_EQ(0, rmdir(child_dev.c_str()));
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400244}
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400245
246TEST(seccomp_actions_available, smoke) {
247 seccomp_ret_log_available();
248 seccomp_ret_kill_process_available();
249}