audit: fix check for existing capability
We must not treat the return code for capng_has_capability() as a boolean,
it returns 0 if the capability is not set, 1 if it is, but CAPNG_FAIL on
failure.
Internally, it calls capng_get_caps_process() if needed, and if this fails,
the failure is forwarded.
@@ -55,14 +55,25 @@ int util_audit_drop_permissions(uint32_t uid, uint32_t gid) {
if (r < 0)
return error_origin(-errno);
} else {
- int have_audit_write;
+ bool have_audit_write;
+
+ r = capng_have_capability(CAPNG_PERMITTED, CAP_AUDIT_WRITE);
+ if (r == CAPNG_FAIL)
+ return error_origin(-EIO);
+ else if (r == 1)
+ have_audit_write = true;
+ else
+ have_audit_write = false;
- have_audit_write = capng_have_capability(CAPNG_PERMITTED, CAP_AUDIT_WRITE);
capng_clear(CAPNG_SELECT_BOTH);
- if (have_audit_write)
- capng_update(CAPNG_ADD,
+
+ if (have_audit_write) {
+ r = capng_update(CAPNG_ADD,
CAPNG_EFFECTIVE | CAPNG_PERMITTED,
CAP_AUDIT_WRITE);
+ if (r < 0)
+ return error_origin(-EINVAL);
+ }
r = capng_change_id(uid, gid, CAPNG_DROP_SUPP_GRP);
if (r)
