| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "syscall" |
| 11 | ) |
| 12 | |
| 13 | // The runner user's DARWIN_USER_TEMP_DIR ("T"). Xcode writes there |
| 14 | // whatever $TMPDIR says: Foundation's temporary directory under Seatbelt |
| 15 | // (Swift Build's swbuild.tmp.*, swift-driver's TemporaryDirectory.*) and |
| 16 | // NSItemReplacementDirectory's TemporaryItems/NSIRD_* (atomic saves of |
| 17 | // logs and result bundles). The checks profile lets a gate write it |
| 18 | // (the coordinator's ruling); this file finds it, refuses one it cannot |
| 19 | // trust, and removes what an attempt left there. |
| 20 | |
| 21 | // UserTempPrefix is where macOS keeps every user's T. |
| 22 | const UserTempPrefix = "/private/var/folders" |
| 23 | |
| 24 | // ErrUserTemp says T was not found or is not the runner user's own. |
| 25 | var ErrUserTemp = errors.New("mac: user temp folder refused") |
| 26 | |
| 27 | // UserTemp resolves the runner user's T: `getconf DARWIN_USER_TEMP_DIR`, |
| 28 | // its symlinks resolved (/var is /private/var), no trailing slash. It |
| 29 | // must be a directory under prefix (UserTempPrefix), owned by uid, mode |
| 30 | // 0700; otherwise the error says why and no checks profile can be made. |
| 31 | func UserTemp(ctx context.Context, x Exec, uid int, prefix string) (string, error) { |
| 32 | ctx, cancel := context.WithTimeout(ctx, QueryLimit) |
| 33 | defer cancel() |
| 34 | out, err := x(ctx, "/usr/bin/getconf", "DARWIN_USER_TEMP_DIR") |
| 35 | raw := strings.TrimSpace(out) |
| 36 | if err != nil || raw == "" || !filepath.IsAbs(raw) { |
| 37 | return "", refuse("getconf did not name it") |
| 38 | } |
| 39 | path, err := filepath.EvalSymlinks(raw) |
| 40 | if err != nil { |
| 41 | return "", refuse("it does not exist") |
| 42 | } |
| 43 | path = filepath.Clean(path) |
| 44 | if !within(prefix, path) || path == prefix || !literalPath(path) { |
| 45 | return "", refuse("it is not under the system's user folders") |
| 46 | } |
| 47 | info, err := os.Lstat(path) |
| 48 | if err != nil || !info.IsDir() { |
| 49 | return "", refuse("it is not a directory") |
| 50 | } |
| 51 | st, ok := info.Sys().(*syscall.Stat_t) |
| 52 | if !ok || int(st.Uid) != uid { |
| 53 | return "", refuse("another user owns it") |
| 54 | } |
| 55 | if info.Mode().Perm() != 0700 { |
| 56 | return "", refuse("its mode is not 0700") |
| 57 | } |
| 58 | return path, nil |
| 59 | } |
| 60 | |
| 61 | // UserTempError is a refused T, with why in words (no path). |
| 62 | type UserTempError struct{ Why string } |
| 63 | |
| 64 | func (e *UserTempError) Error() string { return ErrUserTemp.Error() + ": " + e.Why } |
| 65 | func (e *UserTempError) Is(target error) bool { return target == ErrUserTemp } |
| 66 | |
| 67 | func refuse(why string) error { return &UserTempError{Why: why} } |
| 68 | |
| 69 | // tempItems is NSItemReplacementDirectory's folder in T. |
| 70 | const tempItems = "TemporaryItems" |
| 71 | |
| 72 | // tempEntry is one name in T or T/TemporaryItems and the file it was. |
| 73 | type tempEntry struct { |
| 74 | Dev uint64 `json:"dev"` |
| 75 | Ino uint64 `json:"ino"` |
| 76 | Kind fs.FileMode `json:"kind"` |
| 77 | } |
| 78 | |
| 79 | func entryOf(info fs.FileInfo) tempEntry { |
| 80 | e := tempEntry{Kind: info.Mode().Type()} |
| 81 | if st, ok := info.Sys().(*syscall.Stat_t); ok { |
| 82 | e.Dev, e.Ino = uint64(st.Dev), uint64(st.Ino) |
| 83 | } |
| 84 | return e |
| 85 | } |
| 86 | |
| 87 | // tempListing is what was in T (keys "name") and T/TemporaryItems (keys |
| 88 | // "TemporaryItems/name") right before an attempt. |
| 89 | type tempListing map[string]tempEntry |
| 90 | |
| 91 | // tempState is T held open for one attempt: the directory listed, by |
| 92 | // handle (os.Root), so a T replaced meanwhile (by a symlink, say) is |
| 93 | // never what the cleanup reads or removes in; and the same for |
| 94 | // T/TemporaryItems, held only when it was a real directory. |
| 95 | type tempState struct { |
| 96 | root, items *os.Root |
| 97 | before tempListing |
| 98 | } |
| 99 | |
| 100 | // openTemp opens T for an attempt and lists it. T must still be a real |
| 101 | // directory (not a symlink), this user's own, mode 0700, and the |
| 102 | // directory opened must be the one at the path. |
| 103 | func openTemp(dir string, before tempListing) (*tempState, error) { |
| 104 | info, err := os.Lstat(dir) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | if st, ok := info.Sys().(*syscall.Stat_t); !info.IsDir() || !ok || int(st.Uid) != os.Getuid() || info.Mode().Perm() != 0700 { |
| 109 | return nil, ErrUserTemp |
| 110 | } |
| 111 | root, err := os.OpenRoot(dir) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | s := &tempState{root: root, before: before} |
| 116 | if self, err := root.Stat("."); err != nil || entryOf(self) != entryOf(info) { |
| 117 | s.close() |
| 118 | return nil, ErrUserTemp |
| 119 | } |
| 120 | now, err := list(root, "") |
| 121 | if err != nil { |
| 122 | s.close() |
| 123 | return nil, err |
| 124 | } |
| 125 | // TemporaryItems by handle, and only the directory that was there |
| 126 | // before (a fresh listing: the one there now). |
| 127 | want, had := now[tempItems], true |
| 128 | if before != nil { |
| 129 | want, had = before[tempItems] |
| 130 | } |
| 131 | if items, ok := now[tempItems]; ok && had && items == want && items.Kind == fs.ModeDir { |
| 132 | if s.items, err = root.OpenRoot(tempItems); err == nil { |
| 133 | if self, err := s.items.Stat("."); err != nil || entryOf(self) != items { |
| 134 | s.items.Close() |
| 135 | s.items = nil |
| 136 | } |
| 137 | } else if !errors.Is(err, fs.ErrNotExist) { |
| 138 | s.close() |
| 139 | return nil, err |
| 140 | } |
| 141 | } |
| 142 | if s.items != nil { |
| 143 | in, err := list(s.items, tempItems+"/") |
| 144 | if err != nil { |
| 145 | s.close() |
| 146 | return nil, err |
| 147 | } |
| 148 | for k, v := range in { |
| 149 | now[k] = v |
| 150 | } |
| 151 | } |
| 152 | if s.before == nil { |
| 153 | s.before = now |
| 154 | } |
| 155 | return s, nil |
| 156 | } |
| 157 | |
| 158 | func (s *tempState) close() { |
| 159 | if s.items != nil { |
| 160 | s.items.Close() |
| 161 | } |
| 162 | s.root.Close() |
| 163 | } |
| 164 | |
| 165 | // list names what is in dir (by handle), keyed with prefix. A name gone |
| 166 | // between the listing and the look is not there; any other error is. |
| 167 | func list(dir *os.Root, prefix string) (tempListing, error) { |
| 168 | f, err := dir.Open(".") |
| 169 | if err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | names, err := f.Readdirnames(-1) |
| 173 | f.Close() |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | l := tempListing{} |
| 178 | for _, n := range names { |
| 179 | info, err := dir.Lstat(n) |
| 180 | if errors.Is(err, fs.ErrNotExist) { |
| 181 | continue |
| 182 | } |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | l[prefix+n] = entryOf(info) |
| 187 | } |
| 188 | return l, nil |
| 189 | } |
| 190 | |
| 191 | // clean removes what appeared in T and T/TemporaryItems since before: |
| 192 | // every name not listed then, or listed as another file (same name, |
| 193 | // other device, file number or kind). Each name is removed by the handle of |
| 194 | // its own directory, one path element, so no symlink is followed: a |
| 195 | // symlink goes as itself. A folder the attempt made unreadable is made |
| 196 | // the owner's again to be removed. What vanishes meanwhile is not an |
| 197 | // error; anything else is. |
| 198 | func (s *tempState) clean() error { |
| 199 | var errs []error |
| 200 | for _, d := range []struct { |
| 201 | root *os.Root |
| 202 | prefix string |
| 203 | }{{s.root, ""}, {s.items, tempItems + "/"}} { |
| 204 | if d.root == nil { |
| 205 | continue |
| 206 | } |
| 207 | now, err := list(d.root, d.prefix) |
| 208 | if d.prefix != "" && errors.Is(err, fs.ErrNotExist) { |
| 209 | // TemporaryItems itself is gone (removed while the run |
| 210 | // lasted): nothing is left in it to clean. |
| 211 | continue |
| 212 | } |
| 213 | if err != nil { |
| 214 | errs = append(errs, err) |
| 215 | continue |
| 216 | } |
| 217 | for name, e := range now { |
| 218 | if old, ok := s.before[name]; ok && old == e { |
| 219 | continue |
| 220 | } |
| 221 | if err := remove(d.root, strings.TrimPrefix(name, d.prefix)); err != nil { |
| 222 | errs = append(errs, err) |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return errors.Join(errs...) |
| 227 | } |
| 228 | |
| 229 | // remove removes name in dir and all below it, never through a symlink; |
| 230 | // a folder that cannot be read or written is given back to its owner |
| 231 | // (this user) first. |
| 232 | func remove(dir *os.Root, name string) error { |
| 233 | err := dir.RemoveAll(name) |
| 234 | if err == nil || errors.Is(err, fs.ErrNotExist) { |
| 235 | return nil |
| 236 | } |
| 237 | if info, lerr := dir.Lstat(name); lerr == nil && info.IsDir() { |
| 238 | _ = unlock(dir, name, 0) |
| 239 | } |
| 240 | if err := dir.RemoveAll(name); err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 241 | return err |
| 242 | } |
| 243 | return nil |
| 244 | } |
| 245 | |
| 246 | // unlock makes the folder name (and the folders below it) 0700 again, |
| 247 | // opening each by handle so no symlink is followed. |
| 248 | func unlock(dir *os.Root, name string, depth int) error { |
| 249 | if depth > 64 { |
| 250 | return errors.New("mac: too deep") |
| 251 | } |
| 252 | if err := dir.Chmod(name, 0700); err != nil { |
| 253 | return err |
| 254 | } |
| 255 | sub, err := dir.OpenRoot(name) |
| 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 | defer sub.Close() |
| 260 | in, err := list(sub, "") |
| 261 | if err != nil { |
| 262 | return err |
| 263 | } |
| 264 | for n, e := range in { |
| 265 | if e.Kind == fs.ModeDir { |
| 266 | _ = unlock(sub, n, depth+1) |
| 267 | } |
| 268 | } |
| 269 | return nil |
| 270 | } |
| 271 | |