x86/reboot: Don't corrupt memory on non-BIOS systems
PATCH 1/1 x86/reboot: Don't corrupt memory on non-BIOS systems
native_machine_emergency_restart() writes unconditionally to the physical address of 0x472 to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.
Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
--- arch/x86/kernel/reboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/reboot.c +2 -2
@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then
@@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );/* We're probably dead after this, but... */
base-commit: eea6e4b4dfb8859446177c32961c96726d0117be
Signature · 1 line
2.34.1
H. Peter Anvin
reviewerQuoted 38 lines · Roman Kisel
native_machine_emergency_restart()writes unconditionally to the physical address of0x472to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
Signed-off-by Roman Kisel <[email protected]>---
arch/x86/kernel/reboot.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/reboot.c +2
-2@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then @@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );/* We're probably dead after this, but... */
Quoted 1 line
base-commit:
eea6e4b4dfb8859446177c32961c96726d0117be
Which system have you seen where this "corrupts" memory?
On Thu, 09 Jan 2025 19:23:31 -0800, H. Peter Anvin <[email protected]> wrote:
[...]
Quoted 1 line
Which system have you seen where this "corrupts" memory?
These are X86_64 systems that boot off of confguration in DeviceTree, and neither ACPI, BIOS, nor UEFI are present. The processors start running in the long mode right off the bat, and the physical memory below 4GiB is non-RAM or reserved. The systems are virtual machines.
All that makes *((unsigned short *)__va(0x472)) = mode; constitue a stray write and effectively corrupt contents at the address of 0x472 for these virtual systems.
On the physical systems I have had access to, 0x0..0x1000 seems to be reported as reserved in E820 or UEFI memory map, and that's what the CPU might need in the 16-bit mode for the brief times it might be used.
Perhaps, native_machine_emergency_restart being tailored to the quirks of the physical hardware cannot absorb some condition around *((unsigned short *)__va(0x472)) = mode; to make sure it is fine to write at that address? In the light of that, the callback machine_ops.emergency_restart looks as a safer option.
H. Peter Anvin
reviewerQuoted 38 lines · Roman Kisel
native_machine_emergency_restart()writes unconditionally to the physical address of0x472to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
Signed-off-by Roman Kisel <[email protected]>---
arch/x86/kernel/reboot.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/reboot.c +2
-2@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then @@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );/* We're probably dead after this, but... */
Quoted 1 line
base-commit:
eea6e4b4dfb8859446177c32961c96726d0117be
I should say: this patch is unambiguously *wrong*. It conflates the invocation mechanism with the desired post state, and they are not coupled. Calling the BIOS reboot entry point is not the normal way to reboot even on BIOS systems.
Quoted 1 line · H. Peter Anvin
On January 9, 2025 12:43:52 PM PST, Roman Kisel <[email protected]> wrote:
[...]
Quoted 2 lines
I should say: this patch is unambiguously *wrong*. It conflates the invocation mechanism with the desired post state, and they are not coupled. Calling the BIOS reboot entry point is not the normal way to reboot even on BIOS systems.
Thank you very much for taking time to review and for the chance to learn from you!
Would you like me to propose another patch where the line of
*((unsigned short *)__va(0x472)) = mode;
receives a comment for posterity why that it is okay to write at that address? Perhaps,
/*
* The common practice for the firmware is to report 0x0..0x1000
* as reserved in the RAM map. The value written to the address of
* 0x472 may be used by the firmware to perform the cold or the warm
* boot.
*/
might be a good addition to the code.
I've looked at the UEFI+ACPI spec and couldn't find any mentions of that magical address, and the code writes at that address even in the UEFI case. If you have time to recommend normative documents where that might be explained, I'd greatly appreciate that!
Signature · 2 lines
Thank you, Roman
Quoted 30 lines · Roman Kisel
native_machine_emergency_restart()writes unconditionally to the physical address of0x472to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
Signed-off-by Roman Kisel <[email protected]>--- arch/x86/kernel/reboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/reboot.c +1
-2@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then
arch/x86/kernel/reboot.c static void native_machine_emergency_restart(void) · line 684
Quoted 5 lines
arch/x86/kernel/reboot.c +1
-0@@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );
If the value of 0x472 is only meaningful in the legacy 'BOOT_BIOS' case, then at minimum the whole block should be moved to that case, not just the setting.
Thanks,
Ingo
H. Peter Anvin
reviewerQuoted 43 lines · Ingo Molnar
Quoted 30 lines · Roman Kisel
native_machine_emergency_restart()writes unconditionally to the physical address of0x472to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
Signed-off-by Roman Kisel <[email protected]>--- arch/x86/kernel/reboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/reboot.c +1
-2@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then arch/x86/kernel/reboot.c static void native_machine_emergency_restart(void) · line 684
Quoted 5 lines
arch/x86/kernel/reboot.c +1
-0@@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );If the value of
0x472is only meaningful in the legacy 'BOOT_BIOS' case, then at minimum the whole block should be moved to that case, not just the setting.
Quoted 3 lines
Thanks,
Ingo
Does the memory corruption actually matter, though?
Quoted 44 lines · H. Peter Anvin
Quoted 43 lines · Ingo Molnar
Quoted 30 lines · Roman Kisel
native_machine_emergency_restart()writes unconditionally to the physical address of0x472to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
Signed-off-by Roman Kisel <[email protected]>--- arch/x86/kernel/reboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/reboot.c +1
-2@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then arch/x86/kernel/reboot.c static void native_machine_emergency_restart(void) · line 684
Quoted 5 lines
arch/x86/kernel/reboot.c +1
-0@@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );If the value of
0x472is only meaningful in the legacy 'BOOT_BIOS' case, then at minimum the whole block should be moved to that case, not just the setting.
Quoted 5 lines
Quoted 3 lines
Thanks,
Ingo
Does the memory corruption actually matter, though?
I presume the issue came up by auditing & reviewing host writes to a barebones non-legacy VM?
I'd argue that we shouldn't be writing random values into random legacy addresses, especially if that special address doesnt seem to be covered by any modern spec? Basic defensive coding practices and such.
Thanks,
Ingo
H. Peter Anvin
reviewerQuoted 56 lines · Ingo Molnar
Quoted 44 lines · H. Peter Anvin
Quoted 43 lines · Ingo Molnar
Quoted 30 lines · Roman Kisel
native_machine_emergency_restart()writes unconditionally to the physical address of0x472to pass the warm reboot flags to BIOS. The BIOS reads this on booting to bypass memory test and do the warm boot. On the non-BIOS systems, other means have to be employed, and this write is a memory corruption.Fix that by moving the offending write into the case where the machine is rebooted via BIOS.
Signed-off-by Roman Kisel <[email protected]>--- arch/x86/kernel/reboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/reboot.c +1
-2@@ -637,9 +637,8 @@ static void native_machine_emergency_restart(void)tboot_shutdown (TB_SHUTDOWN_REBOOT ); -/* Tell the BIOS if we want cold or warm reboot */ +/* Tell the firmware if we want cold or warm reboot */ mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; - *((unsigned short *)__va (0x472 )) = mode;/* * If an EFI capsule has been registered with the firmware then arch/x86/kernel/reboot.c static void native_machine_emergency_restart(void) · line 684
Quoted 5 lines
arch/x86/kernel/reboot.c +1
-0@@ -681,6 +680,7 @@ static void native_machine_emergency_restart(void)break ;case BOOT_BIOS : + *((unsigned short *)__va (0x472 )) = mode;machine_real_restart (MRR_BIOS );If the value of
0x472is only meaningful in the legacy 'BOOT_BIOS' case, then at minimum the whole block should be moved to that case, not just the setting.Quoted 5 lines
Quoted 3 lines
Thanks,
Ingo
Does the memory corruption actually matter, though?
I presume the issue came up by auditing & reviewing host writes to a barebones non-legacy VM?
Quoted 7 lines
I'd argue that we shouldn't be writing random values into random legacy addresses, especially if that special address doesnt seem to be covered by any modern spec? Basic defensive coding practices and such.
Thanks,
Ingo
The ultimate answer to the question is probably WWWD.