2014-05-14 18:04:40 +01:00
|
|
|
{ grsecOptions, lib, pkgs }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = {
|
2016-02-28 03:10:59 +00:00
|
|
|
kernelPatch = grsecOptions.kernelPatch;
|
2014-05-14 18:04:40 +01:00
|
|
|
config = {
|
|
|
|
mode = "auto";
|
|
|
|
sysctl = false;
|
|
|
|
denyChrootChmod = false;
|
2014-11-11 12:29:42 +00:00
|
|
|
denyUSB = false;
|
2014-05-14 18:04:40 +01:00
|
|
|
restrictProc = false;
|
|
|
|
restrictProcWithGroup = true;
|
|
|
|
unrestrictProcGid = 121; # Ugh, an awful hack. See grsecurity NixOS gid
|
|
|
|
disableRBAC = false;
|
|
|
|
verboseVersion = false;
|
|
|
|
kernelExtraConfig = "";
|
|
|
|
} // grsecOptions.config;
|
|
|
|
};
|
|
|
|
|
|
|
|
vals = rec {
|
|
|
|
|
2016-02-27 15:33:18 +00:00
|
|
|
mkKernel = patch:
|
|
|
|
{
|
|
|
|
inherit patch;
|
2016-02-27 18:54:55 +00:00
|
|
|
inherit (patch) kernel patches grversion revision;
|
2014-05-14 18:04:40 +01:00
|
|
|
};
|
|
|
|
|
2016-02-28 03:10:59 +00:00
|
|
|
grKernel = mkKernel cfg.kernelPatch;
|
2014-05-14 18:04:40 +01:00
|
|
|
|
|
|
|
## -- grsecurity configuration ---------------------------------------------
|
|
|
|
|
|
|
|
grsecPrioCfg =
|
|
|
|
if cfg.config.priority == "security" then
|
|
|
|
"GRKERNSEC_CONFIG_PRIORITY_SECURITY y"
|
|
|
|
else
|
|
|
|
"GRKERNSEC_CONFIG_PRIORITY_PERF y";
|
|
|
|
|
|
|
|
grsecSystemCfg =
|
|
|
|
if cfg.config.system == "desktop" then
|
|
|
|
"GRKERNSEC_CONFIG_DESKTOP y"
|
|
|
|
else
|
|
|
|
"GRKERNSEC_CONFIG_SERVER y";
|
|
|
|
|
|
|
|
grsecVirtCfg =
|
2015-04-03 11:46:21 +01:00
|
|
|
if cfg.config.virtualisationConfig == null then
|
2014-05-14 18:04:40 +01:00
|
|
|
"GRKERNSEC_CONFIG_VIRT_NONE y"
|
|
|
|
else if cfg.config.virtualisationConfig == "host" then
|
|
|
|
"GRKERNSEC_CONFIG_VIRT_HOST y"
|
|
|
|
else
|
|
|
|
"GRKERNSEC_CONFIG_VIRT_GUEST y";
|
|
|
|
|
2015-04-03 11:46:21 +01:00
|
|
|
grsecHwvirtCfg = if cfg.config.virtualisationConfig == null then "" else
|
2014-05-14 18:04:40 +01:00
|
|
|
if cfg.config.hardwareVirtualisation == true then
|
|
|
|
"GRKERNSEC_CONFIG_VIRT_EPT y"
|
|
|
|
else
|
|
|
|
"GRKERNSEC_CONFIG_VIRT_SOFT y";
|
|
|
|
|
|
|
|
grsecVirtswCfg =
|
|
|
|
let virtCfg = opt: "GRKERNSEC_CONFIG_VIRT_"+opt+" y";
|
|
|
|
in
|
2015-04-03 11:46:21 +01:00
|
|
|
if cfg.config.virtualisationConfig == null then ""
|
2014-05-14 18:04:40 +01:00
|
|
|
else if cfg.config.virtualisationSoftware == "xen" then virtCfg "XEN"
|
|
|
|
else if cfg.config.virtualisationSoftware == "kvm" then virtCfg "KVM"
|
|
|
|
else if cfg.config.virtualisationSoftware == "vmware" then virtCfg "VMWARE"
|
|
|
|
else virtCfg "VIRTUALBOX";
|
|
|
|
|
|
|
|
grsecMainConfig = if cfg.config.mode == "custom" then "" else ''
|
|
|
|
GRKERNSEC_CONFIG_AUTO y
|
|
|
|
${grsecPrioCfg}
|
|
|
|
${grsecSystemCfg}
|
|
|
|
${grsecVirtCfg}
|
|
|
|
${grsecHwvirtCfg}
|
|
|
|
${grsecVirtswCfg}
|
|
|
|
'';
|
|
|
|
|
|
|
|
grsecConfig =
|
|
|
|
let boolToKernOpt = b: if b then "y" else "n";
|
|
|
|
# Disable RANDSTRUCT under virtualbox, as it has some kind of
|
|
|
|
# breakage with the vbox guest drivers
|
2015-09-02 03:54:31 +01:00
|
|
|
#randstruct = optionalString config.virtualisation.virtualbox.guest.enable
|
2014-05-14 18:04:40 +01:00
|
|
|
# "GRKERNSEC_RANDSTRUCT n";
|
|
|
|
|
|
|
|
# Disable restricting links under the testing kernel, as something
|
|
|
|
# has changed causing it to fail miserably during boot.
|
2016-02-28 03:10:59 +00:00
|
|
|
#restrictLinks = optionalString cfg.testing
|
|
|
|
# "GRKERNSEC_LINK n";
|
2014-05-14 18:04:40 +01:00
|
|
|
in ''
|
|
|
|
GRKERNSEC y
|
|
|
|
${grsecMainConfig}
|
|
|
|
|
|
|
|
${if cfg.config.restrictProc then
|
|
|
|
"GRKERNSEC_PROC_USER y"
|
|
|
|
else
|
|
|
|
optionalString cfg.config.restrictProcWithGroup ''
|
|
|
|
GRKERNSEC_PROC_USERGROUP y
|
|
|
|
GRKERNSEC_PROC_GID ${toString cfg.config.unrestrictProcGid}
|
|
|
|
''
|
|
|
|
}
|
|
|
|
|
|
|
|
GRKERNSEC_SYSCTL ${boolToKernOpt cfg.config.sysctl}
|
|
|
|
GRKERNSEC_CHROOT_CHMOD ${boolToKernOpt cfg.config.denyChrootChmod}
|
2014-11-11 12:29:42 +00:00
|
|
|
GRKERNSEC_DENYUSB ${boolToKernOpt cfg.config.denyUSB}
|
2014-05-14 18:04:40 +01:00
|
|
|
GRKERNSEC_NO_RBAC ${boolToKernOpt cfg.config.disableRBAC}
|
|
|
|
|
|
|
|
${cfg.config.kernelExtraConfig}
|
|
|
|
'';
|
|
|
|
|
|
|
|
## -- grsecurity kernel packages -------------------------------------------
|
|
|
|
|
|
|
|
localver = grkern:
|
|
|
|
"-grsec" + optionalString cfg.config.verboseVersion
|
|
|
|
"-${grkern.grversion}-${grkern.revision}";
|
|
|
|
|
|
|
|
grsecurityOverrider = args: grkern: {
|
|
|
|
# Apparently as of gcc 4.6, gcc-plugin headers (which are needed by PaX plugins)
|
|
|
|
# include libgmp headers, so we need these extra tweaks
|
|
|
|
buildInputs = args.buildInputs ++ [ pkgs.gmp ];
|
|
|
|
preConfigure = ''
|
|
|
|
${args.preConfigure or ""}
|
|
|
|
sed -i 's|-I|-I${pkgs.gmp}/include -I|' scripts/gcc-plugin.sh
|
|
|
|
sed -i 's|HOST_EXTRACFLAGS +=|HOST_EXTRACFLAGS += -I${pkgs.gmp}/include|' tools/gcc/Makefile
|
|
|
|
sed -i 's|HOST_EXTRACXXFLAGS +=|HOST_EXTRACXXFLAGS += -I${pkgs.gmp}/include|' tools/gcc/Makefile
|
|
|
|
rm localversion-grsec
|
|
|
|
echo ${localver grkern} > localversion-grsec
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mkGrsecKern = grkern:
|
|
|
|
lowPrio (overrideDerivation (grkern.kernel.override (args: {
|
2016-02-27 18:54:55 +00:00
|
|
|
kernelPatches = args.kernelPatches ++ [ grkern.patch ] ++ grkern.patches;
|
2014-05-14 18:04:40 +01:00
|
|
|
argsOverride = {
|
|
|
|
modDirVersion = "${grkern.kernel.modDirVersion}${localver grkern}";
|
|
|
|
};
|
|
|
|
extraConfig = grsecConfig;
|
|
|
|
features.grsecurity = true;
|
2016-01-23 16:58:35 +00:00
|
|
|
ignoreConfigErrors = true; # Too lazy to model the config options that work with grsecurity and don't for now
|
2014-05-14 18:04:40 +01:00
|
|
|
})) (args: grsecurityOverrider args grkern));
|
|
|
|
|
|
|
|
mkGrsecPkg = grkern: pkgs.linuxPackagesFor grkern (mkGrsecPkg grkern);
|
|
|
|
|
|
|
|
## -- Kernel packages ------------------------------------------------------
|
|
|
|
|
2014-05-17 22:37:12 +01:00
|
|
|
grsecKernel = mkGrsecKern grKernel;
|
2014-05-14 18:04:40 +01:00
|
|
|
grsecPackage = mkGrsecPkg grsecKernel;
|
|
|
|
};
|
|
|
|
in vals
|