// SPDX-License-Identifier: GPL-2.0-or-later1/* Key permission checking2*3* Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.4* Written by David Howells ([email protected])5*/67#include <linux/export.h>8#include <linux/security.h>9#include "internal.h"1011/**12* key_task_permission - Check a key can be used13* @key_ref: The key to check.14* @cred: The credentials to use.15* @need_perm: The permission required.16*17* Check to see whether permission is granted to use a key in the desired way,18* but permit the security modules to override.19*20* The caller must hold either a ref on cred or must hold the RCU readlock.21*22* Returns 0 if successful, -EACCES if access is denied based on the23* permissions bits or the LSM check.24*/25int key_task_permission(const key_ref_t key_ref, const struct cred *cred,26enum key_need_perm need_perm)27{28struct key *key;29key_perm_t kperm, mask;30int ret;3132switch (need_perm) {33default:34WARN_ON(1);35return -EACCES;36case KEY_NEED_UNLINK:37case KEY_SYSADMIN_OVERRIDE:38case KEY_AUTHTOKEN_OVERRIDE:39case KEY_DEFER_PERM_CHECK:40goto lsm;4142case KEY_NEED_VIEW: mask = KEY_OTH_VIEW; break;43case KEY_NEED_READ: mask = KEY_OTH_READ; break;44case KEY_NEED_WRITE: mask = KEY_OTH_WRITE; break;45case KEY_NEED_SEARCH: mask = KEY_OTH_SEARCH; break;46case KEY_NEED_LINK: mask = KEY_OTH_LINK; break;47case KEY_NEED_SETATTR: mask = KEY_OTH_SETATTR; break;48}4950key = key_ref_to_ptr(key_ref);5152/* use the second 8-bits of permissions for keys the caller owns */53if (uid_eq(key->uid, cred->fsuid)) {54kperm = key->perm >> 16;55goto use_these_perms;56}5758/* use the third 8-bits of permissions for keys the caller has a group59* membership in common with */60if (gid_valid(key->gid) && key->perm & KEY_GRP_ALL) {61if (gid_eq(key->gid, cred->fsgid)) {62kperm = key->perm >> 8;63goto use_these_perms;64}6566ret = groups_search(cred->group_info, key->gid);67if (ret) {68kperm = key->perm >> 8;69goto use_these_perms;70}71}7273/* otherwise use the least-significant 8-bits */74kperm = key->perm;7576use_these_perms:7778/* use the top 8-bits of permissions for keys the caller possesses79* - possessor permissions are additive with other permissions80*/81if (is_key_possessed(key_ref))82kperm |= key->perm >> 24;8384if ((kperm & mask) != mask)85return -EACCES;8687/* let LSM be the final arbiter */88lsm:89return security_key_permission(key_ref, cred, need_perm);90}91EXPORT_SYMBOL(key_task_permission);9293/**94* key_validate - Validate a key.95* @key: The key to be validated.96*97* Check that a key is valid, returning 0 if the key is okay, -ENOKEY if the98* key is invalidated, -EKEYREVOKED if the key's type has been removed or if99* the key has been revoked or -EKEYEXPIRED if the key has expired.100*/101int key_validate(const struct key *key)102{103unsigned long flags = READ_ONCE(key->flags);104time64_t expiry = READ_ONCE(key->expiry);105106if (flags & (1 << KEY_FLAG_INVALIDATED))107return -ENOKEY;108109/* check it's still accessible */110if (flags & ((1 << KEY_FLAG_REVOKED) |111(1 << KEY_FLAG_DEAD)))112return -EKEYREVOKED;113114/* check it hasn't expired */115if (expiry) {116if (ktime_get_real_seconds() >= expiry)117return -EKEYEXPIRED;118}119120return 0;121}122EXPORT_SYMBOL(key_validate);123124125