diff --git a/src/asm_files.cpp b/src/asm_files.cpp index bf3433d7..c17920df 100644 --- a/src/asm_files.cpp +++ b/src/asm_files.cpp @@ -475,15 +475,11 @@ vector read_elf(std::istream& input_stream, const std::string& path continue; } offset -= program_offset; - - // Relocation is always on a double width instruction, so check that the offset is within the - // program and that offset + 1 is within the program. - if (((offset / sizeof(ebpf_inst)) + 1) >= prog.prog.size()) { + if (offset / sizeof(ebpf_inst) >= prog.prog.size()) { throw UnmarshalError("Invalid relocation data"); } ebpf_inst& reloc_inst = prog.prog[offset / sizeof(ebpf_inst)]; - ebpf_inst& next_reloc_inst = prog.prog[offset / sizeof(ebpf_inst) + 1]; auto [symbol_name, symbol_section_index] = get_symbol_name_and_section_index(symbols, index); @@ -497,6 +493,15 @@ vector read_elf(std::istream& input_stream, const std::string& path continue; } + // Verify that this is a map or global variable relocation. + verify_load_instruction(reloc_inst, symbol_name, offset); + + // Load instructions are two instructions long, so we need to check the next instruction. + if (prog.prog.size() <= offset / sizeof(ebpf_inst) + 1) { + throw UnmarshalError("Invalid relocation data"); + } + ebpf_inst& next_reloc_inst = prog.prog[offset / sizeof(ebpf_inst) + 1]; + // Perform relocation for symbols located in the maps section. if (map_section_indices.contains(symbol_section_index)) { relocate_map(reloc_inst, symbol_name, map_record_size_or_map_offsets, info, offset, index,