| index b04b45775..41a7fde33 100644 |
| --- a/internal/edge/static.go |
| +++ b/internal/edge/static.go |
| @@ -578,8 +578,17 @@ func ExtractSite(data []byte, dst string) error { return extractSiteTar(bytes.Ne |
| // a real site that fits the ship cap expands to little more. |
| // - siteMaxFileBytes, 256 MiB for any one file: the ship cap. A static |
| // site has no business serving one file bigger than a whole release. |
| -// - siteMaxEntries, 100,000 entries (files and directories): an inode |
| -// budget, many times the largest generated docs site. |
| +// - siteMaxEntries, 100,000 inodes: an inode budget, many times the |
| +// largest generated docs site. Every inode extraction creates counts, |
| +// before it is created: each entry's own file or directory, and each |
| +// directory an entry implies (a missing parent, which extraction makes |
| +// for it). An entry that makes nothing (a pax global header, a |
| +// directory already made) still costs one, so none is free to read. |
| +// Counting archive entries alone let a few deep names make a great |
| +// many directories (O4 round 1). |
| +// - siteMaxName, 4,096 bytes for an entry's name: PATH_MAX, past which |
| +// no file could be made anyway. It bounds the work a name costs, as |
| +// the inode budget bounds the names. |
| // |
| // Vars, not consts, so a test can lower them. |
| var ( |
| @@ -588,6 +597,8 @@ var ( |
| siteMaxEntries = 100_000 |
| ) |
| |
| +const siteMaxName = 4096 |
| + |
| // errSiteTooLarge is the refusal of a tree over one of the limits above. |
| var errSiteTooLarge = errors.New("site tar: over the expanded size limit") |
| |
| @@ -595,8 +606,9 @@ var errSiteTooLarge = errors.New("site tar: over the expanded size limit") |
| // that would write outside it: absolute names, ".." traversal, and non-file |
| // entry types (a symlink in a site tar could alias the extraction outside |
| // the cache — PackDir never emits one, so none is ever legitimate). A tree |
| -// over siteMaxBytes, siteMaxFileBytes or siteMaxEntries is refused with |
| -// errSiteTooLarge; the caller removes what was written. |
| +// over siteMaxBytes, siteMaxFileBytes or siteMaxEntries, or with a name |
| +// over siteMaxName, is refused with errSiteTooLarge; the caller removes |
| +// what was written. |
| func extractSiteTar(src io.Reader, dst string) error { |
| gz, err := gzip.NewReader(src) |
| if err != nil { |
| @@ -605,7 +617,7 @@ func extractSiteTar(src io.Reader, dst string) error { |
| defer gz.Close() |
| tr := tar.NewReader(gz) |
| var total int64 |
| - entries := 0 |
| + x := siteDirs{dst: dst, made: map[string]struct{}{}} |
| for { |
| hdr, err := tr.Next() |
| if err == io.EOF { |
| @@ -614,6 +626,17 @@ func extractSiteTar(src io.Reader, dst string) error { |
| if err != nil { |
| return fmt.Errorf("site tar: %v", err) |
| } |
| + if hdr.Typeflag == tar.TypeXGlobalHeader { |
| + // pax global header: metadata, nothing to write, but an entry |
| + // to read, so it costs one like any other. |
| + if err := x.count(); err != nil { |
| + return err |
| + } |
| + continue |
| + } |
| + if len(hdr.Name) > siteMaxName { |
| + return fmt.Errorf("%w: a name of %d bytes, over %d", errSiteTooLarge, len(hdr.Name), siteMaxName) |
| + } |
| name := filepath.FromSlash(path.Clean(hdr.Name)) |
| if !filepath.IsLocal(name) { |
| return fmt.Errorf("site tar: entry %q escapes the extraction dir", hdr.Name) |
| @@ -621,30 +644,35 @@ func extractSiteTar(src io.Reader, dst string) error { |
| // hdr.Size is what a read of the entry yields (archive/tar stops |
| // there, and expands a sparse entry to it), so checking it here |
| // bounds what the copy below can write. |
| - if hdr.Typeflag != tar.TypeXGlobalHeader { |
| - if entries++; entries > siteMaxEntries { |
| - return fmt.Errorf("%w: more than %d entries", errSiteTooLarge, siteMaxEntries) |
| + switch hdr.Typeflag { |
| + case tar.TypeDir: |
| + made, err := x.mkdirs(name) |
| + if err != nil { |
| + return err |
| } |
| - } |
| - if hdr.Typeflag == tar.TypeReg { |
| + // An entry naming a directory already made (or dst itself) |
| + // still costs one, so no entry is free to read. |
| + if made == 0 { |
| + if err := x.count(); err != nil { |
| + return err |
| + } |
| + } |
| + case tar.TypeReg: |
| if hdr.Size > siteMaxFileBytes { |
| return fmt.Errorf("%w: %q is %d bytes, over %d", errSiteTooLarge, hdr.Name, hdr.Size, siteMaxFileBytes) |
| } |
| if total += hdr.Size; total > siteMaxBytes { |
| return fmt.Errorf("%w: more than %d bytes in all", errSiteTooLarge, siteMaxBytes) |
| } |
| - } |
| - switch hdr.Typeflag { |
| - case tar.TypeDir: |
| - if err := os.MkdirAll(filepath.Join(dst, name), 0o755); err != nil { |
| + if _, err := x.mkdirs(filepath.Dir(name)); err != nil { |
| return err |
| } |
| - case tar.TypeReg: |
| - p := filepath.Join(dst, name) |
| - if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| + // The file's own inode. A name written twice counts twice, |
| + // which only errs toward refusing. |
| + if err := x.count(); err != nil { |
| return err |
| } |
| - f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) |
| + f, err := os.OpenFile(filepath.Join(dst, name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) |
| if err != nil { |
| return err |
| } |
| @@ -655,14 +683,65 @@ func extractSiteTar(src io.Reader, dst string) error { |
| if err := f.Close(); err != nil { |
| return err |
| } |
| - case tar.TypeXGlobalHeader: |
| - // pax global header: metadata, nothing to write |
| default: |
| return fmt.Errorf("site tar: entry %q has unsupported type %d", hdr.Name, hdr.Typeflag) |
| } |
| } |
| } |
| |
| +// siteDirs is extractSiteTar's inode budget and the directories it has |
| +// made under dst, so each is counted once, when it is made. |
| +type siteDirs struct { |
| + dst string |
| + made map[string]struct{} |
| + inodes int |
| +} |
| + |
| +// count charges one inode to the budget, refusing past siteMaxEntries. |
| +func (x *siteDirs) count() error { |
| + if x.inodes++; x.inodes > siteMaxEntries { |
| + return fmt.Errorf("%w: more than %d files and directories", errSiteTooLarge, siteMaxEntries) |
| + } |
| + return nil |
| +} |
| + |
| +// mkdirs makes rel (local, cleaned) and each missing parent under x.dst, |
| +// top down, charging each to the budget before it is made. The walk up |
| +// stops at the first directory already made, whose parents were made with |
| +// it, so a directory costs one lookup per entry under it and its making |
| +// once; the name cap bounds the walk. made holds at most siteMaxEntries |
| +// keys of at most siteMaxName bytes each. It returns how many it made. |
| +func (x *siteDirs) mkdirs(rel string) (int, error) { |
| + var missing []string |
| + for d := rel; d != "."; d = filepath.Dir(d) { |
| + if _, ok := x.made[d]; ok { |
| + break |
| + } |
| + missing = append(missing, d) |
| + } |
| + for i := len(missing) - 1; i >= 0; i-- { |
| + if err := x.count(); err != nil { |
| + return 0, err |
| + } |
| + if err := os.Mkdir(filepath.Join(x.dst, missing[i]), 0o755); err != nil && !existingDir(filepath.Join(x.dst, missing[i]), err) { |
| + return 0, err |
| + } |
| + x.made[missing[i]] = struct{}{} |
| + } |
| + return len(missing), nil |
| +} |
| + |
| +// existingDir reports whether err, from a Mkdir of p, says only that p is |
| +// already a directory: dst itself may be one ExtractSite's caller made |
| +// with content, and MkdirAll accepted that before. |
| +func existingDir(p string, err error) bool { |
| + if !errors.Is(err, fs.ErrExist) { |
| + return false |
| + } |
| + fi, serr := os.Lstat(p) |
| + return serr == nil && fi.IsDir() |
| +} |
| + |
| // serveTree answers one request from an extracted version dir: |
| // http.FileServer semantics with index.html for directories and a clean 404 |
| // (never a directory listing) when neither the file nor an index exists — |