efi: fix out-of-bounds null overwrite vulnerability
PATCH 1/1 efi: fix out-of-bounds null overwrite vulnerability
snprintf's return value is not bound by size value.
(https://www.kernel.org/doc/htmldocs/kernel-api/API-snprintf.html)
if printed value is larger than buffer size, it can overwrite null byte in out-of-bounds buffer.
--- drivers/firmware/efi/cper.c | 1 - 1 file changed, 1 deletion(-)
drivers/firmware/efi/cper.c +0 -1
@@ -267,7 +267,6 @@ static int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg)"DIMM location: not present. DMI handle: 0x%.4x " , mem->mem_dev_handle); - msg[n] ='\0' ;return n; }
Signature · 1 line
1.9.1
Quoted 4 lines · Insu Yun
snprintf's return value is not bound by size value.
(https://www.kernel.org/doc/htmldocs/kernel-api/API-snprintf.html)
if printed value is larger than buffer size, it can overwrite null byte in out-of-bounds buffer.
But this function doesn't use snprintf(), it uses scnprintf() which returns the number of characters written into buf and, because scnprintf() largely follows vnsprintf(), it will never write more than 'size' bytes into the buffer.
Quoted 10 lines
Signed-off-by Insu Yun <[email protected]>--- drivers/firmware/efi/cper.c | 1 - 1 file changed, 1 deletion(-)drivers/firmware/efi/cper.c +0
-0
drivers/firmware/efi/cper.c static int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg) · line 272
Quoted 7 lines
drivers/firmware/efi/cper.c +0
-1@@ -267,7 +267,6 @@ static int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg)"DIMM location: not present. DMI handle: 0x%.4x " , mem->mem_dev_handle); - msg[n] ='\0' ;return n; }
Calling this a vulnerability is a little extreme. These fields come from firmware and if you can't trust the firmware you've got bigger issues.
I'm not even sure this is a bug. Tony?
Luck, Tony
reviewerQuoted 4 lines
But this function doesn't use
snprintf(), it usesscnprintf()which returns the number of characters written into buf and, becausescnprintf()largely followsvnsprintf(), it will never write more than 'size' bytes into the buffer.
if (bank && device)
n = snprintf(msg, len, "DIMM location: %s %s ", bank, device);That looks like "snprintf", not "scnprintf" to me :-)
What about using:
msg[len] = '\0';
to guarantee NUL termination?
-Tony
Quoted 9 lines · Tony
Quoted 4 lines
But this function doesn't use
snprintf(), it usesscnprintf()which returns the number of characters written into buf and, becausescnprintf()largely followsvnsprintf(), it will never write more than 'size' bytes into the buffer.if (bank && device) n = snprintf(msg, len, "DIMM location: %s %s ", bank, device);That looks like "snprintf", not "scnprintf" to me :-)
Oops! Can you believe I looked at the wrong function?
Quoted 5 lines
What about using:
msg[len] = '\0';to guarantee NUL termination?
But that may leave garbage bytes in 'rcd_decode_str' in the case where the string isn't as long as 'len'.
How about memset()'ing the buffer to zero and deleting the NUL termination line?
Luck, Tony
reviewerQuoted 11 lines
Quoted 5 lines
What about using:
msg[len] = '\0';to guarantee NUL termination?
But that may leave garbage bytes in '
rcd_decode_str' in the case where the string isn't as long as 'len'.How about memset()'ing the buffer to zero and deleting the NUL termination line?
Looks like overkill to me. We only use this function in two places:
if (cper_dimm_err_location(cmem, rcd_decode_str))
trace_seq_printf(p, "%s", rcd_decode_str);
if (cper_dimm_err_location(&cmem, rcd_decode_str))
printk("%s%s\n", pfx, rcd_decode_str);
Neither would care if there were garbage after the NUL and before the end of the rcd_decode_str[] array.
This buffer isn't visible to user space, so we aren't leaking data by having garbage bytes after the NUL.
-Tony
Quoted 17 lines · Tony
Quoted 11 lines
Quoted 5 lines
What about using:
msg[len] = '\0';to guarantee NUL termination?
But that may leave garbage bytes in '
rcd_decode_str' in the case where the string isn't as long as 'len'.How about memset()'ing the buffer to zero and deleting the NUL termination line?
Looks like overkill to me. We only use this function in two places:
if (
cper_dimm_err_location(cmem, rcd_decode_str))
trace_seq_printf(p, "%s", rcd_decode_str);
Quoted 8 lines
if (
cper_dimm_err_location(&cmem, rcd_decode_str))
printk("%s%s\n", pfx, rcd_decode_str);Neither would care if there were garbage after the NUL and before the end of the rcd_decode_str[] array.
This buffer isn't visible to user space, so we aren't leaking data by having garbage bytes after the NUL.
What about *before* the NUL? That was the point I was trying to make. If the string you print into the buffer isn't 'len' bytes in size the buffer will look like,
"DIMM location: Foo bar"<garbage.....>\0
Doing the memset() before the call to snprintf() guarantees this won't happen and means you don't have to try and calculate where the NUL needs to be placed.
Quoted 5 lines · Matt Fleming
What about *before* the NUL? That was the point I was trying to make. If the string you print into the buffer isn't 'len' bytes in size the buffer will look like,
"DIMM location: Foo bar<garbage.....>\0"
Doesn't that actually look like:
"DIMM location: Foo bar\0<garbage.....>\0"
snprintf() will NUL terminate what it writes into the buffer if the buffer is big enough for what you were trying to write there. The problem happens when what you want to write is too big. Then you have
"DIMM location: ReallyLongFoo AlsoBar"
at which point "msg[len] = '\0'; will zap that 'r' at the end to make sure printk() will stop at the end of the valid part of buffer.
-Tony