2023-11-11 17:50:39 +01:00
|
|
|
{ fetchFromGitHub
|
|
|
|
, lib
|
|
|
|
, makeWrapper
|
2022-01-30 16:54:48 +01:00
|
|
|
, python3
|
|
|
|
, runCommand
|
2023-11-11 17:50:39 +01:00
|
|
|
, stdenv
|
2022-01-30 16:54:48 +01:00
|
|
|
, stress-ng
|
|
|
|
}:
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2023-06-20 18:57:15 +02:00
|
|
|
stdenv.mkDerivation (finalAttrs: {
|
2019-08-15 14:41:18 +02:00
|
|
|
pname = "graphene-hardened-malloc";
|
2024-04-18 23:18:41 +02:00
|
|
|
version = "2024040900";
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2022-01-04 16:37:07 +01:00
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "GrapheneOS";
|
|
|
|
repo = "hardened_malloc";
|
2023-06-20 18:57:15 +02:00
|
|
|
rev = finalAttrs.version;
|
2024-04-18 23:18:41 +02:00
|
|
|
sha256 = "sha256-1j7xzhuhK8ZRAJm9dJ95xiTIla7lh3LBiWc/+x/kjp0=";
|
2019-04-07 23:05:22 +02:00
|
|
|
};
|
|
|
|
|
2023-01-21 13:00:00 +01:00
|
|
|
nativeCheckInputs = [ python3 ];
|
2021-08-11 23:35:27 +02:00
|
|
|
# these tests cover use as a build-time-linked library
|
2023-06-20 18:57:15 +02:00
|
|
|
checkTarget = "test";
|
2023-11-11 17:50:39 +01:00
|
|
|
doCheck = true;
|
|
|
|
|
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
|
|
|
|
|
|
|
for VARIANT in default light; do make $makeFlags ''${enableParallelBuilding:+-j$NIX_BUILD_CORES} VARIANT=$VARIANT; done
|
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
2021-08-11 23:35:27 +02:00
|
|
|
|
2019-04-07 23:05:22 +02:00
|
|
|
installPhase = ''
|
2023-11-11 17:50:39 +01:00
|
|
|
runHook preInstall
|
|
|
|
|
2021-08-11 23:35:27 +02:00
|
|
|
install -Dm444 -t $out/include include/*
|
2023-11-11 17:50:39 +01:00
|
|
|
install -Dm444 -t $out/lib out/libhardened_malloc.so out-light/libhardened_malloc-light.so
|
2019-04-07 23:05:22 +02:00
|
|
|
|
|
|
|
mkdir -p $out/bin
|
|
|
|
substitute preload.sh $out/bin/preload-hardened-malloc --replace "\$dir" $out/lib
|
|
|
|
chmod 0555 $out/bin/preload-hardened-malloc
|
2023-11-11 17:50:39 +01:00
|
|
|
|
|
|
|
runHook postInstall
|
2019-04-07 23:05:22 +02:00
|
|
|
'';
|
|
|
|
|
2019-07-18 14:15:33 +02:00
|
|
|
separateDebugInfo = true;
|
|
|
|
|
2021-08-11 23:35:27 +02:00
|
|
|
passthru = {
|
|
|
|
ld-preload-tests = stdenv.mkDerivation {
|
2023-06-20 18:57:15 +02:00
|
|
|
name = "${finalAttrs.pname}-ld-preload-tests";
|
|
|
|
inherit (finalAttrs) src;
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2021-08-11 23:35:27 +02:00
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2021-08-11 23:35:27 +02:00
|
|
|
# reuse the projects tests to cover use with LD_PRELOAD. we have
|
|
|
|
# to convince the test programs to build as though they're naive
|
|
|
|
# standalone executables. this includes disabling tests for
|
|
|
|
# malloc_object_size, which doesn't make sense to use via LD_PRELOAD.
|
|
|
|
buildPhase = ''
|
2022-01-30 16:54:48 +01:00
|
|
|
pushd test
|
2021-08-11 23:35:27 +02:00
|
|
|
make LDLIBS= LDFLAGS=-Wl,--unresolved-symbols=ignore-all CXXFLAGS=-lstdc++
|
|
|
|
substituteInPlace test_smc.py \
|
|
|
|
--replace 'test_malloc_object_size' 'dont_test_malloc_object_size' \
|
|
|
|
--replace 'test_invalid_malloc_object_size' 'dont_test_invalid_malloc_object_size'
|
2022-01-30 16:54:48 +01:00
|
|
|
popd # test
|
2021-08-11 23:35:27 +02:00
|
|
|
'';
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2021-08-11 23:35:27 +02:00
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/test
|
2022-01-30 16:54:48 +01:00
|
|
|
cp -r test $out/test
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2021-08-11 23:35:27 +02:00
|
|
|
mkdir -p $out/bin
|
|
|
|
makeWrapper ${python3.interpreter} $out/bin/run-tests \
|
2022-01-30 16:54:48 +01:00
|
|
|
--add-flags "-I -m unittest discover --start-directory $out/test"
|
2021-08-11 23:35:27 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
tests = {
|
2023-06-20 18:57:15 +02:00
|
|
|
ld-preload = runCommand "ld-preload-test-run" { } ''
|
|
|
|
${finalAttrs.finalPackage}/bin/preload-hardened-malloc ${finalAttrs.passthru.ld-preload-tests}/bin/run-tests
|
2021-08-11 23:35:27 +02:00
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
# to compensate for the lack of tests of correct normal malloc operation
|
2023-06-20 18:57:15 +02:00
|
|
|
stress = runCommand "stress-test-run" { } ''
|
|
|
|
${finalAttrs.finalPackage}/bin/preload-hardened-malloc ${stress-ng}/bin/stress-ng \
|
2021-08-11 23:35:27 +02:00
|
|
|
--no-rand-seed \
|
|
|
|
--malloc 8 \
|
|
|
|
--malloc-ops 1000000 \
|
|
|
|
--verify
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2019-04-07 23:05:22 +02:00
|
|
|
|
2021-01-21 18:00:13 +01:00
|
|
|
meta = with lib; {
|
2020-04-01 03:11:51 +02:00
|
|
|
homepage = "https://github.com/GrapheneOS/hardened_malloc";
|
2019-04-07 23:05:22 +02:00
|
|
|
description = "Hardened allocator designed for modern systems";
|
2024-03-19 03:14:51 +01:00
|
|
|
mainProgram = "preload-hardened-malloc";
|
2019-04-07 23:05:22 +02:00
|
|
|
longDescription = ''
|
|
|
|
This is a security-focused general purpose memory allocator providing the malloc API
|
|
|
|
along with various extensions. It provides substantial hardening against heap
|
|
|
|
corruption vulnerabilities yet aims to provide decent overall performance.
|
|
|
|
'';
|
|
|
|
license = licenses.mit;
|
|
|
|
maintainers = with maintainers; [ ris ];
|
2020-04-08 22:02:25 +02:00
|
|
|
platforms = [ "x86_64-linux" "aarch64-linux" ];
|
2019-04-07 23:05:22 +02:00
|
|
|
};
|
2021-08-11 23:35:27 +02:00
|
|
|
})
|