oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package provider
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 "fmt"
8 "io"
9 "io/fs"
10 "os"
11 "path/filepath"
12 "strings"
13)
14
15// A harness installed as an npm tree (Pi; connectors spec §3, "Pinning")
16// is pinned by one digest over the whole installed tree, committed per
17// platform and checked before every launch.
18
19// ErrTree: an installed package tree is writable, has a link that leaves
20// it or an entry of another kind, or does not match its pin.
21var ErrTree = errors.New("provider: the package tree does not match its pin")
22
23// SealTree makes an installed tree read-only, with modes that do not depend
24// on the installing user's umask: directories 0555, files 0555 when npm
25// made them executable for their owner, else 0444. TreeSHA256 refuses a
26// tree that is not sealed.
27func SealTree(dir string) error {
28 var dirs []string
29 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
30 if err != nil {
31 return err
32 }
33 switch {
34 case d.IsDir():
35 dirs = append(dirs, path)
36 case d.Type().IsRegular():
37 info, err := d.Info()
38 if err != nil {
39 return err
40 }
41 mode := os.FileMode(0444)
42 if info.Mode().Perm()&0100 != 0 {
43 mode = 0555
44 }
45 return os.Chmod(path, mode)
46 }
47 return nil
48 })
49 if err != nil {
50 return err
51 }
52 // Deepest first, so a parent is sealed after its children.
53 for i := len(dirs) - 1; i >= 0; i-- {
54 if err := os.Chmod(dirs[i], 0555); err != nil {
55 return err
56 }
57 }
58 return nil
59}
60
61// TreeSHA256 names an installed tree by every entry, in walk order: a
62// directory by its path, a file by its path, mode and SHA-256, a link by
63// its path and target. A link must be relative and, followed link by
64// link, resolve to an entry inside the tree; anything writable, a file
65// with a second name (a hard link another path could write through), any
66// absolute, escaping, dangling or cyclic link and any other kind of entry
67// is refused. dir may be a retained descriptor's path (/proc/self/fd/N/.).
68func TreeSHA256(dir string) (string, error) {
69 h := sha256.New()
70 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
71 if err != nil {
72 return err
73 }
74 info, err := d.Info()
75 if err != nil {
76 return err
77 }
78 rel, _ := filepath.Rel(dir, path)
79 rel = filepath.ToSlash(rel)
80 if info.Mode()&fs.ModeSymlink == 0 && !ownedHere(info) {
81 return ErrTree
82 }
83 switch {
84 case d.IsDir():
85 if info.Mode().Perm()&0222 != 0 {
86 return ErrTree
87 }
88 fmt.Fprintf(h, "d\x00%s\x00%o\n", rel, info.Mode().Perm())
89 case info.Mode().IsRegular():
90 if info.Mode().Perm()&0222 != 0 || !singleLink(info) {
91 return ErrTree
92 }
93 sum, err := treeFileSHA256(path)
94 if err != nil {
95 return err
96 }
97 fmt.Fprintf(h, "f\x00%s\x00%o\x00%s\n", rel, info.Mode().Perm(), sum)
98 case info.Mode()&fs.ModeSymlink != 0:
99 target, err := os.Readlink(path)
100 if err != nil {
101 return err
102 }
103 if filepath.IsAbs(target) || resolveInside(dir, rel) != nil {
104 return ErrTree
105 }
106 fmt.Fprintf(h, "l\x00%s\x00%s\n", rel, filepath.ToSlash(target))
107 default:
108 return ErrTree
109 }
110 return nil
111 })
112 if err != nil {
113 return "", err
114 }
115 return hex.EncodeToString(h.Sum(nil)), nil
116}
117
118// maxLinkHops bounds a chain of links (as the kernel's own limit does).
119const maxLinkHops = 40
120
121// resolveInside follows the entry at rel (relative to root) component by
122// component, each link's target taken from where the link is, and
123// refuses a path that climbs above root, an entry that does not exist,
124// an absolute target and a chain longer than maxLinkHops (a cycle).
125func resolveInside(root, rel string) error {
126 var cur []string
127 queue := strings.Split(filepath.ToSlash(rel), "/")
128 hops := 0
129 for len(queue) > 0 {
130 c := queue[0]
131 queue = queue[1:]
132 switch c {
133 case "", ".":
134 continue
135 case "..":
136 if len(cur) == 0 {
137 return ErrTree
138 }
139 cur = cur[:len(cur)-1]
140 continue
141 }
142 next := append(append([]string(nil), cur...), c)
143 info, err := os.Lstat(filepath.Join(append([]string{root}, next...)...))
144 if err != nil {
145 return ErrTree
146 }
147 if info.Mode()&fs.ModeSymlink == 0 {
148 cur = next
149 continue
150 }
151 if hops++; hops > maxLinkHops {
152 return ErrTree
153 }
154 target, err := os.Readlink(filepath.Join(append([]string{root}, next...)...))
155 if err != nil || filepath.IsAbs(target) {
156 return ErrTree
157 }
158 queue = append(strings.Split(filepath.ToSlash(target), "/"), queue...)
159 }
160 return nil
161}
162
163func treeFileSHA256(path string) (string, error) {
164 f, err := os.Open(path)
165 if err != nil {
166 return "", err
167 }
168 defer f.Close()
169 h := sha256.New()
170 if _, err := io.Copy(h, f); err != nil {
171 return "", err
172 }
173 return hex.EncodeToString(h.Sum(nil)), nil
174}
175
176// TreeRootSafe refuses a tree that someone other than this user (or
177// root) could replace: every directory from / down to it must be a real
178// directory (no link on the way), owned by root or this user, and
179// writable by nobody else unless sticky (as /tmp is, where nobody may
180// rename another's entry). Group write is allowed only for this user's
181// own group (a user private group, Debian's default).
182func TreeRootSafe(path string) error {
183 if !filepath.IsAbs(path) || filepath.Clean(path) != path {
184 return ErrTree
185 }
186 for dir := path; ; dir = filepath.Dir(dir) {
187 info, err := os.Lstat(dir)
188 if err != nil || !info.IsDir() || !ownedHere(info) {
189 return ErrTree
190 }
191 writable := info.Mode().Perm()&0002 != 0 || info.Mode().Perm()&0020 != 0 && !ownGroup(info)
192 if writable && info.Mode()&fs.ModeSticky == 0 {
193 return ErrTree
194 }
195 if dir == "/" {
196 return nil
197 }
198 }
199}
200