Skip to content

Commit

Permalink
Merge pull request #896 from HusterWan/zr/fix-cgroup-parent
Browse files Browse the repository at this point in the history
bugfix: cgroup-parent must be abs
  • Loading branch information
HusterWan authored Mar 15, 2018
2 parents 3391aa8 + 290cfcb commit a6e3da4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,12 @@ func (mgr *ContainerManager) Start(ctx context.Context, id, detachKeys string) (
cgroupsParent = mgr.Config.CgroupParent
}

// cgroupsPath must be absolute path
if cgroupsParent != "" {
if !filepath.IsAbs(cgroupsParent) {
cgroupsParent = filepath.Clean("/" + cgroupsParent)
}

s.Linux.CgroupsPath = filepath.Join(cgroupsParent, c.ID())
}

Expand Down
18 changes: 17 additions & 1 deletion test/cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func testRunWithCgroupParent(c *check.C, cgroupParent, name string) {

file := "/sys/fs/cgroup/memory/" + cgroupParent + "/" + containerID + "/memory.limit_in_bytes"
if _, err := os.Stat(file); err != nil {
c.Fatalf("container %s cgroup mountpoint not exists", containerID)
c.Fatalf("container %s cgroup mountpoint not exists", name)
}

out, err := exec.Command("cat", file).Output()
Expand All @@ -745,3 +745,19 @@ func testRunWithCgroupParent(c *check.C, cgroupParent, name string) {
}

}

// TestRunInvalidCgroupParent checks that a specially-crafted cgroup parent doesn't cause Docker to crash or start modifying /.
func (suite *PouchRunSuite) TestRunInvalidCgroupParent(c *check.C) {
testRunInvalidCgroupParent(c, "../../../../../../../../SHOULD_NOT_EXIST", "SHOULD_NOT_EXIST", "cgroup-invalid-test")

testRunInvalidCgroupParent(c, "/../../../../../../../../SHOULD_NOT_EXIST", "/SHOULD_NOT_EXIST", "cgroup-absolute-invalid-test")
}

func testRunInvalidCgroupParent(c *check.C, cgroupParent, cleanCgroupParent, name string) {
command.PouchRun("run", "-m", "300M", "--cgroup-parent", cgroupParent, "--name", name, busyboxImage, "cat", "/proc/self/cgroup").Assert(c, icmd.Success)

// We expect "/SHOULD_NOT_EXIST" to not exist. If not, we have a security issue.
if _, err := os.Stat("/SHOULD_NOT_EXIST"); err == nil || !os.IsNotExist(err) {
c.Fatalf("SECURITY: --cgroup-parent with ../../ relative paths cause files to be created in the host (this is bad) !!")
}
}

0 comments on commit a6e3da4

Please sign in to comment.