1 From f94593e62066f3ea5cc6f1835f82da910a508f32 Mon Sep 17 00:00:00 2001
2 From: Arend van Spriel <arend.vanspriel@broadcom.com>
3 Date: Thu, 29 Nov 2018 18:12:27 +0100
4 Subject: firmware/efi: Add NULL pointer checks in efivars API functions
6 [ Upstream commit ab2180a15ce54739fed381efb4cb12e78dfb1561 ]
10 ce2e6db554fa ("brcmfmac: Add support for getting nvram contents from EFI variables")
12 we have a device driver accessing the efivars API. Several functions in
13 the efivars API assume __efivars is set, i.e., that they will be accessed
14 only after efivars_register() has been called. However, the following NULL
15 pointer access was reported calling efivar_entry_size() from the brcmfmac
18 Unable to handle kernel NULL pointer dereference at virtual address 00000008
20 [00000008] *pgd=00000000
21 Internal error: Oops: 5 [#1] SMP ARM
23 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
24 Workqueue: events request_firmware_work_func
25 PC is at efivar_entry_size+0x28/0x90
26 LR is at brcmf_fw_complete_request+0x3f8/0x8d4 [brcmfmac]
27 pc : [<c0c40718>] lr : [<bf2a3ef4>] psr: a00d0113
28 sp : ede7fe28 ip : ee983410 fp : c1787f30
29 r10: 00000000 r9 : 00000000 r8 : bf2b2258
30 r7 : ee983000 r6 : c1604c48 r5 : ede7fe88 r4 : edf337c0
31 r3 : 00000000 r2 : 00000000 r1 : ede7fe88 r0 : c17712c8
32 Flags: NzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none
33 Control: 10c5387d Table: ad16804a DAC: 00000051
35 Disassembly showed that the local static variable __efivars is NULL,
36 which is not entirely unexpected given that it is a non-EFI platform.
38 So add a NULL pointer check to efivar_entry_size(), and to related
39 functions while at it. In efivars_register() a couple of sanity checks
42 Reported-by: Jon Hunter <jonathanh@nvidia.com>
43 Signed-off-by: Arend van Spriel <arend.vanspriel@broadcom.com>
44 Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
45 Cc: Andy Lutomirski <luto@kernel.org>
46 Cc: Bhupesh Sharma <bhsharma@redhat.com>
47 Cc: Borislav Petkov <bp@alien8.de>
48 Cc: Dave Hansen <dave.hansen@intel.com>
49 Cc: Eric Snowberg <eric.snowberg@oracle.com>
50 Cc: Hans de Goede <hdegoede@redhat.com>
51 Cc: Joe Perches <joe@perches.com>
52 Cc: Julien Thierry <julien.thierry@arm.com>
53 Cc: Linus Torvalds <torvalds@linux-foundation.org>
54 Cc: Marc Zyngier <marc.zyngier@arm.com>
55 Cc: Matt Fleming <matt@codeblueprint.co.uk>
56 Cc: Nathan Chancellor <natechancellor@gmail.com>
57 Cc: Peter Zijlstra <peterz@infradead.org>
58 Cc: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
59 Cc: Sedat Dilek <sedat.dilek@gmail.com>
60 Cc: Thomas Gleixner <tglx@linutronix.de>
61 Cc: YiFei Zhu <zhuyifei1999@gmail.com>
62 Cc: linux-efi@vger.kernel.org
63 Link: http://lkml.kernel.org/r/20181129171230.18699-9-ard.biesheuvel@linaro.org
64 Signed-off-by: Ingo Molnar <mingo@kernel.org>
65 Signed-off-by: Sasha Levin <sashal@kernel.org>
67 drivers/firmware/efi/vars.c | 99 +++++++++++++++++++++++++++++--------
68 1 file changed, 78 insertions(+), 21 deletions(-)
70 diff --git a/drivers/firmware/efi/vars.c b/drivers/firmware/efi/vars.c
71 index 9336ffdf6e2c..fceaafd67ec6 100644
72 --- a/drivers/firmware/efi/vars.c
73 +++ b/drivers/firmware/efi/vars.c
74 @@ -318,7 +318,12 @@ EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
76 check_var_size(u32 attributes, unsigned long size)
78 - const struct efivar_operations *fops = __efivars->ops;
79 + const struct efivar_operations *fops;
82 + return EFI_UNSUPPORTED;
84 + fops = __efivars->ops;
86 if (!fops->query_variable_store)
87 return EFI_UNSUPPORTED;
88 @@ -329,7 +334,12 @@ check_var_size(u32 attributes, unsigned long size)
90 check_var_size_nonblocking(u32 attributes, unsigned long size)
92 - const struct efivar_operations *fops = __efivars->ops;
93 + const struct efivar_operations *fops;
96 + return EFI_UNSUPPORTED;
98 + fops = __efivars->ops;
100 if (!fops->query_variable_store)
101 return EFI_UNSUPPORTED;
102 @@ -429,13 +439,18 @@ static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
103 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
104 void *data, bool duplicates, struct list_head *head)
106 - const struct efivar_operations *ops = __efivars->ops;
107 + const struct efivar_operations *ops;
108 unsigned long variable_name_size = 1024;
109 efi_char16_t *variable_name;
111 efi_guid_t vendor_guid;
117 + ops = __efivars->ops;
119 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
120 if (!variable_name) {
121 printk(KERN_ERR "efivars: Memory allocation failed.\n");
122 @@ -583,12 +598,14 @@ static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
124 int __efivar_entry_delete(struct efivar_entry *entry)
126 - const struct efivar_operations *ops = __efivars->ops;
129 - status = ops->set_variable(entry->var.VariableName,
130 - &entry->var.VendorGuid,
135 + status = __efivars->ops->set_variable(entry->var.VariableName,
136 + &entry->var.VendorGuid,
139 return efi_status_to_err(status);
141 @@ -607,12 +624,17 @@ EXPORT_SYMBOL_GPL(__efivar_entry_delete);
143 int efivar_entry_delete(struct efivar_entry *entry)
145 - const struct efivar_operations *ops = __efivars->ops;
146 + const struct efivar_operations *ops;
149 if (down_interruptible(&efivars_lock))
156 + ops = __efivars->ops;
157 status = ops->set_variable(entry->var.VariableName,
158 &entry->var.VendorGuid,
160 @@ -650,13 +672,19 @@ EXPORT_SYMBOL_GPL(efivar_entry_delete);
161 int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
162 unsigned long size, void *data, struct list_head *head)
164 - const struct efivar_operations *ops = __efivars->ops;
165 + const struct efivar_operations *ops;
167 efi_char16_t *name = entry->var.VariableName;
168 efi_guid_t vendor = entry->var.VendorGuid;
170 if (down_interruptible(&efivars_lock))
177 + ops = __efivars->ops;
178 if (head && efivar_entry_find(name, vendor, head, false)) {
181 @@ -687,12 +715,17 @@ static int
182 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
183 u32 attributes, unsigned long size, void *data)
185 - const struct efivar_operations *ops = __efivars->ops;
186 + const struct efivar_operations *ops;
189 if (down_trylock(&efivars_lock))
197 status = check_var_size_nonblocking(attributes,
198 size + ucs2_strsize(name, 1024));
199 if (status != EFI_SUCCESS) {
200 @@ -700,6 +733,7 @@ efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
204 + ops = __efivars->ops;
205 status = ops->set_variable_nonblocking(name, &vendor, attributes,
208 @@ -727,9 +761,13 @@ efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
209 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
210 bool block, unsigned long size, void *data)
212 - const struct efivar_operations *ops = __efivars->ops;
213 + const struct efivar_operations *ops;
219 + ops = __efivars->ops;
220 if (!ops->query_variable_store)
223 @@ -829,13 +867,18 @@ EXPORT_SYMBOL_GPL(efivar_entry_find);
225 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
227 - const struct efivar_operations *ops = __efivars->ops;
228 + const struct efivar_operations *ops;
233 if (down_interruptible(&efivars_lock))
239 + ops = __efivars->ops;
240 status = ops->get_variable(entry->var.VariableName,
241 &entry->var.VendorGuid, NULL, size, NULL);
243 @@ -861,12 +904,14 @@ EXPORT_SYMBOL_GPL(efivar_entry_size);
244 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
245 unsigned long *size, void *data)
247 - const struct efivar_operations *ops = __efivars->ops;
250 - status = ops->get_variable(entry->var.VariableName,
251 - &entry->var.VendorGuid,
252 - attributes, size, data);
256 + status = __efivars->ops->get_variable(entry->var.VariableName,
257 + &entry->var.VendorGuid,
258 + attributes, size, data);
260 return efi_status_to_err(status);
262 @@ -882,14 +927,19 @@ EXPORT_SYMBOL_GPL(__efivar_entry_get);
263 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
264 unsigned long *size, void *data)
266 - const struct efivar_operations *ops = __efivars->ops;
269 if (down_interruptible(&efivars_lock))
271 - status = ops->get_variable(entry->var.VariableName,
272 - &entry->var.VendorGuid,
273 - attributes, size, data);
280 + status = __efivars->ops->get_variable(entry->var.VariableName,
281 + &entry->var.VendorGuid,
282 + attributes, size, data);
285 return efi_status_to_err(status);
286 @@ -921,7 +971,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_get);
287 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
288 unsigned long *size, void *data, bool *set)
290 - const struct efivar_operations *ops = __efivars->ops;
291 + const struct efivar_operations *ops;
292 efi_char16_t *name = entry->var.VariableName;
293 efi_guid_t *vendor = &entry->var.VendorGuid;
295 @@ -940,6 +990,11 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
296 if (down_interruptible(&efivars_lock))
305 * Ensure that the available space hasn't shrunk below the safe level
307 @@ -956,6 +1011,8 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
311 + ops = __efivars->ops;
313 status = ops->set_variable(name, vendor, attributes, *size, data);
314 if (status != EFI_SUCCESS) {
315 err = efi_status_to_err(status);