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