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

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

Issue 849893004: Move a couple of utility functions to a new namespace_utils class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename to SupportsUnprivilegedNamespace, which always checks for user namespace support Created 5 years, 11 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 2014 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 <fcntl.h>
8 #include <sched.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include <string>
14
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/files/scoped_file.h"
18 #include "base/logging.h"
19 #include "base/posix/eintr_wrapper.h"
20 #include "base/process/launch.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/third_party/valgrind/valgrind.h"
23
24 namespace sandbox {
25
26 namespace {
27 bool IsRunningOnValgrind() {
28 return RUNNING_ON_VALGRIND;
29 }
30 } // namespace
31
32 // Write a uid or gid mapping from |id| to |id| in |map_file|.
33 bool NamespaceUtils::WriteToIdMapFile(const char* map_file, generic_id_t id) {
jln (very slow on Chromium) 2015/01/23 02:48:23 Nit: add "// static" before the type.
rickyz (no longer on Chrome) 2015/01/23 23:59:37 Done.
34 base::ScopedFD fd(HANDLE_EINTR(open(map_file, O_WRONLY)));
35 if (!fd.is_valid()) {
36 return false;
37 }
38
39 const generic_id_t inside_id = id;
40 const generic_id_t outside_id = id;
41 const std::string mapping =
42 base::StringPrintf("%d %d 1\n", inside_id, outside_id);
43 const size_t len = mapping.size();
44 const ssize_t rc = HANDLE_EINTR(write(fd.get(), mapping.c_str(), len));
45 return rc == static_cast<ssize_t>(len);
46 }
47
48 bool NamespaceUtils::SupportsUnprivilegedNamespace(int type) {
jln (very slow on Chromium) 2015/01/23 02:48:23 Note: this works for kernel support, but it still
rickyz (no longer on Chrome) 2015/01/23 23:59:37 Yeah, that makes sense. CanCreateProcessInNewUserN
49 // Valgrind will let clone(2) pass-through, but doesn't support unshare(),
50 // so always consider namespaces unsupported there.
51 if (IsRunningOnValgrind()) {
52 return false;
53 }
54
55 // As of Linux 3.8, /proc/self/ns/* files exist for all namespace types. Since
56 // user namespaces were added in 3.8, it is OK to rely on the existence of
57 // /proc/self/ns/*.
58 if (!base::PathExists(base::FilePath("/proc/self/ns/user"))) {
59 return false;
60 }
61
62 const char* path;
63 switch (type) {
64 case CLONE_NEWUSER:
65 return true;
66 case CLONE_NEWIPC:
67 path = "/proc/self/ns/ipc";
68 break;
69 case CLONE_NEWNET:
70 path = "/proc/self/ns/net";
71 break;
72 case CLONE_NEWNS:
73 path = "/proc/self/ns/mnt";
74 break;
75 case CLONE_NEWPID:
76 path = "/proc/self/ns/pid";
77 break;
78 case CLONE_NEWUTS:
79 path = "/proc/self/ns/uts";
80 break;
81 default:
82 NOTREACHED();
83 return false;
84 }
85
86 return base::PathExists(base::FilePath(path));
87 }
88
89 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698