From ff3331dba521273747b538f0eb0a761c6d1d8a7a Mon Sep 17 00:00:00 2001
From: Minseok Kim <for8821@g.skku.edu>
Date: Mon, 23 Nov 2026 23:05:55 +0900
Subject: [PATCH] backends/: require a non-empty descriptor in tls_info and
 ioperm_info

Both tls_info and ioperm_info check the note descriptor size only for
alignment, not magnitude:

  if (descsz % 16 != 0)   /* backends/i386_corenote.c, NT_386_TLS */
  if (descsz % 4 != 0)    /* backends/x86_corenote.c, NT_386_IOPERM */

Because 0 % 16 == 0 and 0 % 4 == 0, a zero-length descriptor passes both
guards.  Both functions also set *nregloc to 0, so handle_core_items()
receives the note's real n_descsz without downstream size re-checks.  The
item tables (tls_items[] and ioperm_item) then dereference 16 bytes
(offsets 0x0, 0x4, 0x8, and 0xc) and 4 bytes (offset 0x0) respectively,
reading past the end of a zero-length descriptor.

Across the seven guards in this family, the five EXTRA_NOTES handlers in
backends/linux-core-note.c bound magnitude by exact size comparison, and
libebl/eblobjnote.c pairs both checks for a repeating descriptor:

  if (descsz >= 8 && descsz % 4 == 0)

Because NT_386_TLS and NT_386_IOPERM hold repeating entries, the bound
must be a non-zero minimum (at least 16 and 4 bytes) rather than an
equality.

On an x86 core file containing an NT_386_TLS note with n_descsz 0
followed by a second note, readelf -n previously read into the adjacent
note header and printed its fields as TLS entry values:

      CORE                   0  386_TLS
        index: 16, base: 0x00000000, limit: 0x00000003, flags: 0x44434241

Here, 16, 0x00000000, 0x00000003, and 0x44434241 are the following note's
n_namesz, n_descsz, n_type (NT_PRPSINFO), and the first four bytes of its
owner name "ABCD".

With the non-empty check in place, the zero-length note is listed with no
item line, while a well-formed 16-byte descriptor is unaffected.

ChangeLog:

	* backends/i386_corenote.c (tls_info): Reject empty descriptors
	  by requiring descsz >= 16.
	* backends/x86_corenote.c (ioperm_info): Reject empty descriptors
	  by requiring descsz >= 4.

Signed-off-by: Minseok Kim <for8821@g.skku.edu>

---
 backends/i386_corenote.c | 2 +-
 backends/x86_corenote.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/backends/i386_corenote.c b/backends/i386_corenote.c
index 15cd66b..bcdbe77 100644
--- a/backends/i386_corenote.c
+++ b/backends/i386_corenote.c
@@ -123,7 +123,7 @@ tls_info (GElf_Word descsz, GElf_Word *regs_offset,
 	  size_t *nregloc, const Ebl_Register_Location **reglocs,
 	  size_t *nitems, const Ebl_Core_Item **items)
 {
-  if (descsz % 16 != 0)
+  if (descsz < 16 || descsz % 16 != 0)
     return 0;
 
   *regs_offset = 0;
diff --git a/backends/x86_corenote.c b/backends/x86_corenote.c
index 629462c..3f71a32 100644
--- a/backends/x86_corenote.c
+++ b/backends/x86_corenote.c
@@ -39,7 +39,7 @@ ioperm_info (GElf_Word descsz, GElf_Word *regs_offset,
   static const Ebl_Core_Item ioperm_item =
     { .type = ELF_T_WORD, .format = 'b', .name = "ioperm" };
 
-  if (descsz % 4 != 0)
+  if (descsz < 4 || descsz % 4 != 0)
     return 0;
 
   *regs_offset = 0;
-- 
2.34.1

