2016-01-07 03:06:10 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.security.audit;
|
2016-08-28 16:57:36 +01:00
|
|
|
enabled = cfg.enable == "lock" || cfg.enable;
|
2016-01-07 03:06:10 +00:00
|
|
|
|
|
|
|
failureModes = {
|
|
|
|
silent = 0;
|
|
|
|
printk = 1;
|
|
|
|
panic = 2;
|
|
|
|
};
|
|
|
|
|
2016-08-28 16:57:36 +01:00
|
|
|
disableScript = pkgs.writeScript "audit-disable" ''
|
2018-03-01 19:38:53 +00:00
|
|
|
#!${pkgs.runtimeShell} -eu
|
2016-08-28 16:57:36 +01:00
|
|
|
# Explicitly disable everything, as otherwise journald might start it.
|
|
|
|
auditctl -D
|
|
|
|
auditctl -e 0 -a task,never
|
|
|
|
'';
|
|
|
|
|
2016-01-07 03:06:10 +00:00
|
|
|
# TODO: it seems like people like their rules to be somewhat secret, yet they will not be if
|
|
|
|
# put in the store like this. At the same time, it doesn't feel like a huge deal and working
|
|
|
|
# around that is a pain so I'm leaving it like this for now.
|
|
|
|
startScript = pkgs.writeScript "audit-start" ''
|
2018-03-01 19:38:53 +00:00
|
|
|
#!${pkgs.runtimeShell} -eu
|
2016-01-07 03:06:10 +00:00
|
|
|
# Clear out any rules we may start with
|
|
|
|
auditctl -D
|
|
|
|
|
|
|
|
# Put the rules in a temporary file owned and only readable by root
|
|
|
|
rulesfile="$(mktemp)"
|
|
|
|
${concatMapStrings (x: "echo '${x}' >> $rulesfile\n") cfg.rules}
|
|
|
|
|
|
|
|
# Apply the requested rules
|
|
|
|
auditctl -R "$rulesfile"
|
|
|
|
|
|
|
|
# Enable and configure auditing
|
|
|
|
auditctl \
|
|
|
|
-e ${if cfg.enable == "lock" then "2" else "1"} \
|
|
|
|
-b ${toString cfg.backlogLimit} \
|
|
|
|
-f ${toString failureModes.${cfg.failureMode}} \
|
|
|
|
-r ${toString cfg.rateLimit}
|
|
|
|
'';
|
|
|
|
|
|
|
|
stopScript = pkgs.writeScript "audit-stop" ''
|
2018-03-01 19:38:53 +00:00
|
|
|
#!${pkgs.runtimeShell} -eu
|
2016-01-07 03:06:10 +00:00
|
|
|
# Clear the rules
|
|
|
|
auditctl -D
|
|
|
|
|
|
|
|
# Disable auditing
|
|
|
|
auditctl -e 0
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
security.audit = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.enum [ false true "lock" ];
|
2016-08-22 16:11:53 +01:00
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-01-07 03:06:10 +00:00
|
|
|
Whether to enable the Linux audit system. The special `lock' value can be used to
|
|
|
|
enable auditing and prevent disabling it until a restart. Be careful about locking
|
|
|
|
this, as it will prevent you from changing your audit configuration until you
|
|
|
|
restart. If possible, test your configuration using build-vm beforehand.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
failureMode = mkOption {
|
|
|
|
type = types.enum [ "silent" "printk" "panic" ];
|
|
|
|
default = "printk";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "How to handle critical errors in the auditing system";
|
2016-01-07 03:06:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
backlogLimit = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 64; # Apparently the kernel default
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-01-07 03:06:10 +00:00
|
|
|
The maximum number of outstanding audit buffers allowed; exceeding this is
|
|
|
|
considered a failure and handled in a manner specified by failureMode.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
rateLimit = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 0;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-01-07 03:06:10 +00:00
|
|
|
The maximum messages per second permitted before triggering a failure as
|
|
|
|
specified by failureMode. Setting it to zero disables the limit.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
rules = mkOption {
|
|
|
|
type = types.listOf types.str; # (types.either types.str (types.submodule rule));
|
|
|
|
default = [];
|
|
|
|
example = [ "-a exit,always -F arch=b64 -S execve" ];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-01-07 03:06:10 +00:00
|
|
|
The ordered audit rules, with each string appearing as one line of the audit.rules file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-28 16:57:36 +01:00
|
|
|
config = {
|
2016-01-07 03:06:10 +00:00
|
|
|
systemd.services.audit = {
|
2016-01-18 12:57:27 +00:00
|
|
|
description = "Kernel Auditing";
|
2016-01-07 03:06:10 +00:00
|
|
|
wantedBy = [ "basic.target" ];
|
|
|
|
|
2016-10-15 15:03:41 +01:00
|
|
|
unitConfig = {
|
|
|
|
ConditionVirtualization = "!container";
|
|
|
|
ConditionSecurity = [ "audit" ];
|
|
|
|
};
|
|
|
|
|
2016-01-18 12:57:27 +00:00
|
|
|
|
2016-01-07 03:06:10 +00:00
|
|
|
path = [ pkgs.audit ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
2016-08-28 16:57:36 +01:00
|
|
|
ExecStart = "@${if enabled then startScript else disableScript} audit-start";
|
|
|
|
ExecStop = "@${stopScript} audit-stop";
|
2016-01-07 03:06:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|