|
4 | 4 | package utils
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "fmt"
|
8 | 9 | "os"
|
| 10 | + "path/filepath" |
9 | 11 | "strconv"
|
10 | 12 | "strings"
|
11 | 13 | _ "unsafe" // for go:linkname
|
12 | 14 |
|
| 15 | + "github.com/opencontainers/runc/libcontainer/system" |
| 16 | + |
| 17 | + securejoin "github.com/cyphar/filepath-securejoin" |
13 | 18 | "golang.org/x/sys/unix"
|
14 | 19 | )
|
15 | 20 |
|
@@ -130,3 +135,112 @@ func IsLexicallyInRoot(root, path string) bool {
|
130 | 135 | }
|
131 | 136 | return strings.HasPrefix(path, root)
|
132 | 137 | }
|
| 138 | + |
| 139 | +// MkdirAllInRootOpen attempts to make |
| 140 | +// |
| 141 | +// path, _ := securejoin.SecureJoin(root, unsafePath) |
| 142 | +// os.MkdirAll(path, mode) |
| 143 | +// os.Open(path) |
| 144 | +// |
| 145 | +// safer against attacks where components in the path are changed between |
| 146 | +// SecureJoin returning and MkdirAll (or Open) being called. In particular, we |
| 147 | +// try to detect any symlink components in the path while we are doing the |
| 148 | +// MkdirAll. |
| 149 | +// |
| 150 | +// NOTE: Unlike os.MkdirAll, mode is not Go's os.FileMode, it is the unix mode |
| 151 | +// (the suid/sgid/sticky bits are not the same as for os.FileMode). |
| 152 | +// |
| 153 | +// NOTE: If unsafePath is a subpath of root, we assume that you have already |
| 154 | +// called SecureJoin and so we use the provided path verbatim without resolving |
| 155 | +// any symlinks (this is done in a way that avoids symlink-exchange races). |
| 156 | +// This means that the path also must not contain ".." elements, otherwise an |
| 157 | +// error will occur. |
| 158 | +// |
| 159 | +// This is a somewhat less safe alternative to |
| 160 | +// <https://github.com/cyphar/filepath-securejoin/pull/13>, but it should |
| 161 | +// detect attempts to trick us into creating directories outside of the root. |
| 162 | +// We should migrate to securejoin.MkdirAll once it is merged. |
| 163 | +func MkdirAllInRootOpen(root, unsafePath string, mode uint32) (_ *os.File, Err error) { |
| 164 | + // If the path is already "within" the root, use it verbatim. |
| 165 | + fullPath := unsafePath |
| 166 | + if !IsLexicallyInRoot(root, unsafePath) { |
| 167 | + var err error |
| 168 | + fullPath, err = securejoin.SecureJoin(root, unsafePath) |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + } |
| 173 | + subPath, err := filepath.Rel(root, fullPath) |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + |
| 178 | + // Check for any silly mode bits. |
| 179 | + if mode&^0o7777 != 0 { |
| 180 | + return nil, fmt.Errorf("tried to include non-mode bits in MkdirAll mode: 0o%.3o", mode) |
| 181 | + } |
| 182 | + |
| 183 | + currentDir, err := os.OpenFile(root, unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 184 | + if err != nil { |
| 185 | + return nil, fmt.Errorf("open root handle: %w", err) |
| 186 | + } |
| 187 | + defer func() { |
| 188 | + if Err != nil { |
| 189 | + currentDir.Close() |
| 190 | + } |
| 191 | + }() |
| 192 | + |
| 193 | + for _, part := range strings.Split(subPath, string(filepath.Separator)) { |
| 194 | + switch part { |
| 195 | + case "", ".": |
| 196 | + // Skip over no-op components. |
| 197 | + continue |
| 198 | + case "..": |
| 199 | + return nil, fmt.Errorf("possible breakout detected: found %q component in SecureJoin subpath %s", part, subPath) |
| 200 | + } |
| 201 | + |
| 202 | + nextDir, err := system.Openat(currentDir, part, unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 203 | + switch { |
| 204 | + case err == nil: |
| 205 | + // Update the currentDir. |
| 206 | + _ = currentDir.Close() |
| 207 | + currentDir = nextDir |
| 208 | + |
| 209 | + case errors.Is(err, unix.ENOTDIR): |
| 210 | + // This might be a symlink or some other random file. Either way, |
| 211 | + // error out. |
| 212 | + return nil, fmt.Errorf("cannot mkdir in %s/%s: %w", currentDir.Name(), part, unix.ENOTDIR) |
| 213 | + |
| 214 | + case errors.Is(err, os.ErrNotExist): |
| 215 | + // Luckily, mkdirat will not follow trailing symlinks, so this is |
| 216 | + // safe to do as-is. |
| 217 | + if err := system.Mkdirat(currentDir, part, mode); err != nil { |
| 218 | + return nil, err |
| 219 | + } |
| 220 | + // Open the new directory. There is a race here where an attacker |
| 221 | + // could swap the directory with a different directory, but |
| 222 | + // MkdirAll's fuzzy semantics mean we don't care about that. |
| 223 | + nextDir, err := system.Openat(currentDir, part, unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) |
| 224 | + if err != nil { |
| 225 | + return nil, fmt.Errorf("open newly created directory: %w", err) |
| 226 | + } |
| 227 | + // Update the currentDir. |
| 228 | + _ = currentDir.Close() |
| 229 | + currentDir = nextDir |
| 230 | + |
| 231 | + default: |
| 232 | + return nil, err |
| 233 | + } |
| 234 | + } |
| 235 | + return currentDir, nil |
| 236 | +} |
| 237 | + |
| 238 | +// MkdirAllInRoot is a wrapper around MkdirAllInRootOpen which closes the |
| 239 | +// returned handle, for callers that don't need to use it. |
| 240 | +func MkdirAllInRoot(root, unsafePath string, mode uint32) error { |
| 241 | + f, err := MkdirAllInRootOpen(root, unsafePath, mode) |
| 242 | + if err == nil { |
| 243 | + _ = f.Close() |
| 244 | + } |
| 245 | + return err |
| 246 | +} |
0 commit comments