Skip to contentSkip to conversation

rsa: fix alignment issue when getting public exponent

PATCH New Heiko Stuebner 5 participants 8 messages May 3, 2020 – May 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
---
 lib/rsa/rsa-mod-exp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/rsa/rsa-mod-exp.c +2 -2
index 420ab2eba0..62b2557cc2 100644
@@ -262,8 +262,8 @@ int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
 	if (!prop->public_exponent)
 		key.exponent = RSA_DEFAULT_PUBEXP;
 	else
-		key.exponent =
-			fdt64_to_cpu(*((uint64_t *)(prop->public_exponent)));
+		rsa_convert_big_endian((uint32_t *)&key.exponent,
+				       prop->public_exponent, 2);
 
 	if (!key.len || !prop->modulus || !prop->rr) {
 		debug("%s: Missing RSA key info", __func__);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

Quoted 19 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

Quoted 5 lines

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
#2 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

In reply to Heiko Stuebner

+Tom Rini

Quoted 16 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

Quoted 9 lines

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
---
 lib/rsa/rsa-mod-exp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Nice find! This probably changed when we updated the DT recently since the unaligned-access thing was reverted I think. Or has this problem been there forever?

#3 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

Am Montag, 4. Mai 2020, 16:17:52 CEST schrieb Simon Glass:

Quoted 29 lines

+Tom Rini

Quoted 16 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

Quoted 9 lines

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
---
 lib/rsa/rsa-mod-exp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 3 lines

Nice find! This probably changed when we updated the DT recently since the unaligned-access thing was reverted I think. Or has this problem been there forever?

To me it looks like it must've been present since the beginning.
In commit e0f2f1553414 ("Implement generalised RSA public exponents for verified boot") which introduced the exponent handling it already did:

const uint64_t *public_exponent;
public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);

So if the fdt_getprop internals didn't change since then it would've even then cast the void* to an uint64*

I really don't understand yet why the longer key-name would trigger it though ... names like "dev", "integrity" work fine and seemingly starting at a certain length the alignment changed.

Heiko

Quoted 2 lines
#4 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

Hi Heiko,

Quoted 39 lines · Heiko St?bner

Am Montag, 4. Mai 2020, 16:17:52 CEST schrieb Simon Glass:

Quoted 29 lines

+Tom Rini

Quoted 16 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

Quoted 9 lines

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
---
 lib/rsa/rsa-mod-exp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 3 lines

Nice find! This probably changed when we updated the DT recently since the unaligned-access thing was reverted I think. Or has this problem been there forever?

To me it looks like it must've been present since the beginning.
In commit e0f2f1553414 ("Implement generalised RSA public exponents for verified boot") which introduced the exponent handling it already did:

Quoted 6 lines

const uint64_t *public_exponent;
public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);

So if the fdt_getprop internals didn't change since then it would've even then cast the void* to an uint64*

See this patch:

e8c2d25845 libfdt: Revert 6dcb8ba4 from upstream libfdt

Quoted 3 lines

I really don't understand yet why the longer key-name would trigger it though ... names like "dev", "integrity" work fine and seemingly starting at a certain length the alignment changed.

My guess is that these lengths work:

1-3 chars (aligns to 4 bytes)
8-11 chars (aligns to 12 bytes)
16-19 chars (aligns to 20 bytes)
etc.

Regards,
Simon

#5 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

In reply to Heiko Stuebner
Quoted 32 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
---
  lib/rsa/rsa-mod-exp.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/rsa/rsa-mod-exp.c +0 -0
index 420ab2eba0..62b2557cc2 100644

lib/rsa/rsa-mod-exp.c int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len, · line 269

Quoted 10 lines
lib/rsa/rsa-mod-exp.c +2 -2
@@ -262,8 +262,8 @@ int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
  	if (!prop->public_exponent)
  		key.exponent = RSA_DEFAULT_PUBEXP;
  	else
-		key.exponent =
-			fdt64_to_cpu(*((uint64_t *)(prop->public_exponent)));
+		rsa_convert_big_endian((uint32_t *)&key.exponent,
+				       prop->public_exponent, 2);
 
  	if (!key.len || !prop->modulus || !prop->rr) {
  		debug("%s: Missing RSA key info", __func__);

I debugged the same issue on our AM654x-based board where we do fit image verification in SPL. As there is apparently no unaligned access possible, this bit us as well. Obsoletes my own patch attempt.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
#6 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

Quoted 45 lines · Jan Kiszka
Quoted 31 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")
---
  lib/rsa/rsa-mod-exp.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/rsa/rsa-mod-exp.c +0 -0
index 420ab2eba0..62b2557cc2 100644

lib/rsa/rsa-mod-exp.c int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len, · line 269

Quoted 10 lines
lib/rsa/rsa-mod-exp.c +2 -2
@@ -262,8 +262,8 @@ int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
  	if (!prop->public_exponent)
  		key.exponent = RSA_DEFAULT_PUBEXP;
  	else
-		key.exponent =
-			fdt64_to_cpu(*((uint64_t *)(prop->public_exponent)));
+		rsa_convert_big_endian((uint32_t *)&key.exponent,
+				       prop->public_exponent, 2);
  	if (!key.len || !prop->modulus || !prop->rr) {
  		debug("%s: Missing RSA key info", __func__);
 
Quoted 3 lines

I debugged the same issue on our AM654x-based board where we do fit image verification in SPL. As there is apparently no unaligned access possible, this bit us as well. Obsoletes my own patch attempt.

And, blarg, I just put your tested-by on the mkimage revert and not this bugfix, sorry! I should go make coffee now...

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200507/718907c1/attachment.sig>
#7 lore raw

PATCH 1/1 rsa: fix alignment issue when getting public exponent

In reply to Heiko Stuebner
Quoted 15 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

To fill the exponent field of the rsa_public_key struct, rsa_mod_exp_sw did a cast to uint64_t of the key_prop->public_exponent field. But that alignment is not guaranteed in all cases.

This came to light when in my spl-fit-signature the key-name exceeded a certain length and with it the verification then started failing. (naming it "integrity" worked fine, "integrity-uboot" failed)

key_prop.public_exponent itself is actually a void-pointer, fdt_getprop() also just returns such a void-pointer and inside the devicetree the 64bit exponent is represented as 2 32bit numbers, so assuming a 64bit alignment can lead to false reads.

Quoted 7 lines

So just use the already existing rsa_convert_big_endian() to do the actual conversion from the dt's big-endian to the needed uint64 value.

Fixes fc2f4246b4b3 ("rsa: Split the rsa-verify to separate the modular exponentiation")

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200507/29b21d93/attachment.sig>