Skip to content

Commit 2d7f105

Browse files
cgzonesjarkkojs
authored andcommitted
security: keys: perform capable check only on privileged operations
If the current task fails the check for the queried capability via `capable(CAP_SYS_ADMIN)` LSMs like SELinux generate a denial message. Issuing such denial messages unnecessarily can lead to a policy author granting more privileges to a subject than needed to silence them. Reorder CAP_SYS_ADMIN checks after the check whether the operation is actually privileged. Signed-off-by: Christian Göttsche <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 57012c5 commit 2d7f105

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

security/keys/keyctl.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,19 @@ long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
980980
ret = -EACCES;
981981
down_write(&key->sem);
982982

983-
if (!capable(CAP_SYS_ADMIN)) {
983+
{
984+
bool is_privileged_op = false;
985+
984986
/* only the sysadmin can chown a key to some other UID */
985987
if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
986-
goto error_put;
988+
is_privileged_op = true;
987989

988990
/* only the sysadmin can set the key's GID to a group other
989991
* than one of those that the current process subscribes to */
990992
if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
993+
is_privileged_op = true;
994+
995+
if (is_privileged_op && !capable(CAP_SYS_ADMIN))
991996
goto error_put;
992997
}
993998

@@ -1088,7 +1093,7 @@ long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
10881093
down_write(&key->sem);
10891094

10901095
/* if we're not the sysadmin, we can only change a key that we own */
1091-
if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1096+
if (uid_eq(key->uid, current_fsuid()) || capable(CAP_SYS_ADMIN)) {
10921097
key->perm = perm;
10931098
notify_key(key, NOTIFY_KEY_SETATTR, 0);
10941099
ret = 0;

0 commit comments

Comments
 (0)