Skip to main content

Agent hardening

osapi can run the agent as a user without privileges. Then a configuration setting gives privilege escalation for write operations. Reads run as the user of the agent. Writes use sudo when you configure it. Linux capabilities give access to files without a full sudo setup. When you enable one of these options, the agent verifies the configuration automatically when it starts, before it accepts jobs.

When you enable none of these options, the agent runs commands as its current user, root or a different user.

Configuration​

KeyTypeDefaultEnvironmentDescription
agent.privilege_escalation.enabledboolfalseOSAPI_AGENT_PRIVILEGE_ESCALATION_ENABLEDLeast-privilege mode: sudo for write commands and capability checks at startup.

How it works​

The exec manager has two ways to run a command:

  • RunCmd runs the command as the current user of the agent. All read operations use it, for example to list services, read kernel parameters or get the package state.
  • RunPrivilegedCmd runs the command with sudo in front of it when privilege_escalation.enabled: true. When the setting is disabled, it is the same as RunCmd.

Providers call RunCmd for reads and RunPrivilegedCmd for writes. The providers do not know if sudo is enabled. The exec manager controls it.

// Read — always unprivileged
output, _ := d.execManager.RunCmd("systemctl", []string{"is-active", name})

// Write — elevated when configured
_, err := d.execManager.RunPrivilegedCmd(
"systemctl", []string{"start", name})

Sudoers drop-in​

Make /etc/sudoers.d/osapi-agent with this content. Then the osapi system user can run write commands without a password:

# Service management
osapi ALL=(root) NOPASSWD: /usr/bin/systemctl start *
osapi ALL=(root) NOPASSWD: /usr/bin/systemctl stop *
osapi ALL=(root) NOPASSWD: /usr/bin/systemctl restart *
osapi ALL=(root) NOPASSWD: /usr/bin/systemctl enable *
osapi ALL=(root) NOPASSWD: /usr/bin/systemctl disable *
osapi ALL=(root) NOPASSWD: /usr/bin/systemctl daemon-reload

# Kernel parameters
osapi ALL=(root) NOPASSWD: /usr/sbin/sysctl -p *
osapi ALL=(root) NOPASSWD: /usr/sbin/sysctl --system

# Timezone
osapi ALL=(root) NOPASSWD: /usr/bin/timedatectl set-timezone *

# Hostname
osapi ALL=(root) NOPASSWD: /usr/bin/hostnamectl set-hostname *

# NTP
osapi ALL=(root) NOPASSWD: /usr/bin/chronyc reload sources

# User and group management
osapi ALL=(root) NOPASSWD: /usr/sbin/useradd *
osapi ALL=(root) NOPASSWD: /usr/sbin/usermod *
osapi ALL=(root) NOPASSWD: /usr/sbin/userdel *
osapi ALL=(root) NOPASSWD: /usr/sbin/groupadd *
osapi ALL=(root) NOPASSWD: /usr/sbin/groupdel *
osapi ALL=(root) NOPASSWD: /usr/bin/gpasswd *
osapi ALL=(root) NOPASSWD: /usr/bin/chown *
osapi ALL=(root) NOPASSWD: /bin/sh -c echo *

# Package management
osapi ALL=(root) NOPASSWD: /usr/bin/apt-get install *
osapi ALL=(root) NOPASSWD: /usr/bin/apt-get remove *
osapi ALL=(root) NOPASSWD: /usr/bin/apt-get update

# Certificate trust store
osapi ALL=(root) NOPASSWD: /usr/sbin/update-ca-certificates

# Power management
osapi ALL=(root) NOPASSWD: /sbin/shutdown *

# Containers and images
osapi ALL=(root) NOPASSWD: /usr/bin/docker *

Validate the file with sudo visudo -c -f /etc/sudoers.d/osapi-agent before you reload.

Linux capabilities​

To give access to files without sudo, give the agent binary specific Linux capabilities:

sudo setcap \
'cap_dac_read_search+ep cap_dac_override+ep cap_chown+ep cap_fowner+ep cap_kill+ep cap_net_admin+ep' \
/usr/local/bin/osapi

When privilege_escalation.enabled: true, the agent reads /proc/self/status when it starts, and checks the CapEff bitmask for the required bits:

CapabilityBitPurpose
CAP_DAC_READ_SEARCH2Read restricted files (/etc/shadow, proc)
CAP_DAC_OVERRIDE1Write files of all owners (netplan, sysctl)
CAP_CHOWN0Set file ownership to root:root (netplan configs)
CAP_FOWNER3Skip permission checks on owned files
CAP_KILL5Signal any process
CAP_NET_ADMIN12Network configuration (interface, routes, DNS)

If a required capability is missing, the agent writes a warning to the log. Only the operations that need the capability fail.

Kernel settings​

Some operations need a kernel setting, not a capability or a sudo rule:

SettingValueNeeded for
net.ipv4.ping_group_rangea range that has the agent's groupping

On Linux, the agent sends a ping on an ICMP datagram socket, also when it runs as root. The kernel permits that socket only for the groups in net.ipv4.ping_group_range. On Debian, Ubuntu and Fedora, systemd sets the range to each group, in /usr/lib/sysctl.d/50-default.conf. The default of the kernel is 1 0, which permits no group. Thus on a host without that file, each ping fails at once with socket: permission denied. The agent checks the setting at each start, also without privilege escalation, and logs a warning when no group of the agent is in the range:

WRN a ping from this agent fails error="no group of the agent [0] is in net.ipv4.ping_group_range (1 0); each ping fails"

To permit each group:

echo 'net.ipv4.ping_group_range = 0 2147483647' | sudo tee /etc/sysctl.d/90-osapi-ping.conf
sudo sysctl -p /etc/sysctl.d/90-osapi-ping.conf

Systemd unit file​

Use this unit file to run the agent as a user without privileges. The unit keeps the capabilities when the agent restarts:

[Unit]
Description=OSAPI Agent
After=network.target

[Service]
Type=simple
User=osapi
Group=osapi
ExecStart=/usr/local/bin/osapi agent start
Restart=always
RestartSec=5
AmbientCapabilities=CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE CAP_CHOWN CAP_FOWNER CAP_KILL CAP_NET_ADMIN
CapabilityBoundingSet=CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE CAP_CHOWN CAP_FOWNER CAP_KILL CAP_NET_ADMIN
SecureBits=keep-caps
NoNewPrivileges=no
PrivateTmp=true

[Install]
WantedBy=multi-user.target

AmbientCapabilities gives the capabilities to the process. Then the binary does not need setcap. If you also use sudo, set NoNewPrivileges=no, so that sudo can increase the privileges.

Preflight checks​

When enabled is true, the agent runs a check during agent start, before it subscribes to NATS.

The agent lists its sudo rules once with sudo -n -l. The -n flag makes sudo fail at once if it needs a password. If that fails, sudo is not usable, and the agent logs the error and exits.

Then the agent finds a rule for each program it runs with sudo, by the program's name. It skips a program that is not installed. It does not check the rule's arguments, so a narrow rule such as /usr/sbin/sysctl -p /etc/sysctl.d/osapi-*.conf counts as a rule for sysctl. A program with no rule is a warning, not an error: the agent starts, and only the operations that run that program fail.

The agent also checks each capability above. A missing capability is a warning too.

The agent checks each program that a behavior declares with behavior.Sudo on the platform of the host. It does not check a program that no behavior declares. The page of a behavior under Behaviors names the programs that it runs with sudo. osapi dsl behavior get --name <name> also lists them. Thus you find the sudoers entry of a new behavior there.

Example log, with no rule for useradd:

WRN preflight check failed check=sudo:useradd error="no sudo rule runs useradd; operations that need it fail"
INF preflight checks done

Command reference​

Write operations (use RunPrivilegedCmd)​

CommandDomain
systemctl start/stop/…Service
systemctl daemon-reloadService
sysctl -p, --systemSysctl
timedatectl set-timezoneTimezone
hostnamectl set-hostnameHostname
chronyc reload sourcesNTP
useradd, usermodUser
userdel -rUser
groupadd, groupdelGroup
gpasswd -MGroup
chown -RSSH Key
apt-get install/removePackage
apt-get updatePackage
update-ca-certificatesCertificate
shutdown -r/-hPower
sh -c "echo … chpasswd"User
docker create/start/…Docker
docker pull/rmiImage

Read operations (use RunCmd)​

CommandDomain
systemctl list-unitsService
systemctl list-unit-filesService
systemctl showService
systemctl is-activeService
systemctl is-enabledService
sysctl -nSysctl
timedatectl showTimezone
hostnamectl hostnameHostname
journalctlLog
chronyc trackingNTP
chronyc -c sourcesNTP
id -GnUser
passwd -SUser
dpkg-queryPackage
apt list --upgradablePackage
date +%:zTimezone

What does not change​

  • Controller and NATS server. They already run without privileges. They need no change.
  • command exec and command shell. These endpoints run commands that the user gives, with all the privileges of the agent. exec needs the command:execute RBAC permission. shell needs command:shell. Only the admin role gets each of them.
  • Docker reads. The docker and image behaviors read through gohai, which runs docker version, docker ps, docker inspect and docker images as the agent's user. Thus the osapi user must be able to use the Docker socket, usually as a member of the docker group. Otherwise, each read reports skipped.