Skip to content

Commit

Permalink
namespaces: by default create cgroupns on cgroups v2
Browse files Browse the repository at this point in the history
change the default on cgroups v2 and create a new cgroup namespace.

When a cgroup namespace is used, processes inside the namespace are
only able to see cgroup paths relative to the cgroup namespace root
and not have full visibility on all the cgroups present on the
system.

The previous behaviour is maintained on a cgroups v1 host, where a
cgroup namespace is not created by default.

Closes: containers#4363

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Oct 30, 2019
1 parent e7540d0 commit 439b243
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/common.go
Expand Up @@ -132,7 +132,7 @@ func getCreateFlags(c *cliconfig.PodmanCommand) {
"Drop capabilities from the container",
)
createFlags.String(
"cgroupns", "host",
"cgroupns", "",
"cgroup namespace to use",
)
createFlags.String(
Expand Down
4 changes: 3 additions & 1 deletion docs/podman-create.1.md
Expand Up @@ -67,12 +67,14 @@ Drop Linux capabilities

**--cgroupns**=*mode*

Set the cgroup namespace mode for the container, by default **host** is used.
Set the cgroup namespace mode for the container.
**host**: use the host's cgroup namespace inside the container.
**container:<NAME|ID>**: join the namespace of the specified container.
**private**: create a new cgroup namespace.
**ns:<PATH>**: join the namespace at the specified path.

If the host uses cgroups v1, the default is set to **host**. On cgroups v2 the default is **private**.

**--cgroups**=*mode*

Determines whether the container will create CGroups.
Expand Down
4 changes: 3 additions & 1 deletion docs/podman-run.1.md
Expand Up @@ -81,12 +81,14 @@ Drop Linux capabilities

**--cgroupns**=*mode*

Set the cgroup namespace mode for the container, by default **host** is used.
Set the cgroup namespace mode for the container.
**host**: use the host's cgroup namespace inside the container.
**container:<NAME|ID>**: join the namespace of the specified container.
**private**: create a new cgroup namespace.
**ns:<PATH>**: join the namespace at the specified path.

If the host uses cgroups v1, the default is set to **host**. On cgroups v2 the default is **private**.

**--cgroups**=*mode*

Determines whether the container will create CGroups.
Expand Down
5 changes: 5 additions & 0 deletions pkg/namespaces/namespaces.go
Expand Up @@ -25,6 +25,11 @@ func (n CgroupMode) IsHost() bool {
return n == hostType
}

// IsDefaultValue indicates whether the cgroup namespace has the default value.
func (n CgroupMode) IsDefaultValue() bool {
return n == ""
}

// IsNS indicates a cgroup namespace passed in by path (ns:<path>)
func (n CgroupMode) IsNS() bool {
return strings.HasPrefix(string(n), nsType)
Expand Down
13 changes: 13 additions & 0 deletions pkg/spec/spec.go
Expand Up @@ -629,6 +629,19 @@ func addIpcNS(config *CreateConfig, g *generate.Generator) error {

func addCgroupNS(config *CreateConfig, g *generate.Generator) error {
cgroupMode := config.CgroupMode

if cgroupMode.IsDefaultValue() {
// If the value is not specified, default to "private" on cgroups v2 and "host" on cgroups v1.
unified, err := cgroups.IsCgroup2UnifiedMode()
if err != nil {
return err
}
if unified {
cgroupMode = "private"
} else {
cgroupMode = "host"
}
}
if cgroupMode.IsNS() {
return g.AddOrReplaceLinuxNamespace(string(spec.CgroupNamespace), NS(string(cgroupMode)))
}
Expand Down

0 comments on commit 439b243

Please sign in to comment.