2019-05-20 17:08:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-04-03 15:07:24 +00:00
|
|
|
/* System hash blacklist.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "blacklist: "fmt
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/key.h>
|
|
|
|
#include <linux/key-type.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <keys/system_keyring.h>
|
|
|
|
#include "blacklist.h"
|
|
|
|
|
|
|
|
static struct key *blacklist_keyring;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The description must be a type prefix, a colon and then an even number of
|
|
|
|
* hex digits. The hash is kept in the description.
|
|
|
|
*/
|
|
|
|
static int blacklist_vet_description(const char *desc)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if (*desc == ':')
|
|
|
|
return -EINVAL;
|
|
|
|
for (; *desc; desc++)
|
|
|
|
if (*desc == ':')
|
|
|
|
goto found_colon;
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
found_colon:
|
|
|
|
desc++;
|
|
|
|
for (; *desc; desc++) {
|
|
|
|
if (!isxdigit(*desc))
|
|
|
|
return -EINVAL;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0 || n & 1)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The hash to be blacklisted is expected to be in the description. There will
|
|
|
|
* be no payload.
|
|
|
|
*/
|
|
|
|
static int blacklist_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
|
|
|
if (prep->datalen > 0)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blacklist_free_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blacklist_describe(const struct key *key, struct seq_file *m)
|
|
|
|
{
|
|
|
|
seq_puts(m, key->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct key_type key_type_blacklist = {
|
|
|
|
.name = "blacklist",
|
|
|
|
.vet_description = blacklist_vet_description,
|
|
|
|
.preparse = blacklist_preparse,
|
|
|
|
.free_preparse = blacklist_free_preparse,
|
|
|
|
.instantiate = generic_key_instantiate,
|
|
|
|
.describe = blacklist_describe,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mark_hash_blacklisted - Add a hash to the system blacklist
|
|
|
|
* @hash - The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
|
|
|
|
*/
|
|
|
|
int mark_hash_blacklisted(const char *hash)
|
|
|
|
{
|
|
|
|
key_ref_t key;
|
|
|
|
|
|
|
|
key = key_create_or_update(make_key_ref(blacklist_keyring, true),
|
|
|
|
"blacklist",
|
|
|
|
hash,
|
|
|
|
NULL,
|
|
|
|
0,
|
keys: Replace uid/gid/perm permissions checking with an ACL
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>
2019-06-27 22:03:07 +00:00
|
|
|
&internal_key_acl,
|
2017-04-03 15:07:24 +00:00
|
|
|
KEY_ALLOC_NOT_IN_QUOTA |
|
|
|
|
KEY_ALLOC_BUILT_IN);
|
|
|
|
if (IS_ERR(key)) {
|
|
|
|
pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
|
|
|
|
return PTR_ERR(key);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is_hash_blacklisted - Determine if a hash is blacklisted
|
|
|
|
* @hash: The hash to be checked as a binary blob
|
|
|
|
* @hash_len: The length of the binary hash
|
|
|
|
* @type: Type of hash
|
|
|
|
*/
|
|
|
|
int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
|
|
|
|
{
|
|
|
|
key_ref_t kref;
|
|
|
|
size_t type_len = strlen(type);
|
|
|
|
char *buffer, *p;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
|
|
|
|
if (!buffer)
|
|
|
|
return -ENOMEM;
|
|
|
|
p = memcpy(buffer, type, type_len);
|
|
|
|
p += type_len;
|
|
|
|
*p++ = ':';
|
|
|
|
bin2hex(p, hash, hash_len);
|
|
|
|
p += hash_len * 2;
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
kref = keyring_search(make_key_ref(blacklist_keyring, true),
|
2019-06-26 20:02:32 +00:00
|
|
|
&key_type_blacklist, buffer, false);
|
2017-04-03 15:07:24 +00:00
|
|
|
if (!IS_ERR(kref)) {
|
|
|
|
key_ref_put(kref);
|
|
|
|
ret = -EKEYREJECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(is_hash_blacklisted);
|
|
|
|
|
|
|
|
/*
|
2017-05-08 22:57:53 +00:00
|
|
|
* Initialise the blacklist
|
2017-04-03 15:07:24 +00:00
|
|
|
*/
|
|
|
|
static int __init blacklist_init(void)
|
|
|
|
{
|
|
|
|
const char *const *bl;
|
|
|
|
|
|
|
|
if (register_key_type(&key_type_blacklist) < 0)
|
|
|
|
panic("Can't allocate system blacklist key type\n");
|
|
|
|
|
|
|
|
blacklist_keyring =
|
|
|
|
keyring_alloc(".blacklist",
|
|
|
|
KUIDT_INIT(0), KGIDT_INIT(0),
|
|
|
|
current_cred(),
|
keys: Replace uid/gid/perm permissions checking with an ACL
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>
2019-06-27 22:03:07 +00:00
|
|
|
&internal_keyring_acl,
|
2017-04-03 15:07:24 +00:00
|
|
|
KEY_ALLOC_NOT_IN_QUOTA |
|
|
|
|
KEY_FLAG_KEEP,
|
|
|
|
NULL, NULL);
|
|
|
|
if (IS_ERR(blacklist_keyring))
|
|
|
|
panic("Can't allocate system blacklist keyring\n");
|
|
|
|
|
|
|
|
for (bl = blacklist_hashes; *bl; bl++)
|
|
|
|
if (mark_hash_blacklisted(*bl) < 0)
|
|
|
|
pr_err("- blacklisting failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be initialised before we try and load the keys into the keyring.
|
|
|
|
*/
|
|
|
|
device_initcall(blacklist_init);
|