Skip to contentSkip to conversation

keys: big_key: Reserve the final quota before destroying the payload

PATCH In review Daehyeon Ko 3 participants 7 messages Oct 16 – Oct 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 keys: big_key: Reserve the final quota before destroying the payload

A big_key is charged a fixed 16 bytes of key quota, regardless of the actual payload size. big_key_update() currently reserves the actual new payload length before destroying the old payload, while the subsequent generic_key_instantiate() reserves the fixed quota length.

For an update to a payload shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() tries to reserve it again. If that second reservation fails, the key remains instantiated but its old big_key payload has already been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

On a KASAN build, reading a buffer-backed key reports:

  BUG: KASAN: null-ptr-deref in big_key_read+0x110/0x310
  Read of size 64 at addr 0000000000000000
  ...
  big_key_read+0x110/0x310
  keyctl_read_key+0x2da/0x550

Revoking a shmem-backed key reports, with oops=panic:

  Oops: general protection fault
  KASAN: null-ptr-deref in range [0x68-0x6f]
  RIP: vfs_truncate+0xa2/0x730
  big_key_revoke+0xbd/0xe0
  key_revoke+0xc0/0x1a0
  Kernel panic - not syncing: Fatal exception

Reserve the final fixed quota length before destroying the old payload. The reservation in generic_key_instantiate() then has no quota delta and cannot fail for this reason after the destructive step.

Fixes b6f61c314649 ("keys: Implement update for the big_key type")
Assisted-by LLM
---
 security/keys/big_key.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/big_key.c +1 -1
index 268f702df380e..6aff1a73c7b38 100644
@@ -195,7 +195,7 @@ int big_key_update(struct key *key, struct key_preparsed_payload *prep)
 {
 	int ret;
 
-	ret = key_payload_reserve(key, prep->datalen);
+	ret = key_payload_reserve(key, prep->quotalen);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

A big_key is charged a fixed 16 bytes of key quota, regardless of the actual payload size. big_key_update() currently reserves the actual new payload length before destroying the old payload, while the subsequent generic_key_instantiate() reserves the fixed quota length.

For an update to a payload shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() tries to reserve it again. If that second reservation fails, the key remains instantiated but its old big_key payload has already been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

Quoted 8 lines

On a KASAN build, reading a buffer-backed key reports:

[ ... ]

Fixes b6f61c314649 ("keys: Implement update for the big_key type")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Daehyeon Ko
Quoted 5 lines · Daehyeon Ko

A big_key is charged a fixed 16 bytes of key quota, regardless of the actual payload size. big_key_update() currently reserves the actual new payload length before destroying the old payload, while the subsequent generic_key_instantiate() reserves the fixed quota length.

Quoted 7 lines

For an update to a payload shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() tries to reserve it again. If that second reservation fails, the key remains instantiated but its old big_key payload has already been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

Even at the risk that I'm saying something stupid, makes me wonder why the call exists in big_key.c? I.e. why not remove the call instead of doing what the patch does?

If I'm now missing something out, that's cool, just correct me.

Quoted 46 lines

On a KASAN build, reading a buffer-backed key reports:

  BUG: KASAN: null-ptr-deref in big_key_read+0x110/0x310
  Read of size 64 at addr 0000000000000000
  ...
  big_key_read+0x110/0x310
  keyctl_read_key+0x2da/0x550

Revoking a shmem-backed key reports, with oops=panic:

  Oops: general protection fault
  KASAN: null-ptr-deref in range [0x68-0x6f]
  RIP: vfs_truncate+0xa2/0x730
  big_key_revoke+0xbd/0xe0
  key_revoke+0xc0/0x1a0
  Kernel panic - not syncing: Fatal exception

Reserve the final fixed quota length before destroying the old payload. The reservation in generic_key_instantiate() then has no quota delta and cannot fail for this reason after the destructive step.

Fixes b6f61c314649 ("keys: Implement update for the big_key type")
Assisted-by LLM
---
 security/keys/big_key.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/big_key.c +1 -1
index 268f702df380e..6aff1a73c7b38 100644
@@ -195,7 +195,7 @@ int big_key_update(struct key *key, struct key_preparsed_payload *prep)
 {
 	int ret;
 
-	ret = key_payload_reserve(key, prep->datalen);
+	ret = key_payload_reserve(key, prep->quotalen);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
2.55.0

Br, Jarkko

maintainer
#3 lore raw
Quoted 14 lines · Jarkko Sakkinen
Quoted 5 lines · Daehyeon Ko

A big_key is charged a fixed 16 bytes of key quota, regardless of the actual payload size. big_key_update() currently reserves the actual new payload length before destroying the old payload, while the subsequent generic_key_instantiate() reserves the fixed quota length.

Quoted 7 lines

For an update to a payload shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() tries to reserve it again. If that second reservation fails, the key remains instantiated but its old big_key payload has already been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

Quoted 6 lines

Even at the risk that I'm saying something stupid, makes me wonder why the call exists in big_key.c? I.e. why not remove the call instead of doing what the patch does?

If I'm now missing something out, that's cool, just correct me.

So the way I see it this patch has the right bug and a wrong fix.

I.e. if you still want to move forward with this fix, explain why two repeated calls are still a necessity. Then we can ensure that the fix addressing the root cause.

Alternatively rewrite the patch so that it makes the call path have only a single call site for key_payload_reserve().

Br. Jarkko

#4 lore raw

PATCH v2 1/1 keys: big_key: Remove the redundant quota reservation

A big_key is charged a fixed 16 bytes of key quota, regardless of its payload size. big_key_update() nevertheless reserves the new raw payload length before destroying the old payload, and generic_key_instantiate() then reserves the fixed quota length.

For an update shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() reserves it again. If the second reservation fails, the key remains positive after its old payload has been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

On a KASAN build, reading a buffer-backed key reports:

  BUG: KASAN: null-ptr-deref in big_key_read+0x110/0x310
  Read of size 64 at addr 0000000000000000

Revoking a shmem-backed key reports, with oops=panic:

  KASAN: null-ptr-deref in range [0x68-0x6f]
  RIP: vfs_truncate+0xa2/0x730
  big_key_revoke+0xbd/0xe0
  Kernel panic - not syncing: Fatal exception

The first reservation is unnecessary. A positive big_key already has a 16-byte reservation from successful instantiation, so the reservation in generic_key_instantiate() has zero delta and cannot fail after the old payload is destroyed. A negative key has no old payload to destroy, and generic_key_instantiate() performs the required reservation before publishing the new payload.

Remove the redundant reservation so that the update path adjusts quota only once and cannot expose a temporary quota gap.

Fixes b6f61c314649 ("keys: Implement update for the big_key type")
Assisted-by LLM

---
Changes since v1:
- Remove the redundant first reservation instead of changing its length, leaving generic_key_instantiate() as the single quota reservation on the update path, as suggested by Jarkko.
- Verify full-quota positive and negative updates separately. A positive update keeps a zero quota delta; a negative update can still return -EDQUOT before publishing a payload and succeeds after quota is released.

 security/keys/big_key.c | 6 ------
 1 file changed, 6 deletions(-)
security/keys/big_key.c +0 -6
index 268f702df380e..ef707f2a81a98 100644
@@ -193,12 +193,6 @@ void big_key_destroy(struct key *key)
  */
 int big_key_update(struct key *key, struct key_preparsed_payload *prep)
 {
-	int ret;
-
-	ret = key_payload_reserve(key, prep->datalen);
-	if (ret < 0)
-		return ret;
-
 	if (key_is_positive(key))
 		big_key_destroy(key);
 

base-commit: 58161ead29e50d70a317ea26b472e0c7508e69da

Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH v2] keys: big_key: Remove the redundant quota reservation

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

A big_key is charged a fixed 16 bytes of key quota, regardless of its payload size. big_key_update() nevertheless reserves the new raw payload length before destroying the old payload, and generic_key_instantiate() then reserves the fixed quota length.

For an update shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() reserves it again. If the second reservation fails, the key remains positive after its old payload has been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

On a KASAN build, reading a buffer-backed key reports:

Quoted 7 lines
  BUG: KASAN: null-ptr-deref in big_key_read+0x110/0x310

[ ... ]

Fixes b6f61c314649 ("keys: Implement update for the big_key type")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v2 1/1
#6 lore raw

Re: [PATCH v2] keys: big_key: Remove the redundant quota reservation

In reply to Daehyeon Ko
Quoted 68 lines · Daehyeon Ko

A big_key is charged a fixed 16 bytes of key quota, regardless of its payload size. big_key_update() nevertheless reserves the new raw payload length before destroying the old payload, and generic_key_instantiate() then reserves the fixed quota length.

For an update shorter than 16 bytes, the first reservation temporarily returns quota. Another key operation by the same user can consume that quota before generic_key_instantiate() reserves it again. If the second reservation fails, the key remains positive after its old payload has been destroyed. Reading a buffer-backed key or revoking a shmem-backed key then dereferences a NULL payload pointer.

On a KASAN build, reading a buffer-backed key reports:

  BUG: KASAN: null-ptr-deref in big_key_read+0x110/0x310
  Read of size 64 at addr 0000000000000000

Revoking a shmem-backed key reports, with oops=panic:

  KASAN: null-ptr-deref in range [0x68-0x6f]
  RIP: vfs_truncate+0xa2/0x730
  big_key_revoke+0xbd/0xe0
  Kernel panic - not syncing: Fatal exception

The first reservation is unnecessary. A positive big_key already has a 16-byte reservation from successful instantiation, so the reservation in generic_key_instantiate() has zero delta and cannot fail after the old payload is destroyed. A negative key has no old payload to destroy, and generic_key_instantiate() performs the required reservation before publishing the new payload.

Remove the redundant reservation so that the update path adjusts quota only once and cannot expose a temporary quota gap.

Fixes b6f61c314649 ("keys: Implement update for the big_key type")
Assisted-by LLM

---
Changes since v1:
- Remove the redundant first reservation instead of changing its length, leaving generic_key_instantiate() as the single quota reservation on the update path, as suggested by Jarkko.
- Verify full-quota positive and negative updates separately. A positive update keeps a zero quota delta; a negative update can still return -EDQUOT before publishing a payload and succeeds after quota is released.

 security/keys/big_key.c | 6 ------
 1 file changed, 6 deletions(-)
security/keys/big_key.c +0 -6
index 268f702df380e..ef707f2a81a98 100644
@@ -193,12 +193,6 @@ void big_key_destroy(struct key *key)
  */
 int big_key_update(struct key *key, struct key_preparsed_payload *prep)
 {
-	int ret;
-
-	ret = key_payload_reserve(key, prep->datalen);
-	if (ret < 0)
-		return ret;
-
 	if (key_is_positive(key))
 		big_key_destroy(key);
 
Quoted 4 lines

base-commit: 58161ead29e50d70a317ea26b472e0c7508e69da

Signature · 1 line
2.55.0

Well, that made a difference. I've started actively try to look for eager tactics, and in this case it turned out to be worth it. The first solution was exactly like weighted answer that cut the search too early :-)

Thank you.

PS. Not applying til Monday.

Br, Jarkko