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

fix CVE-2019-5736 by using as less memory as possible #1987

Closed
wants to merge 1 commit into from
Closed
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
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)
Copy link
Member

Choose a reason for hiding this comment

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

If we're going to do it this way, I would want to use memfd_create because using /tmp won't always work and memfd_create is a fool-proof way of doing it. But ultimately I could just rewrite #1984 entirely to not use C code.


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