• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SANDBOX_LINUX_SERVICES_NAMESPACE_UTILS_H_
6 #define SANDBOX_LINUX_SERVICES_NAMESPACE_UTILS_H_
7 
8 #include <sys/types.h>
9 
10 #include <type_traits>
11 
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "sandbox/sandbox_export.h"
15 
16 namespace sandbox {
17 
18 // Utility functions for using Linux namepaces.
19 class SANDBOX_EXPORT NamespaceUtils {
20  public:
21   static_assert(std::is_same<uid_t, gid_t>::value,
22                 "uid_t and gid_t must be the same type");
23   // generic_id_t can be used for either uid_t or gid_t.
24   typedef uid_t generic_id_t;
25 
26   // Write a uid or gid mapping from |id| to |id| in |map_file|. This function
27   // is async-signal-safe.
28   static bool WriteToIdMapFile(const char* map_file,
29                                generic_id_t id) WARN_UNUSED_RESULT;
30 
31   // Returns true if unprivileged namespaces of type |type| is supported
32   // (meaning that both CLONE_NEWUSER and type are are supported).  |type| must
33   // be one of CLONE_NEWIPC, CLONE_NEWNET, CLONE_NEWNS, CLONE_NEWPID,
34   // CLONE_NEWUSER, or CLONE_NEWUTS. This relies on access to /proc, so it will
35   // not work from within a sandbox.
36   static bool KernelSupportsUnprivilegedNamespace(int type);
37 
38   // Returns true if the kernel supports denying setgroups in a user namespace.
39   // On kernels where this is supported, DenySetgroups must be called before a
40   // gid mapping can be added.
41   static bool KernelSupportsDenySetgroups();
42 
43   // Disables setgroups() within the current user namespace. On Linux 3.18.2 and
44   // later, this is required in order to write to /proc/self/gid_map without
45   // having CAP_SETGID. Callers can determine whether is this needed with
46   // KernelSupportsDenySetgroups. This function is async-signal-safe.
47   static bool DenySetgroups() WARN_UNUSED_RESULT;
48 
49  private:
50   DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceUtils);
51 };
52 
53 }  // namespace sandbox
54 
55 #endif  // SANDBOX_LINUX_SERVICES_NAMESPACE_UTILS_H_
56