末日都过了,还在用 Ubuntu 18.04? 好吧,我觉得应该有很多人都在用。。。废话少说,上游 syzbot 天天查你,这不 KASAN bug 又来了,先来看看怎么使能 KASAN。

直接 menuconfig 按如下路径愣是找不到:

Kernel hacking -> Memory Debugging

来看下配置说明,在lib/Kconfig.kasan 里:

    bool "KASAN: dynamic memory safety error detector"
    depends on (((HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC) || \
                 (HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS)) && \
                CC_HAS_WORKING_NOSANITIZE_ADDRESS) || \
               HAVE_ARCH_KASAN_HW_TAGS
    depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB)
    select STACKDEPOT_ALWAYS_INIT
    help
     KASAN has three modes:

     1. Generic KASAN (supported by many architectures, enabled with
        CONFIG_KASAN_GENERIC, similar to userspace ASan),
     2. Software Tag-Based KASAN (arm64 only, based on software memory
        tagging, enabled with CONFIG_KASAN_SW_TAGS, similar to userspace
        HWASan), and
     3. Hardware Tag-Based KASAN (arm64 only, based on hardware memory
        tagging, enabled with CONFIG_KASAN_HW_TAGS).

ok, 我这是 x86_64,就看1就行了。也就是:

(HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC) && CC_HAS_WORKING_NOSANITIZE_ADDRESS

搜了下 .config 没有 CC_HAS_WORKING_NOSANITIZE_ADDRESS 这玩意啊,再看说明:

    bool "Generic KASAN"
    depends on HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC
    depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
    select SLUB_DEBUG if SLUB
    select CONSTRUCTORS
    help
      Enables Generic KASAN.

      Requires GCC 8.3.0+ or Clang.

gcc 版本有要求?赶紧 check 下:

tj@u1804:~/$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.

还真是版本问题,可以不用编译安装,已经有打包好的了,具体安装步骤:

  • add source
sudo apt install software-properties-common 
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
  • install
sudo apt install gcc-8
sudo apt install g++-8
  • enable
tj@u1804:~/$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8
update-alternatives: using /usr/bin/gcc-8 to provide /usr/bin/gcc (gcc) in auto mode
  • verfiy
tj@u1804:~/$ gcc --version
gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
  • delete if needed
sudo update-alternatives --remove gcc /usr/bin/gcc-8
````

now, 我们再从头来一次看看。

```config
4814 CONFIG_HAVE_ARCH_KASAN=y
4815 CONFIG_HAVE_ARCH_KASAN_VMALLOC=y
4816 CONFIG_CC_HAS_KASAN_GENERIC=y
4817 CONFIG_CC_HAS_WORKING_NOSANITIZE_ADDRESS=y
4818 # CONFIG_KASAN is not set

menuconfig 看看,果然在那,just 打个*。