blob: cc1ff754b56c05b4e7276279d8a6ce2319401e32 [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"
Sebastien Marchand75a7cdf2018-11-13 23:47:0321#include "base/system/sys_info.h"
Greg Kerr9e965232017-07-24 22:44:2222#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"
Greg Kerrc382e2ae2017-12-14 23:43:3427#include "services/service_manager/sandbox/mac/common_v2.sb.h"
Tom Sepez614d9332017-10-03 00:06:2728#include "services/service_manager/sandbox/mac/renderer_v2.sb.h"
Tom Sepeze2923d52017-10-12 01:23:5529#include "services/service_manager/sandbox/mac/sandbox_mac.h"
Greg Kerr9e965232017-07-24 22:44:2230#include "testing/gtest/include/gtest/gtest.h"
31#include "testing/multiprocess_func_list.h"
32
33namespace content {
34
35namespace {
36
37void SetParametersForTest(sandbox::SandboxCompiler* compiler,
38 const base::FilePath& logging_path,
39 const base::FilePath& executable_path) {
40 bool enable_logging = true;
Tom Sepeze2923d52017-10-12 01:23:5541 CHECK(compiler->InsertBooleanParam(
Tom Sepez305e0d0d2017-10-19 20:48:5242 service_manager::SandboxMac::kSandboxEnableLogging, enable_logging));
Tom Sepeze2923d52017-10-12 01:23:5543 CHECK(compiler->InsertBooleanParam(
Tom Sepez305e0d0d2017-10-19 20:48:5244 service_manager::SandboxMac::kSandboxDisableDenialLogging,
45 !enable_logging));
Greg Kerr9e965232017-07-24 22:44:2246
47 std::string homedir =
Tom Sepez305e0d0d2017-10-19 20:48:5248 service_manager::SandboxMac::GetCanonicalPath(base::GetHomeDir()).value();
Tom Sepeze2923d52017-10-12 01:23:5549 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5250 service_manager::SandboxMac::kSandboxHomedirAsLiteral, homedir));
Greg Kerr9e965232017-07-24 22:44:2251
52 int32_t major_version, minor_version, bugfix_version;
53 base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version,
54 &bugfix_version);
55 int32_t os_version = (major_version * 100) + minor_version;
Tom Sepeze2923d52017-10-12 01:23:5556 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5257 service_manager::SandboxMac::kSandboxOSVersion,
58 std::to_string(os_version)));
59
60 std::string bundle_path =
61 service_manager::SandboxMac::GetCanonicalPath(base::mac::MainBundlePath())
62 .value();
63 CHECK(compiler->InsertStringParam(
64 service_manager::SandboxMac::kSandboxBundlePath, bundle_path));
Greg Kerr9e965232017-07-24 22:44:2265
Tom Sepeze2923d52017-10-12 01:23:5566 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5267 service_manager::SandboxMac::kSandboxChromeBundleId,
Tom Sepeze2923d52017-10-12 01:23:5568 "com.google.Chrome.test.sandbox"));
69 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5270 service_manager::SandboxMac::kSandboxBrowserPID,
71 std::to_string(getpid())));
Greg Kerr9e965232017-07-24 22:44:2272
Tom Sepeze2923d52017-10-12 01:23:5573 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5274 service_manager::SandboxMac::kSandboxLoggingPathAsLiteral,
Tom Sepeze2923d52017-10-12 01:23:5575 logging_path.value()));
Greg Kerr9e965232017-07-24 22:44:2276
77 // Parameters normally set by the main executable.
78 CHECK(compiler->InsertStringParam("CURRENT_PID", std::to_string(getpid())));
79 CHECK(
80 compiler->InsertStringParam("EXECUTABLE_PATH", executable_path.value()));
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.
91class SandboxV2Test : public base::MultiProcessTest {};
92
93MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) {
94 TestContentClient content_client;
Greg Kerrc382e2ae2017-12-14 23:43:3495 const std::string profile =
96 std::string(service_manager::kSeatbeltPolicyString_common_v2) +
97 service_manager::kSeatbeltPolicyString_renderer_v2;
98 sandbox::SandboxCompiler compiler(profile);
Greg Kerr9e965232017-07-24 22:44:2299
100 // Create the logging file and pass /bin/ls as the executable path.
101 base::ScopedTempDir temp_dir;
102 CHECK(temp_dir.CreateUniqueTempDir());
103 CHECK(temp_dir.IsValid());
104 base::FilePath temp_path = temp_dir.GetPath();
Tom Sepez305e0d0d2017-10-19 20:48:52105 temp_path = service_manager::SandboxMac::GetCanonicalPath(temp_path);
Greg Kerr9e965232017-07-24 22:44:22106 const base::FilePath log_file = temp_path.Append("log-file");
107 const base::FilePath exec_file("/bin/ls");
108
109 SetParametersForTest(&compiler, log_file, exec_file);
110
111 std::string error;
112 bool result = compiler.CompileAndApplyProfile(&error);
113 CHECK(result) << error;
114
115 // Test the properties of the sandbox profile.
116 const char log_msg[] = "logged";
117 CHECK_NE(-1, base::WriteFile(log_file, log_msg, sizeof(log_msg)));
118 // Log file is write only.
119 char read_buf[sizeof(log_msg)];
120 CHECK_EQ(-1, base::ReadFile(log_file, read_buf, sizeof(read_buf)));
121
122 // Try executing the blessed binary.
123 CHECK_NE(-1, system(exec_file.value().c_str()));
124
125 // Try and realpath a file.
126 char resolved_name[4096];
127 CHECK_NE(nullptr, realpath(log_file.value().c_str(), resolved_name));
128
129 // Test shared memory access.
130 int shm_fd = shm_open("apple.shm.notification_center", O_RDONLY, 0644);
131 CHECK_GE(shm_fd, 0);
132
133 // Test mach service access. The port is leaked because the multiprocess
134 // test exits quickly after this look up.
135 mach_port_t service_port;
136 kern_return_t status = bootstrap_look_up(
137 bootstrap_port, "com.apple.system.logger", &service_port);
138 CHECK_EQ(status, BOOTSTRAP_SUCCESS) << bootstrap_strerror(status);
139
Greg Kerr6516911d2017-11-27 23:00:37140 mach_port_t forbidden_mach;
141 status = bootstrap_look_up(bootstrap_port, "com.apple.cfprefsd.daemon",
142 &forbidden_mach);
143 CHECK_NE(BOOTSTRAP_SUCCESS, status);
Greg Kerr9e965232017-07-24 22:44:22144
145 // Read bundle contents.
146 base::FilePath bundle_path = base::mac::MainBundlePath();
147 struct stat st;
148 CHECK_NE(-1, stat(bundle_path.value().c_str(), &st));
149
150 // Test that general file system access isn't available.
151 base::FilePath ascii_path("/usr/share/misc/ascii");
152 std::string ascii_contents;
153 CHECK(!base::ReadFileToStringWithMaxSize(ascii_path, &ascii_contents, 4096));
154
155 base::FilePath system_certs(
156 "/System/Library/Keychains/SystemRootCertificates.keychain");
157 std::string keychain_contents;
158 CHECK(!base::ReadFileToStringWithMaxSize(system_certs, &keychain_contents,
159 4096));
160
161 // Check that not all sysctls, including those that can get the MAC address,
162 // are allowed. See crbug.com/738129. Only 10.10+ supports sysctl filtering.
163 if (base::mac::IsAtLeastOS10_10()) {
164 struct ifaddrs* ifap;
165 CHECK_EQ(-1, getifaddrs(&ifap));
166 }
167
168 std::vector<uint8_t> sysctl_data(4096);
169 size_t data_size = sysctl_data.size();
170 CHECK_EQ(0,
171 sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0));
172
173 return 0;
174}
175
176TEST_F(SandboxV2Test, SandboxProfileTest) {
Jay Civelli4a44260b2017-08-21 19:26:29177 base::Process process = SpawnChild("SandboxProfileProcess");
178 ASSERT_TRUE(process.IsValid());
Greg Kerr9e965232017-07-24 22:44:22179 int exit_code = 42;
Jay Civelli4a44260b2017-08-21 19:26:29180 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
181 &exit_code));
Greg Kerr9e965232017-07-24 22:44:22182 EXPECT_EQ(exit_code, 0);
183}
184
185} // namespace content