[PATCH] libctf: bounds-check type records at endian flip and type counting

Nix nix@esperi.org.uk
Tue Nov 10 14:12:37 GMT 2026


On 20 Oct 2026, Ilias stated:

> Apologies for the truncated copy of this mail sent moments ago -- the
> patch diff did not survive my mail tooling. Complete patch follows
> inline below.

Thanks for this.

> opening a foreign-endian CTF dict whose type section contains a record
> whose variable-length data runs past the end of the section (attacker
> controls kind and vlen) makes flip_types() swap up to vlen * elem
> bytes past the malloc'd buffer (heap OOB read-modify-write), before
> init_static_types() ever runs. Truncated record tails also make both
> the flip loop and the type-counting pass read past the section end,
> and get_ctt_size_*() computed CTF_TYPE_LSIZE eagerly, reading lsize
> fields that do not exist in small-form records.

Yuck! Every time I think this thing is sufficiently armoured, another
hole appears in it.

> ASan reproducer (--ctf consumer via ctf_bufopen()): 68-byte dict,
> single CTF_K_FUNCTION stype with vlen=255 and stroff right after the
> 12-byte record header. Unpatched master: heap-buffer-overflow write in
> flip_types. Patched: clean ECTF_CORRUPT. Regression test included.

The test makes me feel much happier about anything touching this area
(historically tricky to test without code exercised only at test time.)

Throwing this through my local test matrix now. Some of this is
applicable to the upcoming CTFv4 (mostly the LSIZE_SENT stuff): I've
just merged that in directly into my working tree without so much as a
commit log ;} v4 was already doing a bit of this (some of the swap-side
stuff, but it got it wrong: it was only checking before touching the
vlen region and hadn't considered the type header. So thank you again
for your psychic preemptive bugfixing!

(... and sorry for the delay :( )

> diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c
> index ee18e2b6..6f423f8d 100644
> --- a/libctf/ctf-open.c
> +++ b/libctf/ctf-open.c
> @@ -106,9 +106,16 @@ get_ctt_size_v1 (const ctf_dict_t *fp, const
> ctf_type_t *tp,
>  		 ssize_t *sizep, ssize_t *incrementp)
>  {
>    ctf_type_v1_t *t1p = (ctf_type_v1_t *) tp;
> +  size_t lsize = 0;
> +
> +  /* The small form does not have lsize fields at all: only evaluate the
> +     macro once we know this record really is in large form, or it reads
> +     past the end of the record.  */
> +  if (t1p->ctt_size == CTF_LSIZE_SENT_V1)
> +    lsize = CTF_TYPE_LSIZE (t1p);

I'm really kicking myself for this one. I was thinking "we never use the
lsize if inapplicable", but of course that doesn't make the
out-of-bounds read less wrong, even though its result is never used.

> @@ -742,10 +761,25 @@ init_static_types_internal (ctf_dict_t *fp,
> ctf_header_t *cth,
>
>    for (tp = tbuf; tp < tend; typemax++)
>      {
> -      unsigned short kind = LCTF_INFO_KIND (fp, tp->ctt_info);
> -      unsigned long vlen = LCTF_INFO_VLEN (fp, tp->ctt_info);
> +      unsigned short kind;
> +      unsigned long vlen;
>        ssize_t size, increment, vbytes;
>
> +      /* Make sure the record header itself is entirely within the section
> +	 before touching any of its fields, and the large-form record's
> +	 extended fields too.  */
> +      if (_libctf_unlikely_ ((uintptr_t) tend - (uintptr_t) tp
> +			     < sizeof (ctf_stype_t)))
> +	return ECTF_CORRUPT;

(I might tweak the spacing here.)

> +  /* 52-byte header, one 12-byte ctf_stype_t (vlen claims 255 argument
> +     types, i.e. 1KiB of variant data), then a 4-byte strtab -- the type
> +     section ends right after this one record's header.  */
> +  unsigned char buf[52 + 12 + 4];
> +  unsigned char *p = buf;
> +  ctf_sect_t sect;
> +  ctf_dict_t *fp;
> +  ctf_next_t *it = NULL;
> +  char *foo;
> +  int err;
> 
> +  memset (buf, 0, sizeof (buf));
> +
> +  putu16 (p, CTF_MAGIC);        p += 2;  /* foreign-endian marker */
> +  *p++ = CTF_VERSION_3;
> +  *p++ = 0;                              /* flags */
> +  p += 8 * 4;                            /* parlabel..funcidxoff: zero */

Oh I'm not really looking forward to adapting this pile of magic numbers
for CTFv4. Shouldn't be too hard, but painful. I do wish we had a
framework to allow use of GNU poke in the testsuite already :)

> +  /* Consume the accumulated error strings.  */
> +  while ((foo = ctf_errwarning_next (NULL, &it, NULL, NULL)) != NULL)
> +    free (foo);

Hm, for v4 I think I might also have it check that there are some
(because we're expecting at least one).

-- 
NULL && (void)


More information about the Binutils mailing list