2021-01-15 22:18:51 +00:00
|
|
|
{ lib, stdenv, stdenvNoCC, fetchFromGitHub, callPackage, makeWrapper
|
2023-07-15 17:15:38 +00:00
|
|
|
, clang, llvm, gcc, which, libcgroup, python3, perl, gmp
|
|
|
|
, file, wine ? null
|
|
|
|
, cmocka
|
|
|
|
, llvmPackages
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# wine fuzzing is only known to work for win32 binaries, and using a mixture of
|
|
|
|
# 32 and 64-bit libraries ... complicates things, so it's recommended to build
|
|
|
|
# a full 32bit version of this package if you want to do wine fuzzing
|
|
|
|
assert (wine != null) -> (stdenv.targetPlatform.system == "i686-linux");
|
|
|
|
|
|
|
|
let
|
2023-07-15 17:15:38 +00:00
|
|
|
aflplusplus-qemu = callPackage ./qemu.nix { };
|
2020-04-24 23:36:52 +00:00
|
|
|
qemu-exe-name = if stdenv.targetPlatform.system == "x86_64-linux" then "qemu-x86_64"
|
|
|
|
else if stdenv.targetPlatform.system == "i686-linux" then "qemu-i386"
|
|
|
|
else throw "aflplusplus: no support for ${stdenv.targetPlatform.system}!";
|
|
|
|
libdislocator = callPackage ./libdislocator.nix { inherit aflplusplus; };
|
|
|
|
libtokencap = callPackage ./libtokencap.nix { inherit aflplusplus; };
|
2020-05-15 21:57:56 +00:00
|
|
|
aflplusplus = stdenvNoCC.mkDerivation rec {
|
2020-04-24 23:36:52 +00:00
|
|
|
pname = "aflplusplus";
|
2024-01-02 11:29:13 +00:00
|
|
|
version = "4.09c";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
2020-05-15 21:57:56 +00:00
|
|
|
owner = "AFLplusplus";
|
2020-04-24 23:36:52 +00:00
|
|
|
repo = "AFLplusplus";
|
2023-10-09 19:29:22 +00:00
|
|
|
rev = "v${version}";
|
2024-01-02 11:29:13 +00:00
|
|
|
sha256 = "sha256-SQQJpR3+thi4iyrowkOD878nRHNgBJqqUdRFhtqld4k=";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
enableParallelBuilding = true;
|
|
|
|
|
|
|
|
# Note: libcgroup isn't needed for building, just for the afl-cgroup
|
|
|
|
# script.
|
2020-06-18 07:06:33 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper which clang gcc ];
|
2023-07-15 17:15:38 +00:00
|
|
|
buildInputs = [ llvm python3 gmp llvmPackages.bintools ]
|
|
|
|
++ lib.optional (wine != null) python3.pkgs.wrapPython;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
# Flag is already set by package and causes some compiler warnings.
|
|
|
|
# warning: "_FORTIFY_SOURCE" redefined
|
|
|
|
hardeningDisable = [ "fortify" ];
|
2020-05-15 21:57:56 +00:00
|
|
|
|
|
|
|
postPatch = ''
|
2023-07-15 17:15:38 +00:00
|
|
|
# Don't care about this.
|
|
|
|
rm Android.bp
|
|
|
|
|
|
|
|
# Replace the CLANG_BIN variables with the correct path.
|
|
|
|
# Replace "gcc" and friends with full paths in afl-gcc.
|
|
|
|
# Prevents afl-gcc picking up any (possibly incorrect) gcc from the path.
|
|
|
|
# Replace LLVM_BINDIR with a non-existing path to give a hard error when it's used.
|
|
|
|
substituteInPlace src/afl-cc.c \
|
2020-06-18 07:06:33 +00:00
|
|
|
--replace "CLANGPP_BIN" '"${clang}/bin/clang++"' \
|
|
|
|
--replace "CLANG_BIN" '"${clang}/bin/clang"' \
|
2020-05-15 21:57:56 +00:00
|
|
|
--replace '"gcc"' '"${gcc}/bin/gcc"' \
|
|
|
|
--replace '"g++"' '"${gcc}/bin/g++"' \
|
2023-07-15 17:15:38 +00:00
|
|
|
--replace 'getenv("AFL_PATH")' "(getenv(\"AFL_PATH\") ? getenv(\"AFL_PATH\") : \"$out/lib/afl\")"
|
|
|
|
|
|
|
|
substituteInPlace src/afl-ld-lto.c \
|
|
|
|
--replace 'LLVM_BINDIR' '"/nixpkgs-patched-does-not-exist"'
|
|
|
|
|
|
|
|
# Remove the rest of the line
|
|
|
|
sed -i 's|LLVM_BINDIR = .*|LLVM_BINDIR = |' utils/aflpp_driver/GNUmakefile
|
|
|
|
substituteInPlace utils/aflpp_driver/GNUmakefile \
|
|
|
|
--replace 'LLVM_BINDIR = ' 'LLVM_BINDIR = ${clang}/bin/'
|
|
|
|
|
|
|
|
substituteInPlace GNUmakefile.llvm \
|
|
|
|
--replace "\$(LLVM_BINDIR)/clang" "${clang}/bin/clang"
|
2020-05-15 21:57:56 +00:00
|
|
|
'';
|
|
|
|
|
2023-03-04 12:14:45 +00:00
|
|
|
env.NIX_CFLAGS_COMPILE = toString [
|
2023-02-16 17:41:37 +00:00
|
|
|
# Needed with GCC 12
|
|
|
|
"-Wno-error=use-after-free"
|
|
|
|
];
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
makeFlags = [
|
|
|
|
"PREFIX=$(out)"
|
|
|
|
"USE_BINDIR=0"
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
buildPhase = ''
|
2023-07-15 17:15:38 +00:00
|
|
|
runHook preBuild
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
common="$makeFlags -j$NIX_BUILD_CORES"
|
2023-07-15 17:15:38 +00:00
|
|
|
make distrib $common
|
2020-04-24 23:36:52 +00:00
|
|
|
make -C qemu_mode/libcompcov $common
|
|
|
|
make -C qemu_mode/unsigaction $common
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
runHook postBuild
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
postInstall = ''
|
2020-05-15 21:57:56 +00:00
|
|
|
# remove afl-clang(++) which are just symlinks to afl-clang-fast
|
|
|
|
rm $out/bin/afl-clang $out/bin/afl-clang++
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# the makefile neglects to install unsigaction
|
|
|
|
cp qemu_mode/unsigaction/unsigaction*.so $out/lib/afl/
|
|
|
|
|
|
|
|
# Install the custom QEMU emulator for binary blob fuzzing.
|
2023-07-15 17:15:38 +00:00
|
|
|
ln -s ${aflplusplus-qemu}/bin/${qemu-exe-name} $out/bin/afl-qemu-trace
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# give user a convenient way of accessing libcompconv.so, libdislocator.so, libtokencap.so
|
|
|
|
cat > $out/bin/get-afl-qemu-libcompcov-so <<END
|
|
|
|
#!${stdenv.shell}
|
|
|
|
echo $out/lib/afl/libcompcov.so
|
|
|
|
END
|
|
|
|
chmod +x $out/bin/get-afl-qemu-libcompcov-so
|
2023-07-15 17:15:38 +00:00
|
|
|
ln -s ${libdislocator}/bin/get-libdislocator-so $out/bin/
|
|
|
|
ln -s ${libtokencap}/bin/get-libtokencap-so $out/bin/
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Install the cgroups wrapper for asan-based fuzzing.
|
2023-07-15 17:15:38 +00:00
|
|
|
cp utils/asan_cgroups/limit_memory.sh $out/bin/afl-cgroup
|
2020-04-24 23:36:52 +00:00
|
|
|
chmod +x $out/bin/afl-cgroup
|
|
|
|
substituteInPlace $out/bin/afl-cgroup \
|
|
|
|
--replace "cgcreate" "${libcgroup}/bin/cgcreate" \
|
|
|
|
--replace "cgexec" "${libcgroup}/bin/cgexec" \
|
|
|
|
--replace "cgdelete" "${libcgroup}/bin/cgdelete"
|
|
|
|
|
|
|
|
patchShebangs $out/bin
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
'' + lib.optionalString (wine != null) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
substitute afl-wine-trace $out/bin/afl-wine-trace \
|
|
|
|
--replace "qemu_mode/unsigaction" "$out/lib/afl"
|
|
|
|
chmod +x $out/bin/afl-wine-trace
|
|
|
|
|
|
|
|
# qemu needs to be fed ELFs, not wrapper scripts, so we have to cheat a bit if we
|
|
|
|
# detect a wrapped wine
|
|
|
|
for winePath in ${wine}/bin/.wine ${wine}/bin/wine; do
|
|
|
|
if [ -x $winePath ]; then break; fi
|
|
|
|
done
|
|
|
|
makeWrapperArgs="--set-default 'AFL_WINE_PATH' '$winePath'" \
|
2023-07-15 17:15:38 +00:00
|
|
|
wrapPythonProgramsIn $out/bin ${python3.pkgs.pefile}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
nativeInstallCheckInputs = [ perl file cmocka ];
|
2020-04-24 23:36:52 +00:00
|
|
|
doInstallCheck = true;
|
|
|
|
installCheckPhase = ''
|
2023-07-15 17:15:38 +00:00
|
|
|
runHook preInstallCheck
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# replace references to tools in build directory with references to installed locations
|
2023-07-15 17:15:38 +00:00
|
|
|
substituteInPlace test/test-qemu-mode.sh \
|
2020-04-24 23:36:52 +00:00
|
|
|
--replace '../libcompcov.so' '`$out/bin/get-afl-qemu-libcompcov-so`' \
|
2023-07-15 17:15:38 +00:00
|
|
|
--replace '../afl-qemu-trace' '$out/bin/afl-qemu-trace' \
|
|
|
|
--replace '../afl-fuzz' '$out/bin/afl-fuzz' \
|
|
|
|
--replace '../qemu_mode/unsigaction/unsigaction32.so' '$out/lib/afl/unsigaction32.so' \
|
|
|
|
--replace '../qemu_mode/unsigaction/unsigaction64.so' '$out/lib/afl/unsigaction64.so'
|
|
|
|
|
|
|
|
substituteInPlace test/test-libextensions.sh \
|
2020-04-24 23:36:52 +00:00
|
|
|
--replace '../libdislocator.so' '`$out/bin/get-libdislocator-so`' \
|
|
|
|
--replace '../libtokencap.so' '`$out/bin/get-libtokencap-so`'
|
2023-07-15 17:15:38 +00:00
|
|
|
substituteInPlace test/test-llvm.sh \
|
|
|
|
--replace '../afl-cmin.bash' '`$out/bin/afl-cmin.bash`'
|
|
|
|
# perl -pi -e 's|(?<!\.)(?<!-I)(\.\./)([^\s\/]+?)(?<!\.c)(?<!\.s?o)(?=\s)|\$out/bin/\2|g' test/test.sh
|
|
|
|
patchShebangs .
|
|
|
|
cd test && ./test-all.sh
|
|
|
|
|
|
|
|
runHook postInstallCheck
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
passthru = {
|
|
|
|
inherit libdislocator libtokencap;
|
|
|
|
qemu = aflplusplus-qemu;
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
description = ''
|
2020-11-24 20:58:05 +00:00
|
|
|
A heavily enhanced version of AFL, incorporating many features
|
|
|
|
and improvements from the community
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2020-05-15 21:57:56 +00:00
|
|
|
homepage = "https://aflplus.plus";
|
2021-01-15 22:18:51 +00:00
|
|
|
license = lib.licenses.asl20;
|
2020-04-24 23:36:52 +00:00
|
|
|
platforms = ["x86_64-linux" "i686-linux"];
|
2021-01-15 22:18:51 +00:00
|
|
|
maintainers = with lib.maintainers; [ ris mindavi ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in aflplusplus
|