Skip to content

Commit 38e2ecc

Browse files
nefigtutcasparant
authored andcommitted
efi: Fix a race and a buffer overflow while reading efivars via sysfs
fix #30009260 commit 286d325 upstream 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 <bob.sanders@hpe.com> and the LTP testsuite Signed-off-by: Vladis Dronov <vdronov@redhat.com> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Cc: <stable@vger.kernel.org> Link: https://lore.kernel.org/r/20200305084041.24053-2-vdronov@redhat.com Link: https://lore.kernel.org/r/20200308080859.21568-24-ardb@kernel.org Signed-off-by: Chunmei Xu <xuchunmei@linux.alibaba.com> Acked-by: Xunlei Pang <xlpang@linux.alibaba.com>
1 parent 46c728a commit 38e2ecc

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
@@ -139,13 +139,16 @@ static ssize_t
139139
efivar_attr_read(struct efivar_entry *entry, char *buf)
140140
{
141141
struct efi_variable *var = &entry->var;
142+
unsigned long size = sizeof(var->Data);
142143
char *str = buf;
144+
int ret;
143145

144146
if (!entry || !buf)
145147
return -EINVAL;
146148

147-
var->DataSize = 1024;
148-
if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
149+
ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
150+
var->DataSize = size;
151+
if (ret)
149152
return -EIO;
150153

151154
if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
@@ -172,13 +175,16 @@ static ssize_t
172175
efivar_size_read(struct efivar_entry *entry, char *buf)
173176
{
174177
struct efi_variable *var = &entry->var;
178+
unsigned long size = sizeof(var->Data);
175179
char *str = buf;
180+
int ret;
176181

177182
if (!entry || !buf)
178183
return -EINVAL;
179184

180-
var->DataSize = 1024;
181-
if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
185+
ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
186+
var->DataSize = size;
187+
if (ret)
182188
return -EIO;
183189

184190
str += sprintf(str, "0x%lx\n", var->DataSize);
@@ -189,12 +195,15 @@ static ssize_t
189195
efivar_data_read(struct efivar_entry *entry, char *buf)
190196
{
191197
struct efi_variable *var = &entry->var;
198+
unsigned long size = sizeof(var->Data);
199+
int ret;
192200

193201
if (!entry || !buf)
194202
return -EINVAL;
195203

196-
var->DataSize = 1024;
197-
if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
204+
ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
205+
var->DataSize = size;
206+
if (ret)
198207
return -EIO;
199208

200209
memcpy(buf, var->Data, var->DataSize);
@@ -314,14 +323,16 @@ efivar_show_raw(struct efivar_entry *entry, char *buf)
314323
{
315324
struct efi_variable *var = &entry->var;
316325
struct compat_efi_variable *compat;
326+
unsigned long datasize = sizeof(var->Data);
317327
size_t size;
328+
int ret;
318329

319330
if (!entry || !buf)
320331
return 0;
321332

322-
var->DataSize = 1024;
323-
if (efivar_entry_get(entry, &entry->var.Attributes,
324-
&entry->var.DataSize, entry->var.Data))
333+
ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data);
334+
var->DataSize = datasize;
335+
if (ret)
325336
return -EIO;
326337

327338
if (is_compat()) {

0 commit comments

Comments
 (0)