1 // Copyright (c) 2012 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.
5 #import <Cocoa/Cocoa.h>
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/process/kill.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/test/multiprocess_test.h"
18 #include "content/common/sandbox_mac.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/multiprocess_func_list.h"
24 static const char* kSandboxAccessPathKey = "sandbox_dir";
25 static const char* kDeniedSuffix = "_denied";
29 // Tests need to be in the same namespace as the Sandbox class to be useable
30 // with FRIEND_TEST() declaration.
33 class MacDirAccessSandboxTest : public base::MultiProcessTest {
35 bool CheckSandbox(const std::string& directory_to_try) {
36 setenv(kSandboxAccessPathKey, directory_to_try.c_str(), 1);
37 base::Process child_process = SpawnChild("mac_sandbox_path_access");
38 if (!child_process.IsValid()) {
39 LOG(WARNING) << "SpawnChild failed";
43 if (!child_process.WaitForExit(&code)) {
44 LOG(WARNING) << "Process::WaitForExit failed";
51 TEST_F(MacDirAccessSandboxTest, StringEscape) {
52 const struct string_escape_test_data {
53 const char* to_escape;
55 } string_escape_cases[] = {
57 {"\b\f\n\r\t\\\"", "\\b\\f\\n\\r\\t\\\\\\\""},
59 {"sandwich", "sandwich"},
60 {"(sandwich)", "(sandwich)"},
61 {"^\u2135.\u2136$", "^\\u2135.\\u2136$"},
64 for (size_t i = 0; i < arraysize(string_escape_cases); ++i) {
66 std::string in(string_escape_cases[i].to_escape);
67 EXPECT_TRUE(Sandbox::QuotePlainString(in, &out));
68 EXPECT_EQ(string_escape_cases[i].escaped, out);
72 TEST_F(MacDirAccessSandboxTest, RegexEscape) {
73 const std::string kSandboxEscapeSuffix("(/|$)");
74 const struct regex_test_data {
75 const wchar_t *to_escape;
79 {L"/'", "/'"}, // / & ' characters don't need escaping.
80 {L"sandwich", "sandwich"},
81 {L"(sandwich)", "\\(sandwich\\)"},
84 // Check that all characters whose values are smaller than 32 [1F] are
85 // rejected by the regex escaping code.
88 char fail_string[] = {31, 0};
89 char ok_string[] = {32, 0};
90 EXPECT_FALSE(Sandbox::QuoteStringForRegex(fail_string, &out));
91 EXPECT_TRUE(Sandbox::QuoteStringForRegex(ok_string, &out));
94 // Check that all characters whose values are larger than 126 [7E] are
95 // rejected by the regex escaping code.
98 EXPECT_TRUE(Sandbox::QuoteStringForRegex("}", &out)); // } == 0x7D == 125
99 EXPECT_FALSE(Sandbox::QuoteStringForRegex("~", &out)); // ~ == 0x7E == 126
101 Sandbox::QuoteStringForRegex(base::WideToUTF8(L"^\u2135.\u2136$"),
106 for (size_t i = 0; i < arraysize(regex_cases); ++i) {
108 std::string in = base::WideToUTF8(regex_cases[i].to_escape);
109 EXPECT_TRUE(Sandbox::QuoteStringForRegex(in, &out));
110 std::string expected("^");
111 expected.append(regex_cases[i].escaped);
112 expected.append(kSandboxEscapeSuffix);
113 EXPECT_EQ(expected, out);
118 std::string in_utf8("\\^.$|()[]*+?{}");
119 std::string expected;
120 expected.push_back('^');
121 for (size_t i = 0; i < in_utf8.length(); ++i) {
122 expected.push_back('\\');
123 expected.push_back(in_utf8[i]);
125 expected.append(kSandboxEscapeSuffix);
128 EXPECT_TRUE(Sandbox::QuoteStringForRegex(in_utf8, &out));
129 EXPECT_EQ(expected, out);
134 // A class to handle auto-deleting a directory.
135 struct ScopedDirectoryDelete {
136 inline void operator()(base::FilePath* x) const {
138 base::DeleteFile(*x, true);
142 typedef scoped_ptr<base::FilePath, ScopedDirectoryDelete> ScopedDirectory;
144 TEST_F(MacDirAccessSandboxTest, SandboxAccess) {
145 using base::CreateDirectory;
147 base::FilePath tmp_dir;
148 ASSERT_TRUE(base::CreateNewTempDirectory(base::FilePath::StringType(),
150 // This step is important on OS X since the sandbox only understands "real"
151 // paths and the paths CreateNewTempDirectory() returns are empirically in
152 // /var which is a symlink to /private/var .
153 tmp_dir = Sandbox::GetCanonicalSandboxPath(tmp_dir);
154 ScopedDirectory cleanup(&tmp_dir);
156 const char* sandbox_dir_cases[] = {
158 "^hello++ $", // Regex.
159 "\\^.$|()[]*+?{}", // All regex characters.
162 for (size_t i = 0; i < arraysize(sandbox_dir_cases); ++i) {
163 const char* sandbox_dir_name = sandbox_dir_cases[i];
164 base::FilePath sandbox_dir = tmp_dir.Append(sandbox_dir_name);
165 ASSERT_TRUE(CreateDirectory(sandbox_dir));
166 ScopedDirectory cleanup_sandbox(&sandbox_dir);
168 // Create a sibling directory of the sandbox dir, whose name has sandbox dir
169 // as a substring but to which access is denied.
170 std::string sibling_sandbox_dir_name_denied =
171 std::string(sandbox_dir_cases[i]) + kDeniedSuffix;
172 base::FilePath sibling_sandbox_dir = tmp_dir.Append(
173 sibling_sandbox_dir_name_denied.c_str());
174 ASSERT_TRUE(CreateDirectory(sibling_sandbox_dir));
175 ScopedDirectory cleanup_sandbox_sibling(&sibling_sandbox_dir);
177 EXPECT_TRUE(CheckSandbox(sandbox_dir.value()));
181 MULTIPROCESS_TEST_MAIN(mac_sandbox_path_access) {
182 char *sandbox_allowed_dir = getenv(kSandboxAccessPathKey);
183 if (!sandbox_allowed_dir)
186 std::string final_allowed_dir;
188 Sandbox::QuoteStringForRegex(sandbox_allowed_dir, &final_allowed_dir));
190 // Build up a sandbox profile that only allows access to a single directory.
191 std::string sandbox_profile =
193 "(define perm_dir (param \"PERMITTED_DIR\"))"
195 "(allow signal (target self))"
196 "(allow sysctl-read)"
197 "(if (string? perm_dir)"
199 " (allow file-read-metadata )"
200 " (allow file-read* file-write* (regex (string-append #\"\" "
203 // Setup the parameters to pass to the sandbox.
204 SandboxCompiler compiler(sandbox_profile);
205 CHECK(compiler.InsertStringParam("PERMITTED_DIR", final_allowed_dir));
208 std::string error_str;
209 if (!compiler.CompileAndApplyProfile(&error_str)) {
210 LOG(ERROR) << "Failed to Initialize Sandbox: " << error_str;
216 // We should be able to list the contents of the sandboxed directory.
217 DIR *file_list = NULL;
218 file_list = opendir(sandbox_allowed_dir);
220 PLOG(ERROR) << "Sandbox overly restrictive: call to opendir("
221 << sandbox_allowed_dir
227 // Test restrictions on accessing files.
228 base::FilePath allowed_dir_path(sandbox_allowed_dir);
229 base::FilePath allowed_file = allowed_dir_path.Append("ok_to_write");
230 base::FilePath denied_file1 =
231 allowed_dir_path.DirName().Append("cant_access");
233 // Try to write a file who's name has the same prefix as the directory we
235 base::FilePath basename = allowed_dir_path.BaseName();
236 base::FilePath allowed_parent_dir = allowed_dir_path.DirName();
237 std::string tricky_filename = basename.value() + "123";
238 base::FilePath denied_file2 = allowed_parent_dir.Append(tricky_filename);
240 if (open(allowed_file.value().c_str(), O_WRONLY | O_CREAT) <= 0) {
241 PLOG(ERROR) << "Sandbox overly restrictive: failed to write ("
242 << allowed_file.value()
247 // Test that we deny access to a sibling of the sandboxed directory whose
248 // name has the sandboxed directory name as a substring. e.g. if the sandbox
249 // directory is /foo/baz then test /foo/baz_denied.
251 struct stat tmp_stat_info;
252 std::string denied_sibling =
253 std::string(sandbox_allowed_dir) + kDeniedSuffix;
254 if (stat(denied_sibling.c_str(), &tmp_stat_info) > 0) {
255 PLOG(ERROR) << "Sandbox breach: was able to stat ("
256 << denied_sibling.c_str()
262 // Test that we can stat parent directories of the "allowed" directory.
264 struct stat tmp_stat_info;
265 if (stat(allowed_parent_dir.value().c_str(), &tmp_stat_info) != 0) {
266 PLOG(ERROR) << "Sandbox overly restrictive: unable to stat ("
267 << allowed_parent_dir.value()
273 // Test that we can't stat files outside the "allowed" directory.
275 struct stat tmp_stat_info;
276 if (stat(denied_file1.value().c_str(), &tmp_stat_info) > 0) {
277 PLOG(ERROR) << "Sandbox breach: was able to stat ("
278 << denied_file1.value()
284 if (open(denied_file1.value().c_str(), O_WRONLY | O_CREAT) > 0) {
285 PLOG(ERROR) << "Sandbox breach: was able to write ("
286 << denied_file1.value()
291 if (open(denied_file2.value().c_str(), O_WRONLY | O_CREAT) > 0) {
292 PLOG(ERROR) << "Sandbox breach: was able to write ("
293 << denied_file2.value()
301 } // namespace content