2025년 3월 27일부터 AOSP를 빌드하고 기여하려면 aosp-main
대신 android-latest-release
를 사용하는 것이 좋습니다. 자세한 내용은 AOSP 변경사항을 참고하세요.
Kernel Address Sanitizer
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
LLVM 기반의 사용자 공간 구성요소 새니타이저와 마찬가지로 Android에는 Kernel Address Sanitizer (KASan)가 포함되어 있습니다. KASan은 커널 및 컴파일 시간 수정 기능을 결합하여 버그 탐색 및 근본 원인 분석을 단순화하는 계측 시스템을 제공합니다.
KASan은 커널에서 여러 유형의 메모리 위반을 감지할 수 있습니다. 또한 스택, 힙 및 전역 변수에서의 범위를 벗어난 읽기 및 쓰기와 use-after-free 및 double free를 감지할 수 있습니다.
ASan과 마찬가지로 KASan은 런타임 시 메모리 액세스를 추적하기 위해 컴파일 시점의 메모리 함수 계측과 섀도 메모리의 조합을 사용합니다. KASan에서 커널 메모리 공간의 8분의 1은 메모리 액세스가 유효한지 아닌지를 결정하는 섀도 메모리 전용입니다.
KASan은 x86_64 및 arm64 아키텍처에서 지원됩니다. 4.0부터 업스트림 커널의 일부였으며 Android 3.18 기반 커널로 백포트되었습니다.
KASAN 외에도 kcov는 테스트에 유용한 또 다른 커널 수정 기능입니다. kcov는 커널에서 커버리지 기반 퍼징 테스트를 지원하기 위해 개발되었습니다. 이는 syscall 입력 값으로 커버리지를 측정하며 syzkaller와 같은 퍼징 시스템에서 유용합니다.
구현
KASAN 및 kcov가 사용 설정된 커널을 컴파일하려면 커널 빌드 구성에 다음 빌드 플래그를 추가합니다.
CONFIG_KASAN
CONFIG_KASAN_INLINE
CONFIG_TEST_KASAN
CONFIG_KCOV
CONFIG_SLUB
CONFIG_SLUB_DEBUG
CONFIG_CC_OPTIMIZE_FOR_SIZE
그리고 다음을 삭제합니다.
CONFIG_SLUB_DEBUG_ON
CONFIG_SLUB_DEBUG_PANIC_ON
CONFIG_KASAN_OUTLINE
CONFIG_KERNEL_LZ4
그런 다음 평소와 같이 커널을 빌드하고 플래시합니다. KASan 커널은 원래 커널보다 훨씬 큽니다. 필요한 경우 부트 매개변수와 부트로더 설정을 수정합니다.
커널을 플래시한 후 커널 부팅 로그를 확인하여 KASan이 사용 설정되어 실행 중인지 확인합니다. 커널은 다음과 같이 KASAN의 메모리 맵 정보로 시작합니다.
...
[ 0.000000] c0 0 Virtual kernel memory layout:
[ 0.000000] c0 0 kasan : 0xffffff8000000000 - 0xffffff9000000000 ( 64 GB)
[ 0.000000] c0 0 vmalloc : 0xffffff9000010000 - 0xffffffbdbfff0000 ( 182 GB)
[ 0.000000] c0 0 vmemmap : 0xffffffbdc0000000 - 0xffffffbfc0000000 ( 8 GB maximum)
[ 0.000000] c0 0 0xffffffbdc0000000 - 0xffffffbdc3f95400 ( 63 MB actual)
[ 0.000000] c0 0 PCI I/O : 0xffffffbffa000000 - 0xffffffbffb000000 ( 16 MB)
[ 0.000000] c0 0 fixed : 0xffffffbffbdfd000 - 0xffffffbffbdff000 ( 8 KB)
[ 0.000000] c0 0 modules : 0xffffffbffc000000 - 0xffffffc000000000 ( 64 MB)
[ 0.000000] c0 0 memory : 0xffffffc000000000 - 0xffffffc0fe550000 ( 4069 MB)
[ 0.000000] c0 0 .init : 0xffffffc001d33000 - 0xffffffc001dce000 ( 620 KB)
[ 0.000000] c0 0 .text : 0xffffffc000080000 - 0xffffffc001d32284 ( 29385 KB)
...
그리고 버그가 다음과 같이 표시됩니다.
[ 18.539668] c3 1 ==================================================================
[ 18.547662] c3 1 BUG: KASAN: null-ptr-deref on address 0000000000000008
[ 18.554689] c3 1 Read of size 8 by task swapper/0/1
[ 18.559988] c3 1 CPU: 3 PID: 1 Comm: swapper/0 Tainted: G W 3.18.24-xxx #1
[ 18.569275] c3 1 Hardware name: Android Device
[ 18.577433] c3 1 Call trace:
[ 18.580739] c3 1 [<ffffffc00008b32c>] dump_backtrace+0x0/0x2c4
[ 18.586985] c3 1 [<ffffffc00008b600>] show_stack+0x10/0x1c
[ 18.592889] c3 1 [<ffffffc001481194>] dump_stack+0x74/0xc8
[ 18.598792] c3 1 [<ffffffc000202ee0>] kasan_report+0x11c/0x4d0
[ 18.605038] c3 1 [<ffffffc00020286c>] __asan_load8+0x20/0x80
[ 18.611115] c3 1 [<ffffffc000bdefe8>] android_verity_ctr+0x8cc/0x1024
[ 18.617976] c3 1 [<ffffffc000bcaa2c>] dm_table_add_target+0x3dc/0x50c
[ 18.624832] c3 1 [<ffffffc001bdbe60>] dm_run_setup+0x50c/0x678
[ 18.631082] c3 1 [<ffffffc001bda8c0>] prepare_namespace+0x44/0x1ac
[ 18.637676] c3 1 [<ffffffc001bda170>] kernel_init_freeable+0x328/0x364
[ 18.644625] c3 1 [<ffffffc001478e20>] kernel_init+0x10/0xd8
[ 18.650613] c3 1 ==================================================================
또한 커널에서 모듈을 사용 설정하는 경우 test_kasan 커널 모듈을 로드하여 추가로 테스트할 수 있습니다. 모듈은 범위를 벗어난 메모리 액세스 및 use-after-free를 시도하며, 대상 기기에서 KASan을 올바르게 사용 설정하는 데 유용합니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Kernel Address Sanitizer\n\nSimilar to the LLVM-based sanitizers for userspace components, Android includes\nthe Kernel Address Sanitizer (KASan). KASan is a combination of kernel and\ncompile time modifications that result in an instrumented system that allows for\nsimpler bug discovery and root cause analysis.\n\n\nKASan can detect many types of memory violations in the kernel. It can also\ndetect out-of-bound reads and writes on stack, heap and global variables, and\ncan detect use-after-free and double frees.\n\n\nSimilar to ASan, KASan uses a combination of memory-function instrumentation at\ncompile time and shadow memory to track memory accesses at runtime. In KASan, an\neighth of the kernel memory space is dedicated to shadow memory, which\ndetermines if a memory access is valid or not.\n\n\nKASan is supported on x86_64 and arm64 architectures. It has been part of the\nupstream kernel since 4.0, and has been backported to Android 3.18-based\nkernels.\n\n\nIn addition to KASan, kcov is another kernel modification that is useful for\ntesting. kcov was developed to allow for coverage-guided fuzz testing in the\nkernel. It measures coverage in terms of syscall inputs and is useful with\nfuzzing systems, such as [syzkaller](https://github.com/google/syzkaller).\n\nImplementation\n--------------\n\n\nTo compile a kernel with KASan and kcov enabled, add the following build flags\nto your kernel build configuration: \n\n```\nCONFIG_KASAN\nCONFIG_KASAN_INLINE\nCONFIG_TEST_KASAN\nCONFIG_KCOV\nCONFIG_SLUB\nCONFIG_SLUB_DEBUG\nCONFIG_CC_OPTIMIZE_FOR_SIZE\n```\n\n\nAnd removing the following: \n\n```\nCONFIG_SLUB_DEBUG_ON\nCONFIG_SLUB_DEBUG_PANIC_ON\nCONFIG_KASAN_OUTLINE\nCONFIG_KERNEL_LZ4\n```\n\n\nThen build and flash your kernel as usual. The KASan kernel is considerably\nlarger than the original. If needed, modify any boot parameters and\nbootloader settings to take this into consideration.\n\n\nAfter flashing the kernel, check the kernel boot logs to see if KASan is enabled\nand running. The kernel will start up with memory map information for KASan,\nsuch as: \n\n```\n...\n[ 0.000000] c0 0 Virtual kernel memory layout:\n[ 0.000000] c0 0 kasan : 0xffffff8000000000 - 0xffffff9000000000 ( 64 GB)\n[ 0.000000] c0 0 vmalloc : 0xffffff9000010000 - 0xffffffbdbfff0000 ( 182 GB)\n[ 0.000000] c0 0 vmemmap : 0xffffffbdc0000000 - 0xffffffbfc0000000 ( 8 GB maximum)\n[ 0.000000] c0 0 0xffffffbdc0000000 - 0xffffffbdc3f95400 ( 63 MB actual)\n[ 0.000000] c0 0 PCI I/O : 0xffffffbffa000000 - 0xffffffbffb000000 ( 16 MB)\n[ 0.000000] c0 0 fixed : 0xffffffbffbdfd000 - 0xffffffbffbdff000 ( 8 KB)\n[ 0.000000] c0 0 modules : 0xffffffbffc000000 - 0xffffffc000000000 ( 64 MB)\n[ 0.000000] c0 0 memory : 0xffffffc000000000 - 0xffffffc0fe550000 ( 4069 MB)\n[ 0.000000] c0 0 .init : 0xffffffc001d33000 - 0xffffffc001dce000 ( 620 KB)\n[ 0.000000] c0 0 .text : 0xffffffc000080000 - 0xffffffc001d32284 ( 29385 KB)\n...\n```\n\n\nAnd this is how a bug will look: \n\n```\n[ 18.539668] c3 1 ==================================================================\n[ 18.547662] c3 1 BUG: KASAN: null-ptr-deref on address 0000000000000008\n[ 18.554689] c3 1 Read of size 8 by task swapper/0/1\n[ 18.559988] c3 1 CPU: 3 PID: 1 Comm: swapper/0 Tainted: G W 3.18.24-xxx #1\n[ 18.569275] c3 1 Hardware name: Android Device\n[ 18.577433] c3 1 Call trace:\n[ 18.580739] c3 1 [\u003cffffffc00008b32c\u003e] dump_backtrace+0x0/0x2c4\n[ 18.586985] c3 1 [\u003cffffffc00008b600\u003e] show_stack+0x10/0x1c\n[ 18.592889] c3 1 [\u003cffffffc001481194\u003e] dump_stack+0x74/0xc8\n[ 18.598792] c3 1 [\u003cffffffc000202ee0\u003e] kasan_report+0x11c/0x4d0\n[ 18.605038] c3 1 [\u003cffffffc00020286c\u003e] __asan_load8+0x20/0x80\n[ 18.611115] c3 1 [\u003cffffffc000bdefe8\u003e] android_verity_ctr+0x8cc/0x1024\n[ 18.617976] c3 1 [\u003cffffffc000bcaa2c\u003e] dm_table_add_target+0x3dc/0x50c\n[ 18.624832] c3 1 [\u003cffffffc001bdbe60\u003e] dm_run_setup+0x50c/0x678\n[ 18.631082] c3 1 [\u003cffffffc001bda8c0\u003e] prepare_namespace+0x44/0x1ac\n[ 18.637676] c3 1 [\u003cffffffc001bda170\u003e] kernel_init_freeable+0x328/0x364\n[ 18.644625] c3 1 [\u003cffffffc001478e20\u003e] kernel_init+0x10/0xd8\n[ 18.650613] c3 1 ==================================================================\n```\n\n\nIn addition, if modules are enabled in your kernel, you can load the test_kasan\nkernel module for further testing. The module attempts out-of-bounds memory\naccesses and use-after-free and is useful for ensuring you correctly enabled KASan\non a target device."]]