Microsoft MVP성태의 닷넷 이야기
Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블 [링크 복사], [링크+제목 복사],
조회: 5663
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...
NoWriterDateCnt.TitleFile(s)
12086정성태12/20/201920970디버깅 기술: 144. windbg - Marshal.FreeHGlobal에서 발생한 덤프 분석 사례
12085정성태12/20/201918961오류 유형: 586. iisreset - The data is invalid. (2147942413, 8007000d) 오류 발생 - 두 번째 이야기 [1]
12084정성태12/19/201919395디버깅 기술: 143. windbg/sos - Hashtable의 buckets 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12083정성태12/17/201922341Linux: 27. linux - lldb를 이용한 .NET Core 응용 프로그램의 메모리 덤프 분석 방법 [2]
12082정성태12/17/201920576오류 유형: 585. lsof: WARNING: can't stat() fuse.gvfsd-fuse file system
12081정성태12/16/201922437개발 환경 구성: 465. 로컬 PC에서 개발 중인 ASP.NET Core 웹 응용 프로그램을 다른 PC에서도 접근하는 방법 [5]
12080정성태12/16/201919602.NET Framework: 870. C# - 프로세스의 모든 핸들을 열람
12079정성태12/13/201921487오류 유형: 584. 원격 데스크톱(rdp) 환경에서 다중 또는 고용량 파일 복사 시 "Unspecified error" 오류 발생
12078정성태12/13/201921284Linux: 26. .NET Core 응용 프로그램을 위한 메모리 덤프 방법 [3]
12077정성태12/13/201920386Linux: 25. 자주 실행할 명령어 또는 초기 환경을 "~/.bashrc" 파일에 등록
12076정성태12/12/201918914디버깅 기술: 142. Linux - lldb 환경에서 sos 확장 명령어를 이용한 닷넷 프로세스 디버깅 - 배포 방법에 따른 차이
12075정성태12/11/201919719디버깅 기술: 141. Linux - lldb 환경에서 sos 확장 명령어를 이용한 닷넷 프로세스 디버깅
12074정성태12/10/201919407디버깅 기술: 140. windbg/Visual Studio - 값이 변경된 경우를 위한 정지점(BP) 설정(Data Breakpoint)
12073정성태12/10/201920908Linux: 24. Linux/C# - 실행 파일이 아닌 스크립트 형식의 명령어를 Process.Start로 실행하는 방법
12072정성태12/9/201917688오류 유형: 583. iisreset 수행 시 "No such interface supported" 오류
12071정성태12/9/201921218오류 유형: 582. 리눅스 디스크 공간 부족 및 safemode 부팅 방법
12070정성태12/9/201923132오류 유형: 581. resize2fs: Bad magic number in super-block while trying to open /dev/.../root
12069정성태12/2/201919527디버깅 기술: 139. windbg - x64 덤프 분석 시 메서드의 인자 또는 로컬 변수의 값을 확인하는 방법
12068정성태11/28/201928201디버깅 기술: 138. windbg와 Win32 API로 알아보는 Windows Heap 정보 분석 [3]파일 다운로드2
12067정성태11/27/201919600디버깅 기술: 137. 실제 사례를 통해 Debug Diagnostics 도구가 생성한 닷넷 웹 응용 프로그램의 성능 장애 보고서 설명 [1]파일 다운로드1
12066정성태11/27/201919263디버깅 기술: 136. windbg - C# PInvoke 호출 시 마샬링을 담당하는 함수 분석 - OracleCommand.ExecuteReader에서 OpsSql.Prepare2 PInvoke 호출 분석
12065정성태11/25/201917569디버깅 기술: 135. windbg - C# PInvoke 호출 시 마샬링을 담당하는 함수 분석파일 다운로드1
12064정성태11/25/201920497오류 유형: 580. HTTP Error 500.0/500.33 - ANCM In-Process Handler Load Failure
12063정성태11/21/201919428디버깅 기술: 134. windbg - RtlReportCriticalFailure로부터 parameters 정보 찾는 방법
12062정성태11/21/201918912디버깅 기술: 133. windbg - CoTaskMemFree/FreeCoTaskMem에서 발생한 덤프 분석 사례 - 두 번째 이야기
12061정성태11/20/201919354Windows: 167. CoTaskMemAlloc/CoTaskMemFree과 윈도우 Heap의 관계
... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...