blob: 7bdf7e6e4876d0d226ab4801dfd2f330572499c8 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2017 The Chromium Authors
Greg Kerr9e965232017-07-24 22:44:222// 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
Md Hasibul Hasan8d445152024-04-11 07:38:5016#include <string_view>
17
Avi Drissmand4f07082023-05-12 18:05:4418#include "base/apple/bundle_locations.h"
Greg Kerr9e965232017-07-24 22:44:2219#include "base/files/file_util.h"
20#include "base/files/scoped_temp_dir.h"
Greg Kerr9e965232017-07-24 22:44:2221#include "base/mac/mac_util.h"
22#include "base/process/kill.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0323#include "base/system/sys_info.h"
Greg Kerr9e965232017-07-24 22:44:2224#include "base/test/multiprocess_test.h"
25#include "base/test/test_timeouts.h"
Greg Kerr9e965232017-07-24 22:44:2226#include "content/test/test_content_client.h"
27#include "sandbox/mac/sandbox_compiler.h"
28#include "sandbox/mac/seatbelt_exec.h"
Robert Sesek7d0b49b2020-07-08 18:31:2729#include "sandbox/policy/mac/common.sb.h"
Robert Sesek7838cee12021-04-14 18:39:2230#include "sandbox/policy/mac/params.h"
Robert Sesek7d0b49b2020-07-08 18:31:2731#include "sandbox/policy/mac/renderer.sb.h"
32#include "sandbox/policy/mac/sandbox_mac.h"
Greg Kerr9e965232017-07-24 22:44:2233#include "testing/gtest/include/gtest/gtest.h"
34#include "testing/multiprocess_func_list.h"
35
36namespace content {
37
38namespace {
39
40void SetParametersForTest(sandbox::SandboxCompiler* compiler,
41 const base::FilePath& logging_path,
Matthew Denton587371c2023-06-22 14:49:5842 const base::FilePath& executable_path,
43 bool use_syscall_filter) {
Greg Kerr9e965232017-07-24 22:44:2244 bool enable_logging = true;
Robert Sesek794b0822022-12-05 15:22:2745 CHECK(compiler->SetBooleanParameter(sandbox::policy::kParamEnableLogging,
46 enable_logging));
47 CHECK(compiler->SetBooleanParameter(
Robert Sesek7838cee12021-04-14 18:39:2248 sandbox::policy::kParamDisableSandboxDenialLogging, !enable_logging));
Greg Kerr9e965232017-07-24 22:44:2249
50 std::string homedir =
Robert Sesek5aef3522021-04-14 22:48:2351 sandbox::policy::GetCanonicalPath(base::GetHomeDir()).value();
Robert Sesek794b0822022-12-05 15:22:2752 CHECK(
53 compiler->SetParameter(sandbox::policy::kParamHomedirAsLiteral, homedir));
Greg Kerr9e965232017-07-24 22:44:2254
55 int32_t major_version, minor_version, bugfix_version;
56 base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version,
57 &bugfix_version);
58 int32_t os_version = (major_version * 100) + minor_version;
Robert Sesek794b0822022-12-05 15:22:2759 CHECK(compiler->SetParameter(sandbox::policy::kParamOsVersion,
60 base::NumberToString(os_version)));
Tom Sepez305e0d0d2017-10-19 20:48:5261
62 std::string bundle_path =
Avi Drissmand4f07082023-05-12 18:05:4463 sandbox::policy::GetCanonicalPath(base::apple::MainBundlePath()).value();
Robert Sesek794b0822022-12-05 15:22:2764 CHECK(compiler->SetParameter(sandbox::policy::kParamBundlePath, bundle_path));
Greg Kerr9e965232017-07-24 22:44:2265
Robert Sesek794b0822022-12-05 15:22:2766 CHECK(compiler->SetParameter(sandbox::policy::kParamBundleId,
67 "com.google.Chrome.test.sandbox"));
68 CHECK(compiler->SetParameter(sandbox::policy::kParamBrowserPid,
69 base::NumberToString(getpid())));
Greg Kerr9e965232017-07-24 22:44:2270
Robert Sesek794b0822022-12-05 15:22:2771 CHECK(compiler->SetParameter(sandbox::policy::kParamLogFilePath,
72 logging_path.value()));
Greg Kerr9e965232017-07-24 22:44:2273
Robert Sesek794b0822022-12-05 15:22:2774 CHECK(compiler->SetParameter(sandbox::policy::kParamExecutablePath,
75 executable_path.value()));
Greg Kerrf46f31872019-08-08 20:22:4476
Robert Sesek794b0822022-12-05 15:22:2777 CHECK(compiler->SetBooleanParameter(sandbox::policy::kParamFilterSyscalls,
Matthew Denton587371c2023-06-22 14:49:5878 use_syscall_filter));
Greg Kerr9e965232017-07-24 22:44:2279}
80
81} // namespace
82
83// These tests check that the V2 sandbox compiles, initializes, and
84// correctly enforces resource access on all macOS versions. Note that
85// with the exception of certain controlled locations, such as a dummy
86// log file, these tests cannot check that write access to system files
87// is blocked. These tests run on developers' machines and bots, so
88// if the write access goes through, that machine could be corrupted.
89class SandboxV2Test : public base::MultiProcessTest {};
90
91MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) {
92 TestContentClient content_client;
Greg Kerrc382e2ae2017-12-14 23:43:3493 const std::string profile =
Robert Sesek7d0b49b2020-07-08 18:31:2794 std::string(sandbox::policy::kSeatbeltPolicyString_common) +
95 sandbox::policy::kSeatbeltPolicyString_renderer;
Robert Sesek874d19f2022-12-06 21:36:0996 sandbox::SandboxCompiler compiler;
97 compiler.SetProfile(profile);
Greg Kerr9e965232017-07-24 22:44:2298
99 // Create the logging file and pass /bin/ls as the executable path.
100 base::ScopedTempDir temp_dir;
101 CHECK(temp_dir.CreateUniqueTempDir());
102 CHECK(temp_dir.IsValid());
103 base::FilePath temp_path = temp_dir.GetPath();
Robert Sesek5aef3522021-04-14 22:48:23104 temp_path = sandbox::policy::GetCanonicalPath(temp_path);
Greg Kerr9e965232017-07-24 22:44:22105 const base::FilePath log_file = temp_path.Append("log-file");
106 const base::FilePath exec_file("/bin/ls");
107
Alison Gale47d1537d2024-04-19 21:31:46108 // TODO(crbug.com/40273168): re-enable syscall filter for this test.
Matthew Denton587371c2023-06-22 14:49:58109 // SandboxV2Test.SandboxProfileTest uses system() which uses a denied syscall,
110 // which should cause the test to fail.
111 SetParametersForTest(&compiler, log_file, exec_file,
112 /*use_syscall_filter=*/false);
Greg Kerr9e965232017-07-24 22:44:22113
114 std::string error;
Robert Sesek01b092c2022-12-09 15:46:54115 bool result = compiler.CompileAndApplyProfile(error);
Greg Kerr9e965232017-07-24 22:44:22116 CHECK(result) << error;
117
118 // Test the properties of the sandbox profile.
Md Hasibul Hasan8d445152024-04-11 07:38:50119 constexpr std::string_view log_msg = "logged";
120 CHECK(base::WriteFile(log_file, std::string_view(log_msg)));
Greg Kerr9e965232017-07-24 22:44:22121 // Log file is write only.
Claudio DeSouza51509282023-02-25 17:01:08122 char read_buf[log_msg.size()];
Greg Kerr9e965232017-07-24 22:44:22123 CHECK_EQ(-1, base::ReadFile(log_file, read_buf, sizeof(read_buf)));
124
125 // Try executing the blessed binary.
126 CHECK_NE(-1, system(exec_file.value().c_str()));
127
128 // Try and realpath a file.
129 char resolved_name[4096];
130 CHECK_NE(nullptr, realpath(log_file.value().c_str(), resolved_name));
131
132 // Test shared memory access.
133 int shm_fd = shm_open("apple.shm.notification_center", O_RDONLY, 0644);
134 CHECK_GE(shm_fd, 0);
135
136 // Test mach service access. The port is leaked because the multiprocess
137 // test exits quickly after this look up.
138 mach_port_t service_port;
139 kern_return_t status = bootstrap_look_up(
140 bootstrap_port, "com.apple.system.logger", &service_port);
141 CHECK_EQ(status, BOOTSTRAP_SUCCESS) << bootstrap_strerror(status);
142
Greg Kerr6516911d2017-11-27 23:00:37143 mach_port_t forbidden_mach;
144 status = bootstrap_look_up(bootstrap_port, "com.apple.cfprefsd.daemon",
145 &forbidden_mach);
146 CHECK_NE(BOOTSTRAP_SUCCESS, status);
Greg Kerr9e965232017-07-24 22:44:22147
148 // Read bundle contents.
Avi Drissmand4f07082023-05-12 18:05:44149 base::FilePath bundle_path = base::apple::MainBundlePath();
Greg Kerr9e965232017-07-24 22:44:22150 struct stat st;
151 CHECK_NE(-1, stat(bundle_path.value().c_str(), &st));
152
153 // Test that general file system access isn't available.
154 base::FilePath ascii_path("/usr/share/misc/ascii");
155 std::string ascii_contents;
156 CHECK(!base::ReadFileToStringWithMaxSize(ascii_path, &ascii_contents, 4096));
157
158 base::FilePath system_certs(
159 "/System/Library/Keychains/SystemRootCertificates.keychain");
160 std::string keychain_contents;
161 CHECK(!base::ReadFileToStringWithMaxSize(system_certs, &keychain_contents,
162 4096));
163
164 // Check that not all sysctls, including those that can get the MAC address,
Avi Drissman438827be2019-06-06 18:46:11165 // are allowed. See crbug.com/738129.
166 struct ifaddrs* ifap;
167 CHECK_EQ(-1, getifaddrs(&ifap));
Greg Kerr9e965232017-07-24 22:44:22168
169 std::vector<uint8_t> sysctl_data(4096);
170 size_t data_size = sysctl_data.size();
171 CHECK_EQ(0,
172 sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0));
173
Robert Sesek9cad86b2022-12-02 16:27:12174 CHECK(!base::Process::Current().CreationTime().is_null());
175
Greg Kerr9e965232017-07-24 22:44:22176 return 0;
177}
178
179TEST_F(SandboxV2Test, SandboxProfileTest) {
Jay Civelli4a44260b2017-08-21 19:26:29180 base::Process process = SpawnChild("SandboxProfileProcess");
181 ASSERT_TRUE(process.IsValid());
Greg Kerr9e965232017-07-24 22:44:22182 int exit_code = 42;
Jay Civelli4a44260b2017-08-21 19:26:29183 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
184 &exit_code));
Greg Kerr9e965232017-07-24 22:44:22185 EXPECT_EQ(exit_code, 0);
186}
187
Greg Kerr9e965232017-07-24 22:44:22188} // namespace content