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 |
32 class BrokerProcessTestHelper { | |
33 public: | |
34 static int get_ipc_socketpair(const BrokerProcess* broker) { | |
35 return broker->ipc_socketpair_; | |
36 } | |
37 }; | |
38 | |
30 namespace { | 39 namespace { |
31 | 40 |
32 // Creates and open a temporary file on creation and closes | 41 // Creates and open a temporary file on creation and closes |
33 // and removes it on destruction. | 42 // and removes it on destruction. |
34 // Unlike base/ helpers, this does not require JNI on Android. | 43 // Unlike base/ helpers, this does not require JNI on Android. |
35 class ScopedTemporaryFile { | 44 class ScopedTemporaryFile { |
36 public: | 45 public: |
37 ScopedTemporaryFile() | 46 ScopedTemporaryFile() |
38 : fd_(-1) { | 47 : fd_(-1) { |
39 #if defined(OS_ANDROID) | 48 #if defined(OS_ANDROID) |
(...skipping 387 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 | 436 // Don't do anything here, so that ASSERT works in the subfunction as |
428 // expected. | 437 // expected. |
429 } | 438 } |
430 | 439 |
431 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { | 440 TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) { |
432 TestOpenComplexFlags(false /* fast_check_in_client */); | 441 TestOpenComplexFlags(false /* fast_check_in_client */); |
433 // Don't do anything here, so that ASSERT works in the subfunction as | 442 // Don't do anything here, so that ASSERT works in the subfunction as |
434 // expected. | 443 // expected. |
435 } | 444 } |
436 | 445 |
446 // We need to allow noise because the broker will log when it receives our | |
447 // bogus IPCs. | |
448 SANDBOX_TEST_ALLOW_NOISE(BrokerProcess, RecvMsgDescriptorLeak) { | |
449 // Find the four lowest available file descriptors. | |
jln (very slow on Chromium)
2014/04/10 20:21:15
Replace with "Find four available file descriptors
mdempsky
2014/04/10 20:35:51
As discussed, POSIX requires pipe() to return the
| |
450 int available_fds[4]; | |
451 SANDBOX_ASSERT(0 == pipe(available_fds)); | |
452 SANDBOX_ASSERT(0 == pipe(available_fds + 2)); | |
453 | |
454 // Save one FD to send to the broker later, and close the others. | |
455 for (size_t i = 1; i < arraysize(available_fds); i++) { | |
jln (very slow on Chromium)
2014/04/10 20:21:15
Didn't you forget to close available_fds[0]?
Make
mdempsky
2014/04/10 20:35:51
Done.
| |
456 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(available_fds[i]))); | |
457 } | |
458 | |
459 // Lower our file descriptor limit to just allow three more file descriptors | |
460 // to be allocated. (N.B., RLIMIT_NOFILE doesn't limit the number of file | |
461 // descriptors a process can have: it only limits the highest value that can | |
462 // be assigned to newly-created descriptors allocated by the process.) | |
463 const rlim_t fd_limit = | |
jln (very slow on Chromium)
2014/04/10 20:21:15
kFdLimit
mdempsky
2014/04/10 20:35:51
As discussed, this isn't a "compile-time constant"
| |
464 1 + *std::max_element(available_fds, | |
465 available_fds + arraysize(available_fds)); | |
466 const struct rlimit new_rlim = {fd_limit, fd_limit}; | |
467 SANDBOX_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &new_rlim)); | |
468 | |
469 const char kCpuInfo[] = "/proc/cpuinfo"; | |
470 std::vector<std::string> read_whitelist; | |
471 read_whitelist.push_back(kCpuInfo); | |
472 | |
473 BrokerProcess open_broker(EPERM, read_whitelist, std::vector<std::string>()); | |
474 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback))); | |
475 | |
476 const int ipc_fd = BrokerProcessTestHelper::get_ipc_socketpair(&open_broker); | |
477 SANDBOX_ASSERT(ipc_fd >= 0); | |
478 | |
479 static const char kBogus[] = "not a pickle"; | |
480 std::vector<int> fds; | |
481 fds.push_back(available_fds[0]); | |
482 | |
483 // The broker process should only have a couple spare file descriptors | |
jln (very slow on Chromium)
2014/04/10 20:21:15
Sending kFdLimit descriptors is guaranteed to work
mdempsky
2014/04/10 20:35:51
Yep, that's what I was trying to convey with the c
| |
484 // available, but for good measure we send it fd_limit bogus IPCs anyway. | |
485 for (rlim_t i = 0; i < fd_limit; ++i) { | |
486 SANDBOX_ASSERT( | |
487 UnixDomainSocket::SendMsg(ipc_fd, kBogus, sizeof(kBogus), fds)); | |
488 } | |
489 | |
490 const int fd = open_broker.Open(kCpuInfo, O_RDONLY); | |
491 SANDBOX_ASSERT(fd >= 0); | |
492 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(fd))); | |
493 } | |
494 | |
437 } // namespace sandbox | 495 } // namespace sandbox |
OLD | NEW |