blob: 2d025d566f0b31677b3278e45177170092068eda [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(
Tom Sepez305e0d0d2017-10-19 20:48:5241 service_manager::SandboxMac::kSandboxEnableLogging, enable_logging));
Tom Sepeze2923d52017-10-12 01:23:5542 CHECK(compiler->InsertBooleanParam(
Tom Sepez305e0d0d2017-10-19 20:48:5243 service_manager::SandboxMac::kSandboxDisableDenialLogging,
44 !enable_logging));
Greg Kerr9e965232017-07-24 22:44:2245
46 std::string homedir =
Tom Sepez305e0d0d2017-10-19 20:48:5247 service_manager::SandboxMac::GetCanonicalPath(base::GetHomeDir()).value();
Tom Sepeze2923d52017-10-12 01:23:5548 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5249 service_manager::SandboxMac::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(
Tom Sepez305e0d0d2017-10-19 20:48:5256 service_manager::SandboxMac::kSandboxOSVersion,
57 std::to_string(os_version)));
58
59 std::string bundle_path =
60 service_manager::SandboxMac::GetCanonicalPath(base::mac::MainBundlePath())
61 .value();
62 CHECK(compiler->InsertStringParam(
63 service_manager::SandboxMac::kSandboxBundlePath, bundle_path));
Greg Kerr9e965232017-07-24 22:44:2264
Tom Sepeze2923d52017-10-12 01:23:5565 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5266 service_manager::SandboxMac::kSandboxChromeBundleId,
Tom Sepeze2923d52017-10-12 01:23:5567 "com.google.Chrome.test.sandbox"));
68 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5269 service_manager::SandboxMac::kSandboxBrowserPID,
70 std::to_string(getpid())));
Greg Kerr9e965232017-07-24 22:44:2271
Tom Sepeze2923d52017-10-12 01:23:5572 CHECK(compiler->InsertStringParam(
Tom Sepez305e0d0d2017-10-19 20:48:5273 service_manager::SandboxMac::kSandboxLoggingPathAsLiteral,
Tom Sepeze2923d52017-10-12 01:23:5574 logging_path.value()));
Greg Kerr9e965232017-07-24 22:44:2275
76 // Parameters normally set by the main executable.
77 CHECK(compiler->InsertStringParam("CURRENT_PID", std::to_string(getpid())));
78 CHECK(
79 compiler->InsertStringParam("EXECUTABLE_PATH", executable_path.value()));
80}
81
82} // namespace
83
84// These tests check that the V2 sandbox compiles, initializes, and
85// correctly enforces resource access on all macOS versions. Note that
86// with the exception of certain controlled locations, such as a dummy
87// log file, these tests cannot check that write access to system files
88// is blocked. These tests run on developers' machines and bots, so
89// if the write access goes through, that machine could be corrupted.
90class SandboxV2Test : public base::MultiProcessTest {};
91
92MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) {
93 TestContentClient content_client;
Tom Sepez614d9332017-10-03 00:06:2794 sandbox::SandboxCompiler compiler(
95 service_manager::kSeatbeltPolicyString_renderer_v2);
Greg Kerr9e965232017-07-24 22:44:2296
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();
Tom Sepez305e0d0d2017-10-19 20:48:52102 temp_path = service_manager::SandboxMac::GetCanonicalPath(temp_path);
Greg Kerr9e965232017-07-24 22:44:22103 const base::FilePath log_file = temp_path.Append("log-file");
104 const base::FilePath exec_file("/bin/ls");
105
106 SetParametersForTest(&compiler, log_file, exec_file);
107
108 std::string error;
109 bool result = compiler.CompileAndApplyProfile(&error);
110 CHECK(result) << error;
111
112 // Test the properties of the sandbox profile.
113 const char log_msg[] = "logged";
114 CHECK_NE(-1, base::WriteFile(log_file, log_msg, sizeof(log_msg)));
115 // Log file is write only.
116 char read_buf[sizeof(log_msg)];
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
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);
141
142 // Read bundle contents.
143 base::FilePath bundle_path = base::mac::MainBundlePath();
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,
159 // are allowed. See crbug.com/738129. Only 10.10+ supports sysctl filtering.
160 if (base::mac::IsAtLeastOS10_10()) {
161 struct ifaddrs* ifap;
162 CHECK_EQ(-1, getifaddrs(&ifap));
163 }
164
165 std::vector<uint8_t> sysctl_data(4096);
166 size_t data_size = sysctl_data.size();
167 CHECK_EQ(0,
168 sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0));
169
170 return 0;
171}
172
173TEST_F(SandboxV2Test, SandboxProfileTest) {
Jay Civelli4a44260b2017-08-21 19:26:29174 base::Process process = SpawnChild("SandboxProfileProcess");
175 ASSERT_TRUE(process.IsValid());
Greg Kerr9e965232017-07-24 22:44:22176 int exit_code = 42;
Jay Civelli4a44260b2017-08-21 19:26:29177 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
178 &exit_code));
Greg Kerr9e965232017-07-24 22:44:22179 EXPECT_EQ(exit_code, 0);
180}
181
182} // namespace content