Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Writing an experimental eBPF disassembler
Drumato
July 24, 2021
Programming
0
130
Writing an experimental eBPF disassembler
適当にLTネタになるかな,と思って作った話.
https://github.com/Drumato/ebpf-disasm
Drumato
July 24, 2021
Tweet
Share
More Decks by Drumato
See All by Drumato
DEMO Apps recently implemented
drumato
0
18
An incremental approach to implement an admission controller
drumato
0
69
Components of Kubernetes Cluster
drumato
0
86
cybozu-labs-youth-10th
drumato
1
760
ELFに蔓延るNULL三姉妹
drumato
1
2.7k
Other Decks in Programming
See All in Programming
コードの解析と言語習得の心得
jinjin33333
0
110
확장 가능한 테라폼 코드 관리 (Scalable Terraform Code Management)
posquit0
1
290
人類には難しいZynqで組み込みRust
ikemori
0
460
既存のプロジェクトにKMMを導入するための対応策
martysuzuki
2
280
WindowsコンテナDojo:第2回 Windowsコンテナアプリのビルド、公開、デプロイ
oniak3ibm
PRO
0
130
Nix for Scala folks
kubukoz
0
120
You CANt teach an old dog new tricks
michaelbukachi
0
110
UI State Modeling 어떤게 좋을까?
laco2951
0
160
ゼロから作る Protocol Buffer のパーサーとレキサー / Writing Protocol Buffer Parser/Lexer in Go from scratch
yoheimuta
1
150
dbtとBigQueryで始めるData Vault入門
kazk1018
0
160
iOSアプリの技術選択2022
tattn
3
1.3k
Loom is Blooming
josepaumard
3
470
Featured
See All Featured
Side Projects
sachag
449
37k
Reflections from 52 weeks, 52 projects
jeffersonlam
337
17k
Three Pipe Problems
jasonvnalue
89
8.6k
For a Future-Friendly Web
brad_frost
164
7.4k
Creatively Recalculating Your Daily Design Routine
revolveconf
205
10k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
498
130k
Fashionably flexible responsive web design (full day workshop)
malarkey
396
62k
JazzCon 2018 Closing Keynote - Leadership for the Reluctant Leader
reverentgeek
172
8.3k
The Invisible Side of Design
smashingmag
289
48k
How STYLIGHT went responsive
nonsquared
85
3.9k
Navigating Team Friction
lara
175
11k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
37
3.2k
Transcript
eBPF disassemblerを作る Drumato
Attention! • 完全準拠ではない • torvalds/linux/samples/bpfのkernel side sourceがそれっぽくなればOKまで をやった
背景
なんとなくeBPFについての記事を読んでた
"基本的に命令長は64bit固定" という記述を見る
素敵! disasm書きたい! (x64を想いつつ)
書く
身につける必要があった知識 • Object file上でeBPF functionがどのように表現されているか • Opcode encoding/immediateの形式を知る
あとは頑張って書く
これはassembler自作と大して変わらない
ELF Header
eBPF object file#ELF Header
eBPF object file#ELF Header
E_machine以外は普通のrelocに見える
Section Header Table
eBPF object file#SHT
eBPF object file#SHT .textはあるけど 中身は空
eBPF object file#SHT SEC("socket1")のように 書いたsymbolが sectionに
eBPF object file#SHT eBPF mapsもsectionに Entry sizeは Programで決まっていて, Shdr.sh_entsizeを読んでも わからない
Key + valueのどちらも可変なので entsizeは使えないか(linkとinfoは?)
eBPF object file#SHT これは単に"GPL\0"という文字列が 入っているだけ
Symbol table
eBPF object file#symtab
ぱっと見特に気になる点はなし
eBPF object file#summary • なんとなく以下を満たすsectionをdisasすれば良さそう ◦ Shdr.sh_type == SHT_PROGBITS ◦
Shdr.sh_flags & (SHF_ALLOC | SHF_EXECINSTR) != 0 ◦ Shdr.sh_size > 0
eBPF instruction architecture
eBPF instruction architecture#overview
eBPF instruction architecture#overview OpcodeEncoding head byteをどう解釈するか
eBPF instruction architecture#overview InstructionClass Opcodeをどう解釈するか Ex1. ic=0x04ならopcode=0x00はAdd ic=0x05ならopcode=0x00はJA
eBPF instruction format ArithOrJump Memory
eBPF instruction format ArithOrJump Memory ADD/SUB/MUL/DIV/MOD/LSH etc.. JA/JEQ/JGT/JSLT/ etc...
eBPF instruction format ArithOrJump Memory Use src field as register
If S bit is up
eBPF instruction format ArithOrJump Memory ALU/ALU64/JMP/JMP64
eBPF instruction format ArithOrJump Memory IMM/MEM/IND/ABS etc
eBPF instruction format ArithOrJump Memory Byte/Half word/Word/Double Word
eBPF instruction format ArithOrJump Memory LD/LDX/ST/STX
わかったら書く!書く!
機械語programmingは手動かす!
disasm#tips • Building block的に作ると良い ◦ Opcode type/InstClass typeを作ってEncoding typeを作る ▪
それぞれのdecoderを書く ◦ それら(とsrc/dst/offset/imm)をまとめてInstruction typeを作る ▪ decoderを次々呼ぶだけ • srcは ooooxxxx のようになっているけど,4bit rshすると使いやすい ◦ Register numberに変換する感覚
disasm#summary
disasm#summary
このくらいのしょぼ完成度なら サクッと数時間で作れる
references • Linux Socket Filtering aka Berkeley Packet Filter (BPF)
- www.kernel.org • Drumato/ebpf-disasm … 実装 ◦ 完全じゃないよ(Memory InstClassは適当)
Thanks!