2019-05-27 06:55:01 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2007-10-17 06:29:46 +00:00
|
|
|
/* Authentication token and access key management
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2007-10-17 06:29:46 +00:00
|
|
|
* Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
|
2005-04-16 22:20:36 +00:00
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
2017-05-13 11:51:50 +00:00
|
|
|
* See Documentation/security/keys/core.rst for information on keys/keyrings.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_KEY_H
|
|
|
|
#define _LINUX_KEY_H
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/rbtree.h>
|
2005-06-24 05:00:49 +00:00
|
|
|
#include <linux/rcupdate.h>
|
2008-04-29 08:01:32 +00:00
|
|
|
#include <linux/sysctl.h>
|
2009-03-24 08:54:46 +00:00
|
|
|
#include <linux/rwsem.h>
|
2011-07-26 23:09:06 +00:00
|
|
|
#include <linux/atomic.h>
|
2013-09-24 09:35:18 +00:00
|
|
|
#include <linux/assoc_array.h>
|
2017-03-31 12:20:48 +00:00
|
|
|
#include <linux/refcount.h>
|
2017-11-15 16:38:45 +00:00
|
|
|
#include <linux/time64.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#ifdef __KERNEL__
|
2012-02-08 15:53:04 +00:00
|
|
|
#include <linux/uidgid.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* key handle serial number */
|
|
|
|
typedef int32_t key_serial_t;
|
|
|
|
|
2019-07-11 01:43:43 +00:00
|
|
|
/* key handle permissions mask */
|
|
|
|
typedef uint32_t key_perm_t;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
struct key;
|
2019-06-26 20:02:33 +00:00
|
|
|
struct net;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_KEYS
|
|
|
|
|
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
|
|
|
#undef KEY_DEBUGGING
|
2008-04-29 08:01:28 +00:00
|
|
|
|
2019-07-11 01:43:43 +00:00
|
|
|
#define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */
|
|
|
|
#define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */
|
|
|
|
#define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */
|
|
|
|
#define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */
|
|
|
|
#define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */
|
|
|
|
#define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */
|
|
|
|
#define KEY_POS_ALL 0x3f000000
|
|
|
|
|
|
|
|
#define KEY_USR_VIEW 0x00010000 /* user permissions... */
|
|
|
|
#define KEY_USR_READ 0x00020000
|
|
|
|
#define KEY_USR_WRITE 0x00040000
|
|
|
|
#define KEY_USR_SEARCH 0x00080000
|
|
|
|
#define KEY_USR_LINK 0x00100000
|
|
|
|
#define KEY_USR_SETATTR 0x00200000
|
|
|
|
#define KEY_USR_ALL 0x003f0000
|
|
|
|
|
|
|
|
#define KEY_GRP_VIEW 0x00000100 /* group permissions... */
|
|
|
|
#define KEY_GRP_READ 0x00000200
|
|
|
|
#define KEY_GRP_WRITE 0x00000400
|
|
|
|
#define KEY_GRP_SEARCH 0x00000800
|
|
|
|
#define KEY_GRP_LINK 0x00001000
|
|
|
|
#define KEY_GRP_SETATTR 0x00002000
|
|
|
|
#define KEY_GRP_ALL 0x00003f00
|
|
|
|
|
|
|
|
#define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
|
|
|
|
#define KEY_OTH_READ 0x00000002
|
|
|
|
#define KEY_OTH_WRITE 0x00000004
|
|
|
|
#define KEY_OTH_SEARCH 0x00000008
|
|
|
|
#define KEY_OTH_LINK 0x00000010
|
|
|
|
#define KEY_OTH_SETATTR 0x00000020
|
|
|
|
#define KEY_OTH_ALL 0x0000003f
|
|
|
|
|
|
|
|
#define KEY_PERM_UNDEF 0xffffffff
|
|
|
|
|
2020-05-12 14:16:29 +00:00
|
|
|
/*
|
|
|
|
* The permissions required on a key that we're looking up.
|
|
|
|
*/
|
|
|
|
enum key_need_perm {
|
|
|
|
KEY_NEED_UNSPECIFIED, /* Needed permission unspecified */
|
|
|
|
KEY_NEED_VIEW, /* Require permission to view attributes */
|
|
|
|
KEY_NEED_READ, /* Require permission to read content */
|
|
|
|
KEY_NEED_WRITE, /* Require permission to update / modify */
|
|
|
|
KEY_NEED_SEARCH, /* Require permission to search (keyring) or find (key) */
|
|
|
|
KEY_NEED_LINK, /* Require permission to link */
|
|
|
|
KEY_NEED_SETATTR, /* Require permission to change attributes */
|
|
|
|
KEY_NEED_UNLINK, /* Require permission to unlink key */
|
|
|
|
KEY_SYSADMIN_OVERRIDE, /* Special: override by CAP_SYS_ADMIN */
|
|
|
|
KEY_AUTHTOKEN_OVERRIDE, /* Special: override by possession of auth token */
|
|
|
|
KEY_DEFER_PERM_CHECK, /* Special: permission check is deferred */
|
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
struct seq_file;
|
|
|
|
struct user_struct;
|
|
|
|
struct signal_struct;
|
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:23 +00:00
|
|
|
struct cred;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
struct key_type;
|
|
|
|
struct key_owner;
|
2019-06-26 20:02:32 +00:00
|
|
|
struct key_tag;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct keyring_list;
|
|
|
|
struct keyring_name;
|
|
|
|
|
2019-06-26 20:02:32 +00:00
|
|
|
struct key_tag {
|
|
|
|
struct rcu_head rcu;
|
|
|
|
refcount_t usage;
|
|
|
|
bool removed; /* T when subject removed */
|
|
|
|
};
|
|
|
|
|
2013-09-24 09:35:15 +00:00
|
|
|
struct keyring_index_key {
|
2019-06-26 20:02:32 +00:00
|
|
|
/* [!] If this structure is altered, the union in struct key must change too! */
|
|
|
|
unsigned long hash; /* Hash value */
|
2019-06-26 20:02:31 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
#ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */
|
2019-08-19 15:02:01 +00:00
|
|
|
u16 desc_len;
|
|
|
|
char desc[sizeof(long) - 2]; /* First few chars of description */
|
2019-06-26 20:02:31 +00:00
|
|
|
#else
|
2019-08-19 15:02:01 +00:00
|
|
|
char desc[sizeof(long) - 2]; /* First few chars of description */
|
|
|
|
u16 desc_len;
|
2019-06-26 20:02:31 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
unsigned long x;
|
|
|
|
};
|
2013-09-24 09:35:15 +00:00
|
|
|
struct key_type *type;
|
2019-06-26 20:02:32 +00:00
|
|
|
struct key_tag *domain_tag; /* Domain of operation */
|
2013-09-24 09:35:15 +00:00
|
|
|
const char *description;
|
|
|
|
};
|
|
|
|
|
2015-10-21 13:04:48 +00:00
|
|
|
union key_payload {
|
|
|
|
void __rcu *rcu_data0;
|
|
|
|
void *data[4];
|
|
|
|
};
|
|
|
|
|
2005-09-28 16:03:15 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* key reference with possession attribute handling
|
|
|
|
*
|
|
|
|
* NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
|
|
|
|
* defined. This is because we abuse the bottom bit of the reference to carry a
|
|
|
|
* flag to indicate whether the calling process possesses that key in one of
|
|
|
|
* its keyrings.
|
|
|
|
*
|
|
|
|
* the key_ref_t has been made a separate type so that the compiler can reject
|
|
|
|
* attempts to dereference it without proper conversion.
|
|
|
|
*
|
|
|
|
* the three functions are used to assemble and disassemble references
|
|
|
|
*/
|
|
|
|
typedef struct __key_reference_with_attributes *key_ref_t;
|
|
|
|
|
|
|
|
static inline key_ref_t make_key_ref(const struct key *key,
|
2013-09-24 09:35:14 +00:00
|
|
|
bool possession)
|
2005-09-28 16:03:15 +00:00
|
|
|
{
|
|
|
|
return (key_ref_t) ((unsigned long) key | possession);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
|
|
|
|
{
|
|
|
|
return (struct key *) ((unsigned long) key_ref & ~1UL);
|
|
|
|
}
|
|
|
|
|
2013-09-24 09:35:14 +00:00
|
|
|
static inline bool is_key_possessed(const key_ref_t key_ref)
|
2005-09-28 16:03:15 +00:00
|
|
|
{
|
|
|
|
return (unsigned long) key_ref & 1UL;
|
|
|
|
}
|
|
|
|
|
2016-08-30 18:33:13 +00:00
|
|
|
typedef int (*key_restrict_link_func_t)(struct key *dest_keyring,
|
2016-04-25 18:30:39 +00:00
|
|
|
const struct key_type *type,
|
2016-08-30 18:33:13 +00:00
|
|
|
const union key_payload *payload,
|
|
|
|
struct key *restriction_key);
|
2016-04-25 18:30:39 +00:00
|
|
|
|
2016-06-27 23:10:59 +00:00
|
|
|
struct key_restriction {
|
|
|
|
key_restrict_link_func_t check;
|
|
|
|
struct key *key;
|
|
|
|
struct key_type *keytype;
|
|
|
|
};
|
|
|
|
|
2017-10-04 15:43:25 +00:00
|
|
|
enum key_state {
|
|
|
|
KEY_IS_UNINSTANTIATED,
|
|
|
|
KEY_IS_POSITIVE, /* Positively instantiated */
|
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* authentication token / access credential / keyring
|
|
|
|
* - types of key include:
|
|
|
|
* - keyrings
|
|
|
|
* - disk encryption IDs
|
|
|
|
* - Kerberos TGTs and tickets
|
|
|
|
*/
|
|
|
|
struct key {
|
2017-03-31 12:20:48 +00:00
|
|
|
refcount_t usage; /* number of references */
|
2005-04-16 22:20:36 +00:00
|
|
|
key_serial_t serial; /* key serial number */
|
2012-05-11 09:56:56 +00:00
|
|
|
union {
|
|
|
|
struct list_head graveyard_link;
|
|
|
|
struct rb_node serial_node;
|
|
|
|
};
|
watch_queue: Add a key/keyring notification facility
Add a key/keyring change notification facility whereby notifications about
changes in key and keyring content and attributes can be received.
Firstly, an event queue needs to be created:
pipe2(fds, O_NOTIFICATION_PIPE);
ioctl(fds[1], IOC_WATCH_QUEUE_SET_SIZE, 256);
then a notification can be set up to report notifications via that queue:
struct watch_notification_filter filter = {
.nr_filters = 1,
.filters = {
[0] = {
.type = WATCH_TYPE_KEY_NOTIFY,
.subtype_filter[0] = UINT_MAX,
},
},
};
ioctl(fds[1], IOC_WATCH_QUEUE_SET_FILTER, &filter);
keyctl_watch_key(KEY_SPEC_SESSION_KEYRING, fds[1], 0x01);
After that, records will be placed into the queue when events occur in
which keys are changed in some way. Records are of the following format:
struct key_notification {
struct watch_notification watch;
__u32 key_id;
__u32 aux;
} *n;
Where:
n->watch.type will be WATCH_TYPE_KEY_NOTIFY.
n->watch.subtype will indicate the type of event, such as
NOTIFY_KEY_REVOKED.
n->watch.info & WATCH_INFO_LENGTH will indicate the length of the
record.
n->watch.info & WATCH_INFO_ID will be the second argument to
keyctl_watch_key(), shifted.
n->key will be the ID of the affected key.
n->aux will hold subtype-dependent information, such as the key
being linked into the keyring specified by n->key in the case of
NOTIFY_KEY_LINKED.
Note that it is permissible for event records to be of variable length -
or, at least, the length may be dependent on the subtype. Note also that
the queue can be shared between multiple notifications of various types.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: James Morris <jamorris@linux.microsoft.com>
2020-01-14 17:07:11 +00:00
|
|
|
#ifdef CONFIG_KEY_NOTIFICATIONS
|
|
|
|
struct watch_list *watchers; /* Entities watching this key for changes */
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
struct rw_semaphore sem; /* change vs change sem */
|
|
|
|
struct key_user *user; /* owner of this key */
|
2005-10-30 23:02:44 +00:00
|
|
|
void *security; /* security data for this key */
|
2009-09-02 08:14:00 +00:00
|
|
|
union {
|
2017-11-15 16:38:45 +00:00
|
|
|
time64_t expiry; /* time at which key expires (or 0) */
|
|
|
|
time64_t revoked_at; /* time at which key was revoked */
|
2009-09-02 08:14:00 +00:00
|
|
|
};
|
2017-11-15 16:38:45 +00:00
|
|
|
time64_t last_used_at; /* last time used for LRU keyring discard */
|
2012-02-08 15:53:04 +00:00
|
|
|
kuid_t uid;
|
|
|
|
kgid_t gid;
|
2019-07-11 01:43:43 +00:00
|
|
|
key_perm_t perm; /* access permissions */
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned short quotalen; /* length added to quota */
|
2005-06-24 05:00:49 +00:00
|
|
|
unsigned short datalen; /* payload data length
|
|
|
|
* - may not match RCU dereferenced payload
|
|
|
|
* - payload should contain own length
|
|
|
|
*/
|
2017-10-04 15:43:25 +00:00
|
|
|
short state; /* Key state (+) or rejection error (-) */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#ifdef KEY_DEBUGGING
|
|
|
|
unsigned magic;
|
|
|
|
#define KEY_DEBUG_MAGIC 0x18273645u
|
|
|
|
#endif
|
|
|
|
|
2005-06-24 05:00:49 +00:00
|
|
|
unsigned long flags; /* status flags (change with bitops) */
|
2017-10-04 15:43:25 +00:00
|
|
|
#define KEY_FLAG_DEAD 0 /* set if key type has been deleted */
|
|
|
|
#define KEY_FLAG_REVOKED 1 /* set if key had been revoked */
|
|
|
|
#define KEY_FLAG_IN_QUOTA 2 /* set if key consumes quota */
|
|
|
|
#define KEY_FLAG_USER_CONSTRUCT 3 /* set if key is being constructed in userspace */
|
|
|
|
#define KEY_FLAG_ROOT_CAN_CLEAR 4 /* set if key can be cleared by root without permission */
|
|
|
|
#define KEY_FLAG_INVALIDATED 5 /* set if key has been invalidated */
|
|
|
|
#define KEY_FLAG_BUILTIN 6 /* set if key is built in to the kernel */
|
|
|
|
#define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */
|
|
|
|
#define KEY_FLAG_KEEP 8 /* set if key should not be removed */
|
|
|
|
#define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */
|
2005-06-24 05:00:49 +00:00
|
|
|
|
2013-09-24 09:35:15 +00:00
|
|
|
/* the key type and key description string
|
|
|
|
* - the desc is used to match a key against search criteria
|
|
|
|
* - it should be a printable string
|
2005-04-16 22:20:36 +00:00
|
|
|
* - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
|
|
|
|
*/
|
2013-09-24 09:35:15 +00:00
|
|
|
union {
|
|
|
|
struct keyring_index_key index_key;
|
|
|
|
struct {
|
2019-06-26 20:02:32 +00:00
|
|
|
unsigned long hash;
|
2019-06-26 20:02:31 +00:00
|
|
|
unsigned long len_desc;
|
2013-09-24 09:35:15 +00:00
|
|
|
struct key_type *type; /* type of key */
|
2019-06-26 20:02:32 +00:00
|
|
|
struct key_tag *domain_tag; /* Domain of operation */
|
2013-09-24 09:35:15 +00:00
|
|
|
char *description;
|
|
|
|
};
|
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* key data
|
|
|
|
* - this is used to hold the data actually used in cryptography or
|
|
|
|
* whatever
|
|
|
|
*/
|
|
|
|
union {
|
2015-10-21 13:04:48 +00:00
|
|
|
union key_payload payload;
|
|
|
|
struct {
|
|
|
|
/* Keyring bits */
|
|
|
|
struct list_head name_link;
|
|
|
|
struct assoc_array keys;
|
|
|
|
};
|
2013-09-24 09:35:18 +00:00
|
|
|
};
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
|
|
|
|
/* This is set on a keyring to restrict the addition of a link to a key
|
2016-08-31 23:05:43 +00:00
|
|
|
* to it. If this structure isn't provided then it is assumed that the
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
* keyring is open to any addition. It is ignored for non-keyring
|
2017-03-02 00:44:09 +00:00
|
|
|
* keys. Only set this value using keyring_restrict(), keyring_alloc(),
|
|
|
|
* or key_alloc().
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
*
|
|
|
|
* This is intended for use with rings of trusted keys whereby addition
|
|
|
|
* to the keyring needs to be controlled. KEY_ALLOC_BYPASS_RESTRICTION
|
|
|
|
* overrides this, allowing the kernel to add extra keys without
|
|
|
|
* restriction.
|
|
|
|
*/
|
2016-08-31 23:05:43 +00:00
|
|
|
struct key_restriction *restrict_link;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern struct key *key_alloc(struct key_type *type,
|
|
|
|
const char *desc,
|
2012-02-08 15:53:04 +00:00
|
|
|
kuid_t uid, kgid_t gid,
|
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:23 +00:00
|
|
|
const struct cred *cred,
|
2019-07-11 01:43:43 +00:00
|
|
|
key_perm_t perm,
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
unsigned long flags,
|
2016-08-31 23:05:43 +00:00
|
|
|
struct key_restriction *restrict_link);
|
2006-06-26 07:24:50 +00:00
|
|
|
|
|
|
|
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
#define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
|
|
|
|
#define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
|
|
|
|
#define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
|
2016-04-06 15:14:26 +00:00
|
|
|
#define KEY_ALLOC_BUILT_IN 0x0004 /* Key is built into kernel */
|
|
|
|
#define KEY_ALLOC_BYPASS_RESTRICTION 0x0008 /* Override the check on restricted keyrings */
|
2017-09-18 18:37:03 +00:00
|
|
|
#define KEY_ALLOC_UID_KEYRING 0x0010 /* allocating a user or user session keyring */
|
2006-06-26 07:24:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
extern void key_revoke(struct key *key);
|
2012-05-11 09:56:56 +00:00
|
|
|
extern void key_invalidate(struct key *key);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern void key_put(struct key *key);
|
2019-06-26 20:02:32 +00:00
|
|
|
extern bool key_put_tag(struct key_tag *tag);
|
2019-06-26 20:02:32 +00:00
|
|
|
extern void key_remove_domain(struct key_tag *domain_tag);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-09-24 09:35:16 +00:00
|
|
|
static inline struct key *__key_get(struct key *key)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2017-03-31 12:20:48 +00:00
|
|
|
refcount_inc(&key->usage);
|
2005-04-16 22:20:36 +00:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2013-09-24 09:35:16 +00:00
|
|
|
static inline struct key *key_get(struct key *key)
|
|
|
|
{
|
|
|
|
return key ? __key_get(key) : key;
|
|
|
|
}
|
|
|
|
|
2005-09-28 16:03:15 +00:00
|
|
|
static inline void key_ref_put(key_ref_t key_ref)
|
|
|
|
{
|
|
|
|
key_put(key_ref_to_ptr(key_ref));
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:02:33 +00:00
|
|
|
extern struct key *request_key_tag(struct key_type *type,
|
|
|
|
const char *description,
|
|
|
|
struct key_tag *domain_tag,
|
2019-07-11 01:43:43 +00:00
|
|
|
const char *callout_info);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2019-06-19 15:10:15 +00:00
|
|
|
extern struct key *request_key_rcu(struct key_type *type,
|
2019-06-26 20:02:33 +00:00
|
|
|
const char *description,
|
|
|
|
struct key_tag *domain_tag);
|
2019-06-19 15:10:15 +00:00
|
|
|
|
2006-06-29 09:24:28 +00:00
|
|
|
extern struct key *request_key_with_auxdata(struct key_type *type,
|
|
|
|
const char *description,
|
2019-06-26 20:02:33 +00:00
|
|
|
struct key_tag *domain_tag,
|
2008-04-29 08:01:24 +00:00
|
|
|
const void *callout_info,
|
|
|
|
size_t callout_len,
|
2019-07-11 01:43:43 +00:00
|
|
|
void *aux);
|
2007-10-17 06:29:46 +00:00
|
|
|
|
2019-06-26 20:02:33 +00:00
|
|
|
/**
|
|
|
|
* request_key - Request a key and wait for construction
|
|
|
|
* @type: Type of key.
|
|
|
|
* @description: The searchable description of the key.
|
|
|
|
* @callout_info: The data to pass to the instantiation upcall (or NULL).
|
|
|
|
*
|
|
|
|
* As for request_key_tag(), but with the default global domain tag.
|
|
|
|
*/
|
|
|
|
static inline struct key *request_key(struct key_type *type,
|
|
|
|
const char *description,
|
2019-07-11 01:43:43 +00:00
|
|
|
const char *callout_info)
|
2019-06-26 20:02:33 +00:00
|
|
|
{
|
2019-07-11 01:43:43 +00:00
|
|
|
return request_key_tag(type, description, NULL, callout_info);
|
2019-06-26 20:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET
|
2019-05-20 07:48:46 +00:00
|
|
|
/**
|
2019-06-26 20:02:33 +00:00
|
|
|
* request_key_net - Request a key for a net namespace and wait for construction
|
|
|
|
* @type: Type of key.
|
|
|
|
* @description: The searchable description of the key.
|
|
|
|
* @net: The network namespace that is the key's domain of operation.
|
|
|
|
* @callout_info: The data to pass to the instantiation upcall (or NULL).
|
|
|
|
*
|
|
|
|
* As for request_key() except that it does not add the returned key to a
|
|
|
|
* keyring if found, new keys are always allocated in the user's quota, the
|
|
|
|
* callout_info must be a NUL-terminated string and no auxiliary data can be
|
|
|
|
* passed. Only keys that operate the specified network namespace are used.
|
|
|
|
*
|
|
|
|
* Furthermore, it then works as wait_for_key_construction() to wait for the
|
|
|
|
* completion of keys undergoing construction with a non-interruptible wait.
|
|
|
|
*/
|
2019-07-11 01:43:43 +00:00
|
|
|
#define request_key_net(type, description, net, callout_info) \
|
|
|
|
request_key_tag(type, description, net->key_domain, callout_info);
|
2019-05-20 07:48:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* request_key_net_rcu - Request a key for a net namespace under RCU conditions
|
|
|
|
* @type: Type of key.
|
|
|
|
* @description: The searchable description of the key.
|
|
|
|
* @net: The network namespace that is the key's domain of operation.
|
|
|
|
*
|
|
|
|
* As for request_key_rcu() except that only keys that operate the specified
|
|
|
|
* network namespace are used.
|
|
|
|
*/
|
|
|
|
#define request_key_net_rcu(type, description, net) \
|
|
|
|
request_key_rcu(type, description, net->key_domain);
|
2019-06-26 20:02:33 +00:00
|
|
|
#endif /* CONFIG_NET */
|
|
|
|
|
2007-10-17 06:29:46 +00:00
|
|
|
extern int wait_for_key_construction(struct key *key, bool intr);
|
|
|
|
|
2012-05-15 13:11:11 +00:00
|
|
|
extern int key_validate(const struct key *key);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-09-28 16:03:15 +00:00
|
|
|
extern key_ref_t key_create_or_update(key_ref_t keyring,
|
|
|
|
const char *type,
|
|
|
|
const char *description,
|
|
|
|
const void *payload,
|
|
|
|
size_t plen,
|
2019-07-11 01:43:43 +00:00
|
|
|
key_perm_t perm,
|
2006-06-26 07:24:50 +00:00
|
|
|
unsigned long flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-09-28 16:03:15 +00:00
|
|
|
extern int key_update(key_ref_t key,
|
2005-04-16 22:20:36 +00:00
|
|
|
const void *payload,
|
|
|
|
size_t plen);
|
|
|
|
|
|
|
|
extern int key_link(struct key *keyring,
|
|
|
|
struct key *key);
|
|
|
|
|
2019-05-20 20:51:50 +00:00
|
|
|
extern int key_move(struct key *key,
|
|
|
|
struct key *from_keyring,
|
|
|
|
struct key *to_keyring,
|
|
|
|
unsigned int flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
extern int key_unlink(struct key *keyring,
|
|
|
|
struct key *key);
|
|
|
|
|
2012-02-08 15:53:04 +00:00
|
|
|
extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
|
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:23 +00:00
|
|
|
const struct cred *cred,
|
2019-07-11 01:43:43 +00:00
|
|
|
key_perm_t perm,
|
2006-06-26 07:24:50 +00:00
|
|
|
unsigned long flags,
|
2016-08-31 23:05:43 +00:00
|
|
|
struct key_restriction *restrict_link,
|
2006-06-22 21:47:17 +00:00
|
|
|
struct key *dest);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
extern int restrict_link_reject(struct key *keyring,
|
|
|
|
const struct key_type *type,
|
2016-08-30 18:33:13 +00:00
|
|
|
const union key_payload *payload,
|
|
|
|
struct key *restriction_key);
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
extern int keyring_clear(struct key *keyring);
|
|
|
|
|
2005-09-28 16:03:15 +00:00
|
|
|
extern key_ref_t keyring_search(key_ref_t keyring,
|
|
|
|
struct key_type *type,
|
2019-06-26 20:02:32 +00:00
|
|
|
const char *description,
|
|
|
|
bool recurse);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
extern int keyring_add_key(struct key *keyring,
|
|
|
|
struct key *key);
|
|
|
|
|
2017-03-02 00:44:09 +00:00
|
|
|
extern int keyring_restrict(key_ref_t keyring, const char *type,
|
|
|
|
const char *restriction);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
extern struct key *key_lookup(key_serial_t id);
|
|
|
|
|
2012-01-18 10:04:29 +00:00
|
|
|
static inline key_serial_t key_serial(const struct key *key)
|
2008-04-29 08:01:34 +00:00
|
|
|
{
|
|
|
|
return key ? key->serial : 0;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-02-24 19:14:50 +00:00
|
|
|
extern void key_set_timeout(struct key *, unsigned);
|
|
|
|
|
2018-12-04 18:31:27 +00:00
|
|
|
extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
|
2020-05-12 14:16:29 +00:00
|
|
|
enum key_need_perm need_perm);
|
2019-06-26 20:02:32 +00:00
|
|
|
extern void key_free_user_ns(struct user_namespace *);
|
2018-12-04 18:31:27 +00:00
|
|
|
|
2017-10-04 15:43:25 +00:00
|
|
|
static inline short key_read_state(const struct key *key)
|
|
|
|
{
|
|
|
|
/* Barrier versus mark_key_instantiated(). */
|
|
|
|
return smp_load_acquire(&key->state);
|
|
|
|
}
|
|
|
|
|
2011-03-11 17:57:23 +00:00
|
|
|
/**
|
2017-10-04 15:43:25 +00:00
|
|
|
* key_is_positive - Determine if a key has been positively instantiated
|
2011-03-11 17:57:23 +00:00
|
|
|
* @key: The key to check.
|
|
|
|
*
|
|
|
|
* Return true if the specified key has been positively instantiated, false
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2017-10-04 15:43:25 +00:00
|
|
|
static inline bool key_is_positive(const struct key *key)
|
|
|
|
{
|
|
|
|
return key_read_state(key) == KEY_IS_POSITIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool key_is_negative(const struct key *key)
|
2011-03-11 17:57:23 +00:00
|
|
|
{
|
2017-10-04 15:43:25 +00:00
|
|
|
return key_read_state(key) < 0;
|
2011-03-11 17:57:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:11:23 +00:00
|
|
|
#define dereference_key_rcu(KEY) \
|
|
|
|
(rcu_dereference((KEY)->payload.rcu_data0))
|
|
|
|
|
|
|
|
#define dereference_key_locked(KEY) \
|
2015-10-21 13:04:48 +00:00
|
|
|
(rcu_dereference_protected((KEY)->payload.rcu_data0, \
|
2011-03-07 15:05:51 +00:00
|
|
|
rwsem_is_locked(&((struct key *)(KEY))->sem)))
|
|
|
|
|
2012-01-17 20:39:51 +00:00
|
|
|
#define rcu_assign_keypointer(KEY, PAYLOAD) \
|
2012-05-16 23:31:38 +00:00
|
|
|
do { \
|
2015-10-21 13:04:48 +00:00
|
|
|
rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \
|
2012-05-16 23:31:38 +00:00
|
|
|
} while (0)
|
2012-01-17 20:39:51 +00:00
|
|
|
|
2008-04-29 08:01:32 +00:00
|
|
|
#ifdef CONFIG_SYSCTL
|
2014-06-06 21:38:06 +00:00
|
|
|
extern struct ctl_table key_sysctls[];
|
2008-04-29 08:01:32 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* the userspace interface
|
|
|
|
*/
|
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:23 +00:00
|
|
|
extern int install_thread_keyring_to_cred(struct cred *cred);
|
2019-05-22 13:06:51 +00:00
|
|
|
extern void key_fsuid_changed(struct cred *new_cred);
|
|
|
|
extern void key_fsgid_changed(struct cred *new_cred);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern void key_init(void);
|
|
|
|
|
|
|
|
#else /* CONFIG_KEYS */
|
|
|
|
|
|
|
|
#define key_validate(k) 0
|
|
|
|
#define key_serial(k) 0
|
2005-06-24 05:00:51 +00:00
|
|
|
#define key_get(k) ({ NULL; })
|
2008-10-12 04:10:50 +00:00
|
|
|
#define key_revoke(k) do { } while(0)
|
2012-05-11 09:56:56 +00:00
|
|
|
#define key_invalidate(k) do { } while(0)
|
2005-04-16 22:20:36 +00:00
|
|
|
#define key_put(k) do { } while(0)
|
2005-09-28 16:03:15 +00:00
|
|
|
#define key_ref_put(k) do { } while(0)
|
KEYS: Alter use of key instantiation link-to-keyring argument
Alter the use of the key instantiation and negation functions' link-to-keyring
arguments. Currently this specifies a keyring in the target process to link
the key into, creating the keyring if it doesn't exist. This, however, can be
a problem for copy-on-write credentials as it means that the instantiating
process can alter the credentials of the requesting process.
This patch alters the behaviour such that:
(1) If keyctl_instantiate_key() or keyctl_negate_key() are given a specific
keyring by ID (ringid >= 0), then that keyring will be used.
(2) If keyctl_instantiate_key() or keyctl_negate_key() are given one of the
special constants that refer to the requesting process's keyrings
(KEY_SPEC_*_KEYRING, all <= 0), then:
(a) If sys_request_key() was given a keyring to use (destringid) then the
key will be attached to that keyring.
(b) If sys_request_key() was given a NULL keyring, then the key being
instantiated will be attached to the default keyring as set by
keyctl_set_reqkey_keyring().
(3) No extra link will be made.
Decision point (1) follows current behaviour, and allows those instantiators
who've searched for a specifically named keyring in the requestor's keyring so
as to partition the keys by type to still have their named keyrings.
Decision point (2) allows the requestor to make sure that the key or keys that
get produced by request_key() go where they want, whilst allowing the
instantiator to request that the key is retained. This is mainly useful for
situations where the instantiator makes a secondary request, the key for which
should be retained by the initial requestor:
+-----------+ +--------------+ +--------------+
| | | | | |
| Requestor |------->| Instantiator |------->| Instantiator |
| | | | | |
+-----------+ +--------------+ +--------------+
request_key() request_key()
This might be useful, for example, in Kerberos, where the requestor requests a
ticket, and then the ticket instantiator requests the TGT, which someone else
then has to go and fetch. The TGT, however, should be retained in the
keyrings of the requestor, not the first instantiator. To make this explict
an extra special keyring constant is also added.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:14 +00:00
|
|
|
#define make_key_ref(k, p) NULL
|
|
|
|
#define key_ref_to_ptr(k) NULL
|
2005-09-28 16:03:15 +00:00
|
|
|
#define is_key_possessed(k) 0
|
2019-05-22 13:06:51 +00:00
|
|
|
#define key_fsuid_changed(c) do { } while(0)
|
|
|
|
#define key_fsgid_changed(c) do { } while(0)
|
2005-04-16 22:20:36 +00:00
|
|
|
#define key_init() do { } while(0)
|
2019-06-26 20:02:32 +00:00
|
|
|
#define key_free_user_ns(ns) do { } while(0)
|
2019-06-26 20:02:32 +00:00
|
|
|
#define key_remove_domain(d) do { } while(0)
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#endif /* CONFIG_KEYS */
|
|
|
|
#endif /* __KERNEL__ */
|
|
|
|
#endif /* _LINUX_KEY_H */
|