2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-05-13 13:13:06 +00:00
|
|
|
|
2021-02-27 20:26:47 +00:00
|
|
|
with lib;
|
|
|
|
|
2012-07-16 23:47:41 +00:00
|
|
|
let
|
2020-10-18 13:36:24 +00:00
|
|
|
inherit (builtins) attrNames head map match readFile;
|
|
|
|
inherit (lib) types;
|
|
|
|
inherit (config.environment) etc;
|
2012-07-16 23:47:41 +00:00
|
|
|
cfg = config.security.apparmor;
|
2022-08-28 19:18:44 +00:00
|
|
|
mkDisableOption = name: mkEnableOption (lib.mdDoc name) // {
|
2020-10-18 13:36:24 +00:00
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
};
|
2021-02-27 20:26:47 +00:00
|
|
|
enabledPolicies = filterAttrs (n: p: p.enable) cfg.policies;
|
2012-07-16 23:47:41 +00:00
|
|
|
in
|
2015-03-12 09:11:25 +00:00
|
|
|
|
2012-07-16 23:47:41 +00:00
|
|
|
{
|
2020-10-18 13:36:24 +00:00
|
|
|
imports = [
|
2021-02-27 20:26:47 +00:00
|
|
|
(mkRemovedOptionModule [ "security" "apparmor" "confineSUIDApplications" ] "Please use the new options: `security.apparmor.policies.<policy>.enable'.")
|
|
|
|
(mkRemovedOptionModule [ "security" "apparmor" "profiles" ] "Please use the new option: `security.apparmor.policies'.")
|
2020-10-18 13:36:24 +00:00
|
|
|
apparmor/includes.nix
|
|
|
|
apparmor/profiles.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
security.apparmor = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc ''
|
2021-02-27 20:26:47 +00:00
|
|
|
the AppArmor Mandatory Access Control system.
|
2020-10-18 13:36:24 +00:00
|
|
|
|
|
|
|
If you're enabling this module on a running system,
|
|
|
|
note that a reboot will be required to activate AppArmor in the kernel.
|
|
|
|
|
2021-02-28 23:24:54 +00:00
|
|
|
Also, beware that enabling this module privileges stability over security
|
|
|
|
by not trying to kill unconfined but newly confinable running processes by default,
|
|
|
|
though it would be needed because AppArmor can only confine new
|
|
|
|
or already confined processes of an executable.
|
|
|
|
This killing would for instance be necessary when upgrading to a NixOS revision
|
|
|
|
introducing for the first time an AppArmor profile for the executable
|
|
|
|
of a running process.
|
|
|
|
|
2022-08-28 19:18:44 +00:00
|
|
|
Enable [](#opt-security.apparmor.killUnconfinedConfinables)
|
2021-02-28 23:24:54 +00:00
|
|
|
if you want this service to do such killing
|
2022-08-28 19:18:44 +00:00
|
|
|
by sending a `SIGTERM` to those running processes'');
|
2021-02-27 20:26:47 +00:00
|
|
|
policies = mkOption {
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-10-18 13:36:24 +00:00
|
|
|
AppArmor policies.
|
|
|
|
'';
|
|
|
|
type = types.attrsOf (types.submodule ({ name, config, ... }: {
|
|
|
|
options = {
|
|
|
|
enable = mkDisableOption "loading of the profile into the kernel";
|
|
|
|
enforce = mkDisableOption "enforcing of the policy or only complain in the logs";
|
2021-02-27 20:26:47 +00:00
|
|
|
profile = mkOption {
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc "The policy of the profile.";
|
2020-10-18 13:36:24 +00:00
|
|
|
type = types.lines;
|
|
|
|
apply = pkgs.writeText name;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
default = {};
|
|
|
|
};
|
2021-02-27 20:26:47 +00:00
|
|
|
includes = mkOption {
|
2020-10-18 13:36:24 +00:00
|
|
|
type = types.attrsOf types.lines;
|
|
|
|
default = {};
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-10-18 13:36:24 +00:00
|
|
|
List of paths to be added to AppArmor's searched paths
|
2022-08-05 17:39:00 +00:00
|
|
|
when resolving `include` directives.
|
2020-10-18 13:36:24 +00:00
|
|
|
'';
|
2021-02-27 20:26:47 +00:00
|
|
|
apply = mapAttrs pkgs.writeText;
|
2020-10-18 13:36:24 +00:00
|
|
|
};
|
2021-02-27 20:26:47 +00:00
|
|
|
packages = mkOption {
|
2020-10-18 13:36:24 +00:00
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc "List of packages to be added to AppArmor's include path";
|
2020-10-18 13:36:24 +00:00
|
|
|
};
|
2022-08-28 19:18:44 +00:00
|
|
|
enableCache = mkEnableOption (lib.mdDoc ''
|
2021-02-27 20:26:47 +00:00
|
|
|
caching of AppArmor policies
|
2022-08-28 19:18:44 +00:00
|
|
|
in `/var/cache/apparmor/`.
|
2020-10-18 13:36:24 +00:00
|
|
|
|
|
|
|
Beware that AppArmor policies almost always contain Nix store paths,
|
|
|
|
and thus produce at each change of these paths
|
2022-08-28 19:18:44 +00:00
|
|
|
a new cached version accumulating in the cache'');
|
|
|
|
killUnconfinedConfinables = mkEnableOption (lib.mdDoc ''
|
2021-02-27 20:26:47 +00:00
|
|
|
killing of processes which have an AppArmor profile enabled
|
2022-08-28 19:18:44 +00:00
|
|
|
(in [](#opt-security.apparmor.policies))
|
2020-10-18 13:36:24 +00:00
|
|
|
but are not confined (because AppArmor can only confine new processes).
|
2021-02-28 23:24:54 +00:00
|
|
|
|
2022-08-28 19:18:44 +00:00
|
|
|
This is only sending a gracious `SIGTERM` signal to the processes,
|
|
|
|
not a `SIGKILL`.
|
2021-02-28 23:24:54 +00:00
|
|
|
|
2020-10-18 13:36:24 +00:00
|
|
|
Beware that due to a current limitation of AppArmor,
|
2022-08-28 19:18:44 +00:00
|
|
|
only profiles with exact paths (and no name) can enable such kills'');
|
2020-10-18 13:36:24 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-27 20:26:47 +00:00
|
|
|
config = mkIf cfg.enable {
|
2020-10-18 13:36:24 +00:00
|
|
|
assertions = map (policy:
|
|
|
|
{ assertion = match ".*/.*" policy == null;
|
|
|
|
message = "`security.apparmor.policies.\"${policy}\"' must not contain a slash.";
|
|
|
|
# Because, for instance, aa-remove-unknown uses profiles_names_list() in rc.apparmor.functions
|
|
|
|
# which does not recurse into sub-directories.
|
|
|
|
}
|
|
|
|
) (attrNames cfg.policies);
|
|
|
|
|
2020-12-01 16:35:56 +00:00
|
|
|
environment.systemPackages = [
|
|
|
|
pkgs.apparmor-utils
|
|
|
|
pkgs.apparmor-bin-utils
|
|
|
|
];
|
2020-10-18 13:36:24 +00:00
|
|
|
environment.etc."apparmor.d".source = pkgs.linkFarm "apparmor.d" (
|
|
|
|
# It's important to put only enabledPolicies here and not all cfg.policies
|
|
|
|
# because aa-remove-unknown reads profiles from all /etc/apparmor.d/*
|
2021-02-27 20:26:47 +00:00
|
|
|
mapAttrsToList (name: p: { inherit name; path = p.profile; }) enabledPolicies ++
|
|
|
|
mapAttrsToList (name: path: { inherit name path; }) cfg.includes
|
2020-10-18 13:36:24 +00:00
|
|
|
);
|
|
|
|
environment.etc."apparmor/parser.conf".text = ''
|
2021-02-27 20:26:47 +00:00
|
|
|
${if cfg.enableCache then "write-cache" else "skip-cache"}
|
|
|
|
cache-loc /var/cache/apparmor
|
|
|
|
Include /etc/apparmor.d
|
|
|
|
'' +
|
|
|
|
concatMapStrings (p: "Include ${p}/etc/apparmor.d\n") cfg.packages;
|
2020-10-18 13:36:24 +00:00
|
|
|
# For aa-logprof
|
|
|
|
environment.etc."apparmor/apparmor.conf".text = ''
|
|
|
|
'';
|
|
|
|
# For aa-logprof
|
|
|
|
environment.etc."apparmor/severity.db".source = pkgs.apparmor-utils + "/etc/apparmor/severity.db";
|
2020-12-03 13:45:43 +00:00
|
|
|
environment.etc."apparmor/logprof.conf".source = pkgs.runCommand "logprof.conf" {
|
|
|
|
header = ''
|
|
|
|
[settings]
|
|
|
|
# /etc/apparmor.d/ is read-only on NixOS
|
|
|
|
profiledir = /var/cache/apparmor/logprof
|
|
|
|
inactive_profiledir = /etc/apparmor.d/disable
|
|
|
|
# Use: journalctl -b --since today --grep audit: | aa-logprof
|
|
|
|
logfiles = /dev/stdin
|
2020-10-18 13:36:24 +00:00
|
|
|
|
2020-12-03 13:45:43 +00:00
|
|
|
parser = ${pkgs.apparmor-parser}/bin/apparmor_parser
|
|
|
|
ldd = ${pkgs.glibc.bin}/bin/ldd
|
2021-05-17 15:14:08 +00:00
|
|
|
logger = ${pkgs.util-linux}/bin/logger
|
2020-10-18 13:36:24 +00:00
|
|
|
|
2020-12-03 13:45:43 +00:00
|
|
|
# customize how file ownership permissions are presented
|
|
|
|
# 0 - off
|
|
|
|
# 1 - default of what ever mode the log reported
|
|
|
|
# 2 - force the new permissions to be user
|
|
|
|
# 3 - force all perms on the rule to be user
|
|
|
|
default_owner_prompt = 1
|
2020-10-18 13:36:24 +00:00
|
|
|
|
2021-02-27 20:26:47 +00:00
|
|
|
custom_includes = /etc/apparmor.d ${concatMapStringsSep " " (p: "${p}/etc/apparmor.d") cfg.packages}
|
2020-10-18 13:36:24 +00:00
|
|
|
|
2020-12-03 13:45:43 +00:00
|
|
|
[qualifiers]
|
|
|
|
${pkgs.runtimeShell} = icnu
|
|
|
|
${pkgs.bashInteractive}/bin/sh = icnu
|
|
|
|
${pkgs.bashInteractive}/bin/bash = icnu
|
2021-02-27 20:26:47 +00:00
|
|
|
${config.users.defaultUserShell} = icnu
|
2020-12-03 13:45:43 +00:00
|
|
|
'';
|
|
|
|
footer = "${pkgs.apparmor-utils}/etc/apparmor/logprof.conf";
|
|
|
|
passAsFile = [ "header" ];
|
|
|
|
} ''
|
|
|
|
cp $headerPath $out
|
2020-12-03 15:25:44 +00:00
|
|
|
sed '1,/\[qualifiers\]/d' $footer >> $out
|
2020-12-03 13:45:43 +00:00
|
|
|
'';
|
2020-10-18 13:36:24 +00:00
|
|
|
|
|
|
|
boot.kernelParams = [ "apparmor=1" "security=apparmor" ];
|
|
|
|
|
|
|
|
systemd.services.apparmor = {
|
|
|
|
after = [
|
|
|
|
"local-fs.target"
|
|
|
|
"systemd-journald-audit.socket"
|
|
|
|
];
|
|
|
|
before = [ "sysinit.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
unitConfig = {
|
|
|
|
Description="Load AppArmor policies";
|
|
|
|
DefaultDependencies = "no";
|
|
|
|
ConditionSecurity = "apparmor";
|
|
|
|
};
|
|
|
|
# Reloading instead of restarting enables to load new AppArmor profiles
|
|
|
|
# without necessarily restarting all services which have Requires=apparmor.service
|
|
|
|
reloadIfChanged = true;
|
|
|
|
restartTriggers = [
|
|
|
|
etc."apparmor/parser.conf".source
|
|
|
|
etc."apparmor.d".source
|
|
|
|
];
|
|
|
|
serviceConfig = let
|
|
|
|
killUnconfinedConfinables = pkgs.writeShellScript "apparmor-kill" ''
|
|
|
|
set -eu
|
2020-12-01 16:35:56 +00:00
|
|
|
${pkgs.apparmor-bin-utils}/bin/aa-status --json |
|
2020-10-18 13:36:24 +00:00
|
|
|
${pkgs.jq}/bin/jq --raw-output '.processes | .[] | .[] | select (.status == "unconfined") | .pid' |
|
|
|
|
xargs --verbose --no-run-if-empty --delimiter='\n' \
|
|
|
|
kill
|
|
|
|
'';
|
2021-02-27 20:26:47 +00:00
|
|
|
commonOpts = p: "--verbose --show-cache ${optionalString (!p.enforce) "--complain "}${p.profile}";
|
2020-10-18 13:36:24 +00:00
|
|
|
in {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
ExecStartPre = "${pkgs.apparmor-utils}/bin/aa-teardown";
|
2021-02-27 20:26:47 +00:00
|
|
|
ExecStart = mapAttrsToList (n: p: "${pkgs.apparmor-parser}/bin/apparmor_parser --add ${commonOpts p}") enabledPolicies;
|
|
|
|
ExecStartPost = optional cfg.killUnconfinedConfinables killUnconfinedConfinables;
|
2020-10-18 13:36:24 +00:00
|
|
|
ExecReload =
|
|
|
|
# Add or replace into the kernel profiles in enabledPolicies
|
|
|
|
# (because AppArmor can do that without stopping the processes already confined).
|
2021-02-27 20:26:47 +00:00
|
|
|
mapAttrsToList (n: p: "${pkgs.apparmor-parser}/bin/apparmor_parser --replace ${commonOpts p}") enabledPolicies ++
|
2020-10-18 13:36:24 +00:00
|
|
|
# Remove from the kernel any profile whose name is not
|
|
|
|
# one of the names within the content of the profiles in enabledPolicies
|
|
|
|
# (indirectly read from /etc/apparmor.d/*, without recursing into sub-directory).
|
|
|
|
# Note that this does not remove profiles dynamically generated by libvirt.
|
|
|
|
[ "${pkgs.apparmor-utils}/bin/aa-remove-unknown" ] ++
|
|
|
|
# Optionaly kill the processes which are unconfined but now have a profile loaded
|
|
|
|
# (because AppArmor can only start to confine new processes).
|
2021-02-27 20:26:47 +00:00
|
|
|
optional cfg.killUnconfinedConfinables killUnconfinedConfinables;
|
2020-10-18 13:36:24 +00:00
|
|
|
ExecStop = "${pkgs.apparmor-utils}/bin/aa-teardown";
|
|
|
|
CacheDirectory = [ "apparmor" "apparmor/logprof" ];
|
|
|
|
CacheDirectoryMode = "0700";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-27 20:26:47 +00:00
|
|
|
meta.maintainers = with maintainers; [ julm ];
|
2012-07-16 23:47:41 +00:00
|
|
|
}
|