From 46d1d537a7d2188c1ac1ed8713b0564bbefc294e Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Thu, 2 Jul 2026 17:48:52 +0200 Subject: [PATCH] Abort on privilege switch failures unless errno is EPERM --- os/unix.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/os/unix.c b/os/unix.c index 3753a115..442be22f 100644 --- a/os/unix.c +++ b/os/unix.c @@ -1411,22 +1411,50 @@ void os_privileges_acquire(void) { if (can_switch) { if (seteuid(0) != 0) { - os_syslog(OS_LVL_INFO, "failed to acquire privileges, errno=%s(%d)", strerror(errno), errno); + if (errno == EPERM) { + /* If EPERM, process lacks permission to switch privileges (e.g. dropped capabilities); continue with active privileges */ + os_syslog(OS_LVL_INFO, "permission denied to acquire privileges, continuing with active privileges"); + } else { + os_syslog(OS_LVL_INFO, "failed to acquire privileges, errno=%s(%d)", strerror(errno), errno); + os_abort(); + } } if (setegid(0) != 0) { - os_syslog(OS_LVL_INFO, "failed to acquire group privileges, errno=%s(%d)", strerror(errno), errno); + if (errno == EPERM) { + /* If EPERM, process lacks permission to switch privileges (e.g. dropped capabilities); continue with active privileges */ + os_syslog(OS_LVL_INFO, "permission denied to acquire group privileges, continuing with active privileges"); + } else { + os_syslog(OS_LVL_INFO, "failed to acquire group privileges, errno=%s(%d)", strerror(errno), errno); + os_abort(); + } } } } void os_privileges_release(void) { - if (can_switch) { - if (setegid(unpriv_gid) != 0) { + if (can_switch && setegid(unpriv_gid) != 0) { + if (errno == EPERM) { + /* + * If EPERM, process lacks permission to switch privileges (e.g. dropped capabilities); continue with active privileges + */ + os_syslog(OS_LVL_INFO, "permission denied to release group privileges, continuing with active privileges"); + can_switch = 0; + } else { os_syslog(OS_LVL_INFO, "failed to release group privileges, errno=%s(%d)", strerror(errno), errno); + os_abort(); } - if (seteuid(unpriv_uid) != 0) { + } + if (can_switch && seteuid(unpriv_uid) != 0) { + if (errno == EPERM) { + /* + * If EPERM, process lacks permission to switch privileges (e.g. dropped capabilities); continue with active privileges + */ + os_syslog(OS_LVL_INFO, "permission denied to release privileges, continuing with active privileges"); + can_switch = 0; + } else { os_syslog(OS_LVL_INFO, "failed to release privileges, errno=%s(%d)", strerror(errno), errno); + os_abort(); } } }