[PATCH] debugedit: Add terminator checks for .debug_str and .debug_line_str

Mark Wielaard mark@klomp.org
Wed Sep 2 12:19:18 GMT 2026


To make sure a string gotten from .debug_str or .debug_line_str is
always terminated we check the section ends in a zero terminator.
Also make sure to check the index fits the section (missing check in
read_dwarf5_line_entries).

	* tools/debugedit.c (edit_dwarf2): Check DEBUG_STR and
	DEBUG_LINE_STR sections end in zero terminator.
	(read_dwarf5_line_entries): Check idx fits debug_section size.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 tools/debugedit.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/tools/debugedit.c b/tools/debugedit.c
index 502fa2126e77..5107943e309c 100644
--- a/tools/debugedit.c
+++ b/tools/debugedit.c
@@ -2225,6 +2225,13 @@ read_dwarf5_line_entries (DSO *dso, unsigned char **ptrp,
 			{
 			  debug_section *sec = &debug_sections[line_strp
                                            ? DEBUG_LINE_STR : DEBUG_STR];
+			  if (sec->data == NULL || idx >= sec->size)
+			    {
+			      error (0, 0, "%s: Bad %s idx %zd in .debug_line",
+				     dso->filename, sec->name, idx);
+			      return false;
+			    }
+
 			  if (collecting_dirs)
 			    dir = (char *)sec->data + idx;
 			  if (writing_files)
@@ -3123,6 +3130,15 @@ edit_dwarf2 (DSO *dso)
 		  debug_sec->elf_data = data;
 		  debug_sec->size = data->d_size;
 		  debug_sec->sec = i;
+
+		  /* String sections should end with a zero terminator.  */
+		  if ((j == DEBUG_STR || j == DEBUG_LINE_STR)
+		      && debug_sec->size > 0)
+		    {
+		      if (debug_sec->data[debug_sec->size - 1] != '\0')
+			error (0, 0, "%s: %s isn't zero terminated",
+			       dso->filename, debug_sec->name);
+		    }
 		  break;
 		}
 
-- 
2.55.0



More information about the Debugedit mailing list