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

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

Issue 881733002: Add namespace sandbox class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Misc small changes Created 5 years, 10 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
OLDNEW
(Empty)
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.
4
5 #include "sandbox/linux/services/namespace_sandbox.h"
6
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10
11 #include <string>
12
13 #include "base/command_line.h"
14 #include "base/files/file_enumerator.h"
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/process/launch.h"
19 #include "base/process/process.h"
20 #include "base/test/multiprocess_test.h"
21 #include "sandbox/linux/services/credentials.h"
22 #include "sandbox/linux/services/namespace_utils.h"
23 #include "sandbox/linux/tests/unit_tests.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/multiprocess_func_list.h"
26
27 namespace sandbox {
28
29 namespace {
30
31 bool RootDirectoryIsEmpty() {
32 base::FilePath root("/");
33 int file_type =
34 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES;
35 base::FileEnumerator enumerator_before(root, false, file_type);
36 return enumerator_before.Next().empty();
37 }
38
39 class NamespaceSandboxTest : public base::MultiProcessTest {
40 public:
41 void TestProc(const std::string& procname) {
42 if (!Credentials::CanCreateProcessInNewUserNS()) {
43 return;
44 }
45
46 base::LaunchOptions options;
47
48 base::FileHandleMappingVector fds_to_map;
49 fds_to_map.push_back(std::make_pair(STDOUT_FILENO, STDOUT_FILENO));
50 fds_to_map.push_back(std::make_pair(STDERR_FILENO, STDERR_FILENO));
51 options.fds_to_remap = &fds_to_map;
52
53 NamespaceSandbox namespace_sandbox;
54 namespace_sandbox.SetupLaunchOptions(&options, &fds_to_map);
55
56 base::Process process(SpawnChildWithOptions(procname, options));
57 ASSERT_TRUE(process.IsValid());
58 namespace_sandbox.PrepareSandboxedProcess(process.Pid());
59
60 const int kDummyExitCode = 42;
61 int exit_code = kDummyExitCode;
62 EXPECT_TRUE(process.WaitForExit(&exit_code));
63 EXPECT_EQ(0, exit_code);
64 }
65 };
66
67 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) {
68 scoped_ptr<base::Environment> env(base::Environment::Create());
69 bool in_user_ns = NamespaceSandbox::InNewUserNamespace();
70 bool in_pid_ns = NamespaceSandbox::InNewPidNamespace();
71 bool in_net_ns = NamespaceSandbox::InNewNetNamespace();
72 CHECK(in_user_ns);
73 CHECK_EQ(in_pid_ns,
74 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWPID));
75 CHECK_EQ(in_net_ns,
76 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWNET));
77 if (in_pid_ns) {
78 CHECK_EQ(1, getpid());
79 }
80 return 0;
81 }
82
83 TEST_F(NamespaceSandboxTest, BasicUsage) {
84 TestProc("SimpleChildProcess");
85 }
86
87 MULTIPROCESS_TEST_MAIN(ChrootMe) {
88 CHECK(!RootDirectoryIsEmpty());
89 CHECK(sandbox::Credentials::MoveToNewUserNS());
90 CHECK(sandbox::Credentials::DropFileSystemAccess());
91 CHECK(RootDirectoryIsEmpty());
92 return 0;
93 }
94
95 TEST_F(NamespaceSandboxTest, ChrootAndDropCapabilities) {
96 TestProc("ChrootMe");
97 }
98
99 MULTIPROCESS_TEST_MAIN(NestedNamespaceSandbox) {
100 base::LaunchOptions options;
101
102 base::FileHandleMappingVector fds_to_map;
103 fds_to_map.push_back(std::make_pair(STDOUT_FILENO, STDOUT_FILENO));
104 fds_to_map.push_back(std::make_pair(STDERR_FILENO, STDERR_FILENO));
105 options.fds_to_remap = &fds_to_map;
106
107 NamespaceSandbox namespace_sandbox;
108 namespace_sandbox.SetupLaunchOptions(&options, &fds_to_map);
109
110 base::CommandLine command_line(base::FilePath("/bin/true"));
111 base::Process process(base::LaunchProcess(command_line, options));
112 CHECK(process.IsValid());
113 namespace_sandbox.PrepareSandboxedProcess(process.Pid());
114
115 const int kDummyExitCode = 42;
116 int exit_code = kDummyExitCode;
117 CHECK(process.WaitForExit(&exit_code));
118 CHECK_EQ(0, exit_code);
119 return 0;
120 }
121
122 TEST_F(NamespaceSandboxTest, NestedNamespaceSandbox) {
123 TestProc("NestedNamespaceSandbox");
124 }
125
126 } // namespace
127
128 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698