blob: 647bb130c434ebcf152d3432d7483b6067f45fe9 [file] [log] [blame]
Greg Kerr9e965232017-07-24 22:44:221// 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 Kerr9e965232017-07-24 22:44:2224#include "content/test/test_content_client.h"
25#include "sandbox/mac/sandbox_compiler.h"
26#include "sandbox/mac/seatbelt_exec.h"
Tom Sepez614d9332017-10-03 00:06:2727#include "services/service_manager/sandbox/mac/renderer_v2.sb.h"
Tom Sepeze2923d52017-10-12 01:23:5528#include "services/service_manager/sandbox/mac/sandbox_mac.h"
Greg Kerr9e965232017-07-24 22:44:2229#include "testing/gtest/include/gtest/gtest.h"
30#include "testing/multiprocess_func_list.h"
31
32namespace content {
33
34namespace {
35
36void SetParametersForTest(sandbox::SandboxCompiler* compiler,
37 const base::FilePath& logging_path,
38 const base::FilePath& executable_path) {
39 bool enable_logging = true;
Tom Sepeze2923d52017-10-12 01:23:5540 CHECK(compiler->InsertBooleanParam(
41 service_manager::Sandbox::kSandboxEnableLogging, enable_logging));
42 CHECK(compiler->InsertBooleanParam(
43 service_manager::Sandbox::kSandboxDisableDenialLogging, !enable_logging));
Greg Kerr9e965232017-07-24 22:44:2244
45 std::string homedir =
Tom Sepeze2923d52017-10-12 01:23:5546 service_manager::Sandbox::GetCanonicalSandboxPath(base::GetHomeDir())
47 .value();
48 CHECK(compiler->InsertStringParam(
49 service_manager::Sandbox::kSandboxHomedirAsLiteral, homedir));
Greg Kerr9e965232017-07-24 22:44:2250
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 Sepeze2923d52017-10-12 01:23:5555 CHECK(compiler->InsertStringParam(service_manager::Sandbox::kSandboxOSVersion,
Greg Kerr9e965232017-07-24 22:44:2256 std::to_string(os_version)));
57
Tom Sepeze2923d52017-10-12 01:23:5558 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 Kerr9e965232017-07-24 22:44:2263
Tom Sepeze2923d52017-10-12 01:23:5564 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 Kerr9e965232017-07-24 22:44:2269
Tom Sepeze2923d52017-10-12 01:23:5570 CHECK(compiler->InsertStringParam(
71 service_manager::Sandbox::kSandboxLoggingPathAsLiteral,
72 logging_path.value()));
Greg Kerr9e965232017-07-24 22:44:2273
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.
88class SandboxV2Test : public base::MultiProcessTest {};
89
90MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) {
91 TestContentClient content_client;
Tom Sepez614d9332017-10-03 00:06:2792 sandbox::SandboxCompiler compiler(
93 service_manager::kSeatbeltPolicyString_renderer_v2);
Greg Kerr9e965232017-07-24 22:44:2294
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 Sepeze2923d52017-10-12 01:23:55100 temp_path = service_manager::Sandbox::GetCanonicalSandboxPath(temp_path);
Greg Kerr9e965232017-07-24 22:44:22101 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
171TEST_F(SandboxV2Test, SandboxProfileTest) {
Jay Civelli4a44260b2017-08-21 19:26:29172 base::Process process = SpawnChild("SandboxProfileProcess");
173 ASSERT_TRUE(process.IsValid());
Greg Kerr9e965232017-07-24 22:44:22174 int exit_code = 42;
Jay Civelli4a44260b2017-08-21 19:26:29175 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
176 &exit_code));
Greg Kerr9e965232017-07-24 22:44:22177 EXPECT_EQ(exit_code, 0);
178}
179
180} // namespace content