ioctl_ns(2)

๐Ÿ’– ioctl_ns โ€” ioctl() operations for Linux namespaces ๐Ÿ’–

okay so bestie, welcome to ioctl_ns(2), a page straight out of the System Calls Manual, and it's giving "Linux kernel internals but make it cute." this is the whole lore on the ioctl() calls that let you peek at namespaces โ€” you know, the /proc/pid/ns/* files โ€” and figure out who owns who, who's whose parent, what type they are, and all that family drama. ๐Ÿซถ


โœจ NAME โœจ

ioctl_ns โ€” ioctl() operations for Linux namespaces


๐ŸŒธ DESCRIPTION ๐ŸŒธ

๐Ÿ” Discovering namespace relationships

so there's a whole lil' squad of ioctl(2) operations that let you discover namespace relationships (peep user_namespaces(7) and pid_namespaces(7) for the backstory). the call shape is literally this:

new_fd = ioctl(fd, op);

in every single case, fd is pointing at a /proc/pid/ns/* file. and both of these operations hand you back a new file descriptor when they succeed. slay.

  • NS_GET_USERNS (since Linux 4.9) โ€” returns a file descriptor that refers to the owning user namespace for the namespace that fd refers to. ๐Ÿ’…
  • NS_GET_PARENT (since Linux 4.9) โ€” returns a file descriptor that refers to the parent namespace of the namespace that fd refers to. heads up though: this one is valid only for hierarchical namespaces (i.e., PID and user namespaces). for user namespaces, NS_GET_PARENT is synonymous with NS_GET_USERNS. literally twins.

the new file descriptor that comes back from these operations is opened with the O_RDONLY and O_CLOEXEC (close-on-exec; see fcntl(2)) flags.

if you hit the returned file descriptor with fstat(2), you get a stat structure whose st_dev (resident device) and st_ino (inode number) fields together identify the owning/parent namespace. you can match that inode number against the inode number of another /proc/pid/ns/{pid,user} file to figure out whether that's the owning/parent namespace. it's basically a vibe check for namespaces. โœจ

either of these ioctl(2) operations can fail with these errors:

  • EPERM โ€” the requested namespace is outside of the caller's namespace scope. this can happen if, for example, the owning user namespace is an ancestor of the caller's current user namespace. it can also happen when you try to get the parent of the initial user or PID namespace. ๐Ÿ˜ญ
  • ENOTTY โ€” the operation is not supported by this kernel version.

on top of that, the NS_GET_PARENT operation can fail with this one too:

  • EINVAL โ€” fd refers to a nonhierarchical namespace.

there's an example of all this in the EXAMPLES section, keep scrolling bestie. ๐Ÿฆ‹

๐Ÿ”ฎ Discovering the namespace type

the NS_GET_NSTYPE operation (available since Linux 4.11) is how you discover the type of namespace that the file descriptor fd refers to:

nstype = ioctl(fd, NS_GET_NSTYPE);

fd refers to a /proc/pid/ns/* file.

the return value is one of the CLONE_NEW* values that you can pass to clone(2) or unshare(2) in order to create a namespace. iconic.

๐Ÿ‘‘ Discovering the owner of a user namespace

the NS_GET_OWNER_UID operation (available since Linux 4.11) lets you discover the owner user ID of a user namespace โ€” that's the effective user ID of the process that created the user namespace. the call looks like this:

uid_t uid;
ioctl(fd, NS_GET_OWNER_UID, &uid);

fd refers to a /proc/pid/ns/user file.

the owner user ID comes back in the uid_t that the third argument points to. ๐ŸŽ€

this operation can fail with the following error:

  • EINVAL โ€” fd does not refer to a user namespace.

๐Ÿ’€ ERRORS ๐Ÿ’€

any of the ioctl() operations above can return this error:

  • ENOTTY โ€” fd does not refer to a /proc/pid/ns/* file.

๐ŸŒˆ STANDARDS ๐ŸŒˆ

Linux.


๐Ÿงš EXAMPLES ๐Ÿงš

the example below uses the ioctl(2) operations described above to do some simple discovery of namespace relationships. the shell sessions that follow show various examples of the use of this program.

trying to get the parent of the initial user namespace fails, since it has no parent:

$ ./ns_show /proc/self/ns/user p
The parent namespace is outside your namespace scope

create a process running sleep(1) that resides in new user and UTS namespaces, and show that the new UTS namespace is associated with the new user namespace:

$ unshare -Uu sleep 1000 &
[1] 23235
$ ./ns_show /proc/23235/ns/uts u
Device/Inode of owning user namespace is: [0,3] / 4026532448
$ readlink /proc/23235/ns/user
user:[4026532448]

then show that the parent of the new user namespace in the preceding example is the initial user namespace:

$ readlink /proc/self/ns/user
user:[4026531837]
$ ./ns_show /proc/23235/ns/user p
Device/Inode of parent namespace is: [0,3] / 4026531837

start a shell in a new user namespace, and show that from within this shell, the parent user namespace can't be discovered. similarly, the UTS namespace (which is associated with the initial user namespace) can't be discovered.

$ PS1="sh2$ " unshare -U bash
sh2$ ./ns_show /proc/self/ns/user p
The parent namespace is outside your namespace scope
sh2$ ./ns_show /proc/self/ns/uts u
The owning user namespace is outside your namespace scope

๐Ÿ“œ Program source

/* ns_show.c

   Licensed under the GNU General Public License v2 or later.
*/
#include <errno.h>
#include <fcntl.h>
#include <linux/nsfs.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    int          fd, userns_fd, parent_fd;
    struct stat  sb;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s /proc/[pid]/ns/[file] [p|u]\n",
                argv[0]);
        fprintf(stderr, "\nDisplay the result of one or both "
                "of NS_GET_USERNS (u) or NS_GET_PARENT (p)\n"
                "for the specified /proc/[pid]/ns/[file]. If neither "
                "'p' nor 'u' is specified,\n"
                "NS_GET_USERNS is the default.\n");
        exit(EXIT_FAILURE);
    }

    /* Obtain a file descriptor for the 'ns' file specified
       in argv[1]. */

    fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    /* Obtain a file descriptor for the owning user namespace and
       then obtain and display the inode number of that namespace. */

    if (argc < 3 || strchr(argv[2], 'u')) {
        userns_fd = ioctl(fd, NS_GET_USERNS);

        if (userns_fd == -1) {
            if (errno == EPERM)
                printf("The owning user namespace is outside "
                       "your namespace scope\n");
            else
               perror("ioctl-NS_GET_USERNS");
            exit(EXIT_FAILURE);
         }

        if (fstat(userns_fd, &sb) == -1) {
            perror("fstat-userns");
            exit(EXIT_FAILURE);
        }
        printf("Device/Inode of owning user namespace is: "
               "[%x,%x] / %ju\n",
               major(sb.st_dev),
               minor(sb.st_dev),
               (uintmax_t) sb.st_ino);

        close(userns_fd);
    }

    /* Obtain a file descriptor for the parent namespace and
       then obtain and display the inode number of that namespace. */

    if (argc > 2 && strchr(argv[2], 'p')) {
        parent_fd = ioctl(fd, NS_GET_PARENT);

        if (parent_fd == -1) {
            if (errno == EINVAL)
                printf("Can' get parent namespace of a "
                       "nonhierarchical namespace\n");
            else if (errno == EPERM)
                printf("The parent namespace is outside "
                       "your namespace scope\n");
            else
                perror("ioctl-NS_GET_PARENT");
            exit(EXIT_FAILURE);
        }

        if (fstat(parent_fd, &sb) == -1) {
            perror("fstat-parentns");
            exit(EXIT_FAILURE);
        }
        printf("Device/Inode of parent namespace is: [%x,%x] / %ju\n",
               major(sb.st_dev),
               minor(sb.st_dev),
               (uintmax_t) sb.st_ino);

        close(parent_fd);
    }

    exit(EXIT_SUCCESS);
}

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

fstat(2), ioctl(2), proc(5), namespaces(7)


that's a wrap bestie!! ๐Ÿ’œ this page is from Linux man-pages 6.7, dated 2024-03-03, so if you're reading it, that's the era you're in. go forth and interrogate some namespaces, no cap. ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from