[PATCH] arc: validate .arcextmap records

mustafa mustafa@medios.digiscrypt.com
Thu Nov 19 06:33:04 GMT 2026


create_map in arc-ext.c parses the .arcextmap section of an object.
Each record starts with its length and a type byte, but neither is
used for anything: the register number and the condition code are
used as array indexes without a range check, and the names are
copied with xstrdup, which does not stop at the end of the record or
of the section.

For a crafted section this writes outside coreRegisters (28 entries)
and condCodes (16 entries), and reads past the end of the section
while copying a name.  objdump and gdb both go through this code when
they disassemble an ARC file.  Found with UBSan, which reports an
index of 223 into ExtCoreRegister[28].

Stop at a record that is shorter than its header or longer than what
is left of the section, skip records that are too short for their
type, check the register number and condition code ranges, and copy
names with xstrndup bounded by the end of the record.

	* arc-ext.c (create_map): Validate the record length.  Check
	core register numbers and condition codes.  Use xstrndup for
	names.

Signed-off-by: Mohammed Mustafa <mustafa@medios.digiscrypt.com>
---
 opcodes/arc-ext.c | 57 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 12 deletions(-)

diff --git a/opcodes/arc-ext.c b/opcodes/arc-ext.c
index c99a6d2c3b5..f10fa1240e8 100644
--- a/opcodes/arc-ext.c
+++ b/opcodes/arc-ext.c
@@ -93,6 +93,7 @@ create_map (unsigned char *block,
 	    unsigned long length)
 {
   unsigned char *p = block;
+  unsigned char *rec_end;

   while (p && p < (block + length))
     {
@@ -116,17 +117,31 @@ create_map (unsigned char *block,
       if (p[0] == 0)
 	break;

+      /* Ignore the rest of the block if this record is too short to
+	 hold its header or extends past the end of the block.  */
+      if (p[0] < 2 || p[0] > (size_t) (block + length - p))
+	break;
+
+      /* End of the record, used to bound the names.  */
+      rec_end = p + p[0];
       switch (p[1])
 	{
 	case EXT_INSTRUCTION:
 	  {
-	    struct ExtInstruction  *insn = XNEW (struct ExtInstruction);
-	    int			    major = p[2];
-	    int			    minor = p[3];
-	    struct ExtInstruction **bucket =
-		   &arc_extension_map.instructions[INST_HASH (major, minor)];
+	    struct ExtInstruction  *insn;
+	    int			    major;
+	    int			    minor;
+	    struct ExtInstruction **bucket;
+
+	    if (p[0] < 6)
+	      break;
+	    insn = XNEW (struct ExtInstruction);
+	    major = p[2];
+	    minor = p[3];
+	    bucket = &arc_extension_map.instructions[INST_HASH (major, minor)];

-	    insn->name  = xstrdup ((char *) (p + 5));
+	    insn->name  = xstrndup ((char *) (p + 5),
+				    (size_t) (rec_end - (p + 5)));
 	    insn->major = major;
 	    insn->minor = minor;
 	    insn->flags = p[4];
@@ -143,6 +158,10 @@ create_map (unsigned char *block,
 	    unsigned char number = p[2];
 	    char*	  name	 = (char *) (p + 3);

+	    if (p[0] < 4
+		|| number < FIRST_EXTENSION_CORE_REGISTER
+		|| number > LAST_EXTENSION_CORE_REGISTER)
+	      break;
 	    arc_extension_map.
 	      coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].number
 	      = number;
@@ -151,7 +170,7 @@ create_map (unsigned char *block,
 	      = REG_READWRITE;
 	    arc_extension_map.
 	      coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].name
-	      = xstrdup (name);
+	      = xstrndup (name, (size_t) (rec_end - (unsigned char *) name));
 	    break;
 	  }

@@ -161,6 +180,10 @@ create_map (unsigned char *block,
 	    char*	      name   = (char *) (p + 7);
 	    enum ExtReadWrite rw     = p[6];

+	    if (p[0] < 8
+		|| number < FIRST_EXTENSION_CORE_REGISTER
+		|| number > LAST_EXTENSION_CORE_REGISTER)
+	      break;
 	    arc_extension_map.
 	      coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].number
 	      = number;
@@ -169,14 +192,20 @@ create_map (unsigned char *block,
 	      = rw;
 	    arc_extension_map.
 	      coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].name
-	      = xstrdup (name);
+	      = xstrndup (name, (size_t) (rec_end - (unsigned char *) name));
 	    break;
 	  }

 	case EXT_COND_CODE:
 	  {
-	    char *cc_name = xstrdup ((char *) (p + 3));
+	    char *cc_name;

+	    if (p[0] < 4
+		|| p[2] < FIRST_EXTENSION_CONDITION_CODE
+		|| p[2] > LAST_EXTENSION_CONDITION_CODE)
+	      break;
+	    cc_name = xstrndup ((char *) (p + 3),
+				(size_t) (rec_end - (p + 3)));
 	    arc_extension_map.
 	      condCodes[p[2] - FIRST_EXTENSION_CONDITION_CODE]
 	      = cc_name;
@@ -186,10 +215,14 @@ create_map (unsigned char *block,
 	case EXT_AUX_REGISTER:
 	  {
 	    /* Trickier -- need to store linked list of these.  */
-	    struct ExtAuxRegister *newAuxRegister
-	      = XNEW (struct ExtAuxRegister);
-	    char *aux_name = xstrdup ((char *) (p + 6));
+	    struct ExtAuxRegister *newAuxRegister;
+	    char *aux_name;

+	    if (p[0] < 7)
+	      break;
+	    newAuxRegister = XNEW (struct ExtAuxRegister);
+	    aux_name = xstrndup ((char *) (p + 6),
+				 (size_t) (rec_end - (p + 6)));
 	    newAuxRegister->name = aux_name;
 	    newAuxRegister->address = (((unsigned) p[2] << 24) | (p[3] << 16)
 				       | (p[4] << 8) | p[5]);
-- 
2.53.0


More information about the Binutils mailing list