Linux内核内存检测工具KASan

一、简介

KASan,全称Kernel Address Sanitizer,它是一个动态检测内存错误的工具,主要功能是检查内存越界访问和使用已释放的内存等问题。KASan 集成在 Linux 内核中,随 Linux 内核代码一起发布,并由内核社区维护和发展。本文简要介绍 KASan 的原理及使用方法。

二、原理与使用
2.1 KASan原理

KASan利用额外的内存标记可用内存的状态,这部分额外的内存被称作shadow memory(影子区)。在我们Linux内核中有page结构体(页描述符),用来标识物理内存页,每个4KB的页面都需要一个page结构体来进行描述,除此之外还有大量的用来描述各类结构的结构体,这些用于描述各类结构的结构体会占用一定量的内存。同样的KASan也使用额外的内存来对内存使用进行标记(即内存的合法检测以内存空间作为代价)。

KASan将1/8的内存用作shadow memory,例如一个128G的内存,那么需要16G的内存用来作为影子区。KANsan使用特殊的magic num填充shadow memory,在每一次load/store内存的时候检测对应的shadow memory确定操作是否valid。连续8 bytes内存(8 bytes align)使用1 byte shadow memory标记。

在这里插入图片描述
在这里插入图片描述
图片来自:http://www.wowotech.net/memory_management/424.html

从上面可以看出,当有几个byte出现错误时,shadow memory的标志位中使用一个byte的数据来标识了8个byte中出错的个数。当没有错误时该标识的byte值为0~

2.2 配置KASan

KASan 是内核的一部分,使用时需要重新配置、编译并安装内核。KASan 在 Linux 内核 4.0 版本时被引入内核,所以选择的内核代码需要高于 4.0 版本。另外,最基本的 KASan 功能需要 GCC4.9.2 支持,更多的支持则需要 GCC5.0 及以上版本。

要是用KASan就重新配置和编译内核~

找一个版本的内核,我找到了一个linux-4.6.4就直接使用了,使用下列命令进行配置:

make menuconfig

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
通过前面三步,把KASan选上~

继续进入Instrumentation type里面~
在这里插入图片描述
选择Inline instrumentation,这个默认是Outline instrumentation~
在这里插入图片描述
至于这个为什么这么选,可以看:https://www.kernel.org/doc/html/latest/dev-tools/kasan.html

Outline and inline are compiler instrumentation types.
Software tag-based KASAN also has two instrumentation modes (outline, that emits callbacks to check memory accesses; and inline, that performs the shadow memory checks inline). With outline instrumentation mode, a bug report is simply printed from the function that performs the access check. With inline instrumentation a brk instruction is emitted by the compiler, and a dedicated brk handler is used to print bug reports.

选择好后,保存退出,并开始编译内核~

编译内核可以参考: https://blog.csdn.net/SweeNeil/article/details/83684565

2.3 KASan的使用

Linux 内核的源码中已经包含了针对 KASan 的测试代码,其位置在 linux/lib/test_kasan.c。编译内核或者单独编译 lib 模块的时候,会生成 test_kasan.ko 模块。</

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是更全面的 Linux 内核 Debug 技术的推荐,共100个: 1. printk 2. kdb 3. kgdb 4. kdump 5. kexec 6. ftrace 7. SystemTap 8. LTTng 9. perf 10. oprofile 11. kprobes 12. dynamic-debug 13. kcov 14. kasan 15. kmemleak 16. kcsan 17. ktsan 18. kasan-runtime 19. kretprobe 20. uprobes 21. trace-cmd 22. debugfs 23. sysfs 24. procfs 25. kernelshark 26. crash 27. memtest86+ 28. valgrind 29. gdb 30. strace 31. eBPF 32. bcc 33. stapbpf 34. kpatch 35. ksplice 36. kprobes on tracepoints 37. LKDTM 38. syzkaller 39. fuzzing 40. static code analysis 41. clang analyzer 42. Coverity 43. Infer 44. sparse 45. smatch 46. coccinelle 47. gcc plugins 48. kernelbuild 49. kernelci 50. kernelci-dashboard 51. kbuild test 52. kselftest 53. kunit 54. ktest 55. kasan-tests 56. kmemleak-tests 57. kcsan-tests 58. ktsan-tests 59. kretprobe-tests 60. uprobes-tests 61. ftrace-tests 62. LTTng-tests 63. perf-tests 64. oprofile-tests 65. kprobes-tests 66. dynamic-debug-tests 67. kcov-tests 68. kasan-tests 69. kmemleak-tests 70. kcsan-tests 71. ktsan-tests 72. kasan-runtime-tests 73. kretprobe-tests 74. uprobes-tests 75. trace-cmd-tests 76. debugfs-tests 77. sysfs-tests 78. procfs-tests 79. kernelshark-tests 80. crash-tests 81. memtest86+-tests 82. valgrind-tests 83. gdb-tests 84. strace-tests 85. LKDTM-tests 86. syzkaller-tests 87. fuzzing-tests 88. static code analysis tests 89. clang analyzer tests 90. Coverity tests 91. Infer tests 92. sparse tests 93. smatch tests 94. coccinelle tests 95. gcc plugins tests 96. kpatch-tests 97. ksplice-tests 98. kernelboot 99. bootchart 100. boot validation 这些技术在 Linux 内核 Debug 中都有不同的应用和用途,可以根据具体情况选择使用
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值