git.iko.soy/typeable/nixpkgs
on master help sign in
security.audit: add NixOS module

Part of the way towards #11864. We still don't have the auditd userland logging daemon, but journald also tracks audit logs so we can already use this.

commit63bfe20b7253fa579ca1c35d07d1d790475f74c5 authorDan Peebles <pumpkin@me.com> dateThu 7 Jan 2016, 03:06 (11 years ago) parent4a66d92
2 files changed+110
nixos/modules/module-list.nix+1 nixos/modules/security/audit.nix+109
nixos/modules/module-list.nix+1
@@ -84,6 +84,7 @@
8484 ./security/acme.nix
8585 ./security/apparmor.nix
8686 ./security/apparmor-suid.nix
87+ ./security/audit.nix
8788 ./security/ca.nix
8889 ./security/duosec.nix
8990 ./security/grsecurity.nix
nixos/modules/security/audit.nixnew file+109
@@ -0,0 +1,109 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.security.audit;
7+
8+ failureModes = {
9+ silent = 0;
10+ printk = 1;
11+ panic = 2;
12+ };
13+
14+ # TODO: it seems like people like their rules to be somewhat secret, yet they will not be if
15+ # put in the store like this. At the same time, it doesn't feel like a huge deal and working
16+ # around that is a pain so I'm leaving it like this for now.
17+ startScript = pkgs.writeScript "audit-start" ''
18+ #!${pkgs.stdenv.shell} -eu
19+ # Clear out any rules we may start with
20+ auditctl -D
21+
22+ # Put the rules in a temporary file owned and only readable by root
23+ rulesfile="$(mktemp)"
24+ ${concatMapStrings (x: "echo '${x}' >> $rulesfile\n") cfg.rules}
25+
26+ # Apply the requested rules
27+ auditctl -R "$rulesfile"
28+
29+ # Enable and configure auditing
30+ auditctl \
31+ -e ${if cfg.enable == "lock" then "2" else "1"} \
32+ -b ${toString cfg.backlogLimit} \
33+ -f ${toString failureModes.${cfg.failureMode}} \
34+ -r ${toString cfg.rateLimit}
35+ '';
36+
37+ stopScript = pkgs.writeScript "audit-stop" ''
38+ #!${pkgs.stdenv.shell} -eu
39+ # Clear the rules
40+ auditctl -D
41+
42+ # Disable auditing
43+ auditctl -e 0
44+ '';
45+in {
46+ options = {
47+ security.audit = {
48+ enable = mkOption {
49+ type = types.enum [ false true "lock" ];
50+ default = true; # The kernel seems to enable it by default with no rules anyway
51+ description = ''
52+ Whether to enable the Linux audit system. The special `lock' value can be used to
53+ enable auditing and prevent disabling it until a restart. Be careful about locking
54+ this, as it will prevent you from changing your audit configuration until you
55+ restart. If possible, test your configuration using build-vm beforehand.
56+ '';
57+ };
58+
59+ failureMode = mkOption {
60+ type = types.enum [ "silent" "printk" "panic" ];
61+ default = "printk";
62+ description = "How to handle critical errors in the auditing system";
63+ };
64+
65+ backlogLimit = mkOption {
66+ type = types.int;
67+ default = 64; # Apparently the kernel default
68+ description = ''
69+ The maximum number of outstanding audit buffers allowed; exceeding this is
70+ considered a failure and handled in a manner specified by failureMode.
71+ '';
72+ };
73+
74+ rateLimit = mkOption {
75+ type = types.int;
76+ default = 0;
77+ description = ''
78+ The maximum messages per second permitted before triggering a failure as
79+ specified by failureMode. Setting it to zero disables the limit.
80+ '';
81+ };
82+
83+ rules = mkOption {
84+ type = types.listOf types.str; # (types.either types.str (types.submodule rule));
85+ default = [];
86+ example = [ "-a exit,always -F arch=b64 -S execve" ];
87+ description = ''
88+ The ordered audit rules, with each string appearing as one line of the audit.rules file.
89+ '';
90+ };
91+ };
92+ };
93+
94+ config = mkIf (cfg.enable == "lock" || cfg.enable) {
95+ systemd.services.audit = {
96+ description = "pseudo-service representing the kernel audit state";
97+ wantedBy = [ "basic.target" ];
98+
99+ path = [ pkgs.audit ];
100+
101+ serviceConfig = {
102+ Type = "oneshot";
103+ RemainAfterExit = true;
104+ ExecStart = "@${startScript} audit-start";
105+ ExecStop = "@${stopScript} audit-stop";
106+ };
107+ };
108+ };
109+}