GNU build attribute notes store attribute names and string or numeric
values inside the ELF note name field, which is not guaranteed to be
NUL-terminated within namesz bytes.  ebl_object_note currently reads
through name without checking namesz:

- After skipping ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX ("GA", 2 bytes),
  the guard checks namesz < 2 instead of requiring two bytes past the
  prefix, allowing data[0] and data[1] to be read past name + namesz
  when namesz is 2 or 3.

- Named attributes (data[1] in ASCII 32..126) pass &data[1] to printf
  ("%s") and strlen without checking for a terminator inside namesz.

- GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC computes namesz - (value - name) in
  size_t, which wraps to a large unsigned value if value reaches or
  exceeds name + namesz.

- GNU_BUILD_ATTRIBUTE_TYPE_STRING prints value with an unbounded "%s",
  reading past the note buffer when the string lacks a NUL terminator.

Validate namesz against the prefix plus two header bytes before
advancing data, track the end of the note name as nend = name + namesz,
locate named attribute terminators with memchr, guard the numeric byte
count against value > nend, and bound string value printing with
strnlen and "%.*s".  Existing offset arithmetic for well-formed notes
is preserved.

The length check is made before data is formed and the advance past a
named attribute is clamped to nend, so that neither name + prefix_len
nor value is ever constructed past the end of the note name.

        * libebl/eblobjnote.c (ebl_object_note): Require at least two
        bytes after ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX before reading
        data.  Bound named attribute scanning with memchr, prevent
        size_t underflow when computing numeric attribute byte counts,
        and print string attribute values with strnlen precision.

Signed-off-by: Minseok Kim <[email protected]>
---
 libebl/eblobjnote.c | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/libebl/eblobjnote.c b/libebl/eblobjnote.c
index 5ec3a1e..60d9641 100644
--- a/libebl/eblobjnote.c
+++ b/libebl/eblobjnote.c
@@ -194,15 +194,17 @@ ebl_object_note (Ebl *ebl, uint32_t namesz, const char 
*name, uint32_t type,
          /* Most data actually is inside the name.
             https://fedoraproject.org/wiki/Toolchain/Watermark  */
 
-         /* We need at least 2 chars of data to describe the
-            attribute and value encodings.  */
-         const char *data = (name
-                             + strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX));
-         if (namesz < 2)
+         /* We need at least 2 chars of data after the prefix to describe
+            the attribute and value encodings.  */
+         const size_t prefix_len
+           = strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
+         if (namesz < prefix_len + 2)
            {
              printf ("<insufficient data>\n");
              return;
            }
+         const char *data = name + prefix_len;
+         const char *nend = name + namesz;
 
          printf ("    ");
 
@@ -235,8 +237,23 @@ ebl_object_note (Ebl *ebl, uint32_t namesz, const char 
*name, uint32_t type,
              printf ("SHORT_ENUM: ");
              break;
            case 32 ... 126:
-             printf ("\"%s\": ", &data[1]);
-             value += strlen (&data[1]) + 1;
+             {
+               /* The attribute name is inside the note name, which is not
+                  guaranteed to be terminated.  */
+               const char *aname = &data[1];
+               const char *anul = memchr (aname, '\0', nend - aname);
+               if (anul == NULL)
+                 {
+                   printf ("<invalid name>\n");
+                   return;
+                 }
+               printf ("\"%s\": ", aname);
+               size_t skip = (size_t) (anul - aname) + 1;
+               if (skip > (size_t) (nend - value))
+                 value = nend;
+               else
+                 value += skip;
+             }
              break;
            default:
              printf ("<unknown>: ");
@@ -250,7 +267,9 @@ ebl_object_note (Ebl *ebl, uint32_t namesz, const char 
*name, uint32_t type,
                /* Any numbers are always in (unsigned) little endian.  */
                static const Dwarf dbg
                  = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
-               size_t bytes = namesz - (value - name);
+               if (value > nend)
+                 goto unknown;
+               size_t bytes = nend - value;
                uint64_t val;
                if (bytes == 1)
                  val = *(unsigned char *) value;
@@ -266,7 +285,10 @@ ebl_object_note (Ebl *ebl, uint32_t namesz, const char 
*name, uint32_t type,
              }
              break;
            case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
-             printf ("\"%s\"", value);
+             {
+               size_t avail = value < nend ? (size_t) (nend - value) : 0;
+               printf ("\"%.*s\"", (int) strnlen (value, avail), value);
+             }
              break;
            case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
              printf ("TRUE");
-- 
2.34.1

Reply via email to