Skip to content

Commit ede0fa9

Browse files
ebiggersJames Morris
authored andcommitted
KEYS: always initialize keyring_index_key::desc_len
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: b2a4df2 ("KEYS: Expand the capacity of a keyring") Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: David Howells <dhowells@redhat.com> Cc: stable@vger.kernel.org Signed-off-by: James Morris <james.morris@microsoft.com>
1 parent cc1780f commit ede0fa9

File tree

4 files changed

+4
-6
lines changed

4 files changed

+4
-6
lines changed

security/keys/keyring.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,6 @@ static bool search_nested_keyrings(struct key *keyring,
661661
BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
662662
(ctx->flags & STATE_CHECKS) == STATE_CHECKS);
663663

664-
if (ctx->index_key.description)
665-
ctx->index_key.desc_len = strlen(ctx->index_key.description);
666-
667664
/* Check to see if this top-level keyring is what we are looking for
668665
* and whether it is valid or not.
669666
*/
@@ -914,6 +911,7 @@ key_ref_t keyring_search(key_ref_t keyring,
914911
struct keyring_search_context ctx = {
915912
.index_key.type = type,
916913
.index_key.description = description,
914+
.index_key.desc_len = strlen(description),
917915
.cred = current_cred(),
918916
.match_data.cmp = key_default_cmp,
919917
.match_data.raw_data = description,

security/keys/proc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ static int proc_keys_show(struct seq_file *m, void *v)
165165
int rc;
166166

167167
struct keyring_search_context ctx = {
168-
.index_key.type = key->type,
169-
.index_key.description = key->description,
168+
.index_key = key->index_key,
170169
.cred = m->file->f_cred,
171170
.match_data.cmp = lookup_user_key_possessed,
172171
.match_data.raw_data = key,

security/keys/request_key.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ struct key *request_key_and_link(struct key_type *type,
531531
struct keyring_search_context ctx = {
532532
.index_key.type = type,
533533
.index_key.description = description,
534+
.index_key.desc_len = strlen(description),
534535
.cred = current_cred(),
535536
.match_data.cmp = key_default_cmp,
536537
.match_data.raw_data = description,

security/keys/request_key_auth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id)
247247
struct key *authkey;
248248
key_ref_t authkey_ref;
249249

250-
sprintf(description, "%x", target_id);
250+
ctx.index_key.desc_len = sprintf(description, "%x", target_id);
251251

252252
authkey_ref = search_process_keyrings(&ctx);
253253

0 commit comments

Comments
 (0)