Commit e887bb41 authored by Josh Boyer's avatar Josh Boyer Committed by Greg Kroah-Hartman

efi: be more paranoid about available space when creating variables

commit 68d92986 upstream.

UEFI variables are typically stored in flash. For various reasons, avaiable
space is typically not reclaimed immediately upon the deletion of a
variable - instead, the system will garbage collect during initialisation
after a reboot.

Some systems appear to handle this garbage collection extremely poorly,
failing if more than 50% of the system flash is in use. This can result in
the machine refusing to boot. The safest thing to do for the moment is to
forbid writes if they'd end up using more than half of the storage space.
We can make this more finegrained later if we come up with a method for
identifying the broken machines.
Signed-off-by: default avatarMatthew Garrett <matthew.garrett@nebula.com>
Signed-off-by: default avatarMatt Fleming <matt.fleming@intel.com>
[bwh: Backported to 3.2:
 - Drop efivarfs changes and unused check_var_size()
 - Add error codes to include/linux/efi.h, added upstream by
   commit 5d9db883 ('efi: Add support for a UEFI variable filesystem')
 - Add efi_status_to_err(), added upstream by commit 7253eaba
   ('efivarfs: Return an error if we fail to read a variable')]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
Cc: Rui Xiang <rui.xiang@huawei.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent ff534f03
...@@ -409,6 +409,30 @@ get_var_data(struct efivars *efivars, struct efi_variable *var) ...@@ -409,6 +409,30 @@ get_var_data(struct efivars *efivars, struct efi_variable *var)
return status; return status;
} }
static efi_status_t
check_var_size_locked(struct efivars *efivars, u32 attributes,
unsigned long size)
{
u64 storage_size, remaining_size, max_size;
efi_status_t status;
const struct efivar_operations *fops = efivars->ops;
if (!efivars->ops->query_variable_info)
return EFI_UNSUPPORTED;
status = fops->query_variable_info(attributes, &storage_size,
&remaining_size, &max_size);
if (status != EFI_SUCCESS)
return status;
if (!storage_size || size > remaining_size || size > max_size ||
(remaining_size - size) < (storage_size / 2))
return EFI_OUT_OF_RESOURCES;
return status;
}
static ssize_t static ssize_t
efivar_guid_read(struct efivar_entry *entry, char *buf) efivar_guid_read(struct efivar_entry *entry, char *buf)
{ {
...@@ -530,6 +554,11 @@ efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) ...@@ -530,6 +554,11 @@ efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
} }
spin_lock_irq(&efivars->lock); spin_lock_irq(&efivars->lock);
status = check_var_size_locked(efivars, new_var->Attributes,
new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
status = efivars->ops->set_variable(new_var->VariableName, status = efivars->ops->set_variable(new_var->VariableName,
&new_var->VendorGuid, &new_var->VendorGuid,
new_var->Attributes, new_var->Attributes,
...@@ -641,6 +670,36 @@ efivar_unregister(struct efivar_entry *var) ...@@ -641,6 +670,36 @@ efivar_unregister(struct efivar_entry *var)
kobject_put(&var->kobj); kobject_put(&var->kobj);
} }
static int efi_status_to_err(efi_status_t status)
{
int err;
switch (status) {
case EFI_INVALID_PARAMETER:
err = -EINVAL;
break;
case EFI_OUT_OF_RESOURCES:
err = -ENOSPC;
break;
case EFI_DEVICE_ERROR:
err = -EIO;
break;
case EFI_WRITE_PROTECTED:
err = -EROFS;
break;
case EFI_SECURITY_VIOLATION:
err = -EACCES;
break;
case EFI_NOT_FOUND:
err = -ENOENT;
break;
default:
err = -EINVAL;
}
return err;
}
#ifdef CONFIG_PSTORE #ifdef CONFIG_PSTORE
static int efi_pstore_open(struct pstore_info *psi) static int efi_pstore_open(struct pstore_info *psi)
...@@ -711,7 +770,6 @@ static int efi_pstore_write(enum pstore_type_id type, ...@@ -711,7 +770,6 @@ static int efi_pstore_write(enum pstore_type_id type,
struct efivars *efivars = psi->data; struct efivars *efivars = psi->data;
struct efivar_entry *entry, *found = NULL; struct efivar_entry *entry, *found = NULL;
int i, ret = 0; int i, ret = 0;
u64 storage_space, remaining_space, max_variable_size;
efi_status_t status = EFI_NOT_FOUND; efi_status_t status = EFI_NOT_FOUND;
unsigned long flags; unsigned long flags;
...@@ -725,11 +783,11 @@ static int efi_pstore_write(enum pstore_type_id type, ...@@ -725,11 +783,11 @@ static int efi_pstore_write(enum pstore_type_id type,
* size: a size of logging data * size: a size of logging data
* DUMP_NAME_LEN * 2: a maximum size of variable name * DUMP_NAME_LEN * 2: a maximum size of variable name
*/ */
status = efivars->ops->query_variable_info(PSTORE_EFI_ATTRIBUTES,
&storage_space, status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
&remaining_space, size + DUMP_NAME_LEN * 2);
&max_variable_size);
if (status || remaining_space < size + DUMP_NAME_LEN * 2) { if (status) {
spin_unlock_irqrestore(&efivars->lock, flags); spin_unlock_irqrestore(&efivars->lock, flags);
*id = part; *id = part;
return -ENOSPC; return -ENOSPC;
...@@ -877,6 +935,14 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj, ...@@ -877,6 +935,14 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
return -EINVAL; return -EINVAL;
} }
status = check_var_size_locked(efivars, new_var->Attributes,
new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
if (status && status != EFI_UNSUPPORTED) {
spin_unlock_irq(&efivars->lock);
return efi_status_to_err(status);
}
/* now *really* create the variable via EFI */ /* now *really* create the variable via EFI */
status = efivars->ops->set_variable(new_var->VariableName, status = efivars->ops->set_variable(new_var->VariableName,
&new_var->VendorGuid, &new_var->VendorGuid,
......
...@@ -29,7 +29,12 @@ ...@@ -29,7 +29,12 @@
#define EFI_UNSUPPORTED ( 3 | (1UL << (BITS_PER_LONG-1))) #define EFI_UNSUPPORTED ( 3 | (1UL << (BITS_PER_LONG-1)))
#define EFI_BAD_BUFFER_SIZE ( 4 | (1UL << (BITS_PER_LONG-1))) #define EFI_BAD_BUFFER_SIZE ( 4 | (1UL << (BITS_PER_LONG-1)))
#define EFI_BUFFER_TOO_SMALL ( 5 | (1UL << (BITS_PER_LONG-1))) #define EFI_BUFFER_TOO_SMALL ( 5 | (1UL << (BITS_PER_LONG-1)))
#define EFI_NOT_READY ( 6 | (1UL << (BITS_PER_LONG-1)))
#define EFI_DEVICE_ERROR ( 7 | (1UL << (BITS_PER_LONG-1)))
#define EFI_WRITE_PROTECTED ( 8 | (1UL << (BITS_PER_LONG-1)))
#define EFI_OUT_OF_RESOURCES ( 9 | (1UL << (BITS_PER_LONG-1)))
#define EFI_NOT_FOUND (14 | (1UL << (BITS_PER_LONG-1))) #define EFI_NOT_FOUND (14 | (1UL << (BITS_PER_LONG-1)))
#define EFI_SECURITY_VIOLATION (26 | (1UL << (BITS_PER_LONG-1)))
typedef unsigned long efi_status_t; typedef unsigned long efi_status_t;
typedef u8 efi_bool_t; typedef u8 efi_bool_t;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment