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

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

Issue 885443002: Roll Chrome into Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase to ToT mojo 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
« no previous file with comments | « sandbox/linux/services/namespace_utils.cc ('k') | services/surfaces/surfaces_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_utils.h"
6
7 #include <errno.h>
8 #include <sched.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11
12 #include "base/logging.h"
13 #include "base/posix/eintr_wrapper.h"
14 #include "base/process/launch.h"
15 #include "sandbox/linux/services/credentials.h"
16 #include "sandbox/linux/tests/unit_tests.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace sandbox {
20
21 namespace {
22
23 SANDBOX_TEST(NamespaceUtils, KernelSupportsUnprivilegedNamespace) {
24 const bool can_create_user_ns = Credentials::CanCreateProcessInNewUserNS();
25 const bool supports_user_ns =
26 NamespaceUtils::KernelSupportsUnprivilegedNamespace(CLONE_NEWUSER);
27 // can_create_user_ns implies supports_user_ns, but the converse is not
28 // necessarily true, as creating a user namespace can fail for various
29 // reasons.
30 if (can_create_user_ns) {
31 SANDBOX_ASSERT(supports_user_ns);
32 }
33 }
34
35 SANDBOX_TEST(NamespaceUtils, WriteToIdMapFile) {
36 if (!Credentials::CanCreateProcessInNewUserNS()) {
37 return;
38 }
39
40 const uid_t uid = getuid();
41 const gid_t gid = getgid();
42
43 const pid_t pid =
44 base::ForkWithFlags(CLONE_NEWUSER | SIGCHLD, nullptr, nullptr);
45 ASSERT_NE(-1, pid);
46 if (pid == 0) {
47 RAW_CHECK(getuid() != uid);
48 NamespaceUtils::WriteToIdMapFile("/proc/self/uid_map", uid);
49 RAW_CHECK(getuid() == uid);
50
51 RAW_CHECK(getgid() != gid);
52 NamespaceUtils::WriteToIdMapFile("/proc/self/gid_map", gid);
53 RAW_CHECK(getgid() == gid);
54
55 _exit(0);
56 }
57
58 int status = 42;
59 SANDBOX_ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0)));
60 SANDBOX_ASSERT_EQ(0, status);
61 }
62
63 } // namespace.
64
65 } // namespace sandbox.
OLDNEW
« no previous file with comments | « sandbox/linux/services/namespace_utils.cc ('k') | services/surfaces/surfaces_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698