efi_loader: Avoid underflow when calculating remaining var store size
authorAlper Nebi Yasak <alpernebiyasak@gmail.com>
Sat, 8 Jul 2023 15:23:05 +0000 (18:23 +0300)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Sun, 9 Jul 2023 01:53:08 +0000 (03:53 +0200)
commitd7fe913f23b1907ac6ccb1552151fbcaa89bcdb0
treebddc40eaef7b6c74c5010e4c5632864d69ef60f1
parent0ef6343439c62eb4e18c6ce0e1a3739d0e7f1e2e
efi_loader: Avoid underflow when calculating remaining var store size

The efi_var_mem_free() function calculates the available size for a new
EFI variable by subtracting the occupied buffer size and the overhead
for a new variable from the maximum buffer size set in Kconfig. This
is then returned as QueryVariableInfo()'s RemainingVariableStorageSize
output.

This can underflow as the calculation is done in and processed as
unsigned integer types. Check for underflow before doing the subtraction
and return zero if there's no space.

Fixes: f1f990a8c958 ("efi_loader: memory buffer for variables")
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
lib/efi_loader/efi_var_mem.c