linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] efi: cper: fix snprintf() use in cper_dimm_err_location()
@ 2021-04-21 19:46 Rasmus Villemoes
  2021-05-10 15:39 ` Ard Biesheuvel
  0 siblings, 1 reply; 2+ messages in thread
From: Rasmus Villemoes @ 2021-04-21 19:46 UTC (permalink / raw)
  To: Ard Biesheuvel; +Cc: Alex Kluver, Rasmus Villemoes, linux-efi, linux-kernel

snprintf() should be given the full buffer size, not one less. And it
guarantees nul-termination, so doing it manually afterwards is
pointless.

It's even potentially harmful (though probably not in practice because
CPER_REC_LEN is 256), due to the "return how much would have been
written had the buffer been big enough" semantics. I.e., if the bank
and/or device strings are long enough that the "DIMM location ..."
output gets truncated, writing to msg[n] is a buffer overflow.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/firmware/efi/cper.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/firmware/efi/cper.c b/drivers/firmware/efi/cper.c
index e15d484b6a5a..ea7ca74fc173 100644
--- a/drivers/firmware/efi/cper.c
+++ b/drivers/firmware/efi/cper.c
@@ -276,8 +276,7 @@ static int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg)
 	if (!msg || !(mem->validation_bits & CPER_MEM_VALID_MODULE_HANDLE))
 		return 0;
 
-	n = 0;
-	len = CPER_REC_LEN - 1;
+	len = CPER_REC_LEN;
 	dmi_memdev_name(mem->mem_dev_handle, &bank, &device);
 	if (bank && device)
 		n = snprintf(msg, len, "DIMM location: %s %s ", bank, device);
@@ -286,7 +285,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;
 }
 
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] efi: cper: fix snprintf() use in cper_dimm_err_location()
  2021-04-21 19:46 [PATCH] efi: cper: fix snprintf() use in cper_dimm_err_location() Rasmus Villemoes
@ 2021-05-10 15:39 ` Ard Biesheuvel
  0 siblings, 0 replies; 2+ messages in thread
From: Ard Biesheuvel @ 2021-05-10 15:39 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: Alex Kluver, linux-efi, Linux Kernel Mailing List

On Wed, 21 Apr 2021 at 21:46, Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote:
>
> snprintf() should be given the full buffer size, not one less. And it
> guarantees nul-termination, so doing it manually afterwards is
> pointless.
>
> It's even potentially harmful (though probably not in practice because
> CPER_REC_LEN is 256), due to the "return how much would have been
> written had the buffer been big enough" semantics. I.e., if the bank
> and/or device strings are long enough that the "DIMM location ..."
> output gets truncated, writing to msg[n] is a buffer overflow.
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  drivers/firmware/efi/cper.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/drivers/firmware/efi/cper.c b/drivers/firmware/efi/cper.c
> index e15d484b6a5a..ea7ca74fc173 100644
> --- a/drivers/firmware/efi/cper.c
> +++ b/drivers/firmware/efi/cper.c
> @@ -276,8 +276,7 @@ static int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg)
>         if (!msg || !(mem->validation_bits & CPER_MEM_VALID_MODULE_HANDLE))
>                 return 0;
>
> -       n = 0;
> -       len = CPER_REC_LEN - 1;
> +       len = CPER_REC_LEN;
>         dmi_memdev_name(mem->mem_dev_handle, &bank, &device);
>         if (bank && device)
>                 n = snprintf(msg, len, "DIMM location: %s %s ", bank, device);
> @@ -286,7 +285,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;
>  }
>
> --
> 2.29.2
>

Thanks, I will queue this up.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-10 15:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-21 19:46 [PATCH] efi: cper: fix snprintf() use in cper_dimm_err_location() Rasmus Villemoes
2021-05-10 15:39 ` Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).