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

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: Write the uid and gid maps in the pre_exec_delegate. 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 #include <utility>
13
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"
27
28 namespace sandbox {
29
30 namespace {
31
32 bool RootDirectoryIsEmpty() {
33 base::FilePath root("/");
34 int file_type =
35 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES;
36 base::FileEnumerator enumerator_before(root, false, file_type);
37 return enumerator_before.Next().empty();
38 }
39
40 class NamespaceSandboxTest : public base::MultiProcessTest {
41 public:
42 void TestProc(const std::string& procname) {
43 if (!Credentials::CanCreateProcessInNewUserNS()) {
44 return;
45 }
46
47 NamespaceSandbox namespace_sandbox;
48 base::FileHandleMappingVector fds_to_remap = {
49 std::make_pair(STDOUT_FILENO, STDOUT_FILENO),
50 std::make_pair(STDERR_FILENO, STDERR_FILENO),
51 };
52 namespace_sandbox.mutable_launch_options()->fds_to_remap = &fds_to_remap;
53 base::Process process = namespace_sandbox.Launch(MakeCmdLine(procname));
54 ASSERT_TRUE(process.IsValid());
55
56 const int kDummyExitCode = 42;
57 int exit_code = kDummyExitCode;
58 EXPECT_TRUE(process.WaitForExit(&exit_code));
59 EXPECT_EQ(0, exit_code);
60 }
61 };
62
63 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) {
64 scoped_ptr<base::Environment> env(base::Environment::Create());
65 bool in_user_ns = NamespaceSandbox::InNewUserNamespace();
66 bool in_pid_ns = NamespaceSandbox::InNewPidNamespace();
67 bool in_net_ns = NamespaceSandbox::InNewNetNamespace();
68 CHECK(in_user_ns);
69 CHECK_EQ(in_pid_ns,
70 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWPID));
71 CHECK_EQ(in_net_ns,
72 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWNET));
73 if (in_pid_ns) {
74 CHECK_EQ(1, getpid());
75 }
76 return 0;
77 }
78
79 TEST_F(NamespaceSandboxTest, BasicUsage) {
80 TestProc("SimpleChildProcess");
81 }
82
83 MULTIPROCESS_TEST_MAIN(ChrootMe) {
84 CHECK(!RootDirectoryIsEmpty());
85 CHECK(sandbox::Credentials::MoveToNewUserNS());
86 CHECK(sandbox::Credentials::DropFileSystemAccess());
87 CHECK(RootDirectoryIsEmpty());
88 return 0;
89 }
90
91 TEST_F(NamespaceSandboxTest, ChrootAndDropCapabilities) {
92 TestProc("ChrootMe");
93 }
94
95 MULTIPROCESS_TEST_MAIN(NestedNamespaceSandbox) {
96 NamespaceSandbox namespace_sandbox;
97 base::FileHandleMappingVector fds_to_remap = {
98 std::make_pair(STDOUT_FILENO, STDOUT_FILENO),
99 std::make_pair(STDERR_FILENO, STDERR_FILENO),
100 };
101 namespace_sandbox.mutable_launch_options()->fds_to_remap = &fds_to_remap;
102 base::Process process =
103 namespace_sandbox.Launch(base::CommandLine(base::FilePath("/bin/true")));
104 CHECK(process.IsValid());
105
106 const int kDummyExitCode = 42;
107 int exit_code = kDummyExitCode;
108 CHECK(process.WaitForExit(&exit_code));
109 CHECK_EQ(0, exit_code);
110 return 0;
111 }
112
113 TEST_F(NamespaceSandboxTest, NestedNamespaceSandbox) {
114 TestProc("NestedNamespaceSandbox");
115 }
116
117 } // namespace
118
119 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698