blob: 7734a5ecd841af5c5d4f535fa3768d8858dde4f0 [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,
Matthew Denton587371c2023-06-22 14:49:5843 const base::FilePath& executable_path,
44 bool use_syscall_filter) {
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 Kerrf46f31872019-08-08 20:22:4478
Evan Stade526e35a62025-02-01 00:09:3779 CHECK(serializer->SetBooleanParameter(sandbox::policy::kParamFilterSyscalls,
80 use_syscall_filter));
Greg Kerr9e965232017-07-24 22:44:2281}
82
83} // namespace
84
85// These tests check that the V2 sandbox compiles, initializes, and
86// correctly enforces resource access on all macOS versions. Note that
87// with the exception of certain controlled locations, such as a dummy
88// log file, these tests cannot check that write access to system files
89// is blocked. These tests run on developers' machines and bots, so
90// if the write access goes through, that machine could be corrupted.
91class SandboxV2Test : public base::MultiProcessTest {};
92
93MULTIPROCESS_TEST_MAIN(SandboxProfileProcess) {
94 TestContentClient content_client;
Greg Kerrc382e2ae2017-12-14 23:43:3495 const std::string profile =
Robert Sesek7d0b49b2020-07-08 18:31:2796 std::string(sandbox::policy::kSeatbeltPolicyString_common) +
97 sandbox::policy::kSeatbeltPolicyString_renderer;
Evan Stade526e35a62025-02-01 00:09:3798 SandboxSerializer serializer(SandboxSerializer::Target::kSource);
99 serializer.SetProfile(profile);
Greg Kerr9e965232017-07-24 22:44:22100
101 // Create the logging file and pass /bin/ls as the executable path.
102 base::ScopedTempDir temp_dir;
103 CHECK(temp_dir.CreateUniqueTempDir());
104 CHECK(temp_dir.IsValid());
105 base::FilePath temp_path = temp_dir.GetPath();
Robert Sesek5aef3522021-04-14 22:48:23106 temp_path = sandbox::policy::GetCanonicalPath(temp_path);
Greg Kerr9e965232017-07-24 22:44:22107 const base::FilePath log_file = temp_path.Append("log-file");
108 const base::FilePath exec_file("/bin/ls");
109
Alison Gale47d1537d2024-04-19 21:31:46110 // TODO(crbug.com/40273168): re-enable syscall filter for this test.
Matthew Denton587371c2023-06-22 14:49:58111 // SandboxV2Test.SandboxProfileTest uses system() which uses a denied syscall,
112 // which should cause the test to fail.
Evan Stade526e35a62025-02-01 00:09:37113 SetParametersForTest(&serializer, log_file, exec_file,
Matthew Denton587371c2023-06-22 14:49:58114 /*use_syscall_filter=*/false);
Greg Kerr9e965232017-07-24 22:44:22115
Evan Stade526e35a62025-02-01 00:09:37116 std::string error, serialized;
117 CHECK(serializer.SerializePolicy(serialized, error)) << error;
118 CHECK(serializer.ApplySerializedPolicy(serialized));
Greg Kerr9e965232017-07-24 22:44:22119
120 // Test the properties of the sandbox profile.
Md Hasibul Hasan8d445152024-04-11 07:38:50121 constexpr std::string_view log_msg = "logged";
122 CHECK(base::WriteFile(log_file, std::string_view(log_msg)));
Greg Kerr9e965232017-07-24 22:44:22123 // Log file is write only.
Claudio DeSouza51509282023-02-25 17:01:08124 char read_buf[log_msg.size()];
Greg Kerr9e965232017-07-24 22:44:22125 CHECK_EQ(-1, base::ReadFile(log_file, read_buf, sizeof(read_buf)));
126
127 // Try executing the blessed binary.
128 CHECK_NE(-1, system(exec_file.value().c_str()));
129
130 // Try and realpath a file.
131 char resolved_name[4096];
132 CHECK_NE(nullptr, realpath(log_file.value().c_str(), resolved_name));
133
134 // Test shared memory access.
135 int shm_fd = shm_open("apple.shm.notification_center", O_RDONLY, 0644);
136 CHECK_GE(shm_fd, 0);
137
138 // Test mach service access. The port is leaked because the multiprocess
139 // test exits quickly after this look up.
140 mach_port_t service_port;
141 kern_return_t status = bootstrap_look_up(
142 bootstrap_port, "com.apple.system.logger", &service_port);
143 CHECK_EQ(status, BOOTSTRAP_SUCCESS) << bootstrap_strerror(status);
144
Greg Kerr6516911d2017-11-27 23:00:37145 mach_port_t forbidden_mach;
146 status = bootstrap_look_up(bootstrap_port, "com.apple.cfprefsd.daemon",
147 &forbidden_mach);
148 CHECK_NE(BOOTSTRAP_SUCCESS, status);
Greg Kerr9e965232017-07-24 22:44:22149
150 // Read bundle contents.
Avi Drissmand4f07082023-05-12 18:05:44151 base::FilePath bundle_path = base::apple::MainBundlePath();
Greg Kerr9e965232017-07-24 22:44:22152 struct stat st;
153 CHECK_NE(-1, stat(bundle_path.value().c_str(), &st));
154
155 // Test that general file system access isn't available.
156 base::FilePath ascii_path("/usr/share/misc/ascii");
157 std::string ascii_contents;
158 CHECK(!base::ReadFileToStringWithMaxSize(ascii_path, &ascii_contents, 4096));
159
160 base::FilePath system_certs(
161 "/System/Library/Keychains/SystemRootCertificates.keychain");
162 std::string keychain_contents;
163 CHECK(!base::ReadFileToStringWithMaxSize(system_certs, &keychain_contents,
164 4096));
165
166 // Check that not all sysctls, including those that can get the MAC address,
Avi Drissman438827be2019-06-06 18:46:11167 // are allowed. See crbug.com/738129.
168 struct ifaddrs* ifap;
169 CHECK_EQ(-1, getifaddrs(&ifap));
Greg Kerr9e965232017-07-24 22:44:22170
171 std::vector<uint8_t> sysctl_data(4096);
172 size_t data_size = sysctl_data.size();
173 CHECK_EQ(0,
174 sysctlbyname("hw.ncpu", sysctl_data.data(), &data_size, nullptr, 0));
175
Robert Sesek9cad86b2022-12-02 16:27:12176 CHECK(!base::Process::Current().CreationTime().is_null());
177
Greg Kerr9e965232017-07-24 22:44:22178 return 0;
179}
180
181TEST_F(SandboxV2Test, SandboxProfileTest) {
Jay Civelli4a44260b2017-08-21 19:26:29182 base::Process process = SpawnChild("SandboxProfileProcess");
183 ASSERT_TRUE(process.IsValid());
Greg Kerr9e965232017-07-24 22:44:22184 int exit_code = 42;
Jay Civelli4a44260b2017-08-21 19:26:29185 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
186 &exit_code));
Greg Kerr9e965232017-07-24 22:44:22187 EXPECT_EQ(exit_code, 0);
188}
189
Greg Kerr9e965232017-07-24 22:44:22190} // namespace content