diff options
Diffstat (limited to 'drivers/firmware/efi/efivars.c')
-rw-r--r-- | drivers/firmware/efi/efivars.c | 192 |
1 files changed, 162 insertions, 30 deletions
diff --git a/drivers/firmware/efi/efivars.c b/drivers/firmware/efi/efivars.c index 50ea412a25e6..463c56545ae8 100644 --- a/drivers/firmware/efi/efivars.c +++ b/drivers/firmware/efi/efivars.c | |||
@@ -69,6 +69,7 @@ | |||
69 | #include <linux/module.h> | 69 | #include <linux/module.h> |
70 | #include <linux/slab.h> | 70 | #include <linux/slab.h> |
71 | #include <linux/ucs2_string.h> | 71 | #include <linux/ucs2_string.h> |
72 | #include <linux/compat.h> | ||
72 | 73 | ||
73 | #define EFIVARS_VERSION "0.08" | 74 | #define EFIVARS_VERSION "0.08" |
74 | #define EFIVARS_DATE "2004-May-17" | 75 | #define EFIVARS_DATE "2004-May-17" |
@@ -86,6 +87,15 @@ static struct kset *efivars_kset; | |||
86 | static struct bin_attribute *efivars_new_var; | 87 | static struct bin_attribute *efivars_new_var; |
87 | static struct bin_attribute *efivars_del_var; | 88 | static struct bin_attribute *efivars_del_var; |
88 | 89 | ||
90 | struct compat_efi_variable { | ||
91 | efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)]; | ||
92 | efi_guid_t VendorGuid; | ||
93 | __u32 DataSize; | ||
94 | __u8 Data[1024]; | ||
95 | __u32 Status; | ||
96 | __u32 Attributes; | ||
97 | } __packed; | ||
98 | |||
89 | struct efivar_attribute { | 99 | struct efivar_attribute { |
90 | struct attribute attr; | 100 | struct attribute attr; |
91 | ssize_t (*show) (struct efivar_entry *entry, char *buf); | 101 | ssize_t (*show) (struct efivar_entry *entry, char *buf); |
@@ -189,45 +199,107 @@ efivar_data_read(struct efivar_entry *entry, char *buf) | |||
189 | memcpy(buf, var->Data, var->DataSize); | 199 | memcpy(buf, var->Data, var->DataSize); |
190 | return var->DataSize; | 200 | return var->DataSize; |
191 | } | 201 | } |
192 | /* | ||
193 | * We allow each variable to be edited via rewriting the | ||
194 | * entire efi variable structure. | ||
195 | */ | ||
196 | static ssize_t | ||
197 | efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) | ||
198 | { | ||
199 | struct efi_variable *new_var, *var = &entry->var; | ||
200 | int err; | ||
201 | 202 | ||
202 | if (count != sizeof(struct efi_variable)) | 203 | static inline int |
203 | return -EINVAL; | 204 | sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor, |
204 | 205 | unsigned long size, u32 attributes, u8 *data) | |
205 | new_var = (struct efi_variable *)buf; | 206 | { |
206 | /* | 207 | /* |
207 | * If only updating the variable data, then the name | 208 | * If only updating the variable data, then the name |
208 | * and guid should remain the same | 209 | * and guid should remain the same |
209 | */ | 210 | */ |
210 | if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) || | 211 | if (memcmp(name, var->VariableName, sizeof(var->VariableName)) || |
211 | efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) { | 212 | efi_guidcmp(vendor, var->VendorGuid)) { |
212 | printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n"); | 213 | printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n"); |
213 | return -EINVAL; | 214 | return -EINVAL; |
214 | } | 215 | } |
215 | 216 | ||
216 | if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){ | 217 | if ((size <= 0) || (attributes == 0)){ |
217 | printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n"); | 218 | printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n"); |
218 | return -EINVAL; | 219 | return -EINVAL; |
219 | } | 220 | } |
220 | 221 | ||
221 | if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 || | 222 | if ((attributes & ~EFI_VARIABLE_MASK) != 0 || |
222 | efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) { | 223 | efivar_validate(name, data, size) == false) { |
223 | printk(KERN_ERR "efivars: Malformed variable content\n"); | 224 | printk(KERN_ERR "efivars: Malformed variable content\n"); |
224 | return -EINVAL; | 225 | return -EINVAL; |
225 | } | 226 | } |
226 | 227 | ||
227 | memcpy(&entry->var, new_var, count); | 228 | return 0; |
229 | } | ||
230 | |||
231 | static inline bool is_compat(void) | ||
232 | { | ||
233 | if (IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) | ||
234 | return true; | ||
235 | |||
236 | return false; | ||
237 | } | ||
238 | |||
239 | static void | ||
240 | copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src) | ||
241 | { | ||
242 | memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN); | ||
243 | memcpy(dst->Data, src->Data, sizeof(src->Data)); | ||
244 | |||
245 | dst->VendorGuid = src->VendorGuid; | ||
246 | dst->DataSize = src->DataSize; | ||
247 | dst->Attributes = src->Attributes; | ||
248 | } | ||
249 | |||
250 | /* | ||
251 | * We allow each variable to be edited via rewriting the | ||
252 | * entire efi variable structure. | ||
253 | */ | ||
254 | static ssize_t | ||
255 | efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) | ||
256 | { | ||
257 | struct efi_variable *new_var, *var = &entry->var; | ||
258 | efi_char16_t *name; | ||
259 | unsigned long size; | ||
260 | efi_guid_t vendor; | ||
261 | u32 attributes; | ||
262 | u8 *data; | ||
263 | int err; | ||
264 | |||
265 | if (is_compat()) { | ||
266 | struct compat_efi_variable *compat; | ||
267 | |||
268 | if (count != sizeof(*compat)) | ||
269 | return -EINVAL; | ||
270 | |||
271 | compat = (struct compat_efi_variable *)buf; | ||
272 | attributes = compat->Attributes; | ||
273 | vendor = compat->VendorGuid; | ||
274 | name = compat->VariableName; | ||
275 | size = compat->DataSize; | ||
276 | data = compat->Data; | ||
277 | |||
278 | err = sanity_check(var, name, vendor, size, attributes, data); | ||
279 | if (err) | ||
280 | return err; | ||
281 | |||
282 | copy_out_compat(&entry->var, compat); | ||
283 | } else { | ||
284 | if (count != sizeof(struct efi_variable)) | ||
285 | return -EINVAL; | ||
286 | |||
287 | new_var = (struct efi_variable *)buf; | ||
228 | 288 | ||
229 | err = efivar_entry_set(entry, new_var->Attributes, | 289 | attributes = new_var->Attributes; |
230 | new_var->DataSize, new_var->Data, NULL); | 290 | vendor = new_var->VendorGuid; |
291 | name = new_var->VariableName; | ||
292 | size = new_var->DataSize; | ||
293 | data = new_var->Data; | ||
294 | |||
295 | err = sanity_check(var, name, vendor, size, attributes, data); | ||
296 | if (err) | ||
297 | return err; | ||
298 | |||
299 | memcpy(&entry->var, new_var, count); | ||
300 | } | ||
301 | |||
302 | err = efivar_entry_set(entry, attributes, size, data, NULL); | ||
231 | if (err) { | 303 | if (err) { |
232 | printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err); | 304 | printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err); |
233 | return -EIO; | 305 | return -EIO; |
@@ -240,6 +312,8 @@ static ssize_t | |||
240 | efivar_show_raw(struct efivar_entry *entry, char *buf) | 312 | efivar_show_raw(struct efivar_entry *entry, char *buf) |
241 | { | 313 | { |
242 | struct efi_variable *var = &entry->var; | 314 | struct efi_variable *var = &entry->var; |
315 | struct compat_efi_variable *compat; | ||
316 | size_t size; | ||
243 | 317 | ||
244 | if (!entry || !buf) | 318 | if (!entry || !buf) |
245 | return 0; | 319 | return 0; |
@@ -249,9 +323,23 @@ efivar_show_raw(struct efivar_entry *entry, char *buf) | |||
249 | &entry->var.DataSize, entry->var.Data)) | 323 | &entry->var.DataSize, entry->var.Data)) |
250 | return -EIO; | 324 | return -EIO; |
251 | 325 | ||
252 | memcpy(buf, var, sizeof(*var)); | 326 | if (is_compat()) { |
327 | compat = (struct compat_efi_variable *)buf; | ||
328 | |||
329 | size = sizeof(*compat); | ||
330 | memcpy(compat->VariableName, var->VariableName, | ||
331 | EFI_VAR_NAME_LEN); | ||
332 | memcpy(compat->Data, var->Data, sizeof(compat->Data)); | ||
333 | |||
334 | compat->VendorGuid = var->VendorGuid; | ||
335 | compat->DataSize = var->DataSize; | ||
336 | compat->Attributes = var->Attributes; | ||
337 | } else { | ||
338 | size = sizeof(*var); | ||
339 | memcpy(buf, var, size); | ||
340 | } | ||
253 | 341 | ||
254 | return sizeof(*var); | 342 | return size; |
255 | } | 343 | } |
256 | 344 | ||
257 | /* | 345 | /* |
@@ -326,15 +414,39 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj, | |||
326 | struct bin_attribute *bin_attr, | 414 | struct bin_attribute *bin_attr, |
327 | char *buf, loff_t pos, size_t count) | 415 | char *buf, loff_t pos, size_t count) |
328 | { | 416 | { |
417 | struct compat_efi_variable *compat = (struct compat_efi_variable *)buf; | ||
329 | struct efi_variable *new_var = (struct efi_variable *)buf; | 418 | struct efi_variable *new_var = (struct efi_variable *)buf; |
330 | struct efivar_entry *new_entry; | 419 | struct efivar_entry *new_entry; |
420 | bool need_compat = is_compat(); | ||
421 | efi_char16_t *name; | ||
422 | unsigned long size; | ||
423 | u32 attributes; | ||
424 | u8 *data; | ||
331 | int err; | 425 | int err; |
332 | 426 | ||
333 | if (!capable(CAP_SYS_ADMIN)) | 427 | if (!capable(CAP_SYS_ADMIN)) |
334 | return -EACCES; | 428 | return -EACCES; |
335 | 429 | ||
336 | if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 || | 430 | if (need_compat) { |
337 | efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) { | 431 | if (count != sizeof(*compat)) |
432 | return -EINVAL; | ||
433 | |||
434 | attributes = compat->Attributes; | ||
435 | name = compat->VariableName; | ||
436 | size = compat->DataSize; | ||
437 | data = compat->Data; | ||
438 | } else { | ||
439 | if (count != sizeof(*new_var)) | ||
440 | return -EINVAL; | ||
441 | |||
442 | attributes = new_var->Attributes; | ||
443 | name = new_var->VariableName; | ||
444 | size = new_var->DataSize; | ||
445 | data = new_var->Data; | ||
446 | } | ||
447 | |||
448 | if ((attributes & ~EFI_VARIABLE_MASK) != 0 || | ||
449 | efivar_validate(name, data, size) == false) { | ||
338 | printk(KERN_ERR "efivars: Malformed variable content\n"); | 450 | printk(KERN_ERR "efivars: Malformed variable content\n"); |
339 | return -EINVAL; | 451 | return -EINVAL; |
340 | } | 452 | } |
@@ -343,10 +455,13 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj, | |||
343 | if (!new_entry) | 455 | if (!new_entry) |
344 | return -ENOMEM; | 456 | return -ENOMEM; |
345 | 457 | ||
346 | memcpy(&new_entry->var, new_var, sizeof(*new_var)); | 458 | if (need_compat) |
459 | copy_out_compat(&new_entry->var, compat); | ||
460 | else | ||
461 | memcpy(&new_entry->var, new_var, sizeof(*new_var)); | ||
347 | 462 | ||
348 | err = efivar_entry_set(new_entry, new_var->Attributes, new_var->DataSize, | 463 | err = efivar_entry_set(new_entry, attributes, size, |
349 | new_var->Data, &efivar_sysfs_list); | 464 | data, &efivar_sysfs_list); |
350 | if (err) { | 465 | if (err) { |
351 | if (err == -EEXIST) | 466 | if (err == -EEXIST) |
352 | err = -EINVAL; | 467 | err = -EINVAL; |
@@ -369,15 +484,32 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj, | |||
369 | char *buf, loff_t pos, size_t count) | 484 | char *buf, loff_t pos, size_t count) |
370 | { | 485 | { |
371 | struct efi_variable *del_var = (struct efi_variable *)buf; | 486 | struct efi_variable *del_var = (struct efi_variable *)buf; |
487 | struct compat_efi_variable *compat; | ||
372 | struct efivar_entry *entry; | 488 | struct efivar_entry *entry; |
489 | efi_char16_t *name; | ||
490 | efi_guid_t vendor; | ||
373 | int err = 0; | 491 | int err = 0; |
374 | 492 | ||
375 | if (!capable(CAP_SYS_ADMIN)) | 493 | if (!capable(CAP_SYS_ADMIN)) |
376 | return -EACCES; | 494 | return -EACCES; |
377 | 495 | ||
496 | if (is_compat()) { | ||
497 | if (count != sizeof(*compat)) | ||
498 | return -EINVAL; | ||
499 | |||
500 | compat = (struct compat_efi_variable *)buf; | ||
501 | name = compat->VariableName; | ||
502 | vendor = compat->VendorGuid; | ||
503 | } else { | ||
504 | if (count != sizeof(*del_var)) | ||
505 | return -EINVAL; | ||
506 | |||
507 | name = del_var->VariableName; | ||
508 | vendor = del_var->VendorGuid; | ||
509 | } | ||
510 | |||
378 | efivar_entry_iter_begin(); | 511 | efivar_entry_iter_begin(); |
379 | entry = efivar_entry_find(del_var->VariableName, del_var->VendorGuid, | 512 | entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true); |
380 | &efivar_sysfs_list, true); | ||
381 | if (!entry) | 513 | if (!entry) |
382 | err = -EINVAL; | 514 | err = -EINVAL; |
383 | else if (__efivar_entry_delete(entry)) | 515 | else if (__efivar_entry_delete(entry)) |