Skip to content

Commit 286d325

Browse files
nefigtutIngo Molnar
authored andcommitted
efi: Fix a race and a buffer overflow while reading efivars via sysfs
There is a race and a buffer overflow corrupting a kernel memory while reading an EFI variable with a size more than 1024 bytes via the older sysfs method. This happens because accessing struct efi_variable in efivar_{attr,size,data}_read() and friends is not protected from a concurrent access leading to a kernel memory corruption and, at best, to a crash. The race scenario is the following: CPU0: CPU1: efivar_attr_read() var->DataSize = 1024; efivar_entry_get(... &var->DataSize) down_interruptible(&efivars_lock) efivar_attr_read() // same EFI var var->DataSize = 1024; efivar_entry_get(... &var->DataSize) down_interruptible(&efivars_lock) virt_efi_get_variable() // returns EFI_BUFFER_TOO_SMALL but // var->DataSize is set to a real // var size more than 1024 bytes up(&efivars_lock) virt_efi_get_variable() // called with var->DataSize set // to a real var size, returns // successfully and overwrites // a 1024-bytes kernel buffer up(&efivars_lock) This can be reproduced by concurrent reading of an EFI variable which size is more than 1024 bytes: ts# for cpu in $(seq 0 $(nproc --ignore=1)); do ( taskset -c $cpu \ cat /sys/firmware/efi/vars/KEKDefault*/size & ) ; done Fix this by using a local variable for a var's data buffer size so it does not get overwritten. Fixes: e14ab23 ("efivars: efivar_entry API") Reported-by: Bob Sanders <[email protected]> and the LTP testsuite Signed-off-by: Vladis Dronov <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent 61a0925 commit 286d325

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

drivers/firmware/efi/efivars.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ static ssize_t
8383
efivar_attr_read(struct efivar_entry *entry, char *buf)
8484
{
8585
struct efi_variable *var = &entry->var;
86+
unsigned long size = sizeof(var->Data);
8687
char *str = buf;
88+
int ret;
8789

8890
if (!entry || !buf)
8991
return -EINVAL;
9092

91-
var->DataSize = 1024;
92-
if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
93+
ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
94+
var->DataSize = size;
95+
if (ret)
9396
return -EIO;
9497

9598
if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
@@ -116,13 +119,16 @@ static ssize_t
116119
efivar_size_read(struct efivar_entry *entry, char *buf)
117120
{
118121
struct efi_variable *var = &entry->var;
122+
unsigned long size = sizeof(var->Data);
119123
char *str = buf;
124+
int ret;
120125

121126
if (!entry || !buf)
122127
return -EINVAL;
123128

124-
var->DataSize = 1024;
125-
if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
129+
ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
130+
var->DataSize = size;
131+
if (ret)
126132
return -EIO;
127133

128134
str += sprintf(str, "0x%lx\n", var->DataSize);
@@ -133,12 +139,15 @@ static ssize_t
133139
efivar_data_read(struct efivar_entry *entry, char *buf)
134140
{
135141
struct efi_variable *var = &entry->var;
142+
unsigned long size = sizeof(var->Data);
143+
int ret;
136144

137145
if (!entry || !buf)
138146
return -EINVAL;
139147

140-
var->DataSize = 1024;
141-
if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
148+
ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
149+
var->DataSize = size;
150+
if (ret)
142151
return -EIO;
143152

144153
memcpy(buf, var->Data, var->DataSize);
@@ -250,14 +259,16 @@ efivar_show_raw(struct efivar_entry *entry, char *buf)
250259
{
251260
struct efi_variable *var = &entry->var;
252261
struct compat_efi_variable *compat;
262+
unsigned long datasize = sizeof(var->Data);
253263
size_t size;
264+
int ret;
254265

255266
if (!entry || !buf)
256267
return 0;
257268

258-
var->DataSize = 1024;
259-
if (efivar_entry_get(entry, &entry->var.Attributes,
260-
&entry->var.DataSize, entry->var.Data))
269+
ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data);
270+
var->DataSize = datasize;
271+
if (ret)
261272
return -EIO;
262273

263274
if (in_compat_syscall()) {

0 commit comments

Comments
 (0)