TL;DR

  • For a background process that runs from boot, a root helper writes a plist into /Library/LaunchDaemons.
  • The helper copied the run-as user from its argument into the plist’s UserName. Write root and it runs as root.
  • It now accepts the user only when the UID equals SUDO_UID and the name matches that UID’s account.

Picture a Mac whose sudoers has just this one line added:

alice ALL=(root) NOPASSWD: /usr/local/bin/agentd server bootstrap-daemon *

The admin means “let alice register the background process, nothing else”. But this helper gave alice the power to run any program as root, at every boot.

Splitting only the registration into root, so it survives reboots

I run a background process (the daemon below) on Macs, and it starts the agents. The daemon runs with the permissions of the account that set it up.

If it only needs to run after login, a LaunchAgent in ~/Library/LaunchAgents is enough, with no root. But a LaunchAgent doesn’t start until someone logs in. After a reboot, on a Mac where nobody has logged in, it doesn’t run.

So I use a LaunchDaemon. A plist in /Library/LaunchDaemons is loaded at boot, and runs as the user named by its UserName key. Writing to that directory needs root.

Setup has two stages. The normal account builds the settings, and only the step that needs root goes to the helper.

Admins who don’t want to hand out full sudo can allow only this helper, as in the line above.

Writing root into the spec gave a root daemon

The problem was that the helper trusted the spec. User and UID went straight into the plist’s UserName, and Binary into ProgramArguments.

alice can build this spec:

{"User": "root", "UID": "0", "Binary": "/Users/alice/tmp/anything"}

The helper writes it as is, and launchd runs alice’s binary as root at every boot. The permission the sudoers line was meant to narrow becomes full root.

The daemon starts agents. The tools an agent calls run as alice too. With a NOPASSWD rule, a single tool call can do this registration.

sudoers argument patterns cannot constrain what is inside the spec

My first thought was to narrow the arguments in sudoers. sudoers allows wildcards and regular expressions in arguments. But the spec is base64-encoded JSON. “User is the caller” cannot be written as a string pattern.

Apple’s recommended shape for a privileged helper is to bundle it in an app, register it with SMAppService, and check the caller’s code signature over XPC. That assumes an app bundle. It doesn’t fit this path, where setup is a CLI run over SSH.

What remains is for the helper itself to know who asked, and check the spec against that.

Accepting only the account that matches SUDO_UID

sudo puts SUDO_UID, the invoking user’s UID, into the command’s environment. Under the default env_reset, sudo sets SUDO_* from the invoking user, and the caller cannot change them. The exception is a rule with SETENV, which is implied for rules that allow ALL. Such users can already become root, so they are not who this helper protects against.

The helper checks the spec with three conditions:

func checkSpec(s spec, sudoUID string) error {
	if s.UID == "0" || s.User == "root" {
		return errors.New("must not run as root")
	}
	if sudoUID == "" || s.UID != sudoUID {
		return errors.New("must run as the account that invoked sudo")
	}
	account, err := user.LookupId(s.UID)
	if err != nil {
		return err
	}
	if account.Username != s.User {
		return errors.New("spec does not match its account")
	}
	return nil
}
  • UID 0 and root are rejected regardless of SUDO_UID. Even when root calls the helper directly, there is no reason to run the daemon as root.
  • A UID that differs from SUDO_UID is rejected. Called without sudo, SUDO_UID is empty, so that is rejected too.
  • The user name must equal the real account name for that UID. The plist’s UserName holds a name, so this stops a spec that matches the UID but names root.

Binary stays free. alice’s binary runs with alice’s permissions, which is nothing alice couldn’t do already.

The same idea narrowed which files the helper deletes. When removing an old LaunchAgent plist, it used to use the spec’s Home. Now it only looks in the checked account’s home directory. A helper running as root no longer deletes a path the caller chose.

On Linux too, the daemon doesn’t run as root

On Linux the daemon runs as the account that set it up. Set it up as root, and the daemon and the agents it starts run as root.

macOS already refused root setups. Linux now refuses new ones too. A daemon already registered as root can still update. Refusing everything would leave existing machines stuck on their current version.