Skip to content

Commit 8e1cd2f

Browse files
committed
init: verify after chdir that cwd is inside the container
If a file descriptor of a directory in the host's mount namespace is leaked to runc init, a malicious config.json could use /proc/self/fd/... as a working directory to allow for host filesystem access after the container runs. This can also be exploited by a container process if it knows that an administrator will use "runc exec --cwd" and the target --cwd (the attacker can change that cwd to be a symlink pointing to /proc/self/fd/... and wait for the process to exec and then snoop on /proc/$pid/cwd to get access to the host). The former issue can lead to a critical vulnerability in Docker and Kubernetes, while the latter is a container breakout. We can (ab)use the fact that getcwd(2) on Linux detects this exact case, and getcwd(3) and Go's Getwd() return an error as a result. Thus, if we just do os.Getwd() after chdir we can easily detect this case and error out. In runc 1.1, a /sys/fs/cgroup handle happens to be leaked to "runc init", making this exploitable. On runc main it just so happens that the leaked /sys/fs/cgroup gets clobbered and thus this is only consistently exploitable for runc 1.1. Fixes: GHSA-xr7r-f8xq-vfvv CVE-2024-21626 Co-developed-by: lifubang <[email protected]> Signed-off-by: lifubang <[email protected]> [refactored the implementation and added more comments] Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 313ec8b commit 8e1cd2f

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

libcontainer/init_linux.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net"
99
"os"
10+
"path/filepath"
1011
"runtime"
1112
"runtime/debug"
1213
"strconv"
@@ -268,6 +269,32 @@ func populateProcessEnvironment(env []string) error {
268269
return nil
269270
}
270271

272+
// verifyCwd ensures that the current directory is actually inside the mount
273+
// namespace root of the current process.
274+
func verifyCwd() error {
275+
// getcwd(2) on Linux detects if cwd is outside of the rootfs of the
276+
// current mount namespace root, and in that case prefixes "(unreachable)"
277+
// to the returned string. glibc's getcwd(3) and Go's Getwd() both detect
278+
// when this happens and return ENOENT rather than returning a non-absolute
279+
// path. In both cases we can therefore easily detect if we have an invalid
280+
// cwd by checking the return value of getcwd(3). See getcwd(3) for more
281+
// details, and CVE-2024-21626 for the security issue that motivated this
282+
// check.
283+
//
284+
// We have to use unix.Getwd() here because os.Getwd() has a workaround for
285+
// $PWD which involves doing stat(.), which can fail if the current
286+
// directory is inaccessible to the container process.
287+
if wd, err := unix.Getwd(); errors.Is(err, unix.ENOENT) {
288+
return errors.New("current working directory is outside of container mount namespace root -- possible container breakout detected")
289+
} else if err != nil {
290+
return fmt.Errorf("failed to verify if current working directory is safe: %w", err)
291+
} else if !filepath.IsAbs(wd) {
292+
// We shouldn't ever hit this, but check just in case.
293+
return fmt.Errorf("current working directory is not absolute -- possible container breakout detected: cwd is %q", wd)
294+
}
295+
return nil
296+
}
297+
271298
// finalizeNamespace drops the caps, sets the correct user
272299
// and working dir, and closes any leaked file descriptors
273300
// before executing the command inside the namespace
@@ -326,6 +353,10 @@ func finalizeNamespace(config *initConfig) error {
326353
return fmt.Errorf("chdir to cwd (%q) set in config.json failed: %w", config.Cwd, err)
327354
}
328355
}
356+
// Make sure our final working directory is inside the container.
357+
if err := verifyCwd(); err != nil {
358+
return err
359+
}
329360
if err := system.ClearKeepCaps(); err != nil {
330361
return fmt.Errorf("unable to clear keep caps: %w", err)
331362
}

libcontainer/integration/seccomp_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
libseccomp "github.com/seccomp/libseccomp-golang"
1414
)
1515

16-
func TestSeccompDenyGetcwdWithErrno(t *testing.T) {
16+
func TestSeccompDenySyslogWithErrno(t *testing.T) {
1717
if testing.Short() {
1818
return
1919
}
@@ -25,7 +25,7 @@ func TestSeccompDenyGetcwdWithErrno(t *testing.T) {
2525
DefaultAction: configs.Allow,
2626
Syscalls: []*configs.Syscall{
2727
{
28-
Name: "getcwd",
28+
Name: "syslog",
2929
Action: configs.Errno,
3030
ErrnoRet: &errnoRet,
3131
},
@@ -39,7 +39,7 @@ func TestSeccompDenyGetcwdWithErrno(t *testing.T) {
3939
buffers := newStdBuffers()
4040
pwd := &libcontainer.Process{
4141
Cwd: "/",
42-
Args: []string{"pwd"},
42+
Args: []string{"dmesg"},
4343
Env: standardEnvironment,
4444
Stdin: buffers.Stdin,
4545
Stdout: buffers.Stdout,
@@ -65,17 +65,17 @@ func TestSeccompDenyGetcwdWithErrno(t *testing.T) {
6565
}
6666

6767
if exitCode == 0 {
68-
t.Fatalf("Getcwd should fail with negative exit code, instead got %d!", exitCode)
68+
t.Fatalf("dmesg should fail with negative exit code, instead got %d!", exitCode)
6969
}
7070

71-
expected := "pwd: getcwd: No such process"
71+
expected := "dmesg: klogctl: No such process"
7272
actual := strings.Trim(buffers.Stderr.String(), "\n")
7373
if actual != expected {
7474
t.Fatalf("Expected output %s but got %s\n", expected, actual)
7575
}
7676
}
7777

78-
func TestSeccompDenyGetcwd(t *testing.T) {
78+
func TestSeccompDenySyslog(t *testing.T) {
7979
if testing.Short() {
8080
return
8181
}
@@ -85,7 +85,7 @@ func TestSeccompDenyGetcwd(t *testing.T) {
8585
DefaultAction: configs.Allow,
8686
Syscalls: []*configs.Syscall{
8787
{
88-
Name: "getcwd",
88+
Name: "syslog",
8989
Action: configs.Errno,
9090
},
9191
},
@@ -98,7 +98,7 @@ func TestSeccompDenyGetcwd(t *testing.T) {
9898
buffers := newStdBuffers()
9999
pwd := &libcontainer.Process{
100100
Cwd: "/",
101-
Args: []string{"pwd"},
101+
Args: []string{"dmesg"},
102102
Env: standardEnvironment,
103103
Stdin: buffers.Stdin,
104104
Stdout: buffers.Stdout,
@@ -124,10 +124,10 @@ func TestSeccompDenyGetcwd(t *testing.T) {
124124
}
125125

126126
if exitCode == 0 {
127-
t.Fatalf("Getcwd should fail with negative exit code, instead got %d!", exitCode)
127+
t.Fatalf("dmesg should fail with negative exit code, instead got %d!", exitCode)
128128
}
129129

130-
expected := "pwd: getcwd: Operation not permitted"
130+
expected := "dmesg: klogctl: Operation not permitted"
131131
actual := strings.Trim(buffers.Stderr.String(), "\n")
132132
if actual != expected {
133133
t.Fatalf("Expected output %s but got %s\n", expected, actual)

0 commit comments

Comments
 (0)