mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-04 08:17:46 +00:00

-----BEGIN PGP SIGNATURE----- iQIVAwUAXRyyVvu3V2unywtrAQL3xQ//eifjlELkRAPm2EReWwwahdM+9QL/0bAy e8eAzP9EaphQGUhpIzM9Y7Cx+a8XW2xACljY8hEFGyxXhDMoLa35oSoJOeay6vQt QcgWnDYsET8Z7HOsFCP3ZQqlbbqfsB6CbIKtZoEkZ8ib7eXpYcy1qTydu7wqrl4A AaJalAhlUKKUx9hkGGJTh2xvgmxgSJkxx3cNEWJQ2uGgY/ustBpqqT4iwFDsgA/q fcYTQFfNQBsC8/SmvQgxJSc+reUdQdp0z1vd8qjpSdFFcTq1qOtK0qDdz1Bbyl24 hAxvNM1KKav83C8aF7oHhEwLrkD+XiYKixdEiCJJp+A2i+vy2v8JnfgtFTpTgLNK 5xu2VmaiWmee9SLCiDIBKE4Ghtkr8DQ/5cKFCwthT8GXgQUtdsdwAaT3bWdCNfRm DqgU/AyyXhoHXrUM25tPeF3hZuDn2yy6b1TbKA9GCpu5TtznZIHju40Px/XMIpQH 8d6s/pg+u/SnkhjYWaTvTcvsQ2FB/vZY/UzAVyosnoMBkVfL4UtAHGbb8FBVj1nf Dv5VjSjl4vFjgOr3jygEAeD2cJ7L6jyKbtC/jo4dnOmPrSRShIjvfSU04L3z7FZS XFjMmGb2Jj8a7vAGFmsJdwmIXZ1uoTwX56DbpNL88eCgZWFPGKU7TisdIWAmJj8U N9wholjHJgw= =E3bF -----END PGP SIGNATURE----- Merge tag 'keys-acl-20190703' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs Pull keyring ACL support from David Howells: "This changes the permissions model used by keys and keyrings to be based on an internal ACL by the following means: - Replace the permissions mask internally with an ACL that contains a list of ACEs, each with a specific subject with a permissions mask. Potted default ACLs are available for new keys and keyrings. ACE subjects can be macroised to indicate the UID and GID specified on the key (which remain). Future commits will be able to add additional subject types, such as specific UIDs or domain tags/namespaces. Also split a number of permissions to give finer control. Examples include splitting the revocation permit from the change-attributes permit, thereby allowing someone to be granted permission to revoke a key without allowing them to change the owner; also the ability to join a keyring is split from the ability to link to it, thereby stopping a process accessing a keyring by joining it and thus acquiring use of possessor permits. - Provide a keyctl to allow the granting or denial of one or more permits to a specific subject. Direct access to the ACL is not granted, and the ACL cannot be viewed" * tag 'keys-acl-20190703' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs: keys: Provide KEYCTL_GRANT_PERMISSION keys: Replace uid/gid/perm permissions checking with an ACL
186 lines
4.8 KiB
C
186 lines
4.8 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* General persistent per-UID keyrings register
|
|
*
|
|
* Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
|
|
#include <linux/user_namespace.h>
|
|
#include <linux/cred.h>
|
|
|
|
#include "internal.h"
|
|
|
|
unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
|
|
|
|
static struct key_acl persistent_register_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),
|
|
}
|
|
};
|
|
|
|
static struct key_acl persistent_keyring_acl = {
|
|
.usage = REFCOUNT_INIT(1),
|
|
.nr_ace = 2,
|
|
.possessor_viewable = true,
|
|
.aces = {
|
|
KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE |
|
|
KEY_ACE_SEARCH | KEY_ACE_LINK |
|
|
KEY_ACE_CLEAR | KEY_ACE_INVAL),
|
|
KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Create the persistent keyring register for the current user namespace.
|
|
*
|
|
* Called with the namespace's sem locked for writing.
|
|
*/
|
|
static int key_create_persistent_register(struct user_namespace *ns)
|
|
{
|
|
struct key *reg = keyring_alloc(".persistent_register",
|
|
KUIDT_INIT(0), KGIDT_INIT(0),
|
|
current_cred(),
|
|
&persistent_register_keyring_acl,
|
|
KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
|
|
if (IS_ERR(reg))
|
|
return PTR_ERR(reg);
|
|
|
|
ns->persistent_keyring_register = reg;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Create the persistent keyring for the specified user.
|
|
*
|
|
* Called with the namespace's sem locked for writing.
|
|
*/
|
|
static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
|
|
struct keyring_index_key *index_key)
|
|
{
|
|
struct key *persistent;
|
|
key_ref_t reg_ref, persistent_ref;
|
|
|
|
if (!ns->persistent_keyring_register) {
|
|
long err = key_create_persistent_register(ns);
|
|
if (err < 0)
|
|
return ERR_PTR(err);
|
|
} else {
|
|
reg_ref = make_key_ref(ns->persistent_keyring_register, true);
|
|
persistent_ref = find_key_to_update(reg_ref, index_key);
|
|
if (persistent_ref)
|
|
return persistent_ref;
|
|
}
|
|
|
|
persistent = keyring_alloc(index_key->description,
|
|
uid, INVALID_GID, current_cred(),
|
|
&persistent_keyring_acl,
|
|
KEY_ALLOC_NOT_IN_QUOTA, NULL,
|
|
ns->persistent_keyring_register);
|
|
if (IS_ERR(persistent))
|
|
return ERR_CAST(persistent);
|
|
|
|
return make_key_ref(persistent, true);
|
|
}
|
|
|
|
/*
|
|
* Get the persistent keyring for a specific UID and link it to the nominated
|
|
* keyring.
|
|
*/
|
|
static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
|
|
key_ref_t dest_ref)
|
|
{
|
|
struct keyring_index_key index_key;
|
|
struct key *persistent;
|
|
key_ref_t reg_ref, persistent_ref;
|
|
char buf[32];
|
|
long ret;
|
|
|
|
/* Look in the register if it exists */
|
|
memset(&index_key, 0, sizeof(index_key));
|
|
index_key.type = &key_type_keyring;
|
|
index_key.description = buf;
|
|
index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
|
|
key_set_index_key(&index_key);
|
|
|
|
if (ns->persistent_keyring_register) {
|
|
reg_ref = make_key_ref(ns->persistent_keyring_register, true);
|
|
down_read(&ns->keyring_sem);
|
|
persistent_ref = find_key_to_update(reg_ref, &index_key);
|
|
up_read(&ns->keyring_sem);
|
|
|
|
if (persistent_ref)
|
|
goto found;
|
|
}
|
|
|
|
/* It wasn't in the register, so we'll need to create it. We might
|
|
* also need to create the register.
|
|
*/
|
|
down_write(&ns->keyring_sem);
|
|
persistent_ref = key_create_persistent(ns, uid, &index_key);
|
|
up_write(&ns->keyring_sem);
|
|
if (!IS_ERR(persistent_ref))
|
|
goto found;
|
|
|
|
return PTR_ERR(persistent_ref);
|
|
|
|
found:
|
|
ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
|
|
if (ret == 0) {
|
|
persistent = key_ref_to_ptr(persistent_ref);
|
|
ret = key_link(key_ref_to_ptr(dest_ref), persistent);
|
|
if (ret == 0) {
|
|
key_set_timeout(persistent, persistent_keyring_expiry);
|
|
ret = persistent->serial;
|
|
}
|
|
}
|
|
|
|
key_ref_put(persistent_ref);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Get the persistent keyring for a specific UID and link it to the nominated
|
|
* keyring.
|
|
*/
|
|
long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
|
|
{
|
|
struct user_namespace *ns = current_user_ns();
|
|
key_ref_t dest_ref;
|
|
kuid_t uid;
|
|
long ret;
|
|
|
|
/* -1 indicates the current user */
|
|
if (_uid == (uid_t)-1) {
|
|
uid = current_uid();
|
|
} else {
|
|
uid = make_kuid(ns, _uid);
|
|
if (!uid_valid(uid))
|
|
return -EINVAL;
|
|
|
|
/* You can only see your own persistent cache if you're not
|
|
* sufficiently privileged.
|
|
*/
|
|
if (!uid_eq(uid, current_uid()) &&
|
|
!uid_eq(uid, current_euid()) &&
|
|
!ns_capable(ns, CAP_SETUID))
|
|
return -EPERM;
|
|
}
|
|
|
|
/* There must be a destination keyring */
|
|
dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
|
|
if (IS_ERR(dest_ref))
|
|
return PTR_ERR(dest_ref);
|
|
if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
|
|
ret = -ENOTDIR;
|
|
goto out_put_dest;
|
|
}
|
|
|
|
ret = key_get_persistent(ns, uid, dest_ref);
|
|
|
|
out_put_dest:
|
|
key_ref_put(dest_ref);
|
|
return ret;
|
|
}
|