blob: d94e81945e4d2b44ea24e74bb073e493cfe49d2e [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"
Sebastien Marchand75a7cdf2018-11-13 23:47:0322#include "base/system/sys_info.h"
Greg Kerr9e965232017-07-24 22:44:2223#include "base/test/multiprocess_test.h"
24#include "base/test/test_timeouts.h"
Greg Kerr9e965232017-07-24 22:44:2225#include "content/test/test_content_client.h"
Evan Stade526e35a62025-02-01 00:09:3726#include "sandbox/mac/sandbox_serializer.h"
Greg Kerr9e965232017-07-24 22:44:2227#include "sandbox/mac/seatbelt_exec.h"
Robert Sesek7d0b49b2020-07-08 18:31:2728#include "sandbox/policy/mac/common.sb.h"
Robert Sesek7838cee12021-04-14 18:39:2229#include "sandbox/policy/mac/params.h"
Robert Sesek7d0b49b2020-07-08 18:31:2730#include "sandbox/policy/mac/renderer.sb.h"
31#include "sandbox/policy/mac/sandbox_mac.h"
Greg Kerr9e965232017-07-24 22:44:2232#include "testing/gtest/include/gtest/gtest.h"
33#include "testing/multiprocess_func_list.h"
34
35namespace content {
36
Evan Stade526e35a62025-02-01 00:09:3737using sandbox::SandboxSerializer;
38
Greg Kerr9e965232017-07-24 22:44:2239namespace {
40
Evan Stade526e35a62025-02-01 00:09:3741void SetParametersForTest(SandboxSerializer* serializer,
Greg Kerr9e965232017-07-24 22:44:2242 const base::FilePath& logging_path,
Sven Zheng770eff82025-04-04 17:57:5343 const base::FilePath& executable_path) {
Greg Kerr9e965232017-07-24 22:44:2244 bool enable_logging = true;
Evan Stade526e35a62025-02-01 00:09:3745 CHECK(serializer->SetBooleanParameter(sandbox::policy::kParamEnableLogging,
46 enable_logging));
47 CHECK(serializer->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();
Evan Stade526e35a62025-02-01 00:09:3752 CHECK(serializer->SetParameter(sandbox::policy::kParamHomedirAsLiteral,
53 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;
Evan Stade526e35a62025-02-01 00:09:3759 CHECK(serializer->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();
Evan Stade526e35a62025-02-01 00:09:3764 CHECK(
65 serializer->SetParameter(sandbox::policy::kParamBundlePath, bundle_path));
Greg Kerr9e965232017-07-24 22:44:2266
Evan Stade526e35a62025-02-01 00:09:3767 CHECK(serializer->SetParameter(sandbox::policy::kParamBundleId,
68 "com.google.Chrome.test.sandbox"));
69 CHECK(serializer->SetParameter(sandbox::policy::kParamBrowserPid,
70 base::NumberToString(getpid())));
Greg Kerr9e965232017-07-24 22:44:2271
Evan Stade526e35a62025-02-01 00:09:3772 CHECK(serializer->SetParameter(sandbox::policy::kParamLogFilePath,
73 logging_path.value()));
Greg Kerr9e965232017-07-24 22:44:2274
Evan Stade526e35a62025-02-01 00:09:3775 CHECK(serializer->SetParameter(sandbox::policy::kParamExecutablePath,
76 executable_path.value()));
Greg Kerr9e965232017-07-24 22:44:2277}
78
79} // namespace
80
81// These tests check that the V2 sandbox compiles, initializes, and
82// correctly enforces resource access on all macOS versions. Note that
83// with the exception of certain controlled locations, such as a dummy
84// log file, these tests cannot check that write access to system files
85// is blocked. These tests run on developers' machines and bots, so
86// if the write access goes through, that machine could be corrupted.
87class SandboxV2Test : public base::MultiProcessTest {};
88
89MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) {
90 TestContentClient content_client;
Greg Kerrc382e2ae2017-12-14 23:43:3491 const std::string profile =
Robert Sesek7d0b49b2020-07-08 18:31:2792 std::string(sandbox::policy::kSeatbeltPolicyString_common) +
93 sandbox::policy::kSeatbeltPolicyString_renderer;
Evan Stade526e35a62025-02-01 00:09:3794 SandboxSerializer serializer(SandboxSerializer::Target::kSource);
95 serializer.SetProfile(profile);
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();
Robert Sesek5aef3522021-04-14 22:48:23102 temp_path = sandbox::policy::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
Sven Zheng770eff82025-04-04 17:57:53106 SetParametersForTest(&serializer, log_file, exec_file);
Greg Kerr9e965232017-07-24 22:44:22107
Evan Stade526e35a62025-02-01 00:09:37108 std::string error, serialized;
109 CHECK(serializer.SerializePolicy(serialized, error)) << error;
110 CHECK(serializer.ApplySerializedPolicy(serialized));
Greg Kerr9e965232017-07-24 22:44:22111
112 // Test the properties of the sandbox profile.
Md Hasibul Hasan8d445152024-04-11 07:38:50113 constexpr std::string_view log_msg = "logged";
114 CHECK(base::WriteFile(log_file, std::string_view(log_msg)));
Greg Kerr9e965232017-07-24 22:44:22115 // Log file is write only.
Claudio DeSouza51509282023-02-25 17:01:08116 char read_buf[log_msg.size()];
Greg Kerr9e965232017-07-24 22:44:22117 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
Greg Kerr6516911d2017-11-27 23:00:37137 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);
Greg Kerr9e965232017-07-24 22:44:22141
142 // Read bundle contents.
Avi Drissmand4f07082023-05-12 18:05:44143 base::FilePath bundle_path = base::apple::MainBundlePath();
Greg Kerr9e965232017-07-24 22:44:22144 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,
Avi Drissman438827be2019-06-06 18:46:11159 // are allowed. See crbug.com/738129.
160 struct ifaddrs* ifap;
161 CHECK_EQ(-1, getifaddrs(&ifap));
Greg Kerr9e965232017-07-24 22:44:22162
163 std::vector<uint8_t> sysctl_data(4096);
164 size_t data_size = sysctl_data.size();
165 CHECK_EQ(0,
166 sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0));
167
Robert Sesek9cad86b2022-12-02 16:27:12168 CHECK(!base::Process::Current().CreationTime().is_null());
169
Greg Kerr9e965232017-07-24 22:44:22170 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
Greg Kerr9e965232017-07-24 22:44:22182} // namespace content