Skip to contentSkip to conversation

efi: Initialize canary to non-zero value

PATCH Superseded Glenn Washburn 4 participants 7 messages Nov 12, 2023 – Nov 18, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi: Initialize canary to non-zero value

The canary, __stack_chk_guard, is in the BSS and so will get initialized to zero if it is not explicitly initialized. If the UEFI firmware does not support the RNG protocol, then the canary will not be randomized and will be used as zero. This seems like a possibly easier value to write by an attacker. Initialize canary to static random bytes, so that it is still random when there is not RNG protocol.

---
 grub-core/kern/efi/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/efi/init.c +1 -1
index 0e28bea17a76..b85d98ca47fd 100644
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
 
 static grub_efi_uint8_t stack_chk_guard_buf[32];
 
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;
 
 void __attribute__ ((noreturn))
 __stack_chk_fail (void)
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Glenn Washburn

The canary, __stack_chk_guard, is in the BSS and so will get initialized to zero if it is not explicitly initialized. If the UEFI firmware does not support the RNG protocol, then the canary will not be randomized and will be used as zero. This seems like a possibly easier value to write by an attacker. Initialize canary to static random bytes, so that it is still random when there is not RNG protocol.

Quoted 9 lines
---
  grub-core/kern/efi/init.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/efi/init.c +0 -0
index 0e28bea17a76..b85d98ca47fd 100644

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 47

Quoted 8 lines
grub-core/kern/efi/init.c +1 -1
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
 
  static grub_efi_uint8_t stack_chk_guard_buf[32];
 
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;
 
  void __attribute__ ((noreturn))
  __stack_chk_fail (void)

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by
#2 lore raw
Quoted 11 lines · Heinrich Schuchardt
Quoted 8 lines · Glenn Washburn

The canary, __stack_chk_guard, is in the BSS and so will get initialized to zero if it is not explicitly initialized. If the UEFI firmware does not support the RNG protocol, then the canary will not be randomized and will be used as zero. This seems like a possibly easier value to write by an attacker. Initialize canary to static random bytes, so that it is still random when there is not RNG protocol.

Quoted 10 lines
Quoted 10 lines
---
  grub-core/kern/efi/init.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/efi/init.c +0 -0
index 0e28bea17a76..b85d98ca47fd 100644
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
 
Quoted 4 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 44

Quoted 4 lines
grub-core/kern/efi/init.c +1 -1
  static grub_efi_uint8_t stack_chk_guard_buf[32];
 
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;

I would add last sentence from the commit message before this line. I can do it for you before push...

Quoted 3 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 47

Quoted 3 lines
grub-core/kern/efi/init.c +0 -0
  void __attribute__ ((noreturn))
  __stack_chk_fail (void)

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
Quoted 8 lines · On Mon, 13 Nov 2023 17:18:50 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 7 lines · Heinrich Schuchardt
Quoted 6 lines · Glenn Washburn

The canary, __stack_chk_guard, is in the BSS and so will get initialized to zero if it is not explicitly initialized. If the UEFI firmware does not support the RNG protocol, then the canary will not be randomized and will be used as zero. This seems like a possibly easier value to write by an attacker. Initialize canary to static random bytes, so that it is still random when there is not RNG protocol.

Ugh. s/not/no/

Quoted 17 lines
Quoted 4 lines
Quoted 2 lines
Quoted 10 lines
Quoted 10 lines
---
  grub-core/kern/efi/init.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/efi/init.c +0 -0
index 0e28bea17a76..b85d98ca47fd 100644
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
 
Quoted 4 lines
Quoted 4 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 44

Quoted 4 lines
grub-core/kern/efi/init.c +1 -1
  static grub_efi_uint8_t stack_chk_guard_buf[32];
 
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;

Just now reflecting on this I had the idea that perhaps even better is to have this randomly generated at build time and inserted as an autoconf variable. Then every build would have a different canary. I think I'll send a patch for this soon.

Quoted 3 lines

I would add last sentence from the commit message before this line. I can do it for you before push...

Sure. And please remove the 't' mentioned above in the commit message.

Glenn

Quoted 6 lines
Quoted 4 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 47

Quoted 4 lines
grub-core/kern/efi/init.c +0 -0
  void __attribute__ ((noreturn))
  __stack_chk_fail (void)

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 39 lines · On Tue, 14 Nov 2023, 03:19 Glenn Washburn, <[email protected]> wrote:
Quoted 3 lines · On Mon, 13 Nov 2023 17:18:50 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Heinrich Schuchardt
Quoted 1 line · Glenn Washburn

The canary, __stack_chk_guard, is in the BSS and so will get

initialized to

Quoted 1 line
Quoted 1 line
Quoted 1 line

zero if it is not explicitly initialized. If the UEFI firmware does

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

support the RNG protocol, then the canary will not be randomized and

will

Quoted 1 line
Quoted 1 line
Quoted 1 line

be used as zero. This seems like a possibly easier value to write by

an

Quoted 1 line
Quoted 1 line
Quoted 1 line

attacker. Initialize canary to static random bytes, so that it is

still

Quoted 1 line
Quoted 1 line
Quoted 1 line

random when there is not RNG protocol.

Ugh. s/not/no/

Quoted 11 lines
Quoted 4 lines
Quoted 2 lines
Quoted 4 lines
Quoted 4 lines
---
  grub-core/kern/efi/init.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 5 lines
Quoted 5 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid =

Quoted 5 lines
grub-core/kern/efi/init.c +0 -0
index 0e28bea17a76..b85d98ca47fd 100644
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid =

GRUB_EFI_RNG_PROTOCOL_GUID;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
grub-core/kern/efi/init.c +0 -0
  static grub_efi_uint8_t stack_chk_guard_buf[32];
 
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 44

Quoted 2 lines
grub-core/kern/efi/init.c +1 -1
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;

Just now reflecting on this I had the idea that perhaps even better is to have this randomly generated at build time and inserted as an autoconf variable. Then every build would have a different canary. I think I'll send a patch for this soon.

But please insure it is reproducible (as in reproducible builds initiative).

But also, one already has time and date macros that are unique and reproducible in most distros. Can you use TIME macro as either the random value or the seed to make a suitable random number?

https://reproducible-builds.org/docs/source-date-epoch/ and all the ways it is already available at cpp time.

Quoted 13 lines
Quoted 3 lines

I would add last sentence from the commit message before this line. I can do it for you before push...

Sure. And please remove the 't' mentioned above in the commit message.

Glenn

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
grub-core/kern/efi/init.c +0 -0
  void __attribute__ ((noreturn))
  __stack_chk_fail (void)
Quoted 7 lines
Quoted 1 line

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw
Quoted 52 lines · On Tue, 14 Nov 2023 04:05:00 +0000 Dimitri John Ledkov…
Quoted 39 lines · On Tue, 14 Nov 2023, 03:19 Glenn Washburn, <[email protected]> wrote:
Quoted 3 lines · On Mon, 13 Nov 2023 17:18:50 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Heinrich Schuchardt
Quoted 1 line · Glenn Washburn

The canary, __stack_chk_guard, is in the BSS and so will get

initialized to

Quoted 1 line
Quoted 1 line
Quoted 1 line

zero if it is not explicitly initialized. If the UEFI firmware does

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

support the RNG protocol, then the canary will not be randomized and

will

Quoted 1 line
Quoted 1 line
Quoted 1 line

be used as zero. This seems like a possibly easier value to write by

an

Quoted 1 line
Quoted 1 line
Quoted 1 line

attacker. Initialize canary to static random bytes, so that it is

still

Quoted 1 line
Quoted 1 line
Quoted 1 line

random when there is not RNG protocol.

Ugh. s/not/no/

Quoted 11 lines
Quoted 4 lines
Quoted 2 lines
Quoted 4 lines
Quoted 4 lines
---
  grub-core/kern/efi/init.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 5 lines
Quoted 5 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid =

Quoted 5 lines
grub-core/kern/efi/init.c +0 -0
index 0e28bea17a76..b85d98ca47fd 100644
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid =

GRUB_EFI_RNG_PROTOCOL_GUID;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
grub-core/kern/efi/init.c +0 -0
  static grub_efi_uint8_t stack_chk_guard_buf[32];
 
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 44

Quoted 2 lines
grub-core/kern/efi/init.c +1 -1
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;

Just now reflecting on this I had the idea that perhaps even better is to have this randomly generated at build time and inserted as an autoconf variable. Then every build would have a different canary. I think I'll send a patch for this soon.

Quoted 1 line

But please insure it is reproducible (as in reproducible builds initiative).

This is a good idea.

Quoted 5 lines

But also, one already has time and date macros that are unique and reproducible in most distros. Can you use TIME macro as either the random value or the seed to make a suitable random number?
https://reproducible-builds.org/docs/source-date-epoch/ and all the ways it is already available at cpp time.

This link seems to be suggesting to use the SOURCE_DATE_EPOCH environment variable if it exists. Is this what you mean by "TIME macro"? If not, could you explain further?

I'm wondering if it might be better to provide a --enable-stack-protector-init= configure option instead. I like the idea of not having this and everything be automated based on the reproducible value, which would create less work for those using this feature. However, I'm concerned that the reproducible value itself may not be suitable as a canary and that other tools/dependencies might be required to make it more suitable (eg. sha1sum). Do you have any thoughts on this?

Glenn

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines

I would add last sentence from the commit message before this line. I can do it for you before push...

Sure. And please remove the 't' mentioned above in the commit message.

Glenn

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
grub-core/kern/efi/init.c +0 -0
  void __attribute__ ((noreturn))
  __stack_chk_fail (void)
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw
In reply to Daniel Kiper
Quoted 25 lines · On Mon, 13 Nov 2023 17:18:50 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 11 lines · Heinrich Schuchardt
Quoted 8 lines · Glenn Washburn

The canary, __stack_chk_guard, is in the BSS and so will get initialized to zero if it is not explicitly initialized. If the UEFI firmware does not support the RNG protocol, then the canary will not be randomized and will be used as zero. This seems like a possibly easier value to write by an attacker. Initialize canary to static random bytes, so that it is still random when there is not RNG protocol.

Quoted 10 lines
Quoted 10 lines
---
  grub-core/kern/efi/init.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/efi/init.c +0 -0
index 0e28bea17a76..b85d98ca47fd 100644
@@ -41,7 +41,7 @@ static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
 
Quoted 4 lines
Quoted 4 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 44

Quoted 4 lines
grub-core/kern/efi/init.c +1 -1
  static grub_efi_uint8_t stack_chk_guard_buf[32];
 
-grub_addr_t __stack_chk_guard;
+grub_addr_t __stack_chk_guard = (grub_addr_t) 0x92f2b7e2f193b25c;

I've come across more information[1] on this subject that leads me to believe that at least the byte with the lowest memory address should be a NULL byte (eg. grub_cpu_to_be64_compile_time(0x00f2b7e2f193b25c)) to make the common case of string buffer overflows non-bypassable. And in the absence of random bytes at runtime, it might be a good idea to xor with the current time and perhaps the EFI app base load address. So I suggest not merging this yet and I'll have a v2 coming soon.

Glenn

[1]
https://www.sans.org/blog/stack-canaries-gingerly-sidestepping-the-cage/

Quoted 9 lines

I would add last sentence from the commit message before this line. I can do it for you before push...

Quoted 3 lines

grub-core/kern/efi/init.c static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID; · line 47

Quoted 3 lines
grub-core/kern/efi/init.c +0 -0
  void __attribute__ ((noreturn))
  __stack_chk_fail (void)

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel