Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #import <Foundation/Foundation.h> |
| 6 | #import <IOSurface/IOSurface.h> |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 7 | #include <ifaddrs.h> |
| 8 | #include <servers/bootstrap.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/sysctl.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 15 | #include <string_view> |
| 16 | |
Avi Drissman | d4f0708 | 2023-05-12 18:05:44 | [diff] [blame] | 17 | #include "base/apple/bundle_locations.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 18 | #include "base/files/file_util.h" |
| 19 | #include "base/files/scoped_temp_dir.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 20 | #include "base/mac/mac_util.h" |
| 21 | #include "base/process/kill.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 22 | #include "base/system/sys_info.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 23 | #include "base/test/multiprocess_test.h" |
| 24 | #include "base/test/test_timeouts.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 25 | #include "content/test/test_content_client.h" |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 26 | #include "sandbox/mac/sandbox_serializer.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 27 | #include "sandbox/mac/seatbelt_exec.h" |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 28 | #include "sandbox/policy/mac/common.sb.h" |
Robert Sesek | 7838cee1 | 2021-04-14 18:39:22 | [diff] [blame] | 29 | #include "sandbox/policy/mac/params.h" |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 30 | #include "sandbox/policy/mac/renderer.sb.h" |
| 31 | #include "sandbox/policy/mac/sandbox_mac.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | #include "testing/multiprocess_func_list.h" |
| 34 | |
| 35 | namespace content { |
| 36 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 37 | using sandbox::SandboxSerializer; |
| 38 | |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 39 | namespace { |
| 40 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 41 | void SetParametersForTest(SandboxSerializer* serializer, |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 42 | const base::FilePath& logging_path, |
Matthew Denton | 587371c | 2023-06-22 14:49:58 | [diff] [blame] | 43 | const base::FilePath& executable_path, |
| 44 | bool use_syscall_filter) { |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 45 | bool enable_logging = true; |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 46 | CHECK(serializer->SetBooleanParameter(sandbox::policy::kParamEnableLogging, |
| 47 | enable_logging)); |
| 48 | CHECK(serializer->SetBooleanParameter( |
Robert Sesek | 7838cee1 | 2021-04-14 18:39:22 | [diff] [blame] | 49 | sandbox::policy::kParamDisableSandboxDenialLogging, !enable_logging)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 50 | |
| 51 | std::string homedir = |
Robert Sesek | 5aef352 | 2021-04-14 22:48:23 | [diff] [blame] | 52 | sandbox::policy::GetCanonicalPath(base::GetHomeDir()).value(); |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 53 | CHECK(serializer->SetParameter(sandbox::policy::kParamHomedirAsLiteral, |
| 54 | homedir)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 55 | |
| 56 | int32_t major_version, minor_version, bugfix_version; |
| 57 | base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version, |
| 58 | &bugfix_version); |
| 59 | int32_t os_version = (major_version * 100) + minor_version; |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 60 | CHECK(serializer->SetParameter(sandbox::policy::kParamOsVersion, |
| 61 | base::NumberToString(os_version))); |
Tom Sepez | 305e0d0d | 2017-10-19 20:48:52 | [diff] [blame] | 62 | |
| 63 | std::string bundle_path = |
Avi Drissman | d4f0708 | 2023-05-12 18:05:44 | [diff] [blame] | 64 | sandbox::policy::GetCanonicalPath(base::apple::MainBundlePath()).value(); |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 65 | CHECK( |
| 66 | serializer->SetParameter(sandbox::policy::kParamBundlePath, bundle_path)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 67 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 68 | CHECK(serializer->SetParameter(sandbox::policy::kParamBundleId, |
| 69 | "com.google.Chrome.test.sandbox")); |
| 70 | CHECK(serializer->SetParameter(sandbox::policy::kParamBrowserPid, |
| 71 | base::NumberToString(getpid()))); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 72 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 73 | CHECK(serializer->SetParameter(sandbox::policy::kParamLogFilePath, |
| 74 | logging_path.value())); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 75 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 76 | CHECK(serializer->SetParameter(sandbox::policy::kParamExecutablePath, |
| 77 | executable_path.value())); |
Greg Kerr | f46f3187 | 2019-08-08 20:22:44 | [diff] [blame] | 78 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 79 | CHECK(serializer->SetBooleanParameter(sandbox::policy::kParamFilterSyscalls, |
| 80 | use_syscall_filter)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | // These tests check that the V2 sandbox compiles, initializes, and |
| 86 | // correctly enforces resource access on all macOS versions. Note that |
| 87 | // with the exception of certain controlled locations, such as a dummy |
| 88 | // log file, these tests cannot check that write access to system files |
| 89 | // is blocked. These tests run on developers' machines and bots, so |
| 90 | // if the write access goes through, that machine could be corrupted. |
| 91 | class SandboxV2Test : public base::MultiProcessTest {}; |
| 92 | |
| 93 | MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) { |
| 94 | TestContentClient content_client; |
Greg Kerr | c382e2ae | 2017-12-14 23:43:34 | [diff] [blame] | 95 | const std::string profile = |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 96 | std::string(sandbox::policy::kSeatbeltPolicyString_common) + |
| 97 | sandbox::policy::kSeatbeltPolicyString_renderer; |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 98 | SandboxSerializer serializer(SandboxSerializer::Target::kSource); |
| 99 | serializer.SetProfile(profile); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 100 | |
| 101 | // Create the logging file and pass /bin/ls as the executable path. |
| 102 | base::ScopedTempDir temp_dir; |
| 103 | CHECK(temp_dir.CreateUniqueTempDir()); |
| 104 | CHECK(temp_dir.IsValid()); |
| 105 | base::FilePath temp_path = temp_dir.GetPath(); |
Robert Sesek | 5aef352 | 2021-04-14 22:48:23 | [diff] [blame] | 106 | temp_path = sandbox::policy::GetCanonicalPath(temp_path); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 107 | const base::FilePath log_file = temp_path.Append("log-file"); |
| 108 | const base::FilePath exec_file("/bin/ls"); |
| 109 | |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 110 | // TODO(crbug.com/40273168): re-enable syscall filter for this test. |
Matthew Denton | 587371c | 2023-06-22 14:49:58 | [diff] [blame] | 111 | // SandboxV2Test.SandboxProfileTest uses system() which uses a denied syscall, |
| 112 | // which should cause the test to fail. |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 113 | SetParametersForTest(&serializer, log_file, exec_file, |
Matthew Denton | 587371c | 2023-06-22 14:49:58 | [diff] [blame] | 114 | /*use_syscall_filter=*/false); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 115 | |
Evan Stade | 526e35a6 | 2025-02-01 00:09:37 | [diff] [blame^] | 116 | std::string error, serialized; |
| 117 | CHECK(serializer.SerializePolicy(serialized, error)) << error; |
| 118 | CHECK(serializer.ApplySerializedPolicy(serialized)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 119 | |
| 120 | // Test the properties of the sandbox profile. |
Md Hasibul Hasan | 8d44515 | 2024-04-11 07:38:50 | [diff] [blame] | 121 | constexpr std::string_view log_msg = "logged"; |
| 122 | CHECK(base::WriteFile(log_file, std::string_view(log_msg))); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 123 | // Log file is write only. |
Claudio DeSouza | 5150928 | 2023-02-25 17:01:08 | [diff] [blame] | 124 | char read_buf[log_msg.size()]; |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 125 | CHECK_EQ(-1, base::ReadFile(log_file, read_buf, sizeof(read_buf))); |
| 126 | |
| 127 | // Try executing the blessed binary. |
| 128 | CHECK_NE(-1, system(exec_file.value().c_str())); |
| 129 | |
| 130 | // Try and realpath a file. |
| 131 | char resolved_name[4096]; |
| 132 | CHECK_NE(nullptr, realpath(log_file.value().c_str(), resolved_name)); |
| 133 | |
| 134 | // Test shared memory access. |
| 135 | int shm_fd = shm_open("apple.shm.notification_center", O_RDONLY, 0644); |
| 136 | CHECK_GE(shm_fd, 0); |
| 137 | |
| 138 | // Test mach service access. The port is leaked because the multiprocess |
| 139 | // test exits quickly after this look up. |
| 140 | mach_port_t service_port; |
| 141 | kern_return_t status = bootstrap_look_up( |
| 142 | bootstrap_port, "com.apple.system.logger", &service_port); |
| 143 | CHECK_EQ(status, BOOTSTRAP_SUCCESS) << bootstrap_strerror(status); |
| 144 | |
Greg Kerr | 6516911d | 2017-11-27 23:00:37 | [diff] [blame] | 145 | mach_port_t forbidden_mach; |
| 146 | status = bootstrap_look_up(bootstrap_port, "com.apple.cfprefsd.daemon", |
| 147 | &forbidden_mach); |
| 148 | CHECK_NE(BOOTSTRAP_SUCCESS, status); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 149 | |
| 150 | // Read bundle contents. |
Avi Drissman | d4f0708 | 2023-05-12 18:05:44 | [diff] [blame] | 151 | base::FilePath bundle_path = base::apple::MainBundlePath(); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 152 | struct stat st; |
| 153 | CHECK_NE(-1, stat(bundle_path.value().c_str(), &st)); |
| 154 | |
| 155 | // Test that general file system access isn't available. |
| 156 | base::FilePath ascii_path("/usr/share/misc/ascii"); |
| 157 | std::string ascii_contents; |
| 158 | CHECK(!base::ReadFileToStringWithMaxSize(ascii_path, &ascii_contents, 4096)); |
| 159 | |
| 160 | base::FilePath system_certs( |
| 161 | "/System/Library/Keychains/SystemRootCertificates.keychain"); |
| 162 | std::string keychain_contents; |
| 163 | CHECK(!base::ReadFileToStringWithMaxSize(system_certs, &keychain_contents, |
| 164 | 4096)); |
| 165 | |
| 166 | // Check that not all sysctls, including those that can get the MAC address, |
Avi Drissman | 438827be | 2019-06-06 18:46:11 | [diff] [blame] | 167 | // are allowed. See crbug.com/738129. |
| 168 | struct ifaddrs* ifap; |
| 169 | CHECK_EQ(-1, getifaddrs(&ifap)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 170 | |
| 171 | std::vector<uint8_t> sysctl_data(4096); |
| 172 | size_t data_size = sysctl_data.size(); |
| 173 | CHECK_EQ(0, |
| 174 | sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0)); |
| 175 | |
Robert Sesek | 9cad86b | 2022-12-02 16:27:12 | [diff] [blame] | 176 | CHECK(!base::Process::Current().CreationTime().is_null()); |
| 177 | |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | TEST_F(SandboxV2Test, SandboxProfileTest) { |
Jay Civelli | 4a44260b | 2017-08-21 19:26:29 | [diff] [blame] | 182 | base::Process process = SpawnChild("SandboxProfileProcess"); |
| 183 | ASSERT_TRUE(process.IsValid()); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 184 | int exit_code = 42; |
Jay Civelli | 4a44260b | 2017-08-21 19:26:29 | [diff] [blame] | 185 | EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), |
| 186 | &exit_code)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 187 | EXPECT_EQ(exit_code, 0); |
| 188 | } |
| 189 | |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 190 | } // namespace content |