Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 1 | // Copyright 2017 The Chromium 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 | #import <Foundation/Foundation.h> |
| 6 | #import <IOSurface/IOSurface.h> |
| 7 | |
| 8 | #include <ifaddrs.h> |
| 9 | #include <servers/bootstrap.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/sysctl.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "base/files/file_util.h" |
| 17 | #include "base/files/scoped_temp_dir.h" |
| 18 | #include "base/mac/bundle_locations.h" |
| 19 | #include "base/mac/mac_util.h" |
| 20 | #include "base/process/kill.h" |
| 21 | #include "base/sys_info.h" |
| 22 | #include "base/test/multiprocess_test.h" |
| 23 | #include "base/test/test_timeouts.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 24 | #include "content/test/test_content_client.h" |
| 25 | #include "sandbox/mac/sandbox_compiler.h" |
| 26 | #include "sandbox/mac/seatbelt_exec.h" |
Tom Sepez | 614d933 | 2017-10-03 00:06:27 | [diff] [blame] | 27 | #include "services/service_manager/sandbox/mac/renderer_v2.sb.h" |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 28 | #include "services/service_manager/sandbox/mac/sandbox_mac.h" |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
| 30 | #include "testing/multiprocess_func_list.h" |
| 31 | |
| 32 | namespace content { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | void SetParametersForTest(sandbox::SandboxCompiler* compiler, |
| 37 | const base::FilePath& logging_path, |
| 38 | const base::FilePath& executable_path) { |
| 39 | bool enable_logging = true; |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 40 | CHECK(compiler->InsertBooleanParam( |
| 41 | service_manager::Sandbox::kSandboxEnableLogging, enable_logging)); |
| 42 | CHECK(compiler->InsertBooleanParam( |
| 43 | service_manager::Sandbox::kSandboxDisableDenialLogging, !enable_logging)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 44 | |
| 45 | std::string homedir = |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 46 | service_manager::Sandbox::GetCanonicalSandboxPath(base::GetHomeDir()) |
| 47 | .value(); |
| 48 | CHECK(compiler->InsertStringParam( |
| 49 | service_manager::Sandbox::kSandboxHomedirAsLiteral, homedir)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 50 | |
| 51 | int32_t major_version, minor_version, bugfix_version; |
| 52 | base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version, |
| 53 | &bugfix_version); |
| 54 | int32_t os_version = (major_version * 100) + minor_version; |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 55 | CHECK(compiler->InsertStringParam(service_manager::Sandbox::kSandboxOSVersion, |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 56 | std::to_string(os_version))); |
| 57 | |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 58 | std::string bundle_path = service_manager::Sandbox::GetCanonicalSandboxPath( |
| 59 | base::mac::MainBundlePath()) |
| 60 | .value(); |
| 61 | CHECK(compiler->InsertStringParam( |
| 62 | service_manager::Sandbox::kSandboxBundlePath, bundle_path)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 63 | |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 64 | CHECK(compiler->InsertStringParam( |
| 65 | service_manager::Sandbox::kSandboxChromeBundleId, |
| 66 | "com.google.Chrome.test.sandbox")); |
| 67 | CHECK(compiler->InsertStringParam( |
| 68 | service_manager::Sandbox::kSandboxBrowserPID, std::to_string(getpid()))); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 69 | |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 70 | CHECK(compiler->InsertStringParam( |
| 71 | service_manager::Sandbox::kSandboxLoggingPathAsLiteral, |
| 72 | logging_path.value())); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 73 | |
| 74 | // Parameters normally set by the main executable. |
| 75 | CHECK(compiler->InsertStringParam("CURRENT_PID", std::to_string(getpid()))); |
| 76 | CHECK( |
| 77 | compiler->InsertStringParam("EXECUTABLE_PATH", executable_path.value())); |
| 78 | } |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | // These tests check that the V2 sandbox compiles, initializes, and |
| 83 | // correctly enforces resource access on all macOS versions. Note that |
| 84 | // with the exception of certain controlled locations, such as a dummy |
| 85 | // log file, these tests cannot check that write access to system files |
| 86 | // is blocked. These tests run on developers' machines and bots, so |
| 87 | // if the write access goes through, that machine could be corrupted. |
| 88 | class SandboxV2Test : public base::MultiProcessTest {}; |
| 89 | |
| 90 | MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) { |
| 91 | TestContentClient content_client; |
Tom Sepez | 614d933 | 2017-10-03 00:06:27 | [diff] [blame] | 92 | sandbox::SandboxCompiler compiler( |
| 93 | service_manager::kSeatbeltPolicyString_renderer_v2); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 94 | |
| 95 | // Create the logging file and pass /bin/ls as the executable path. |
| 96 | base::ScopedTempDir temp_dir; |
| 97 | CHECK(temp_dir.CreateUniqueTempDir()); |
| 98 | CHECK(temp_dir.IsValid()); |
| 99 | base::FilePath temp_path = temp_dir.GetPath(); |
Tom Sepez | e2923d5 | 2017-10-12 01:23:55 | [diff] [blame^] | 100 | temp_path = service_manager::Sandbox::GetCanonicalSandboxPath(temp_path); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 101 | const base::FilePath log_file = temp_path.Append("log-file"); |
| 102 | const base::FilePath exec_file("/bin/ls"); |
| 103 | |
| 104 | SetParametersForTest(&compiler, log_file, exec_file); |
| 105 | |
| 106 | std::string error; |
| 107 | bool result = compiler.CompileAndApplyProfile(&error); |
| 108 | CHECK(result) << error; |
| 109 | |
| 110 | // Test the properties of the sandbox profile. |
| 111 | const char log_msg[] = "logged"; |
| 112 | CHECK_NE(-1, base::WriteFile(log_file, log_msg, sizeof(log_msg))); |
| 113 | // Log file is write only. |
| 114 | char read_buf[sizeof(log_msg)]; |
| 115 | CHECK_EQ(-1, base::ReadFile(log_file, read_buf, sizeof(read_buf))); |
| 116 | |
| 117 | // Try executing the blessed binary. |
| 118 | CHECK_NE(-1, system(exec_file.value().c_str())); |
| 119 | |
| 120 | // Try and realpath a file. |
| 121 | char resolved_name[4096]; |
| 122 | CHECK_NE(nullptr, realpath(log_file.value().c_str(), resolved_name)); |
| 123 | |
| 124 | // Test shared memory access. |
| 125 | int shm_fd = shm_open("apple.shm.notification_center", O_RDONLY, 0644); |
| 126 | CHECK_GE(shm_fd, 0); |
| 127 | |
| 128 | // Test mach service access. The port is leaked because the multiprocess |
| 129 | // test exits quickly after this look up. |
| 130 | mach_port_t service_port; |
| 131 | kern_return_t status = bootstrap_look_up( |
| 132 | bootstrap_port, "com.apple.system.logger", &service_port); |
| 133 | CHECK_EQ(status, BOOTSTRAP_SUCCESS) << bootstrap_strerror(status); |
| 134 | |
| 135 | mach_port_t forbidden_mach; |
| 136 | status = bootstrap_look_up(bootstrap_port, "com.apple.cfprefsd.daemon", |
| 137 | &forbidden_mach); |
| 138 | CHECK_NE(BOOTSTRAP_SUCCESS, status); |
| 139 | |
| 140 | // Read bundle contents. |
| 141 | base::FilePath bundle_path = base::mac::MainBundlePath(); |
| 142 | struct stat st; |
| 143 | CHECK_NE(-1, stat(bundle_path.value().c_str(), &st)); |
| 144 | |
| 145 | // Test that general file system access isn't available. |
| 146 | base::FilePath ascii_path("/usr/share/misc/ascii"); |
| 147 | std::string ascii_contents; |
| 148 | CHECK(!base::ReadFileToStringWithMaxSize(ascii_path, &ascii_contents, 4096)); |
| 149 | |
| 150 | base::FilePath system_certs( |
| 151 | "/System/Library/Keychains/SystemRootCertificates.keychain"); |
| 152 | std::string keychain_contents; |
| 153 | CHECK(!base::ReadFileToStringWithMaxSize(system_certs, &keychain_contents, |
| 154 | 4096)); |
| 155 | |
| 156 | // Check that not all sysctls, including those that can get the MAC address, |
| 157 | // are allowed. See crbug.com/738129. Only 10.10+ supports sysctl filtering. |
| 158 | if (base::mac::IsAtLeastOS10_10()) { |
| 159 | struct ifaddrs* ifap; |
| 160 | CHECK_EQ(-1, getifaddrs(&ifap)); |
| 161 | } |
| 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 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | TEST_F(SandboxV2Test, SandboxProfileTest) { |
Jay Civelli | 4a44260b | 2017-08-21 19:26:29 | [diff] [blame] | 172 | base::Process process = SpawnChild("SandboxProfileProcess"); |
| 173 | ASSERT_TRUE(process.IsValid()); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 174 | int exit_code = 42; |
Jay Civelli | 4a44260b | 2017-08-21 19:26:29 | [diff] [blame] | 175 | EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), |
| 176 | &exit_code)); |
Greg Kerr | 9e96523 | 2017-07-24 22:44:22 | [diff] [blame] | 177 | EXPECT_EQ(exit_code, 0); |
| 178 | } |
| 179 | |
| 180 | } // namespace content |