OLD | NEW |
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/services/broker_process.h" | 5 #include "sandbox/linux/services/broker_process.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
| 14 #include <algorithm> |
14 #include <string> | 15 #include <string> |
15 #include <vector> | 16 #include <vector> |
16 | 17 |
17 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
18 #include "base/bind.h" | 19 #include "base/bind.h" |
19 #include "base/file_util.h" | 20 #include "base/file_util.h" |
20 #include "base/files/scoped_file.h" | 21 #include "base/files/scoped_file.h" |
21 #include "base/logging.h" | 22 #include "base/logging.h" |
22 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
23 #include "base/posix/eintr_wrapper.h" | 24 #include "base/posix/eintr_wrapper.h" |
| 25 #include "base/posix/unix_domain_socket_linux.h" |
24 #include "sandbox/linux/tests/test_utils.h" | 26 #include "sandbox/linux/tests/test_utils.h" |
25 #include "sandbox/linux/tests/unit_tests.h" | 27 #include "sandbox/linux/tests/unit_tests.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
27 | 29 |
28 namespace sandbox { | 30 namespace sandbox { |
29 | 31 |
30 namespace { | 32 namespace { |
31 | 33 |
32 // Creates and open a temporary file on creation and closes | 34 // Creates and open a temporary file on creation and closes |
33 // and removes it on destruction. | 35 // and removes it on destruction. |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // Don't do anything here, so that ASSERT works in the subfunction as | 429 // Don't do anything here, so that ASSERT works in the subfunction as |
428 // expected. | 430 // expected. |
429 } | 431 } |
430 | 432 |
431 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { | 433 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { |
432 TestOpenComplexFlags(false /* fast_check_in_client */); | 434 TestOpenComplexFlags(false /* fast_check_in_client */); |
433 // Don't do anything here, so that ASSERT works in the subfunction as | 435 // Don't do anything here, so that ASSERT works in the subfunction as |
434 // expected. | 436 // expected. |
435 } | 437 } |
436 | 438 |
| 439 // We need to allow noise because the broker will log when it receives our |
| 440 // bogus IPCs. |
| 441 SANDBOX_TEST_ALLOW_NOISE(BrokerProcess, RecvMsgDescriptorLeak) { |
| 442 // Find the four lowest available file descriptors. |
| 443 int available_fds[4]; |
| 444 SANDBOX_ASSERT(0 == pipe(available_fds)); |
| 445 SANDBOX_ASSERT(0 == pipe(available_fds + 2)); |
| 446 |
| 447 // Save one FD to send to the broker later, and close the others. |
| 448 for (size_t i = 1; i < arraysize(available_fds); i++) { |
| 449 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(available_fds[i]))); |
| 450 } |
| 451 |
| 452 // Lower our file descriptor limit to just allow three more file descriptors |
| 453 // to be allocated. (N.B., RLIMIT_NOFILE doesn't limit the number of file |
| 454 // descriptors a process can have: it only limits the highest value that can |
| 455 // be assigned to newly-created descriptors allocated by the process.) |
| 456 const rlim_t fd_limit = |
| 457 1 + *std::max_element(available_fds, |
| 458 available_fds + arraysize(available_fds)); |
| 459 const struct rlimit new_rlim = {fd_limit, fd_limit}; |
| 460 SANDBOX_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &new_rlim)); |
| 461 |
| 462 const char kCpuInfo[] = "/proc/cpuinfo"; |
| 463 std::vector<std::string> read_whitelist; |
| 464 read_whitelist.push_back(kCpuInfo); |
| 465 |
| 466 BrokerProcess open_broker(EPERM, read_whitelist, std::vector<std::string>()); |
| 467 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback))); |
| 468 |
| 469 const int ipc_fd = open_broker.ipc_socketpair_; |
| 470 SANDBOX_ASSERT(ipc_fd >= 0); |
| 471 |
| 472 static const char kBogus[] = "not a pickle"; |
| 473 std::vector<int> fds; |
| 474 fds.push_back(available_fds[0]); |
| 475 |
| 476 // The broker process should only have a couple spare file descriptors |
| 477 // available, but for good measure we send it fd_limit bogus IPCs anyway. |
| 478 for (rlim_t i = 0; i < fd_limit; ++i) { |
| 479 SANDBOX_ASSERT( |
| 480 UnixDomainSocket::SendMsg(ipc_fd, kBogus, sizeof(kBogus), fds)); |
| 481 } |
| 482 |
| 483 const int fd = open_broker.Open(kCpuInfo, O_RDONLY); |
| 484 SANDBOX_ASSERT(fd >= 0); |
| 485 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(fd))); |
| 486 } |
| 487 |
437 } // namespace sandbox | 488 } // namespace sandbox |
OLD | NEW |