| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"runtime" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/go-git/go-billy/v5" |
| 12 |
|
| 13 |
"github.com/go-git/go-git/v5/internal/pathutil" |
| 14 |
) |
| 15 |
|
| 16 |
// defaultProtectHFS returns the default value for core.protectHFS |
| 17 |
// when not explicitly configured. Matches upstream Git's |
| 18 |
// PROTECT_HFS_DEFAULT[1], which the Makefile sets to 1 on Darwin |
| 19 |
// and leaves at 0 on every other platform. |
| 20 |
// |
| 21 |
// [1]: https://github.com/git/git/blob/v2.54.0/config.mak.uname#L146 |
| 22 |
func defaultProtectHFS() bool { |
| 23 |
return runtime.GOOS == "darwin" |
| 24 |
} |
| 25 |
|
| 26 |
// defaultProtectNTFS returns the default value for core.protectNTFS |
| 27 |
// when not explicitly configured. Matches upstream Git's |
| 28 |
// PROTECT_NTFS_DEFAULT, which has been 1 on every platform since |
| 29 |
// 9102f958ee5 (CVE-2019-1353)[1]: WSL allows Linux processes to |
| 30 |
// reach NTFS-mounted worktrees on Windows hosts, so the |
| 31 |
// is_ntfs_dotgit guard cannot safely be gated on the runtime OS. |
| 32 |
// |
| 33 |
// [1]: https://github.com/git/git/commit/9102f958ee5 |
| 34 |
func defaultProtectNTFS() bool { |
| 35 |
return true |
| 36 |
} |
| 37 |
|
| 38 |
// worktreeFilesystem wraps a billy.Filesystem and validates every path it |
| 39 |
// is handed, so worktree operations cannot use dangerous paths at the |
| 40 |
// boundary. Two layers apply: |
| 41 |
// |
| 42 |
// - validPath rejects dangerous path *strings*: .git and its HFS+/NTFS |
| 43 |
// variants, "..", control characters, volume names. |
| 44 |
// - validNoLeadingSymlink rejects paths whose leading directories |
| 45 |
// already exist on disk as symlinks, so a write or delete cannot |
| 46 |
// follow a planted link out of the tree. |
| 47 |
// |
| 48 |
// Both layers run on every mutating operation (validWritePath) and every |
| 49 |
// read (validReadPath). Chroot additionally refuses a symlink as the final |
| 50 |
// component, so a sub-filesystem such as a submodule worktree cannot be |
| 51 |
// scoped to a redirected target. |
| 52 |
// |
| 53 |
// The wrapper intentionally stops at leading-component traversal. Callers |
| 54 |
// that need final-component no-follow semantics for materialisation |
| 55 |
// (checkoutFile) enforce that directly by removing the blocking symlink |
| 56 |
// before opening the destination path. |
| 57 |
type worktreeFilesystem struct { |
| 58 |
billy.Filesystem |
| 59 |
protectNTFS bool |
| 60 |
protectHFS bool |
| 61 |
} |
| 62 |
|
| 63 |
func newWorktreeFilesystem(fs billy.Filesystem, protectNTFS, protectHFS bool) *worktreeFilesystem { |
| 64 |
return &worktreeFilesystem{Filesystem: fs, protectNTFS: protectNTFS, protectHFS: protectHFS} |
| 65 |
} |
| 66 |
|
| 67 |
func (sfs *worktreeFilesystem) Create(filename string) (billy.File, error) { |
| 68 |
if err := sfs.validWritePath(filename); err != nil { |
| 69 |
return nil, fmt.Errorf("create: %w", err) |
| 70 |
} |
| 71 |
return sfs.Filesystem.Create(filename) |
| 72 |
} |
| 73 |
|
| 74 |
func (sfs *worktreeFilesystem) Open(filename string) (billy.File, error) { |
| 75 |
if err := sfs.validReadPath(filename); err != nil { |
| 76 |
return nil, fmt.Errorf("open: %w", err) |
| 77 |
} |
| 78 |
return sfs.Filesystem.Open(filename) |
| 79 |
} |
| 80 |
|
| 81 |
func (sfs *worktreeFilesystem) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) { |
| 82 |
if err := sfs.validWritePath(filename); err != nil { |
| 83 |
return nil, fmt.Errorf("openfile: %w", err) |
| 84 |
} |
| 85 |
return sfs.Filesystem.OpenFile(filename, flag, perm) |
| 86 |
} |
| 87 |
|
| 88 |
func (sfs *worktreeFilesystem) Stat(filename string) (os.FileInfo, error) { |
| 89 |
if err := sfs.validReadPath(filename); err != nil { |
| 90 |
return nil, fmt.Errorf("stat: %w", err) |
| 91 |
} |
| 92 |
return sfs.Filesystem.Stat(filename) |
| 93 |
} |
| 94 |
|
| 95 |
func (sfs *worktreeFilesystem) Remove(filename string) error { |
| 96 |
if err := sfs.validWritePath(filename); err != nil { |
| 97 |
return fmt.Errorf("remove: %w", err) |
| 98 |
} |
| 99 |
return sfs.Filesystem.Remove(filename) |
| 100 |
} |
| 101 |
|
| 102 |
func (sfs *worktreeFilesystem) Rename(from, to string) error { |
| 103 |
if err := sfs.validWritePath(from, to); err != nil { |
| 104 |
return fmt.Errorf("rename: %w", err) |
| 105 |
} |
| 106 |
return sfs.Filesystem.Rename(from, to) |
| 107 |
} |
| 108 |
|
| 109 |
func (sfs *worktreeFilesystem) ReadDir(path string) ([]os.FileInfo, error) { |
| 110 |
if err := sfs.validReadPath(path); err != nil { |
| 111 |
return nil, fmt.Errorf("readdir: %w", err) |
| 112 |
} |
| 113 |
return sfs.Filesystem.ReadDir(path) |
| 114 |
} |
| 115 |
|
| 116 |
func (sfs *worktreeFilesystem) Lstat(filename string) (os.FileInfo, error) { |
| 117 |
if err := sfs.validReadPath(filename); err != nil { |
| 118 |
return nil, fmt.Errorf("lstat: %w", err) |
| 119 |
} |
| 120 |
return sfs.Filesystem.Lstat(filename) |
| 121 |
} |
| 122 |
|
| 123 |
func (sfs *worktreeFilesystem) Symlink(target, link string) error { |
| 124 |
if err := sfs.validWritePath(link); err != nil { |
| 125 |
return fmt.Errorf("symlink: %w", err) |
| 126 |
} |
| 127 |
if err := sfs.validSymlinkName(link); err != nil { |
| 128 |
return fmt.Errorf("symlink: %w", err) |
| 129 |
} |
| 130 |
return sfs.Filesystem.Symlink(target, link) |
| 131 |
} |
| 132 |
|
| 133 |
func (sfs *worktreeFilesystem) Readlink(link string) (string, error) { |
| 134 |
if err := sfs.validReadPath(link); err != nil { |
| 135 |
return "", fmt.Errorf("readlink: %w", err) |
| 136 |
} |
| 137 |
return sfs.Filesystem.Readlink(link) |
| 138 |
} |
| 139 |
|
| 140 |
func (sfs *worktreeFilesystem) MkdirAll(path string, perm os.FileMode) error { |
| 141 |
// MkdirAll on the worktree root is a no-op: the root always exists, |
| 142 |
// so there is nothing to materialise. Mirroring the tolerance that |
| 143 |
// validReadPath gives to read-side operations avoids breaking callers |
| 144 |
// that walk a directory tree and pass the relative-to-root prefix |
| 145 |
// ("") through to the worktree FS. |
| 146 |
if path == "" || path == "." || path == "/" { |
| 147 |
return nil |
| 148 |
} |
| 149 |
if err := sfs.validWritePath(path); err != nil { |
| 150 |
return fmt.Errorf("mkdirall: %w", err) |
| 151 |
} |
| 152 |
return sfs.Filesystem.MkdirAll(path, perm) |
| 153 |
} |
| 154 |
|
| 155 |
func (sfs *worktreeFilesystem) TempFile(_, _ string) (billy.File, error) { |
| 156 |
return nil, fmt.Errorf("tempfile: %w", errUnsupportedOperation) |
| 157 |
} |
| 158 |
|
| 159 |
func (sfs *worktreeFilesystem) Chroot(path string) (billy.Filesystem, error) { |
| 160 |
if err := sfs.validReadPath(path); err != nil { |
| 161 |
return nil, fmt.Errorf("chroot: %w", err) |
| 162 |
} |
| 163 |
// Chroot scopes a sub-filesystem to path, so the final component must |
| 164 |
// be a real directory too: a symlink there would silently redirect the |
| 165 |
// scope (e.g. a submodule worktree) to a target outside the tree. This |
| 166 |
// is the "valid path, wrong target" case that validNoLeadingSymlink, |
| 167 |
// which only inspects leading components, does not cover. |
| 168 |
// |
| 169 |
// A non-existent target is fine: Chroot creates it as a real |
| 170 |
// directory. Any other Lstat error means we cannot prove the target |
| 171 |
// is not a symlink, so fail closed rather than scope through it. |
| 172 |
if fi, err := sfs.Filesystem.Lstat(path); err != nil { |
| 173 |
if !os.IsNotExist(err) { |
| 174 |
return nil, fmt.Errorf("chroot: cannot stat %q: %w", path, err) |
| 175 |
} |
| 176 |
} else if fi.Mode()&os.ModeSymlink != 0 { |
| 177 |
return nil, fmt.Errorf("chroot: invalid path %q: is a symlink", path) |
| 178 |
} |
| 179 |
return sfs.Filesystem.Chroot(path) |
| 180 |
} |
| 181 |
|
| 182 |
// validReadPath is like validWritePath but treats the empty string and "." |
| 183 |
// as valid references to the worktree root. Read-side operations on the |
| 184 |
// root (e.g. ReadDir(""), Lstat(".")) are legitimate. Mutating the root |
| 185 |
// itself is not, so write-side operations reject it via validPath. Reads |
| 186 |
// are still refused through a leading symlink, so the wrapper never |
| 187 |
// follows a planted link even on the read surface. |
| 188 |
func (sfs *worktreeFilesystem) validReadPath(p string) error { |
| 189 |
if p == "" || p == "." || p == "/" { |
| 190 |
return nil |
| 191 |
} |
| 192 |
if err := sfs.validPath(p); err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
return sfs.validNoLeadingSymlink(p) |
| 196 |
} |
| 197 |
|
| 198 |
var errUnsupportedOperation = errors.New("unsupported operation") |
| 199 |
|
| 200 |
// isDotGitVariant reports whether part is .git, git~1, or an HFS+ |
| 201 |
// equivalent of .git (when protectHFS is true). NTFS variants of .git |
| 202 |
// (e.g. ".git " with trailing space, ".git::$INDEX_ALLOCATION") are |
| 203 |
// detected separately by pathutil.WindowsValidPath, which applies |
| 204 |
// regardless of position in the path. Both validators reuse this |
| 205 |
// helper. |
| 206 |
func isDotGitVariant(part string, protectHFS bool) bool { |
| 207 |
if pathutil.IsDotGitName(part) { |
| 208 |
return true |
| 209 |
} |
| 210 |
if protectHFS && pathutil.IsHFSDotGit(part) { |
| 211 |
return true |
| 212 |
} |
| 213 |
return false |
| 214 |
} |
| 215 |
|
| 216 |
// validPath checks whether paths are valid for the worktree |
| 217 |
// filesystem abstraction. It is intentionally tolerant of .git as |
| 218 |
// the final path component of a multi-component path |
| 219 |
// (e.g. "submodule/.git"), so that legitimate gitlink pointer files |
| 220 |
// can still be Stat'd, Read, and Removed via the wrapper during |
| 221 |
// submodule cleanup. Attacker-controlled tree-entry paths are |
| 222 |
// validated separately by pathutil.ValidTreePath at the boundaries |
| 223 |
// where data leaves the trusted store (Tree.FindEntry, the explicit |
| 224 |
// callers in CherryPick and Submodule.Repository). |
| 225 |
// |
| 226 |
// For upstream rules: |
| 227 |
// https://github.com/git/git/blob/v2.54.0/read-cache.c#L987 |
| 228 |
// https://github.com/git/git/blob/v2.54.0/path.c#L1419 |
| 229 |
func (sfs *worktreeFilesystem) validPath(paths ...string) error { |
| 230 |
for _, p := range paths { |
| 231 |
for i := 0; i < len(p); i++ { |
| 232 |
if p[i] < 0x20 || p[i] == 0x7f { |
| 233 |
return fmt.Errorf("invalid path %q: contains control character", p) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
parts := strings.FieldsFunc(p, func(r rune) bool { return (r == '\\' || r == '/') }) |
| 238 |
if len(parts) == 0 { |
| 239 |
return fmt.Errorf("invalid path: %q", p) |
| 240 |
} |
| 241 |
|
| 242 |
if sfs.protectNTFS { |
| 243 |
// Volume names are not supported, in both formats: \\ and <DRIVE_LETTER>:. |
| 244 |
if vol := filepath.VolumeName(p); vol != "" { |
| 245 |
return fmt.Errorf("invalid path: %q", p) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
for i, part := range parts { |
| 250 |
if part == "." || part == ".." { |
| 251 |
return fmt.Errorf("invalid path %q: cannot use %q", p, part) |
| 252 |
} |
| 253 |
|
| 254 |
// Reject .git (and equivalents) as a path component when it is |
| 255 |
// either the first component (root-level .git) or a non-final |
| 256 |
// component (traversal into a .git directory, e.g. "a/.git/config"). |
| 257 |
// A final non-first .git component (e.g. "submodule/.git") is |
| 258 |
// allowed because submodule worktrees contain a .git pointer file. |
| 259 |
if isDotGitVariant(part, sfs.protectHFS) && (i == 0 || i < len(parts)-1) { |
| 260 |
return fmt.Errorf("invalid path component: %q", p) |
| 261 |
} |
| 262 |
|
| 263 |
if sfs.protectNTFS && !pathutil.WindowsValidPath(part) { |
| 264 |
return fmt.Errorf("invalid path: %q", p) |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
return nil |
| 269 |
} |
| 270 |
|
| 271 |
// validWritePath validates paths for mutating operations. It layers the |
| 272 |
// filesystem-state check validNoLeadingSymlink on top of the string-only |
| 273 |
// checks in validPath, so a write can neither name a dangerous path nor |
| 274 |
// reach one by traversing an existing symlink. Every mutating method on |
| 275 |
// the wrapper funnels through here, so the leading-symlink invariant holds |
| 276 |
// for all worktree writers without each call site having to remember it. |
| 277 |
func (sfs *worktreeFilesystem) validWritePath(paths ...string) error { |
| 278 |
if err := sfs.validPath(paths...); err != nil { |
| 279 |
return err |
| 280 |
} |
| 281 |
return sfs.validNoLeadingSymlink(paths...) |
| 282 |
} |
| 283 |
|
| 284 |
// validNoLeadingSymlink rejects paths whose leading directory components |
| 285 |
// resolve through a symlink that already exists on the underlying |
| 286 |
// filesystem. validPath guards the path string. This guards the on-disk |
| 287 |
// state, so a write or delete cannot reach outside the worktree by |
| 288 |
// traversing a symlink that a tree or an earlier step left in place. |
| 289 |
// |
| 290 |
// This is the fail-closed backstop for the whole class. Callers that want |
| 291 |
// upstream's replace-and-continue behaviour (checkout) remove the blocking |
| 292 |
// symlink first via clearBlockingSymlinks, so no symlink remains when the |
| 293 |
// write reaches the wrapper. Callers that do not get a safe error, |
| 294 |
// matching upstream Git refusing rather than following the link. See |
| 295 |
// has_symlink_leading_path (symlinks.c) and the check_leading_path guard |
| 296 |
// in unlink_entry (entry.c). |
| 297 |
func (sfs *worktreeFilesystem) validNoLeadingSymlink(paths ...string) error { |
| 298 |
for _, p := range paths { |
| 299 |
for dir := filepath.Dir(p); dir != "." && dir != "" && dir != string(filepath.Separator); dir = filepath.Dir(dir) { |
| 300 |
fi, err := sfs.Filesystem.Lstat(dir) |
| 301 |
if err != nil { |
| 302 |
// A missing ancestor is materialised as a real directory, |
| 303 |
// so it cannot be a symlink and is safe to skip. Any other |
| 304 |
// error (permission, I/O) means we cannot prove the |
| 305 |
// component is not a symlink, so fail closed rather than |
| 306 |
// let the operation traverse an unverified component. |
| 307 |
if os.IsNotExist(err) { |
| 308 |
continue |
| 309 |
} |
| 310 |
return fmt.Errorf("invalid path %q: cannot stat leading component %q: %w", p, dir, err) |
| 311 |
} |
| 312 |
if fi.Mode()&os.ModeSymlink != 0 { |
| 313 |
return fmt.Errorf("invalid path %q: leading component %q is a symlink", p, dir) |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
return nil |
| 318 |
} |
| 319 |
|
| 320 |
// validSymlinkName checks the per-component name of a symlink for |
| 321 |
// dotfile names that attackers can use to trick a checkout into |
| 322 |
// writing a dangerous symlink. Each path component is compared |
| 323 |
// against .gitmodules case-insensitively, against its NTFS variants |
| 324 |
// (e.g. ".gitmodules .", ".gitmodules::$INDEX_ALLOCATION", or 8.3 |
| 325 |
// short-name forms) when protectNTFS is on, and against its HFS+ |
| 326 |
// variants (Unicode ignored code points folded into ".gitmodules") |
| 327 |
// when protectHFS is on. |
| 328 |
// |
| 329 |
// Reference: upstream Git verify_path_internal at read-cache.c#L1004-L1024 |
| 330 |
// in tag v2.54.0[1]. |
| 331 |
// |
| 332 |
// [1]: https://github.com/git/git/blob/v2.54.0/read-cache.c#L1004-L1024 |
| 333 |
func (sfs *worktreeFilesystem) validSymlinkName(name string) error { |
| 334 |
parts := strings.FieldsFunc(name, func(r rune) bool { |
| 335 |
return r == '/' || r == '\\' |
| 336 |
}) |
| 337 |
for _, part := range parts { |
| 338 |
if strings.EqualFold(part, gitmodulesFile) { |
| 339 |
return ErrGitModulesSymlink |
| 340 |
} |
| 341 |
if sfs.protectNTFS && pathutil.IsNTFSDotGitmodules(part) { |
| 342 |
return ErrGitModulesSymlink |
| 343 |
} |
| 344 |
if sfs.protectHFS && pathutil.IsHFSDotGitmodules(part) { |
| 345 |
return ErrGitModulesSymlink |
| 346 |
} |
| 347 |
} |
| 348 |
return nil |
| 349 |
} |