sandbox_mac_compiler_unittest.mm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <fcntl.h>
  5. #include <stdint.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include "base/files/file.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/mac/mac_util.h"
  11. #include "base/posix/eintr_wrapper.h"
  12. #include "base/process/kill.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/test/multiprocess_test.h"
  15. #include "base/test/test_timeouts.h"
  16. #include "sandbox/mac/sandbox_compiler.h"
  17. #include "sandbox/mac/seatbelt.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "testing/multiprocess_func_list.h"
  20. namespace sandbox {
  21. class SandboxMacCompilerTest : public base::MultiProcessTest {};
  22. MULTIPROCESS_TEST_MAIN(BasicProfileProcess) {
  23. std::string profile =
  24. "(version 1)"
  25. "(deny default (with no-log))"
  26. "(allow file-read* file-write* (literal \"/\"))";
  27. SandboxCompiler compiler(profile);
  28. std::string error;
  29. CHECK(compiler.CompileAndApplyProfile(&error));
  30. return 0;
  31. }
  32. TEST_F(SandboxMacCompilerTest, BasicProfileTest) {
  33. base::Process process = SpawnChild("BasicProfileProcess");
  34. ASSERT_TRUE(process.IsValid());
  35. int exit_code = 42;
  36. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  37. &exit_code));
  38. EXPECT_EQ(exit_code, 0);
  39. }
  40. MULTIPROCESS_TEST_MAIN(BasicProfileWithParamProcess) {
  41. std::string profile =
  42. "(version 1)"
  43. "(deny default (with no-log))"
  44. "(allow file-read* file-write* (literal (param \"DIR\")))";
  45. SandboxCompiler compiler(profile);
  46. CHECK(compiler.InsertStringParam("DIR", "/"));
  47. std::string error;
  48. CHECK(compiler.CompileAndApplyProfile(&error));
  49. return 0;
  50. }
  51. TEST_F(SandboxMacCompilerTest, BasicProfileTestWithParam) {
  52. base::Process process = SpawnChild("BasicProfileWithParamProcess");
  53. ASSERT_TRUE(process.IsValid());
  54. int exit_code = 42;
  55. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  56. &exit_code));
  57. EXPECT_EQ(exit_code, 0);
  58. }
  59. MULTIPROCESS_TEST_MAIN(ProfileFunctionalProcess) {
  60. std::string profile =
  61. "(version 1)"
  62. "(deny default (with no-log))"
  63. "(allow file-read-data file-read-metadata (literal \"/dev/urandom\"))";
  64. SandboxCompiler compiler(profile);
  65. std::string error;
  66. CHECK(compiler.CompileAndApplyProfile(&error));
  67. // The profile compiled and applied successfully, now try and read 1 byte from
  68. // /dev/urandom.
  69. uint8_t byte;
  70. int fd = open("/dev/urandom", O_RDONLY);
  71. CHECK_NE(fd, -1);
  72. EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte));
  73. return 0;
  74. }
  75. TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTest) {
  76. base::Process process = SpawnChild("ProfileFunctionalProcess");
  77. ASSERT_TRUE(process.IsValid());
  78. int exit_code = 42;
  79. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  80. &exit_code));
  81. EXPECT_EQ(exit_code, 0);
  82. }
  83. MULTIPROCESS_TEST_MAIN(ProfileFunctionalTestWithParamsProcess) {
  84. std::string profile =
  85. "(version 1)"
  86. "(deny default (with no-log))"
  87. "(if (string=? (param \"ALLOW_FILE\") \"TRUE\")"
  88. " (allow file-read-data file-read-metadata (literal (param "
  89. "\"URANDOM\"))))";
  90. SandboxCompiler compiler(profile);
  91. CHECK(compiler.InsertBooleanParam("ALLOW_FILE", true));
  92. CHECK(compiler.InsertStringParam("URANDOM", "/dev/urandom"));
  93. std::string error;
  94. CHECK(compiler.CompileAndApplyProfile(&error));
  95. // The profile compiled and applied successfully, now try and read 1 byte from
  96. // /dev/urandom.
  97. uint8_t byte;
  98. int fd = open("/dev/urandom", O_RDONLY);
  99. CHECK_NE(fd, -1);
  100. EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte));
  101. // Make sure the sandbox isn't overly permissive.
  102. struct stat st;
  103. EXPECT_EQ(stat("/", &st), -1);
  104. return 0;
  105. }
  106. TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestWithParams) {
  107. base::Process process = SpawnChild("ProfileFunctionalTestWithParamsProcess");
  108. ASSERT_TRUE(process.IsValid());
  109. int exit_code = 42;
  110. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  111. &exit_code));
  112. EXPECT_EQ(exit_code, 0);
  113. }
  114. MULTIPROCESS_TEST_MAIN(ProfileFunctionalityTestErrorProcess) {
  115. std::string profile = "(+ 5 a)";
  116. SandboxCompiler compiler(profile);
  117. // Make sure that this invalid profile results in an error returned.
  118. std::string error;
  119. CHECK_EQ(error, "");
  120. CHECK(!compiler.CompileAndApplyProfile(&error));
  121. CHECK_NE(error, "");
  122. return 0;
  123. }
  124. TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestError) {
  125. base::Process process = SpawnChild("ProfileFunctionalityTestErrorProcess");
  126. ASSERT_TRUE(process.IsValid());
  127. int exit_code = 42;
  128. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  129. &exit_code));
  130. EXPECT_EQ(exit_code, 0);
  131. }
  132. MULTIPROCESS_TEST_MAIN(SandboxCheckTestProcess) {
  133. CHECK(!Seatbelt::IsSandboxed());
  134. std::string profile =
  135. "(version 1)"
  136. "(deny default (with no-log))";
  137. SandboxCompiler compiler(profile);
  138. std::string error;
  139. CHECK(compiler.CompileAndApplyProfile(&error));
  140. CHECK(Seatbelt::IsSandboxed());
  141. return 0;
  142. }
  143. TEST_F(SandboxMacCompilerTest, SandboxCheckTest) {
  144. base::Process process = SpawnChild("SandboxCheckTestProcess");
  145. ASSERT_TRUE(process.IsValid());
  146. int exit_code = 42;
  147. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  148. &exit_code));
  149. EXPECT_EQ(exit_code, 0);
  150. }
  151. MULTIPROCESS_TEST_MAIN(Ftruncate) {
  152. std::string profile = "(version 1)"
  153. "(deny default (with no-log))";
  154. SandboxCompiler compiler(profile);
  155. std::string error;
  156. CHECK(compiler.CompileAndApplyProfile(&error)) << error;
  157. std::unique_ptr<base::Environment> env = base::Environment::Create();
  158. std::string fd_string;
  159. CHECK(env->GetVar("FD_TO_TRUNCATE", &fd_string));
  160. int fd;
  161. CHECK(base::StringToInt(fd_string, &fd));
  162. const char kTestBuf[] = "hello";
  163. CHECK_EQ(static_cast<ssize_t>(strlen(kTestBuf)),
  164. HANDLE_EINTR(write(fd, kTestBuf, strlen(kTestBuf))));
  165. return ftruncate(fd, 0) == 0 ? 0 : 15;
  166. }
  167. // Tests ftruncate() behavior on an inherited, open, writable FD. Prior to
  168. // macOS 10.15, the sandbox did not permit ftruncate (but it did permit regular
  169. // writing) on such FDs. This verifies the behavior before, on, and after macOS
  170. // 10.15. See https://crbug.com/1084565 for details.
  171. TEST_F(SandboxMacCompilerTest, Ftruncate) {
  172. base::ScopedTempDir temp_dir;
  173. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  174. base::File file(
  175. temp_dir.GetPath().Append("file.txt"),
  176. base::File::FLAG_CREATE | base::File::FLAG_READ | base::File::FLAG_WRITE);
  177. ASSERT_TRUE(file.IsValid());
  178. const std::string contents =
  179. "Wouldn't it be nice to be able to use ftruncate?\n";
  180. EXPECT_EQ(static_cast<int>(contents.length()),
  181. file.WriteAtCurrentPos(contents.data(), contents.length()));
  182. EXPECT_EQ(static_cast<int64_t>(contents.length()), file.GetLength());
  183. base::PlatformFile fd = file.GetPlatformFile();
  184. base::LaunchOptions options;
  185. options.fds_to_remap.emplace_back(fd, fd);
  186. options.environment["FD_TO_TRUNCATE"] = base::NumberToString(fd);
  187. base::Process process = SpawnChildWithOptions("Ftruncate", options);
  188. ASSERT_TRUE(process.IsValid());
  189. int exit_code = 42;
  190. EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
  191. &exit_code));
  192. if (base::mac::IsAtLeastOS10_15()) {
  193. EXPECT_EQ(0, exit_code);
  194. EXPECT_EQ(0, file.GetLength());
  195. } else {
  196. EXPECT_EQ(15, exit_code);
  197. EXPECT_GT(file.GetLength(), static_cast<int64_t>(contents.length()));
  198. }
  199. }
  200. } // namespace sandbox