本文为看雪论坛优秀文章
看雪论坛作者ID:ZxyNull
一
OSS-fuzz
homepage: "https://github.com/containerd/containerd"
main_repo: "https://github.com/containerd/containerd"
primary_contact: "[email protected]"
auto_ccs :
- "[email protected]"
language: go
fuzzing_engines:
- libfuzzer
sanitizers:
- address
homepage: 项目地址
main_repo:托管代码的源代码存储库的路径
laguange: 项目编写的编程语言
primary_contact, auto_css: 联系人列表
fuzzing_engines: 模糊测试所使用的引擎,如afl,libfuzzer
sanitizer: 消毒剂,支持ASAN 和MSAN ,可以有效的提高模糊测试发现crash的概率
architectures: 架构列表
...
FROM gcr.io/oss-fuzz-base/base-builder-go
RUN apt-get update && apt-get install -y btrfs-progs libc-dev pkg-config libseccomp-dev gcc wget libbtrfs-dev
RUN git clone --depth 1 https://github.com/containerd/containerd
RUN git clone --depth 1 https://github.com/cncf/cncf-fuzzing
COPY build.sh $SRC/
WORKDIR $SRC/containerd
FROM: 规定了项目的基本镜像
RUN:构建镜像时执行的命令,首先下载了一些必须的软件和库,之后下载了项目源码
COPY: 将build.sh复制到镜像中
WORKDIR :指定工作目录
...
#!/bin/bash -eu
./buildconf.sh
# configure scripts usually use correct environment variables.
./configure
make clean
make -j$(nproc) all
$CXX $CXXFLAGS -std=c++11 -Ilib/ \
$SRC/parse_fuzzer.cc -o $OUT/parse_fuzzer \
$LIB_FUZZING_ENGINE .libs/libexpat.a
cp $SRC/*.dict $SRC/*.options $OUT/
$OUT ->/out:用来存储构建好的文件
$SRC -> /src: 放源文件的位置
$WORK -> work: 存储中间文件的位置
二
环境准备
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=https://127.0.0.1:7890"
Environment="NO_PROXY=127.0.0.1"
systemctl daemon-reload
systemctl restart docker
systemctl show docker --property Environment
FROM gcr.io/oss-fuzz-base/base-builder-go
RUN apt-get update && apt-get install -y btrfs-progs libc-dev pkg-config libseccomp-dev gcc wget libbtrfs-dev
ENV HTTP_PROXY "http://192.168.xx.xx:7890"
ENV HTTPS_PROXY "http://192.168.xx.xx:7890"
RUN git clone https://github.com/containerd/containerd
WORKDIR containerd
RUN git checkout -b remotes/origin/release/1.6 remotes/origin/release/1.6
WORKDIR $SRC
RUN git clone --depth 1 https://github.com/cncf/cncf-fuzzing
COPY build.sh $SRC/
WORKDIR $SRC/containerd
三
构建Harness进行模糊测试
cd /path/to/oss-fuzz
python infra/helper.py build_image containerd
python infra/helper.py build_fuzzers containerd
package fuzz
import (
fuzz "github.com/AdaLogics/go-fuzz-headers"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
"github.com/containerd/containerd/pkg/cri/server"
)
func FuzzParseAuth(data []byte) int {
f := fuzz.NewConsumer(data)
auth := &runtime.AuthConfig{}
err := f.GenerateStruct(auth)
if err != nil {
return 0
}
host, err := f.GetString()
if err != nil {
return 0
}
_, _, _ = server.ParseAuth(auth, host)
return 1
}
python infra/helper.py run_fuzzer --corpus-dir=./build/out/containerd/corpus containerd fuzz_image_store
runtime: unexpected return pc for runtime.gopark called from 0x0
stack: frame={sp:0x10c000078f40, fp:0x10c000078f60} stack=[0x10c000078000,0x10c000079000)
0x000010c000078e40: 0x0000000000000000 0x0000000000000000
0x000010c000078e50: 0x0000000000000000 0x0000000000000000
0x000010c000078e60: 0x7a75662f706d742f 0x3833303039332d7a
0x000010c000078e70: 0x39332d7a7a75662f 0x3430383538333030
0x000010c000078e80: 0xdef0995b8d5812aa 0x758f15f0dcd67525
0x000010c000078e90: 0xee5d5b00aa1475d6 0x1d3fd1a2d44b0579
0x000010c000078ea0: 0x0000000000000000 0x0000000000000000
0x000010c000078eb0: 0x0000006901000000 0x0000000000000000
0x000010c000078ec0: 0x0000000000000000 0x0000000000000000
0x000010c000078ed0: 0x0000000000070000 0x0000000000000000
0x000010c000078ee0: 0xffffffffffffffff 0x00ffffffffffffff
0x000010c000078ef0: 0x000010c0001ddb80 0x000010c0005a3600
0x000010c000078f00: 0x000010c0004651e0 0x000010c000465340
0x000010c000078f10: 0x000010c0001dc420 0x000010c0003920e0
0x000010c000078f20: 0x000010c0001948d0 0x000010c0001906b0
0x000010c000078f30: 0x000010c000582f90 0x000010c000582fd0
0x000010c000078f40: <0x000010c0005837d0 0x000010c000190590
0x000010c000078f50: 0x0000000000000000 !0x0000000000000000
0x000010c000078f60: >0x000093f73283d9b8 0x000010c0001282c0
0x000010c000078f70: 0x0000000000001418 0x0000000000000000
0x000010c000078f80: 0x0000000000000000 0x0000000000000000
0x000010c000078f90: 0x00000a8c46505853 0x0000000000000207
0x000010c000078fa0: 0x0000000000000a88 0x0000000000000000
0x000010c000078fb0: 0x0000000000000000 0x0000000000000000
0x000010c000078fc0: 0x0000000000000203 0x0000000000000000
0x000010c000078fd0: 0x0000000000000000 0x0000000000000000
0x000010c000078fe0: 0x0000000000000000 0x0000000000000000
0x000010c000078ff0: 0x0000000000000000 0x0000000000000000
fatal error: unknown caller pc
runtime stack:
runtime.throw({0x1f3f3fb, 0x328e0e0})
runtime/panic.go:1198 +0x71
runtime.gentraceback(0x7f220a620c90, 0x1, 0x0, 0x7f220a620b30, 0x0, 0x0, 0x7fffffff, 0x7f220a620c90, 0x0, 0x0)
runtime/traceback.go:274 +0x1956
runtime.scanstack(0x10c000001ba0, 0x10c000051698)
runtime/mgcmark.go:748 +0x197
runtime.markroot.func1()
runtime/mgcmark.go:232 +0xb1
runtime.markroot(0x10c000051698, 0x1f)
runtime/mgcmark.go:205 +0x170
runtime.gcDrain(0x10c000051698, 0x3)
runtime/mgcmark.go:1013 +0x379
runtime.gcBgMarkWorker.func2()
runtime/mgc.go:1269 +0xa5
runtime.systemstack()
runtime/asm_amd64.s:383 +0x46
goroutine 6 [GC worker (idle)]:
runtime.systemstack_switch()
runtime/asm_amd64.s:350 fp=0x10c00006af60 sp=0x10c00006af58 pc=0x5c4a20
runtime.gcBgMarkWorker()
runtime/mgc.go:1256 +0x1b3 fp=0x10c00006afe0 sp=0x10c00006af60 pc=0x5790b3
runtime.goexit()
runtime/asm_amd64.s:1581 +0x1 fp=0x10c00006afe8 sp=0x10c00006afe0 pc=0x5c6cc1
created by runtime.gcBgMarkStartWorkers
runtime/mgc.go:1124 +0x25
goroutine 17 [runnable, locked to thread]:
runtime.goexit()
runtime/asm_amd64.s:1581 +0x1
goroutine 7 [chan receive]:
k8s.io/klog/v2.(*loggingT).flushDaemon(0x0)
k8s.io/klog/[email protected].30.0/klog.go:1181 +0x8b
created by k8s.io/klog/v2.init.0
k8s.io/klog/[email protected].30.0/klog.go:420 +0x115
AddressSanitizer:DEADLYSIGNAL
=================================================================
==12==ERROR: AddressSanitizer: ABRT on unknown address 0x00000000000c (pc 0x0000005c85e1 bp 0x7f220a620678 sp 0x7f220a620660 T8)
SCARINESS: 10 (signal)
#0 0x5c85e1 in runtime.raise.abi0 runtime/sys_linux_amd64.s:165
#1 0x5aa097 in runtime.crash runtime/signal_unix.go:861
#2 0x593c70 in runtime.fatalthrow.func1 runtime/panic.go:1257
#3 0x593bef in runtime.fatalthrow runtime/panic.go:1250
#4 0x5939b0 in runtime.throw runtime/panic.go:1198
#5 0x5b9975 in runtime.gentraceback runtime/traceback.go:274
#6 0x57b856 in runtime.scanstack runtime/mgcmark.go:748
#7 0x57a790 in runtime.markroot.func1 runtime/mgcmark.go:232
#8 0x57a54f in runtime.markroot runtime/mgcmark.go:205
#9 0x57c3b8 in runtime.gcDrain runtime/mgcmark.go:1013
#10 0x579404 in runtime.gcBgMarkWorker.func2 runtime/mgc.go:1269
#11 0x5c4a85 in runtime.systemstack.abi0 runtime/asm_amd64.s:383
DEDUP_TOKEN: runtime.raise.abi0--runtime.crash--runtime.fatalthrow.func1
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: ABRT runtime/sys_linux_amd64.s:165 in runtime.raise.abi0
Thread T8 created by T3 here:
#0 0x50d32c in __interceptor_pthread_create /src/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:207:3
#1 0x55d070 in _cgo_try_pthread_create /_/runtime/cgo/gcc_libinit.c:100:9
#2 0x599d86 in runtime.newm runtime/proc.go:2230
#3 0x59a46e in runtime.startm runtime/proc.go:2485
#4 0x59a999 in runtime.wakep runtime/proc.go:2584
#5 0x59c164 in runtime.resetspinning runtime/proc.go:3216
#6 0x59c71d in runtime.schedule runtime/proc.go:3374
#7 0x59cc4c in runtime.park_m runtime/proc.go:3516
#8 0x5c4a04 in runtime.mcall runtime/asm_amd64.s:307
DEDUP_TOKEN: __interceptor_pthread_create--_cgo_try_pthread_create--runtime.newm
Thread T3 created by T1 here:
#0 0x50d32c in __interceptor_pthread_create /src/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:207:3
#1 0x55d070 in _cgo_try_pthread_create /_/runtime/cgo/gcc_libinit.c:100:9
#2 0x599d86 in runtime.newm runtime/proc.go:2230
#3 0x59a46e in runtime.startm runtime/proc.go:2485
#4 0x59a999 in runtime.wakep runtime/proc.go:2584
#5 0x59e897 in runtime.newproc.func1 runtime/proc.go:4261
#6 0x5c4a85 in runtime.systemstack.abi0 runtime/asm_amd64.s:383
DEDUP_TOKEN: __interceptor_pthread_create--_cgo_try_pthread_create--runtime.newm
Thread T1 created by T0 here:
#0 0x50d32c in __interceptor_pthread_create /src/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:207:3
#1 0x55cfbf in _cgo_try_pthread_create /_/runtime/cgo/gcc_libinit.c:100:9
#2 0x55cfbf in x_cgo_sys_thread_create /_/runtime/cgo/gcc_libinit.c:27:12
#3 0x1f0cb0c in __libc_csu_init (/out/fuzz_image_store+0x1f0cb0c)
DEDUP_TOKEN: __interceptor_pthread_create--_cgo_try_pthread_create--x_cgo_sys_thread_create
==12==ABORTING
MS: 2 EraseBytes-ChangeBinInt-; base unit: feb33bf726c50d41c5dc2c8cea890cb18040c1f8
0x10,0xd,0xb,0x3b,0x2,0x0,0x0,0x0,0x0,0x0,0x84,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x8,0x0,0x0,0x0,0x0,0x0,0x3,0xfa,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
\020\015\013;\002\000\000\000\000\000\204\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\010\000\000\000\000\000\003\372\000\000\000\000\000\000\000\000\000\000\000\000
artifact_prefix='./'; Test unit written to ./crash-66c182f8f6dac7209a14e631d117b0879331cbfe
Base64: EA0LOwIAAAAAAIQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAD+gAAAAAAAAAAAAAAAA==
go-fuzz-header:
https://github.com/AdaLogics/go-fuzz-headers
https://adalogics.com/blog/structure-aware-go-fuzzing-complex-types
oss-fuzz教程
https://n0va-scy.github.io/2022/02/14/oss-fuzz%E5%88%9D%E6%8E%A2/
https://github.com/google/oss-fuzz/blob/master/docs/getting-started/new_project_guide.md
containerd
https://github.com/containerd/containerd/blob/11de19af68c7d21c8fe01058026257ecd5d6ed13/contrib/fuzz/oss_fuzz_build.sh
看雪ID:ZxyNull
https://bbs.pediy.com/user-home-921173.htm
# 往期推荐
球分享
球点赞
球在看
点击“阅读原文”,了解更多!