Ghidra is a software reverse engineering (SRE) framework
Fix the semantics of eBPF byte swap instructions
Nicolas Iooss52cb7a36e685
diff --git a/Ghidra/Processors/eBPF/data/languages/eBPF.sinc b/Ghidra/Processors/eBPF/data/languages/eBPF.sincindex f41b39840e..16384685a4 100644--- a/Ghidra/Processors/eBPF/data/languages/eBPF.sinc+++ b/Ghidra/Processors/eBPF/data/languages/eBPF.sinc@@ -2,6 +2,8 @@# eBPF Processor Specification for Ghidra###############################################################################+define endian=$(ENDIAN);+#eBPF is a RISC register machine with a total of 11 64-bit registers, a program counter and a 512 byte fixed-size stack.#9 registers are general purpose read-write, one is a read-only stack pointer and the program counter is implicit,#i.e. we can only jump to a certain offset from it. The eBPF registers are always 64-bit wide.@@ -104,34 +106,55 @@ DST4: dst is dst { local tmp:4 = dst:4; export tmp; }#Bytewasp instructions###############################################################################++@if ENDIAN == "little"# BPF_ALU | BPF_K | BPF_END-:LE16 dst is imm=0x10 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 { dst=((dst) >> 8) | ((dst) << 8); }-:LE32 dst is imm=0x20 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 { dst=((dst) >> 24) | (((dst) & 0x00FF0000) >> 8) | (((dst) & 0x0000FF00) << 8) | ((dst) << 24); }-:LE64 dst is imm=0x40 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 {- dst=( (dst << 56) & 0xff00000000000000 ) |- ( (dst << 40) & 0x00ff000000000000 ) |- ( (dst << 24) & 0x0000ff0000000000 ) |- ( (dst << 8) & 0x000000ff00000000 ) |- ( (dst >> 8) & 0x00000000ff000000 ) |- ( (dst >> 24) & 0x0000000000ff0000 ) |- ( (dst >> 40) & 0x000000000000ff00 ) |- ( (dst >> 56) & 0x00000000000000ff );-}+:LE16 dst is imm=0x10 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 { dst = zext(dst:2); }+:LE32 dst is imm=0x20 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 { dst = zext(dst:4); }+:LE64 dst is imm=0x40 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 {}# BPF_ALU | BPF_X | BPF_END-:BE16 dst is imm=0x10 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 { dst=((dst) >> 8) | ((dst) << 8); }-:BE32 dst is imm=0x20 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 { dst=((dst) >> 24) | (((dst) & 0x00FF0000) >> 8) | (((dst) & 0x0000FF00) << 8) | ((dst) << 24); }+:BE16 dst is imm=0x10 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 {+ dst = ((dst & 0xff00) >> 8) | ((dst & 0x00ff) << 8);+}+:BE32 dst is imm=0x20 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 {+ dst = ((dst & 0xff000000) >> 24) | (((dst) & 0x00ff0000) >> 8) | (((dst) & 0x0000ff00) << 8) | ((dst & 0x000000ff) << 24);+}:BE64 dst is imm=0x40 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 {- dst=( (dst << 56) & 0xff00000000000000 ) |- ( (dst << 40) & 0x00ff000000000000 ) |- ( (dst << 24) & 0x0000ff0000000000 ) |- ( (dst << 8) & 0x000000ff00000000 ) |- ( (dst >> 8) & 0x00000000ff000000 ) |- ( (dst >> 24) & 0x0000000000ff0000 ) |- ( (dst >> 40) & 0x000000000000ff00 ) |- ( (dst >> 56) & 0x00000000000000ff );+ dst = ((dst << 56) & 0xff00000000000000) |+ ((dst << 40) & 0x00ff000000000000) |+ ((dst << 24) & 0x0000ff0000000000) |+ ((dst << 8) & 0x000000ff00000000) |+ ((dst >> 8) & 0x00000000ff000000) |+ ((dst >> 24) & 0x0000000000ff0000) |+ ((dst >> 40) & 0x000000000000ff00) |+ ((dst >> 56) & 0x00000000000000ff);+}+@else # ENDIAN == "big"+# BPF_ALU | BPF_K | BPF_END+:LE16 dst is imm=0x10 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 {+ dst = ((dst & 0xff00) >> 8) | ((dst & 0x00ff) << 8);+}+:LE32 dst is imm=0x20 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 {+ dst = ((dst & 0xff000000) >> 24) | (((dst) & 0x00ff0000) >> 8) | (((dst) & 0x0000ff00) << 8) | ((dst & 0x000000ff) << 24);+}+:LE64 dst is imm=0x40 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=0 & op_insn_class=0x4 {+ dst = ((dst << 56) & 0xff00000000000000) |+ ((dst << 40) & 0x00ff000000000000) |+ ((dst << 24) & 0x0000ff0000000000) |+ ((dst << 8) & 0x000000ff00000000) |+ ((dst >> 8) & 0x00000000ff000000) |+ ((dst >> 24) & 0x0000000000ff0000) |+ ((dst >> 40) & 0x000000000000ff00) |+ ((dst >> 56) & 0x00000000000000ff);}-++# BPF_ALU | BPF_X | BPF_END+:BE16 dst is imm=0x10 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 { dst = zext(dst:2); }+:BE32 dst is imm=0x20 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 { dst = zext(dst:4); }+:BE64 dst is imm=0x40 & dst & op_alu_jmp_opcode=0xd & op_alu_jmp_source=1 & op_insn_class=0x4 {}+@endif # ENDIAN = "big"+#Memory instructions - Load and Store###############################################################################diff --git a/Ghidra/Processors/eBPF/data/languages/eBPF_le.slaspec b/Ghidra/Processors/eBPF/data/languages/eBPF_le.slaspecindex 1f16bfe969..6f9d2973ed 100644--- a/Ghidra/Processors/eBPF/data/languages/eBPF_le.slaspec+++ b/Ghidra/Processors/eBPF/data/languages/eBPF_le.slaspec@@ -1,3 +1,3 @@-define endian=little;+@define ENDIAN "little"@include "eBPF.sinc"