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