blob: ec2754f4591eb7bc5033e6d25f2d33fad09ce5f9 [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>
Greg Kerr9e965232017-07-24 22:44:227#include <ifaddrs.h>
8#include <servers/bootstrap.h>
9#include <sys/socket.h>
10#include <sys/stat.h>
11#include <sys/sysctl.h>
12#include <sys/types.h>
13#include <unistd.h>
14
Md Hasibul Hasan8d445152024-04-11 07:38:5015#include <string_view>
16
Avi Drissmand4f07082023-05-12 18:05:4417#include "base/apple/bundle_locations.h"
Greg Kerr9e965232017-07-24 22:44:2218#include "base/files/file_util.h"
19#include "base/files/scoped_temp_dir.h"
Greg Kerr9e965232017-07-24 22:44:2220#include "base/mac/mac_util.h"
21#include "base/process/kill.h"
Lei Zhang0a85e65a2025-05-23 19:22:0622#include "base/strings/string_number_conversions.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"
Evan Stade526e35a62025-02-01 00:09:3727#include "sandbox/mac/sandbox_serializer.h"
Greg Kerr9e965232017-07-24 22:44:2228#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
Evan Stade526e35a62025-02-01 00:09:3738using sandbox::SandboxSerializer;
39
Greg Kerr9e965232017-07-24 22:44:2240namespace {
41
Evan Stade526e35a62025-02-01 00:09:3742void SetParametersForTest(SandboxSerializer* serializer,
Greg Kerr9e965232017-07-24 22:44:2243 const base::FilePath& logging_path,
Sven Zheng770eff82025-04-04 17:57:5344 const base::FilePath& executable_path) {
Greg Kerr9e965232017-07-24 22:44:2245 bool enable_logging = true;
Evan Stade526e35a62025-02-01 00:09:3746 CHECK(serializer->SetBooleanParameter(sandbox::policy::kParamEnableLogging,
47 enable_logging));
48 CHECK(serializer->SetBooleanParameter(
Robert Sesek7838cee12021-04-14 18:39:2249 sandbox::policy::kParamDisableSandboxDenialLogging, !enable_logging));
Greg Kerr9e965232017-07-24 22:44:2250
51 std::string homedir =
Robert Sesek5aef3522021-04-14 22:48:2352 sandbox::policy::GetCanonicalPath(base::GetHomeDir()).value();
Evan Stade526e35a62025-02-01 00:09:3753 CHECK(serializer->SetParameter(sandbox::policy::kParamHomedirAsLiteral,
54 homedir));
Greg Kerr9e965232017-07-24 22:44:2255
56 int32_t major_version, minor_version, bugfix_version;
57 base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version,
58 &bugfix_version);
59 int32_t os_version = (major_version * 100) + minor_version;
Evan Stade526e35a62025-02-01 00:09:3760 CHECK(serializer->SetParameter(sandbox::policy::kParamOsVersion,
61 base::NumberToString(os_version)));
Tom Sepez305e0d0d2017-10-19 20:48:5262
63 std::string bundle_path =
Avi Drissmand4f07082023-05-12 18:05:4464 sandbox::policy::GetCanonicalPath(base::apple::MainBundlePath()).value();
Evan Stade526e35a62025-02-01 00:09:3765 CHECK(
66 serializer->SetParameter(sandbox::policy::kParamBundlePath, bundle_path));
Greg Kerr9e965232017-07-24 22:44:2267
Evan Stade526e35a62025-02-01 00:09:3768 CHECK(serializer->SetParameter(sandbox::policy::kParamBundleId,
69 "com.google.Chrome.test.sandbox"));
70 CHECK(serializer->SetParameter(sandbox::policy::kParamBrowserPid,
71 base::NumberToString(getpid())));
Greg Kerr9e965232017-07-24 22:44:2272
Evan Stade526e35a62025-02-01 00:09:3773 CHECK(serializer->SetParameter(sandbox::policy::kParamLogFilePath,
74 logging_path.value()));
Greg Kerr9e965232017-07-24 22:44:2275
Evan Stade526e35a62025-02-01 00:09:3776 CHECK(serializer->SetParameter(sandbox::policy::kParamExecutablePath,
77 executable_path.value()));
Greg Kerr9e965232017-07-24 22:44:2278}
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;
Greg Kerrc382e2ae2017-12-14 23:43:3492 const std::string profile =
Robert Sesek7d0b49b2020-07-08 18:31:2793 std::string(sandbox::policy::kSeatbeltPolicyString_common) +
94 sandbox::policy::kSeatbeltPolicyString_renderer;
Evan Stade526e35a62025-02-01 00:09:3795 SandboxSerializer serializer(SandboxSerializer::Target::kSource);
96 serializer.SetProfile(profile);
Greg Kerr9e965232017-07-24 22:44:2297
98 // Create the logging file and pass /bin/ls as the executable path.
99 base::ScopedTempDir temp_dir;
100 CHECK(temp_dir.CreateUniqueTempDir());
101 CHECK(temp_dir.IsValid());
102 base::FilePath temp_path = temp_dir.GetPath();
Robert Sesek5aef3522021-04-14 22:48:23103 temp_path = sandbox::policy::GetCanonicalPath(temp_path);
Greg Kerr9e965232017-07-24 22:44:22104 const base::FilePath log_file = temp_path.Append("log-file");
105 const base::FilePath exec_file("/bin/ls");
106
Sven Zheng770eff82025-04-04 17:57:53107 SetParametersForTest(&serializer, log_file, exec_file);
Greg Kerr9e965232017-07-24 22:44:22108
Evan Stade526e35a62025-02-01 00:09:37109 std::string error, serialized;
110 CHECK(serializer.SerializePolicy(serialized, error)) << error;
111 CHECK(serializer.ApplySerializedPolicy(serialized));
Greg Kerr9e965232017-07-24 22:44:22112
113 // Test the properties of the sandbox profile.
Md Hasibul Hasan8d445152024-04-11 07:38:50114 constexpr std::string_view log_msg = "logged";
115 CHECK(base::WriteFile(log_file, std::string_view(log_msg)));
Greg Kerr9e965232017-07-24 22:44:22116 // Log file is write only.
Claudio DeSouza51509282023-02-25 17:01:08117 char read_buf[log_msg.size()];
Greg Kerr9e965232017-07-24 22:44:22118 CHECK_EQ(-1, base::ReadFile(log_file, read_buf, sizeof(read_buf)));
119
120 // Try executing the blessed binary.
121 CHECK_NE(-1, system(exec_file.value().c_str()));
122
123 // Try and realpath a file.
124 char resolved_name[4096];
125 CHECK_NE(nullptr, realpath(log_file.value().c_str(), resolved_name));
126
127 // Test shared memory access.
128 int shm_fd = shm_open("apple.shm.notification_center", O_RDONLY, 0644);
129 CHECK_GE(shm_fd, 0);
130
131 // Test mach service access. The port is leaked because the multiprocess
132 // test exits quickly after this look up.
133 mach_port_t service_port;
134 kern_return_t status = bootstrap_look_up(
135 bootstrap_port, "com.apple.system.logger", &service_port);
136 CHECK_EQ(status, BOOTSTRAP_SUCCESS) << bootstrap_strerror(status);
137
Greg Kerr6516911d2017-11-27 23:00:37138 mach_port_t forbidden_mach;
139 status = bootstrap_look_up(bootstrap_port, "com.apple.cfprefsd.daemon",
140 &forbidden_mach);
141 CHECK_NE(BOOTSTRAP_SUCCESS, status);
Greg Kerr9e965232017-07-24 22:44:22142
143 // Read bundle contents.
Avi Drissmand4f07082023-05-12 18:05:44144 base::FilePath bundle_path = base::apple::MainBundlePath();
Greg Kerr9e965232017-07-24 22:44:22145 struct stat st;
146 CHECK_NE(-1, stat(bundle_path.value().c_str(), &st));
147
148 // Test that general file system access isn't available.
149 base::FilePath ascii_path("/usr/share/misc/ascii");
150 std::string ascii_contents;
151 CHECK(!base::ReadFileToStringWithMaxSize(ascii_path, &ascii_contents, 4096));
152
153 base::FilePath system_certs(
154 "/System/Library/Keychains/SystemRootCertificates.keychain");
155 std::string keychain_contents;
156 CHECK(!base::ReadFileToStringWithMaxSize(system_certs, &keychain_contents,
157 4096));
158
159 // Check that not all sysctls, including those that can get the MAC address,
Avi Drissman438827be2019-06-06 18:46:11160 // are allowed. See crbug.com/738129.
161 struct ifaddrs* ifap;
162 CHECK_EQ(-1, getifaddrs(&ifap));
Greg Kerr9e965232017-07-24 22:44:22163
164 std::vector<uint8_t> sysctl_data(4096);
165 size_t data_size = sysctl_data.size();
166 CHECK_EQ(0,
167 sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0));
168
Robert Sesek9cad86b2022-12-02 16:27:12169 CHECK(!base::Process::Current().CreationTime().is_null());
170
Greg Kerr9e965232017-07-24 22:44:22171 return 0;
172}
173
174TEST_F(SandboxV2Test, SandboxProfileTest) {
Jay Civelli4a44260b2017-08-21 19:26:29175 base::Process process = SpawnChild("SandboxProfileProcess");
176 ASSERT_TRUE(process.IsValid());
Greg Kerr9e965232017-07-24 22:44:22177 int exit_code = 42;
Jay Civelli4a44260b2017-08-21 19:26:29178 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
179 &exit_code));
Greg Kerr9e965232017-07-24 22:44:22180 EXPECT_EQ(exit_code, 0);
181}
182
Greg Kerr9e965232017-07-24 22:44:22183} // namespace content