efi/libstub: Fix mixed-mode crash at boot
Subsystems (2)
cover efi/libstub: Fix mixed-mode crash at boot
Hi All,
Commit 0d95981438c3 ("x86: efi/random: Invoke EFI_RNG_PROTOCOL to seed the UEFI RNG table") causes drivers/efi/libstub/random.c to be used on x86 for the first time
and some of the code in that file does not deal properly with mixed-mode setups causing a crash at boot on mixed-mode devices.
The first patch in this series fixes this and is the regression-fix from $subject.
While looking into this I did a quick search for other cases of the same problem in the libstub code and I found the same issue in the handling of LILO-style file= kernel commandline arguments, which I guess are not used that often because AFAICT we have no bug report for these not working in mixed-mode. The second patch fixes this, this is a pre-existing problem and not a 5.5 regression, still we should probably also include this fix in 5.5.
Regards,
Hans
PATCH 1/2 efi/libstub/random: Initialize pointer variables to zero for mixed mode
Commit 0d95981438c3 ("x86: efi/random: Invoke EFI_RNG_PROTOCOL to seed the UEFI RNG table"), causes the drivers/efi/libstub/random.c code to get used on x86 for the first time.
But this code was not written with EFI mixed mode in mind (running a 64 bit kernel on 32 bit EFI firmware), this causes the kernel to crash during early boot when running in mixed mode.
The problem is that in mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized which leads to crashes.
This commit fixes this by initializing pointers which are passed by reference to EFI calls to NULL before passing them, so that the upper 32 bits are initialized to 0.
--- drivers/firmware/efi/libstub/random.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/random.c +3 -3
@@ -33,7 +33,7 @@ efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg, {efi_guid_t rng_proto =EFI_RNG_PROTOCOL_GUID ;efi_status_t status; -struct efi_rng_protocol *rng; +struct efi_rng_protocol *rng =NULL ; status =efi_call_early (locate_protocol, &rng_proto,NULL , (void **)&rng);
@@ -162,8 +162,8 @@ efi_status_t efi_random_get_seed(efi_system_table_t *sys_table_arg)efi_guid_t rng_proto =EFI_RNG_PROTOCOL_GUID ;efi_guid_t rng_algo_raw =EFI_RNG_ALGORITHM_RAW ;efi_guid_t rng_table_guid =LINUX_EFI_RANDOM_SEED_TABLE_GUID ; -struct efi_rng_protocol *rng; -struct linux_efi_random_seed *seed; +struct efi_rng_protocol *rng =NULL ; +struct linux_efi_random_seed *seed =NULL ;efi_status_t status; status = efi_call_early(locate_protocol, &rng_proto, NULL,
Signature · 1 line
2.23.0
Re: [PATCH 5.5 regression fix 1/2] efi/libstub/random: Initialize pointer variables to zero for mixed mode
Quoted 14 lines · Hans de Goede
Commit
0d95981438c3("x86: efi/random: InvokeEFI_RNG_PROTOCOLto seed the UEFI RNG table"), causes thedrivers/efi/libstub/random.ccode to get used on x86 for the first time.But this code was not written with EFI mixed mode in mind (running a 64 bit kernel on 32 bit EFI firmware), this causes the kernel to crash during early boot when running in mixed mode.
The problem is that in mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized which leads to crashes.
Quoted 6 lines
This commit fixes this by initializing pointers which are passed by reference to EFI calls to NULL before passing them, so that the upper 32 bits are initialized to 0.
Fixes 0d95981438c3 ("x86: efi/random: Invoke EFI_RNG_PROTOCOL to seed the UEFI RNG table")Signed-off-by Hans de Goede <[email protected]>
Thanks Hans.
I'm a bit annoyed with myself since I should have been able to catch this in my QEMU tests and I didn't
I'll queue this (and the next patch) as a fix
Quoted 31 lines
--- drivers/firmware/efi/libstub/random.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/firmware/efi/libstub/random.c +3
-4@@ -33,7 +33,7 @@ efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg, {efi_guid_t rng_proto =EFI_RNG_PROTOCOL_GUID ;efi_status_t status; -struct efi_rng_protocol *rng; +struct efi_rng_protocol *rng =NULL ; status =efi_call_early (locate_protocol, &rng_proto,NULL , (void **)&rng);@@ -162,8 +162,8 @@ efi_status_t efi_random_get_seed(efi_system_table_t *sys_table_arg)efi_guid_t rng_proto =EFI_RNG_PROTOCOL_GUID ;efi_guid_t rng_algo_raw =EFI_RNG_ALGORITHM_RAW ;efi_guid_t rng_table_guid =LINUX_EFI_RANDOM_SEED_TABLE_GUID ; -struct efi_rng_protocol *rng; -struct linux_efi_random_seed *seed; +struct efi_rng_protocol *rng =NULL ; +struct linux_efi_random_seed *seed =NULL ;efi_status_t status; status = efi_call_early(locate_protocol, &rng_proto, NULL, --2.23.0
PATCH 2/2 efi/libstub/helper: Initialize pointer variables to zero for mixed mode
In reply to Hans de GoedeWhen running in EFI mixed mode (running a 64 bit kernel on 32 bit EFI firmware), we _must_ initialize any pointers which are returned by reference by an EFI call to NULL before making the EFI call.
In mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized unless we explicitly set them to 0 before the call.
We have had this bug in the efi-stub-helper.c file reading code for a while now, but this has likely not been noticed sofar because this code only gets triggered when LILO style file=... arguments are present on the kernel cmdline.
--- drivers/firmware/efi/libstub/efi-stub-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +2 -2
@@ -365,7 +365,7 @@ static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, u64 *file_sz) {efi_file_handle_t *h, *fh = __fh; -efi_file_info_t *info; +efi_file_info_t *info =NULL ;efi_status_t status;efi_guid_t info_guid =EFI_FILE_INFO_ID ;unsigned long info_sz;
@@ -527,7 +527,7 @@ efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,unsigned long *load_addr, unsigned long *load_size) { - struct file_info *files; + struct file_info *files = NULL;unsigned long file_addr;u64 file_size_total;efi_file_handle_t *fh =NULL ;
Signature · 1 line
2.23.0
Re: [PATCH 5.5 regression fix 2/2] efi/libstub/helper: Initialize pointer variables to zero for mixed mode
Quoted 26 lines · Hans de Goede
When running in EFI mixed mode (running a 64 bit kernel on 32 bit EFI firmware), we _must_ initialize any pointers which are returned by reference by an EFI call to NULL before making the EFI call.
In mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized unless we explicitly set them to 0 before the call.
We have had this bug in the
efi-stub-helper.cfile reading code for a while now, but this has likely not been noticed sofar because this code only gets triggered when LILO style file=... arguments are present on the kernel cmdline.Signed-off-by Hans de Goede <[email protected]>--- drivers/firmware/efi/libstub/efi-stub-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0
drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 367
Quoted 3 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0@@ -365,7 +365,7 @@ static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, u64 *file_sz) {efi_file_handle_t *h, *fh = __fh;
What about h? Doesn't it suffer from the same problem?
drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 371
Quoted 5 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-1-efi_file_info_t *info; +efi_file_info_t *info =NULL ;efi_status_t status;efi_guid_t info_guid =EFI_FILE_INFO_ID ;unsigned long info_sz;
And info_sz?
Quoted 12 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-2@@ -527,7 +527,7 @@ efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,unsigned long *load_addr, unsigned long *load_size) { - struct file_info *files; + struct file_info *files = NULL;unsigned long file_addr;u64 file_size_total;efi_file_handle_t *fh =NULL ; --2.23.0
Re: [PATCH 5.5 regression fix 2/2] efi/libstub/helper: Initialize pointer variables to zero for mixed mode
Hi,
Quoted 33 lines · Ard Biesheuvel
Quoted 26 lines · Hans de Goede
When running in EFI mixed mode (running a 64 bit kernel on 32 bit EFI firmware), we _must_ initialize any pointers which are returned by reference by an EFI call to NULL before making the EFI call.
In mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized unless we explicitly set them to 0 before the call.
We have had this bug in the
efi-stub-helper.cfile reading code for a while now, but this has likely not been noticed sofar because this code only gets triggered when LILO style file=... arguments are present on the kernel cmdline.Signed-off-by Hans de Goede <[email protected]>--- drivers/firmware/efi/libstub/efi-stub-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 367
Quoted 3 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0@@ -365,7 +365,7 @@ static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, u64 *file_sz) {efi_file_handle_t *h, *fh = __fh;What about h? Doesn't it suffer from the same problem?
Quoted 7 lines
drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 371
Quoted 5 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-1-efi_file_info_t *info; +efi_file_info_t *info =NULL ;efi_status_t status;efi_guid_t info_guid =EFI_FILE_INFO_ID ;unsigned long info_sz;And
info_sz?
You are right in both cases. I only checked allocate_pool and locate_protocol callers as those are the usual suspects.
Shall I send a v2 of just this patch, or of the entire series, or are you going to fix this up?
Regards,
Hans
Quoted 15 lines
Quoted 14 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-2@@ -527,7 +527,7 @@ efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,unsigned long *load_addr, unsigned long *load_size) { - struct file_info *files; + struct file_info *files = NULL;unsigned long file_addr;u64 file_size_total;efi_file_handle_t *fh =NULL ; --2.23.0
Re: [PATCH 5.5 regression fix 2/2] efi/libstub/helper: Initialize pointer variables to zero for mixed mode
In reply to Ard BiesheuvelHi,
Quoted 33 lines · Ard Biesheuvel
Quoted 26 lines · Hans de Goede
When running in EFI mixed mode (running a 64 bit kernel on 32 bit EFI firmware), we _must_ initialize any pointers which are returned by reference by an EFI call to NULL before making the EFI call.
In mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized unless we explicitly set them to 0 before the call.
We have had this bug in the
efi-stub-helper.cfile reading code for a while now, but this has likely not been noticed sofar because this code only gets triggered when LILO style file=... arguments are present on the kernel cmdline.Signed-off-by Hans de Goede <[email protected]>--- drivers/firmware/efi/libstub/efi-stub-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 367
Quoted 3 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0@@ -365,7 +365,7 @@ static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, u64 *file_sz) {efi_file_handle_t *h, *fh = __fh;What about h? Doesn't it suffer from the same problem?
Quoted 7 lines
drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 371
Quoted 5 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-1-efi_file_info_t *info; +efi_file_info_t *info =NULL ;efi_status_t status;efi_guid_t info_guid =EFI_FILE_INFO_ID ;unsigned long info_sz;And
info_sz?
And "efi_file_io_interface_t *io" and "efi_file_handle_t *fh" in efi_open_volume().
I think that is all of them.
Regards,
Hans
Re: [PATCH 5.5 regression fix 2/2] efi/libstub/helper: Initialize pointer variables to zero for mixed mode
Quoted 43 lines · Hans de Goede
Hi,
Quoted 39 lines · Ard Biesheuvel
Quoted 26 lines · Hans de Goede
When running in EFI mixed mode (running a 64 bit kernel on 32 bit EFI firmware), we _must_ initialize any pointers which are returned by reference by an EFI call to NULL before making the EFI call.
In mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized unless we explicitly set them to 0 before the call.
We have had this bug in the
efi-stub-helper.cfile reading code for a while now, but this has likely not been noticed sofar because this code only gets triggered when LILO style file=... arguments are present on the kernel cmdline.Signed-off-by Hans de Goede <[email protected]>--- drivers/firmware/efi/libstub/efi-stub-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 367
Quoted 3 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0@@ -365,7 +365,7 @@ static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, u64 *file_sz) {efi_file_handle_t *h, *fh = __fh;What about h? Doesn't it suffer from the same problem?
Quoted 5 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-1-efi_file_info_t *info; +efi_file_info_t *info =NULL ;efi_status_t status;efi_guid_t info_guid =EFI_FILE_INFO_ID ;unsigned long info_sz;
Quoted 7 lines
Quoted 1 line
And
info_sz?And "
efi_file_io_interface_t*io" and "efi_file_handle_t*fh" inefi_open_volume().I think that is all of them.
OK.
I'll fix it up locally.
Re: [PATCH 5.5 regression fix 2/2] efi/libstub/helper: Initialize pointer variables to zero for mixed mode
Quoted 50 lines · Ard Biesheuvel
Quoted 48 lines · Hans de Goede
Hi,
Quoted 33 lines · Ard Biesheuvel
Quoted 26 lines · Hans de Goede
When running in EFI mixed mode (running a 64 bit kernel on 32 bit EFI firmware), we _must_ initialize any pointers which are returned by reference by an EFI call to NULL before making the EFI call.
In mixed mode pointers are 64 bit, but when running on a 32 bit firmware, EFI calls which return a pointer value by reference only fill the lower 32 bits of the passed pointer, leaving the upper 32 bits uninitialized unless we explicitly set them to 0 before the call.
We have had this bug in the
efi-stub-helper.cfile reading code for a while now, but this has likely not been noticed sofar because this code only gets triggered when LILO style file=... arguments are present on the kernel cmdline.Signed-off-by Hans de Goede <[email protected]>--- drivers/firmware/efi/libstub/efi-stub-helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 367
Quoted 3 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +0
-0@@ -365,7 +365,7 @@ static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, u64 *file_sz) {efi_file_handle_t *h, *fh = __fh;What about h? Doesn't it suffer from the same problem?
Quoted 7 lines
drivers/firmware/efi/libstub/efi-stub-helper.c static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, · line 371
Quoted 5 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +1
-1-efi_file_info_t *info; +efi_file_info_t *info =NULL ;efi_status_t status;efi_guid_t info_guid =EFI_FILE_INFO_ID ;unsigned long info_sz;And
info_sz?And "
efi_file_io_interface_t*io" and "efi_file_handle_t*fh" inefi_open_volume().
Quoted 6 lines
Quoted 2 lines
I think that is all of them.
OK.
I'll fix it up locally.
Actually, I am going to drop this patch, and disable file loading entirely for mixed mode. Your findings here prove it is unlikely that it works on mixed mode systems today, and the efi_file_handle_t::open() prototype is actually incompatible with mixed mode, since it takes u64 arguments, which are marshalled incorrectly by the thunking code (each function arg gets a 32-bit stack slot for the 32-bit calling convention, which works fine for pointers and smaller types, but it breaks for u64 since they require two stack slots)
Quoted 9 lines · Hans de Goede
Hi All,
Commit
0d95981438c3("x86: efi/random: InvokeEFI_RNG_PROTOCOLto seed the UEFI RNG table") causesdrivers/efi/libstub/random.cto be used on x86 for the first time
and some of the code in that file does not deal properly with mixed-mode setups causing a crash at boot on mixed-mode devices.The first patch in this series fixes this and is the regression-fix from $subject.
Sorry for the bug, and thanks for the bugfix!
Dominik