2019-04-23 22:16:22 +02:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.environment.memoryAllocator;
|
|
|
|
|
|
|
|
|
|
# The set of alternative malloc(3) providers.
|
|
|
|
|
providers = {
|
2019-08-13 23:52:01 +02:00
|
|
|
|
graphene-hardened = {
|
2019-04-23 22:16:22 +02:00
|
|
|
|
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.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-13 23:52:01 +02:00
|
|
|
|
jemalloc = {
|
2019-04-23 22:16:22 +02:00
|
|
|
|
libPath = "${pkgs.jemalloc}/lib/libjemalloc.so";
|
|
|
|
|
description = ''
|
|
|
|
|
A general purpose allocator that emphasizes fragmentation avoidance
|
|
|
|
|
and scalable concurrency support.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2019-05-29 19:16:45 +02:00
|
|
|
|
|
2021-10-29 20:56:17 +02:00
|
|
|
|
scudo = let
|
|
|
|
|
platformMap = {
|
|
|
|
|
aarch64-linux = "aarch64";
|
|
|
|
|
x86_64-linux = "x86_64";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
systemPlatform = platformMap.${pkgs.stdenv.hostPlatform.system} or (throw "scudo not supported on ${pkgs.stdenv.hostPlatform.system}");
|
|
|
|
|
in {
|
|
|
|
|
libPath = "${pkgs.llvmPackages_latest.compiler-rt}/lib/linux/libclang_rt.scudo-${systemPlatform}.so";
|
2019-05-29 19:16:45 +02:00
|
|
|
|
description = ''
|
|
|
|
|
A user-mode allocator based on LLVM Sanitizer’s CombinedAllocator,
|
|
|
|
|
which aims at providing additional mitigations against heap based
|
|
|
|
|
vulnerabilities, while maintaining good performance.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2021-09-20 19:10:28 +02:00
|
|
|
|
|
|
|
|
|
mimalloc = {
|
|
|
|
|
libPath = "${pkgs.mimalloc}/lib/libmimalloc.so";
|
|
|
|
|
description = ''
|
|
|
|
|
A compact and fast general purpose allocator, which may
|
|
|
|
|
optionally be built with mitigations against various heap
|
|
|
|
|
vulnerabilities.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2019-04-23 22:16:22 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-08-13 23:52:01 +02:00
|
|
|
|
providerConf = providers.${cfg.provider};
|
2019-04-23 22:16:22 +02:00
|
|
|
|
|
|
|
|
|
# An output that contains only the shared library, to avoid
|
|
|
|
|
# needlessly bloating the system closure
|
|
|
|
|
mallocLib = pkgs.runCommand "malloc-provider-${cfg.provider}"
|
|
|
|
|
rec {
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
allowSubstitutes = false;
|
|
|
|
|
origLibPath = providerConf.libPath;
|
|
|
|
|
libName = baseNameOf origLibPath;
|
|
|
|
|
}
|
|
|
|
|
''
|
|
|
|
|
mkdir -p $out/lib
|
|
|
|
|
cp -L $origLibPath $out/lib/$libName
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
# The full path to the selected provider shlib.
|
|
|
|
|
providerLibPath = "${mallocLib}/lib/${mallocLib.libName}";
|
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
meta = {
|
|
|
|
|
maintainers = [ maintainers.joachifm ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
environment.memoryAllocator.provider = mkOption {
|
|
|
|
|
type = types.enum ([ "libc" ] ++ attrNames providers);
|
|
|
|
|
default = "libc";
|
2022-08-29 23:34:22 +02:00
|
|
|
|
description = lib.mdDoc ''
|
2019-04-23 22:16:22 +02:00
|
|
|
|
The system-wide memory allocator.
|
|
|
|
|
|
|
|
|
|
Briefly, the system-wide memory allocator providers are:
|
2022-08-29 23:34:22 +02:00
|
|
|
|
|
|
|
|
|
- `libc`: the standard allocator provided by libc
|
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList
|
|
|
|
|
(name: value: "- `${name}`: ${replaceStrings [ "\n" ] [ " " ] value.description}")
|
2019-04-23 22:16:22 +02:00
|
|
|
|
providers)}
|
|
|
|
|
|
2022-08-29 23:34:22 +02:00
|
|
|
|
::: {.warning}
|
2019-04-23 22:16:22 +02:00
|
|
|
|
Selecting an alternative allocator (i.e., anything other than
|
2022-08-29 23:34:22 +02:00
|
|
|
|
`libc`) may result in instability, data loss,
|
2019-04-23 22:16:22 +02:00
|
|
|
|
and/or service failure.
|
2022-08-29 23:34:22 +02:00
|
|
|
|
:::
|
2019-04-23 22:16:22 +02:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkIf (cfg.provider != "libc") {
|
2019-06-02 16:03:26 +02:00
|
|
|
|
environment.etc."ld-nix.so.preload".text = ''
|
|
|
|
|
${providerLibPath}
|
|
|
|
|
'';
|
2020-10-18 15:36:24 +02:00
|
|
|
|
security.apparmor.includes = {
|
|
|
|
|
"abstractions/base" = ''
|
|
|
|
|
r /etc/ld-nix.so.preload,
|
|
|
|
|
r ${config.environment.etc."ld-nix.so.preload".source},
|
2021-06-06 17:30:45 +02:00
|
|
|
|
include "${pkgs.apparmorRulesFromClosure {
|
|
|
|
|
name = "mallocLib";
|
|
|
|
|
baseRules = ["mr $path/lib/**.so*"];
|
|
|
|
|
} [ mallocLib ] }"
|
2020-10-18 15:36:24 +02:00
|
|
|
|
'';
|
|
|
|
|
};
|
2019-04-23 22:16:22 +02:00
|
|
|
|
};
|
|
|
|
|
}
|