{"id":3884,"date":"2022-12-20T17:28:20","date_gmt":"2022-12-20T20:28:20","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/user_namespaces-man7\/"},"modified":"2022-12-20T17:28:20","modified_gmt":"2022-12-20T20:28:20","slug":"user_namespaces-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/user_namespaces-man7\/","title":{"rendered":"USER_NAMESPACES (man7)"},"content":{"rendered":"<h1 align=\"center\">USER_NAMESPACES<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">user_namespaces \u2212 overview of Linux user namespaces<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an overview of namespaces, see <b>namespaces<\/b>(7).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">User namespaces isolate security-related identifiers and attributes, in particular, user IDs and group IDs (see <b>credentials<\/b>(7)), the root directory, keys (see <b>keyrings<\/b>(7)), and capabilities (see <b>capabilities<\/b>(7)). A process\u2019s user and group IDs can be different inside and outside a user namespace. In particular, a process can have a normal unprivileged user ID outside a user namespace while at the same time having a user ID of 0 inside the namespace; in other words, the process has full privileges for operations inside the user namespace, but is unprivileged for operations outside the namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Nested namespaces, namespace membership<\/b> <br \/> User namespaces can be nested; that is, each user namespace\u2014except the initial (&#8220;root&#8221;) namespace\u2014has a parent user namespace, and can have zero or more child user namespaces. The parent user namespace is the user namespace of the process that creates the user namespace via a call to <b>unshare<\/b>(2) or <b>clone<\/b>(2) with the <b>CLONE_NEWUSER<\/b> flag.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The kernel imposes (since version 3.11) a limit of 32 nested levels of user namespaces. Calls to <b>unshare<\/b>(2) or <b>clone<\/b>(2) that would cause this limit to be exceeded fail with the error <b>EUSERS<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Each process is a member of exactly one user namespace. A process created via <b>fork<\/b>(2) or <b>clone<\/b>(2) without the <b>CLONE_NEWUSER<\/b> flag is a member of the same user namespace as its parent. A single-threaded process can join another user namespace with <b>setns<\/b>(2) if it has the <b>CAP_SYS_ADMIN<\/b> in that namespace; upon doing so, it gains a full set of capabilities in that namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A call to <b>clone<\/b>(2) or <b>unshare<\/b>(2) with the <b>CLONE_NEWUSER<\/b> flag makes the new child process (for <b>clone<\/b>(2)) or the caller (for <b>unshare<\/b>(2)) a member of the new user namespace created by the call.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>NS_GET_PARENT ioctl<\/b>(2) operation can be used to discover the parental relationship between user namespaces; see <b>ioctl_ns<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Capabilities<\/b> <br \/> The child process created by <b>clone<\/b>(2) with the <b>CLONE_NEWUSER<\/b> flag starts out with a complete set of capabilities in the new user namespace. Likewise, a process that creates a new user namespace using <b>unshare<\/b>(2) or joins an existing user namespace using <b>setns<\/b>(2) gains a full set of capabilities in that namespace. On the other hand, that process has no capabilities in the parent (in the case of <b>clone<\/b>(2)) or previous (in the case of <b>unshare<\/b>(2) and <b>setns<\/b>(2)) user namespace, even if the new namespace is created or joined by the root user (i.e., a process with user ID 0 in the root namespace).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that a call to <b>execve<\/b>(2) will cause a process\u2019s capabilities to be recalculated in the usual way (see <b>capabilities<\/b>(7)). Consequently, unless the process has a user ID of 0 within the namespace, or the executable file has a nonempty inheritable capabilities mask, the process will lose all capabilities. See the discussion of user and group ID mappings, below.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A call to <b>clone<\/b>(2) or <b>unshare<\/b>(2) using the <b>CLONE_NEWUSER<\/b> flag or a call to <b>setns<\/b>(2) that moves the caller into another user namespace sets the &#8220;securebits&#8221; flags (see <b>capabilities<\/b>(7)) to their default values (all flags disabled) in the child (for <b>clone<\/b>(2)) or caller (for <b>unshare<\/b>(2) or <b>setns<\/b>(2)). Note that because the caller no longer has capabilities in its original user namespace after a call to <b>setns<\/b>(2), it is not possible for a process to reset its &#8220;securebits&#8221; flags while retaining its user namespace membership by using a pair of <b>setns<\/b>(2) calls to move to another user namespace and then return to its original user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The rules for determining whether or not a process has a capability in a particular user namespace are as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>1.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>A process has a capability inside a user namespace if it is a member of that namespace and it has the capability in its effective capability set. A process can gain capabilities in its effective capability set in various ways. For example, it may execute a set-user-ID program or an executable with associated file capabilities. In addition, a process may gain capabilities via the effect of <b>clone<\/b>(2), <b>unshare<\/b>(2), or <b>setns<\/b>(2), as already described.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>2.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>If a process has a capability in a user namespace, then it has that capability in all child (and further removed descendant) namespaces as well.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>3.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>When a user namespace is created, the kernel records the effective user ID of the creating process as being the &#8220;owner&#8221; of the namespace. A process that resides in the parent of the user namespace and whose effective user ID matches the owner of the namespace has all capabilities in the namespace. By virtue of the previous rule, this means that the process has all capabilities in all further removed descendant user namespaces as well. The <b>NS_GET_OWNER_UID ioctl<\/b>(2) operation can be used to discover the user ID of the owner of the namespace; see <b>ioctl_ns<\/b>(2).<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Effect of capabilities within a user namespace<\/b> <br \/> Having a capability inside a user namespace permits a process to perform operations (that require privilege) only on resources governed by that namespace. In other words, having a capability in a user namespace permits a process to perform privileged operations on resources that are governed by (nonuser) namespaces owned by (associated with) the user namespace (see the next subsection).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">On the other hand, there are many privileged operations that affect resources that are not associated with any namespace type, for example, changing the system (i.e., calendar) time (governed by <b>CAP_SYS_TIME<\/b>), loading a kernel module (governed by <b>CAP_SYS_MODULE<\/b>), and creating a device (governed by <b>CAP_MKNOD<\/b>). Only a process with privileges in the <i>initial<\/i> user namespace can perform such operations.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Holding <b>CAP_SYS_ADMIN<\/b> within the user namespace that owns a process\u2019s mount namespace allows that process to create bind mounts and mount the following types of filesystems:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p style=\"margin-top: 1em\"><i>\/proc<\/i> (since Linux 3.8)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p><i>\/sys<\/i> (since Linux 3.8)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p><i>devpts<\/i> (since Linux 3.9)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p><b>tmpfs<\/b>(5) (since Linux 3.9)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p><i>ramfs<\/i> (since Linux 3.9)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p><i>mqueue<\/i> (since Linux 3.9)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"40%\">\n<p><i>bpf<\/i> (since Linux 4.4)<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Holding <b>CAP_SYS_ADMIN<\/b> within the user namespace that owns a process\u2019s cgroup namespace allows (since Linux 4.6) that process to the mount the cgroup version 2 filesystem and cgroup version 1 named hierarchies (i.e., cgroup filesystems mounted with the <i>&#8220;none,name=&#8221;<\/i> option).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Holding <b>CAP_SYS_ADMIN<\/b> within the user namespace that owns a process\u2019s PID namespace allows (since Linux 3.8) that process to mount <i>\/proc<\/i> filesystems.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note however, that mounting block-based filesystems can be done only by a process that holds <b>CAP_SYS_ADMIN<\/b> in the initial user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Interaction of user namespaces and other types of namespaces<\/b> <br \/> Starting in Linux 3.8, unprivileged processes can create user namespaces, and the other types of namespaces can be created with just the <b>CAP_SYS_ADMIN<\/b> capability in the caller\u2019s user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When a nonuser namespace is created, it is owned by the user namespace in which the creating process was a member at the time of the creation of the namespace. Privileged operations on resources governed by the nonuser namespace require that the process has the necessary capabilities in the user namespace that owns the nonuser namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <b>CLONE_NEWUSER<\/b> is specified along with other <b>CLONE_NEW*<\/b> flags in a single <b>clone<\/b>(2) or <b>unshare<\/b>(2) call, the user namespace is guaranteed to be created first, giving the child (<b>clone<\/b>(2)) or caller (<b>unshare<\/b>(2)) privileges over the remaining namespaces created by the call. Thus, it is possible for an unprivileged caller to specify this combination of flags.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When a new namespace (other than a user namespace) is created via <b>clone<\/b>(2) or <b>unshare<\/b>(2), the kernel records the user namespace of the creating process as the owner of the new namespace. (This association can\u2019t be changed.) When a process in the new namespace subsequently performs privileged operations that operate on global resources isolated by the namespace, the permission checks are performed according to the process\u2019s capabilities in the user namespace that the kernel associated with the new namespace. For example, suppose that a process attempts to change the hostname (<b>sethostname<\/b>(2)), a resource governed by the UTS namespace. In this case, the kernel will determine which user namespace owns the process\u2019s UTS namespace, and check whether the process has the required capability (<b>CAP_SYS_ADMIN<\/b>) in that user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>NS_GET_USERNS ioctl<\/b>(2) operation can be used to discover the user namespace that owns a nonuser namespace; see <b>ioctl_ns<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>User and group ID mappings: uid_map and gid_map<\/b> <br \/> When a user namespace is created, it starts out without a mapping of user IDs (group IDs) to the parent user namespace. The <i>\/proc\/[pid]\/uid_map<\/i> and <i>\/proc\/[pid]\/gid_map<\/i> files (available since Linux 3.5) expose the mappings for user and group IDs inside the user namespace for the process <i>pid<\/i>. These files can be read to view the mappings in a user namespace and written to (once) to define the mappings.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The description in the following paragraphs explains the details for <i>uid_map<\/i>; <i>gid_map<\/i> is exactly the same, but each instance of &#8220;user ID&#8221; is replaced by &#8220;group ID&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>uid_map<\/i> file exposes the mapping of user IDs from the user namespace of the process <i>pid<\/i> to the user namespace of the process that opened <i>uid_map<\/i> (but see a qualification to this point below). In other words, processes that are in different user namespaces will potentially see different values when reading from a particular <i>uid_map<\/i> file, depending on the user ID mappings for the user namespaces of the reading processes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Each line in the <i>uid_map<\/i> file specifies a 1-to-1 mapping of a range of contiguous user IDs between two user namespaces. (When a user namespace is first created, this file is empty.) The specification in each line takes the form of three numbers delimited by white space. The first two numbers specify the starting user ID in each of the two user namespaces. The third number specifies the length of the mapped range. In detail, the fields are interpreted as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"4%\">\n<p style=\"margin-top: 1em\">(1)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">The start of the range of user IDs in the user namespace of the process <i>pid<\/i>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"4%\">\n<p>(2)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p>The start of the range of user IDs to which the user IDs specified by field one map. How field two is interpreted depends on whether the process that opened <i>uid_map<\/i> and the process <i>pid<\/i> are in the same user namespace, as follows:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%;\">a)<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\"><\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">If the two processes are in different user namespaces: field two is the start of a range of user IDs in the user namespace of the process that opened <i>uid_map<\/i>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p>b)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>If the two processes are in the same user namespace: field two is the start of the range of user IDs in the parent user namespace of the process <i>pid<\/i>. This case enables the opener of <i>uid_map<\/i> (the common case here is opening <i>\/proc\/self\/uid_map<\/i>) to see the mapping of user IDs into the user namespace of the process that created this user namespace.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p>(3)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">The length of the range of user IDs that is mapped between the two user namespaces.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">System calls that return user IDs (group IDs)\u2014for example, <b>getuid<\/b>(2), <b>getgid<\/b>(2), and the credential fields in the structure returned by <b>stat<\/b>(2)\u2014return the user ID (group ID) mapped into the caller\u2019s user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When a process accesses a file, its user and group IDs are mapped into the initial user namespace for the purpose of permission checking and assigning IDs when creating a file. When a process retrieves file user and group IDs via <b>stat<\/b>(2), the IDs are mapped in the opposite direction, to produce values relative to the process user and group ID mappings.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The initial user namespace has no parent namespace, but, for consistency, the kernel provides dummy user and group ID mapping files for this namespace. Looking at the <i>uid_map<\/i> file (<i>gid_map<\/i> is the same) from a shell in the initial namespace shows:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>cat \/proc\/$$\/uid_map<\/b> <br \/> 0 0 4294967295<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This mapping tells us that the range starting at user ID 0 in this namespace maps to a range starting at 0 in the (nonexistent) parent namespace, and the length of the range is the largest 32-bit unsigned integer. This leaves 4294967295 (the 32-bit signed \u22121 value) unmapped. This is deliberate: <i>(uid_t)\u00a0\u22121<\/i> is used in several interfaces (e.g., <b>setreuid<\/b>(2)) as a way to specify &#8220;no user ID&#8221;. Leaving <i>(uid_t)\u00a0\u22121<\/i> unmapped and unusable guarantees that there will be no confusion when using these interfaces.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Defining user and group ID mappings: writing to uid_map and gid_map<\/b> <br \/> After the creation of a new user namespace, the <i>uid_map<\/i> file of <i>one<\/i> of the processes in the namespace may be written to <i>once<\/i> to define the mapping of user IDs in the new user namespace. An attempt to write more than once to a <i>uid_map<\/i> file in a user namespace fails with the error <b>EPERM<\/b>. Similar rules apply for <i>gid_map<\/i> files.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The lines written to <i>uid_map<\/i> (<i>gid_map<\/i>) must conform to the following rules:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">The three fields must be valid numbers, and the last field must be greater than 0.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Lines are terminated by newline characters.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>There is a limit on the number of lines in the file. In Linux 4.14 and earlier, this limit was (arbitrarily) set at 5 lines. Since Linux 4.15, the limit is 340 lines. In addition, the number of bytes written to the file must be less than the system page size, and the write must be performed at the start of the file (i.e., <b>lseek<\/b>(2) and <b>pwrite<\/b>(2) can\u2019t be used to write to nonzero offsets in the file).<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>The range of user IDs (group IDs) specified in each line cannot overlap with the ranges in any other lines. In the initial implementation (Linux 3.8), this requirement was satisfied by a simplistic implementation that imposed the further requirement that the values in both field 1 and field 2 of successive lines must be in ascending numerical order, which prevented some otherwise valid maps from being created. Linux 3.9 and later fix this limitation, allowing any valid set of nonoverlapping maps.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>At least one line must be written to the file.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Writes that violate the above rules fail with the error <b>EINVAL<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In order for a process to write to the <i>\/proc\/[pid]\/uid_map<\/i> (<i>\/proc\/[pid]\/gid_map<\/i>) file, all of the following requirements must be met:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">1.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">The writing process must have the <b>CAP_SETUID<\/b> (<b>CAP_SETGID<\/b>) capability in the user namespace of the process <i>pid<\/i>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>2.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>The writing process must either be in the user namespace of the process <i>pid<\/i> or be in the parent user namespace of the process <i>pid<\/i>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>3.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>The mapped user IDs (group IDs) must in turn have a mapping in the parent user namespace.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>4.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>One of the following two cases applies:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:15%;\">*<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"20%\"><\/td>\n<td width=\"80%\">\n<p style=\"margin-top: 1em\"><i>Either<\/i> the writing process has the <b>CAP_SETUID<\/b> (<b>CAP_SETGID<\/b>) capability in the <i>parent<\/i> user namespace.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:20%;\">+<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"2%\"><\/td>\n<td width=\"3%\"><\/td>\n<td width=\"80%\">\n<p style=\"margin-top: 1em\">No further restrictions apply: the process can make mappings to arbitrary user IDs (group IDs) in the parent user namespace.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"80%\">\n<p><i>Or<\/i> otherwise all of the following restrictions apply:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:20%;\">+<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"20%\"><\/td>\n<td width=\"2%\"><\/td>\n<td width=\"3%\"><\/td>\n<td width=\"75%\">\n<p style=\"margin-top: 1em\">The data written to <i>uid_map<\/i> (<i>gid_map<\/i>) must consist of a single line that maps the writing process\u2019s effective user ID (group ID) in the parent user namespace to a user ID (group ID) in the user namespace.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"20%\"><\/td>\n<td width=\"2%\">\n<p>+<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"75%\">\n<p>The writing process must have the same effective user ID as the process that created the user namespace.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"20%\"><\/td>\n<td width=\"2%\">\n<p>+<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"75%\">\n<p>In the case of <i>gid_map<\/i>, use of the <b>setgroups<\/b>(2) system call must first be denied by writing &#8220;<i>deny<\/i>&#8221; to the <i>\/proc\/[pid]\/setgroups<\/i> file (see below) before writing to <i>gid_map<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Writes that violate the above rules fail with the error <b>EPERM<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Interaction with system calls that change process UIDs or GIDs<\/b> <br \/> In a user namespace where the <i>uid_map<\/i> file has not been written, the system calls that change user IDs will fail. Similarly, if the <i>gid_map<\/i> file has not been written, the system calls that change group IDs will fail. After the <i>uid_map<\/i> and <i>gid_map<\/i> files have been written, only the mapped values may be used in system calls that change user and group IDs.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For user IDs, the relevant system calls include <b>setuid<\/b>(2), <b>setfsuid<\/b>(2), <b>setreuid<\/b>(2), and <b>setresuid<\/b>(2). For group IDs, the relevant system calls include <b>setgid<\/b>(2), <b>setfsgid<\/b>(2), <b>setregid<\/b>(2), <b>setresgid<\/b>(2), and <b>setgroups<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Writing &#8220;<i>deny<\/i>&#8221; to the <i>\/proc\/[pid]\/setgroups<\/i> file before writing to <i>\/proc\/[pid]\/gid_map<\/i> will permanently disable <b>setgroups<\/b>(2) in a user namespace and allow writing to <i>\/proc\/[pid]\/gid_map<\/i> without having the <b>CAP_SETGID<\/b> capability in the parent user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>The \/proc\/[pid]\/setgroups file<\/b> <br \/> The <i>\/proc\/[pid]\/setgroups<\/i> file displays the string &#8220;<i>allow<\/i>&#8221; if processes in the user namespace that contains the process <i>pid<\/i> are permitted to employ the <b>setgroups<\/b>(2) system call; it displays &#8220;<i>deny<\/i>&#8221; if <b>setgroups<\/b>(2) is not permitted in that user namespace. Note that regardless of the value in the <i>\/proc\/[pid]\/setgroups<\/i> file (and regardless of the process\u2019s capabilities), calls to <b>setgroups<\/b>(2) are also not permitted if <i>\/proc\/[pid]\/gid_map<\/i> has not yet been set.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A privileged process (one with the <b>CAP_SYS_ADMIN<\/b> capability in the namespace) may write either of the strings &#8220;<i>allow<\/i>&#8221; or &#8220;<i>deny<\/i>&#8221; to this file <i>before<\/i> writing a group ID mapping for this user namespace to the file <i>\/proc\/[pid]\/gid_map<\/i>. Writing the string &#8220;<i>deny<\/i>&#8221; prevents any process in the user namespace from employing <b>setgroups<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The essence of the restrictions described in the preceding paragraph is that it is permitted to write to <i>\/proc\/[pid]\/setgroups<\/i> only so long as calling <b>setgroups<\/b>(2) is disallowed because <i>\/proc\/[pid]\/gid_map<\/i> has not been set. This ensures that a process cannot transition from a state where <b>setgroups<\/b>(2) is allowed to a state where <b>setgroups<\/b>(2) is denied; a process can transition only from <b>setgroups<\/b>(2) being disallowed to <b>setgroups<\/b>(2) being allowed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The default value of this file in the initial user namespace is &#8220;<i>allow<\/i>&#8220;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Once <i>\/proc\/[pid]\/gid_map<\/i> has been written to (which has the effect of enabling <b>setgroups<\/b>(2) in the user namespace), it is no longer possible to disallow <b>setgroups<\/b>(2) by writing &#8220;<i>deny<\/i>&#8221; to <i>\/proc\/[pid]\/setgroups<\/i> (the write fails with the error <b>EPERM<\/b>).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A child user namespace inherits the <i>\/proc\/[pid]\/setgroups<\/i> setting from its parent.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the <i>setgroups<\/i> file has the value &#8220;<i>deny<\/i>&#8220;, then the <b>setgroups<\/b>(2) system call can\u2019t subsequently be reenabled (by writing &#8220;<i>allow<\/i>&#8221; to the file) in this user namespace. (Attempts to do so fail with the error <b>EPERM<\/b>.) This restriction also propagates down to all child user namespaces of this user namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>\/proc\/[pid]\/setgroups<\/i> file was added in Linux 3.19, but was backported to many earlier stable kernel series, because it addresses a security issue. The issue concerned files with permissions such as &#8220;rwx\u2212\u2212\u2212rwx&#8221;. Such files give fewer permissions to &#8220;group&#8221; than they do to &#8220;other&#8221;. This means that dropping groups using <b>setgroups<\/b>(2) might allow a process file access that it did not formerly have. Before the existence of user namespaces this was not a concern, since only a privileged process (one with the <b>CAP_SETGID<\/b> capability) could call <b>setgroups<\/b>(2). However, with the introduction of user namespaces, it became possible for an unprivileged process to create a new namespace in which the user had all privileges. This then allowed formerly unprivileged users to drop groups and thus gain file access that they did not previously have. The <i>\/proc\/[pid]\/setgroups<\/i> file was added to address this security issue, by denying any pathway for an unprivileged process to drop groups with <b>setgroups<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Unmapped user and group IDs<\/b> <br \/> There are various places where an unmapped user ID (group ID) may be exposed to user space. For example, the first process in a new user namespace may call <b>getuid<\/b>(2) before a user ID mapping has been defined for the namespace. In most such cases, an unmapped user ID is converted to the overflow user ID (group ID); the default value for the overflow user ID (group ID) is 65534. See the descriptions of <i>\/proc\/sys\/kernel\/overflowuid<\/i> and <i>\/proc\/sys\/kernel\/overflowgid<\/i> in <b>proc<\/b>(5).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The cases where unmapped IDs are mapped in this fashion include system calls that return user IDs (<b>getuid<\/b>(2), <b>getgid<\/b>(2), and similar), credentials passed over a UNIX domain socket, credentials returned by <b>stat<\/b>(2), <b>waitid<\/b>(2), and the System V IPC &#8220;ctl&#8221; <b>IPC_STAT<\/b> operations, credentials exposed by <i>\/proc\/[pid]\/status<\/i> and the files in <i>\/proc\/sysvipc\/*<\/i>, credentials returned via the <i>si_uid<\/i> field in the <i>siginfo_t<\/i> received with a signal (see <b>sigaction<\/b>(2)), credentials written to the process accounting file (see <b>acct<\/b>(5)), and credentials returned with POSIX message queue notifications (see <b>mq_notify<\/b>(3)).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There is one notable case where unmapped user and group IDs are <i>not<\/i> converted to the corresponding overflow ID value. When viewing a <i>uid_map<\/i> or <i>gid_map<\/i> file in which there is no mapping for the second field, that field is displayed as 4294967295 (\u22121 as an unsigned integer).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Accessing files<\/b> <br \/> In order to determine permissions when an unprivileged process accesses a file, the process credentials (UID, GID) and the file credentials are in effect mapped back to what they would be in the initial user namespace and then compared to determine the permissions that the process has on the file. The same is also of other objects that employ the credentials plus permissions mask accessibility model, such as System V IPC objects<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Operation of file-related capabilities<\/b> <br \/> Certain capabilities allow a process to bypass various kernel-enforced restrictions when performing operations on files owned by other users or groups. These capabilities are: <b>CAP_CHOWN<\/b>, <b>CAP_DAC_OVERRIDE<\/b>, <b>CAP_DAC_READ_SEARCH<\/b>, <b>CAP_FOWNER<\/b>, and <b>CAP_FSETID<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Within a user namespace, these capabilities allow a process to bypass the rules if the process has the relevant capability over the file, meaning that:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">the process has the relevant effective capability in its user namespace; and<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>the file\u2019s user ID and group ID both have valid mappings in the user namespace.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>CAP_FOWNER<\/b> capability is treated somewhat exceptionally: it allows a process to bypass the corresponding rules so long as at least the file\u2019s user ID has a mapping in the user namespace (i.e., the file\u2019s group ID does not need to have a valid mapping).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Set-user-ID and set-group-ID programs<\/b> <br \/> When a process inside a user namespace executes a set-user-ID (set-group-ID) program, the process\u2019s effective user (group) ID inside the namespace is changed to whatever value is mapped for the user (group) ID of the file. However, if either the user <i>or<\/i> the group ID of the file has no mapping inside the namespace, the set-user-ID (set-group-ID) bit is silently ignored: the new program is executed, but the process\u2019s effective user (group) ID is left unchanged. (This mirrors the semantics of executing a set-user-ID or set-group-ID program that resides on a filesystem that was mounted with the <b>MS_NOSUID<\/b> flag, as described in <b>mount<\/b>(2).)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Miscellaneous<\/b> <br \/> When a process\u2019s user and group IDs are passed over a UNIX domain socket to a process in a different user namespace (see the description of <b>SCM_CREDENTIALS<\/b> in <b>unix<\/b>(7)), they are translated into the corresponding values as per the receiving process\u2019s user and group ID mappings.<\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Namespaces are a Linux-specific feature.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Over the years, there have been a lot of features that have been added to the Linux kernel that have been made available only to privileged users because of their potential to confuse set-user-ID-root applications. In general, it becomes safe to allow the root user in a user namespace to use those features because it is impossible, while in a user namespace, to gain more privilege than the root user of a user namespace has.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Availability<\/b> <br \/> Use of user namespaces requires a kernel that is configured with the <b>CONFIG_USER_NS<\/b> option. User namespaces require support in a range of subsystems across the kernel. When an unsupported subsystem is configured into the kernel, it is not possible to configure user namespaces support.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As at Linux 3.8, most relevant subsystems supported user namespaces, but a number of filesystems did not have the infrastructure needed to map user and group IDs between user namespaces. Linux 3.9 added the required infrastructure support for many of the remaining unsupported filesystems (Plan 9 (9P), Andrew File System (AFS), Ceph, CIFS, CODA, NFS, and OCFS2). Linux 3.12 added support for the last of the unsupported major filesystems, XFS.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The program below is designed to allow experimenting with user namespaces, as well as other types of namespaces. It creates namespaces as specified by command-line options and then executes a command inside those namespaces. The comments and <i>usage()<\/i> function inside the program provide a full explanation of the program. The following shell session demonstrates its use.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">First, we look at the run-time environment:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>uname \u2212rs<\/b> # Need Linux 3.8 or later <br \/> Linux 3.8.0 <br \/> $ <b>id \u2212u<\/b> # Running as unprivileged user <br \/> 1000 <br \/> $ <b>id \u2212g<\/b> <br \/> 1000<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Now start a new shell in new user (<i>\u2212U<\/i>), mount (<i>\u2212m<\/i>), and PID (<i>\u2212p<\/i>) namespaces, with user ID (<i>\u2212M<\/i>) and group ID (<i>\u2212G<\/i>) 1000 mapped to 0 inside the user namespace:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>.\/userns_child_exec \u2212p \u2212m \u2212U \u2212M &#8216;0 1000 1&#8217; \u2212G &#8216;0 1000 1&#8217; bash<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The shell has PID 1, because it is the first process in the new PID namespace:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">bash$ <b>echo $$<\/b> <br \/> 1<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Mounting a new <i>\/proc<\/i> filesystem and listing all of the processes visible in the new PID namespace shows that the shell can\u2019t see any processes outside the PID namespace:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">bash$ <b>mount \u2212t proc proc \/proc<\/b> <br \/> bash$ <b>ps ax<\/b> <br \/> PID TTY STAT TIME COMMAND <br \/> 1 pts\/3 S 0:00 bash <br \/> 22 pts\/3 R+ 0:00 ps ax<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Inside the user namespace, the shell has user and group ID 0, and a full set of permitted and effective capabilities:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">bash$ <b>cat \/proc\/$$\/status | egrep &#8216;^[UG]id&#8217;<\/b><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\">\n<p>Uid:<\/p>\n<\/td>\n<td width=\"8%\">\n<p>0<\/p>\n<\/td>\n<td width=\"7%\">\n<p>0<\/p>\n<\/td>\n<td width=\"62%\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\">\n<p>Gid:<\/p>\n<\/td>\n<td width=\"8%\">\n<p>0<\/p>\n<\/td>\n<td width=\"7%\">\n<p>0<\/p>\n<\/td>\n<td width=\"62%\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%;\">bash$ <b>cat \/proc\/$$\/status | egrep &#8216;^Cap(Prm|Inh|Eff)&#8217;<\/b><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\">\n<p>CapInh:<\/p>\n<\/td>\n<td width=\"77%\">\n<p>0000000000000000<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\">\n<p>CapPrm:<\/p>\n<\/td>\n<td width=\"77%\">\n<p>0000001fffffffff<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\">\n<p>CapEff:<\/p>\n<\/td>\n<td width=\"77%\">\n<p>0000001fffffffff<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source<\/b> <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var userns_child_exec.c<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Licensed under GNU General Public License v2 or later<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Create a child process that executes a shell command in new <br \/> namespace(s); allow UID and GID mappings to be specified when <br \/> creating a user namespace. <br \/> bodies\/ usr\/ <br \/> #define _GNU_SOURCE <br \/> #include <sched.h> <br \/> #include <unistd.h> <br \/> #include <stdint.h> <br \/> #include <stdlib.h> <br \/> #include <sys\/wait.h> <br \/> #include <signal.h> <br \/> #include <fcntl.h> <br \/> #include <stdio.h> <br \/> #include <string.h> <br \/> #include <limits.h> #include <errno.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* A simple error\u2212handling function: print an error message based <br \/> on the value in &#8216;errno&#8217; and terminate the calling process *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE);  <br \/> } while (0)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">struct child_args { <br \/> char **argv; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Command to be executed by child, with args bodies\/ usr\/ <br \/> int pipe_fd[2]; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Pipe used to synchronize parent and child bodies\/ usr\/ <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static int verbose;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static void <br \/> usage(char *pname) <br \/> { <br \/> fprintf(stderr, &#8220;Usage: %s [options] cmd [arg&#8230;]nn&#8221;, pname); <br \/> fprintf(stderr, &#8220;Create a child process that executes a shell &#8221; <br \/> &#8220;command in a new user namespace,n&#8221; <br \/> &#8220;and possibly also other new namespace(s).nn&#8221;); <br \/> fprintf(stderr, &#8220;Options can be:nn&#8221;); <br \/> #define fpe(str) fprintf(stderr, &#8221; %s&#8221;, str); <br \/> fpe(&#8220;\u2212i New IPC namespacen&#8221;); <br \/> fpe(&#8220;\u2212m New mount namespacen&#8221;); <br \/> fpe(&#8220;\u2212n New network namespacen&#8221;); <br \/> fpe(&#8220;\u2212p New PID namespacen&#8221;); <br \/> fpe(&#8220;\u2212u New UTS namespacen&#8221;); <br \/> fpe(&#8220;\u2212U New user namespacen&#8221;); <br \/> fpe(&#8220;\u2212M uid_map Specify UID map for user namespacen&#8221;); <br \/> fpe(&#8220;\u2212G gid_map Specify GID map for user namespacen&#8221;); <br \/> fpe(&#8220;\u2212z Map user&#8217;s UID and GID to 0 in user namespacen&#8221;); <br \/> fpe(&#8221; (equivalent to: \u2212M &#8216;0 <uid> 1&#8242; \u2212G &#8216;0 <gid> 1&#8242;)n&#8221;); <br \/> fpe(&#8220;\u2212v Display verbose messagesn&#8221;); <br \/> fpe(&#8220;n&#8221;); <br \/> fpe(&#8220;If \u2212z, \u2212M, or \u2212G is specified, \u2212U is required.n&#8221;); <br \/> fpe(&#8220;It is not permitted to specify both \u2212z and either \u2212M or \u2212G.n&#8221;); <br \/> fpe(&#8220;n&#8221;); <br \/> fpe(&#8220;Map strings for \u2212M and \u2212G consist of records of the form:n&#8221;); <br \/> fpe(&#8220;n&#8221;); <br \/> fpe(&#8221; ID\u2212inside\u2212ns ID\u2212outside\u2212ns lenn&#8221;); <br \/> fpe(&#8220;n&#8221;); <br \/> fpe(&#8220;A map string can contain multiple records, separated&#8221; <br \/> &#8221; by commas;n&#8221;); <br \/> fpe(&#8220;the commas are replaced by newlines before writing&#8221; <br \/> &#8221; to map files.n&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Update the mapping file &#8216;map_file&#8217;, with the value provided in <br \/> &#8216;mapping&#8217;, a string that defines a UID or GID mapping. A UID or <br \/> GID mapping consists of one or more newline\u2212delimited records <br \/> of the form:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ID_inside\u2212ns ID\u2212outside\u2212ns length<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Requiring the user to supply a string that contains newlines is <br \/> of course inconvenient for command\u2212line use. Thus, we permit the <br \/> use of commas to delimit records in this string, and replace them <br \/> with newlines before writing the string to the file. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static void <br \/> update_map(char *mapping, char *map_file) <br \/> { <br \/> int fd; <br \/> size_t map_len; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Length of &#8216;mapping&#8217; *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Replace commas in mapping string with newlines *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">map_len = strlen(mapping); <br \/> for (int j = 0; j < map_len; j++) <br \/> if (mapping[j] == &#8216;,&#8217;) <br \/> mapping[j] = &#8216;n&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fd = open(map_file, O_RDWR); <br \/> if (fd == \u22121) { <br \/> fprintf(stderr, &#8220;ERROR: open %s: %sn&#8221;, map_file, <br \/> strerror(errno)); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (write(fd, mapping, map_len) != map_len) { <br \/> fprintf(stderr, &#8220;ERROR: write %s: %sn&#8221;, map_file, <br \/> strerror(errno)); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(fd); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Linux 3.19 made a change in the handling of setgroups(2) and the <br \/> &#8216;gid_map&#8217; file to address a security issue. The issue allowed <br \/> *unprivileged* users to employ user namespaces in order to drop <br \/> The upshot of the 3.19 changes is that in order to update the <br \/> &#8216;gid_maps&#8217; file, use of the setgroups() system call in this <br \/> user namespace must first be disabled by writing &#8220;deny&#8221; to one of <br \/> the \/proc\/PID\/setgroups files for this namespace. That is the <br \/> purpose of the following function. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static void <br \/> proc_setgroups_write(pid_t child_pid, char *str) <br \/> { <br \/> char setgroups_path[PATH_MAX]; <br \/> int fd;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">snprintf(setgroups_path, PATH_MAX, &#8220;\/proc\/%jd\/setgroups&#8221;, <br \/> (intmax_t) child_pid);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fd = open(setgroups_path, O_RDWR); <br \/> if (fd == \u22121) {<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* We may be on a system that doesn&#8217;t support <br \/> \/proc\/PID\/setgroups. In that case, the file won&#8217;t exist, <br \/> and the system won&#8217;t impose the restrictions that Linux 3.19 <br \/> added. That&#8217;s fine: we don&#8217;t need to do anything in order <br \/> to permit &#8216;gid_map&#8217; to be updated.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">However, if the error from open() was something other than <br \/> the ENOENT error that is expected for that case, let the <br \/> user know. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (errno != ENOENT) <br \/> fprintf(stderr, &#8220;ERROR: open %s: %sn&#8221;, setgroups_path, <br \/> strerror(errno)); <br \/> return; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (write(fd, str, strlen(str)) == \u22121) <br \/> fprintf(stderr, &#8220;ERROR: write %s: %sn&#8221;, setgroups_path, <br \/> strerror(errno));<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(fd); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static int \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Start function for cloned child bodies\/ usr\/ <br \/> childFunc(void *arg) <br \/> { <br \/> struct child_args *args = arg; <br \/> char ch;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Wait until the parent has updated the UID and GID mappings. <br \/> See the comment in main(). We wait for end of file on a <br \/> pipe that will be closed by the parent process once it has <br \/> updated the mappings. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(args\u2212>pipe_fd[1]); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Close our descriptor for the write <br \/> end of the pipe so that we see EOF <br \/> when parent closes its descriptor bodies\/ usr\/ <br \/> if (read(args\u2212>pipe_fd[0], &#038;ch, 1) != 0) { <br \/> fprintf(stderr, <br \/> &#8220;Failure in child: read from pipe returned != 0n&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(args\u2212>pipe_fd[0]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Execute a shell command *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;About to exec %sn&#8221;, args\u2212>argv[0]); <br \/> execvp(args\u2212>argv[0], args\u2212>argv); <br \/> errExit(&#8220;execvp&#8221;); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define STACK_SIZE (1024 bodies manpages.csv script_extrae_body.sh script.sh usr 1024)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static char child_stack[STACK_SIZE]; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Space for child&#8217;s stack *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> int flags, opt, map_zero; <br \/> pid_t child_pid; <br \/> struct child_args args; <br \/> char *uid_map, *gid_map; <br \/> const int MAP_BUF_SIZE = 100; <br \/> char map_buf[MAP_BUF_SIZE]; <br \/> char map_path[PATH_MAX];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Parse command\u2212line options. The initial &#8216;+&#8217; character in <br \/> the final getopt() argument prevents GNU\u2212style permutation <br \/> of command\u2212line options. That&#8217;s useful, since sometimes <br \/> the &#8216;command&#8217; to be executed by this program itself <br \/> has command\u2212line options. We don&#8217;t want getopt() to treat <br \/> those as options to this program. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">flags = 0; <br \/> verbose = 0; <br \/> gid_map = NULL; <br \/> uid_map = NULL; <br \/> map_zero = 0; <br \/> while ((opt = getopt(argc, argv, &#8220;+imnpuUM:G:zv&#8221;)) != \u22121) { <br \/> switch (opt) { <br \/> case &#8216;i&#8217;: flags |= CLONE_NEWIPC; break; <br \/> case &#8216;m&#8217;: flags |= CLONE_NEWNS; break; <br \/> case &#8216;n&#8217;: flags |= CLONE_NEWNET; break; <br \/> case &#8216;p&#8217;: flags |= CLONE_NEWPID; break; <br \/> case &#8216;u&#8217;: flags |= CLONE_NEWUTS; break; <br \/> case &#8216;v&#8217;: verbose = 1; break; <br \/> case &#8216;z&#8217;: map_zero = 1; break; <br \/> case &#8216;M&#8217;: uid_map = optarg; break; <br \/> case &#8216;G&#8217;: gid_map = optarg; break; <br \/> case &#8216;U&#8217;: flags |= CLONE_NEWUSER; break; <br \/> default: usage(argv[0]); <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* \u2212M or \u2212G without \u2212U is nonsensical *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (((uid_map != NULL || gid_map != NULL || map_zero) &#038;&#038; <br \/> !(flags &#038; CLONE_NEWUSER)) || <br \/> (map_zero &#038;&#038; (uid_map != NULL || gid_map != NULL))) <br \/> usage(argv[0]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">args.argv = &#038;argv[optind];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* We use a pipe to synchronize the parent and child, in order to <br \/> ensure that the parent sets the UID and GID maps before the child <br \/> calls execve(). This ensures that the child maintains its <br \/> capabilities during the execve() in the common case where we <br \/> want to map the child&#8217;s effective user ID to 0 in the new user <br \/> namespace. Without this synchronization, the child would lose <br \/> its capabilities if it performed an execve() with nonzero <br \/> user IDs (see the capabilities(7) man page for details of the <br \/> transformation of a process&#8217;s capabilities during execve()). *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (pipe(args.pipe_fd) == \u22121) <br \/> errExit(&#8220;pipe&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Create the child in new namespace(s) *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">child_pid = clone(childFunc, child_stack + STACK_SIZE, <br \/> flags | SIGCHLD, &#038;args); <br \/> if (child_pid == \u22121) <br \/> errExit(&#8220;clone&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Parent falls through to here *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (verbose) <br \/> printf(&#8220;%s: PID of child created by clone() is %jdn&#8221;, <br \/> argv[0], (intmax_t) child_pid);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Update the UID and GID maps in the child *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (uid_map != NULL || map_zero) { <br \/> snprintf(map_path, PATH_MAX, &#8220;\/proc\/%jd\/uid_map&#8221;, <br \/> (intmax_t) child_pid); <br \/> if (map_zero) { <br \/> snprintf(map_buf, MAP_BUF_SIZE, &#8220;0 %jd 1&#8221;, <br \/> (intmax_t) getuid()); <br \/> uid_map = map_buf; <br \/> } <br \/> update_map(uid_map, map_path); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (gid_map != NULL || map_zero) { <br \/> proc_setgroups_write(child_pid, &#8220;deny&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">snprintf(map_path, PATH_MAX, &#8220;\/proc\/%jd\/gid_map&#8221;, <br \/> (intmax_t) child_pid); <br \/> if (map_zero) { <br \/> snprintf(map_buf, MAP_BUF_SIZE, &#8220;0 %ld 1&#8221;, <br \/> (intmax_t) getgid()); <br \/> gid_map = map_buf; <br \/> } <br \/> update_map(gid_map, map_path); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Close the write end of the pipe, to signal to the child that we <br \/> have updated the UID and GID maps *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(args.pipe_fd[1]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (waitpid(child_pid, NULL, 0) == \u22121) \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Wait for child bodies\/ usr\/ <br \/> errExit(&#8220;waitpid&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (verbose) <br \/> printf(&#8220;%s: terminatingn&#8221;, argv[0]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>newgidmap<\/b>(1), <b>newuidmap<\/b>(1), <b>clone<\/b>(2), <b>ptrace<\/b>(2), <b>setns<\/b>(2), <b>unshare<\/b>(2), <b>proc<\/b>(5), <b>subgid<\/b>(5), <b>subuid<\/b>(5), <b>capabilities<\/b>(7), <b>cgroup_namespaces<\/b>(7), <b>credentials<\/b>(7), <b>namespaces<\/b>(7), <b>pid_namespaces<\/b>(7)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The kernel source file <i>Documentation\/namespaces\/resource-control.txt<\/i>.<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  user_namespaces \u2212 overview of Linux user namespaces <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,979],"class_list":["post-3884","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-user_namespaces"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=3884"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3884\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=3884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=3884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=3884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}