android_kernel_samsung_msm8226/security/keys
Eric Biggers b3482c9a41 KEYS: add missing permission check for request_key() destination
commit 4dca6ea1d9432052afb06baf2e3ae78188a4410b upstream.

When the request_key() syscall is not passed a destination keyring, it
links the requested key (if constructed) into the "default" request-key
keyring.  This should require Write permission to the keyring.  However,
there is actually no permission check.

This can be abused to add keys to any keyring to which only Search
permission is granted.  This is because Search permission allows joining
the keyring.  keyctl_set_reqkey_keyring(KEY_REQKEY_DEFL_SESSION_KEYRING)
then will set the default request-key keyring to the session keyring.
Then, request_key() can be used to add keys to the keyring.

Both negatively and positively instantiated keys can be added using this
method.  Adding negative keys is trivial.  Adding a positive key is a
bit trickier.  It requires that either /sbin/request-key positively
instantiates the key, or that another thread adds the key to the process
keyring at just the right time, such that request_key() misses it
initially but then finds it in construct_alloc_key().

Fix this bug by checking for Write permission to the keyring in
construct_get_dest_keyring() when the default keyring is being used.

We don't do the permission check for non-default keyrings because that
was already done by the earlier call to lookup_user_key().  Also,
request_key_and_link() is currently passed a 'struct key *' rather than
a key_ref_t, so the "possessed" bit is unavailable.

We also don't do the permission check for the "requestor keyring", to
continue to support the use case described by commit 8bbf4976b5
("KEYS: Alter use of key instantiation link-to-keyring argument") where
/sbin/request-key recursively calls request_key() to add keys to the
original requestor's destination keyring.  (I don't know of any users
who actually do that, though...)

Fixes: 3e30148c3d ("[PATCH] Keys: Make request-key create an authorisation key")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
[bwh: Backported to 3.2:
 - s/KEY_NEED_WRITE/KEY_WRITE/
 - Adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
CVE-2017-17807
Signed-off-by: Kevin F. Haggerty <haggertk@lineageos.org>

Change-Id: I3fefd566fe562f342776e8612cfd84d38ec4e658
2020-01-06 08:40:27 +01:00
..
encrypted-keys KEYS: Fix handling of stored error in a negatively instantiated user key 2019-08-06 11:48:16 +02:00
Makefile
compat.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
gc.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
internal.h
key.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
keyctl.c KEYS: Disallow keyrings beginning with '.' to be joined as session keyrings 2019-08-06 12:24:25 +02:00
keyring.c
permission.c
proc.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
process_keys.c UPSTREAM: capabilities: ambient capabilities 2019-08-08 15:08:07 +02:00
request_key.c KEYS: add missing permission check for request_key() destination 2020-01-06 08:40:27 +01:00
request_key_auth.c
sysctl.c
trusted.c KEYS: Fix handling of stored error in a negatively instantiated user key 2019-08-06 11:48:16 +02:00
trusted.h
user_defined.c KEYS: Fix handling of stored error in a negatively instantiated user key 2019-08-06 11:48:16 +02:00