Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphene-hardened-malloc: migrate to by-name, build light variant #266540

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions nixos/modules/config/malloc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ let
graphene-hardened = {
libPath = "${pkgs.graphene-hardened-malloc}/lib/libhardened_malloc.so";
description = ''
An allocator designed to mitigate memory corruption attacks, such as
those caused by use-after-free bugs.
Hardened memory allocator coming from GrapheneOS project.
The default configuration template has all normal optional security
features enabled and is quite aggressive in terms of sacrificing
performance and memory usage for security.
'';
};

graphene-hardened-light = {
surfaceflinger marked this conversation as resolved.
Show resolved Hide resolved
libPath = "${pkgs.graphene-hardened-malloc}/lib/libhardened_malloc-light.so";
description = ''
Hardened memory allocator coming from GrapheneOS project.
The light configuration template disables the slab quarantines,
write after free check, slot randomization and raises the guard
slab interval from 1 to 8 but leaves zero-on-free and slab canaries enabled.
The light configuration has solid performance and memory usage while still
being far more secure than mainstream allocators with much better security
properties.
'';
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
{ lib
, stdenv
, fetchFromGitHub
{ fetchFromGitHub
, lib
, makeWrapper
, python3
, runCommand
, makeWrapper
, stdenv
, stress-ng
}:

stdenv.mkDerivation (finalAttrs: {
pname = "graphene-hardened-malloc";
version = "12";
version = "2024040900";

src = fetchFromGitHub {
owner = "GrapheneOS";
repo = "hardened_malloc";
rev = finalAttrs.version;
sha256 = "sha256-ujwzr4njNsf/VTyEq7zKHWxoivU3feavSTx+MLIj1ZM=";
sha256 = "sha256-1j7xzhuhK8ZRAJm9dJ95xiTIla7lh3LBiWc/+x/kjp0=";
};

doCheck = true;
nativeCheckInputs = [ python3 ];
# these tests cover use as a build-time-linked library
checkTarget = "test";
doCheck = true;

buildPhase = ''
runHook preBuild

for VARIANT in default light; do make $makeFlags ''${enableParallelBuilding:+-j$NIX_BUILD_CORES} VARIANT=$VARIANT; done

runHook postBuild
'';
Comment on lines +26 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use makeTargets instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my tinkering, looks like both makeTargets and makeFlags won't work with how hardened_malloc's Makefile handles VARIANT.

From what I experimented with, we have 2 variants left:

  • let mkMalloc = variant: stdenv.mkDerivation {}; in { graphene-hardened-malloc = mkMalloc "default"; graphene-hardened-malloc-light = mkMalloc "light"; }
    but this will create graphene-hardened-malloc.default and graphene-hardened-malloc.light + I don't like how these let blabla = stdenv.mkDerivation in look 🙄
  • we can introduce an overridable variant attribute like mimalloc package and then override it in the module.


installPhase = ''
runHook preInstall

install -Dm444 -t $out/include include/*
install -Dm444 -t $out/lib out/libhardened_malloc.so
install -Dm444 -t $out/lib out/libhardened_malloc.so out-light/libhardened_malloc-light.so

mkdir -p $out/bin
substitute preload.sh $out/bin/preload-hardened-malloc --replace "\$dir" $out/lib
chmod 0555 $out/bin/preload-hardened-malloc

runHook postInstall
'';

separateDebugInfo = true;
Expand Down
2 changes: 0 additions & 2 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18977,8 +18977,6 @@ with pkgs;

grail = callPackage ../development/libraries/grail { };

graphene-hardened-malloc = callPackage ../development/libraries/graphene-hardened-malloc { };

graphene = callPackage ../development/libraries/graphene { };

griffe = with python3Packages; toPythonApplication griffe;
Expand Down