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
| Key | Type | Default | Environment | Description |
|---|---|---|---|---|
agent.privilege_escalation.enabled | bool | false | OSAPI_AGENT_PRIVILEGE_ESCALATION_ENABLED | Least-privilege mode: sudo for write commands and capability checks at startup. |
How it works
The exec manager has two ways to run a command:
RunCmdruns 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.RunPrivilegedCmdruns the command withsudoin front of it whenprivilege_escalation.enabled: true. When the setting is disabled, it is the same asRunCmd.
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:
| Capability | Bit | Purpose |
|---|---|---|
CAP_DAC_READ_SEARCH | 2 | Read restricted files (/etc/shadow, proc) |
CAP_DAC_OVERRIDE | 1 | Write files of all owners (netplan, sysctl) |
CAP_CHOWN | 0 | Set file ownership to root:root (netplan configs) |
CAP_FOWNER | 3 | Skip permission checks on owned files |
CAP_KILL | 5 | Signal any process |
CAP_NET_ADMIN | 12 | Network 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:
| Setting | Value | Needed for |
|---|---|---|
net.ipv4.ping_group_range | a range that has the agent's group | ping |
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)
| Command | Domain |
|---|---|
systemctl start/stop/… | Service |
systemctl daemon-reload | Service |
sysctl -p, --system | Sysctl |
timedatectl set-timezone | Timezone |
hostnamectl set-hostname | Hostname |
chronyc reload sources | NTP |
useradd, usermod | User |
userdel -r | User |
groupadd, groupdel | Group |
gpasswd -M | Group |
chown -R | SSH Key |
apt-get install/remove | Package |
apt-get update | Package |
update-ca-certificates | Certificate |
shutdown -r/-h | Power |
sh -c "echo … chpasswd" | User |
docker create/start/… | Docker |
docker pull/rmi | Image |
Read operations (use RunCmd)
| Command | Domain |
|---|---|
systemctl list-units | Service |
systemctl list-unit-files | Service |
systemctl show | Service |
systemctl is-active | Service |
systemctl is-enabled | Service |
sysctl -n | Sysctl |
timedatectl show | Timezone |
hostnamectl hostname | Hostname |
journalctl | Log |
chronyc tracking | NTP |
chronyc -c sources | NTP |
id -Gn | User |
passwd -S | User |
dpkg-query | Package |
apt list --upgradable | Package |
date +%:z | Timezone |
What does not change
- Controller and NATS server. They already run without privileges. They need no change.
command execandcommand shell. These endpoints run commands that the user gives, with all the privileges of the agent.execneeds thecommand:executeRBAC permission.shellneedscommand: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 inspectanddocker imagesas the agent's user. Thus theosapiuser must be able to use the Docker socket, usually as a member of thedockergroup. Otherwise, each read reportsskipped.