Microsoft MVP성태의 닷넷 이야기
Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블 [링크 복사], [링크+제목 복사],
조회: 5721
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 2개 있습니다.)
Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블
; https://www.sysnet.pe.kr/2/0/13780

Linux: 93. Ubuntu 22.04 - 커널 이미지로부터 커널 함수 역어셈블
; https://www.sysnet.pe.kr/2/0/13782




WSL 2 - 커널 이미지로부터 커널 함수 역어셈블

현시점(2024-10-22) WSL의 리눅스 커널 버전은 5.15.153.1이 최신입니다.

$ uname -r
5.15.153.1-microsoft-standard-WSL2

그리고 이것의 빌드 이미지는 윈도우 디렉터리에 저장돼 있는데요,

"C:\Windows\System32\lxss\tools\kernel"
/mnt/c/Windows/System32/lxss/tools/kernel

아래의 글을 보면,

How to extract and disassemble a Linux kernel image (vmlinuz)
; https://blog.packagecloud.io/how-to-extract-and-disassmble-a-linux-kernel-image-vmlinuz/

커널 이미지를 이렇게 추출할 수 있다고 나옵니다.

$ wget -O extract-vmlinux https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux

$ chmod +x extract-vmlinux

$ sudo ./extract-vmlinux /boot/vmlinuz > vmlinux

$ objdump -D vmlinux | less

따라서 WSL 환경에서는 /boot/vmlinuz 대신 윈도우 디렉터리의 kernel 파일을 대상으로 추출할 수 있습니다.

$ sudo ./extract-vmlinux /mnt/c/Windows/System32/lxss/tools/kernel > vmlinux

하지만, 저렇게 커널 이미지만 추출할 수 있을 뿐, 정작 System.map 파일이 없어 특정 함수의 주소를 찾을 수는 없습니다. (제가 방법을 모르는 것일 수도 있지만) WSL의 경우 System.map 파일을 구하려면 커널 소스코드를 직접 빌드해야 하는데요, 기왕 빌드해야 한다면 최신 커널로 업데이트하면 좋겠지만, 아쉽게도 6.6.x 버전은 Docker Desktop이 지원되지 않아 현재 버전(5.15.153.1)을 다운로드해야 합니다.

linux-msft-wsl-5.15.153.1
; https://github.com/microsoft/WSL2-Linux-Kernel/releases/tag/linux-msft-wsl-5.15.153.1

빌드 방법은 지난 글에 설명한 것을 그대로 따릅니다.

WSL 2 - Mariner VM 커널 이미지 업데이트 방법
; https://www.sysnet.pe.kr/2/0/13779

약간의 버전 차이만 있을 뿐이니, 아래에 간략하게 명령어만을 나열해 놓겠습니다.

$ git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --depth=1 -b linux-msft-wsl-5.15.y

$ sudo apt update && sudo apt upgrade -y
$ sudo apt update && sudo apt install build-essential flex bison libssl-dev libelf-dev bc python3 -y

// Ubuntu 20.04의 경우
$ sudo apt install dwarves -y

// Ubuntu 24.04의 경우
$ sudo apt install pahole -y

$ cd WSL2-Linux-Kernel

$ make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
...[생략]...
  LD      vmlinux
  BTFIDS  vmlinux
  SORTTAB vmlinux
  SYSMAP  System.map
...[생략]...
  LD      arch/x86/boot/compressed/vmlinux
  ZOFFSET arch/x86/boot/zoffset.h
  OBJCOPY arch/x86/boot/vmlinux.bin
  AS      arch/x86/boot/header.o
  LD      arch/x86/boot/setup.elf
  OBJCOPY arch/x86/boot/setup.bin
  BUILD   arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready  (#1)

$ sudo make modules_install headers_install

$ cp arch/x86/boot/bzImage /mnt/c/temp

// c:\temp에 복사한 파일을 윈도우 탐색기에서 다시 "c:\" 루트 경로에 "bzImage-wsl2-5.15.153.1" 파일명으로 복사

마지막으로, 저 이미지 파일을 %USERPROFILE%\.wslconfig 파일에 다음과 같이 반영한 다음,

c:\temp> type %USERPROFILE%\.wslconfig
[wsl2]
kernel=C:\\bzImage-wsl2-5.15.153.1

wsl을 재시작합니다.

// 관리자 권한으로 실행
c:\temp> wsl --shutdown

확인을 위해 아무 Linux 배포본을 하나 실행한 다음 uname 명령을 내리면,

$ uname -r
5.15.153.1-microsoft-standard-WSL2+

기존 uname 결과에서 끝에 "+"만 붙은 것을 확인할 수 있습니다. 다행히, 이 버전으로는 Docker Desktop이 잘 동작합니다.




자, 그럼 방금 전의 빌드로 인해 System.map 파일을 구할 수 있으니 특정 함수가 정의된 주소를 찾을 수 있습니다.

// make 빌드를 실행했던 경로에 System.map 파일 위치

$ cat ./System.map | grep do_sys_open
ffffffff8131a0c0 t do_sys_openat2
ffffffff8131bea0 T do_sys_open

또한, 커널 이미지도 vmlinux 파일로 이미 풀려 있으므로 굳이 extract-vmlinux로 추출할 필요 없이 해당 함수의 역어셈블 코드를 알아낼 수 있습니다.

$ objdump -S --start-address=0xffffffff8131bea0 ./vmlinux | less
./vmlinux:     file format elf64-x86-64

Disassembly of section .text:

ffffffff8131bea0 <do_sys_open>:
        putname(tmp);
        return fd;
}

long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
ffffffff8131bea0:       e8 bb b6 db ff          callq  ffffffff810d7560 <__fentry__>
ffffffff8131bea5:       48 83 ec 20             sub    $0x20,%rsp
ffffffff8131bea9:       65 48 8b 04 25 28 00    mov    %gs:0x28,%rax
ffffffff8131beb0:       00 00
ffffffff8131beb2:       48 89 44 24 18          mov    %rax,0x18(%rsp)
ffffffff8131beb7:       31 c0                   xor    %eax,%eax
        if (how.flags & O_PATH)
ffffffff8131beb9:       f7 c2 00 00 20 00       test   $0x200000,%edx
ffffffff8131bebf:       89 d0                   mov    %edx,%eax
ffffffff8131bec1:       74 3a                   je     ffffffff8131befd <do_sys_open+0x5d>
                how.flags &= O_PATH_FLAGS;
ffffffff8131bec3:       25 00 00 2b 00          and    $0x2b0000,%eax
        if (how.flags & O_PATH)
ffffffff8131bec8:       31 c9                   xor    %ecx,%ecx
        struct open_how how = build_open_how(flags, mode);
        return do_sys_openat2(dfd, filename, &how);
ffffffff8131beca:       48 89 e2                mov    %rsp,%rdx
        return how;
ffffffff8131becd:       48 89 04 24             mov    %rax,(%rsp)
ffffffff8131bed1:       48 89 4c 24 08          mov    %rcx,0x8(%rsp)
ffffffff8131bed6:       48 c7 44 24 10 00 00    movq   $0x0,0x10(%rsp)
ffffffff8131bedd:       00 00
        return do_sys_openat2(dfd, filename, &how);
ffffffff8131bedf:       e8 dc e1 ff ff          callq  ffffffff8131a0c0 <do_sys_openat2>
}
...[생략]...




이 외에도 GDB를 이용한 방법이 있습니다.

$ gdb ~/temp/WSL2-Linux-Kernel/vmlinux
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2
...[생략]...

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/testusr/temp/WSL2-Linux-Kernel/vmlinux...
(gdb) 

위와 같이 vmlinux 이미지 파일을 gdb로 시작하면 그에 대한 symbol 정보도 함께 자동으로 읽어냅니다. 이후, gdb 명령어를 이용해 특정 함수의 코드를 역어셈블할 수 있습니다.

(gdb) disass do_sys_open
Dump of assembler code for function do_sys_open:
   0xffffffff8131bea0 <+0>:     callq  0xffffffff810d7560 <__fentry__>
   0xffffffff8131bea5 <+5>:     sub    $0x20,%rsp
   0xffffffff8131bea9 <+9>:     mov    %gs:0x28,%rax
   0xffffffff8131beb2 <+18>:    mov    %rax,0x18(%rsp)
   0xffffffff8131beb7 <+23>:    xor    %eax,%eax
   0xffffffff8131beb9 <+25>:    test   $0x200000,%edx
   0xffffffff8131bebf <+31>:    mov    %edx,%eax
   0xffffffff8131bec1 <+33>:    je     0xffffffff8131befd <do_sys_open+93>
   0xffffffff8131bec3 <+35>:    and    $0x2b0000,%eax
   0xffffffff8131bec8 <+40>:    xor    %ecx,%ecx
   0xffffffff8131beca <+42>:    mov    %rsp,%rdx
   0xffffffff8131becd <+45>:    mov    %rax,(%rsp)
   0xffffffff8131bed1 <+49>:    mov    %rcx,0x8(%rsp)
   0xffffffff8131bed6 <+54>:    movq   $0x0,0x10(%rsp)
   0xffffffff8131bedf <+63>:    callq  0xffffffff8131a0c0 <do_sys_openat2>
   0xffffffff8131bee4 <+68>:    mov    0x18(%rsp),%rsi
   0xffffffff8131bee9 <+73>:    xor    %gs:0x28,%rsi
   0xffffffff8131bef2 <+82>:    jne    0xffffffff8131bf12 <do_sys_open+114>
   0xffffffff8131bef4 <+84>:    add    $0x20,%rsp
   0xffffffff8131bef8 <+88>:    jmpq   0xffffffff822054c0 <__x86_return_thunk>
   0xffffffff8131befd <+93>:    and    $0x7fffc3,%eax
   0xffffffff8131bf02 <+98>:    and    $0x400040,%edx
   0xffffffff8131bf08 <+104>:   je     0xffffffff8131bf17 <do_sys_open+119>
   0xffffffff8131bf0a <+106>:   and    $0xfff,%ecx
   0xffffffff8131bf10 <+112>:   jmp    0xffffffff8131beca <do_sys_open+42>
   0xffffffff8131bf12 <+114>:   callq  0xffffffff81ee9120 <__stack_chk_fail>
   0xffffffff8131bf17 <+119>:   xor    %ecx,%ecx
   0xffffffff8131bf19 <+121>:   jmp    0xffffffff8131beca <do_sys_open+42>
End of assembler dump.)




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 10/22/2024]

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

비밀번호

댓글 작성자
 




... 106  107  108  109  110  111  112  113  114  115  116  117  [118]  119  120  ...
NoWriterDateCnt.TitleFile(s)
10974정성태5/20/201623658.NET Framework: 588. C# - OxyPlot 라이브러리로 복소수 표현파일 다운로드1
10973정성태5/20/201628733.NET Framework: 587. C# Plotting 라이브러리 OxyPlot [3]파일 다운로드1
10972정성태5/19/201627768Math: 16. C# - 갈루아 필드 GF(2) 연산 [3]파일 다운로드1
10971정성태5/19/201620571오류 유형: 334. Visual Studio - 빌드 시 경고 warning MSB3884: Could not find rule set file "...". [2]
10970정성태5/19/201624933오류 유형: 333. OxyPlot 라이브러리의 컨트롤을 Toolbox에 등록 시 오류 [2]
10969정성태5/18/201624223.NET Framework: 586. C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (3) - "Open with" 목록에 등록파일 다운로드1
10968정성태5/18/201619222오류 유형: 332. Visual Studio - 단위 테스트 생성 시 "Design time expression evaluation" 오류 메시지
10967정성태5/12/201624331.NET Framework: 585. C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (2) - 웹 브라우저가 다운로드 후 자동 실행
10966정성태5/12/201631981.NET Framework: 584. C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (1) - 기본 [1]파일 다운로드1
10965정성태5/12/201624023디버깅 기술: 81. try/catch로 조용히 사라진 예외를 파악하고 싶다면?
10964정성태5/12/201622654오류 유형: 331. ASP.NET에서 System.BadImageFormatException 예외가 발생하는 경우
10963정성태5/11/201624898VS.NET IDE: 107. Visual Studio 2015의 "DTAR_..." 특수 폴더가 생성되는 문제파일 다운로드2
10962정성태5/11/201624975오류 유형: 330. Visual Studio 단위 테스트 시 DisconnectedContext 예외 발생
10961정성태5/11/201624785.NET Framework: 583. 문제 재현 - Managed Debugging Assistant 'DisconnectedContext' has detected a problem in '...'파일 다운로드1
10960정성태5/10/201622205오류 유형: 329. ATL 메서드 추가 마법사 창에서 8ce0000b 오류 발생
10959정성태5/9/201624844.NET Framework: 582. CLR Profiler - 별도 정의한 .NET 코드를 호출하도록 IL 코드 변경파일 다운로드1
10958정성태5/6/201651874개발 환경 구성: 284. "Let's Encrypt"에서 제공하는 무료 SSL 인증서를 IIS에 적용하는 방법 (1) [3]
10957정성태5/3/201627149오류 유형: 328. 윈도우 백업 시 오류 - 0x80780166 두 번째 이야기 [1]
10956정성태5/3/201622690Windows: 117. BitLocker - This device can't use a Trusted Platform Module.
10955정성태5/3/201629376.NET Framework: 581. C# - 순열(Permutation) 예제 코드파일 다운로드2
10954정성태5/3/201630332.NET Framework: 580. C# - 조합(Combination) 예제 코드 [2]파일 다운로드1
10953정성태5/2/201619846.NET Framework: 579. Assembly.LoadFrom으로 로드된 어셈블리의 JIT 컴파일 코드 공유?파일 다운로드1
10952정성태5/2/201621979.NET Framework: 578. 도메인 중립적인 어셈블리가 비-도메인 중립적인 어셈블리를 참조하는 경우파일 다운로드1
10951정성태5/2/201619900.NET Framework: 577. CLR Profiler로 살펴보는 SharedDomain의 모듈 로드 동작파일 다운로드1
10950정성태5/2/201626311.NET Framework: 576. 기본적인 CLR Profiler 소스 코드 설명 [2]파일 다운로드2
10949정성태4/28/201619918.NET Framework: 575. SharedDomain과 JIT 컴파일파일 다운로드1
... 106  107  108  109  110  111  112  113  114  115  116  117  [118]  119  120  ...