std.process.dropPrivileges in Zig 0.12.0 does not clear environment credentials
0 reputation · 18 May 2024, 20:25 UTC
0 reputation · 18 May 2024, 20:25 UTC
std.process.setuid, std.process.setgid and std.process.dropPrivileges were added to the Zig standard library in 0.12.0 to support least-privilege execution after startup on POSIX targets. dropPrivileges is documented as setting gid before uid and clearing supplementary groups.
The design goal for long-running services is to start with elevated privileges, perform setup, then relinquish them. The process module provides the syscalls for the transition, but it does not modify the process environment.
Environment variables that may carry secrets or time-limited tokens remain accessible after the privilege drop. The standard library does not define a credential cache or automatic invalidation for expired tokens in privileged mode, leaving the handling of stale or expired credentials to application code.
What is the intended responsibility boundary for scrubbing environment-based credentials before calling dropPrivileges? Should expired tokens be considered invalid after a privilege drop, or does the application need explicit invalidation logic? Is a standard library credential cache with automatic expiry in scope for Zig?
32275 reputation · 19 May 2024, 06:29 UTC
In Zig 0.12.0, std.process.dropPrivileges performs only the POSIX credential transition (setgroups, then setgid, then setuid). It does not touch the process environment, so any environment variable that held a secret or token remains readable after the call.
setgroups, setgid and setuid syscalls; no code accesses or modifies environ.Zig’s standard library follows a thin‑wrapper philosophy: it avoids hidden policy such as guessing which variables are secrets, because that judgment is application‑specific and could break legitimate post‑drop configuration reads.
const token = std.posix.getenv(\"API_TOKEN\") orelse return error.MissingSecret;).dropPrivileges, unset or overwrite the variable: std.posix.unsetenv(\"API_TOKEN\"); (overwriting first can help if the underlying storage might retain a copy).std.process.dropPrivileges(uid, gid);.Missing diagnostic detail: Have you already spawned any child processes before the environment scrub? If yes, the recommendation changes because those children retain the original environment and you must address them separately (e.g., by restarting them after the drop or by ensuring they never received the secret).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.