> KEYS: Replace assoc_array with rhashtable

TL;DR: keyring_read() now needs keyring->sem, but s390 cert_store calls it
unlocked; this may trigger a lockdep splat and possibly a use-after-free if a
link or index is freed during the walk.

> diff --git a/security/keys/keyring.c b/security/keys/keyring.c
> --- a/security/keys/keyring.c
> +++ b/security/keys/keyring.c

[ ... ]

>  static long keyring_read(const struct key *keyring,
>                        char *buffer, size_t buflen)
>  {
> -     struct keyring_read_iterator_context ctx;
> +     struct keyring_index *links = keyring_index_locked(keyring);
> +     key_serial_t *serials = (key_serial_t *)buffer;
> +     struct keyring_link *link;
> +     size_t count = 0;
>       long ret;
>  
>       kenter("{%d},,%zu", key_serial(keyring), buflen);

[ ... ]

> +     if (buffer && buflen && links) {
> +             list_for_each_entry(link, &links->list, list) {
> +                     if (count == buflen / sizeof(*serials))
> +                             break;
> +
> +                     serials[count++] = keyring_link_key(link)->serial;
> +                     cond_resched();
> +             }
>       }

keyring_index_locked() is rcu_dereference_protected() on
lockdep_is_held(&keyring->sem), and the list is then walked with plain
list_for_each_entry() and a cond_resched() per entry.  That is fine when
the caller holds keyring->sem, as __keyctl_read_key() does.

Is that also true for all callers of the read method?  The s390 certificate
store calls it with no lock held:

arch/s390/kernel/cert_store.c:invalidate_keyring_keys() {
    keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
    ...
    rc = key_type_keyring.read(keyring, (char *)key_array,
                               keyring_payload_len);
}

The path is refresh_store() -> fill_cs_keyring() -> create_cs_keyring() ->
cleanup_cs_keys() -> find_cs_keyring() -> invalidate_keyring_keys(), and it
holds only cs_refresh_lock.

Before this patch the (keyring, NULL, 0) size query only read
nr_leaves_on_tree, and the real read used assoc_array_iterate(), which has
no lockdep check and never scheduled.  Now keyring_index_locked() runs at
function entry, so with CONFIG_PROVE_RCU=y a second cert_store refresh
looks like it would report a suspicious rcu_dereference_protected() usage.

The walk also runs with neither keyring->sem nor rcu_read_lock(), and
cond_resched() lets a grace period complete while the walker is sitting on
a link.  Can a concurrent key_unlink() or keyring_gc() (list_del_rcu() plus
call_rcu(keyring_link_free_rcu)), or keyring_clear() or keyring_revoke()
(keyring_index_free() -> queue_rcu_work()), free the link or the whole
index under the walker?

Should invalidate_keyring_keys() take down_read(&keyring->sem) around its
two read calls as part of this conversion?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/43549115625

Reply via email to