^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Key permission checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * key_task_permission - Check a key can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * @key_ref: The key to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @cred: The credentials to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @need_perm: The permission required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Check to see whether permission is granted to use a key in the desired way,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * but permit the security modules to override.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * The caller must hold either a ref on cred or must hold the RCU readlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Returns 0 if successful, -EACCES if access is denied based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * permissions bits or the LSM check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) enum key_need_perm need_perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) key_perm_t kperm, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) switch (need_perm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) case KEY_NEED_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) case KEY_SYSADMIN_OVERRIDE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) case KEY_AUTHTOKEN_OVERRIDE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case KEY_DEFER_PERM_CHECK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto lsm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case KEY_NEED_VIEW: mask = KEY_OTH_VIEW; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) case KEY_NEED_READ: mask = KEY_OTH_READ; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) case KEY_NEED_WRITE: mask = KEY_OTH_WRITE; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) case KEY_NEED_SEARCH: mask = KEY_OTH_SEARCH; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case KEY_NEED_LINK: mask = KEY_OTH_LINK; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) case KEY_NEED_SETATTR: mask = KEY_OTH_SETATTR; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* use the second 8-bits of permissions for keys the caller owns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (uid_eq(key->uid, cred->fsuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kperm = key->perm >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto use_these_perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* use the third 8-bits of permissions for keys the caller has a group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * membership in common with */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (gid_valid(key->gid) && key->perm & KEY_GRP_ALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (gid_eq(key->gid, cred->fsgid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) kperm = key->perm >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto use_these_perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ret = groups_search(cred->group_info, key->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) kperm = key->perm >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) goto use_these_perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* otherwise use the least-significant 8-bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) kperm = key->perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) use_these_perms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* use the top 8-bits of permissions for keys the caller possesses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * - possessor permissions are additive with other permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (is_key_possessed(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) kperm |= key->perm >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if ((kperm & mask) != mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* let LSM be the final arbiter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) lsm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return security_key_permission(key_ref, cred, need_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) EXPORT_SYMBOL(key_task_permission);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * key_validate - Validate a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @key: The key to be validated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Check that a key is valid, returning 0 if the key is okay, -ENOKEY if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * key is invalidated, -EKEYREVOKED if the key's type has been removed or if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * the key has been revoked or -EKEYEXPIRED if the key has expired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int key_validate(const struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned long flags = READ_ONCE(key->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) time64_t expiry = READ_ONCE(key->expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (flags & (1 << KEY_FLAG_INVALIDATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* check it's still accessible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (flags & ((1 << KEY_FLAG_REVOKED) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) (1 << KEY_FLAG_DEAD)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EKEYREVOKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* check it hasn't expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (expiry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (ktime_get_real_seconds() >= expiry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -EKEYEXPIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL(key_validate);