Microsoft MVP성태의 닷넷 이야기
Linux: 95. eBPF - kprobe를 이용한 트레이스 [링크 복사], [링크+제목 복사],
조회: 5296
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 14개 있습니다.)
Linux: 86. Golang + bpf2go를 사용한 eBPF 기본 예제
; https://www.sysnet.pe.kr/2/0/13769

Linux: 94. eBPF - vmlinux.h 헤더 포함하는 방법 (bpf2go에서 사용)
; https://www.sysnet.pe.kr/2/0/13783

Linux: 95. eBPF - kprobe를 이용한 트레이스
; https://www.sysnet.pe.kr/2/0/13784

Linux: 96. eBPF (bpf2go) - fentry, fexit를 이용한 트레이스
; https://www.sysnet.pe.kr/2/0/13788

Linux: 100.  eBPF의 2가지 방식 - libbcc와 libbpf(CO-RE)
; https://www.sysnet.pe.kr/2/0/13801

Linux: 103. eBPF (bpf2go) - Tracepoint를 이용한 트레이스 (BPF_PROG_TYPE_TRACEPOINT)
; https://www.sysnet.pe.kr/2/0/13810

Linux: 105. eBPF - bpf2go에서 전역 변수 설정 방법
; https://www.sysnet.pe.kr/2/0/13815

Linux: 106. eBPF / bpf2go - (BPF_MAP_TYPE_HASH) Map을 이용한 전역 변수 구현
; https://www.sysnet.pe.kr/2/0/13817

Linux: 107. eBPF - libbpf CO-RE의 CONFIG_DEBUG_INFO_BTF 빌드 여부에 대한 의존성
; https://www.sysnet.pe.kr/2/0/13819

Linux: 109. eBPF / bpf2go - BPF_PERF_OUTPUT / BPF_MAP_TYPE_PERF_EVENT_ARRAY 사용법
; https://www.sysnet.pe.kr/2/0/13824

Linux: 110. eBPF / bpf2go - BPF_RINGBUF_OUTPUT / BPF_MAP_TYPE_RINGBUF 사용법
; https://www.sysnet.pe.kr/2/0/13825

Linux: 115. eBPF (bpf2go) - ARRAY / HASH map 기본 사용법
; https://www.sysnet.pe.kr/2/0/13893

Linux: 116. eBPF / bpf2go - BTF Style Maps 정의 구문과 데이터 정렬 문제
; https://www.sysnet.pe.kr/2/0/13894

Linux: 117. eBPF / bpf2go - Map에 추가된 요소의 개수를 확인하는 방법
; https://www.sysnet.pe.kr/2/0/13895




eBPF - kprobe를 이용한 트레이스

지난 글에서, bpf2go를 이용해 eBPF 프로그램을 go 언어로 작성해 봤는데요,

Golang + bpf2go를 사용한 eBPF 기본 예제
; https://www.sysnet.pe.kr/2/0/13769

빠르게 테스트를 할 요량이라면 간단하게 사용할 수 있는 bpftrace를 이용하는 것도 괜찮은 선택입니다. 예를 들어, 아래의 kprobe 예제는 do_sys_open / proc_sys_open 커널 함수가 호출될 때 메시지를 출력합니다.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy

$ sudo ./bpftrace -l 'kprobe:*sys_open'
kprobe:__ia32_compat_sys_open
kprobe:__ia32_sys_open
kprobe:__x64_sys_open
kprobe:do_sys_open
kprobe:proc_sys_open

// WSL 환경에서는 do_sys_open으로 지정한 경우 호출이 잘 되었지만,
// Ubuntu 22.04 / CentOS 9 VM 환경에서는 do_sys_open에 대해 오류는 없지만 호출이 안 됩니다.

$ sudo ./bpftrace -e 'kprobe:do_sys_open { printf("do_sys_open called\n"); }'
Attaching 1 probe...
^C

// 그래서 Ubuntu 22.04, CentOS 9 VM 환경에서는 do_sys_open 대신 proc_sys_open으로 걸어봤습니다.

$ sudo ./bpftrace -e 'kprobe:proc_sys_open { printf("proc_sys_open called\n"); }'
Attaching 1 probe...
proc_sys_open called
proc_sys_open called
proc_sys_open called
^C

이러한 eBPF의 kprobe 작동 원리가 궁금한데요,

Kernel Probes (Kprobes) - How Does a Kprobe Work?
; https://docs.kernel.org/trace/kprobes.html#how-does-a-kprobe-work

How Does a Kprobe Work?

When a kprobe is registered, Kprobes makes a copy of the probed instruction and replaces the first byte(s) of the probed instruction with a breakpoint instruction (e.g., int3 on i386 and x86_64).
When a CPU hits the breakpoint instruction, a trap occurs, the CPU's registers are saved, and control passes to Kprobes via the notifier_call_chain mechanism. Kprobes executes the "pre_handler" associated with the kprobe, passing the handler the addresses of the kprobe struct and the saved registers.

Next, Kprobes single-steps its copy of the probed instruction. (It would be simpler to single-step the actual instruction in place, but then Kprobes would have to temporarily remove the breakpoint instruction. This would open a small time window when another CPU could sail right past the probepoint.)

After the instruction is single-stepped, Kprobes executes the "post_handler," if any, that is associated with the kprobe. Execution then continues with the instruction following the probepoint.


그러니까, kprobe 대상의 함수에 BP(int 3) 1바이트 코드를 삽입해 두고, BP가 걸렸을 때 eBPF 함수를 실행해 준 다음 다시 BP를 이전 바이트로 복원해 실행을 계속하는 식입니다.

달리 말하면, 우리가 IDE 디버깅 모드에서 BP를 걸어 실행을 멈추고 재개하는 것과 동일한 원리로 동작한다고 보면 됩니다.




이러한 디버깅 방식이기 때문에 갖는 재미있는 특징이 하나 있습니다.

사실, "kprobe:proc_sys_open"에서 "proc_sys_open"은 커널 함수의 이름이면서 그 함수의 주소를 대표합니다. 주소라는 측면에서 볼 때, offset도 지정이 가능한데요, 이 값은 반드시 기계어의 한 단위로 지정해야 합니다. 예를 들어, "kprobe:proc_sys_open+0x1"로 걸어 보면 어떨까요? 만약 시작 주소의 기계어가 "nop" 1바이트라면 +0x1은 "nop" 다음 바이트를 가리켜 유효한 주소가 됩니다. 하지만 그건 운이 좋은 경우이고, 대부분은 이런 메시지가 출력될 것입니다.

// Ubuntu 22.04, CentOS 9 VM에서 실행한 경우

$ sudo ./bpftrace -e 'kprobe:proc_sys_open+0x1 { printf("proc_sys_open called\n"); }'
Attaching 1 probe...
cannot attach kprobe, Invalid or incomplete multibyte or wide character
ERROR: Possible attachment attempt in the middle of an instruction, try a different offset.
ERROR: Error attaching probe: kprobe:proc_sys_open+1

// WSL + Ubuntu 20.04에서 실행한 경우

$ sudo ./bpftrace -e 'kprobe:do_sys_open+1 { printf("do_sys_open called\n"); }'
Attaching 1 probe...
BUG: [/build/source/src/attached_probe.cpp:415] Could not add kprobe into middle of instruction: /lib/modules/5.15.153.1-microsoft-standard-WSL2+/build/vmlinux:do_sys_open+1
Aborted

이유를 알기 위해서는, proc_sys_open을 역어셈블하면 됩니다.

// Ubuntu 22.04, CentOS 9 VM에서 실행한 경우

$ sudo cat /boot/System.map-$(uname -r) | grep proc_sys_open
ffffffff815b2200 t __pfx_proc_sys_open
ffffffff815b2210 t proc_sys_open

$ objdump -S --start-address=0xffffffff815b2210 ./vmlinux | less

./vmlinux:     file format elf64-x86-64

Disassembly of section .text:

ffffffff815b2210 <.text+0x5b2210>:
ffffffff815b2210:       e8 cb 3a b0 ff          call   0xffffffff810b5ce0
ffffffff815b2215:       55                      push   %rbp
ffffffff815b2216:       48 c7 c0 e8 44 67 83    mov    $0xffffffff836744e8,%rax
ffffffff815b221d:       48 89 e5                mov    %rsp,%rbp
ffffffff815b2220:       41 55                   push   %r13
ffffffff815b2222:       49 89 f5                mov    %rsi,%r13
ffffffff815b2225:       41 54                   push   %r12
ffffffff815b2227:       49 89 fc                mov    %rdi,%r12
ffffffff815b222a:       53                      push   %rbx
ffffffff815b222b:       48 8b 5f d8             mov    -0x28(%rdi),%rbx
ffffffff815b222f:       48 c7 c7 b8 b8 f2 83    mov    $0xffffffff83f2b8b8,%rdi
ffffffff815b2236:       48 85 db                test   %rbx,%rbx
ffffffff815b2239:       48 0f 44 d8             cmove  %rax,%rbx
ffffffff815b223d:       e8 8e da c6 00          call   0xffffffff8221fcd0
ffffffff815b2242:       48 83 7b 18 00          cmpq   $0x0,0x18(%rbx)
ffffffff815b2247:       75 70                   jne    0xffffffff815b22b9
ffffffff815b2249:       83 43 0c 01             addl   $0x1,0xc(%rbx)
ffffffff815b224d:       48 c7 c7 b8 b8 f2 83    mov    $0xffffffff83f2b8b8,%rdi
ffffffff815b2254:       e8 b7 db c6 00          call   0xffffffff8221fe10
ffffffff815b2259:       49 8b 44 24 e0          mov    -0x20(%r12),%rax
ffffffff815b225e:       48 81 fb 00 f0 ff ff    cmp    $0xfffffffffffff000,%rbx
ffffffff815b2265:       77 65                   ja     0xffffffff815b22cc
ffffffff815b2267:       48 8b 40 28             mov    0x28(%rax),%rax
ffffffff815b226b:       48 85 c0                test   %rax,%rax
ffffffff815b226e:       74 0a                   je     0xffffffff815b227a
ffffffff815b2270:       48 63 00                movslq (%rax),%rax
ffffffff815b2273:       49 89 85 c8 00 00 00    mov    %rax,0xc8(%r13)
ffffffff815b227a:       48 c7 c7 b8 b8 f2 83    mov    $0xffffffff83f2b8b8,%rdi
ffffffff815b2281:       e8 4a da c6 00          call   0xffffffff8221fcd0
ffffffff815b2286:       83 6b 0c 01             subl   $0x1,0xc(%rbx)
ffffffff815b228a:       74 1d                   je     0xffffffff815b22a9
ffffffff815b228c:       48 c7 c7 b8 b8 f2 83    mov    $0xffffffff83f2b8b8,%rdi
...[생략]...

따라서, 위와 같은 경우에는 +0x5, +0x6, +0xd, +0x10, +0x12, +0x15 ... 등으로 지정해야 하는 것입니다. 그럼, 바로 그 위치에 "int 3 (0xcc)"을 삽입해, 이전까지의 기계어를 한 단위로 실행한 후 BP가 걸리게 됩니다.

저렇게 보면, eBPF가 꽤나 매력적인 기술임에는 틀림없는 것 같습니다. ^^ 커널 함수를 이토록 쉽고 안전하게 가로채기를 할 수 있다니, 그야말로 혁신이라고 밖에 볼 수 없습니다.




그나저나 한 가지 혼란스러운 부분이 있는데요, 바로 대상이 되는 함수를 지정하는 이름 규칙이 통일돼 있지 않다는 점입니다. 예를 들어, 지난 bpf2go 예제에서는 kprobe 대상을 지정할 때,

SEC("kprobe/sys_clone") 

"kprobe/sys_clone"으로 지정했는데요, sys_clone 함수 이름이 실제로 커널에 정의된 이름은 아닙니다. 원래 정의된 커널 함수는 __x64_, 또는 __ia32_ 접미사가 붙어 있는데요, (커널 4.17부터 적용)

// CentOS 9 VM에서 실행한 경우

$ sudo bpftrace -l 'kprobe:*sys_clone'
kprobe:__ia32_sys_clone
kprobe:__x64_sys_clone

$ grep sys_clone /proc/kallsyms
0000000000000000 t __pfx___do_sys_clone
0000000000000000 t __do_sys_clone
0000000000000000 T __pfx___x64_sys_clone
0000000000000000 T __x64_sys_clone
0000000000000000 T __pfx___ia32_sys_clone
0000000000000000 T __ia32_sys_clone
0000000000000000 t __pfx___do_sys_clone3
0000000000000000 t __do_sys_clone3
0000000000000000 T __pfx___x64_sys_clone3
0000000000000000 T __x64_sys_clone3
0000000000000000 T __pfx___ia32_sys_clone3
0000000000000000 T __ia32_sys_clone3
0000000000000000 d _eil_addr___ia32_sys_clone3
0000000000000000 d _eil_addr___x64_sys_clone3
0000000000000000 d _eil_addr___ia32_sys_clone
0000000000000000 d _eil_addr___x64_sys_clone

아마도 bpf2go는 패키지 내부적으로 플랫폼 환경에 따른 접미사를 적절하게 붙여주는 듯합니다.

반면, 위의 이름 규칙을 bpftrace로는 사용할 수 없습니다. 예를 들어, "sys_clone"을 bpftrace에 사용해 보면,

// bpftrace의 경우 독자적인 문법 제공 (bpftrace Language)
$ sudo /usr/bin/bpftrace -e 'kprobe:sys_clone { printf("sys_clone called\n"); }'
Attaching 1 probe...
cannot attach kprobe, probe entry may not exist
Error attaching probe: 'kprobe:sys_clone'

이를 찾을 수 없어 에러가 발생합니다. 즉, bpftrace는 이름과 정확히 일치해야만 정상 동작합니다.

$ sudo /usr/bin/bpftrace -e 'kprobe:__x64_sys_clone { printf("sys_clone called\n"); }'
Attaching 1 probe...
sys_clone called
sys_clone called
^C




이런 오류가 나온다면?

$ sudo bpftrace -e 'kprobe:proc_sys_open { printf("proc_sys_open called\n"); }'
Attaching 1 probe...
ERROR: Could not resolve symbol proc_sys_open. Use BPFTRACE_VMLINUX env variable to specify vmlinux path. Compile bpftrace with ALLOW_UNSAFE_PROBE option to force skip the check.

혹은 offset을 추가한 경우 이런 오류가 발생한다면?

$ sudo bpftrace -e 'kprobe:proc_sys_open+5 { printf("proc_sys_open called\n"); }'
Attaching 1 probe...
Can't check if kprobe is in proper place (compiled without (k|u)probe offset support): /lib/modules/5.15.153.1-microsoft-standard-WSL2+/build/vmlinux:proc_sys_open+5

새로운 버전의 bpftrace를 다운로드해 사용하면 됩니다.






만약 이런 식으로 "The kernel contains ... struct but does not support loading an iterator program against it. Please report this bug." 오류 메시지가 나오거나,

$ sudo /usr/bin/bpftrace -l '*sys_clone*'
WARNING: The kernel contains bpf_iter__bpf_map struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__task struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__unix struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__task_file struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__bpf_map_elem struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__bpf_sk_storage_map struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__task_vma struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__bpf_prog struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__sockmap struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__netlink struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__tcp struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__udp struct but does not support loading an iterator program against it. Please report this bug.
WARNING: The kernel contains bpf_iter__ipv6_route struct but does not support loading an iterator program against it. Please report this bug.
WARNING: Could not read symbols from /sys/kernel/tracing/available_events: No such file or directory
Segmentation fault

또는, 아래와 같은 오류가 발생한다면?

$ sudo bpftrace -e 'kprobe:do_sys_open { printf("do_sys_open called\n"); }'
stdin:1:1-19: WARNING: do_sys_open is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
kprobe:do_sys_open { printf("do_sys_open called\n"); }
~~~~~~~~~~~~~~~~~~
Attaching 1 probe...
^Copen(/sys/kernel/tracing/kprobe_events): No such file or directory
WARNING: failed to detach probe: kprobe:do_sys_open

/sys/kernel/tracing/kprobe_events 등을 마운트하시면 됩니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 1/16/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...
NoWriterDateCnt.TitleFile(s)
12108정성태1/10/202017391오류 유형: 587. Kernel Driver 시작 시 127(The specified procedure could not be found.) 오류 메시지 발생
12107정성태1/10/202018527.NET Framework: 877. C# - 프로세스의 모든 핸들을 열람 - 두 번째 이야기
12106정성태1/8/202019605VC++: 136. C++ - OSR Driver Loader와 같은 Legacy 커널 드라이버 설치 프로그램 제작 [1]
12105정성태1/8/202018121디버깅 기술: 153. C# - PEB를 조작해 로드된 DLL을 숨기는 방법
12104정성태1/7/202019298DDK: 9. 커널 메모리를 읽고 쓰는 NT Legacy driver와 C# 클라이언트 프로그램 [4]
12103정성태1/7/202022396DDK: 8. Visual Studio 2019 + WDK Legacy Driver 제작- Hello World 예제 [1]파일 다운로드2
12102정성태1/6/202018765디버깅 기술: 152. User 권한(Ring 3)의 프로그램에서 _ETHREAD 주소(및 커널 메모리를 읽을 수 있다면 _EPROCESS 주소) 구하는 방법
12101정성태1/5/202018992.NET Framework: 876. C# - PEB(Process Environment Block)를 통해 로드된 모듈 목록 열람
12100정성태1/3/202016480.NET Framework: 875. .NET 3.5 이하에서 IntPtr.Add 사용
12099정성태1/3/202019297디버깅 기술: 151. Windows 10 - Process Explorer로 확인한 Handle 정보를 windbg에서 조회 [1]
12098정성태1/2/202019063.NET Framework: 874. C# - 커널 구조체의 Offset 값을 하드 코딩하지 않고 사용하는 방법 [3]
12097정성태1/2/202017129디버깅 기술: 150. windbg - Wow64, x86, x64에서의 커널 구조체(예: TEB) 구조체 확인
12096정성태12/30/201919850디버깅 기술: 149. C# - DbgEng.dll을 이용한 간단한 디버거 제작 [1]
12095정성태12/27/201921554VC++: 135. C++ - string_view의 동작 방식
12094정성태12/26/201919274.NET Framework: 873. C# - 코드를 통해 PDB 심벌 파일 다운로드 방법
12093정성태12/26/201918838.NET Framework: 872. C# - 로딩된 Native DLL의 export 함수 목록 출력파일 다운로드1
12092정성태12/25/201917654디버깅 기술: 148. cdb.exe를 이용해 (ntdll.dll 등에 정의된) 커널 구조체 출력하는 방법
12091정성태12/25/201919938디버깅 기술: 147. pdb 파일을 다운로드하기 위한 symchk.exe 실행에 필요한 최소 파일 [1]
12090정성태12/24/201920037.NET Framework: 871. .NET AnyCPU로 빌드된 PE 헤더의 로딩 전/후 차이점 [1]파일 다운로드1
12089정성태12/23/201918945디버깅 기술: 146. gflags와 _CrtIsMemoryBlock을 이용한 Heap 메모리 손상 여부 체크
12088정성태12/23/201917925Linux: 28. Linux - 윈도우의 "Run as different user" 기능을 shell에서 실행하는 방법
12087정성태12/21/201918383디버깅 기술: 145. windbg/sos - Dictionary의 entries 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12086정성태12/20/201920878디버깅 기술: 144. windbg - Marshal.FreeHGlobal에서 발생한 덤프 분석 사례
12085정성태12/20/201918835오류 유형: 586. iisreset - The data is invalid. (2147942413, 8007000d) 오류 발생 - 두 번째 이야기 [1]
12084정성태12/19/201919279디버깅 기술: 143. windbg/sos - Hashtable의 buckets 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12083정성태12/17/201922262Linux: 27. linux - lldb를 이용한 .NET Core 응용 프로그램의 메모리 덤프 분석 방법 [2]
... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...