xtensa_insnbuf_from_chars buffer overflow checking
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, just using fr_fix is wrong.
The gdb changes are fairly obvious. 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).
Revised:
- xtensa_insnbuf_from_chars return type and comment
- don't include stdbool.h in xtensa-modules.c
- remove one assert in xtensa_insnbuf_from_frag.
include/
* xtensa-isa.h (xtensa_insnbuf_from_chars): Update prototype
and comment.
bfd/
* xtensa-isa.c (xtensa_insnbuf_from_chars): Return
XTENSA_UNDEFINED on num_chars too small and length_decode
errors.
* 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: Don't use system header include brackets
for xtensa-isa.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.
7 files changed