Skip to content

Commit 50a19c6

Browse files
crosbymichaelmlaventure
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]>
1 parent f59ba3c commit 50a19c6

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
@@ -408,6 +408,11 @@ void nsexec(void)
408408
if (pipenum == -1)
409409
return;
410410

411+
/* make the process non-dumpable */
412+
if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) {
413+
bail("failed to set process as non-dumpable");
414+
}
415+
411416
/* Parse all of the netlink configuration. */
412417
nl_parse(pipenum, &config);
413418

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)