| 1 |
First you need to install afl++. If your distro doesn't package it, you can |
| 2 |
get it from https://github.com/AFLplusplus/AFLplusplus |
| 3 |
|
| 4 |
Next build and install afl++, this needs llvm/clang installed. |
| 5 |
|
| 6 |
Nftables configue + compile steps: |
| 7 |
|
| 8 |
To get the best results, build nftables with the following options: |
| 9 |
|
| 10 |
CC=afl-clang-lto LD=afl-clang-lto CFLAGS+=-fsanitize=address ./configure \ |
| 11 |
--disable-shared --with-json --without-xtables \ |
| 12 |
--with-cli=readline --enable-fuzzer --disable-man-doc |
| 13 |
|
| 14 |
[ you might want to enable xtables or use a different cli, your choice ]. |
| 15 |
|
| 16 |
Important options are: |
| 17 |
--disable-shared, so that libnftables is instrumented too. |
| 18 |
--enable-fuzzer |
| 19 |
|
| 20 |
--enable-fuzzer provides tools/nft-afl that allows more fine-grained control |
| 21 |
nft-afl provides a few options to guide the fuzzing process, some are shared |
| 22 |
with nft binary: |
| 23 |
|
| 24 |
--check |
| 25 |
|
| 26 |
Prevents the fuzzer-generated rulesets from being committed to the kernel. |
| 27 |
|
| 28 |
--random-outflags |
| 29 |
|
| 30 |
Periodically alter output behaviour by enabling or disabling --terse, |
| 31 |
--numeric, --echo and so on. |
| 32 |
|
| 33 |
--json |
| 34 |
|
| 35 |
Format output in JSON |
| 36 |
|
| 37 |
--fuzzer <stage> |
| 38 |
|
| 39 |
Instruct nft-afl to stop fuzzing after reaching the given stage. Stages are: |
| 40 |
|
| 41 |
1: 'parser': |
| 42 |
Only run / exercise the flex/bison parser. |
| 43 |
|
| 44 |
2: 'eval': stop after the evaluation phase. |
| 45 |
This attempts to build a complete ruleset in memory, does |
| 46 |
symbol resolution, adds needed shift/masks to payload instructions |
| 47 |
etc. |
| 48 |
|
| 49 |
3: 'netlink-ro': |
| 50 |
Also build/serialize the ruleset into netlink-commands to send to the |
| 51 |
kernel, but omit the final write so the kernel will not see the message. |
| 52 |
This is the default mode. |
| 53 |
|
| 54 |
4: 'netlink-rw': |
| 55 |
Same as 3 but the message will be sent to the kernel. |
| 56 |
You can combine this option with the '--check' option to send data to the |
| 57 |
kernel but without committing any changes. |
| 58 |
Unlike 3), even when combined with '--check', this option can still trigger |
| 59 |
a kernel crash if there are bugs in the kernel, e.g. during the |
| 60 |
valiation / transaction / abort stages. |
| 61 |
When using this without '--check', remember to lauch nft in its own network |
| 62 |
namespace to prevent VM connectivity loss due to committed 'drop' rules. |
| 63 |
|
| 64 |
Use 'netlink-ro' if you want to prevent nft from ever submitting any |
| 65 |
changes to the kernel or if you are only interested in fuzzing nftables |
| 66 |
and its libraries. |
| 67 |
|
| 68 |
All --fuzzer modes EXCEPT 'netlink-rw' do imply --check as these modes never |
| 69 |
alter state in the kernel. |
| 70 |
|
| 71 |
Before each input, nft-afl checks the kernel "taint" status as provided |
| 72 |
by "/proc/sys/kernel/tainted". If this is non-zero, fuzzing stops. |
| 73 |
|
| 74 |
To run libnftables under afl++, run nft-afl like this: |
| 75 |
|
| 76 |
unshare -n \ |
| 77 |
afl-fuzz -g 16 -G 2000 -t 5000 -i tests/afl++/in -o tests/afl++/out \ |
| 78 |
-- tools/nft-afl |
| 79 |
|
| 80 |
arg should be either "netlink-ro" (if you only want to exercise libnftables |
| 81 |
userspace) or "netlink-rw" (if you want to test kernel code paths too). |
| 82 |
|
| 83 |
Its also a good idea to do this from tmux/screen so you can disconnect/reattach |
| 84 |
later. You can also spawn multiple instances. |
| 85 |
In that case, add the '-M' option to afl-fuzz for the first instance you start, |
| 86 |
and '-S' for subsequent secondary instances. |
| 87 |
|
| 88 |
This expects a unique directory name as argument, so interesting findings |
| 89 |
from the different instances are cleary separated. |
| 90 |
|
| 91 |
With above default options, outputs will be in 'tests/afl++/out/<variantname>'. |
| 92 |
Please see the afl++ docs for more information about this. |
| 93 |
|
| 94 |
You can use tests/afl++/run-afl.sh script to autogenerate an initial set of valid |
| 95 |
inputs that the fuzzer can start from. |
| 96 |
|
| 97 |
Use |
| 98 |
|
| 99 |
sysctl -f tests/afl++/afl-sysctl.conf |
| 100 |
|
| 101 |
to enable some fuzzer-beneficial sysctl options. |
| 102 |
|
| 103 |
Kernel config: |
| 104 |
When using the 'netlink-rw' option it is best to also use a debug kernel |
| 105 |
with at least: |
| 106 |
|
| 107 |
# CONFIG_NOTIFIER_ERROR_INJECTION is not set |
| 108 |
CONFIG_KASAN=y |
| 109 |
CONFIG_DEBUG_ATOMIC_SLEEP=y |
| 110 |
CONFIG_DEBUG_LOCKDEP=y |
| 111 |
CONFIG_FAULT_INJECTION=y |
| 112 |
CONFIG_FAILSLAB=y |
| 113 |
CONFIG_DEBUG_KMEMLEAK=y |
| 114 |
|
| 115 |
If you want to sample test coverage, then set |
| 116 |
CONFIG_GCOV_KERNEL=y |
| 117 |
|
| 118 |
echo GCOV_PROFILE := y > net/netfilter/Makefile |
| 119 |
|
| 120 |
or enable CONFIG_GCOV_PROFILE_ALL=y. |