Chromium Code Reviews

Side by Side Diff: sandbox/linux/syscall_broker/broker_process_unittest.cc

Issue 761903003: Update from https://crrev.com/306655 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « sandbox/linux/syscall_broker/broker_process.cc ('k') | sandbox/linux/tests/unit_tests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sandbox/linux/syscall_broker/broker_process.h" 5 #include "sandbox/linux/syscall_broker/broker_process.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <poll.h> 9 #include <poll.h>
10 #include <sys/resource.h> 10 #include <sys/resource.h>
(...skipping 36 matching lines...)
47 47
48 namespace { 48 namespace {
49 49
50 bool NoOpCallback() { 50 bool NoOpCallback() {
51 return true; 51 return true;
52 } 52 }
53 53
54 } // namespace 54 } // namespace
55 55
56 TEST(BrokerProcess, CreateAndDestroy) { 56 TEST(BrokerProcess, CreateAndDestroy) {
57 std::vector<std::string> read_whitelist; 57 std::vector<BrokerFilePermission> permissions;
58 read_whitelist.push_back("/proc/cpuinfo"); 58 permissions.push_back(BrokerFilePermission::ReadOnly("/proc/cpuinfo"));
59 59
60 scoped_ptr<BrokerProcess> open_broker( 60 scoped_ptr<BrokerProcess> open_broker(new BrokerProcess(EPERM, permissions));
61 new BrokerProcess(EPERM, read_whitelist, std::vector<std::string>()));
62 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback))); 61 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback)));
63 62
64 ASSERT_TRUE(TestUtils::CurrentProcessHasChildren()); 63 ASSERT_TRUE(TestUtils::CurrentProcessHasChildren());
65 // Destroy the broker and check it has exited properly. 64 // Destroy the broker and check it has exited properly.
66 open_broker.reset(); 65 open_broker.reset();
67 ASSERT_FALSE(TestUtils::CurrentProcessHasChildren()); 66 ASSERT_FALSE(TestUtils::CurrentProcessHasChildren());
68 } 67 }
69 68
70 TEST(BrokerProcess, TestOpenAccessNull) { 69 TEST(BrokerProcess, TestOpenAccessNull) {
71 const std::vector<std::string> empty; 70 std::vector<BrokerFilePermission> empty;
72 BrokerProcess open_broker(EPERM, empty, empty); 71 BrokerProcess open_broker(EPERM, empty);
73 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback))); 72 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
74 73
75 int fd = open_broker.Open(NULL, O_RDONLY); 74 int fd = open_broker.Open(NULL, O_RDONLY);
76 ASSERT_EQ(fd, -EFAULT); 75 ASSERT_EQ(fd, -EFAULT);
77 76
78 int ret = open_broker.Access(NULL, F_OK); 77 int ret = open_broker.Access(NULL, F_OK);
79 ASSERT_EQ(ret, -EFAULT); 78 ASSERT_EQ(ret, -EFAULT);
80 } 79 }
81 80
82 void TestOpenFilePerms(bool fast_check_in_client, int denied_errno) { 81 void TestOpenFilePerms(bool fast_check_in_client, int denied_errno) {
83 const char kR_WhiteListed[] = "/proc/DOESNOTEXIST1"; 82 const char kR_WhiteListed[] = "/proc/DOESNOTEXIST1";
84 // We can't debug the init process, and shouldn't be able to access 83 // We can't debug the init process, and shouldn't be able to access
85 // its auxv file. 84 // its auxv file.
86 const char kR_WhiteListedButDenied[] = "/proc/1/auxv"; 85 const char kR_WhiteListedButDenied[] = "/proc/1/auxv";
87 const char kW_WhiteListed[] = "/proc/DOESNOTEXIST2"; 86 const char kW_WhiteListed[] = "/proc/DOESNOTEXIST2";
88 const char kRW_WhiteListed[] = "/proc/DOESNOTEXIST3"; 87 const char kRW_WhiteListed[] = "/proc/DOESNOTEXIST3";
89 const char k_NotWhitelisted[] = "/proc/DOESNOTEXIST4"; 88 const char k_NotWhitelisted[] = "/proc/DOESNOTEXIST4";
90 89
91 std::vector<std::string> read_whitelist; 90 std::vector<BrokerFilePermission> permissions;
92 read_whitelist.push_back(kR_WhiteListed); 91 permissions.push_back(BrokerFilePermission::ReadOnly(kR_WhiteListed));
93 read_whitelist.push_back(kR_WhiteListedButDenied); 92 permissions.push_back(
94 read_whitelist.push_back(kRW_WhiteListed); 93 BrokerFilePermission::ReadOnly(kR_WhiteListedButDenied));
94 permissions.push_back(BrokerFilePermission::WriteOnly(kW_WhiteListed));
95 permissions.push_back(BrokerFilePermission::ReadWrite(kRW_WhiteListed));
95 96
96 std::vector<std::string> write_whitelist; 97 BrokerProcess open_broker(denied_errno, permissions, fast_check_in_client);
97 write_whitelist.push_back(kW_WhiteListed);
98 write_whitelist.push_back(kRW_WhiteListed);
99
100 BrokerProcess open_broker(
101 denied_errno, read_whitelist, write_whitelist, fast_check_in_client);
102 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback))); 98 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
103 99
104 int fd = -1; 100 int fd = -1;
105 fd = open_broker.Open(kR_WhiteListed, O_RDONLY); 101 fd = open_broker.Open(kR_WhiteListed, O_RDONLY);
106 ASSERT_EQ(fd, -ENOENT); 102 ASSERT_EQ(fd, -ENOENT);
107 fd = open_broker.Open(kR_WhiteListed, O_WRONLY); 103 fd = open_broker.Open(kR_WhiteListed, O_WRONLY);
108 ASSERT_EQ(fd, -denied_errno); 104 ASSERT_EQ(fd, -denied_errno);
109 fd = open_broker.Open(kR_WhiteListed, O_RDWR); 105 fd = open_broker.Open(kR_WhiteListed, O_RDWR);
110 ASSERT_EQ(fd, -denied_errno); 106 ASSERT_EQ(fd, -denied_errno);
111 int ret = -1; 107 int ret = -1;
(...skipping 124 matching lines...)
236 // Don't do anything here, so that ASSERT works in the subfunction as 232 // Don't do anything here, so that ASSERT works in the subfunction as
237 // expected. 233 // expected.
238 } 234 }
239 235
240 TEST(BrokerProcess, OpenOpenFilePermsNoClientCheckNoEnt) { 236 TEST(BrokerProcess, OpenOpenFilePermsNoClientCheckNoEnt) {
241 TestOpenFilePerms(false /* fast_check_in_client */, ENOENT); 237 TestOpenFilePerms(false /* fast_check_in_client */, ENOENT);
242 // Don't do anything here, so that ASSERT works in the subfunction as 238 // Don't do anything here, so that ASSERT works in the subfunction as
243 // expected. 239 // expected.
244 } 240 }
245 241
246 void TestOpenCpuinfo(bool fast_check_in_client) { 242 void TestBadPaths(bool fast_check_in_client) {
247 const char kFileCpuInfo[] = "/proc/cpuinfo"; 243 const char kFileCpuInfo[] = "/proc/cpuinfo";
248 std::vector<std::string> read_whitelist; 244 const char kNotAbsPath[] = "proc/cpuinfo";
249 read_whitelist.push_back(kFileCpuInfo); 245 const char kDotDotStart[] = "/../proc/cpuinfo";
246 const char kDotDotMiddle[] = "/proc/self/../cpuinfo";
247 const char kDotDotEnd[] = "/proc/..";
248 const char kTrailingSlash[] = "/proc/";
250 249
251 scoped_ptr<BrokerProcess> open_broker(new BrokerProcess( 250 std::vector<BrokerFilePermission> permissions;
252 EPERM, read_whitelist, std::vector<std::string>(), fast_check_in_client)); 251
252 permissions.push_back(BrokerFilePermission::ReadOnlyRecursive("/proc/"));
253 scoped_ptr<BrokerProcess> open_broker(
254 new BrokerProcess(EPERM, permissions, fast_check_in_client));
255 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback)));
256 // Open cpuinfo via the broker.
257 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY);
258 base::ScopedFD cpuinfo_fd_closer(cpuinfo_fd);
259 ASSERT_GE(cpuinfo_fd, 0);
260
261 int fd = -1;
262 int can_access;
263
264 can_access = open_broker->Access(kNotAbsPath, R_OK);
265 ASSERT_EQ(can_access, -EPERM);
266 fd = open_broker->Open(kNotAbsPath, O_RDONLY);
267 ASSERT_EQ(fd, -EPERM);
268
269 can_access = open_broker->Access(kDotDotStart, R_OK);
270 ASSERT_EQ(can_access, -EPERM);
271 fd = open_broker->Open(kDotDotStart, O_RDONLY);
272 ASSERT_EQ(fd, -EPERM);
273
274 can_access = open_broker->Access(kDotDotMiddle, R_OK);
275 ASSERT_EQ(can_access, -EPERM);
276 fd = open_broker->Open(kDotDotMiddle, O_RDONLY);
277 ASSERT_EQ(fd, -EPERM);
278
279 can_access = open_broker->Access(kDotDotEnd, R_OK);
280 ASSERT_EQ(can_access, -EPERM);
281 fd = open_broker->Open(kDotDotEnd, O_RDONLY);
282 ASSERT_EQ(fd, -EPERM);
283
284 can_access = open_broker->Access(kTrailingSlash, R_OK);
285 ASSERT_EQ(can_access, -EPERM);
286 fd = open_broker->Open(kTrailingSlash, O_RDONLY);
287 ASSERT_EQ(fd, -EPERM);
288 }
289
290 TEST(BrokerProcess, BadPathsClientCheck) {
291 TestBadPaths(true /* fast_check_in_client */);
292 // Don't do anything here, so that ASSERT works in the subfunction as
293 // expected.
294 }
295
296 TEST(BrokerProcess, BadPathsNoClientCheck) {
297 TestBadPaths(false /* fast_check_in_client */);
298 // Don't do anything here, so that ASSERT works in the subfunction as
299 // expected.
300 }
301
302 void TestOpenCpuinfo(bool fast_check_in_client, bool recursive) {
303 const char kFileCpuInfo[] = "/proc/cpuinfo";
304 const char kDirProc[] = "/proc/";
305
306 std::vector<BrokerFilePermission> permissions;
307 if (recursive)
308 permissions.push_back(BrokerFilePermission::ReadOnlyRecursive(kDirProc));
309 else
310 permissions.push_back(BrokerFilePermission::ReadOnly(kFileCpuInfo));
311
312 scoped_ptr<BrokerProcess> open_broker(
313 new BrokerProcess(EPERM, permissions, fast_check_in_client));
253 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback))); 314 ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback)));
254 315
255 int fd = -1; 316 int fd = -1;
256 fd = open_broker->Open(kFileCpuInfo, O_RDWR); 317 fd = open_broker->Open(kFileCpuInfo, O_RDWR);
257 base::ScopedFD fd_closer(fd); 318 base::ScopedFD fd_closer(fd);
258 ASSERT_EQ(fd, -EPERM); 319 ASSERT_EQ(fd, -EPERM);
259 320
260 // Check we can read /proc/cpuinfo. 321 // Check we can read /proc/cpuinfo.
261 int can_access = open_broker->Access(kFileCpuInfo, R_OK); 322 int can_access = open_broker->Access(kFileCpuInfo, R_OK);
262 ASSERT_EQ(can_access, 0); 323 ASSERT_EQ(can_access, 0);
(...skipping 23 matching lines...)
286 ASSERT_EQ(read_len1, read_len2); 347 ASSERT_EQ(read_len1, read_len2);
287 // Compare the cpuinfo as returned by the broker with the one we opened 348 // Compare the cpuinfo as returned by the broker with the one we opened
288 // ourselves. 349 // ourselves.
289 ASSERT_EQ(memcmp(buf, buf2, read_len1), 0); 350 ASSERT_EQ(memcmp(buf, buf2, read_len1), 0);
290 351
291 ASSERT_TRUE(TestUtils::CurrentProcessHasChildren()); 352 ASSERT_TRUE(TestUtils::CurrentProcessHasChildren());
292 open_broker.reset(); 353 open_broker.reset();
293 ASSERT_FALSE(TestUtils::CurrentProcessHasChildren()); 354 ASSERT_FALSE(TestUtils::CurrentProcessHasChildren());
294 } 355 }
295 356
296 // Run the same thing twice. The second time, we make sure that no security 357 // Run this test 4 times. With and without the check in client
297 // check is performed on the client. 358 // and using a recursive path.
298 TEST(BrokerProcess, OpenCpuinfoWithClientCheck) { 359 TEST(BrokerProcess, OpenCpuinfoWithClientCheck) {
299 TestOpenCpuinfo(true /* fast_check_in_client */); 360 TestOpenCpuinfo(true /* fast_check_in_client */, false /* not recursive */);
300 // Don't do anything here, so that ASSERT works in the subfunction as 361 // Don't do anything here, so that ASSERT works in the subfunction as
301 // expected. 362 // expected.
302 } 363 }
303 364
304 TEST(BrokerProcess, OpenCpuinfoNoClientCheck) { 365 TEST(BrokerProcess, OpenCpuinfoNoClientCheck) {
305 TestOpenCpuinfo(false /* fast_check_in_client */); 366 TestOpenCpuinfo(false /* fast_check_in_client */, false /* not recursive */);
306 // Don't do anything here, so that ASSERT works in the subfunction as 367 // Don't do anything here, so that ASSERT works in the subfunction as
307 // expected. 368 // expected.
308 } 369 }
370
371 TEST(BrokerProcess, OpenCpuinfoWithClientCheckRecursive) {
372 TestOpenCpuinfo(true /* fast_check_in_client */, true /* recursive */);
373 // Don't do anything here, so that ASSERT works in the subfunction as
374 // expected.
375 }
376
377 TEST(BrokerProcess, OpenCpuinfoNoClientCheckRecursive) {
378 TestOpenCpuinfo(false /* fast_check_in_client */, true /* recursive */);
379 // Don't do anything here, so that ASSERT works in the subfunction as
380 // expected.
381 }
309 382
310 TEST(BrokerProcess, OpenFileRW) { 383 TEST(BrokerProcess, OpenFileRW) {
311 ScopedTemporaryFile tempfile; 384 ScopedTemporaryFile tempfile;
312 const char* tempfile_name = tempfile.full_file_name(); 385 const char* tempfile_name = tempfile.full_file_name();
313 386
314 std::vector<std::string> whitelist; 387 std::vector<BrokerFilePermission> permissions;
315 whitelist.push_back(tempfile_name); 388 permissions.push_back(BrokerFilePermission::ReadWrite(tempfile_name));
316 389
317 BrokerProcess open_broker(EPERM, whitelist, whitelist); 390 BrokerProcess open_broker(EPERM, permissions);
318 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback))); 391 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
319 392
320 // Check we can access that file with read or write. 393 // Check we can access that file with read or write.
321 int can_access = open_broker.Access(tempfile_name, R_OK | W_OK); 394 int can_access = open_broker.Access(tempfile_name, R_OK | W_OK);
322 ASSERT_EQ(can_access, 0); 395 ASSERT_EQ(can_access, 0);
323 396
324 int tempfile2 = -1; 397 int tempfile2 = -1;
325 tempfile2 = open_broker.Open(tempfile_name, O_RDWR); 398 tempfile2 = open_broker.Open(tempfile_name, O_RDWR);
326 ASSERT_GE(tempfile2, 0); 399 ASSERT_GE(tempfile2, 0);
327 400
328 // Write to the descriptor opened by the broker. 401 // Write to the descriptor opened by the broker.
329 char test_text[] = "TESTTESTTEST"; 402 char test_text[] = "TESTTESTTEST";
330 ssize_t len = write(tempfile2, test_text, sizeof(test_text)); 403 ssize_t len = write(tempfile2, test_text, sizeof(test_text));
331 ASSERT_EQ(len, static_cast<ssize_t>(sizeof(test_text))); 404 ASSERT_EQ(len, static_cast<ssize_t>(sizeof(test_text)));
332 405
333 // Read back from the original file descriptor what we wrote through 406 // Read back from the original file descriptor what we wrote through
334 // the descriptor provided by the broker. 407 // the descriptor provided by the broker.
335 char buf[1024]; 408 char buf[1024];
336 len = read(tempfile.fd(), buf, sizeof(buf)); 409 len = read(tempfile.fd(), buf, sizeof(buf));
337 410
338 ASSERT_EQ(len, static_cast<ssize_t>(sizeof(test_text))); 411 ASSERT_EQ(len, static_cast<ssize_t>(sizeof(test_text)));
339 ASSERT_EQ(memcmp(test_text, buf, sizeof(test_text)), 0); 412 ASSERT_EQ(memcmp(test_text, buf, sizeof(test_text)), 0);
340 413
341 ASSERT_EQ(close(tempfile2), 0); 414 ASSERT_EQ(close(tempfile2), 0);
342 } 415 }
343 416
344 // SANDBOX_TEST because the process could die with a SIGPIPE 417 // SANDBOX_TEST because the process could die with a SIGPIPE
345 // and we want this to happen in a subprocess. 418 // and we want this to happen in a subprocess.
346 SANDBOX_TEST(BrokerProcess, BrokerDied) { 419 SANDBOX_TEST(BrokerProcess, BrokerDied) {
347 std::vector<std::string> read_whitelist; 420 const char kCpuInfo[] = "/proc/cpuinfo";
348 read_whitelist.push_back("/proc/cpuinfo"); 421 std::vector<BrokerFilePermission> permissions;
422 permissions.push_back(BrokerFilePermission::ReadOnly(kCpuInfo));
349 423
350 BrokerProcess open_broker(EPERM, 424 BrokerProcess open_broker(EPERM, permissions, true /* fast_check_in_client */,
351 read_whitelist,
352 std::vector<std::string>(),
353 true /* fast_check_in_client */,
354 true /* quiet_failures_for_tests */); 425 true /* quiet_failures_for_tests */);
355 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback))); 426 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback)));
356 const pid_t broker_pid = open_broker.broker_pid(); 427 const pid_t broker_pid = open_broker.broker_pid();
357 SANDBOX_ASSERT(kill(broker_pid, SIGKILL) == 0); 428 SANDBOX_ASSERT(kill(broker_pid, SIGKILL) == 0);
358 429
359 // Now we check that the broker has been signaled, but do not reap it. 430 // Now we check that the broker has been signaled, but do not reap it.
360 siginfo_t process_info; 431 siginfo_t process_info;
361 SANDBOX_ASSERT(HANDLE_EINTR(waitid( 432 SANDBOX_ASSERT(HANDLE_EINTR(waitid(
362 P_PID, broker_pid, &process_info, WEXITED | WNOWAIT)) == 433 P_PID, broker_pid, &process_info, WEXITED | WNOWAIT)) ==
363 0); 434 0);
364 SANDBOX_ASSERT(broker_pid == process_info.si_pid); 435 SANDBOX_ASSERT(broker_pid == process_info.si_pid);
365 SANDBOX_ASSERT(CLD_KILLED == process_info.si_code); 436 SANDBOX_ASSERT(CLD_KILLED == process_info.si_code);
366 SANDBOX_ASSERT(SIGKILL == process_info.si_status); 437 SANDBOX_ASSERT(SIGKILL == process_info.si_status);
367 438
368 // Check that doing Open with a dead broker won't SIGPIPE us. 439 // Check that doing Open with a dead broker won't SIGPIPE us.
369 SANDBOX_ASSERT(open_broker.Open("/proc/cpuinfo", O_RDONLY) == -ENOMEM); 440 SANDBOX_ASSERT(open_broker.Open(kCpuInfo, O_RDONLY) == -ENOMEM);
370 SANDBOX_ASSERT(open_broker.Access("/proc/cpuinfo", O_RDONLY) == -ENOMEM); 441 SANDBOX_ASSERT(open_broker.Access(kCpuInfo, O_RDONLY) == -ENOMEM);
371 } 442 }
372 443
373 void TestOpenComplexFlags(bool fast_check_in_client) { 444 void TestOpenComplexFlags(bool fast_check_in_client) {
374 const char kCpuInfo[] = "/proc/cpuinfo"; 445 const char kCpuInfo[] = "/proc/cpuinfo";
375 std::vector<std::string> whitelist; 446 std::vector<BrokerFilePermission> permissions;
376 whitelist.push_back(kCpuInfo); 447 permissions.push_back(BrokerFilePermission::ReadOnly(kCpuInfo));
377 448
378 BrokerProcess open_broker(EPERM, whitelist, whitelist, fast_check_in_client); 449 BrokerProcess open_broker(EPERM, permissions, fast_check_in_client);
379 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback))); 450 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
380 // Test that we do the right thing for O_CLOEXEC and O_NONBLOCK. 451 // Test that we do the right thing for O_CLOEXEC and O_NONBLOCK.
381 int fd = -1; 452 int fd = -1;
382 int ret = 0; 453 int ret = 0;
383 fd = open_broker.Open(kCpuInfo, O_RDONLY); 454 fd = open_broker.Open(kCpuInfo, O_RDONLY);
384 ASSERT_GE(fd, 0); 455 ASSERT_GE(fd, 0);
385 ret = fcntl(fd, F_GETFL); 456 ret = fcntl(fd, F_GETFL);
386 ASSERT_NE(-1, ret); 457 ASSERT_NE(-1, ret);
387 // The descriptor shouldn't have the O_CLOEXEC attribute, nor O_NONBLOCK. 458 // The descriptor shouldn't have the O_CLOEXEC attribute, nor O_NONBLOCK.
388 ASSERT_EQ(0, ret & (O_CLOEXEC | O_NONBLOCK)); 459 ASSERT_EQ(0, ret & (O_CLOEXEC | O_NONBLOCK));
(...skipping 58 matching lines...)
447 518
448 // Valgrind doesn't allow changing the hard descriptor limit, so we only 519 // Valgrind doesn't allow changing the hard descriptor limit, so we only
449 // change the soft descriptor limit here. 520 // change the soft descriptor limit here.
450 struct rlimit rlim; 521 struct rlimit rlim;
451 SANDBOX_ASSERT(0 == getrlimit(RLIMIT_NOFILE, &rlim)); 522 SANDBOX_ASSERT(0 == getrlimit(RLIMIT_NOFILE, &rlim));
452 SANDBOX_ASSERT(fd_limit <= rlim.rlim_cur); 523 SANDBOX_ASSERT(fd_limit <= rlim.rlim_cur);
453 rlim.rlim_cur = fd_limit; 524 rlim.rlim_cur = fd_limit;
454 SANDBOX_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &rlim)); 525 SANDBOX_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &rlim));
455 526
456 static const char kCpuInfo[] = "/proc/cpuinfo"; 527 static const char kCpuInfo[] = "/proc/cpuinfo";
457 std::vector<std::string> read_whitelist; 528 std::vector<BrokerFilePermission> permissions;
458 read_whitelist.push_back(kCpuInfo); 529 permissions.push_back(BrokerFilePermission::ReadOnly(kCpuInfo));
459 530
460 BrokerProcess open_broker(EPERM, read_whitelist, std::vector<std::string>()); 531 BrokerProcess open_broker(EPERM, permissions);
461 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback))); 532 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback)));
462 533
463 const int ipc_fd = BrokerProcessTestHelper::GetIPCDescriptor(&open_broker); 534 const int ipc_fd = BrokerProcessTestHelper::GetIPCDescriptor(&open_broker);
464 SANDBOX_ASSERT(ipc_fd >= 0); 535 SANDBOX_ASSERT(ipc_fd >= 0);
465 536
466 static const char kBogus[] = "not a pickle"; 537 static const char kBogus[] = "not a pickle";
467 std::vector<int> fds; 538 std::vector<int> fds;
468 fds.push_back(message_fd.get()); 539 fds.push_back(message_fd.get());
469 540
470 // The broker process should only have a couple spare file descriptors 541 // The broker process should only have a couple spare file descriptors
(...skipping 20 matching lines...)
491 struct pollfd poll_fd = {reader, POLLIN | POLLRDHUP, 0}; 562 struct pollfd poll_fd = {reader, POLLIN | POLLRDHUP, 0};
492 const int num_events = HANDLE_EINTR(poll(&poll_fd, 1, timeout_in_ms)); 563 const int num_events = HANDLE_EINTR(poll(&poll_fd, 1, timeout_in_ms));
493 if (1 == num_events && poll_fd.revents | POLLHUP) 564 if (1 == num_events && poll_fd.revents | POLLHUP)
494 return true; 565 return true;
495 return false; 566 return false;
496 } 567 }
497 568
498 // Closing the broker client's IPC channel should terminate the broker 569 // Closing the broker client's IPC channel should terminate the broker
499 // process. 570 // process.
500 TEST(BrokerProcess, BrokerDiesOnClosedChannel) { 571 TEST(BrokerProcess, BrokerDiesOnClosedChannel) {
501 std::vector<std::string> read_whitelist; 572 std::vector<BrokerFilePermission> permissions;
502 read_whitelist.push_back("/proc/cpuinfo"); 573 permissions.push_back(BrokerFilePermission::ReadOnly("/proc/cpuinfo"));
503 574
504 // Get the writing end of a pipe into the broker (child) process so 575 // Get the writing end of a pipe into the broker (child) process so
505 // that we can reliably detect when it dies. 576 // that we can reliably detect when it dies.
506 int lifeline_fds[2]; 577 int lifeline_fds[2];
507 PCHECK(0 == pipe(lifeline_fds)); 578 PCHECK(0 == pipe(lifeline_fds));
508 579
509 BrokerProcess open_broker(EPERM, read_whitelist, std::vector<std::string>(), 580 BrokerProcess open_broker(EPERM, permissions, true /* fast_check_in_client */,
510 true /* fast_check_in_client */,
511 false /* quiet_failures_for_tests */); 581 false /* quiet_failures_for_tests */);
512 ASSERT_TRUE(open_broker.Init(base::Bind(&CloseFD, lifeline_fds[0]))); 582 ASSERT_TRUE(open_broker.Init(base::Bind(&CloseFD, lifeline_fds[0])));
513 // Make sure the writing end only exists in the broker process. 583 // Make sure the writing end only exists in the broker process.
514 CloseFD(lifeline_fds[1]); 584 CloseFD(lifeline_fds[1]);
515 base::ScopedFD reader(lifeline_fds[0]); 585 base::ScopedFD reader(lifeline_fds[0]);
516 586
517 const pid_t broker_pid = open_broker.broker_pid(); 587 const pid_t broker_pid = open_broker.broker_pid();
518 588
519 // This should cause the broker process to exit. 589 // This should cause the broker process to exit.
520 BrokerProcessTestHelper::CloseChannel(&open_broker); 590 BrokerProcessTestHelper::CloseChannel(&open_broker);
521 591
522 const int kTimeoutInMilliseconds = 5000; 592 const int kTimeoutInMilliseconds = 5000;
523 const bool broker_lifeline_closed = 593 const bool broker_lifeline_closed =
524 WaitForClosedPipeWriter(reader.get(), kTimeoutInMilliseconds); 594 WaitForClosedPipeWriter(reader.get(), kTimeoutInMilliseconds);
525 // If the broker exited, its lifeline fd should be closed. 595 // If the broker exited, its lifeline fd should be closed.
526 ASSERT_TRUE(broker_lifeline_closed); 596 ASSERT_TRUE(broker_lifeline_closed);
527 // Now check that the broker has exited, but do not reap it. 597 // Now check that the broker has exited, but do not reap it.
528 siginfo_t process_info; 598 siginfo_t process_info;
529 ASSERT_EQ(0, HANDLE_EINTR(waitid(P_PID, broker_pid, &process_info, 599 ASSERT_EQ(0, HANDLE_EINTR(waitid(P_PID, broker_pid, &process_info,
530 WEXITED | WNOWAIT))); 600 WEXITED | WNOWAIT)));
531 EXPECT_EQ(broker_pid, process_info.si_pid); 601 EXPECT_EQ(broker_pid, process_info.si_pid);
532 EXPECT_EQ(CLD_EXITED, process_info.si_code); 602 EXPECT_EQ(CLD_EXITED, process_info.si_code);
533 EXPECT_EQ(1, process_info.si_status); 603 EXPECT_EQ(1, process_info.si_status);
534 } 604 }
535 605
606 TEST(BrokerProcess, CreateFile) {
607 std::string temp_str;
608 {
609 ScopedTemporaryFile tmp_file;
610 temp_str = tmp_file.full_file_name();
611 }
612 const char* tempfile_name = temp_str.c_str();
613
614 std::vector<BrokerFilePermission> permissions;
615 permissions.push_back(BrokerFilePermission::ReadWriteCreate(tempfile_name));
616
617 BrokerProcess open_broker(EPERM, permissions);
618 ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
619
620 int fd = -1;
621
622 // Try without O_EXCL
623 fd = open_broker.Open(tempfile_name, O_RDWR | O_CREAT);
624 ASSERT_EQ(fd, -EPERM);
625
626 const char kTestText[] = "TESTTESTTEST";
627 // Create a file
628 fd = open_broker.Open(tempfile_name, O_RDWR | O_CREAT | O_EXCL);
629 ASSERT_GE(fd, 0);
630 {
631 base::ScopedFD scoped_fd(fd);
632
633 // Confirm fail if file exists
634 int bad_fd = open_broker.Open(tempfile_name, O_RDWR | O_CREAT | O_EXCL);
635 ASSERT_EQ(bad_fd, -EEXIST);
636
637 // Write to the descriptor opened by the broker.
638
639 ssize_t len = HANDLE_EINTR(write(fd, kTestText, sizeof(kTestText)));
640 ASSERT_EQ(len, static_cast<ssize_t>(sizeof(kTestText)));
641 }
642
643 int fd_check = open(tempfile_name, O_RDONLY);
644 ASSERT_GE(fd_check, 0);
645 {
646 base::ScopedFD scoped_fd(fd_check);
647 char buf[1024];
648 ssize_t len = HANDLE_EINTR(read(fd_check, buf, sizeof(buf)));
649
650 ASSERT_EQ(len, static_cast<ssize_t>(sizeof(kTestText)));
651 ASSERT_EQ(memcmp(kTestText, buf, sizeof(kTestText)), 0);
652 }
653 }
654
536 } // namespace syscall_broker 655 } // namespace syscall_broker
537 656
538 } // namespace sandbox 657 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/syscall_broker/broker_process.cc ('k') | sandbox/linux/tests/unit_tests.cc » ('j') | no next file with comments »

Powered by Google App Engine