From: Max Filippov <jcmvbkbc@gmail.com>
To: Alan Modra <amodra@gmail.com>
Cc: binutils@sourceware.org, gdb-patches@sourceware.org
Subject: Re: xtensa_insnbuf_from_chars buffer overflow checking
Date: Tue, 3 Nov 2026 05:22:26 -0800 [thread overview]
Message-ID: <CAMo8BfKcC5+K=tF9nCka4gx3Bck5KQMGC5Dh1e1TLTHN1Rrp4w@mail.gmail.com> (raw)
In-Reply-To: <aum7neb5qQ87r3z9@squeak.grove.modra.org>
[-- Attachment #1: Type: text/plain, Size: 7879 bytes --]
Hi Alan,
On Tue, Nov 3, 2026 at 2:46 AM Alan Modra <amodra@gmail.com> wrote:
>
> R_XTENSA_ASM_SIMPLIFY has size zero, thus it can appear right at the
> end of a section without failing bfd_reloc_offset_in_range. Of course
> the reloc should never appear at the end of a section let alone in
> debug sections, but fuzzers do horrible things.
>
> For such an object we end up at xtensa_insnbuf_from_chars with num_chars
> zero (via get_expanded_call_opcode, elf_xtensa_do_asm_simplify,
> elf_xtensa_do_reloc, bfd_elf_xtensa_reloc, bfd_perform_relocation,
> bfd_generic_get_relocated_section_contents,
> bfd_simple_get_relocated_section_contents). A value of zero is
> allowed, because gas and gdb uses of xtensa_insnbuf_from_chars pass a
> zero rather than the actual buffer size. When the buffer size really
> is zero we get a buffer overflow. A relatively simple fix for this
> problem would be to change gas and gdb to pass INT_MAX rather than
> zero for num_chars. That would allow proper checks in elf32-xtensa.c,
> but I think we can do better than that.
>
> This patch modifies xtensa_insnbuf_from_chars to return whether the
> buffer was large enough, supplies actual buffer length args to the
> function where it is used in gas and gdb, and checks return status in
> most places. I omitted the status check in a few places in gas where
> other functions called had their status ignored.
>
> The gas changes use a wrapper for xtensa_insnbuf_from_chars called
> xtensa_insnbuf_from_frag since data in all cases is being read out of
> gas frags, and passing a frag pointer allows access to the frag buffer
> length. I'm reasonably confident that xtensa_insnbuf_from_frag isn't
> called from a frag under construction, so frag->fr_fix has been set to
> the fixed length of the frag. I'm less certain that
> xtensa_insnbuf_from_frag won't want to read past that into the
> variable part of a frag, for example with rs_machine_dependent frags.
> If so, using fr_fix is wrong, and the assertions there will trigger.
>
> The gdb changes are fairly obvious. Just before each
> xtensa_insnbuf_from_chars call, data has been read from the target
> into ibuf at address ba of length bt-ba. We step through ibuf using
> address ia at ia-ba, so the remaining ibuf length is bt-ba-(ia-ba).
>
> OK to apply?
>
> include/
> * xtensa-isa.h (xtensa_insnbuf_from_chars): Update prototype.
> bfd/
> * xtensa-isa.c (xtensa_insnbuf_from_chars): Return false on
> num_chars too small.
> * elf32-xtensa.c (elf_xtensa_do_reloc): Check return from
> xtensa_insnbuf_from_chars.
> (replace_tls_insn): Likewise.
> (get_relocation_opcode): Likewise.
> (insn_decode_len): Likewise.
> (insn_num_slots): Likewise.
> (insn_decode_opcode): Likewise.
> (narrow_instruction): Likewise.
> (widen_instruction): Likewise.
> (get_expanded_call_opcode): Likewise.
> (compute_ebb_proposed_actions): Likewise.
> * xtensa-modules.c: Include stdbool.h.
> opcodes/
> * xtensa-dis.c: Include xtensa-isa.h after bfd.h.
> gas/
> * config/tc-xtensa.c (xtensa_insnbuf_from_frag): New function.
> (get_opcode_from_frag): Rename from get_opcode_from_buf. Replace
> buf param with frag and offset params. Use xtensa_insnbuf_from_frag
> and check return. Adjust all callers.
> (vinsn_from_frag): Rename similarly and replace char* param
> with a fragS*, moving the fr_opcode indirection from callers
> to here. Use xtensa_insnbuf_from_frag. Adjust callers.
> (tinsn_from_frag): Rename and adjust similarly.
> (frag_format_size): Use xtensa_insnbuf_from_frag and check return.
> (xg_apply_fix_value): Likwise.
> (md_pcrel_from): Likwise.
> (next_instrs_are_b_retw): Likwise.
> (unrelaxed_frag_min_insn_count): Likwise.
> (unrelaxed_frag_has_b_j): Likwise.
> (xg_is_relaxable_fixup): Likwise.
> (md_convert_frag): Likwise.
> (xtensa_find_unaligned_branch_targets): Likewise but ignore return.
> (xtensa_find_unaligned_loops): Likwise.
> (xtensa_fix_short_loop_frags): Likwise.
> (xtensa_relax_frag): Likwise.
> gdb/
> * xtensa-tdep.c (xtensa_scan_prologue): Pass buffer length
> to xtensa_insnbuf_from_chars and check return.
> (call0_ret, call0_analyze_prologue, execute_code): Likewise.
>
...
> diff --git a/gas/config/tc-xtensa.c b/gas/config/tc-xtensa.c
> index e642ce03ac3..6febf68eca3 100644
> --- a/gas/config/tc-xtensa.c
> +++ b/gas/config/tc-xtensa.c
> @@ -543,7 +543,7 @@ static bool tinsn_has_invalid_symbolic_operands (const TInsn *);
> static bool tinsn_has_complex_operands (const TInsn *);
> static bool tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
> static bool tinsn_check_arguments (const TInsn *);
> -static void tinsn_from_chars (TInsn *, char *, int);
> +static void tinsn_from_frag (TInsn *, const fragS *, int);
> static void tinsn_immed_from_frag (TInsn *, fragS *, int);
> static int get_num_stack_text_bytes (IStack *);
> static int get_num_stack_literal_bytes (IStack *);
> @@ -558,7 +558,7 @@ static bool vinsn_has_specific_opcodes (vliw_insn *);
> static void xg_free_vinsn (vliw_insn *);
> static bool vinsn_to_insnbuf
> (vliw_insn *, char *, fragS *, bool);
> -static void vinsn_from_chars (vliw_insn *, char *);
> +static void vinsn_from_frag (vliw_insn *, const fragS *);
>
> /* Expression Utilities. */
>
> @@ -2665,9 +2665,23 @@ get_relaxable_immed (xtensa_opcode opcode)
> return last_immed;
> }
>
> +/* Wrapper for xtensa_insnbuf_from_chars to convert frag bytes
> + to xtensa_insnbuf. */
> +
> +static bool
> +xtensa_insnbuf_from_frag (xtensa_isa isa, xtensa_insnbuf insnbuf,
> + const fragS *frag, size_t off)
> +{
> + const unsigned char *buf = (const unsigned char *) frag->fr_literal + off;
> + size_t len = frag->fr_fix - off;
> + gas_assert (frag->fr_fix > off);
> + bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
> + gas_assert (ret);
This assertion is triggered when assembling the attached source with
option --text-section-literals with the following backtrace:
#0 0x00007ffff7e062e0 in exit () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x000055555566b9ac in xexit ()
#2 0x00005555555a2c86 in as_abort ()
#3 0x00005555555b917f in xtensa_insnbuf_from_frag.isra ()
#4 0x00005555555b956f in frag_format_size ()
#5 0x00005555555bc4fe in get_aligned_diff ()
#6 0x00005555555bc8e1 in relax_frag_for_align ()
#7 0x00005555555c8a6a in xtensa_relax_frag ()
#8 0x00005555555b52f8 in relax_segment ()
#9 0x00005555555b5762 in relax_seg ()
#10 0x00005555555dbcdf in bfd_map_over_sections ()
#11 0x00005555555b589a in write_object_file ()
#12 0x000055555558e071 in main ()
...
> diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
> index 5c25f122d29..4818559ff94 100644
> --- a/include/xtensa-isa.h
> +++ b/include/xtensa-isa.h
> @@ -210,7 +210,7 @@ extern int
> xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
> unsigned char *cp, int num_chars);
>
> -extern void
> +extern bool
Other similar functions in this library have return type `int` and return
0 on success and a negative error code on failure. Could this function
please follow this pattern too? That's because this header is included
into the generated configuration-specific xtensa-modules.c and does
not include <stdbool.h> itself.
> xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn,
> const unsigned char *cp, int num_chars);
>
...
--
Thanks.
-- Max
[-- Attachment #2: pr87014.s --]
[-- Type: application/octet-stream, Size: 5607 bytes --]
.file "pr87014.C"
.text
.section .rodata
.align 8
.LC0:
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.word -1
.text
.literal_position
.literal .LC1, .LC0
.literal .LC2, memcpy@PLT
.align 4
.global _Z9fillstackv
.type _Z9fillstackv, @function
_Z9fillstackv:
.LFB0:
movi a9, -0x90
add.n sp, sp, a9
.LCFI0:
s32i a0, sp, 140
s32i a15, sp, 136
.LCFI1:
mov.n a15, sp
.LCFI2:
l32r a9, .LC1
mov.n a10, a15
mov.n a11, a9
movi a9, 0x80
mov.n a4, a9
mov.n a3, a11
mov.n a2, a10
l32r a9, .LC2
callx0 a9
nop.n
movi a9, 0x80
add.n sp, a15, a9
l32i.n a0, sp, 12
l32i.n a15, sp, 8
addi sp, sp, 16
ret.n
.LFE0:
.size _Z9fillstackv, .-_Z9fillstackv
.literal_position
.literal .LC3, __cxa_allocate_exception@PLT
.literal .LC4, _ZTIi
.literal .LC5, __cxa_throw@PLT
.align 4
.global _Z1fxxxxxxx
.type _Z1fxxxxxxx, @function
_Z1fxxxxxxx:
.LFB1:
addi sp, sp, -48
.LCFI3:
s32i.n a0, sp, 44
s32i.n a15, sp, 40
.LCFI4:
mov.n a15, sp
.LCFI5:
s32i.n a2, a15, 0
s32i.n a3, a15, 4
s32i.n a4, a15, 8
s32i.n a5, a15, 12
s32i.n a6, a15, 16
s32i.n a7, a15, 20
movi.n a2, 4
l32r a9, .LC3
callx0 a9
mov.n a9, a2
mov.n a10, a9
movi.n a9, 0
s32i.n a9, a10, 0
l32r a9, .LC4
movi.n a4, 0
mov.n a3, a9
mov.n a2, a10
l32r a9, .LC5
callx0 a9
.LFE1:
.size _Z1fxxxxxxx, .-_Z1fxxxxxxx
.literal_position
.literal .LC6, -1, -1
.literal .LC7, _Z1fxxxxxxx@PLT
.literal .LC8, _Unwind_Resume@PLT
.literal .LC9, __cxa_begin_catch@PLT
.literal .LC10, __cxa_end_catch@PLT
.align 4
.global _Z1gv
.type _Z1gv, @function
_Z1gv:
.LFB2:
addi sp, sp, -64
.LCFI6:
s32i.n a0, sp, 60
s32i.n a15, sp, 56
.LCFI7:
mov.n a15, sp
.LCFI8:
l32r a10, .LC6
l32r a11, .LC6+4
s32i.n a10, sp, 24
s32i.n a11, sp, 28
l32r a10, .LC6
l32r a11, .LC6+4
s32i.n a10, sp, 16
s32i.n a11, sp, 20
l32r a10, .LC6
l32r a11, .LC6+4
s32i.n a10, sp, 8
s32i.n a11, sp, 12
l32r a10, .LC6
l32r a11, .LC6+4
s32i.n a10, sp, 0
s32i.n a11, sp, 4
l32r a6, .LC6
l32r a7, .LC6+4
l32r a4, .LC6
l32r a5, .LC6+4
l32r a2, .LC6
l32r a3, .LC6+4
l32r a9, .LC7
.LEHB0:
callx0 a9
.LEHE0:
j .L8
.L7:
mov.n a10, a2
mov.n a9, a3
beqi a9, 1, .L5
mov.n a9, a10
mov.n a2, a9
l32r a9, .LC8
.LEHB1:
callx0 a9
.LEHE1:
.L5:
mov.n a9, a10
mov.n a2, a9
l32r a9, .LC9
callx0 a9
mov.n a9, a2
l32i.n a9, a9, 0
s32i.n a9, a15, 32
l32r a9, .LC10
callx0 a9
.L8:
nop.n
mov.n sp, a15
l32i.n a0, sp, 60
l32i.n a15, sp, 56
addi sp, sp, 64
ret.n
.LFE2:
.section .gcc_except_table,"a",@progbits
.align 4
.LLSDA2:
.byte 0xff
.byte 0x9b
.uleb128 .LLSDATT2-.LLSDATTD2
.LLSDATTD2:
.byte 0x1
.uleb128 .LLSDACSE2-.LLSDACSB2
.LLSDACSB2:
.uleb128 .LEHB0-.LFB2
.uleb128 .LEHE0-.LEHB0
.uleb128 .L7-.LFB2
.uleb128 0x1
.uleb128 .LEHB1-.LFB2
.uleb128 .LEHE1-.LEHB1
.uleb128 0
.uleb128 0
.LLSDACSE2:
.byte 0x1
.byte 0
.align 4
.4byte DW.ref._ZTIi@pcrel
.LLSDATT2:
.text
.size _Z1gv, .-_Z1gv
.literal_position
.literal .LC11, _Z9fillstackv@PLT
.literal .LC12, _Z1gv@PLT
.align 4
.global main
.type main, @function
main:
.LFB3:
addi sp, sp, -16
.LCFI9:
s32i.n a0, sp, 12
s32i.n a15, sp, 8
.LCFI10:
mov.n a15, sp
.LCFI11:
l32r a9, .LC11
callx0 a9
l32r a9, .LC12
callx0 a9
movi.n a9, 0
mov.n a2, a9
mov.n sp, a15
l32i.n a0, sp, 12
l32i.n a15, sp, 8
addi sp, sp, 16
ret.n
.LFE3:
.size main, .-main
.section .eh_frame,"a",@progbits
.Lframe1:
.4byte .LECIE1-.LSCIE1
.LSCIE1:
.4byte 0
.byte 0x3
.string "zPLR"
.uleb128 0x1
.sleb128 -4
.uleb128 0
.uleb128 0x7
.byte 0x9b
.4byte DW.ref.__gxx_personality_v0@pcrel
.byte 0x1b
.byte 0x1b
.byte 0xc
.uleb128 0x1
.uleb128 0
.align 4
.LECIE1:
.LSFDE1:
.4byte .LEFDE1-.LASFDE1
.LASFDE1:
.4byte .LASFDE1-.Lframe1
.4byte .LFB1@pcrel
.4byte .LFE1-.LFB1
.uleb128 0x4
.4byte 0
.byte 0x4
.4byte .LCFI3-.LFB1
.byte 0xe
.uleb128 0x30
.byte 0x4
.4byte .LCFI4-.LCFI3
.byte 0x80
.uleb128 0x1
.byte 0x8f
.uleb128 0x2
.byte 0x4
.4byte .LCFI5-.LCFI4
.byte 0xd
.uleb128 0xf
.align 4
.LEFDE1:
.LSFDE3:
.4byte .LEFDE3-.LASFDE3
.LASFDE3:
.4byte .LASFDE3-.Lframe1
.4byte .LFB2@pcrel
.4byte .LFE2-.LFB2
.uleb128 0x4
.4byte .LLSDA2@pcrel
.byte 0x4
.4byte .LCFI6-.LFB2
.byte 0xe
.uleb128 0x40
.byte 0x4
.4byte .LCFI7-.LCFI6
.byte 0x80
.uleb128 0x1
.byte 0x8f
.uleb128 0x2
.byte 0x4
.4byte .LCFI8-.LCFI7
.byte 0xd
.uleb128 0xf
.align 4
.LEFDE3:
.LSFDE5:
.4byte .LEFDE5-.LASFDE5
.LASFDE5:
.4byte .LASFDE5-.Lframe1
.4byte .LFB3@pcrel
.4byte .LFE3-.LFB3
.uleb128 0x4
.4byte 0
.byte 0x4
.4byte .LCFI9-.LFB3
.byte 0xe
.uleb128 0x10
.byte 0x4
.4byte .LCFI10-.LCFI9
.byte 0x80
.uleb128 0x1
.byte 0x8f
.uleb128 0x2
.byte 0x4
.4byte .LCFI11-.LCFI10
.byte 0xd
.uleb128 0xf
.align 4
.LEFDE5:
.hidden DW.ref._ZTIi
.weak DW.ref._ZTIi
.section .data.rel.local.DW.ref._ZTIi,"awG",@progbits,DW.ref._ZTIi,comdat
.align 4
.type DW.ref._ZTIi, @object
.size DW.ref._ZTIi, 4
DW.ref._ZTIi:
.word _ZTIi
.hidden DW.ref.__gxx_personality_v0
.weak DW.ref.__gxx_personality_v0
.section .data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
.align 4
.type DW.ref.__gxx_personality_v0, @object
.size DW.ref.__gxx_personality_v0, 4
DW.ref.__gxx_personality_v0:
.word __gxx_personality_v0
.global __gxx_personality_v0
.ident "GCC: (GNU) 17.0.0 20261027 (experimental)"
.section .note.GNU-stack,"",@progbits
next prev parent reply other threads:[~2026-11-03 13:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 10:46 Alan Modra
2026-11-03 13:22 ` Max Filippov [this message]
2026-11-03 23:04 ` Alan Modra
2026-11-04 6:38 ` Max Filippov
2026-11-09 4:20 ` Alan Modra
2026-11-09 14:04 ` Max Filippov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMo8BfKcC5+K=tF9nCka4gx3Bck5KQMGC5Dh1e1TLTHN1Rrp4w@mail.gmail.com' \
--to=jcmvbkbc@gmail.com \
--cc=amodra@gmail.com \
--cc=binutils@sourceware.org \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox