Skip to contentSkip to conversation

term/serial: Ensure proper NULL termination after grub_strncpy

PATCH In review Glenn Washburn 2 participants 2 messages Jul 14, 2023 – Oct 5, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 term/serial: Ensure proper NULL termination after grub_strncpy

A large enough argument to the --port option could cause a string buffer to be not NULL terminated because grub_strncpy() does not guarantee NULL termination if copied string is longer than max characters to copy.

Fixes 712309eaae04 (term/serial: Use grub_strncpy() instead of grub_snprintf() when only copying string)
---
 grub-core/term/serial.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
grub-core/term/serial.c +4 -1
index 869555430153..8260dcb7a87a 100644
@@ -257,7 +257,10 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
     {
       if (grub_strncmp (state[OPTION_PORT].arg, "mmio,", sizeof ("mmio,") - 1) == 0 ||
 	  grub_strncmp (state[OPTION_PORT].arg, "pci,", sizeof ("pci,") - 1) == 0)
-	grub_strncpy (pname, state[1].arg, sizeof (pname));
+	{
+	  grub_strncpy (pname, state[1].arg, sizeof (pname));
+	  pname[sizeof (pname) - 1] = '\0';
+	}
       else
 	grub_snprintf (pname, sizeof (pname), "port%lx",
 		       grub_strtoul (state[1].arg, 0, 0));
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Glenn Washburn

A large enough argument to the --port option could cause a string buffer to be not NULL terminated because grub_strncpy() does not guarantee NULL termination if copied string is longer than max characters to copy.

Fixes 712309eaae04 (term/serial: Use grub_strncpy() instead of grub_snprintf() when only copying string)

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel