|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "os/user" |
| 9 | + "runtime" |
| 10 | + "strconv" |
| 11 | + "syscall" |
| 12 | +) |
| 13 | + |
| 14 | +// privDrop is the macOS half of the helper: it drops the child command back to the |
| 15 | +// human user so `plug <cmd>` never runs your command as root. |
| 16 | +// |
| 17 | +// On macOS plug is installed as a setuid-root helper (`chown root` + `chmod u+s`, |
| 18 | +// posted once at install). Launching it gives the process euid 0 — enough to create |
| 19 | +// the utun and repoint the system resolver — while the REAL uid stays the user's. |
| 20 | +// Unlike Linux file capabilities, which are dropped for free because they don't |
| 21 | +// survive an exec, a setuid-root euid IS inherited across exec: without an explicit |
| 22 | +// drop, the child (`npm run …`, your app) would run as root. That would wreck file |
| 23 | +// ownership in your working tree and is exactly the kind of surprise we refuse to |
| 24 | +// ship. So the child is spawned under the user's own credentials instead. |
| 25 | + |
| 26 | +// applyPrivDrop makes cmd spawn as the human user when plug is running privileged. |
| 27 | +// No-op when there is nothing to drop (see resolveDropTarget), so it's inert on the |
| 28 | +// Linux capabilities path and on a genuine root login. |
| 29 | +func applyPrivDrop(cmd *exec.Cmd) { |
| 30 | + uid, gid, ok := resolveDropTarget(os.Geteuid(), os.Getuid(), os.Getgid(), |
| 31 | + os.Getenv("SUDO_UID"), os.Getenv("SUDO_GID")) |
| 32 | + if !ok { |
| 33 | + return |
| 34 | + } |
| 35 | + if cmd.SysProcAttr == nil { |
| 36 | + cmd.SysProcAttr = &syscall.SysProcAttr{} |
| 37 | + } |
| 38 | + cred := &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)} |
| 39 | + // Carry the user's supplementary groups (staff, admin, _developer, …). Without |
| 40 | + // this the child would keep root's groups, which can break group-owned access. |
| 41 | + if groups := supplementaryGroups(uid); groups != nil { |
| 42 | + cred.Groups = groups |
| 43 | + } |
| 44 | + cmd.SysProcAttr.Credential = cred |
| 45 | +} |
| 46 | + |
| 47 | +// resolveDropTarget decides which uid/gid the child must drop to, from the process |
| 48 | +// ids and sudo's record of the invoker. ok is false when there is nothing to drop: |
| 49 | +// - euid != 0 → unprivileged (Linux caps path); leave the child untouched. |
| 50 | +// - real uid is 0 → launched as genuine root with no SUDO_UID; don't guess a |
| 51 | +// user to become — run the child as root, as the user explicitly asked. |
| 52 | +// |
| 53 | +// The two privileged-but-droppable cases both yield ok == true: |
| 54 | +// - setuid-root (euid 0, real uid = the user) → drop to the real uid (macOS). |
| 55 | +// - `sudo plug` (euid 0, real uid 0) → drop to SUDO_UID/SUDO_GID. |
| 56 | +func resolveDropTarget(euid, ruid, rgid int, sudoUID, sudoGID string) (uid, gid int, ok bool) { |
| 57 | + if euid != 0 { |
| 58 | + return 0, 0, false |
| 59 | + } |
| 60 | + uid, gid = ruid, rgid |
| 61 | + if uid == 0 { |
| 62 | + uid = atoiOr(sudoUID, 0) |
| 63 | + gid = atoiOr(sudoGID, 0) |
| 64 | + } |
| 65 | + if uid == 0 { |
| 66 | + return 0, 0, false |
| 67 | + } |
| 68 | + return uid, gid, true |
| 69 | +} |
| 70 | + |
| 71 | +// supplementaryGroups returns uid's group memberships as a syscall.Credential |
| 72 | +// group list, or nil if they can't be resolved (the caller then leaves Groups nil, |
| 73 | +// which clears root's groups rather than leaking them). |
| 74 | +func supplementaryGroups(uid int) []uint32 { |
| 75 | + u, err := user.LookupId(strconv.Itoa(uid)) |
| 76 | + if err != nil { |
| 77 | + return nil |
| 78 | + } |
| 79 | + gids, err := u.GroupIds() |
| 80 | + if err != nil { |
| 81 | + return nil |
| 82 | + } |
| 83 | + var out []uint32 |
| 84 | + for _, g := range gids { |
| 85 | + if n, err := strconv.Atoi(g); err == nil { |
| 86 | + out = append(out, uint32(n)) |
| 87 | + } |
| 88 | + } |
| 89 | + return out |
| 90 | +} |
| 91 | + |
| 92 | +// chownToUser gives path back to the human user when plug runs as the macOS |
| 93 | +// setuid helper (euid 0). Files plug writes under the user's ~/.plug — the version |
| 94 | +// cache, a pinned known_hosts — would otherwise land root-owned, so the user could |
| 95 | +// neither clean nor update them without sudo. No-op when unprivileged (see |
| 96 | +// resolveDropTarget), so the Linux caps path is untouched. |
| 97 | +func chownToUser(path string) { |
| 98 | + uid, gid, ok := resolveDropTarget(os.Geteuid(), os.Getuid(), os.Getgid(), |
| 99 | + os.Getenv("SUDO_UID"), os.Getenv("SUDO_GID")) |
| 100 | + if !ok { |
| 101 | + return |
| 102 | + } |
| 103 | + _ = os.Chown(path, uid, gid) |
| 104 | +} |
| 105 | + |
| 106 | +// preserveHelperPrivilege re-applies the macOS setuid-root bit to a freshly written |
| 107 | +// launcher, so `plug self-update` doesn't silently disable the helper — a rename |
| 108 | +// installs a new inode that has lost the bit. Only on macOS, and only when we're |
| 109 | +// already root (the setuid helper is), so it can run without a sudo prompt and a |
| 110 | +// capability-based Linux install is never turned into a setuid one. |
| 111 | +func preserveHelperPrivilege(path string) { |
| 112 | + if runtime.GOOS != "darwin" || os.Geteuid() != 0 { |
| 113 | + return |
| 114 | + } |
| 115 | + _ = os.Chown(path, 0, 0) |
| 116 | + _ = os.Chmod(path, 0o755|os.ModeSetuid) |
| 117 | +} |
| 118 | + |
| 119 | +func atoiOr(s string, def int) int { |
| 120 | + if n, err := strconv.Atoi(s); err == nil { |
| 121 | + return n |
| 122 | + } |
| 123 | + return def |
| 124 | +} |
0 commit comments