diff options
author | Eric Biggers <ebiggers@google.com> | 2019-02-22 15:36:18 +0000 |
---|---|---|
committer | Ben Hutchings <ben@decadent.org.uk> | 2019-05-02 21:41:58 +0100 |
commit | 696bfa8c3d86e000b4a01fd391020e71d4fc1a9b (patch) | |
tree | 1613a2bd9acfd2b01d9101b661aa15bcf6ab29bb | |
parent | 1a844ebde7e7de0970d028faca990507e4287104 (diff) | |
download | linux-stable-696bfa8c3d86e000b4a01fd391020e71d4fc1a9b.tar.gz linux-stable-696bfa8c3d86e000b4a01fd391020e71d4fc1a9b.tar.bz2 linux-stable-696bfa8c3d86e000b4a01fd391020e71d4fc1a9b.zip |
KEYS: always initialize keyring_index_key::desc_len
commit ede0fa98a900e657d1fcd80b50920efc896c1a4c upstream.
syzbot hit the 'BUG_ON(index_key->desc_len == 0);' in __key_link_begin()
called from construct_alloc_key() during sys_request_key(), because the
length of the key description was never calculated.
The problem is that we rely on ->desc_len being initialized by
search_process_keyrings(), specifically by search_nested_keyrings().
But, if the process isn't subscribed to any keyrings that never happens.
Fix it by always initializing keyring_index_key::desc_len as soon as the
description is set, like we already do in some places.
The following program reproduces the BUG_ON() when it's run as root and
no session keyring has been installed. If it doesn't work, try removing
pam_keyinit.so from /etc/pam.d/login and rebooting.
#include <stdlib.h>
#include <unistd.h>
#include <keyutils.h>
int main(void)
{
int id = add_key("keyring", "syz", NULL, 0, KEY_SPEC_USER_KEYRING);
keyctl_setperm(id, KEY_OTH_WRITE);
setreuid(5000, 5000);
request_key("user", "desc", "", id);
}
Reported-by: syzbot+ec24e95ea483de0a24da@syzkaller.appspotmail.com
Fixes: b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: James Morris <james.morris@microsoft.com>
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
-rw-r--r-- | security/keys/keyring.c | 4 | ||||
-rw-r--r-- | security/keys/proc.c | 3 | ||||
-rw-r--r-- | security/keys/request_key.c | 1 | ||||
-rw-r--r-- | security/keys/request_key_auth.c | 2 |
4 files changed, 4 insertions, 6 deletions
diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 0fa27abd3777..f906d73c2596 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -604,9 +604,6 @@ static bool search_nested_keyrings(struct key *keyring, ctx->index_key.type->name, ctx->index_key.description); - if (ctx->index_key.description) - ctx->index_key.desc_len = strlen(ctx->index_key.description); - /* Check to see if this top-level keyring is what we are looking for * and whether it is valid or not. */ @@ -870,6 +867,7 @@ key_ref_t keyring_search(key_ref_t keyring, struct keyring_search_context ctx = { .index_key.type = type, .index_key.description = description, + .index_key.desc_len = strlen(description), .cred = current_cred(), .match = type->match, .match_data = description, diff --git a/security/keys/proc.c b/security/keys/proc.c index 0115fc283000..35a9e3bef76f 100644 --- a/security/keys/proc.c +++ b/security/keys/proc.c @@ -191,8 +191,7 @@ static int proc_keys_show(struct seq_file *m, void *v) int rc; struct keyring_search_context ctx = { - .index_key.type = key->type, - .index_key.description = key->description, + .index_key = key->index_key, .cred = m->file->f_cred, .match = lookup_user_key_possessed, .match_data = key, diff --git a/security/keys/request_key.c b/security/keys/request_key.c index c120a393ff2d..55ca39acb49e 100644 --- a/security/keys/request_key.c +++ b/security/keys/request_key.c @@ -561,6 +561,7 @@ struct key *request_key_and_link(struct key_type *type, struct keyring_search_context ctx = { .index_key.type = type, .index_key.description = description, + .index_key.desc_len = strlen(description), .cred = current_cred(), .match = type->match, .match_data = description, diff --git a/security/keys/request_key_auth.c b/security/keys/request_key_auth.c index 74e3fe9b47be..ac4a90b494e6 100644 --- a/security/keys/request_key_auth.c +++ b/security/keys/request_key_auth.c @@ -233,7 +233,7 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id) struct key *authkey; key_ref_t authkey_ref; - sprintf(description, "%x", target_id); + ctx.index_key.desc_len = sprintf(description, "%x", target_id); authkey_ref = search_process_keyrings(&ctx); |