1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
--- a/libcontainer/init_linux.go
+++ b/libcontainer/init_linux.go
@@ -7,6 +7,7 @@ import (
"net"
"os"
+ "path/filepath"
"runtime"
"runtime/debug"
"strconv"
@@ -268,6 +272,32 @@ func populateProcessEnvironment(env []string) error {
return nil
}
+// verifyCwd确保当前工作目录仍在容器的挂载命名空间根目录内。如果getcwd(2)返回ENOENT,
// 表示cwd在容器外部。
// 参见CVE-2024-21626。
+func verifyCwd() error {
+ if wd, err := unix.Getwd(); errors.Is(err, unix.ENOENT) {
+ return errors.New("current working directory is outside of container mount namespace root -- possible container breakout detected")
+ } else if err != nil {
+ return fmt.Errorf("failed to verify if current working directory is safe: %w", err)
+ } else if !filepath.IsAbs(wd) {
+ // 健全性检查:cwd应始终为绝对路径
+ return fmt.Errorf("current working directory is not absolute -- possible container breakout detected: cwd is %q", wd)
+ }
+ return nil
+}
@@ -326,6 +353,10 @@ func finalizeNamespace(config *initConfig) error {
if err := system.ClearKeepCaps(); err != nil {
return fmt.Errorf("unable to clear keep caps: %w", err)
}
+ // 在chdir到config.Cwd后,确保它仍在容器内
+ if err := verifyCwd(); err != nil {
+ return err
+ }
return nil
}
|