)]}'
{
  "commit": "bb1d54e2554f86cd3740f847fa3b333a299ed7f6",
  "tree": "a8238191e0a3e7f6528165ca76954af2436c23ce",
  "parents": [
    "96248aa4173886a47a7aa1798861267edde28801"
  ],
  "author": {
    "name": "Alan Modra",
    "email": "amodra@gmail.com",
    "time": "Mon Nov 09 10:27:15 2026 +1030"
  },
  "committer": {
    "name": "Alan Modra",
    "email": "amodra@gmail.com",
    "time": "Tue Nov 10 07:26:20 2026 +1030"
  },
  "message": "xtensa_insnbuf_from_chars buffer overflow checking\n\nR_XTENSA_ASM_SIMPLIFY has size zero, thus it can appear right at the\nend of a section without failing bfd_reloc_offset_in_range.  Of course\nthe reloc should never appear at the end of a section let alone in\ndebug sections, but fuzzers do horrible things.\n\nFor such an object we end up at xtensa_insnbuf_from_chars with num_chars\nzero (via get_expanded_call_opcode, elf_xtensa_do_asm_simplify,\nelf_xtensa_do_reloc, bfd_elf_xtensa_reloc, bfd_perform_relocation,\nbfd_generic_get_relocated_section_contents,\nbfd_simple_get_relocated_section_contents).  A value of zero is\nallowed, because gas and gdb uses of xtensa_insnbuf_from_chars pass a\nzero rather than the actual buffer size.  When the buffer size really\nis zero we get a buffer overflow.  A relatively simple fix for this\nproblem would be to change gas and gdb to pass INT_MAX rather than\nzero for num_chars.  That would allow proper checks in elf32-xtensa.c,\nbut I think we can do better than that.\n\nThis patch modifies xtensa_insnbuf_from_chars to return whether the\nbuffer was large enough, supplies actual buffer length args to the\nfunction where it is used in gas and gdb, and checks return status in\nmost places.  I omitted the status check in a few places in gas where\nother functions called had their status ignored.\n\nThe gas changes use a wrapper for xtensa_insnbuf_from_chars called\nxtensa_insnbuf_from_frag since data in all cases is being read out of\ngas frags, and passing a frag pointer allows access to the frag buffer\nlength.  I\u0027m reasonably confident that xtensa_insnbuf_from_frag isn\u0027t\ncalled from a frag under construction, so frag-\u003efr_fix has been set to\nthe fixed length of the frag.  I\u0027m less certain that\nxtensa_insnbuf_from_frag won\u0027t want to read past that into the\nvariable part of a frag, for example with rs_machine_dependent frags.\nIf so, just using fr_fix is wrong.\n\nThe gdb changes are fairly obvious.  Before each\nxtensa_insnbuf_from_chars call, data has been read from the target\ninto ibuf at address ba of length bt-ba.  We step through ibuf using\naddress ia at ia-ba, so the remaining ibuf length is bt-ba-(ia-ba).\n\nRevised:\n- xtensa_insnbuf_from_chars return type and comment\n- don\u0027t include stdbool.h in xtensa-modules.c\n- remove one assert in xtensa_insnbuf_from_frag.\n\ninclude/\n\t* xtensa-isa.h (xtensa_insnbuf_from_chars): Update prototype\n\tand comment.\nbfd/\n\t* xtensa-isa.c (xtensa_insnbuf_from_chars): Return\n\tXTENSA_UNDEFINED on num_chars too small and length_decode\n\terrors.\n\t* elf32-xtensa.c (elf_xtensa_do_reloc): Check return from\n\txtensa_insnbuf_from_chars.\n\t(replace_tls_insn): Likewise.\n\t(get_relocation_opcode): Likewise.\n\t(insn_decode_len): Likewise.\n\t(insn_num_slots): Likewise.\n\t(insn_decode_opcode): Likewise.\n\t(narrow_instruction): Likewise.\n\t(widen_instruction): Likewise.\n\t(get_expanded_call_opcode): Likewise.\n\t(compute_ebb_proposed_actions): Likewise.\n\t* xtensa-modules.c: Don\u0027t use system header include brackets\n\tfor xtensa-isa.h.\nopcodes/\n\t* xtensa-dis.c: Include xtensa-isa.h after bfd.h.\ngas/\n\t* config/tc-xtensa.c (xtensa_insnbuf_from_frag): New function.\n\t(get_opcode_from_frag): Rename from get_opcode_from_buf.  Replace\n\tbuf param with frag and offset params.  Use xtensa_insnbuf_from_frag\n\tand check return.  Adjust all callers.\n\t(vinsn_from_frag): Rename similarly and replace char* param\n\twith a fragS*, moving the fr_opcode indirection from callers\n\tto here.  Use xtensa_insnbuf_from_frag.  Adjust callers.\n\t(tinsn_from_frag): Rename and adjust similarly.\n\t(frag_format_size): Use xtensa_insnbuf_from_frag and check return.\n\t(xg_apply_fix_value): Likwise.\n\t(md_pcrel_from): Likwise.\n\t(next_instrs_are_b_retw): Likwise.\n\t(unrelaxed_frag_min_insn_count): Likwise.\n\t(unrelaxed_frag_has_b_j): Likwise.\n\t(xg_is_relaxable_fixup): Likwise.\n\t(md_convert_frag): Likwise.\n\t(xtensa_find_unaligned_branch_targets): Likewise but ignore return.\n\t(xtensa_find_unaligned_loops): Likwise.\n\t(xtensa_fix_short_loop_frags): Likwise.\n\t(xtensa_relax_frag): Likwise.\ngdb/\n\t* xtensa-tdep.c (xtensa_scan_prologue): Pass buffer length\n\tto xtensa_insnbuf_from_chars and check return.\n\t(call0_ret, call0_analyze_prologue, execute_code): Likewise.\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "709f81e5bda9f2f314ef7d1328d6a0702dd3e4cd",
      "old_mode": 33188,
      "old_path": "bfd/elf32-xtensa.c",
      "new_id": "1c7e779ef5e40b903b599c7b98baa76a511131f9",
      "new_mode": 33188,
      "new_path": "bfd/elf32-xtensa.c"
    },
    {
      "type": "modify",
      "old_id": "74a234ab343922e3a9846b0c19ea1b8537e59fe6",
      "old_mode": 33188,
      "old_path": "bfd/xtensa-isa.c",
      "new_id": "771a97cfb5912901db46d718827f1bc2586ebad1",
      "new_mode": 33188,
      "new_path": "bfd/xtensa-isa.c"
    },
    {
      "type": "modify",
      "old_id": "79bc6d1413f2694adc4a2a6134fccc3357ea2f48",
      "old_mode": 33188,
      "old_path": "bfd/xtensa-modules.c",
      "new_id": "912e7e49eda8ce41294f0cf75194ddc9df6f3ed5",
      "new_mode": 33188,
      "new_path": "bfd/xtensa-modules.c"
    },
    {
      "type": "modify",
      "old_id": "e642ce03ac32c0147173f0fca663f1d52b54d799",
      "old_mode": 33188,
      "old_path": "gas/config/tc-xtensa.c",
      "new_id": "274f6eb1c4dcf2835dd01e600daf7642e207e706",
      "new_mode": 33188,
      "new_path": "gas/config/tc-xtensa.c"
    },
    {
      "type": "modify",
      "old_id": "9af18a857bac131c8c63da3d251b5b1cde327734",
      "old_mode": 33188,
      "old_path": "gdb/xtensa-tdep.c",
      "new_id": "cad1f39855a01de89d3c3fbac2fa872aacbb722e",
      "new_mode": 33188,
      "new_path": "gdb/xtensa-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "5c25f122d291331007053ebd9cd3671937a7358e",
      "old_mode": 33188,
      "old_path": "include/xtensa-isa.h",
      "new_id": "70563ea53ef50e67cc52b22180665abe01d45a6c",
      "new_mode": 33188,
      "new_path": "include/xtensa-isa.h"
    },
    {
      "type": "modify",
      "old_id": "9b17e5e1f7a8012971cef34a04ca02ae31ee9111",
      "old_mode": 33188,
      "old_path": "opcodes/xtensa-dis.c",
      "new_id": "ae29dc3cfe003639a5e664e97be294da5b2459e8",
      "new_mode": 33188,
      "new_path": "opcodes/xtensa-dis.c"
    }
  ]
}
