git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #53802 from delroth/kernel-randstruct

Use a deterministic seed for kernel RANDSTRUCT, and re-enable in hardened kernels

commit11cd6aeb0c731941410c901d5d5d95e43e92c8d5 authorJoachim F <joachifm@users.noreply.github.com> dateFri 11 Jan 2019, 14:49 (8 years ago) parentc1a1310b26c824
3 files changed+34−1
pkgs/os-specific/linux/kernel/hardened-config.nix+5 pkgs/os-specific/linux/kernel/manual-config.nix+17−1 pkgs/os-specific/linux/kernel/randstruct-provide-seed.patch+12
pkgs/os-specific/linux/kernel/hardened-config.nix+5
@@ -125,6 +125,11 @@ ${optionalString (versionAtLeast version "4.20") ''
125125 GCC_PLUGIN_STACKLEAK y # A port of the PaX stackleak plugin
126126''}
127127
128+${optionalString (versionAtLeast version "4.13") ''
129+ GCC_PLUGIN_RANDSTRUCT y # A port of the PaX randstruct plugin
130+ GCC_PLUGIN_RANDSTRUCT_PERFORMANCE y
131+''}
132+
128133# Disable various dangerous settings
129134ACPI_CUSTOM_METHOD n # Allows writing directly to physical memory
130135PROC_KCORE n # Exposes kernel text image layout
pkgs/os-specific/linux/kernel/manual-config.nix+17−1
@@ -88,7 +88,10 @@ let
8888
8989 inherit src;
9090
91− patches = map (p: p.patch) kernelPatches;
91+ patches =
92+ map (p: p.patch) kernelPatches
93+ # Required for deterministic builds along with some postPatch magic.
94+ ++ optional (stdenv.lib.versionAtLeast version "4.13") ./randstruct-provide-seed.patch;
9295
9396 prePatch = ''
9497 for mf in $(find -name Makefile -o -name Makefile.include -o -name install.sh); do
@@ -99,6 +102,19 @@ let
99102 sed -i scripts/ld-version.sh -e "s|/usr/bin/awk|${buildPackages.gawk}/bin/awk|"
100103 '';
101104
105+ postPatch = ''
106+ # Set randstruct seed to a deterministic but diversified value. Note:
107+ # we could have instead patched gen-random-seed.sh to take input from
108+ # the buildFlags, but that would require also patching the kernel's
109+ # toplevel Makefile to add a variable export. This would be likely to
110+ # cause future patch conflicts.
111+ if [ -f scripts/gcc-plugins/gen-random-seed.sh ]; then
112+ substituteInPlace scripts/gcc-plugins/gen-random-seed.sh \
113+ --replace NIXOS_RANDSTRUCT_SEED \
114+ $(echo ${src} ${configfile} | sha256sum | cut -d ' ' -f 1 | tr -d '\n')
115+ fi
116+ '';
117+
102118 configurePhase = ''
103119 runHook preConfigure
104120
pkgs/os-specific/linux/kernel/randstruct-provide-seed.patchnew file+12
@@ -0,0 +1,12 @@
1+diff -ru a/scripts/gcc-plugins/gen-random-seed.sh b/scripts/gcc-plugins/gen-random-seed.sh
2+--- a/scripts/gcc-plugins/gen-random-seed.sh 2019-01-11 11:50:29.228258920 +0100
3++++ b/scripts/gcc-plugins/gen-random-seed.sh 2019-01-11 12:18:33.555902720 +0100
4+@@ -2,7 +2,7 @@
5+ # SPDX-License-Identifier: GPL-2.0
6+
7+ if [ ! -f "$1" ]; then
8+- SEED=`od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n'`
9++ SEED="NIXOS_RANDSTRUCT_SEED"
10+ echo "const char *randstruct_seed = \"$SEED\";" > "$1"
11+ HASH=`echo -n "$SEED" | sha256sum | cut -d" " -f1 | tr -d ' \n'`
12+ echo "#define RANDSTRUCT_HASHED_SEED \"$HASH\"" > "$2"