Skip to contentSkip to conversation

x86 fixes for 3.9

GIT PULL H. Peter Anvin 7 participants 13 messages Apr 25, 2013 – Apr 26, 2013 x86

lore
Share & tools
lore
#0 lore raw

Hi Linus,

The following changes since commit 0fbd06761f5c17cc9b20e02af60fd7ee9c895996:

Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc (2013-04-24 17:10:18 -0700)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86-urgent-for-linus

This is exclusively two EFI fixes:

* The EFI variable anti-bricking algorithm merged in -rc8 broke booting on some Apple machines because they implement EFI spec 1.10, which doesn't provide a QueryVariableInfo() runtime function and the logic used to check for the existence of that function was insufficient. Fix from Josh Boyer.

* The anti-bricking algorithm also introduced a compiler warning on 32-bit. Fix from Borislav Petkov.

----------------------------------------------------------------

Borislav Petkov (1):
x86, efi: Fix a build warning

H. Peter Anvin (1):
Merge tag 'efi-urgent' into x86/urgent

Josh Boyer (1):
efi: Check EFI revision in setup_efi_vars

 arch/x86/boot/compressed/eboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/eboot.c +2 -2
index 8615f75..35ee62f 100644
@@ -258,7 +258,7 @@ static efi_status_t setup_efi_vars(struct boot_params *params)
 	u64 store_size, remaining_size, var_size;
 	efi_status_t status;
 
-	if (!sys_table->runtime->query_variable_info)
+	if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
 		return EFI_UNSUPPORTED;
 
 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
@@ -266,7 +266,7 @@ static efi_status_t setup_efi_vars(struct boot_params *params)
 	while (data && data->next)
 		data = (struct setup_data *)(unsigned long)data->next;
 
-	status = efi_call_phys4(sys_table->runtime->query_variable_info,
+	status = efi_call_phys4((void *)sys_table->runtime->query_variable_info,
 				EFI_VARIABLE_NON_VOLATILE |
 				EFI_VARIABLE_BOOTSERVICE_ACCESS |
 				EFI_VARIABLE_RUNTIME_ACCESS, &store_size,
#1 lore raw

arch/x86/boot/compressed/eboot.c static efi_status_t setup_efi_vars(struct boot_params *params) · line 262

Quoted 4 lines · H. Peter Anvin
arch/x86/boot/compressed/eboot.c +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

And why the hell don't we have a real structure that has been filled out properly, and instead apparently just do this "point to random memory that doesn't necessarily have the full structure?

Linus

#2 lore raw
Quoted 9 lines · Linus Torvalds

arch/x86/boot/compressed/eboot.c static efi_status_t setup_efi_vars(struct boot_params *params) · line 262

Quoted 4 lines · H. Peter Anvin
arch/x86/boot/compressed/eboot.c +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

As far as the spec's concerned, yes. As far as reality's concerned - if anything doesn't provide it, we're already crashing when efi_virt_query_variable_info() gets called. Nobody's complained so far.

Quoted 3 lines

And why the hell don't we have a real structure that has been filled out properly, and instead apparently just do this "point to random memory that doesn't necessarily have the full structure?

This is early boot code, we're not in the kernel proper yet. All we have is the structure that's handed to us by the firmware, and the size of that structure varies depending on its version.

Signature · 2 lines
Matthew Garrett | [email protected]
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�
#3 lore raw
Quoted 7 lines · Matthew Garrett
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · H. Peter Anvin
arch/x86/boot/compressed/eboot.c +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;
Quoted 7 lines
Quoted 3 lines

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

As far as the spec's concerned, yes. As far as reality's concerned - if anything doesn't provide it, we're already crashing when efi_virt_query_variable_info() gets called. Nobody's complained so far.

Well, I don't know if this is related, but commit e971318bbed6 broke the google EFI SMI driver with

BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [<ffffffff814a7245>] variable_is_present+0x55/0x170
Call Trace:
[<ffffffff814a9936>] register_efivars+0x106/0x370
[<ffffffff818ff430>] ? firmware_map_add_early+0xb1/0xb1
[<ffffffff818ff6dd>] gsmi_init+0x2ad/0x3da
[<ffffffff8100020f>] do_one_initcall+0x3f/0x170

...

Signature · 2 lines
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
#4 lore raw
Quoted 19 lines · Michel Lespinasse
Quoted 7 lines · Matthew Garrett
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · H. Peter Anvin
arch/x86/boot/compressed/eboot.c +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;
Quoted 7 lines
Quoted 3 lines

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

As far as the spec's concerned, yes. As far as reality's concerned - if anything doesn't provide it, we're already crashing when efi_virt_query_variable_info() gets called. Nobody's complained so far.

Well, I don't know if this is related, but commit e971318bbed6 broke the google EFI SMI driver with

Quoted 9 lines
BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [<ffffffff814a7245>] variable_is_present+0x55/0x170
Call Trace:
[<ffffffff814a9936>] register_efivars+0x106/0x370
[<ffffffff818ff430>] ? firmware_map_add_early+0xb1/0xb1
[<ffffffff818ff6dd>] gsmi_init+0x2ad/0x3da
[<ffffffff8100020f>] do_one_initcall+0x3f/0x170

...

I don't know either. Could you test this patch and see if it does anything?

-hpa

#5 lore raw
Quoted 29 lines · H. Peter Anvin
Quoted 19 lines · Michel Lespinasse
Quoted 7 lines · Matthew Garrett
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · H. Peter Anvin
arch/x86/boot/compressed/eboot.c +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;
Quoted 7 lines
Quoted 3 lines

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

As far as the spec's concerned, yes. As far as reality's concerned - if anything doesn't provide it, we're already crashing when efi_virt_query_variable_info() gets called. Nobody's complained so far.

Well, I don't know if this is related, but commit e971318bbed6 broke the google EFI SMI driver with

Quoted 8 lines
BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [<ffffffff814a7245>] variable_is_present+0x55/0x170
Call Trace:
[<ffffffff814a9936>] register_efivars+0x106/0x370
[<ffffffff818ff430>] ? firmware_map_add_early+0xb1/0xb1
[<ffffffff818ff6dd>] gsmi_init+0x2ad/0x3da
[<ffffffff8100020f>] do_one_initcall+0x3f/0x170

...

Quoted 1 line

I don't know either. Could you test this patch and see if it does anything?

Nope, still seeing the crash with this patch applied.

Signature · 2 lines
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
#6 lore raw
Quoted 30 lines · Michel Lespinasse
Quoted 29 lines · H. Peter Anvin
Quoted 27 lines · Michel Lespinasse
Quoted 7 lines · Matthew Garrett
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · H. Peter Anvin
(quoted diff) +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;
Quoted 7 lines
Quoted 3 lines

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

As far as the spec's concerned, yes. As far as reality's concerned - if anything doesn't provide it, we're already crashing when efi_virt_query_variable_info() gets called. Nobody's complained so far.

Well, I don't know if this is related, but commit e971318bbed6 broke the google EFI SMI driver with

BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [<ffffffff814a7245>] variable_is_present+0x55/0x170
Call Trace:
[<ffffffff814a9936>] register_efivars+0x106/0x370
[<ffffffff818ff430>] ? firmware_map_add_early+0xb1/0xb1
[<ffffffff818ff6dd>] gsmi_init+0x2ad/0x3da
[<ffffffff8100020f>] do_one_initcall+0x3f/0x170

...

Quoted 3 lines
Quoted 1 line

I don't know either. Could you test this patch and see if it does anything?

Nope, still seeing the crash with this patch applied.

Could you try the following patch against Linus' tree? The bug you've found and the changes in the pull request are unrelated.

---

drivers/firmware/efivars.c +3 -0
index 182ce94..2a4f619 100644
@@ -1635,6 +1635,9 @@ static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
 	unsigned long strsize1, strsize2;
 	bool found = false;
 
+	if (list_empty(&efivars->list))
+		return false;
+
 	strsize1 = ucs2_strsize(variable_name, 1024);
 	list_for_each_entry_safe(entry, n, &efivars->list, list) {
 		strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
#7 lore raw
Quoted 26 lines · Matt Fleming
Quoted 13 lines · Michel Lespinasse
Quoted 12 lines · H. Peter Anvin
Quoted 10 lines · Michel Lespinasse

Well, I don't know if this is related, but commit e971318bbed6 broke the google EFI SMI driver with

BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [<ffffffff814a7245>] variable_is_present+0x55/0x170
Call Trace:
[<ffffffff814a9936>] register_efivars+0x106/0x370
[<ffffffff818ff430>] ? firmware_map_add_early+0xb1/0xb1
[<ffffffff818ff6dd>] gsmi_init+0x2ad/0x3da
[<ffffffff8100020f>] do_one_initcall+0x3f/0x170

...

Quoted 3 lines
Quoted 1 line

I don't know either. Could you test this patch and see if it does anything?

Nope, still seeing the crash with this patch applied.

Could you try the following patch against Linus' tree? The bug you've found and the changes in the pull request are unrelated.

drivers/firmware/efivars.c +0 -0
index 182ce94..2a4f619 100644

drivers/firmware/efivars.c static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor) · line 1643

Quoted 9 lines
drivers/firmware/efivars.c +3 -0
@@ -1635,6 +1635,9 @@ static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
        unsigned long strsize1, strsize2;
        bool found = false;
 
+       if (list_empty(&efivars->list))
+               return false;
+
        strsize1 = ucs2_strsize(variable_name, 1024);
        list_for_each_entry_safe(entry, n, &efivars->list, list) {
                strsize2 = ucs2_strsize(entry->var.VariableName, 1024);

Still seeing the crash.

I went and compared the crash dump with the vmlinux disassembly; the issue is a NULL pointer dereference in list_for_each_entry_safe(). list_empty() checks that the head node points to itself, but here the head node has NULL. I think this may be due to gsmi_init() being called before efivars_init(). Not sure what's the proper fix though.

Signature · 2 lines
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
#8 lore raw
Quoted 7 lines · Michel Lespinasse

Still seeing the crash.

I went and compared the crash dump with the vmlinux disassembly; the issue is a NULL pointer dereference in list_for_each_entry_safe(). list_empty() checks that the head node points to itself, but here the head node has NULL. I think this may be due to gsmi_init() being called before efivars_init(). Not sure what's the proper fix though.

Ohh... I see what you mean. The bug is in variable_is_present() because it accesses __efivars directly, which a) isn't the struct efivars gsmi.c uses and b) hasn't been initialised. Something like this might work.

---

drivers/firmware/efivars.c +7 -5
index 182ce94..f4baa11 100644
@@ -1628,10 +1628,11 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 	return count;
 }
 
-static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
+static bool variable_is_present(struct efivars *efivars,
+				efi_char16_t *variable_name,
+				efi_guid_t *vendor)
 {
 	struct efivar_entry *entry, *n;
-	struct efivars *efivars = &__efivars;
 	unsigned long strsize1, strsize2;
 	bool found = false;
 
@@ -1703,8 +1704,8 @@ static void efivar_update_sysfs_entries(struct work_struct *work)
 			if (status != EFI_SUCCESS) {
 				break;
 			} else {
-				if (!variable_is_present(variable_name,
-				    &vendor)) {
+				if (!variable_is_present(efivars,
+				    variable_name, &vendor)) {
 					found = true;
 					break;
 				}
@@ -2008,7 +2009,8 @@ int register_efivars(struct efivars *efivars,
 			 * we'll ever see a different variable name,
 			 * and may end up looping here forever.
 			 */
-			if (variable_is_present(variable_name, &vendor_guid)) {
+			if (variable_is_present(efivars, variable_name,
+						&vendor_guid)) {
 				dup_variable_bug(variable_name, &vendor_guid,
 						 variable_name_size);
 				status = EFI_NOT_FOUND;
#9 lore raw
Quoted 9 lines · Matt Fleming
Quoted 7 lines · Michel Lespinasse

Still seeing the crash.

I went and compared the crash dump with the vmlinux disassembly; the issue is a NULL pointer dereference in list_for_each_entry_safe(). list_empty() checks that the head node points to itself, but here the head node has NULL. I think this may be due to gsmi_init() being called before efivars_init(). Not sure what's the proper fix though.

Quoted 3 lines

Ohh... I see what you mean. The bug is in variable_is_present() because it accesses __efivars directly, which a) isn't the struct efivars gsmi.c uses and b) hasn't been initialised. Something like this might work.

[... skipping patch ...]

Yes, this one fixes it. Thanks !

Signature · 2 lines
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
Tested-by Michel Lespinasse
#10 lore raw
Quoted 10 lines · Michel Lespinasse
Quoted 9 lines · Matt Fleming
Quoted 7 lines · Michel Lespinasse

Still seeing the crash.

I went and compared the crash dump with the vmlinux disassembly; the issue is a NULL pointer dereference in list_for_each_entry_safe(). list_empty() checks that the head node points to itself, but here the head node has NULL. I think this may be due to gsmi_init() being called before efivars_init(). Not sure what's the proper fix though.

Quoted 7 lines
Quoted 3 lines

Ohh... I see what you mean. The bug is in variable_is_present() because it accesses __efivars directly, which a) isn't the struct efivars gsmi.c uses and b) hasn't been initialised. Something like this might work.

[... skipping patch ...]

Yes, this one fixes it. Thanks !

Thanks for testing!

Linus, did you want to take this one directly?

---

Quoted 1 line

From f576b789b29dab31ddc1c7d37af63f10fb203fb7 Mon Sep 17 00:00:00 2001

From: Matt Fleming <[email protected]>
Date: Fri, 26 Apr 2013 10:10:55 +0100
Subject: [PATCH] efivars: only check for duplicates on the registered list

variable_is_present() accesses '__efivars' directly, but when called via gsmi_init() Michel reports observing the following crash,

  BUG: unable to handle kernel NULL pointer dereference at (null)
  IP: [<ffffffff814a7245>] variable_is_present+0x55/0x170
  Call Trace:
  [<ffffffff814a9936>] register_efivars+0x106/0x370
  [<ffffffff818ff430>] ? firmware_map_add_early+0xb1/0xb1
  [<ffffffff818ff6dd>] gsmi_init+0x2ad/0x3da
  [<ffffffff8100020f>] do_one_initcall+0x3f/0x170

The reason for the crash is that '__efivars' hasn't been initialised nor has it been registered with register_efivars() by the time the google EFI SMI driver runs. The gsmi code uses its own struct efivars, and therefore, a different variable list. Fix the above crash by passing the registered struct efivars to variable_is_present(), so that we traverse the correct list.

---
 drivers/firmware/efivars.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/firmware/efivars.c +7 -5
index 182ce94..f4baa11 100644
@@ -1628,10 +1628,11 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 	return count;
 }
 
-static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
+static bool variable_is_present(struct efivars *efivars,
+				efi_char16_t *variable_name,
+				efi_guid_t *vendor)
 {
 	struct efivar_entry *entry, *n;
-	struct efivars *efivars = &__efivars;
 	unsigned long strsize1, strsize2;
 	bool found = false;
 
@@ -1703,8 +1704,8 @@ static void efivar_update_sysfs_entries(struct work_struct *work)
 			if (status != EFI_SUCCESS) {
 				break;
 			} else {
-				if (!variable_is_present(variable_name,
-				    &vendor)) {
+				if (!variable_is_present(efivars,
+				    variable_name, &vendor)) {
 					found = true;
 					break;
 				}
@@ -2008,7 +2009,8 @@ int register_efivars(struct efivars *efivars,
 			 * we'll ever see a different variable name,
 			 * and may end up looping here forever.
 			 */
-			if (variable_is_present(variable_name, &vendor_guid)) {
+			if (variable_is_present(efivars, variable_name,
+						&vendor_guid)) {
 				dup_variable_bug(variable_name, &vendor_guid,
 						 variable_name_size);
 				status = EFI_NOT_FOUND;
Signature · 1 line
1.7.10.4
#11 lore raw
In reply to Matthew Garrett
Quoted 7 lines · Matthew Garrett
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · H. Peter Anvin
arch/x86/boot/compressed/eboot.c +1 -1
-       if (!sys_table->runtime->query_variable_info)
+       if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
                return EFI_UNSUPPORTED;
Quoted 7 lines
Quoted 3 lines

Is a EFI 2.00 system table *guaranteed* to have that "query_variable_info" function? The above adds the version check, but removes the check for a NULL pointer.

As far as the spec's concerned, yes. As far as reality's concerned - if anything doesn't provide it, we're already crashing when efi_virt_query_variable_info() gets called. Nobody's complained so far.

I'm worried about the fragility of this code - this is firmware code ...

I think firmware code should be fundamentally paranoid and robust, and in this case treat all EFI-provided data as hostile and do a much sanity checking of it as possible - and provide an actionable error message if the checks fail, not just 'crash'.

Even if no-one complained, yet.

Thanks,

Ingo

#12 lore raw
Quoted 8 lines · Ingo Molnar

I'm worried about the fragility of this code - this is firmware code ...

I think firmware code should be fundamentally paranoid and robust, and in this case treat all EFI-provided data as hostile and do a much sanity checking of it as possible - and provide an actionable error message if the checks fail, not just 'crash'.

Even if no-one complained, yet.

I'm not sure how much more robust checking for NULL makes it, it's not going to save us from garbage pointers, etc. Instead of the pointer being NULL a more likely bug is that query_variable_info() exists (for those firmware that implement the relevant spec version), but doesn't work correctly. That's the kind of bug we've been seeing in other functions.

To be fair to Josh, his original submission did include the NULL check, but I asked him to remove it. If someone really wants to re-add the check for a NULL pointer, I'm not super opposed to it.