Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

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

Issue 229893002: Add unit test to check for broker FD leak (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ensure available_fds[0] is closed too Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sandbox/linux/services/broker_process.h ('k') | sandbox/linux/tests/unit_tests.h » ('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/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
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.
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 base::ScopedFD message_fd(available_fds[0]);
456 for (size_t i = 1; i < arraysize(available_fds); i++) {
457 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(available_fds[i])));
458 }
459
460 // Lower our file descriptor limit to just allow three more file descriptors
461 // to be allocated. (N.B., RLIMIT_NOFILE doesn't limit the number of file
462 // descriptors a process can have: it only limits the highest value that can
463 // be assigned to newly-created descriptors allocated by the process.)
464 const rlim_t fd_limit =
465 1 + *std::max_element(available_fds,
466 available_fds + arraysize(available_fds));
467 const struct rlimit new_rlim = {fd_limit, fd_limit};
468 SANDBOX_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &new_rlim));
469
470 static const char kCpuInfo[] = "/proc/cpuinfo";
471 std::vector<std::string> read_whitelist;
472 read_whitelist.push_back(kCpuInfo);
473
474 BrokerProcess open_broker(EPERM, read_whitelist, std::vector<std::string>());
475 SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback)));
476
477 const int ipc_fd = BrokerProcessTestHelper::get_ipc_socketpair(&open_broker);
478 SANDBOX_ASSERT(ipc_fd >= 0);
479
480 static const char kBogus[] = "not a pickle";
481 std::vector<int> fds;
482 fds.push_back(message_fd.get());
483
484 // The broker process should only have a couple spare file descriptors
485 // available, but for good measure we send it fd_limit bogus IPCs anyway.
486 for (rlim_t i = 0; i < fd_limit; ++i) {
487 SANDBOX_ASSERT(
488 UnixDomainSocket::SendMsg(ipc_fd, kBogus, sizeof(kBogus), fds));
489 }
490
491 const int fd = open_broker.Open(kCpuInfo, O_RDONLY);
492 SANDBOX_ASSERT(fd >= 0);
493 SANDBOX_ASSERT(0 == IGNORE_EINTR(close(fd)));
494 }
495
437 } // namespace sandbox 496 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/services/broker_process.h ('k') | sandbox/linux/tests/unit_tests.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698