Skip to content

Commit

Permalink
fix CVE-2019-5736 with the least memory
Browse files Browse the repository at this point in the history
Signed-off-by: Lifubang <lifubang@acmcoder.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
  • Loading branch information
lifubang committed Feb 19, 2019
1 parent 751f18d commit dbbee9d
Show file tree
Hide file tree
Showing 47 changed files with 3,189 additions and 2 deletions.
36 changes: 36 additions & 0 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"

"github.com/golang/protobuf/proto"
"github.com/pborman/uuid"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -460,7 +461,42 @@ func (c *linuxContainer) newParentProcess(p *Process) (parentProcess, error) {
return c.newInitProcess(p, cmd, parentPipe, childPipe)
}

func runcInTmp() (string, error) {
runc, err := os.Readlink("/proc/self/exe")
if err != nil {
return "", err
}
id := uuid.New()
temp := fmt.Sprintf("/tmp/runc.%s", id)

sf, err := unix.Open(runc, unix.O_RDONLY, 0)
if err != nil {
return "", err
}
defer unix.Close(sf)
df, err := unix.Open(temp, unix.O_CREAT|unix.O_WRONLY, 0)
if err != nil {
return "", err
}
_, err = syscall.Sendfile(df, sf, nil, 0x7FFFF000)
if err == nil {
unix.Close(df)
// chmod temp runc r-x silently
os.Chmod(temp, 0500)
} else {
unix.Close(df)
}
return temp, err
}

func (c *linuxContainer) commandTemplate(p *Process, childPipe *os.File) (*exec.Cmd, error) {
tmp, err := runcInTmp()
if err != nil {
return nil, err
}
// use temp runc binary file in /tmp
c.initPath = tmp
c.initArgs[0] = tmp
cmd := exec.Command(c.initPath, c.initArgs[1:]...)
cmd.Args[0] = c.initArgs[0]
cmd.Stdin = p.Stdin
Expand Down
7 changes: 5 additions & 2 deletions libcontainer/nsenter/nsexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,11 @@ void nsexec(void)
* to ensure that containers won't be able to access the host binary
* through /proc/self/exe. See CVE-2019-5736.
*/
if (ensure_cloned_binary() < 0)
bail("could not ensure we are a cloned binary");
/*
* Revert it first to test my code
*/
//if (ensure_cloned_binary() < 0)
// bail("could not ensure we are a cloned binary");

/* Parse all of the netlink configuration. */
nl_parse(pipenum, &config);
Expand Down
4 changes: 4 additions & 0 deletions libcontainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func (p *setnsProcess) signal(sig os.Signal) error {
func (p *setnsProcess) start() (err error) {
defer p.parentPipe.Close()
err = p.cmd.Start()
// for runc binary in /tmp, we can remove it
os.Remove(p.cmd.Args[0])
p.childPipe.Close()
if err != nil {
return newSystemErrorWithCause(err, "starting setns process")
Expand Down Expand Up @@ -262,6 +264,8 @@ func (p *initProcess) waitForChildExit(childPid int) error {
func (p *initProcess) start() error {
defer p.parentPipe.Close()
err := p.cmd.Start()
// for runc binary in /tmp, we can remove it
os.Remove(p.cmd.Args[0])
p.process.ops = p
p.childPipe.Close()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ golang.org/x/sys 41f3e6584952bb034a481797859f6ab34b6803bd https://github.com/gol
# console dependencies
github.com/containerd/console 2748ece16665b45a47f884001d5831ec79703880
github.com/pkg/errors v0.8.0

# uuid
github.com/pborman/uuid v1.2.0
10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/google/uuid/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions vendor/github.com/google/uuid/json_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dbbee9d

Please sign in to comment.