{
    "content": [
        {
            "type": "text",
            "text": "# CGROUP_NAMESPACES (man)\n\n## NAME\n\ncgroupnamespaces - overview of Linux cgroup namespaces\n\n## DESCRIPTION\n\nFor an overview of namespaces, see namespaces(7).\n\n## Sections\n\n- **NAME**\n- **DESCRIPTION**\n- **CONFORMING TO**\n- **NOTES**\n- **SEE ALSO**\n- **COLOPHON**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "CGROUP_NAMESPACES",
        "section": "",
        "mode": "man",
        "summary": "cgroupnamespaces - overview of Linux cgroup namespaces",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [
            {
                "name": "unshare",
                "section": "1",
                "url": "https://www.chedong.com/phpMan.php/man/unshare/1/json"
            },
            {
                "name": "clone",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/clone/2/json"
            },
            {
                "name": "setns",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/setns/2/json"
            },
            {
                "name": "unshare",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/unshare/2/json"
            },
            {
                "name": "proc",
                "section": "5",
                "url": "https://www.chedong.com/phpMan.php/man/proc/5/json"
            },
            {
                "name": "cgroups",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/cgroups/7/json"
            },
            {
                "name": "credentials",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/credentials/7/json"
            },
            {
                "name": "spaces",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/spaces/7/json"
            },
            {
                "name": "usernamespaces",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/usernamespaces/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 83,
                "subsections": []
            },
            {
                "name": "CONFORMING TO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 35,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "COLOPHON",
                "lines": 7,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "cgroupnamespaces - overview of Linux cgroup namespaces\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "For an overview of namespaces, see namespaces(7).\n\nCgroup  namespaces  virtualize  the  view of a process's cgroups (see cgroups(7)) as seen via\n/proc/[pid]/cgroup and /proc/[pid]/mountinfo.\n\nEach cgroup namespace has its own set of cgroup root directories.  These root directories are\nthe  base  points  for  the  relative locations displayed in the corresponding records in the\n/proc/[pid]/cgroup file.  When a process creates a new cgroup namespace using clone(2) or un‐‐\nshare(2)  with  the  CLONENEWCGROUP  flag, its current cgroups directories become the cgroup\nroot directories of the new namespace.  (This applies both for the cgroups version 1  hierar‐\nchies and the cgroups version 2 unified hierarchy.)\n\nWhen  reading the cgroup memberships of a \"target\" process from /proc/[pid]/cgroup, the path‐\nname shown in the third field of each record will be relative to the reading  process's  root\ndirectory  for  the  corresponding  cgroup  hierarchy.  If the cgroup directory of the target\nprocess lies outside the root directory of the reading process's cgroup namespace,  then  the\npathname will show ../ entries for each ancestor level in the cgroup hierarchy.\n\nThe following shell session demonstrates the effect of creating a new cgroup namespace.\n\nFirst, (as superuser) in a shell in the initial cgroup namespace, we create a child cgroup in\nthe freezer hierarchy, and place a process in that cgroup that we will use  as  part  of  the\ndemonstration below:\n\n# mkdir -p /sys/fs/cgroup/freezer/sub2\n# sleep 10000 &     # Create a process that lives for a while\n[1] 20124\n# echo 20124 > /sys/fs/cgroup/freezer/sub2/cgroup.procs\n\nWe  then  create  another  child  cgroup in the freezer hierarchy and put the shell into that\ncgroup:\n\n# mkdir -p /sys/fs/cgroup/freezer/sub\n# echo $$                      # Show PID of this shell\n30655\n# echo 30655 > /sys/fs/cgroup/freezer/sub/cgroup.procs\n# cat /proc/self/cgroup | grep freezer\n7:freezer:/sub\n\nNext, we use unshare(1) to create a process running a new shell in new cgroup and mount name‐\nspaces:\n\n# PS1=\"sh2# \" unshare -Cm bash\n\nFrom  the  new  shell started by unshare(1), we then inspect the /proc/[pid]/cgroup files of,\nrespectively, the new shell, a process that is in the initial cgroup  namespace  (init,  with\nPID 1), and the process in the sibling cgroup (sub2):\n\nsh2# cat /proc/self/cgroup | grep freezer\n7:freezer:/\nsh2# cat /proc/1/cgroup | grep freezer\n7:freezer:/..\nsh2# cat /proc/20124/cgroup | grep freezer\n7:freezer:/../sub2\n\nFrom  the  output  of the first command, we see that the freezer cgroup membership of the new\nshell (which is in the same cgroup as the initial shell) is shown  defined  relative  to  the\nfreezer cgroup root directory that was established when the new cgroup namespace was created.\n(In absolute terms, the new shell is in the /sub freezer cgroup, and the  root  directory  of\nthe freezer cgroup hierarchy in the new cgroup namespace is also /sub.  Thus, the new shell's\ncgroup membership is displayed as '/'.)\n\nHowever, when we look in /proc/self/mountinfo we see the following anomaly:\n\nsh2# cat /proc/self/mountinfo | grep freezer\n155 145 0:32 /.. /sys/fs/cgroup/freezer ...\n\nThe fourth field of this line (/..)  should show the directory in the cgroup filesystem which\nforms  the  root  of this mount.  Since by the definition of cgroup namespaces, the process's\ncurrent freezer cgroup directory became its root freezer cgroup directory, we should see  '/'\nin  this field.  The problem here is that we are seeing a mount entry for the cgroup filesys‐\ntem corresponding to the initial cgroup namespace (whose cgroup filesystem is  indeed  rooted\nat  the  parent  directory  of sub).  To fix this problem, we must remount the freezer cgroup\nfilesystem from the new shell (i.e., perform the mount from a process  that  is  in  the  new\ncgroup namespace), after which we see the expected results:\n\nsh2# mount --make-rslave /     # Don't propagate mount events\n# to other namespaces\nsh2# umount /sys/fs/cgroup/freezer\nsh2# mount -t cgroup -o freezer freezer /sys/fs/cgroup/freezer\nsh2# cat /proc/self/mountinfo | grep freezer\n155 145 0:32 / /sys/fs/cgroup/freezer rw,relatime ...\n",
                "subsections": []
            },
            "CONFORMING TO": {
                "content": "Namespaces are a Linux-specific feature.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "Use of cgroup namespaces requires a kernel that is configured with the CONFIGCGROUPS option.\n\nThe virtualization provided by cgroup namespaces serves a number of purposes:\n\n* It  prevents  information leaks whereby cgroup directory paths outside of a container would\notherwise be visible to processes in the container.  Such leakages could, for example,  re‐\nveal information about the container framework to containerized applications.\n\n* It  eases  tasks  such as container migration.  The virtualization provided by cgroup name‐\nspaces allows containers to be  isolated  from  knowledge  of  the  pathnames  of  ancestor\ncgroups.    Without   such   isolation,   the   full   cgroup   pathnames   (displayed   in\n/proc/self/cgroups) would need to be replicated on the target system when migrating a  con‐\ntainer;  those  pathnames  would  also  need to be unique, so that they don't conflict with\nother pathnames on the target system.\n\n* It allows better confinement of containerized processes, because it is  possible  to  mount\nthe  container's  cgroup filesystems such that the container processes can't gain access to\nancestor cgroup directories.  Consider, for example, the following scenario:\n\n• We have a cgroup directory, /cg/1, that is owned by user ID 9000.\n\n• We have a process, X, also owned by user ID 9000, that is namespaced under  the  cgroup\n/cg/1/2  (i.e.,  X was placed in a new cgroup namespace via clone(2) or unshare(2) with\nthe CLONENEWCGROUP flag).\n\nIn the absence of cgroup namespacing, because the cgroup  directory  /cg/1  is  owned  (and\nwritable)  by UID 9000 and process X is also owned by user ID 9000, process X would be able\nto modify the contents of cgroups files (i.e., change cgroup settings) not only in  /cg/1/2\nbut  also  in  the ancestor cgroup directory /cg/1.  Namespacing process X under the cgroup\ndirectory /cg/1/2, in combination with suitable mount operations for the cgroup  filesystem\n(as  shown  above), prevents it modifying files in /cg/1, since it cannot even see the con‐\ntents of that directory (or of further removed cgroup ancestor directories).  Combined with\ncorrect  enforcement of hierarchical limits, this prevents process X from escaping the lim‐\nits imposed by ancestor cgroups.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "unshare(1),  clone(2),  setns(2),  unshare(2),  proc(5),  cgroups(7),  credentials(7),  name‐‐\nspaces(7), usernamespaces(7)\n",
                "subsections": []
            },
            "COLOPHON": {
                "content": "This  page  is  part  of  release  5.10 of the Linux man-pages project.  A description of the\nproject, information about reporting bugs, and the latest version of this page, can be  found\nat https://www.kernel.org/doc/man-pages/.\n\n\n\nLinux                                        2020-11-01                         CGROUPNAMESPACES(7)",
                "subsections": []
            }
        }
    }
}