package provider import ( "crypto/sha256" "encoding/hex" "errors" "fmt" "io" "io/fs" "os" "path/filepath" "strings" ) // A harness installed as an npm tree (Pi; connectors spec ยง3, "Pinning") // is pinned by one digest over the whole installed tree, committed per // platform and checked before every launch. // ErrTree: an installed package tree is writable, has a link that leaves // it or an entry of another kind, or does not match its pin. var ErrTree = errors.New("provider: the package tree does not match its pin") // SealTree makes an installed tree read-only, with modes that do not depend // on the installing user's umask: directories 0555, files 0555 when npm // made them executable for their owner, else 0444. TreeSHA256 refuses a // tree that is not sealed. func SealTree(dir string) error { var dirs []string err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } switch { case d.IsDir(): dirs = append(dirs, path) case d.Type().IsRegular(): info, err := d.Info() if err != nil { return err } mode := os.FileMode(0444) if info.Mode().Perm()&0100 != 0 { mode = 0555 } return os.Chmod(path, mode) } return nil }) if err != nil { return err } // Deepest first, so a parent is sealed after its children. for i := len(dirs) - 1; i >= 0; i-- { if err := os.Chmod(dirs[i], 0555); err != nil { return err } } return nil } // TreeSHA256 names an installed tree by every entry, in walk order: a // directory by its path, a file by its path, mode and SHA-256, a link by // its path and target. A link must be relative and, followed link by // link, resolve to an entry inside the tree; anything writable, a file // with a second name (a hard link another path could write through), any // absolute, escaping, dangling or cyclic link and any other kind of entry // is refused. dir may be a retained descriptor's path (/proc/self/fd/N/.). func TreeSHA256(dir string) (string, error) { h := sha256.New() err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } info, err := d.Info() if err != nil { return err } rel, _ := filepath.Rel(dir, path) rel = filepath.ToSlash(rel) if info.Mode()&fs.ModeSymlink == 0 && !ownedHere(info) { return ErrTree } switch { case d.IsDir(): if info.Mode().Perm()&0222 != 0 { return ErrTree } fmt.Fprintf(h, "d\x00%s\x00%o\n", rel, info.Mode().Perm()) case info.Mode().IsRegular(): if info.Mode().Perm()&0222 != 0 || !singleLink(info) { return ErrTree } sum, err := treeFileSHA256(path) if err != nil { return err } fmt.Fprintf(h, "f\x00%s\x00%o\x00%s\n", rel, info.Mode().Perm(), sum) case info.Mode()&fs.ModeSymlink != 0: target, err := os.Readlink(path) if err != nil { return err } if filepath.IsAbs(target) || resolveInside(dir, rel) != nil { return ErrTree } fmt.Fprintf(h, "l\x00%s\x00%s\n", rel, filepath.ToSlash(target)) default: return ErrTree } return nil }) if err != nil { return "", err } return hex.EncodeToString(h.Sum(nil)), nil } // maxLinkHops bounds a chain of links (as the kernel's own limit does). const maxLinkHops = 40 // resolveInside follows the entry at rel (relative to root) component by // component, each link's target taken from where the link is, and // refuses a path that climbs above root, an entry that does not exist, // an absolute target and a chain longer than maxLinkHops (a cycle). func resolveInside(root, rel string) error { var cur []string queue := strings.Split(filepath.ToSlash(rel), "/") hops := 0 for len(queue) > 0 { c := queue[0] queue = queue[1:] switch c { case "", ".": continue case "..": if len(cur) == 0 { return ErrTree } cur = cur[:len(cur)-1] continue } next := append(append([]string(nil), cur...), c) info, err := os.Lstat(filepath.Join(append([]string{root}, next...)...)) if err != nil { return ErrTree } if info.Mode()&fs.ModeSymlink == 0 { cur = next continue } if hops++; hops > maxLinkHops { return ErrTree } target, err := os.Readlink(filepath.Join(append([]string{root}, next...)...)) if err != nil || filepath.IsAbs(target) { return ErrTree } queue = append(strings.Split(filepath.ToSlash(target), "/"), queue...) } return nil } func treeFileSHA256(path string) (string, error) { f, err := os.Open(path) if err != nil { return "", err } defer f.Close() h := sha256.New() if _, err := io.Copy(h, f); err != nil { return "", err } return hex.EncodeToString(h.Sum(nil)), nil } // TreeRootSafe refuses a tree that someone other than this user (or // root) could replace: every directory from / down to it must be a real // directory (no link on the way), owned by root or this user, and // writable by nobody else unless sticky (as /tmp is, where nobody may // rename another's entry). Group write is allowed only for this user's // own group (a user private group, Debian's default). func TreeRootSafe(path string) error { if !filepath.IsAbs(path) || filepath.Clean(path) != path { return ErrTree } for dir := path; ; dir = filepath.Dir(dir) { info, err := os.Lstat(dir) if err != nil || !info.IsDir() || !ownedHere(info) { return ErrTree } writable := info.Mode().Perm()&0002 != 0 || info.Mode().Perm()&0020 != 0 && !ownGroup(info) if writable && info.Mode()&fs.ModeSticky == 0 { return ErrTree } if dir == "/" { return nil } } }