Skip to contentSkip to conversation

rsa: fix rsa_gen_key_prop() buffer-sizing and zero-modulus handling

PATCH In review Prasad Kummari 3 participants 7 messages Oct 6 – Oct 23

lore
Share & tools
lore
#0 lore raw

v2 cover rsa: fix rsa_gen_key_prop() buffer-sizing and zero-modulus handling

Fix two memory issues in rsa_gen_key_prop():
- Correct n[]/rr[] sizing to prevent out-of-bounds writes.
- Reject all-zero modulus to prevent out-of-bounds reads.

Changes in v2:
- Patch 1: use DIV_ROUND_UP() instead of open-coded rounding for the n[]/rr[] allocation sizes.
- Patch 2: new, addressing the num_bits == 0 case.

UT:

Sandbox:

=> ut lib lib_rsa_keyprop_oob
Test: lib_rsa_keyprop_oob: rsa.c
rsa_gen_key_prop: n_sz=258 leading_zeros=1 stripped_bytes=257 num_bits=2056
rsa_gen_key_prop: n_words=66 rr_words=130 rrtmp_words=130 n=000000001c135890 rr=000000001c1359a0 rrtmp=000000001c135bb0 (FIXED sizing)
rsa_gen_key_prop: br_i32_decode(n) done, n[0] (bit_length)=2056 n0inv_input n[1]=0x968b6a4d
rsa_gen_key_prop: rlen=4112 rr zero-init loop wrote indices 1..129
rsa_gen_key_prop: br_i32_reduce(rr) done, rr[0] (bit_length)=2056 Tests run: 1, 0 ms, average: 0 ms, failures: 0

=> ut lib lib_rsa_keyprop_zero_modulus
Test: lib_rsa_keyprop_zero_modulus: rsa.c
rsa_gen_key_prop: n_sz=3 leading_zeros=3 num_bits=0, rejecting (-EINVAL) Tests run: 1, 0 ms, average: 0 ms, failures: 0
=>

Prasad Kummari (2):
rsa: fix undersized modulus buffers in rsa_gen_key_prop()
rsa: reject zero-bit modulus in rsa_gen_key_prop()

 lib/rsa/rsa-keyprop.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 rsa: fix undersized modulus buffers in rsa_gen_key_prop()

n and rr are sized with a floor division by 32 (num_bits >> 5), but br_i32_decode() and the zero-init loop before br_i32_reduce() need ceil(bits/32) words plus a header word. Whenever the stripped modulus byte length isn't a multiple of 4, br_i32_decode() writes one word past the end of n; whenever it's odd, the zero-init loop writes one zero word past the end of rr. The modulus length is fully attacker-controlled via the DER public key's ASN.1 INTEGER length, so both overflows are reachable with a crafted key.

Switch both allocations to ceiling division so they always hold the full word count each site writes, using DIV_ROUND_UP() instead of open-coded rounding for readability. rrtmp is already exactly sized and is left unchanged.

Fixes 49d0ea3746c ("lib: rsa: fix allocated size for rr and rrtmp in rsa_gen_key_prop()")
---
 lib/rsa/rsa-keyprop.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/rsa/rsa-keyprop.c +4 -2
index 2ac039ac3f4..ca186a1440c 100644
@@ -12,6 +12,7 @@
 #include <image.h>
 #include <malloc.h>
 #include <crypto/internal/rsa.h>
+#include <linux/kernel.h>
 #include <u-boot/rsa-mod-exp.h>
 #include <asm/unaligned.h>
 
@@ -687,8 +688,9 @@ int rsa_gen_key_prop(const void *key, uint32_t keylen, struct key_prop **prop)
 	}
 	memcpy((void *)(*prop)->modulus, &rsa_key.n[i], rsa_key.n_sz - i);
 
-	n = calloc(sizeof(uint32_t), 1 + ((*prop)->num_bits >> 5));
-	rr = calloc(sizeof(uint32_t), 1 + (((*prop)->num_bits * 2) >> 5));
+	n = calloc(sizeof(uint32_t), 1 + DIV_ROUND_UP((*prop)->num_bits, 32));
+	rr = calloc(sizeof(uint32_t),
+		    1 + DIV_ROUND_UP((*prop)->num_bits * 2, 32));
 	rrtmp = calloc(sizeof(uint32_t), 2 + (((*prop)->num_bits * 2) >> 5));
 	if (!n || !rr || !rrtmp) {
 		ret = -ENOMEM;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 rsa: reject zero-bit modulus in rsa_gen_key_prop()

In reply to Prasad Kummari

rsa_get_n() only rejects a zero-length INTEGER, so an all-zero modulus (e.g. a single 0x00 byte) is accepted and leaves num_bits at 0 after the leading-zero-stripping loop. With num_bits == 0, n is allocated as a single header word, so br_i32_ninv32(n[1]) reads one word past the end of n, and modulus is allocated via malloc(0).

Reject num_bits == 0 with -EINVAL right after stripping the leading zeroes, before any of the buffers sized from it are allocated.

Fixes e0d310b098b1 ("lib: rsa: generate additional parameters for public key")
---
 lib/rsa/rsa-keyprop.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/rsa/rsa-keyprop.c +4 -0
index ca186a1440c..e74eea18977 100644
@@ -681,6 +681,10 @@ int rsa_gen_key_prop(const void *key, uint32_t keylen, struct key_prop **prop)
 	for (i = 0; i < rsa_key.n_sz && !rsa_key.n[i]; i++)
 		;
 	(*prop)->num_bits = (rsa_key.n_sz - i) * 8;
+	if (!(*prop)->num_bits) {
+		ret = -EINVAL;
+		goto out;
+	}
 	(*prop)->modulus = malloc(rsa_key.n_sz - i);
 	if (!(*prop)->modulus) {
 		ret = -ENOMEM;
Signature · 1 line
2.43.0
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] rsa: reject zero-bit modulus in rsa_gen_key_prop()

Quoted 11 lines · Prasad Kummari

rsa: reject zero-bit modulus in rsa_gen_key_prop()

rsa_get_n() only rejects a zero-length INTEGER, so an all-zero modulus (e.g. a single 0x00 byte) is accepted and leaves num_bits at 0 after the leading-zero-stripping loop. With num_bits == 0, n is allocated as a single header word, so br_i32_ninv32(n[1]) reads one word past the end of n, and modulus is allocated via malloc(0).

Reject num_bits == 0 with -EINVAL right after stripping the leading zeroes, before any of the buffers sized from it are allocated.

Quoted 6 lines
Fixes e0d310b098b1 ("lib: rsa: generate additional parameters for public key")

lib/rsa/rsa-keyprop.c | 4 ++++

 1 file changed, 4 insertions(+)
#4 lore raw
In reply to Prasad Kummari

Hi Prasad,

Quoted 6 lines · Prasad Kummari

UT:

Sandbox:

=> ut lib lib_rsa_keyprop_oob

I'm not seeing this test in the v2 series?

On the out-of-bounds test, a one-word heap overrun usually goes unnoticed, so please check that the test really fails without patch 1. For example, it could check the heap with mallinfo() or a guard word after the allocation. Otherwise it only shows that a 2056-bit key with a leading zero byte is decoded.

Regards,
Simon

#5 lore raw

AMD General

Hi Simon,

Quoted 21 lines

-----Original Message-----
From: Simon Glass <[email protected]>
Sent: Sunday, October 11, 2026 1:59 PM
To: Kummari, Prasad <[email protected]>
Cc: [email protected]; git (AMD-Xilinx) <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v2 0/2] rsa: fix rsa_gen_key_prop() buffer-sizing and zero- modulus handling

Hi Prasad,

Quoted 6 lines · Prasad Kummari

UT:

Sandbox:

=> ut lib lib_rsa_keyprop_oob

Quoted 7 lines

I'm not seeing this test in the v2 series?

On the out-of-bounds test, a one-word heap overrun usually goes unnoticed, so please check that the test really fails without patch 1.
For example, it could check the heap with mallinfo() or a guard word after the allocation. Otherwise it only shows that a 2056-bit key with a leading zero byte is decoded.

With fix: n_words=66, rr_words=130 — decode writes index 65/129, fits exactly
Without patch 1 fix: n_words=65, rr_words=129 — decode writes the *same* index 65/129 — one word past the end, both buffers mallinfo() delta: 624 bytes, identical in both cases.

Sandbox logs:

With fix:

=> ut lib lib_rsa_keyprop_oob
Test: lib_rsa_keyprop_oob: rsa.c
rsa_gen_key_prop: n_sz=258 leading_zeros=1 stripped_bytes=257 num_bits=2056
rsa_gen_key_prop: n_words=66 rr_words=130 rrtmp_words=130 n=000000001c136490 rr=000000001c1365a0 rrtmp=000000001c1367b0 (FIXED sizing)
rsa_gen_key_prop: br_i32_decode(n) done, n[0] (bit_length)=2056 n0inv_input n[1]=0x968b6a4d
rsa_gen_key_prop: rlen=4112 rr zero-init loop wrote indices 1..129
rsa_gen_key_prop: br_i32_reduce(rr) done, rr[0] (bit_length)=2056
lib_rsa_keyprop_oob: mallinfo uordblks before=2253616 after=2254240 delta=624
Tests run: 1, 0 ms, average: 0 ms, failures: 0

=> ut lib lib_rsa_keyprop_zero_modulus
Test: lib_rsa_keyprop_zero_modulus: rsa.c
rsa_gen_key_prop: n_sz=3 leading_zeros=3 num_bits=0, rejecting (-EINVAL) Tests run: 1, 0 ms, average: 0 ms, failures: 0

=> ut lib lib_rsa_keyprop_zero_modulus
Test: lib_rsa_keyprop_zero_modulus: rsa.c
rsa_gen_key_prop: n_sz=3 leading_zeros=3 num_bits=0, rejecting (-EINVAL) Tests run: 1, 0 ms, average: 0 ms, failures: 0
=>

Without fix:

=> ut lib lib_rsa_keyprop_oob
Test: lib_rsa_keyprop_oob: rsa.c
rsa_gen_key_prop: n_sz=258 leading_zeros=1 stripped_bytes=257 num_bits=2056
rsa_gen_key_prop: n_words=65 rr_words=129 rrtmp_words=130 n=000000001c136490 rr=000000001c1365a0 rrtmp=000000001c1367b0 (UNFIXED sizing)
rsa_gen_key_prop: br_i32_decode(n) done, n[0] (bit_length)=2056 n0inv_input n[1]=0x968b6a4d
rsa_gen_key_prop: rlen=4112 rr zero-init loop wrote indices 1..129
rsa_gen_key_prop: br_i32_reduce(rr) done, rr[0] (bit_length)=2056
lib_rsa_keyprop_oob: mallinfo uordblks before=2253616 after=2254240 delta=624
Tests run: 1, 0 ms, average: 0 ms, failures: 0

=> ut lib lib_rsa_keyprop_zero_modulus
Test: lib_rsa_keyprop_zero_modulus: rsa.c
rsa_gen_key_prop: n_sz=3 leading_zeros=3 num_bits=0, rejecting (-EINVAL) Tests run: 1, 0 ms, average: 0 ms, failures: 0
=>

Regards,
Prasad.

#6 lore raw
In reply to Prasad Kummari
Quoted 10 lines · Prasad Kummari

Fix two memory issues in rsa_gen_key_prop():
- Correct n[]/rr[] sizing to prevent out-of-bounds writes.
- Reject all-zero modulus to prevent out-of-bounds reads.

Changes in v2:
- Patch 1: use DIV_ROUND_UP() instead of open-coded rounding for the n[]/rr[] allocation sizes.
- Patch 2: new, addressing the num_bits == 0 case.

[...]

Applied to u-boot/main, thanks!

[1/2] rsa: fix undersized modulus buffers in rsa_gen_key_prop()

      commit: 0745fe3426e5c22e2a81257985982a129a891800
[2/2] rsa: reject zero-bit modulus in rsa_gen_key_prop()
      commit: d29cc201be9bfba3db5b18a71abb0b7052eaedb5
Signature · 1 line
Tom