KEYS: rhashtable bug fixes
Subsystems (3)
Jarkko Sakkinen
maintainerv2 cover KEYS: rhashtable bug fixes
My patch set that migrates assoc_array to rhashtable had some LLM scavegend bug fixes. Unfortunately most them where either plain wrong or not in the right scope. The bug fixes in this patch set are by human labour constructed fully but they are derived from what I got out of the original LLM source. Thus, they are tagged as LLM assisted.
Changelog
=========
- v2: There was enough tweaks that a sanity check round makes sense just in case.
- v2: Addressed issues pointed out by David.
- v1: https://lore.kernel.org/keyrings/[email protected]/
Jarkko Sakkinen (6):
KEYS: Expand key->{datalen,quotalen} to unsigned int
KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
KEYS: Prevent overflow of user->{qnkeys,qnbytes} in key_alloc()
KEYS: Prevent overflow of quota in key_alloc()
KEYS: Reject @datalen values over INT_MAX
KEYS: Verify the key identity in __keyctl_unlink
include/linux/key.h | 6 ++++-- security/keys/key.c | 9 +++++++++ security/keys/keyctl.c | 2 -- security/keys/keyring.c | 6 ++++++ 4 files changed, 19 insertions(+), 4 deletions(-)
--
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 1/6 KEYS: Expand key->{datalen,quotalen} to unsigned int
In key_alloc() and key_payload_reserve(), key->quotalen and key->datalen can receive values larger than USHRT_MAX.
Change their types from unsigned short to unsigned int so that they can carry the full payload and avoid truncation.
--- include/linux/key.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/key.h +2 -2
@@ -216,8 +216,8 @@ struct key {kuid_t uid;kgid_t gid;key_perm_t perm;/* access permissions */ -unsigned short quotalen;/* length added to quota */ -unsigned short datalen;/* payload data length +unsigned int quotalen;/* length added to quota */ +unsigned int datalen;/* payload data length * - may not match RCU dereferenced payload * - payload should contain own length */
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 2/6 KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
In reply to Jarkko Sakkinen@desclen in key_alloc() is saved to the 16-bit wide desc_len field in struct keyring_index_key.
Reject @desclen values at or above KEY_MAX_DESC_SIZE with -EINVAL in order to account for the NUL terminator and prevent integer overflow.
--- include/linux/key.h | 2 ++ security/keys/key.c | 2 ++ security/keys/keyctl.c | 2 -- 3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/key.h +2 -0
@@ -37,6 +37,8 @@ struct net;#undef KEY_DEBUGGING +#define KEY_MAX_DESC_SIZE 4096 +#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 */
security/keys/key.c +2 -0
@@ -245,6 +245,8 @@ struct key *key_alloc(struct key_type *type, const char *desc, } desclen =strlen (desc); +if (desclen >=KEY_MAX_DESC_SIZE ) + goto error; quotalen = desclen +1 + type->def_datalen;/* get hold of the key tracking for this user */
security/keys/keyctl.c +0 -2
@@ -24,8 +24,6 @@#include <keys/request_key_auth-type.h> #include "internal.h" -#define KEY_MAX_DESC_SIZE 4096 -static const unsigned char keyrings_capabilities[2 ] = { [0] = (KEYCTL_CAPS0_CAPABILITIES | (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 3/6 KEYS: Prevent overflow of user->{qnkeys,qnbytes} in key_alloc()
In reply to Jarkko SakkinenAdd the missing overflow checks for user->qnkeys and user->qnbytes in order keep calculations within INT_MAX range, which is the absolute maximum quota.
--- security/keys/key.c | 4 ++++ 1 file changed, 4 insertions(+)
security/keys/key.c +4 -0
@@ -263,6 +263,10 @@ struct key *key_alloc(struct key_type *type, const char *desc, key_quota_root_maxbytes : key_quota_maxbytes;spin_lock_irqsave (&user->lock, irqflags); +if (user->qnkeys ==INT_MAX || + quotalen >INT_MAX - user->qnbytes) + goto no_quota; +if (!(flags &KEY_ALLOC_QUOTA_OVERRUN )) { if (user->qnkeys + 1 > maxkeys || user->qnbytes + quotalen > maxbytes ||
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 4/6 KEYS: Prevent overflow of quota in key_alloc()
In reply to Jarkko SakkinenCheck that the calculated quota length does not go over INT_MAX.
--- security/keys/key.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/key.c +1 -1
@@ -245,7 +245,7 @@ struct key *key_alloc(struct key_type *type, const char *desc, } desclen =strlen (desc); -if (desclen >=KEY_MAX_DESC_SIZE ) +if (desclen >=KEY_MAX_DESC_SIZE || type->def_datalen >INT_MAX - desclen -1 ) goto error; quotalen = desclen +1 + type->def_datalen;
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 5/6 KEYS: Reject @datalen values over INT_MAX
In reply to Jarkko SakkinenProtect quota from overflowing by rejecting @datalen values that surpass over INT_MAX.
--- security/keys/key.c | 3 +++ 1 file changed, 3 insertions(+)
security/keys/key.c +3 -0
@@ -384,6 +384,9 @@ int key_payload_reserve(struct key *key, size_t datalen)key_check (key); +if (datalen >INT_MAX ) +return -EINVAL ; +/* contemplate the quota adjustment */ if (delta !=0 &&test_bit (KEY_FLAG_IN_QUOTA , &key->flags)) { unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ?
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 6/6 KEYS: Verify the key identity in __keyctl_unlink
In reply to Jarkko SakkinenLet's define the following preconditions:
- Two keys: A and B.
- R is a keyring.
- Both have the same type/description/domain.
Then:
1. KEYCTL_LINK(A, R) => {A}
1. KEYCTL_LINK(B, R) => {B}
2. KEYCTL_UNLINK(A, R) => {}
Instead, verify key tidentity before unlinking, and return -ENOENT on mismatch.
--- security/keys/keyring.c | 6 ++++++ 1 file changed, 6 insertions(+)
security/keys/keyring.c +6 -0
@@ -1501,9 +1501,15 @@ static int __key_unlink_begin(struct key *keyring, struct key *key, struct assoc_array_edit **_edit) {struct assoc_array_edit *edit; +const void *object;BUG_ON (*_edit !=NULL ); + object =assoc_array_find (&keyring->keys, &keyring_assoc_array_ops, + &key->index_key); +if (!object ||keyring_ptr_to_key (object) != key) +return -ENOENT ; + edit =assoc_array_delete (&keyring->keys, &keyring_assoc_array_ops, &key->index_key);if (IS_ERR (edit))
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v2 6/6] KEYS: Verify the key identity in __keyctl_unlink
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines
Let's define the following preconditions:
- Two keys: A and B.
- R is a keyring.
- Both have the same type/description/domain.Then:
1.
KEYCTL_LINK(A, R)=> {A}
1.KEYCTL_LINK(B, R)=> {B}
2.KEYCTL_UNLINK(A, R)=> {}
Quoted 8 lines
Instead, verify key tidentity before unlinking, and return
-ENOENTon mismatch.Assisted-by LLMFixes b2a4df200d57 ("KEYS: Expand the capacity of a keyring")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v2 4/6] KEYS: Prevent overflow of quota in key_alloc()
In reply to Jarkko SakkinenSashiko has reviewed this patch and found no issues.
Quoted 7 lines
Check that the calculated quota length does not go over
INT_MAX.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v2 5/6] KEYS: Reject @datalen values over INT_MAX
In reply to Jarkko SakkinenSashiko has reviewed this patch and found no issues.
Quoted 8 lines
Protect quota from overflowing by rejecting @datalen values that surpass over
INT_MAX.Assisted-by LLMFixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v2 1/6] KEYS: Expand key->{datalen,quotalen} to unsigned int
In reply to Jarkko SakkinenSashiko has reviewed this patch and found no issues.
Quoted 11 lines
In
key_alloc()andkey_payload_reserve(),key->quotalenandkey->datalencan receive values larger thanUSHRT_MAX.Change their types from unsigned short to unsigned int so that they can carry the full payload and avoid truncation.
Assisted-by LLMFixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v2 3/6] KEYS: Prevent overflow of user->{qnkeys,qnbytes} in key_alloc()
In reply to Jarkko SakkinenSashiko has reviewed this patch and found no issues.
Quoted 9 lines
Add the missing overflow checks for
user->qnkeysanduser->qnbytesin order keep calculations withinINT_MAXrange, which is the absolute maximum quota.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Jarkko Sakkinen
maintainerQuoted 22 lines · Jarkko Sakkinen
My patch set that migrates
assoc_arrayto rhashtable had some LLM scavegend bug fixes. Unfortunately most them where either plain wrong or not in the right scope. The bug fixes in this patch set are by human labour constructed fully but they are derived from what I got out of the original LLM source. Thus, they are tagged as LLM assisted.Changelog
=========- v2: There was enough tweaks that a sanity check round makes sense just in case.
- v2: Addressed issues pointed out by David.
- v1: https://lore.kernel.org/keyrings/[email protected]/Jarkko Sakkinen (6):
KEYS: Expand key->{datalen,quotalen} to unsigned int
KEYS: Reject @desclen values >=KEY_MAX_DESC_SIZEinkey_alloc()
KEYS: Prevent overflow of user->{qnkeys,qnbytes} inkey_alloc()
KEYS: Prevent overflow of quota inkey_alloc()
KEYS: Reject @datalen values overINT_MAX
KEYS: Verify the key identity in__keyctl_unlink
Quoted 9 lines
include/linux/key.h | 6 ++++-- security/keys/key.c | 9 +++++++++ security/keys/keyctl.c | 2 -- security/keys/keyring.c | 6 ++++++ 4 files changed, 19 insertions(+), 4 deletions(-)--
2.47.3
The benefit with this slicing is at least that backport conflicts should be easy to resolve, and if I recall correctly all go to v5.10+ stable kernels.
Br, Jarkko
[email protected]
AI reviewRe: [PATCH v2 2/6] KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
In reply to Jarkko SakkinenThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] keys: integer truncation of desc_len in __key_create_or_update() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] keys: integer truncation of desc_len in __key_create_or_update() --
commit 40e3162313abde79a8374f519fc697c857a50981
Author: Jarkko Sakkinen <[email protected]>
KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
This commit moves KEY_MAX_DESC_SIZE to include/linux/key.h and adds a check in key_alloc() to reject descriptions with a length at or above KEY_MAX_DESC_SIZE with -EINVAL so that the 16-bit desc_len field in struct keyring_index_key does not overflow.
security/keys/key.c struct key *key_alloc(struct key_type *type, const char *desc, · line 252
Quoted 13 lines
security/keys/key.c +2
-0@@ -245,6 +245,8 @@ struct key *key_alloc(struct key_type *type, const char *desc, } desclen =strlen (desc); +if (desclen >=KEY_MAX_DESC_SIZE ) + goto error; quotalen = desclen +1 + type->def_datalen;/* get hold of the key tracking for this user */
[Severity: Critical]
Is checking desclen in key_alloc() too late for the __key_create_or_update() path?
In security/keys/key.c:__key_create_or_update(), strlen(index_key.description) is assigned to the 16-bit index_key.desc_len field and passed to key_set_index_key(), __key_link_begin(), and find_key_to_update() before key_alloc() is ever called:
security/keys/key.c:__key_create_or_update() {
...
if (index_key.type->preparse) {
ret = index_key.type->preparse(&prep);
if (ret < 0) {
key_ref = ERR_PTR(ret);
goto error_free_prep;
}
if (!index_key.description)
index_key.description = prep.description;
key_ref = ERR_PTR(-EINVAL);
if (!index_key.description)
goto error_free_prep;
}
index_key.desc_len = strlen(index_key.description);
key_set_index_key(&index_key);
ret = __key_link_lock(keyring, &index_key);
if (ret < 0) {
key_ref = ERR_PTR(ret);
goto error_free_prep;
}
ret = __key_link_begin(keyring, &index_key, &edit);
...
}When userspace calls sys_add_key() with a NULL _description, the strndup_user(_description, KEY_MAX_DESC_SIZE) check in sys_add_key() is skipped and index_key.type->preparse() can generate prep.description from the payload (for example, x509_key_preparse() in crypto/asymmetric_keys/x509_public_key.c builds prep->description from the certificate subject and serial/SKID without a KEY_MAX_DESC_SIZE limit). In-kernel callers of key_create_or_update() and key_create() also pass description directly into __key_create_or_update().
If strlen(index_key.description) is a multiple of 65536 (or 0), index_key.desc_len truncates to 0 and triggers BUG_ON(index_key->desc_len == 0) in security/keys/keyring.c:__key_link_begin() before key_alloc() is reached. If strlen(index_key.description) > 65535 truncates to a non-zero 16-bit length L, key_set_index_key() and find_key_to_update() hash and match only the first L bytes of index_key.description, which can collide with and update an existing key with that L-byte prefix without ever reaching key_alloc().
Should __key_create_or_update() also validate that the description length is non-zero and less than KEY_MAX_DESC_SIZE before assigning index_key.desc_len?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 1/6] KEYS: Expand key->{datalen,quotalen} to unsigned int
In reply to Jarkko SakkinenQuoted 2 lines · Jarkko Sakkinen
In
key_alloc()andkey_payload_reserve(),key->quotalenandkey->datalencan receive values larger thanUSHRT_MAX.
Could use an explanation about how this can happen. A keyring with more than 16383 links, I guess?
- Eric
Re: [PATCH v2 2/6] KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
In reply to Jarkko SakkinenQuoted 5 lines · Jarkko Sakkinen
@desclen in
key_alloc()is saved to the 16-bit widedesc_lenfield in structkeyring_index_key.Reject @desclen values at or above
KEY_MAX_DESC_SIZEwith-EINVALin order to account for the NUL terminator and prevent integer overflow.
This should say something like: "in order to match the userspace limit and avoid u16 truncation".
There are also similar truncations of desc_len to u16 in key_create_or_update() and keyring_search().
Quoted 1 line
+#define
KEY_MAX_DESC_SIZE4096
Could use a comment mentioning that it includes the trailing NUL.
- Eric
Re: [PATCH v2 3/6] KEYS: Prevent overflow of user->{qnkeys,qnbytes} in key_alloc()
In reply to Jarkko SakkinenQuoted 9 lines · Jarkko Sakkinen
Add the missing overflow checks for
user->qnkeysanduser->qnbytesin order keep calculations withinINT_MAXrange, which is the absolute maximum quota.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Don't keyctl_chown_key() and key_payload_reserve() need similar fixes?
- Eric
Re: [PATCH v2 4/6] KEYS: Prevent overflow of quota in key_alloc()
In reply to Jarkko SakkinenQuoted 7 lines · Jarkko Sakkinen
Check that the calculated quota length does not go over
INT_MAX.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Seems this should be merged into patch 3?
Also is this reachable? I.e. do any of the key types actually specify def_datalen > INT_MAX - KEY_MAX_DESC_SIZE + 1?
- Eric
Re: [PATCH v2 5/6] KEYS: Reject @datalen values over INT_MAX
In reply to Jarkko SakkinenQuoted 2 lines · Jarkko Sakkinen
Protect quota from overflowing by rejecting @datalen values that surpass over
INT_MAX.
Is there an example of how this can happen, and why the limit needs to still be so large? add_key() for example limits the payload length to 1 MiB.
- Eric
Re: [PATCH v2 6/6] KEYS: Verify the key identity in __keyctl_unlink
In reply to Jarkko SakkinenQuoted 1 line · Jarkko Sakkinen
[PATCH v2 6/6] KEYS: Verify the key identity in
__keyctl_unlink
There is no __keyctl_unlink. Seems you mean __key_unlink_begin.
Quoted 11 lines
Let's define the following preconditions:
- Two keys: A and B.
- R is a keyring.
- Both have the same type/description/domain.Then:
1.
KEYCTL_LINK(A, R)=> {A}
1.KEYCTL_LINK(B, R)=> {B}
2.KEYCTL_UNLINK(A, R)=> {}
Should be 1, 2, and 3?
Quoted 1 line
Instead, verify key tidentity before unlinking, and return
-ENOENTon
tidentity => identity
Also should explicitly say what the bug is.
- Eric
Jarkko Sakkinen
maintainerRe: [PATCH v2 1/6] KEYS: Expand key->{datalen,quotalen} to unsigned int
In reply to Eric BiggersQuoted 6 lines · Eric Biggers
Quoted 2 lines · Jarkko Sakkinen
In
key_alloc()andkey_payload_reserve(),key->quotalenandkey->datalencan receive values larger thanUSHRT_MAX.Could use an explanation about how this can happen. A keyring with more than 16383 links, I guess?
It is good to sanity check these given the source of origin.
I tried to implement your example and I think I was successful.
Here's what I did:
keyctl new_session >/dev/null
owner=$(keyctl newring owner @s)
keeper=$(keyctl newring keeper @s)
for ((i=0; i<16384; i++)); do
key=$(keyctl add user "k$i" x "$keeper")
keyctl link "$key" "$owner"
done
awk -v uid="$(id -u):" '$1 == uid {print $NF}' /proc/key-users
keyctl clear "$owner"
awk -v uid="$(id -u):" '$1 == uid {print $NF}' /proc/key-usersIt is mostly safe from unprivileged user as /proc/sys/kernel/keys/maxkeys and /proc/sys/kernel/keys/maxbytes are by default set to 200 and 20000.
When I run it as root, this what I get on first run:
252793/25000000
252793/25000000
Second run:
383862/25000000
383862/25000000
And third run for good measure:
514934/25000000
514934/25000000
So yeah, it is right on the uapi surface easily accessible :-)
Quoted 2 lines
- Eric
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/6] KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
In reply to Eric BiggersQuoted 4 lines · Eric Biggers
Quoted 3 lines · Jarkko Sakkinen
@desclen in
key_alloc()is saved to the 16-bit widedesc_lenfield in structkeyring_index_key.
Quoted 8 lines
Quoted 2 lines
Reject @desclen values at or above
KEY_MAX_DESC_SIZEwith-EINVALin order to account for the NUL terminator and prevent integer overflow.This should say something like: "in order to match the userspace limit and avoid u16 truncation".
There are also similar truncations of
desc_lento u16 inkey_create_or_update()andkeyring_search().
Ack.
Quoted 4 lines
Quoted 2 lines
+#define
KEY_MAX_DESC_SIZE4096Could use a comment mentioning that it includes the trailing NUL.
OK, that improved the commit mesage somewhat:
KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
@desclen in key_alloc() is saved to the 16-bit wide desc_len field in struct keyring_index_key.
Reject @desclen values at or above KEY_MAX_DESC_SIZE with -EINVAL in order to match the userspace limit. The constraints inherits from the one byte required for the NULL character.
Quoted 2 lines
- Eric
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 3/6] KEYS: Prevent overflow of user->{qnkeys,qnbytes} in key_alloc()
In reply to Eric BiggersQuoted 5 lines · Eric Biggers
Quoted 4 lines · Jarkko Sakkinen
Add the missing overflow checks for
user->qnkeysanduser->qnbytesin order keep calculations withinINT_MAXrange, which is the absolute maximum quota.
Quoted 7 lines
Quoted 5 lines
Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>Don't
keyctl_chown_key()andkey_payload_reserve()need similar fixes?
Thanks, I'll investigate.
Unless any specific reason, I would not hold merging this series. I have written down a follow-up backlog i.e., post-tasks once this series is merged.
Quoted 2 lines
- Eric
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 4/6] KEYS: Prevent overflow of quota in key_alloc()
In reply to Eric BiggersQuoted 9 lines · Eric Biggers
Quoted 7 lines · Jarkko Sakkinen
Check that the calculated quota length does not go over
INT_MAX.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Quoted 4 lines
Seems this should be merged into patch 3?
Also is this reachable? I.e. do any of the key types actually specify
def_datalen>INT_MAX-KEY_MAX_DESC_SIZE+ 1?
This is very good question and my *guess* is that no there is not.
That said, when not treated as a bug fix this patch is a great safe-guard e.g., to block corraterall effects of some other bugs.
So yeah, that's I would propose here. Let's not call it a bug fix but still merge.
Quoted 2 lines
- Eric
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 4/6] KEYS: Prevent overflow of quota in key_alloc()
Quoted 15 lines · Jarkko Sakkinen
Quoted 9 lines · Eric Biggers
Quoted 7 lines · Jarkko Sakkinen
Check that the calculated quota length does not go over
INT_MAX.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>Quoted 4 lines
Seems this should be merged into patch 3?
Also is this reachable? I.e. do any of the key types actually specify
def_datalen>INT_MAX-KEY_MAX_DESC_SIZE+ 1?
Quoted 8 lines
This is very good question and my *guess* is that no there is not.
That said, when not treated as a bug fix this patch is a great safe-guard e.g., to block corraterall effects of some other bugs.
So yeah, that's I would propose here. Let's not call it a bug fix but still merge.
Or actually: no let's not do this. I'm making my life only more difficult.
I think it is totally sane to squash this to patch 3 and it is reasonable for that fix have this additional safe-guard.
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 6/6] KEYS: Verify the key identity in __keyctl_unlink
In reply to Eric BiggersQuoted 4 lines · Eric Biggers
Quoted 1 line · Jarkko Sakkinen
[PATCH v2 6/6] KEYS: Verify the key identity in
__keyctl_unlinkThere is no
__keyctl_unlink. Seems you mean__key_unlink_begin.
This is definitely my typo :-) I was looking at the implementation in other terminal while writing this and apparently lost focus.
Quoted 7 lines
Quoted 7 lines
Let's define the following preconditions:
- Two keys: A and B.
- R is a keyring.
- Both have the same type/description/domain.
Quoted 7 lines
Quoted 5 lines
Then:
1.
KEYCTL_LINK(A, R)=> {A}
1.KEYCTL_LINK(B, R)=> {B}
2.KEYCTL_UNLINK(A, R)=> {}Should be 1, 2, and 3?
I tried to describe the scenarion but apparently was not so successful.
I.e. unlinking A removes B given the match in attributes while you would expect getting -ENOENT and B retained. Or this is the idea.
Quoted 6 lines
Quoted 2 lines
Instead, verify key tidentity before unlinking, and return
-ENOENTontidentity => identity
Also should explicitly say what the bug is.
I tried and failed... But yeah, the bug is illict successful removal so there is nothing unclear on that side.
Is clearer description enough or should I supplement this keyctl snippet?
Quoted 2 lines
- Eric
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 5/6] KEYS: Reject @datalen values over INT_MAX
In reply to Eric BiggersQuoted 7 lines · Eric Biggers
Quoted 2 lines · Jarkko Sakkinen
Protect quota from overflowing by rejecting @datalen values that surpass over
INT_MAX.Is there an example of how this can happen, and why the limit needs to still be so large?
add_key()for example limits the payload length to 1 MiB.
I did a bit scavenging, and to my knowleddge, no. So I would retain this as safety measure but would not consider it as a bug fix.
Quoted 2 lines
- Eric
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 6/6] KEYS: Verify the key identity in __keyctl_unlink
In reply to Jarkko SakkinenQuoted 22 lines · Jarkko Sakkinen
Quoted 4 lines · Eric Biggers
Quoted 1 line · Jarkko Sakkinen
[PATCH v2 6/6] KEYS: Verify the key identity in
__keyctl_unlinkThere is no
__keyctl_unlink. Seems you mean__key_unlink_begin.This is definitely my typo :-) I was looking at the implementation in other terminal while writing this and apparently lost focus.
Quoted 13 lines
Quoted 12 lines
Let's define the following preconditions:
- Two keys: A and B.
- R is a keyring.
- Both have the same type/description/domain.Then:
1.
KEYCTL_LINK(A, R)=> {A}
1.KEYCTL_LINK(B, R)=> {B}
2.KEYCTL_UNLINK(A, R)=> {}
Quoted 6 lines
Quoted 1 line
Should be 1, 2, and 3?
I tried to describe the scenarion but apparently was not so successful.
I.e. unlinking A removes B given the match in attributes while you would expect getting
-ENOENTand B retained. Or this is the idea.
Here's a little bit tweaked version of David's example:
#!/bin/sh
set -e
keyctl new_session >/dev/null keyctl newring ring_a @s keyctl newring ring_b @s A=keyctl add user A ss %:ring_a B=keyctl add user A qq %:ring_b keyctl newring R @s
keyctl show
keyctl link $A %:R
keyctl link $B %:R
keyctl unlink $A %:R
keyctl show
What I get is:
77968717
178740336
781765689
Session Keyring
952124334 --alswrv 0 0 keyring: _ses 77968717 --alswrv 0 0 \_ keyring: ring_a 32558793 --alswrv 0 0 | \_ user: A 781765689 --alswrv 0 0 \_ keyring: R 178740336 --alswrv 0 0 \_ keyring: ring_b 907061159 --alswrv 0 0 \_ user: A Session Keyring 952124334 --alswrv 0 0 keyring: _ses 77968717 --alswrv 0 0 \_ keyring: ring_a 32558793 --alswrv 0 0 | \_ user: A 781765689 --alswrv 0 0 \_ keyring: R 178740336 --alswrv 0 0 \_ keyring: ring_b 907061159 --alswrv 0 0 \_ user: A
What I would expect is -ENOENT.
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 5/6] KEYS: Reject @datalen values over INT_MAX
In reply to Jarkko SakkinenQuoted 11 lines · Jarkko Sakkinen
Quoted 7 lines · Eric Biggers
Quoted 2 lines · Jarkko Sakkinen
Protect quota from overflowing by rejecting @datalen values that surpass over
INT_MAX.Is there an example of how this can happen, and why the limit needs to still be so large?
add_key()for example limits the payload length to 1 MiB.I did a bit scavenging, and to my knowleddge, no. So I would retain this as safety measure but would not consider it as a bug fix.
While it is true that add_key() has 1 MiB limit there's also root quota. That was what I was originally thinking about now that I scratched my head a bit.
But...
However, it is capped by /proc/sys/kernel/keys/root_maxbytes, SYSCTL_INT_MAX already.
So that makes the check this patch adds irrelevant, and thus it should be dropped.
Phew... At least it is now investigated in-depth enough, I think.
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 5/6] KEYS: Reject @datalen values over INT_MAX
Quoted 19 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen
Quoted 7 lines · Eric Biggers
Quoted 2 lines · Jarkko Sakkinen
Protect quota from overflowing by rejecting @datalen values that surpass over
INT_MAX.Is there an example of how this can happen, and why the limit needs to still be so large?
add_key()for example limits the payload length to 1 MiB.I did a bit scavenging, and to my knowleddge, no. So I would retain this as safety measure but would not consider it as a bug fix.
While it is true that
add_key()has 1 MiB limit there's also root quota. That was what I was originally thinking about now that I scratched my head a bit.But...
Quoted 8 lines
However, it is capped by
/proc/sys/kernel/keys/root_maxbytes,SYSCTL_INT_MAXalready.So that makes the check this patch adds irrelevant, and thus it should be dropped.
Phew... At least it is now investigated in-depth enough, I think.
This also makes the other safe-guard useless, so I'll drop that too.
And likely expand the patch set to address the entry points that you mentioned.
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/6] KEYS: Reject @desclen values >= KEY_MAX_DESC_SIZE in key_alloc()
In reply to Jarkko SakkinenQuoted 8 lines · Jarkko Sakkinen
Quoted 7 lines · Eric Biggers
Quoted 5 lines · Jarkko Sakkinen
@desclen in
key_alloc()is saved to the 16-bit widedesc_lenfield in structkeyring_index_key.Reject @desclen values at or above
KEY_MAX_DESC_SIZEwith-EINVALin order to account for the NUL terminator and prevent integer overflow.
Quoted 7 lines
Quoted 5 lines
This should say something like: "in order to match the userspace limit and avoid u16 truncation".
There are also similar truncations of
desc_lento u16 inkey_create_or_update()andkeyring_search().Ack.
Please correct if I'm wrong but I think these don't need anything special given that both are capped by strndup().
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 3/6] KEYS: Prevent overflow of user->{qnkeys,qnbytes} in key_alloc()
In reply to Jarkko SakkinenQuoted 12 lines · Jarkko Sakkinen
Quoted 11 lines · Eric Biggers
Quoted 9 lines · Jarkko Sakkinen
Add the missing overflow checks for
user->qnkeysanduser->qnbytesin order keep calculations withinINT_MAXrange, which is the absolute maximum quota.Assisted-by LLMFixes 7e047ef5fe2d ("[PATCH] keys: sort out key quota system")Reviewed-by David Howells <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Quoted 3 lines
Quoted 1 line
Don't
keyctl_chown_key()andkey_payload_reserve()need similar fixes?Thanks, I'll investigate.
OK so this is me misleading as my description is missing one important
detail: KEY_ALLOC_QUOTA_OVERRUN bypasses the maxkeys and maxbytes checks.
This is the exact reason why this fix is needed and commit message does not properly address that i.e. lacks the actual root cause.
Also:
1. keyctl_chown_key() checks against maxkeys and maxbytes so it is clear.
2. key_payload_reserve() checs against maxbytes.
Br, Jarkko