From f1137b8edb83d7a1885b02ebd2ec911446dfe878 Mon Sep 17 00:00:00 2001
From: Akhil Mittra <akhil.mittra@gmail.com>
Date: Wed, 25 Nov 2026 15:48:23 +0000
Subject: [PATCH] libdw: Check opcode table nforms before advancing readp

get_table_for_offset reads the number of forms of each
opcode_operands_table entry as an ULEB128 and only checked it after
adding it to readp.  A huge nforms value makes that pointer addition
overflow, which is undefined behaviour and can wrap readp back to
before endp so the check passes.  Check nforms against the remaining
bytes before advancing readp.

	* libdw/dwarf_getmacros.c (get_table_for_offset): Check e.nforms
	against the remaining bytes before adding it to readp.
	* tests/testfile-macros-nforms.bz2: New test input.
	* tests/run-dwarf-getmacros.sh: Test it.
	* tests/Makefile.am (EXTRA_DIST): Add testfile-macros-nforms.bz2.

Signed-off-by: Akhil Mittra <akhil.mittra@gmail.com>
---

Notes: clang's -fsanitize=pointer-overflow reports the old addition
("addition of unsigned offset ... overflowed") on the new test file.
GCC appears to fold the old check into the safe comparison itself, so
the --enable-sanitize-undefined GCC build passes the test before and
after the change.  make check: 261 PASS, 8 SKIP, 1 FAIL
(run-large-elf-file.sh, killed by the OOM killer on a 7 GB machine).

 libdw/dwarf_getmacros.c          |  12 +++++++-----
 tests/Makefile.am                |   1 +
 tests/run-dwarf-getmacros.sh     |  12 ++++++++++++
 tests/testfile-macros-nforms.bz2 | Bin 0 -> 177 bytes
 4 files changed, 20 insertions(+), 5 deletions(-)
 create mode 100644 tests/testfile-macros-nforms.bz2

diff --git a/libdw/dwarf_getmacros.c b/libdw/dwarf_getmacros.c
index 183e885f..8af3f787 100644
--- a/libdw/dwarf_getmacros.c
+++ b/libdw/dwarf_getmacros.c
@@ -277,16 +277,18 @@ get_table_for_offset (Dwarf *dbg, Dwarf_Word macoff,
 	  if (readp >= endp)
 	    goto invalid;
 	  get_uleb128 (e.nforms, readp, endp);
-	  e.forms = readp;
-	  op_protos[opcode - 1] = e;
-
-	  readp += e.nforms;
-	  if (readp > endp)
+	  /* Check before advancing readp; a huge nforms would otherwise
+	     overflow the pointer addition.  */
+	  if (e.nforms > (size_t) (endp - readp))
 	    {
 	    invalid:
 	      __libdw_seterrno (DWARF_E_INVALID_DWARF);
 	      return NULL;
 	    }
+	  e.forms = readp;
+	  op_protos[opcode - 1] = e;
+
+	  readp += e.nforms;
 	}
     }
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 98b267f5..9507b4a0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -443,6 +443,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 	     testfile46.bz2 testfile47.bz2 testfile48.bz2 testfile48.debug.bz2 \
 	     testfile49.bz2 testfile50.bz2 testfile51.bz2 \
 	     testfile-macros-0xff.bz2 testfile-macros-opcode0.bz2 \
+	     testfile-macros-nforms.bz2 \
 	     run-readelf-macro.sh testfilemacro.bz2 testfileclangmacro.bz2 \
 	     run-readelf-loc.sh testfileloc.bz2 \
 	     splitdwarf4-not-split4.dwo.bz2 \
diff --git a/tests/run-dwarf-getmacros.sh b/tests/run-dwarf-getmacros.sh
index bda6690d..a52cf468 100755
--- a/tests/run-dwarf-getmacros.sh
+++ b/tests/run-dwarf-getmacros.sh
@@ -715,6 +715,18 @@ testrun_compare ${abs_builddir}/dwarf-getmacros testfile-macros-opcode0 0xb <<\E
 invalid DWARF
 EOF
 
+# A .debug_macro opcode_operands_table entry whose form count is larger
+# than the rest of the section used to overflow the pointer addition
+# in get_table_for_offset() before the bounds check.  It must be
+# rejected as invalid DWARF.  Created from testfile-macros-opcode0 with
+#   printf '\x05\x00\x04\x01\x80\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01' > m
+#   objcopy --update-section .debug_macro=m testfile-macros-opcode0 \
+#     testfile-macros-nforms
+testfiles testfile-macros-nforms
+testrun_compare ${abs_builddir}/dwarf-getmacros testfile-macros-nforms 0xb <<\EOF
+invalid DWARF
+EOF
+
 # See testfile-dwp.source.
 testfiles testfile-dwp-5 testfile-dwp-5.dwp
 testfiles testfile-dwp-4-strict testfile-dwp-4-strict.dwp
diff --git a/tests/testfile-macros-nforms.bz2 b/tests/testfile-macros-nforms.bz2
new file mode 100644
index 0000000000000000000000000000000000000000..c7282318a9c4fa88dc0eb4905ee6560a6cf29dc1
GIT binary patch
literal 177
zcmV;i08alxT4*^jL0KkKS?rlrYybdt|M33o;D7=F5CK~RKp?;4p8&uB03bjB005u>
zk`SoJsTrX(z|aYSHin*{N)aIS227bYp|t=20==q^mf?Ht8K<&jOpDf<?42f1t?U4n
zss?g~L@HDvx6}v5F3JxDfoqZyVGqh*rcOX*L0yJQ@CJNVlIfGa@(o9vx6l%y^ZZmR
fS&9wNGBg|rS$iRZiRON>>#=tvQ-ui*$(2^XDwIo|

literal 0
HcmV?d00001

-- 
2.43.0

