| 1 | package chroot |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io/fs" |
| 6 | "os" |
| 7 | "path" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "syscall" |
| 11 | |
| 12 | "github.com/go-git/go-billy/v6" |
| 13 | "github.com/go-git/go-billy/v6/helper/polyfill" |
| 14 | ) |
| 15 | |
| 16 | // ChrootHelper is a helper to implement billy.Chroot. It is not a security |
| 17 | // boundary; callers that need containment should use a filesystem implementation |
| 18 | // that enforces paths at the OS boundary instead. |
| 19 | type ChrootHelper struct { //nolint |
| 20 | underlying billy.Filesystem |
| 21 | base string |
| 22 | } |
| 23 | |
| 24 | const maxFollowedSymlinks = 8 // Aligns with POSIX_SYMLOOP_MAX |
| 25 | |
| 26 | // New creates a new filesystem wrapping the given 'fs', rooted at base. |
| 27 | // All paths passed to the returned filesystem are joined with base before |
| 28 | // being forwarded to the underlying filesystem. |
| 29 | func New(fs billy.Basic, base string) billy.Filesystem { |
| 30 | return &ChrootHelper{ |
| 31 | underlying: polyfill.New(fs), |
| 32 | base: base, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (fs *ChrootHelper) underlyingPath(filename string) (string, error) { |
| 37 | if isCrossBoundaries(filename) { |
| 38 | return "", billy.ErrCrossedBoundary |
| 39 | } |
| 40 | |
| 41 | return fs.Join(fs.Root(), filename), nil |
| 42 | } |
| 43 | |
| 44 | func (fs *ChrootHelper) followedPath(filename string, followFinal bool, op string) (string, error) { |
| 45 | fullpath, err := fs.underlyingPath(filename) |
| 46 | if err != nil { |
| 47 | return "", err |
| 48 | } |
| 49 | |
| 50 | sl, ok := fs.underlying.(billy.Symlink) |
| 51 | if !ok { |
| 52 | return fullpath, nil |
| 53 | } |
| 54 | |
| 55 | rel, err := fs.relativeToRoot(fullpath) |
| 56 | if err != nil { |
| 57 | return "", err |
| 58 | } |
| 59 | |
| 60 | fullpath, err = fs.resolveFollowedPath(rel, followFinal, op, sl) |
| 61 | if errors.Is(err, billy.ErrNotSupported) { |
| 62 | return fs.underlyingPath(filename) |
| 63 | } |
| 64 | |
| 65 | return fullpath, err |
| 66 | } |
| 67 | |
| 68 | func (fs *ChrootHelper) resolveFollowedPath(rel string, followFinal bool, op string, sl billy.Symlink) (string, error) { |
| 69 | if rel == "" { |
| 70 | return fs.resolveFollowedRoot(followFinal, op, sl) |
| 71 | } |
| 72 | |
| 73 | parts := splitRelativePath(rel) |
| 74 | resolved := "" |
| 75 | followed := 0 |
| 76 | |
| 77 | for len(parts) > 0 { |
| 78 | part := parts[0] |
| 79 | parts = parts[1:] |
| 80 | |
| 81 | currentRel := joinRelativePath(resolved, part) |
| 82 | currentPath := fs.Join(fs.Root(), currentRel) |
| 83 | if len(parts) == 0 && !followFinal { |
| 84 | return currentPath, nil |
| 85 | } |
| 86 | |
| 87 | fi, err := sl.Lstat(currentPath) |
| 88 | if err != nil { |
| 89 | if os.IsNotExist(err) { |
| 90 | return fs.Join(fs.Root(), joinRelativePath(append([]string{currentRel}, parts...)...)), nil |
| 91 | } |
| 92 | return "", err |
| 93 | } |
| 94 | |
| 95 | if fi.Mode()&os.ModeSymlink == 0 { |
| 96 | resolved = currentRel |
| 97 | continue |
| 98 | } |
| 99 | |
| 100 | followed++ |
| 101 | if followed > maxFollowedSymlinks { |
| 102 | return "", symlinkLoopError(op, currentPath) |
| 103 | } |
| 104 | |
| 105 | target, err := sl.Readlink(currentPath) |
| 106 | if err != nil { |
| 107 | return "", err |
| 108 | } |
| 109 | |
| 110 | targetRel, err := fs.linkTargetRel(currentPath, target) |
| 111 | if err != nil { |
| 112 | return "", err |
| 113 | } |
| 114 | if targetRel == currentRel { |
| 115 | return "", symlinkLoopError(op, currentPath) |
| 116 | } |
| 117 | |
| 118 | parts = append(splitRelativePath(targetRel), parts...) |
| 119 | resolved = "" |
| 120 | } |
| 121 | |
| 122 | return fs.Join(fs.Root(), resolved), nil |
| 123 | } |
| 124 | |
| 125 | func symlinkLoopError(op, path string) error { |
| 126 | return &os.PathError{Op: op, Path: path, Err: syscall.ELOOP} |
| 127 | } |
| 128 | |
| 129 | func (fs *ChrootHelper) resolveFollowedRoot(followFinal bool, op string, sl billy.Symlink) (string, error) { |
| 130 | root := fs.Join(fs.Root(), "") |
| 131 | if !followFinal { |
| 132 | return root, nil |
| 133 | } |
| 134 | |
| 135 | fi, err := sl.Lstat(root) |
| 136 | if err != nil { |
| 137 | if os.IsNotExist(err) { |
| 138 | return root, nil |
| 139 | } |
| 140 | return "", err |
| 141 | } |
| 142 | |
| 143 | if fi.Mode()&os.ModeSymlink == 0 { |
| 144 | return root, nil |
| 145 | } |
| 146 | |
| 147 | target, err := sl.Readlink(root) |
| 148 | if err != nil { |
| 149 | return "", err |
| 150 | } |
| 151 | |
| 152 | targetRel, err := fs.linkTargetRel(root, target) |
| 153 | if err != nil { |
| 154 | return root, err |
| 155 | } |
| 156 | if targetRel == "" { |
| 157 | return "", symlinkLoopError(op, root) |
| 158 | } |
| 159 | |
| 160 | return fs.resolveFollowedPath(targetRel, followFinal, op, sl) |
| 161 | } |
| 162 | |
| 163 | func (fs *ChrootHelper) relativeToRoot(filename string) (string, error) { |
| 164 | rel, err := filepath.Rel(filepath.Clean(fs.Root()), filepath.Clean(filename)) |
| 165 | if err != nil || isCrossBoundaries(rel) { |
| 166 | return "", billy.ErrCrossedBoundary |
| 167 | } |
| 168 | |
| 169 | if rel == "." { |
| 170 | return "", nil |
| 171 | } |
| 172 | return rel, nil |
| 173 | } |
| 174 | |
| 175 | func (fs *ChrootHelper) linkTargetRel(linkPath, target string) (string, error) { |
| 176 | target = filepath.FromSlash(target) |
| 177 | if filepath.IsAbs(target) || strings.HasPrefix(target, string(filepath.Separator)) { |
| 178 | return fs.relativeToRoot(target) |
| 179 | } |
| 180 | |
| 181 | return fs.relativeToRoot(fs.Join(filepath.Dir(linkPath), target)) |
| 182 | } |
| 183 | |
| 184 | func splitRelativePath(filename string) []string { |
| 185 | filename = filepath.Clean(filename) |
| 186 | if filename == "" || filename == "." { |
| 187 | return nil |
| 188 | } |
| 189 | |
| 190 | return strings.Split(filepath.ToSlash(filename), "/") |
| 191 | } |
| 192 | |
| 193 | func joinRelativePath(elem ...string) string { |
| 194 | parts := make([]string, 0, len(elem)) |
| 195 | for _, part := range elem { |
| 196 | if part == "" || part == "." { |
| 197 | continue |
| 198 | } |
| 199 | parts = append(parts, part) |
| 200 | } |
| 201 | |
| 202 | if len(parts) == 0 { |
| 203 | return "" |
| 204 | } |
| 205 | return filepath.Join(parts...) |
| 206 | } |
| 207 | |
| 208 | func isCreateExclusive(flag int) bool { |
| 209 | return flag&os.O_CREATE != 0 && flag&os.O_EXCL != 0 |
| 210 | } |
| 211 | |
| 212 | func isCrossBoundaries(name string) bool { |
| 213 | name = filepath.ToSlash(name) |
| 214 | name = strings.TrimLeft(name, "/") |
| 215 | name = path.Clean(name) |
| 216 | |
| 217 | return name == ".." || strings.HasPrefix(name, "../") |
| 218 | } |
| 219 | |
| 220 | func (fs *ChrootHelper) Create(filename string) (billy.File, error) { |
| 221 | fullpath, err := fs.followedPath(filename, true, "create") |
| 222 | if err != nil { |
| 223 | return nil, err |
| 224 | } |
| 225 | |
| 226 | f, err := fs.underlying.Create(fullpath) |
| 227 | if err != nil { |
| 228 | return nil, err |
| 229 | } |
| 230 | |
| 231 | return newFile(fs, f, filename), nil |
| 232 | } |
| 233 | |
| 234 | func (fs *ChrootHelper) Open(filename string) (billy.File, error) { |
| 235 | fullpath, err := fs.followedPath(filename, true, "open") |
| 236 | if err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | |
| 240 | f, err := fs.underlying.Open(fullpath) |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | |
| 245 | return newFile(fs, f, filename), nil |
| 246 | } |
| 247 | |
| 248 | func (fs *ChrootHelper) OpenFile(filename string, flag int, mode fs.FileMode) (billy.File, error) { |
| 249 | fullpath, err := fs.followedPath(filename, !isCreateExclusive(flag), "open") |
| 250 | if err != nil { |
| 251 | return nil, err |
| 252 | } |
| 253 | |
| 254 | f, err := fs.underlying.OpenFile(fullpath, flag, mode) |
| 255 | if err != nil { |
| 256 | return nil, err |
| 257 | } |
| 258 | |
| 259 | return newFile(fs, f, filename), nil |
| 260 | } |
| 261 | |
| 262 | func (fs *ChrootHelper) Stat(filename string) (os.FileInfo, error) { |
| 263 | fullpath, err := fs.followedPath(filename, true, "stat") |
| 264 | if err != nil { |
| 265 | return nil, err |
| 266 | } |
| 267 | |
| 268 | fi, err := fs.underlying.Stat(fullpath) |
| 269 | if err != nil { |
| 270 | return nil, err |
| 271 | } |
| 272 | return fileInfo{FileInfo: fi, name: filepath.Base(filename)}, nil |
| 273 | } |
| 274 | |
| 275 | func (fs *ChrootHelper) Rename(from, to string) error { |
| 276 | var err error |
| 277 | from, err = fs.underlyingPath(from) |
| 278 | if err != nil { |
| 279 | return err |
| 280 | } |
| 281 | |
| 282 | to, err = fs.underlyingPath(to) |
| 283 | if err != nil { |
| 284 | return err |
| 285 | } |
| 286 | |
| 287 | return fs.underlying.Rename(from, to) |
| 288 | } |
| 289 | |
| 290 | func (fs *ChrootHelper) Remove(path string) error { |
| 291 | fullpath, err := fs.underlyingPath(path) |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | |
| 296 | return fs.underlying.Remove(fullpath) |
| 297 | } |
| 298 | |
| 299 | func (fs *ChrootHelper) Join(elem ...string) string { |
| 300 | return fs.underlying.Join(elem...) |
| 301 | } |
| 302 | |
| 303 | func (fs *ChrootHelper) TempFile(dir, prefix string) (billy.File, error) { |
| 304 | fullpath, err := fs.underlyingPath(dir) |
| 305 | if err != nil { |
| 306 | return nil, err |
| 307 | } |
| 308 | |
| 309 | tf, ok := fs.underlying.(billy.TempFile) |
| 310 | if !ok { |
| 311 | return nil, errors.New("underlying fs does not implement billy.TempFile") |
| 312 | } |
| 313 | |
| 314 | f, err := tf.TempFile(fullpath, prefix) |
| 315 | if err != nil { |
| 316 | return nil, err |
| 317 | } |
| 318 | |
| 319 | return newFile(fs, f, fs.Join(dir, filepath.Base(f.Name()))), nil |
| 320 | } |
| 321 | |
| 322 | func (fs *ChrootHelper) ReadDir(path string) ([]fs.DirEntry, error) { |
| 323 | fullpath, err := fs.followedPath(path, true, "readdir") |
| 324 | if err != nil { |
| 325 | return nil, err |
| 326 | } |
| 327 | |
| 328 | d, ok := fs.underlying.(billy.Dir) |
| 329 | if !ok { |
| 330 | return nil, errors.New("underlying fs does not implement billy.Dir") |
| 331 | } |
| 332 | return d.ReadDir(fullpath) |
| 333 | } |
| 334 | |
| 335 | func (fs *ChrootHelper) MkdirAll(filename string, perm fs.FileMode) error { |
| 336 | fullpath, err := fs.underlyingPath(filename) |
| 337 | if err != nil { |
| 338 | return err |
| 339 | } |
| 340 | |
| 341 | d, ok := fs.underlying.(billy.Dir) |
| 342 | if !ok { |
| 343 | return errors.New("underlying fs does not implement billy.Dir") |
| 344 | } |
| 345 | return d.MkdirAll(fullpath, perm) |
| 346 | } |
| 347 | |
| 348 | func (fs *ChrootHelper) Lstat(filename string) (os.FileInfo, error) { |
| 349 | fullpath, err := fs.underlyingPath(filename) |
| 350 | if err != nil { |
| 351 | return nil, err |
| 352 | } |
| 353 | |
| 354 | sl, ok := fs.underlying.(billy.Symlink) |
| 355 | if !ok { |
| 356 | return nil, errors.New("underlying fs does not implement billy.Symlink") |
| 357 | } |
| 358 | return sl.Lstat(fullpath) |
| 359 | } |
| 360 | |
| 361 | // Symlink creates link with the given target. Slashes in target are |
| 362 | // normalised to the host separator. Absolute targets are rewritten to be |
| 363 | // rooted under the chroot base before being stored, so that the link is |
| 364 | // resolvable when the chroot is reopened from a different working directory. |
| 365 | // Relative targets are stored as provided. |
| 366 | func (fs *ChrootHelper) Symlink(target, link string) error { |
| 367 | target = filepath.FromSlash(target) |
| 368 | |
| 369 | // only rewrite target if it's already absolute |
| 370 | if filepath.IsAbs(target) || strings.HasPrefix(target, string(filepath.Separator)) { |
| 371 | target = fs.Join(fs.Root(), target) |
| 372 | target = filepath.Clean(filepath.FromSlash(target)) |
| 373 | } |
| 374 | |
| 375 | link, err := fs.underlyingPath(link) |
| 376 | if err != nil { |
| 377 | return err |
| 378 | } |
| 379 | |
| 380 | sl, ok := fs.underlying.(billy.Symlink) |
| 381 | if !ok { |
| 382 | return errors.New("underlying fs does not implement billy.Symlink") |
| 383 | } |
| 384 | return sl.Symlink(target, link) |
| 385 | } |
| 386 | |
| 387 | // Readlink returns the target stored for link. |
| 388 | // |
| 389 | // Relative targets are returned unchanged, preserving the original separators |
| 390 | // as written by [ChrootHelper.Symlink] or the underlying filesystem. Absolute |
| 391 | // targets that resolve under the chroot base are translated back to be |
| 392 | // absolute relative to the chroot (using the host path separator), so callers |
| 393 | // see paths in the chroot's coordinate system rather than the underlying |
| 394 | // filesystem's. Absolute targets that resolve outside the base are returned |
| 395 | // as written. |
| 396 | func (fs *ChrootHelper) Readlink(link string) (string, error) { |
| 397 | fullpath, err := fs.underlyingPath(link) |
| 398 | if err != nil { |
| 399 | return "", err |
| 400 | } |
| 401 | |
| 402 | sl, ok := fs.underlying.(billy.Symlink) |
| 403 | if !ok { |
| 404 | return "", errors.New("underlying fs does not implement billy.Symlink") |
| 405 | } |
| 406 | |
| 407 | target, err := sl.Readlink(fullpath) |
| 408 | if err != nil { |
| 409 | return "", err |
| 410 | } |
| 411 | |
| 412 | rawTarget := target |
| 413 | target = filepath.FromSlash(target) |
| 414 | if filepath.Separator == '\\' && len(target) >= 3 && |
| 415 | target[0] == '\\' && target[2] == ':' && |
| 416 | filepath.VolumeName(fs.base) != "" { |
| 417 | target = target[1:] |
| 418 | } |
| 419 | |
| 420 | if !filepath.IsAbs(target) && !strings.HasPrefix(target, string(filepath.Separator)) { |
| 421 | return rawTarget, nil |
| 422 | } |
| 423 | |
| 424 | target, err = filepath.Rel(fs.base, target) |
| 425 | if err != nil { |
| 426 | return "", err |
| 427 | } |
| 428 | |
| 429 | return string(os.PathSeparator) + target, nil |
| 430 | } |
| 431 | |
| 432 | func (fs *ChrootHelper) Chmod(path string, mode fs.FileMode) error { |
| 433 | fullpath, err := fs.underlyingPath(path) |
| 434 | if err != nil { |
| 435 | return err |
| 436 | } |
| 437 | |
| 438 | c, ok := fs.underlying.(billy.Chmod) |
| 439 | if !ok { |
| 440 | return errors.New("underlying fs does not implement billy.Chmod") |
| 441 | } |
| 442 | return c.Chmod(fullpath, mode) |
| 443 | } |
| 444 | |
| 445 | func (fs *ChrootHelper) Chroot(path string) (billy.Filesystem, error) { |
| 446 | fullpath, err := fs.underlyingPath(path) |
| 447 | if err != nil { |
| 448 | return nil, err |
| 449 | } |
| 450 | |
| 451 | return New(fs.underlying, fullpath), nil |
| 452 | } |
| 453 | |
| 454 | func (fs *ChrootHelper) Root() string { |
| 455 | return fs.base |
| 456 | } |
| 457 | |
| 458 | func (fs *ChrootHelper) Underlying() billy.Basic { |
| 459 | return fs.underlying |
| 460 | } |
| 461 | |
| 462 | // Capabilities implements the Capable interface. |
| 463 | func (fs *ChrootHelper) Capabilities() billy.Capability { |
| 464 | return billy.Capabilities(fs.underlying) |
| 465 | } |
| 466 | |
| 467 | type file struct { |
| 468 | billy.File |
| 469 | name string |
| 470 | } |
| 471 | |
| 472 | type fileInfo struct { |
| 473 | os.FileInfo |
| 474 | name string |
| 475 | } |
| 476 | |
| 477 | func newFile(fs billy.Filesystem, f billy.File, filename string) billy.File { |
| 478 | filename = fs.Join(fs.Root(), filename) |
| 479 | filename, _ = filepath.Rel(fs.Root(), filename) |
| 480 | |
| 481 | return &file{ |
| 482 | File: f, |
| 483 | name: filename, |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | func (f *file) Name() string { |
| 488 | return f.name |
| 489 | } |
| 490 | |
| 491 | func (fi fileInfo) Name() string { |
| 492 | return fi.name |
| 493 | } |