1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sandbox/mac/seatbelt.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <iterator>
#include <optional>
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/mac/mac_util.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "sandbox/mac/sandbox_test.h"
#include "sandbox/mac/seatbelt.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
namespace sandbox {
using SeatbeltTest = SandboxTest;
MULTIPROCESS_TEST_MAIN(SandboxCheckTestProcess) {
CHECK(!Seatbelt::IsSandboxed());
const char* profile =
"(version 1)"
"(deny default (with no-log))";
std::string error;
CHECK(Seatbelt::Init(profile, 0, &error));
CHECK(Seatbelt::IsSandboxed());
return 0;
}
TEST_F(SeatbeltTest, SandboxCheckTest) {
base::Process process = SpawnChild("SandboxCheckTestProcess");
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
EXPECT_EQ(exit_code, 0);
}
MULTIPROCESS_TEST_MAIN(Ftruncate) {
const char* profile =
"(version 1)"
"(deny default (with no-log))";
std::string error;
CHECK(Seatbelt::Init(profile, 0, &error)) << error;
std::unique_ptr<base::Environment> env = base::Environment::Create();
std::optional<std::string> fd_string = env->GetVar("FD_TO_TRUNCATE");
CHECK(fd_string.has_value());
int fd;
CHECK(base::StringToInt(*fd_string, &fd));
const char kTestBuf[] = "hello";
CHECK_EQ(static_cast<ssize_t>(strlen(kTestBuf)),
HANDLE_EINTR(write(fd, kTestBuf, strlen(kTestBuf))));
return ftruncate(fd, 0) == 0 ? 0 : 15;
}
// Tests ftruncate() behavior on an inherited, open, writable FD. Prior to macOS
// 10.15, the sandbox did not permit ftruncate on such FDs, but now it does.
// This verifies the new behavior. See https://crbug.com/1084565 for details.
TEST_F(SeatbeltTest, Ftruncate) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::File file(
temp_dir.GetPath().Append("file.txt"),
base::File::FLAG_CREATE | base::File::FLAG_READ | base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
const std::string contents =
"Wouldn't it be nice to be able to use ftruncate?\n";
EXPECT_TRUE(file.WriteAtCurrentPosAndCheck(base::as_byte_span(contents)));
EXPECT_EQ(static_cast<int64_t>(contents.length()), file.GetLength());
base::PlatformFile fd = file.GetPlatformFile();
base::LaunchOptions options;
options.fds_to_remap.emplace_back(fd, fd);
options.environment["FD_TO_TRUNCATE"] = base::NumberToString(fd);
base::Process process = SpawnChildWithOptions("Ftruncate", options);
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
EXPECT_EQ(0, exit_code);
EXPECT_EQ(0, file.GetLength());
}
MULTIPROCESS_TEST_MAIN(ProcessSelfInfo) {
const char* profile = R"(
(version 1)
(deny default (with no-log))
; `process-info` is default-allowed.
(deny process-info*)
(allow process-info-pidinfo (target self))
(deny sysctl-read)
)";
std::string error;
CHECK(Seatbelt::Init(profile, 0, &error)) << error;
std::array<int, 4> mib = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
kinfo_proc proc;
size_t size = sizeof(proc);
int rv = sysctl(mib.data(), mib.size(), &proc, &size, nullptr, 0);
PCHECK(rv == 0);
mib.back() = getppid();
errno = 0;
rv = sysctl(mib.data(), mib.size(), &proc, &size, nullptr, 0);
PCHECK(rv == -1);
PCHECK(errno == EPERM);
return 0;
}
TEST_F(SeatbeltTest, ProcessSelfInfo) {
base::Process process = SpawnChild("ProcessSelfInfo");
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
EXPECT_EQ(exit_code, 0);
}
} // namespace sandbox
|