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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/mac/code_sign_clone_manager.h"
#import <Foundation/Foundation.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/apple/foundation_util.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/files/scoped_temp_file.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_path_override.h"
#include "base/version.h"
#include "build/branding_buildflags.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/main_function_params.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !BUILDFLAG(CHROME_FOR_TESTING)
namespace {
constexpr char kMainExecutable[] = "Contents/MacOS/TestApp";
const std::vector<base::FilePath> test_files({
base::FilePath(kMainExecutable),
base::FilePath("Contents/Info.plist"),
base::FilePath("Contents/Frameworks/TestApp.framework/Versions/1.1.1.1/"
"TestApp"),
base::FilePath("Contents/Frameworks/TestApp.framework/Versions/Current"),
});
base::FilePath TestAppPath() {
return base::apple::NSStringToFilePath(NSTemporaryDirectory())
.Append("TestApp.app");
}
base::FilePath CreateTestApp() {
base::FilePath test_app = TestAppPath();
EXPECT_TRUE(base::CreateDirectory(test_app));
for (base::FilePath file : test_files) {
EXPECT_TRUE(base::CreateDirectory(test_app.Append(file.DirName())));
EXPECT_TRUE(base::WriteFile(test_app.Append(file), "test"));
}
return test_app;
}
} // namespace
namespace code_sign_clone_manager {
TEST(CodeSignCloneManagerTest, Clone) {
content::BrowserTaskEnvironment task_environment;
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{kMacAppCodeSignClone, kMacAppCodeSignCloneRenameAsBundle}, {});
// Create the test app,
base::FilePath test_app = CreateTestApp();
// Prevent `CodeSignCloneManager` from running the cleanup helper process.
// Under test `CHILD_PROCESS_EXE` resolves to the test binary which does not
// handle the cleanup switches.
base::ScopedPathOverride scoped_path_override(content::CHILD_PROCESS_EXE);
base::RunLoop run_loop;
base::FilePath tmp_app_path;
CodeSignCloneManager clone_manager(
test_app, base::FilePath("TestApp"),
base::BindOnce(
[](base::OnceClosure quit_closure, base::FilePath* out_app_path,
base::FilePath in_app_path) {
*out_app_path = in_app_path;
std::move(quit_closure).Run();
},
run_loop.QuitClosure(), &tmp_app_path));
run_loop.Run();
ASSERT_NE(tmp_app_path, base::FilePath());
// Make sure the tmp app path has a ".bundle" extension.
//
// (`base::FilePath::Extension()` only returns a double extension for common
// extensions that occur in double-extensions (e.g. `.bz`, `.gz`, `.z`) and
// `.bundle` is not on that list, so it doesn't return `.app.bundle`.)
EXPECT_EQ(tmp_app_path.Extension(), ".bundle");
// Make sure the tmp app path has the expected files.
for (base::FilePath file : test_files) {
EXPECT_TRUE(base::PathExists(tmp_app_path.Append(file)));
}
// Make sure the main executable is a hard link.
struct stat src_stat;
EXPECT_EQ(stat(test_app.Append(kMainExecutable).value().c_str(), &src_stat),
0);
struct stat tmp_stat;
EXPECT_EQ(
stat(tmp_app_path.Append(kMainExecutable).value().c_str(), &tmp_stat), 0);
EXPECT_EQ(src_stat.st_dev, tmp_stat.st_dev);
EXPECT_EQ(src_stat.st_ino, tmp_stat.st_ino);
EXPECT_GT(src_stat.st_nlink, 1);
EXPECT_TRUE(clone_manager.get_needs_cleanup_for_testing());
// The clone is typically cleaned up by a helper child process when
// `clone_manager` goes out of scope. In the test environment that does not
// happen so manually clean up.
base::DeletePathRecursively(tmp_app_path.DirName());
}
TEST(CodeSignCloneManagerTest, InvalidDirhelperPath) {
content::BrowserTaskEnvironment task_environment;
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kMacAppCodeSignClone);
base::FilePath test_app = CreateTestApp();
base::ScopedPathOverride scoped_path_override(content::CHILD_PROCESS_EXE);
// Set an unsupported `_dirhelper` path.
CodeSignCloneManager::SetDirhelperPathForTesting(base::FilePath("/tmp"));
base::RunLoop run_loop;
base::FilePath tmp_app_path;
CodeSignCloneManager clone_manager(
test_app, base::FilePath("TestApp"),
base::BindOnce(
[](base::OnceClosure quit_closure, base::FilePath* out_app_path,
base::FilePath in_app_path) {
*out_app_path = in_app_path;
std::move(quit_closure).Run();
},
run_loop.QuitClosure(), &tmp_app_path));
run_loop.Run();
// Ensure there was a failure.
EXPECT_TRUE(tmp_app_path.empty());
EXPECT_FALSE(clone_manager.get_needs_cleanup_for_testing());
CodeSignCloneManager::ClearDirhelperPathForTesting();
}
TEST(CodeSignCloneManagerTest, FailedHardLink) {
content::BrowserTaskEnvironment task_environment;
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kMacAppCodeSignClone);
base::FilePath test_app = CreateTestApp();
base::ScopedPathOverride scoped_path_override(content::CHILD_PROCESS_EXE);
base::RunLoop run_loop;
base::FilePath tmp_app_path;
CodeSignCloneManager clone_manager(
test_app, base::FilePath("DoesNotExist"),
base::BindOnce(
[](base::OnceClosure quit_closure, base::FilePath* out_app_path,
base::FilePath in_app_path) {
*out_app_path = in_app_path;
std::move(quit_closure).Run();
},
run_loop.QuitClosure(), &tmp_app_path));
run_loop.Run();
// Ensure there was a failure.
EXPECT_TRUE(tmp_app_path.empty());
EXPECT_FALSE(clone_manager.get_needs_cleanup_for_testing());
}
TEST(CodeSignCloneManagerTest, FailedClone) {
base::test::TaskEnvironment task_environment;
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kMacAppCodeSignClone);
// Starting in macOS 10.15 the system volume is a separate read-only volume.
// Cloning files from the system volume to a non-system volume will fail with
// EXDEV (Cross-device link). Perfect for this test.
// https://support.apple.com/en-us/101400
base::FilePath test_app("/System/Applications/Calculator.app");
base::ScopedPathOverride scoped_path_override(content::CHILD_PROCESS_EXE);
base::RunLoop run_loop;
base::FilePath tmp_app_path;
CodeSignCloneManager clone_manager(
test_app, base::FilePath("Calculator"),
base::BindOnce(
[](base::OnceClosure quit_closure, base::FilePath* out_app_path,
base::FilePath in_app_path) {
*out_app_path = in_app_path;
std::move(quit_closure).Run();
},
run_loop.QuitClosure(), &tmp_app_path));
run_loop.Run();
// Ensure there was a failure.
EXPECT_TRUE(tmp_app_path.empty());
EXPECT_FALSE(clone_manager.get_needs_cleanup_for_testing());
}
TEST(CodeSignCloneManagerTest, ChromeCodeSignCloneCleanupMain) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kMacAppCodeSignClone);
struct TestCase {
std::string name;
bool wants_delete;
};
std::vector<TestCase> test_cases{
{std::string("abcdef"), true}, {std::string("ABCDEF"), true},
{std::string("012345"), true}, {std::string("tKdILk"), true},
{std::string("-KdILk"), true}, {std::string("../tKk"), false},
{std::string("tKd../"), false}, {std::string("tKdP.."), true},
{std::string("."), false}, {std::string(".."), false},
{std::string("../"), false}, {std::string("./"), false},
{std::string("../../"), false}, {std::string("AAAAAAAAAA"), false},
};
// Create a unique per test invocation temp dir to avoid collisions when
// this test is run multiple times in parallel.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
CodeSignCloneManager::SetTemporaryDirectoryPathForTesting(
scoped_temp_dir.GetPath());
base::FilePath temp_dir =
CodeSignCloneManager::GetCloneTemporaryDirectoryForTesting();
for (TestCase& test : test_cases) {
base::FilePath test_dir = temp_dir.Append("code_sign_clone." + test.name);
EXPECT_TRUE(base::CreateDirectory(test_dir));
// Test that ChromeCodeSignCloneCleanupMain only deletes valid paths.
base::CommandLine cli(
{"/does/not/exist",
base::StringPrintf("--%s=%s", switches::kUniqueTempDirSuffix,
test.name.c_str()),
"--no-wait-for-parent-exit-for-testing"});
internal::ChromeCodeSignCloneCleanupMain(content::MainFunctionParams(&cli));
EXPECT_EQ(base::PathExists(test_dir), !test.wants_delete) << test.name;
}
CodeSignCloneManager::ClearTemporaryDirectoryPathForTesting();
}
TEST(CodeSignCloneManagerTest, IsFileOpenMoreThanOnceSameProcess) {
base::ScopedTempFile temp_file;
ASSERT_TRUE(temp_file.Create());
// Open the file. We are assuming this is the first time the file has been
// opened, by any process on the host. This is a reasonable assumption given
// the temp file is relatively out of the way. However, it is not guaranteed.
// Another process could open the temp file before the call to
// `IsFileOpenMoreThanOnce`, throwing off the results. If this test is flaky,
// we should find another approach.
base::ScopedFD fd_first(
HANDLE_EINTR(open(temp_file.path().value().c_str(), O_RDONLY)));
ASSERT_NE(fd_first, -1) << strerror(errno);
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd_first.get()),
internal::FileOpenMoreThanOnce::kNo);
// Open it again.
base::ScopedFD fd_second(
HANDLE_EINTR(open(temp_file.path().value().c_str(), O_RDONLY)));
ASSERT_NE(fd_second, -1) << strerror(errno);
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd_first.get()),
internal::FileOpenMoreThanOnce::kYes);
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd_second.get()),
internal::FileOpenMoreThanOnce::kYes);
// Close one of the file descriptors. Ensure `IsFileOpenMoreThanOnce` reflects
// the change.
fd_first.reset();
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd_second.get()),
internal::FileOpenMoreThanOnce::kNo);
// With one file descriptor still open, check for exclusivity using the path.
// In this case, the file will be opened by `IsFileOpenMoreThanOnce`, `kYes`
// should be returned.
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(temp_file.path()),
internal::FileOpenMoreThanOnce::kYes);
// Close the last file descriptor, checking by path should now return `kNo`.
fd_second.reset();
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(temp_file.path()),
internal::FileOpenMoreThanOnce::kNo);
}
TEST(CodeSignCloneManagerTest, IsFileOpenMoreThanOnceSeparateProcess) {
base::ScopedTempFile temp_file;
ASSERT_TRUE(temp_file.Create());
// The same comment about this test potentially being flaky also applies here.
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(temp_file.path()),
internal::FileOpenMoreThanOnce::kNo);
// Open the temp file. The temp file will be opened again by the child
// process.
base::ScopedFD fd(
HANDLE_EINTR(open(temp_file.path().value().c_str(), O_RDONLY)));
ASSERT_NE(fd, -1) << strerror(errno);
// Ensure the file is still only open once. Again, the same comment about this
// test potentially being flaky also applies here.
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd.get()),
internal::FileOpenMoreThanOnce::kNo);
{
// Use TEE(1) to open `temp_file` a second time, as well as synchronizing
// when `IsFileOpenMoreThanOnce` is safe to be called. The test will write a
// sentinel byte to `tee`'s stdin and wait for the byte to arrive on `tee`'s
// stdout'. This indicates that `tee` is up and running and has opened the
// `temp_file`. The sentinel byte will also be written to `temp_file`, but
// that is immaterial. It is simply a side effect of using `tee` as the
// "opener" of `temp_file`.
// `TeeChildProcess` is a small, special purpose class. On construction, a
// `tee` child process is started. A sentinel byte is written to
// `stdin_writer`, then read back from `stdout_reader`. On destruction,
// `stdin_writer` is closed, causing `tee` to exit.
class TeeChildProcess {
public:
explicit TeeChildProcess(const base::FilePath& temp_file_path) {
Init(temp_file_path);
if (testing::Test::HasFatalFailure()) {
return;
}
RoundTripSentinelByte();
}
TeeChildProcess(const TeeChildProcess&) = delete;
TeeChildProcess& operator=(const TeeChildProcess&) = delete;
~TeeChildProcess() {
if (!process_.IsValid()) {
return;
}
WaitForExit();
}
private:
void Init(const base::FilePath& temp_file_path) {
int tee_input_pipe[2];
ASSERT_EQ(pipe(tee_input_pipe), 0) << strerror(errno);
base::ScopedFD stdin_reader(tee_input_pipe[0]);
stdin_writer_.reset(tee_input_pipe[1]);
int tee_output_pipe[2];
ASSERT_EQ(pipe(tee_output_pipe), 0) << strerror(errno);
stdout_reader_.reset(tee_output_pipe[0]);
base::ScopedFD stdout_writer(tee_output_pipe[1]);
base::LaunchOptions options;
options.fds_to_remap.emplace_back(stdin_reader.get(), STDIN_FILENO);
options.fds_to_remap.emplace_back(stdout_writer.get(), STDOUT_FILENO);
process_ = base::LaunchProcess(
{
"/usr/bin/tee",
temp_file_path.value().c_str(),
},
options);
ASSERT_TRUE(process_.IsValid());
}
void RoundTripSentinelByte() {
// Write the sentinel byte.
char buff = '\0';
ASSERT_EQ(HANDLE_EINTR(write(stdin_writer_.get(), &buff, 1)), 1);
// Wait for `tee` to respond back with the byte.
buff = 'A';
ASSERT_EQ(HANDLE_EINTR(read(stdout_reader_.get(), &buff, 1)), 1);
EXPECT_EQ(buff, '\0');
}
void WaitForExit() {
stdin_writer_.reset();
int status;
ASSERT_TRUE(process_.WaitForExit(&status));
EXPECT_EQ(status, 0);
}
base::Process process_;
base::ScopedFD stdout_reader_;
base::ScopedFD stdin_writer_;
};
TeeChildProcess tee_child_process_scoper(temp_file.path());
ASSERT_NO_FATAL_FAILURE();
// The temp file should now be open by both this test and `tee`.
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd.get()),
internal::FileOpenMoreThanOnce::kYes);
// `tee` will exit when this scope ends.
}
// The temp file should now only be open by this test. Yet again, the same
// comment about this test potentially being flaky also applies here.
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(fd.get()),
internal::FileOpenMoreThanOnce::kNo);
}
TEST(CodeSignCloneManagerTest, IsFileOpenMoreThanOnceInvalidPath) {
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(base::FilePath()),
internal::FileOpenMoreThanOnce::kError);
}
TEST(CodeSignCloneManagerTest, IsFileOpenMoreThanOnceBadFileDescriptor) {
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(-1),
internal::FileOpenMoreThanOnce::kError);
}
TEST(CodeSignCloneManagerTest, IsFileOpenMoreThanOnceHardLink) {
base::ScopedTempFile temp_file;
ASSERT_TRUE(temp_file.Create());
// A small scoper class that creates a hard link of the provided `path`. The
// created hard link will reside in the same directory as `path`, and will
// have the file extension `.link`. For example:
//
// ScopedHardLink scoped_hard_link(base::FilePath("/tmp/foo"));
// scoped_hard_link.get(); // returns "/tmp/foo.link"
class ScopedHardLink {
public:
explicit ScopedHardLink(const base::FilePath& path) { Init(path); }
ScopedHardLink(const ScopedHardLink&) = delete;
ScopedHardLink& operator=(const ScopedHardLink&) = delete;
~ScopedHardLink() { base::DeleteFile(path_); }
base::FilePath get() { return path_; }
private:
void Init(const base::FilePath& path) {
path_ = path.AddExtension("link");
ASSERT_NE(link(path.value().c_str(), path_.value().c_str()), -1)
<< strerror(errno);
}
base::FilePath path_;
};
ScopedHardLink scoped_hard_link(temp_file.path());
ASSERT_NO_FATAL_FAILURE();
// Both `temp_file` and `scoped_link` point to the same inode, expect
// `IsFileOpenMoreThanOnce` behavior to be the same for both.
// See the excellent write up by mark@chromium.org on the topic:
// https://chromium-review.googlesource.com/c/chromium/src/+/5793760/comment/9df14e62_3a24ccc0
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(temp_file.path()),
internal::FileOpenMoreThanOnce::kNo);
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(scoped_hard_link.get()),
internal::FileOpenMoreThanOnce::kNo);
// Open one of the paths.
base::ScopedFD temp_file_open_fd(
HANDLE_EINTR(open(temp_file.path().value().c_str(), O_RDONLY)));
ASSERT_NE(temp_file_open_fd, -1) << strerror(errno);
// Again, the `IsFileOpenMoreThanOnce` behavior should be the same for both.
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(temp_file.path()),
internal::FileOpenMoreThanOnce::kYes);
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(scoped_hard_link.get()),
internal::FileOpenMoreThanOnce::kYes);
// After closing, the behavior should also be the same.
temp_file_open_fd.reset();
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(temp_file.path()),
internal::FileOpenMoreThanOnce::kNo);
EXPECT_EQ(internal::IsFileOpenMoreThanOnce(scoped_hard_link.get()),
internal::FileOpenMoreThanOnce::kNo);
}
} // namespace code_sign_clone_manager
#endif // #if !BUILDFLAG(CHROME_FOR_TESTING)
|