1 // Copyright 2015 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 #include "sandbox/linux/services/namespace_sandbox.h"
14 #include "base/command_line.h"
15 #include "base/files/file_enumerator.h"
16 #include "base/files/file_path.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/process/launch.h"
20 #include "base/process/process.h"
21 #include "base/test/multiprocess_test.h"
22 #include "sandbox/linux/services/credentials.h"
23 #include "sandbox/linux/services/namespace_utils.h"
24 #include "sandbox/linux/tests/unit_tests.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "testing/multiprocess_func_list.h"
32 bool RootDirectoryIsEmpty() {
33 base::FilePath
root("/");
35 base::FileEnumerator::DIRECTORIES
| base::FileEnumerator::FILES
;
36 base::FileEnumerator
enumerator_before(root
, false, file_type
);
37 return enumerator_before
.Next().empty();
40 class NamespaceSandboxTest
: public base::MultiProcessTest
{
42 void TestProc(const std::string
& procname
) {
43 if (!Credentials::CanCreateProcessInNewUserNS()) {
47 base::FileHandleMappingVector fds_to_remap
= {
48 std::make_pair(STDOUT_FILENO
, STDOUT_FILENO
),
49 std::make_pair(STDERR_FILENO
, STDERR_FILENO
),
51 base::LaunchOptions launch_options
;
52 launch_options
.fds_to_remap
= &fds_to_remap
;
54 base::Process process
=
55 NamespaceSandbox::LaunchProcess(MakeCmdLine(procname
), launch_options
);
56 ASSERT_TRUE(process
.IsValid());
58 const int kDummyExitCode
= 42;
59 int exit_code
= kDummyExitCode
;
60 EXPECT_TRUE(process
.WaitForExit(&exit_code
));
61 EXPECT_EQ(0, exit_code
);
65 MULTIPROCESS_TEST_MAIN(SimpleChildProcess
) {
66 scoped_ptr
<base::Environment
> env(base::Environment::Create());
67 bool in_user_ns
= NamespaceSandbox::InNewUserNamespace();
68 bool in_pid_ns
= NamespaceSandbox::InNewPidNamespace();
69 bool in_net_ns
= NamespaceSandbox::InNewNetNamespace();
72 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWPID
));
74 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWNET
));
76 CHECK_EQ(1, getpid());
81 TEST_F(NamespaceSandboxTest
, BasicUsage
) {
82 TestProc("SimpleChildProcess");
85 MULTIPROCESS_TEST_MAIN(ChrootMe
) {
86 CHECK(!RootDirectoryIsEmpty());
87 CHECK(sandbox::Credentials::MoveToNewUserNS());
88 CHECK(sandbox::Credentials::DropFileSystemAccess());
89 CHECK(RootDirectoryIsEmpty());
93 // Temporarily disabled on ASAN due to crbug.com/451603.
94 TEST_F(NamespaceSandboxTest
, DISABLE_ON_ASAN(ChrootAndDropCapabilities
)) {
98 MULTIPROCESS_TEST_MAIN(NestedNamespaceSandbox
) {
99 base::FileHandleMappingVector fds_to_remap
= {
100 std::make_pair(STDOUT_FILENO
, STDOUT_FILENO
),
101 std::make_pair(STDERR_FILENO
, STDERR_FILENO
),
103 base::LaunchOptions launch_options
;
104 launch_options
.fds_to_remap
= &fds_to_remap
;
105 base::Process process
= NamespaceSandbox::LaunchProcess(
106 base::CommandLine(base::FilePath("/bin/true")), launch_options
);
107 CHECK(process
.IsValid());
109 const int kDummyExitCode
= 42;
110 int exit_code
= kDummyExitCode
;
111 CHECK(process
.WaitForExit(&exit_code
));
112 CHECK_EQ(0, exit_code
);
116 TEST_F(NamespaceSandboxTest
, NestedNamespaceSandbox
) {
117 TestProc("NestedNamespaceSandbox");
122 } // namespace sandbox