
Recall first from Writing a systemd unit
- READY=1 reports completed initialization, while watchdog messages report continuing health.
- A unit's sections separate identity, ordering, process settings and boot-time enablement, and enabling and starting are separate facts.
- Watchdog health must reflect real work progress, not an independent timer.
Credentials and permanent privilege removal
3 min readThe second lesson on this topic described the service with a unit and made startup readiness observable. This lesson answers how a service drops privileges and uses capability sets.
An older collector starts as root to open a protected device. Drop its authority before handling untrusted requests.
Credentials are the user IDs, group IDs and other security attributes the kernel uses to decide what a task may do. Linux records a real user ID, effective user ID and saved set-user-ID. A UID is a numeric user identifier; root normally has UID zero.
The real UID generally records the originating identity. The effective UID normally determines permissions, with Linux filesystem checks using a related filesystem UID. The saved UID can permit later restoration of an earlier effective identity. Group IDs have corresponding roles. Supplementary groups provide additional access, such as membership in a hardware-access group.
seteuid(uid) changes the effective UID while leaving the saved UID available. It suits temporary changes, not an assumed permanent drop. setreuid(real, effective) changes two IDs; -1 leaves an argument unchanged. Its saved-ID side effects depend on the requested changes. Avoid treating these interfaces as interchangeable. See setreuid(2).
A A Linux capability is a named privilege allowing specific operations beyond ordinary user and group permissions. With CAP_SETUID, setuid(nonroot_uid) replaces real, effective and saved UIDs. Unprivileged callers have narrower rules. setgid() similarly changes group IDs with CAP_SETGID. Check every result. See setuid(2).
For explicit Linux-only permanent changes, use setresgid() and setresuid() to name all three values. The following function assumes a single-threaded, privileged startup phase and already validated, nonzero target IDs. Compile with _GNU_SOURCE defined before headers:
#define _GNU_SOURCE
#include <sys/types.h>
#include <stddef.h>
#include <grp.h>
#include <unistd.h>
static int drop_identity(uid_t uid, gid_t gid)
{
if (setgroups(0, NULL) == -1)
return -1;
if (setresgid(gid, gid, gid) == -1)
return -1;
if (setresuid(uid, uid, uid) == -1)
return -1;
return 0;
}
This selects no supplementary groups. If approved access requires group memberships, call initgroups("telemetry", gid) instead of clearing them. That function consults the group database. Never preserve the launcher's groups by accident. Set groups before dropping the authority needed to change them. Treat any partial failure as fatal; do not serve requests. See initgroups(3) and setresuid(2).
Verify all ID fields with getresuid() and getresgid(), plus getgroups(). Also inspect capability sets. UID changes usually remove capabilities under normal rules, but securebits and capability-retention settings can change that behavior. Securebits are Linux flags controlling special privilege rules. Do not claim complete privilege removal from UID numbers alone.
Going deeperA dropped identity does not revoke open handles
An already opened device or file can remain usable after a UID change. Review every retained descriptor as authority. Linux kernel credentials are per-thread, represented through struct cred; libc wrappers coordinate ID changes between threads to support process-wide POSIX behavior. Raw system calls can bypass that coordination. Drop privileges before starting workers, and avoid direct syscall shortcuts for credential changes.
Prefer User=telemetry when the application needs no privileged startup at all. The manager can establish credentials before your parser, libraries and application initialization run.
Capability sets and execution
4 min readCAP_NET_BIND_SERVICE permits binding privileged Internet ports. CAP_CHOWN permits otherwise restricted ownership changes. CAP_DAC_OVERRIDE bypasses ordinary discretionary file access checks. Executing a regular file still requires at least one execute permission bit. It does not bypass every security mechanism. CAP_SYS_ADMIN covers many unrelated administration operations, including mounting filesystems and certain namespace operations. It is far too broad for a simple telemetry collector.
Capabilities are per-thread attributes. Tools often call them process capabilities because ordinary applications keep consistent credentials across threads. Five sets matter:
| Set | Meaning |
|---|---|
| Effective | Powers currently checked for privileged operations |
| Permitted | Powers the thread may place into its effective set |
| Inheritable | Powers eligible to combine with a program file's inheritable set |
| Ambient | Powers carried across ordinary, nonprivileged program execution |
| Bounding | Ceiling applied to file-permitted capability gains during execution |
An executable can also carry file capabilities in security.capability. This is an extended attribute, or metadata attached to the file outside its normal contents. It records permitted and inheritable sets plus an effective flag. The file effective value is a flag, not an independent bitmap matching every process capability.
execve() replaces the running program image. In the ordinary non-root case, capability calculation can be summarized as follows. P means the calling thread, F means the executable file, and new means after execution. & means intersection and | means union:
new ambient = privileged file ? empty : old ambient
new permitted = (old inheritable & file inheritable)
| (file permitted & old bounding)
| new ambient
new effective = file effective flag ? new permitted : new ambient
new inheritable = old inheritable
new bounding = old bounding
Here, a privileged file includes a set-user-ID or set-group-ID executable, or one carrying file capabilities. Ambient membership requires membership in both permitted and inheritable sets. Ordinary execution does not preserve arbitrary old permitted capabilities. Also, bounding-set removal alone does not erase a capability already held elsewhere. Root-specific rules, securebits, nosuid, tracing and no_new_privs add important conditions. Use capabilities(7) when analyzing an actual execution path.
1Give a compiled helper one file capability
Assume your lab has produced a native executable bind-helper that binds TCP port 80. A port identifies a network endpoint; binding reserves the local address and port. Build it as a foreground program without a “must be root” check. Install a controlled copy, grant one capability and inspect its metadata:
sudo install -d -o root -g root -m 0755 /usr/local/libexec
sudo install -o root -g root -m 0755 ./bind-helper /usr/local/libexec/bind-helper
sudo setcap 'cap_net_bind_service=ep' /usr/local/libexec/bind-helper
getcap /usr/local/libexec/bind-helper
/usr/local/libexec/bind-helper
Run the final command as the ordinary lab user. Here p selects the file permitted set and e sets its effective flag. getcap reports executable metadata; inspect the running task too. Never grant this capability to a general interpreter: arbitrary scripts would share that power.
2Check the actual privilege boundary
The traditional privileged-port boundary is 1024. The network-namespace setting net.ipv4.ip_unprivileged_port_start can lower it, including to zero. Read it with sysctl net.ipv4.ip_unprivileged_port_start before interpreting success without capabilities. A network namespace supplies a separate networking environment; containers may have different settings.
File capabilities require compatible filesystem metadata. Copying, modifying or replacing the executable can lose or clear them. Verify after deployment. Set-user-ID root usually grants broad root authority to the program; a single file capability limits the initial grant. Powerful capabilities can still be nearly as dangerous. Neither mechanism makes untrusted code safe.
3Prefer a unit-scoped grant where suitable
For a non-root system service that binds its own low port, add:
User=telemetry
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=yes
The bounding set restricts available powers; it does not grant one by itself. The ambient setting arranges the grant across execution without changing executable metadata. Use an ordinary executable without file capabilities for this variant. NoNewPrivileges=yes blocks new execution-based privilege gains, including file-capability gains, but does not remove capabilities already established by the manager. See the kernel explanation.
Key takeaways
- Drop supplementary groups and saved identities when permanently dropping privileges.
- Capabilities grant individual powers, and their sets change when programs execute.
- Real, effective and saved IDs serve different roles, and already-open descriptors can survive a UID change.
- The bounding set only limits gains, while ambient capabilities carry a grant across an ordinary exec without file metadata.
Reflect
- Explain to a colleague, in two sentences: analyze UID changes and capability transformations across execve().
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
Official documentation first. Links checked when the lesson was published.