Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: container can not write cgroup with privileged #2552

Merged
merged 1 commit into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions daemon/mgr/spec_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error {
s.Mounts = sortMounts(mounts)

if c.HostConfig.Privileged {
if !s.Root.Readonly {
for i := range s.Mounts {
// Clear readonly for /sys.
for i := range s.Mounts {
if s.Mounts[i].Destination == "/sys" {
clearReadonly(&s.Mounts[i])
}
if s.Mounts[i].Destination == "/sys" && !s.Root.Readonly {
clearReadonly(&s.Mounts[i])
}

// Clear readonly for cgroup
if s.Mounts[i].Type == "cgroup" {
clearReadonly(&s.Mounts[i])
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/cli_run_with_privileged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/util"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
Expand Down Expand Up @@ -86,3 +87,21 @@ func (suite *PouchRunPrivilegedSuite) TestRunCheckSysWritableWithAndWithoutPrivi
c.Errorf("expected %s, but got %s", expected, out)
}
}

// TestCgroupWritableWithAndWithoutPrivileged tests cgroup can be writable with privileged,
// can not be writable without privileged
func (suite *PouchRunPrivilegedSuite) TestCgroupWritableWithAndWithoutPrivileged(c *check.C) {
name := "TestRunCheckCgroupWritable"
command.PouchRun("run", "--name", name, "--privileged", busyboxImage, "sh", "-c", "mkdir /sys/fs/cgroup/cpu/test").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)

name1 := "TestRunCheckCgroupCannotWritable"
res := command.PouchRun("run", "--name", name1, busyboxImage, "sh", "-c", "mkdir /sys/fs/cgroup/cpu/test")
defer DelContainerForceMultyTime(c, name1)

if res.ExitCode == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if condition is duplicated with c.Assert(util.PartialEqual(res.Combined(), "Read-only file system"), check.IsNil) . I think that we can remove the if condition. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy from other test in test/cli_run_with_privileged_test.go, author may think error stdout is not enough to judge error, I agree with he. But I do not mind to remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not mind, I will do this in next pr, and fix all tests in test/cli_run_with_privileged_test.go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

c.Errorf("non-privileged container executes mkdir /sys/fs/cgroup/cpu/test should failed, but succeeded: %v", res.Combined())
}

c.Assert(util.PartialEqual(res.Combined(), "Read-only file system"), check.IsNil)
}