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