|
| 1 | +package dmz |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "golang.org/x/sys/unix" |
| 11 | + |
| 12 | + "github.com/opencontainers/runc/libcontainer/utils" |
| 13 | +) |
| 14 | + |
| 15 | +func fsopen(fsName string, flags int) (*os.File, error) { |
| 16 | + // Make sure we always set O_CLOEXEC. |
| 17 | + flags |= unix.FSOPEN_CLOEXEC |
| 18 | + fd, err := unix.Fsopen(fsName, flags) |
| 19 | + if err != nil { |
| 20 | + return nil, os.NewSyscallError("fsopen "+fsName, err) |
| 21 | + } |
| 22 | + return os.NewFile(uintptr(fd), "fscontext:"+fsName), nil |
| 23 | +} |
| 24 | + |
| 25 | +func fsmount(ctx *os.File, flags, mountAttrs int) (*os.File, error) { |
| 26 | + // Make sure we always set O_CLOEXEC. |
| 27 | + flags |= unix.FSMOUNT_CLOEXEC |
| 28 | + fd, err := unix.Fsmount(int(ctx.Fd()), flags, mountAttrs) |
| 29 | + if err != nil { |
| 30 | + return nil, os.NewSyscallError("fsmount "+ctx.Name(), err) |
| 31 | + } |
| 32 | + runtime.KeepAlive(ctx) // make sure fd is kept alive while it's used |
| 33 | + return os.NewFile(uintptr(fd), "fsmount:"+ctx.Name()), nil |
| 34 | +} |
| 35 | + |
| 36 | +func escapeOverlayLowerDir(path string) string { |
| 37 | + // If the lowerdir path contains ":" we need to escape them, and if there |
| 38 | + // were any escape characters already (\) we need to escape those first. |
| 39 | + return strings.ReplaceAll(strings.ReplaceAll(path, `\`, `\\`), `:`, `\:`) |
| 40 | +} |
| 41 | + |
| 42 | +// sealedOverlayfs will create an internal overlayfs mount using fsopen() that |
| 43 | +// uses the directory containing the binary as a lowerdir and a temporary tmpfs |
| 44 | +// as an upperdir. There is no way to "unwrap" this (unlike MS_BIND+MS_RDONLY) |
| 45 | +// and so we can create a safe zero-copy sealed version of /proc/self/exe. |
| 46 | +// This only works for privileged users and on kernels with overlayfs and |
| 47 | +// fsopen() enabled. |
| 48 | +// |
| 49 | +// TODO: Since Linux 5.11, overlayfs can be created inside user namespaces so |
| 50 | +// it is technically possible to create an overlayfs even for rootless |
| 51 | +// containers. Unfortunately, this would require some ugly manual CGo+fork |
| 52 | +// magic so we can do this later if we feel it's really needed. |
| 53 | +func sealedOverlayfs(binPath, tmpDir string) (_ *os.File, Err error) { |
| 54 | + // Try to do the superblock creation first to bail out early if we can't |
| 55 | + // use this method. |
| 56 | + overlayCtx, err := fsopen("overlay", unix.FSOPEN_CLOEXEC) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + defer overlayCtx.Close() |
| 61 | + |
| 62 | + // binPath is going to be /proc/self/exe, so do a readlink to get the real |
| 63 | + // path. overlayfs needs the real underlying directory for this protection |
| 64 | + // mode to work properly. |
| 65 | + if realPath, err := os.Readlink(binPath); err == nil { |
| 66 | + binPath = realPath |
| 67 | + } |
| 68 | + binLowerDirPath, binName := filepath.Split(binPath) |
| 69 | + // Escape any ":"s or "\"s in the path. |
| 70 | + binLowerDirPath = escapeOverlayLowerDir(binLowerDirPath) |
| 71 | + |
| 72 | + // Overlayfs requires two lowerdirs in order to run in "lower-only" mode, |
| 73 | + // where writes are completely blocked. Ideally we would create a dummy |
| 74 | + // tmpfs for this, but it turns out that overlayfs doesn't allow for |
| 75 | + // anonymous mountns paths. |
| 76 | + // NOTE: I'm working on a patch to fix this but it won't be backported. |
| 77 | + dummyLowerDirPath := escapeOverlayLowerDir(tmpDir) |
| 78 | + |
| 79 | + // Configure the lowerdirs. The binary lowerdir needs to be on the top to |
| 80 | + // ensure that a file called "runc" (binName) in the dummy lowerdir doesn't |
| 81 | + // mask the binary. |
| 82 | + lowerDirStr := binLowerDirPath + ":" + dummyLowerDirPath |
| 83 | + if err := unix.FsconfigSetString(int(overlayCtx.Fd()), "lowerdir", lowerDirStr); err != nil { |
| 84 | + return nil, fmt.Errorf("fsconfig set overlayfs lowerdir=%s: %w", lowerDirStr, err) |
| 85 | + } |
| 86 | + |
| 87 | + // Get an actual handle to the overlayfs. |
| 88 | + if err := unix.FsconfigCreate(int(overlayCtx.Fd())); err != nil { |
| 89 | + return nil, os.NewSyscallError("fsconfig create overlayfs", err) |
| 90 | + } |
| 91 | + overlayFd, err := fsmount(overlayCtx, unix.FSMOUNT_CLOEXEC, unix.MS_RDONLY|unix.MS_NODEV|unix.MS_NOSUID) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + defer overlayFd.Close() |
| 96 | + |
| 97 | + // Grab a handle to the binary through overlayfs. |
| 98 | + exeFile, err := utils.Openat(overlayFd, binName, unix.O_PATH|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("open %s from overlayfs (lowerdir=%s): %w", binName, lowerDirStr, err) |
| 101 | + } |
| 102 | + // NOTE: We would like to check that exeFile is the same as /proc/self/exe, |
| 103 | + // except this is a little difficult. Depending on what filesystems the |
| 104 | + // layers are on, overlayfs can remap the inode numbers (and it always |
| 105 | + // creates its own device numbers -- see ovl_map_dev_ino) so we can't do a |
| 106 | + // basic stat-based check. The only reasonable option would be to hash both |
| 107 | + // files and compare them, but this would require fully reading both files |
| 108 | + // which would produce a similar performance overhead to memfd cloning. |
| 109 | + // |
| 110 | + // Ultimately, there isn't a real attack to be worried about here. An |
| 111 | + // attacker would need to be able to modify files in /usr/sbin (or wherever |
| 112 | + // runc lives), at which point they could just replace the runc binary with |
| 113 | + // something malicious anyway. |
| 114 | + return exeFile, nil |
| 115 | +} |
0 commit comments