git.iko.soy/typeable/nixpkgs
on master help sign in
nixos: rewrite the grsecurity module

The new module is specifically adapted to the NixOS Grsecurity/PaX kernel. The module declares the required kernel configurations and so *should* be somewhat compatible with custom Grsecurity kernels. The module exposes only a limited number of options, minimising the need for user intervention beyond enabling the module. For experts, Grsecurity/PaX behavior may be configured via `boot.kernelParams` and `boot.kernel.sysctl`. The module assumes the user knows what she's doing (esp. if she decides to modify configuration values not directly exposed by the module). Administration of Grsecurity's role based access control system is yet to be implemented.

commit0677cc61c8fae0b699a9be837c897b8d7b6d837c authorJoachim Fasting <joachifm@fastmail.fm> dateThu 9 Jun 2016, 20:12 (10 years ago) parent75b9a7b
2 files changed+113−283
nixos/modules/rename.nix+20 nixos/modules/security/grsecurity.nix+93−283
nixos/modules/rename.nix+20
@@ -114,6 +114,26 @@ with lib;
114114 (mkRenamedOptionModule [ "services" "iodined" "extraConfig" ] [ "services" "iodine" "server" "extraConfig" ])
115115 (mkRemovedOptionModule [ "services" "iodined" "client" ])
116116
117+ # Grsecurity
118+ (mkRemovedOptionModule [ "security" "grsecurity" "kernelPatch" ])
119+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "mode" ])
120+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "priority" ])
121+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "system" ])
122+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "virtualisationConfig" ])
123+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "hardwareVirtualisation" ])
124+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "virtualisationSoftware" ])
125+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "sysctl" ])
126+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "denyChrootChmod" ])
127+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "denyChrootCaps" ])
128+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "denyUSB" ])
129+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "restrictProc" ])
130+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "restrictProcWithGroup" ])
131+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "unrestrictProcGid" ])
132+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "disableRBAC" ])
133+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "disableSimultConnect" ])
134+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "verboseVersion" ])
135+ (mkRemovedOptionModule [ "security" "grsecurity" "config" "kernelExtraConfig" ])
136+
117137 # Options that are obsolete and have no replacement.
118138 (mkRemovedOptionModule [ "boot" "initrd" "luks" "enable" ])
119139 (mkRemovedOptionModule [ "programs" "bash" "enable" ])
nixos/modules/security/grsecurity.nix+93−283
@@ -1,312 +1,122 @@
1−{ config, lib, pkgs, ... }:
1+{ config, pkgs, lib, ... }:
22
33with lib;
44
55let
66 cfg = config.security.grsecurity;
7−
8− customGrsecPkg =
9− (import ../../../pkgs/build-support/grsecurity {
10− grsecOptions = cfg;
11− inherit pkgs lib;
12− }).grsecPackage;
7+ grsecLockPath = "/proc/sys/kernel/grsecurity/grsec_lock";
8+
9+ # Ascertain whether ZFS is required for booting the system; grsecurity is
10+ # currently incompatible with ZFS, rendering the system unbootable.
11+ zfsNeededForBoot = filter
12+ (fs: (fs.neededForBoot
13+ || elem fs.mountPoint [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/etc" ])
14+ && fs.fsType == "zfs")
15+ (attrValues config.fileSystems) != [];
1316in
14−{
15− options = {
16− security.grsecurity = {
17− enable = mkOption {
18− type = types.bool;
19− default = false;
20− description = ''
21− Enable grsecurity support. This enables advanced exploit
22− hardening for the Linux kernel, and adds support for
23− administrative Role-Based Acess Control (RBAC) via
24− <literal>gradm</literal>. It also includes traditional
25− utilities for PaX.
26− '';
27− };
28−
29− kernelPatch = mkOption {
30− type = types.attrs;
31− example = lib.literalExample "pkgs.kernelPatches.grsecurity_4_1";
32− description = ''
33− Grsecurity patch to use.
34− '';
35− };
36−
37− config = {
38− mode = mkOption {
39− type = types.enum [ "auto" "custom" ];
40− default = "auto";
41− description = ''
42− grsecurity configuration mode. This specifies whether
43− grsecurity is auto-configured or otherwise completely
44− manually configured.
45− '';
46− };
47−
48− priority = mkOption {
49− type = types.enum [ "security" "performance" ];
50− default = "security";
51− description = ''
52− grsecurity configuration priority. This specifies whether
53− the kernel configuration should emphasize speed or
54− security.
55− '';
56− };
57−
58− system = mkOption {
59− type = types.enum [ "desktop" "server" ];
60− default = "desktop";
61− description = ''
62− grsecurity system configuration.
63− '';
64− };
6517
66− virtualisationConfig = mkOption {
67− type = types.nullOr (types.enum [ "host" "guest" ]);
68− default = null;
69− description = ''
70− grsecurity virtualisation configuration. This specifies
71− the virtualisation role of the machine - that is, whether
72− it will be a virtual machine guest, a virtual machine
73− host, or neither.
74− '';
75− };
76−
77− hardwareVirtualisation = mkOption {
78− type = types.nullOr types.bool;
79− default = null;
80− example = true;
81− description = ''
82− grsecurity hardware virtualisation configuration. Set to
83− <literal>true</literal> if your machine supports hardware
84− accelerated virtualisation.
85− '';
86− };
87−
88− virtualisationSoftware = mkOption {
89− type = types.nullOr (types.enum [ "kvm" "xen" "vmware" "virtualbox" ]);
90− default = null;
91− description = ''
92− Configure grsecurity for use with this virtualisation software.
93− '';
94− };
95−
96− sysctl = mkOption {
97− type = types.bool;
98− default = false;
99− description = ''
100− If true, then set <literal>GRKERN_SYSCTL y</literal>. If
101− enabled then grsecurity can be controlled using sysctl
102− (and turned off). You are advised to *never* enable this,
103− but if you do, make sure to always set the sysctl
104− <literal>kernel.grsecurity.grsec_lock</literal> to
105− non-zero as soon as all sysctl options are set. *THIS IS
106− EXTREMELY IMPORTANT*!
107− '';
108− };
109−
110− denyChrootChmod = mkOption {
111− type = types.bool;
112− default = false;
113− description = ''
114− If true, then set <literal>GRKERN_CHROOT_CHMOD
115− y</literal>. If enabled, this denies processes inside a
116− chroot from setting the suid or sgid bits using
117− <literal>chmod</literal> or <literal>fchmod</literal>.
118−
119− By default this protection is disabled - it makes it
120− impossible to use Nix to build software on your system,
121− which is what most users want.
18+{
19+ options.security.grsecurity = {
20+
21+ enable = mkEnableOption "Grsecurity/PaX";
22+
23+ lockTunables = mkOption {
24+ type = types.bool;
25+ example = false;
26+ default = true;
27+ description = ''
28+ Whether to automatically lock grsecurity tunables
29+ (<option>boot.kernel.sysctl."kernel.grsecurity.*"</option>). Disable
30+ this to allow configuration of grsecurity features while the system is
31+ running. The lock can be manually engaged by activating the
32+ <literal>grsec-lock</literal> service unit.
33+ '';
34+ };
12235
123− If you are using NixOps to deploy your software to a
124− remote machine, you're encouraged to enable this as you
125− won't need to compile code.
126− '';
127− };
36+ };
12837
129− denyChrootCaps = mkOption {
130− type = types.bool;
131− default = false;
132− description = ''
133− Whether to lower capabilities of all processes within a chroot,
134− preventing commands that require <literal>CAP_SYS_ADMIN</literal>.
38+ config = mkIf cfg.enable {
13539
136− This protection is disabled by default because it breaks
137− <literal>nixos-rebuild</literal>. Whenever possible, it is
138− highly recommended to enable this protection.
139− '';
140− };
40+ # Allow the user to select a different package set, subject to the stated
41+ # required kernel config
42+ boot.kernelPackages = mkDefault pkgs.linuxPackages_grsec_nixos;
14143
142− denyUSB = mkOption {
143− type = types.bool;
144− default = false;
145− description = ''
146− If true, then set <literal>GRKERNSEC_DENYUSB y</literal>.
44+ system.requiredKernelConfig = with config.lib.kernelConfig;
45+ [ (isEnabled "GRKERNSEC")
46+ (isEnabled "PAX")
47+ (isYES "GRKERNSEC_SYSCTL")
48+ (isYES "GRKERNSEC_SYSCTL_DISTRO")
49+ ];
14750
148− This enables a sysctl with name
149− <literal>kernel.grsecurity.deny_new_usb</literal>. Setting
150− its value to <literal>1</literal> will prevent any new USB
151− devices from being recognized by the OS. Any attempted
152− USB device insertion will be logged.
51+ # Crashing on an overflow in kernel land is user unfriendly and may prevent
52+ # the system from booting, which is too severe for our use case.
53+ boot.kernelParams = [ "pax_size_overflow_report_only" ];
15354
154− This option is intended to be used against custom USB
155− devices designed to exploit vulnerabilities in various USB
156− device drivers.
157− '';
158− };
55+ # Install PaX related utillities into the system profile. Eventually, we
56+ # also want to include gradm here.
57+ environment.systemPackages = with pkgs; [ paxctl pax-utils ];
15958
160− restrictProc = mkOption {
161− type = types.bool;
162− default = false;
163− description = ''
164− If true, then set <literal>GRKERN_PROC_USER
165− y</literal>. This restricts non-root users to only viewing
166− their own processes and restricts network-related
167− information, kernel symbols, and module information.
168− '';
169− };
59+ # Install rules for the grsec device node
60+ services.udev.packages = [ pkgs.gradm ];
17061
171− restrictProcWithGroup = mkOption {
172− type = types.bool;
173− default = true;
174− description = ''
175− If true, then set <literal>GRKERN_PROC_USERGROUP
176− y</literal>. This is similar to
177− <literal>restrictProc</literal> except it allows a special
178− group (specified by <literal>unrestrictProcGid</literal>)
179− to still access otherwise classified information in
180− <literal>/proc</literal>.
181− '';
182− };
62+ # This service unit is responsible for locking the Grsecurity tunables. The
63+ # unit is always defined, but only activated on bootup if lockTunables is
64+ # toggled. When lockTunables is toggled, failure to activate the unit will
65+ # enter emergency mode. The intent is to make it difficult to silently
66+ # enter multi-user mode without having locked the tunables. Some effort is
67+ # made to ensure that starting the unit is an idempotent operation.
68+ systemd.services.grsec-lock = {
69+ description = "Lock grsecurity tunables";
18370
184− unrestrictProcGid = mkOption {
185− type = types.int;
186− default = config.ids.gids.grsecurity;
187− description = ''
188− If set, specifies a GID which is exempt from
189− <literal>/proc</literal> restrictions (set by
190− <literal>GRKERN_PROC_USERGROUP</literal>). By default,
191− this is set to the GID for <literal>grsecurity</literal>,
192− a predefined NixOS group, which the
193− <literal>root</literal> account is a member of. You may
194− conveniently add other users to this group if you need
195− access to <literal>/proc</literal>
196− '';
197− };
71+ wantedBy = optional cfg.lockTunables "multi-user.target";
19872
199− disableRBAC = mkOption {
200− type = types.bool;
201− default = false;
202− description = ''
203− If true, then set <literal>GRKERN_NO_RBAC
204− y</literal>. This disables the
205− <literal>/dev/grsec</literal> device, which in turn
206− disables the RBAC system (and <literal>gradm</literal>).
207− '';
208− };
73+ wants = [ "local-fs.target" "systemd-sysctl.service" ];
74+ after = [ "local-fs.target" "systemd-sysctl.service" ];
75+ conflicts = [ "shutdown.target" ];
20976
210− disableSimultConnect = mkOption {
211− type = types.bool;
212− default = false;
213− description = ''
214− Disable TCP simultaneous connect. The TCP simultaneous connect
215− feature allows two clients to connect without either of them
216− entering the listening state. This feature of the TCP specification
217− is claimed to enable an attacker to deny the target access to a given
218− server by guessing the source port the target would use to make the
219− connection.
77+ restartIfChanged = false;
22078
221− This option is OFF by default because TCP simultaneous connect has
222− some legitimate uses. Enable this option if you know what this TCP
223− feature is for and know that you do not need it.
224− '';
225− };
79+ script = ''
80+ if ${pkgs.gnugrep}/bin/grep -Fq 0 ${grsecLockPath} ; then
81+ echo -n 1 > ${grsecLockPath}
82+ fi
83+ '';
22684
227− verboseVersion = mkOption {
228− type = types.bool;
229− default = false;
230− description = "Use verbose version in kernel localversion.";
231− };
85+ unitConfig = {
86+ ConditionPathIsReadWrite = grsecLockPath;
87+ DefaultDependencies = false;
88+ } // optionalAttrs cfg.lockTunables {
89+ OnFailure = "emergency.target";
90+ };
23291
233− kernelExtraConfig = mkOption {
234− type = types.str;
235− default = "";
236− description = "Extra kernel configuration parameters.";
237− };
92+ serviceConfig = {
93+ Type = "oneshot";
94+ RemainAfterExit = true;
23895 };
23996 };
240− };
24197
242− config = mkIf cfg.enable {
243− assertions =
244− [
245− { assertion = (cfg.config.restrictProc -> !cfg.config.restrictProcWithGroup) ||
246− (cfg.config.restrictProcWithGroup -> !cfg.config.restrictProc);
247− message = "You cannot enable both restrictProc and restrictProcWithGroup";
248− }
249− { assertion = config.boot.kernelPackages.kernel.features ? grsecurity
250− && config.boot.kernelPackages.kernel.features.grsecurity;
251− message = "grsecurity enabled, but kernel doesn't have grsec support";
252− }
253− { assertion = (cfg.config.mode == "auto" && (cfg.config.virtualisationConfig != null)) ->
254− cfg.config.hardwareVirtualisation != null;
255− message = "when using auto grsec mode with virtualisation, you must specify if your hardware has virtualisation extensions";
256− }
257− { assertion = (cfg.config.mode == "auto" && (cfg.config.virtualisationConfig != null)) ->
258− cfg.config.virtualisationSoftware != null;
259− message = "grsecurity configured for virtualisation but no virtualisation software specified";
260− }
261− ];
262−
263− security.grsecurity.kernelPatch = lib.mkDefault pkgs.kernelPatches.grsecurity_latest;
264−
265− systemd.services.grsec-lock = mkIf cfg.config.sysctl {
266− description = "grsecurity sysctl-lock Service";
267− wants = [ "systemd-sysctl.service" ];
268− after = [ "systemd-sysctl.service" ];
269− wantedBy = [ "multi-user.target" ];
270− serviceConfig.Type = "oneshot";
271− serviceConfig.RemainAfterExit = "yes";
272− unitConfig.ConditionPathIsReadWrite = "/proc/sys/kernel/grsecurity/grsec_lock";
273− script = ''
274− locked=`cat /proc/sys/kernel/grsecurity/grsec_lock`
275− if [ "$locked" == "0" ]; then
276− echo 1 > /proc/sys/kernel/grsecurity/grsec_lock
277− echo grsecurity sysctl lock - enabled
278− else
279− echo grsecurity sysctl lock already enabled - doing nothing
280− fi
281− '';
98+ # Configure system tunables
99+ boot.kernel.sysctl = {
100+ # Removed under grsecurity
101+ "kernel.kptr_restrict" = mkForce null;
102+ } // optionalAttrs config.nix.useSandbox {
103+ # chroot(2) restrictions that conflict with sandboxed Nix builds
104+ "kernel.grsecurity.chroot_caps" = mkForce 0;
105+ "kernel.grsecurity.chroot_deny_chroot" = mkForce 0;
106+ "kernel.grsecurity.chroot_deny_mount" = mkForce 0;
107+ "kernel.grsecurity.chroot_deny_pivot" = mkForce 0;
108+ } // optionalAttrs config.boot.enableContainers {
109+ # chroot(2) restrictions that conflict with NixOS lightweight containers
110+ "kernel.grsecurity.chroot_deny_chmod" = mkForce 0;
111+ "kernel.grsecurity.chroot_deny_mount" = mkForce 0;
112+ "kernel.grsecurity.chroot_restrict_nice" = mkForce 0;
282113 };
283114
284−# systemd.services.grsec-learn = {
285−# description = "grsecurity learning Service";
286−# wantedBy = [ "local-fs.target" ];
287−# serviceConfig = {
288−# Type = "oneshot";
289−# RemainAfterExit = "yes";
290−# ExecStart = "${pkgs.gradm}/sbin/gradm -VFL /etc/grsec/learning.logs";
291−# ExecStop = "${pkgs.gradm}/sbin/gradm -D";
292−# };
293−# };
294−
295− system.activationScripts = lib.optionalAttrs (!cfg.config.disableRBAC) { grsec = ''
296− mkdir -p /etc/grsec
297− if [ ! -f /etc/grsec/learn_config ]; then
298− cp ${pkgs.gradm}/etc/grsec/learn_config /etc/grsec
299− fi
300− if [ ! -f /etc/grsec/policy ]; then
301− cp ${pkgs.gradm}/etc/grsec/policy /etc/grsec
302− fi
303− chmod -R 0600 /etc/grsec
304− ''; };
115+ assertions = [
116+ { assertion = !zfsNeededForBoot;
117+ message = "grsecurity is currently incompatible with ZFS";
118+ }
119+ ];
305120
306− # Enable AppArmor, gradm udev rules, and utilities
307− security.apparmor.enable = true;
308− boot.kernelPackages = customGrsecPkg;
309− services.udev.packages = lib.optional (!cfg.config.disableRBAC) pkgs.gradm;
310− environment.systemPackages = [ pkgs.paxctl pkgs.pax-utils ] ++ lib.optional (!cfg.config.disableRBAC) pkgs.gradm;
311121 };
312122}