Skip to content

Commit 2f7393a

Browse files
crosbymichaeltonistiigi
authored andcommitted
Set init processes as non-dumpable
This sets the init processes that join and setup the container's namespaces as non-dumpable before they setns to the container's pid (or any other ) namespace. This settings is automatically reset to the default after the Exec in the container so that it does not change functionality for the applications that are running inside, just our init processes. This prevents parent processes, the pid 1 of the container, to ptrace the init process before it drops caps and other sets LSMs. This patch also ensures that the stateDirFD being used is still closed prior to exec, even though it is set as O_CLOEXEC, because of the order in the kernel. https://github.com/torvalds/linux/blob/v4.9/fs/exec.c#L1290-L1318 The order during the exec syscall is that the process is set back to dumpable before O_CLOEXEC are processed. Signed-off-by: Michael Crosby <[email protected]> (cherry picked from commit 50a19c6ff828c58e5dab13830bd3dacde268afe5) Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 5137186 commit 2f7393a

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

libcontainer/init_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ func newContainerInit(t initType, pipe *os.File, stateDirFD int) (initer, error)
7777
switch t {
7878
case initSetns:
7979
return &linuxSetnsInit{
80-
config: config,
80+
config: config,
81+
stateDirFD: stateDirFD,
8182
}, nil
8283
case initStandard:
8384
return &linuxStandardInit{

libcontainer/nsenter/nsexec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ void nsexec(void)
435435
if (pipenum == -1)
436436
return;
437437

438+
/* make the process non-dumpable */
439+
if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) {
440+
bail("failed to set process as non-dumpable");
441+
}
442+
438443
/* Parse all of the netlink configuration. */
439444
nl_parse(pipenum, &config);
440445

libcontainer/setns_init_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package libcontainer
55
import (
66
"fmt"
77
"os"
8+
"syscall"
89

910
"github.com/opencontainers/runc/libcontainer/apparmor"
1011
"github.com/opencontainers/runc/libcontainer/keys"
@@ -16,7 +17,8 @@ import (
1617
// linuxSetnsInit performs the container's initialization for running a new process
1718
// inside an existing container.
1819
type linuxSetnsInit struct {
19-
config *initConfig
20+
config *initConfig
21+
stateDirFD int
2022
}
2123

2224
func (l *linuxSetnsInit) getSessionRingName() string {
@@ -49,5 +51,8 @@ func (l *linuxSetnsInit) Init() error {
4951
if err := label.SetProcessLabel(l.config.ProcessLabel); err != nil {
5052
return err
5153
}
54+
// close the statedir fd before exec because the kernel resets dumpable in the wrong order
55+
// https://github.com/torvalds/linux/blob/v4.9/fs/exec.c#L1290-L1318
56+
syscall.Close(l.stateDirFD)
5257
return system.Execv(l.config.Args[0], l.config.Args[0:], os.Environ())
5358
}

libcontainer/standard_init_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ func (l *linuxStandardInit) Init() error {
171171
return newSystemErrorWithCause(err, "init seccomp")
172172
}
173173
}
174+
// close the statedir fd before exec because the kernel resets dumpable in the wrong order
175+
// https://github.com/torvalds/linux/blob/v4.9/fs/exec.c#L1290-L1318
176+
syscall.Close(l.stateDirFD)
174177
if err := syscall.Exec(name, l.config.Args[0:], os.Environ()); err != nil {
175178
return newSystemErrorWithCause(err, "exec user process")
176179
}

0 commit comments

Comments
 (0)