mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-25 06:13:30 +07:00
2e12256b9a
Replace the uid/gid/perm permissions checking on a key with an ACL to allow the SETATTR and SEARCH permissions to be split. This will also allow a greater range of subjects to represented. ============ WHY DO THIS? ============ The problem is that SETATTR and SEARCH cover a slew of actions, not all of which should be grouped together. For SETATTR, this includes actions that are about controlling access to a key: (1) Changing a key's ownership. (2) Changing a key's security information. (3) Setting a keyring's restriction. And actions that are about managing a key's lifetime: (4) Setting an expiry time. (5) Revoking a key. and (proposed) managing a key as part of a cache: (6) Invalidating a key. Managing a key's lifetime doesn't really have anything to do with controlling access to that key. Expiry time is awkward since it's more about the lifetime of the content and so, in some ways goes better with WRITE permission. It can, however, be set unconditionally by a process with an appropriate authorisation token for instantiating a key, and can also be set by the key type driver when a key is instantiated, so lumping it with the access-controlling actions is probably okay. As for SEARCH permission, that currently covers: (1) Finding keys in a keyring tree during a search. (2) Permitting keyrings to be joined. (3) Invalidation. But these don't really belong together either, since these actions really need to be controlled separately. Finally, there are number of special cases to do with granting the administrator special rights to invalidate or clear keys that I would like to handle with the ACL rather than key flags and special checks. =============== WHAT IS CHANGED =============== The SETATTR permission is split to create two new permissions: (1) SET_SECURITY - which allows the key's owner, group and ACL to be changed and a restriction to be placed on a keyring. (2) REVOKE - which allows a key to be revoked. The SEARCH permission is split to create: (1) SEARCH - which allows a keyring to be search and a key to be found. (2) JOIN - which allows a keyring to be joined as a session keyring. (3) INVAL - which allows a key to be invalidated. The WRITE permission is also split to create: (1) WRITE - which allows a key's content to be altered and links to be added, removed and replaced in a keyring. (2) CLEAR - which allows a keyring to be cleared completely. This is split out to make it possible to give just this to an administrator. (3) REVOKE - see above. Keys acquire ACLs which consist of a series of ACEs, and all that apply are unioned together. An ACE specifies a subject, such as: (*) Possessor - permitted to anyone who 'possesses' a key (*) Owner - permitted to the key owner (*) Group - permitted to the key group (*) Everyone - permitted to everyone Note that 'Other' has been replaced with 'Everyone' on the assumption that you wouldn't grant a permit to 'Other' that you wouldn't also grant to everyone else. Further subjects may be made available by later patches. The ACE also specifies a permissions mask. The set of permissions is now: VIEW Can view the key metadata READ Can read the key content WRITE Can update/modify the key content SEARCH Can find the key by searching/requesting LINK Can make a link to the key SET_SECURITY Can change owner, ACL, expiry INVAL Can invalidate REVOKE Can revoke JOIN Can join this keyring CLEAR Can clear this keyring The KEYCTL_SETPERM function is then deprecated. The KEYCTL_SET_TIMEOUT function then is permitted if SET_SECURITY is set, or if the caller has a valid instantiation auth token. The KEYCTL_INVALIDATE function then requires INVAL. The KEYCTL_REVOKE function then requires REVOKE. The KEYCTL_JOIN_SESSION_KEYRING function then requires JOIN to join an existing keyring. The JOIN permission is enabled by default for session keyrings and manually created keyrings only. ====================== BACKWARD COMPATIBILITY ====================== To maintain backward compatibility, KEYCTL_SETPERM will translate the permissions mask it is given into a new ACL for a key - unless KEYCTL_SET_ACL has been called on that key, in which case an error will be returned. It will convert possessor, owner, group and other permissions into separate ACEs, if each portion of the mask is non-zero. SETATTR permission turns on all of INVAL, REVOKE and SET_SECURITY. WRITE permission turns on WRITE, REVOKE and, if a keyring, CLEAR. JOIN is turned on if a keyring is being altered. The KEYCTL_DESCRIBE function translates the ACL back into a permissions mask to return depending on possessor, owner, group and everyone ACEs. It will make the following mappings: (1) INVAL, JOIN -> SEARCH (2) SET_SECURITY -> SETATTR (3) REVOKE -> WRITE if SETATTR isn't already set (4) CLEAR -> WRITE Note that the value subsequently returned by KEYCTL_DESCRIBE may not match the value set with KEYCTL_SETATTR. ======= TESTING ======= This passes the keyutils testsuite for all but a couple of tests: (1) tests/keyctl/dh_compute/badargs: The first wrong-key-type test now returns EOPNOTSUPP rather than ENOKEY as READ permission isn't removed if the type doesn't have ->read(). You still can't actually read the key. (2) tests/keyctl/permitting/valid: The view-other-permissions test doesn't work as Other has been replaced with Everyone in the ACL. Signed-off-by: David Howells <dhowells@redhat.com>
281 lines
6.9 KiB
C
281 lines
6.9 KiB
C
/* Key permission checking
|
|
*
|
|
* Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*/
|
|
|
|
#include <linux/export.h>
|
|
#include <linux/security.h>
|
|
#include <linux/user_namespace.h>
|
|
#include <linux/uaccess.h>
|
|
#include "internal.h"
|
|
|
|
struct key_acl default_key_acl = {
|
|
.usage = REFCOUNT_INIT(1),
|
|
.nr_ace = 2,
|
|
.possessor_viewable = true,
|
|
.aces = {
|
|
KEY_POSSESSOR_ACE(KEY_ACE__PERMS & ~KEY_ACE_JOIN),
|
|
KEY_OWNER_ACE(KEY_ACE_VIEW),
|
|
}
|
|
};
|
|
EXPORT_SYMBOL(default_key_acl);
|
|
|
|
struct key_acl joinable_keyring_acl = {
|
|
.usage = REFCOUNT_INIT(1),
|
|
.nr_ace = 2,
|
|
.possessor_viewable = true,
|
|
.aces = {
|
|
KEY_POSSESSOR_ACE(KEY_ACE__PERMS & ~KEY_ACE_JOIN),
|
|
KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_LINK | KEY_ACE_JOIN),
|
|
}
|
|
};
|
|
EXPORT_SYMBOL(joinable_keyring_acl);
|
|
|
|
struct key_acl internal_key_acl = {
|
|
.usage = REFCOUNT_INIT(1),
|
|
.nr_ace = 2,
|
|
.aces = {
|
|
KEY_POSSESSOR_ACE(KEY_ACE_SEARCH),
|
|
KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_SEARCH),
|
|
}
|
|
};
|
|
EXPORT_SYMBOL(internal_key_acl);
|
|
|
|
struct key_acl internal_keyring_acl = {
|
|
.usage = REFCOUNT_INIT(1),
|
|
.nr_ace = 2,
|
|
.aces = {
|
|
KEY_POSSESSOR_ACE(KEY_ACE_SEARCH),
|
|
KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_SEARCH),
|
|
}
|
|
};
|
|
EXPORT_SYMBOL(internal_keyring_acl);
|
|
|
|
struct key_acl internal_writable_keyring_acl = {
|
|
.usage = REFCOUNT_INIT(1),
|
|
.nr_ace = 2,
|
|
.aces = {
|
|
KEY_POSSESSOR_ACE(KEY_ACE_SEARCH | KEY_ACE_WRITE),
|
|
KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE | KEY_ACE_SEARCH),
|
|
}
|
|
};
|
|
EXPORT_SYMBOL(internal_writable_keyring_acl);
|
|
|
|
/**
|
|
* key_task_permission - Check a key can be used
|
|
* @key_ref: The key to check.
|
|
* @cred: The credentials to use.
|
|
* @desired_perm: The permission to check for.
|
|
*
|
|
* Check to see whether permission is granted to use a key in the desired way,
|
|
* but permit the security modules to override.
|
|
*
|
|
* The caller must hold either a ref on cred or must hold the RCU readlock.
|
|
*
|
|
* Returns 0 if successful, -EACCES if access is denied based on the
|
|
* permissions bits or the LSM check.
|
|
*/
|
|
int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
|
|
unsigned int desired_perm)
|
|
{
|
|
const struct key_acl *acl;
|
|
const struct key *key;
|
|
unsigned int allow = 0;
|
|
int i;
|
|
|
|
BUILD_BUG_ON(KEY_NEED_VIEW != KEY_ACE_VIEW ||
|
|
KEY_NEED_READ != KEY_ACE_READ ||
|
|
KEY_NEED_WRITE != KEY_ACE_WRITE ||
|
|
KEY_NEED_SEARCH != KEY_ACE_SEARCH ||
|
|
KEY_NEED_LINK != KEY_ACE_LINK ||
|
|
KEY_NEED_SETSEC != KEY_ACE_SET_SECURITY ||
|
|
KEY_NEED_INVAL != KEY_ACE_INVAL ||
|
|
KEY_NEED_REVOKE != KEY_ACE_REVOKE ||
|
|
KEY_NEED_JOIN != KEY_ACE_JOIN ||
|
|
KEY_NEED_CLEAR != KEY_ACE_CLEAR);
|
|
|
|
key = key_ref_to_ptr(key_ref);
|
|
|
|
rcu_read_lock();
|
|
|
|
acl = rcu_dereference(key->acl);
|
|
if (!acl || acl->nr_ace == 0)
|
|
goto no_access_rcu;
|
|
|
|
for (i = 0; i < acl->nr_ace; i++) {
|
|
const struct key_ace *ace = &acl->aces[i];
|
|
|
|
switch (ace->type) {
|
|
case KEY_ACE_SUBJ_STANDARD:
|
|
switch (ace->subject_id) {
|
|
case KEY_ACE_POSSESSOR:
|
|
if (is_key_possessed(key_ref))
|
|
allow |= ace->perm;
|
|
break;
|
|
case KEY_ACE_OWNER:
|
|
if (uid_eq(key->uid, cred->fsuid))
|
|
allow |= ace->perm;
|
|
break;
|
|
case KEY_ACE_GROUP:
|
|
if (gid_valid(key->gid)) {
|
|
if (gid_eq(key->gid, cred->fsgid))
|
|
allow |= ace->perm;
|
|
else if (groups_search(cred->group_info, key->gid))
|
|
allow |= ace->perm;
|
|
}
|
|
break;
|
|
case KEY_ACE_EVERYONE:
|
|
allow |= ace->perm;
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
if (!(allow & desired_perm))
|
|
goto no_access;
|
|
|
|
return security_key_permission(key_ref, cred, desired_perm);
|
|
|
|
no_access_rcu:
|
|
rcu_read_unlock();
|
|
no_access:
|
|
return -EACCES;
|
|
}
|
|
EXPORT_SYMBOL(key_task_permission);
|
|
|
|
/**
|
|
* key_validate - Validate a key.
|
|
* @key: The key to be validated.
|
|
*
|
|
* Check that a key is valid, returning 0 if the key is okay, -ENOKEY if the
|
|
* key is invalidated, -EKEYREVOKED if the key's type has been removed or if
|
|
* the key has been revoked or -EKEYEXPIRED if the key has expired.
|
|
*/
|
|
int key_validate(const struct key *key)
|
|
{
|
|
unsigned long flags = READ_ONCE(key->flags);
|
|
time64_t expiry = READ_ONCE(key->expiry);
|
|
|
|
if (flags & (1 << KEY_FLAG_INVALIDATED))
|
|
return -ENOKEY;
|
|
|
|
/* check it's still accessible */
|
|
if (flags & ((1 << KEY_FLAG_REVOKED) |
|
|
(1 << KEY_FLAG_DEAD)))
|
|
return -EKEYREVOKED;
|
|
|
|
/* check it hasn't expired */
|
|
if (expiry) {
|
|
if (ktime_get_real_seconds() >= expiry)
|
|
return -EKEYEXPIRED;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(key_validate);
|
|
|
|
/*
|
|
* Roughly render an ACL to an old-style permissions mask. We cannot
|
|
* accurately render what the ACL, particularly if it has ACEs that represent
|
|
* subjects outside of { poss, user, group, other }.
|
|
*/
|
|
unsigned int key_acl_to_perm(const struct key_acl *acl)
|
|
{
|
|
unsigned int perm = 0, tperm;
|
|
int i;
|
|
|
|
BUILD_BUG_ON(KEY_OTH_VIEW != KEY_ACE_VIEW ||
|
|
KEY_OTH_READ != KEY_ACE_READ ||
|
|
KEY_OTH_WRITE != KEY_ACE_WRITE ||
|
|
KEY_OTH_SEARCH != KEY_ACE_SEARCH ||
|
|
KEY_OTH_LINK != KEY_ACE_LINK ||
|
|
KEY_OTH_SETATTR != KEY_ACE_SET_SECURITY);
|
|
|
|
if (!acl || acl->nr_ace == 0)
|
|
return 0;
|
|
|
|
for (i = 0; i < acl->nr_ace; i++) {
|
|
const struct key_ace *ace = &acl->aces[i];
|
|
|
|
switch (ace->type) {
|
|
case KEY_ACE_SUBJ_STANDARD:
|
|
tperm = ace->perm & KEY_OTH_ALL;
|
|
|
|
/* Invalidation and joining were allowed by SEARCH */
|
|
if (ace->perm & (KEY_ACE_INVAL | KEY_ACE_JOIN))
|
|
tperm |= KEY_OTH_SEARCH;
|
|
|
|
/* Revocation was allowed by either SETATTR or WRITE */
|
|
if ((ace->perm & KEY_ACE_REVOKE) && !(tperm & KEY_OTH_SETATTR))
|
|
tperm |= KEY_OTH_WRITE;
|
|
|
|
/* Clearing was allowed by WRITE */
|
|
if (ace->perm & KEY_ACE_CLEAR)
|
|
tperm |= KEY_OTH_WRITE;
|
|
|
|
switch (ace->subject_id) {
|
|
case KEY_ACE_POSSESSOR:
|
|
perm |= tperm << 24;
|
|
break;
|
|
case KEY_ACE_OWNER:
|
|
perm |= tperm << 16;
|
|
break;
|
|
case KEY_ACE_GROUP:
|
|
perm |= tperm << 8;
|
|
break;
|
|
case KEY_ACE_EVERYONE:
|
|
perm |= tperm << 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return perm;
|
|
}
|
|
|
|
/*
|
|
* Destroy a key's ACL.
|
|
*/
|
|
void key_put_acl(struct key_acl *acl)
|
|
{
|
|
if (acl && refcount_dec_and_test(&acl->usage))
|
|
kfree_rcu(acl, rcu);
|
|
}
|
|
|
|
/*
|
|
* Try to set the ACL. This either attaches or discards the proposed ACL.
|
|
*/
|
|
long key_set_acl(struct key *key, struct key_acl *acl)
|
|
{
|
|
int i;
|
|
|
|
/* If we're not the sysadmin, we can only change a key that we own. */
|
|
if (!capable(CAP_SYS_ADMIN) && !uid_eq(key->uid, current_fsuid())) {
|
|
key_put_acl(acl);
|
|
return -EACCES;
|
|
}
|
|
|
|
for (i = 0; i < acl->nr_ace; i++) {
|
|
const struct key_ace *ace = &acl->aces[i];
|
|
if (ace->type == KEY_ACE_SUBJ_STANDARD &&
|
|
ace->subject_id == KEY_ACE_POSSESSOR) {
|
|
if (ace->perm & KEY_ACE_VIEW)
|
|
acl->possessor_viewable = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
rcu_swap_protected(key->acl, acl, lockdep_is_held(&key->sem));
|
|
key_put_acl(acl);
|
|
return 0;
|
|
}
|