fix: harden system dirs at install; keep sudo attribution without passwd

Address remaining review comments:

- shim: force root:root 0755 on the managed system dirs (shim tree and
  profile.d) after MkdirAll. A pre-created dir with weaker ownership,
  possible under Debian's group-writable /usr/local/lib, would let a
  non-root user replace the shims every account executes.

- audit: when SUDO_USER has no passwd entry (minimal containers),
  attribute cloud events from sudo's recorded SUDO_USER/SUDO_UID env
  instead of falling back to root. Still gated on euid 0.

- setup: reword the root-without---system warning; alias/shim install
  follows HOME, so claiming it configures only root's home was wrong.

- shim: skip the non-root-owner validation test on Windows, where file
  ownership is not resolvable.
This commit is contained in:
Sahilb315
2026-07-14 13:24:08 +05:30
parent 4b2a25a378
commit 748d40c14f
6 changed files with 51 additions and 1 deletions
+21
View File
@@ -207,6 +207,24 @@ func ValidateSystemBinary(path string) error {
return validateSystemExecutable(path)
}
// secureSystemDir forces root ownership and 0755 on a directory pmg manages
// system-wide. MkdirAll leaves pre-existing directories untouched, so a dir
// pre-created with weaker ownership (possible under Debian's group-writable
// /usr/local/lib) would let a non-root user replace shims; this closes that
// hole. No-op when not running as root (unit tests, dry contexts).
func secureSystemDir(path string) error {
if os.Geteuid() != 0 {
return nil
}
if err := os.Chown(path, 0, 0); err != nil {
return fmt.Errorf("failed to set root ownership on %s: %w", path, err)
}
if err := os.Chmod(path, 0o755); err != nil {
return fmt.Errorf("failed to set permissions on %s: %w", path, err)
}
return nil
}
func shimsPresent(dir string) bool {
_, ok := firstShimContent(dir)
return ok
@@ -246,6 +264,9 @@ func writeSystemProfile(binDir string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("failed to create profile.d directory: %w", err)
}
if err := secureSystemDir(filepath.Dir(path)); err != nil {
return err
}
content := fmt.Sprintf(`# %s - managed by pmg setup install --system
# remove by running: pmg setup remove --system