Add run-readelf-badnote.sh with two compressed big-endian ELF test
inputs covering an unterminated NT_STAPSDT argument string and an
unterminated GNU build attribute string value in ebl_object_note.

The build attribute input fails the test without the preceding fix,
since the unbounded "%s" prints bytes from past the end of the note
name.  The stapsdt input does not change what is printed either way and
is included for path coverage, and as a check of the read itself when
configured with --enable-valgrind.

        * tests/Makefile.am (TESTS): Add run-readelf-badnote.sh.
        (EXTRA_DIST): Add run-readelf-badnote.sh,
        testfile-badnote-sdt.bz2 and testfile-badnote-ga.bz2.
        * tests/run-readelf-badnote.sh: New test.
        * tests/testfile-badnote-sdt.bz2: New testfile.
        * tests/testfile-badnote-ga.bz2: New testfile.

Signed-off-by: Minseok Kim <[email protected]>
---
 tests/Makefile.am              |   3 ++
 tests/run-readelf-badnote.sh   |  75 +++++++++++++++++++++++++++++++++
 tests/testfile-badnote-ga.bz2  | Bin 0 -> 158 bytes
 tests/testfile-badnote-sdt.bz2 | Bin 0 -> 159 bytes
 4 files changed, 78 insertions(+)
 create mode 100755 tests/run-readelf-badnote.sh
 create mode 100644 tests/testfile-badnote-ga.bz2
 create mode 100644 tests/testfile-badnote-sdt.bz2

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 98b267f..f707d5c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -164,6 +164,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile 
test-nlist \
        run-readelf-aranges.sh run-readelf-line.sh run-readelf-z.sh \
        run-readelf-frames.sh \
        run-readelf-n.sh \
+       run-readelf-badnote.sh \
        run-readelf-r-manyfuncs.sh \
        run-retain.sh \
        run-native-test.sh run-bug1-test.sh \
@@ -462,6 +463,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
             run-readelf-types.sh \
             run-readelf-frames.sh \
             run-readelf-n.sh \
+            run-readelf-badnote.sh \
+            testfile-badnote-sdt.bz2 testfile-badnote-ga.bz2 \
             testfile-gnu-property-note.bz2 testfile-gnu-property-note.o.bz2 \
             testfile_gnu_props.32le.o.bz2 \
             testfile_gnu_props.64le.o.bz2 \
diff --git a/tests/run-readelf-badnote.sh b/tests/run-readelf-badnote.sh
new file mode 100755
index 0000000..3980330
--- /dev/null
+++ b/tests/run-readelf-badnote.sh
@@ -0,0 +1,75 @@
+#! /bin/sh
+# Copyright (C) 2026 Minseok Kim
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/test-subr.sh
+
+# Both inputs are hand built big-endian ELF shared objects of 4096 bytes
+# whose only section header of interest is a note section placed so that
+# the note data ends exactly at the end of the file.  Neither can be
+# produced by a compiler, since both carry a malformed note on purpose.
+#
+# To regenerate testfile-badnote-sdt.bz2: write a big-endian ELFCLASS64
+# shared object with a .note.stapsdt section at offset 0xfac holding one
+# note with n_namesz 8 ("stapsdt\0"), n_descsz 64 and n_type 3.  Fill the
+# descriptor with three 8-byte addresses followed by an empty provider
+# string, a 37-byte probe name and an argument string that is not
+# terminated before the end of the descriptor.  Then bzip2 -9 it.
+#
+# To regenerate testfile-badnote-ga.bz2: write a big-endian ELFCLASS64
+# shared object with a .note.gnu.build.attributes section at offset 0xfec
+# holding one note with n_namesz 4, n_descsz 4 and n_type 0x100
+# (NT_GNU_BUILD_ATTRIBUTE_OPEN).  The four name bytes are "GA", then
+# '$' (GNU_BUILD_ATTRIBUTE_TYPE_STRING) and 0x06
+# (GNU_BUILD_ATTRIBUTE_ABI), with no NUL terminator, so the string value
+# of the attribute begins at the end of the note name.  Then bzip2 -9 it.
+#
+# Before the note name and stapsdt descriptor reads in ebl_object_note
+# were bounded, readelf -n on these two files read past the end of the
+# buffer libelf allocated for the note data.
+#
+# The two inputs are not equally strong as regression tests.  The build
+# attribute one changes what is printed, so it fails here without the
+# fix.  The stapsdt one does not: an over-long memchr length cannot
+# change which byte is found first, so the descriptor is rejected either
+# way and only the read itself differs.  It is kept because it covers
+# the path, and because configuring with --enable-valgrind turns it into
+# a real check of that read.
+
+testfiles testfile-badnote-sdt testfile-badnote-ga
+
+# The argument string of the SDT probe is unterminated, so the
+# descriptor has to be rejected rather than walked.
+testrun_compare ${abs_top_builddir}/src/readelf -n testfile-badnote-sdt << EOF
+
+Note section [ 1] '.note.stapsdt' of 84 bytes at offset 0xfac:
+  Owner          Data size  Type
+  stapsdt               64  Version: 3
+invalid SDT probe descriptor
+EOF
+
+# The build attribute name is unterminated and its string value starts
+# at the end of the note name, so the value has to print as empty.
+testrun_compare ${abs_top_builddir}/src/readelf -n testfile-badnote-ga << EOF
+
+Note section [ 1] '.note.gnu.build.attributes' of 20 bytes at offset 0xfec:
+  Owner          Data size  Type
+  GA                     4  GNU Build Attribute OPEN
+    Address Range: <unknown data>
+    ABI: ""
+EOF
+
+exit 0
diff --git a/tests/testfile-badnote-ga.bz2 b/tests/testfile-badnote-ga.bz2
new file mode 100644
index 
0000000000000000000000000000000000000000..581e0b214c1a341913eb4779a7406e3a63f1fe39
GIT binary patch
literal 158
zcmV;P0Ac?^T4*^jL0KkKSq}JHWdHzd^@IJGjsyh(bA&eKo&bO#00aOCAOLbABO@W8
z00E(<pQ;Q>X^L$_N1`+hG%*=3oOEhq3D;FU^#ru{3lXU~5<?<D5K=%EmUKSmB)M79
zj1#*swHNBcsv_hfxyp<*_VHhZ3=rOs*cv+Q?)3!=GZjXc{GzcBP+U?mZYoF|jBmx<
MkxmpO1HKm7Ky$x5hyVZp

literal 0
HcmV?d00001

diff --git a/tests/testfile-badnote-sdt.bz2 b/tests/testfile-badnote-sdt.bz2
new file mode 100644
index 
0000000000000000000000000000000000000000..30dc8756617cbe1fe65a6da7b16aeebd595b631e
GIT binary patch
literal 159
zcmV;Q0AT+@T4*^jL0KkKSrSwij{pF3car_siXadHV}t|%c0t?#pauW|00<xek_{4i
zjQ{`uXf)6<CXm#6nq)F)^#P#Kpv!YWN*`*Km-5#degwA(fxQ7V=o`@lABf2aq6<yn
zF;%=^31x*C2SMn=6h%nrju|nsj2SMEDG1%daSH1uI4Jq?0(uc<)`vtKa}CVCBLrWI
Nxgwk>NC_$n$ACr0I%xm^

literal 0
HcmV?d00001

-- 
2.34.1

Reply via email to