git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #93457 from ju1m/apparmor

apparmor: fix and improve the service

commit31a4e2e28bf29fc5ab1f70d28b5dbc2205a638a0 authorMichael Raskin <7c6f434c@mail.ru> dateSun 27 Sep 2020, 13:07 (6 years ago) parent862e6fefb6d63f
20 files changed+793−170
maintainers/maintainer-list.nix+1−1 nixos/doc/manual/release-notes/rl-2009.xml+18 nixos/modules/config/fonts/fontconfig.nix+34 nixos/modules/config/malloc.nix+7 nixos/modules/module-list.nix−1 nixos/modules/security/apparmor-suid.nix−49 nixos/modules/security/apparmor.nix+187−54 nixos/modules/security/apparmor/includes.nix+301 nixos/modules/security/apparmor/profiles.nix+11 nixos/modules/security/pam.nix+57 nixos/modules/security/wrappers/default.nix+8 nixos/modules/services/torrent/transmission.nix+10−53 nixos/modules/tasks/network-interfaces.nix+15 nixos/modules/virtualisation/lxc.nix+8−4 nixos/modules/virtualisation/lxd.nix+8−4 pkgs/os-specific/linux/apparmor/default.nix+52−2 pkgs/os-specific/linux/apparmor/fix-rc.apparmor.functions.sh+32 pkgs/os-specific/linux/iputils/default.nix+22 pkgs/tools/networking/inetutils/default.nix+21−1 pkgs/top-level/all-packages.nix+1−1
maintainers/maintainer-list.nix+1−1
@@ -4251,7 +4251,7 @@
42514251 name = "Julien Dehos";
42524252 };
42534253 julm = {
4254− email = "julm+nix@sourcephile.fr";
4254+ email = "julm+nixpkgs@sourcephile.fr";
42554255 github = "ju1m";
42564256 githubId = 21160136;
42574257 name = "Julien Moutinho";
nixos/doc/manual/release-notes/rl-2009.xml+18
@@ -979,6 +979,24 @@ services.transmission.settings.rpc-bind-address = "0.0.0.0";
979979 to get the previous behavior of listening on all network interfaces.
980980 </para>
981981 </listitem>
982+ <listitem>
983+ <para>
984+ The <literal>security.apparmor</literal> module,
985+ for the <link xlink:href="https://gitlab.com/apparmor/apparmor/-/wikis/Documentation">AppArmor</link>
986+ Mandatory Access Control system,
987+ has been substantialy improved along with related tools,
988+ so that module maintainers can now more easily write AppArmor profiles for NixOS.
989+ The most notable change on the user-side is the new option <xref linkend="opt-security.apparmor.policies"/>,
990+ replacing the previous <literal>profiles</literal> option
991+ to provide a way to disable a profile
992+ and to select whether to confine in enforce mode (default)
993+ or in complain mode (see <literal>journalctl -b --grep apparmor</literal>).
994+ Before enabling this module, either directly
995+ or by importing <literal>&lt;nixpkgs/nixos/modules/profiles/hardened.nix&gt;</literal>,
996+ please be sure to read the documentation of <link linkend="opt-security.apparmor.enable">security.apparmor.enable</link>,
997+ and especially the part about <xref linkend="opt-security.apparmor.killUnconfinedConfinables"/>.
998+ </para>
999+ </listitem>
9821000 <listitem>
9831001 <para>
9841002 With this release <literal>systemd-networkd</literal> (when enabled through <xref linkend="opt-networking.useNetworkd"/>)
nixos/modules/config/fonts/fontconfig.nix+34
@@ -448,6 +448,40 @@ in
448448 (mkIf cfg.enable {
449449 environment.systemPackages = [ pkgs.fontconfig ];
450450 environment.etc.fonts.source = "${fontconfigEtc}/etc/fonts/";
451+ security.apparmor.includes."abstractions/fonts" = ''
452+ # fonts.conf
453+ r ${pkg.out}/etc/fonts/fonts.conf,
454+
455+ # fontconfig default config files
456+ r ${pkg.out}/etc/fonts/conf.d/*.conf,
457+
458+ # 00-nixos-cache.conf
459+ r ${cacheConf},
460+
461+ # 10-nixos-rendering.conf
462+ r ${renderConf},
463+
464+ # 50-user.conf
465+ ${optionalString cfg.includeUserConf ''
466+ r ${pkg.out}/etc/fonts/conf.d.bak/50-user.conf,
467+ ''}
468+
469+ # local.conf (indirect priority 51)
470+ ${optionalString (cfg.localConf != "") ''
471+ r ${localConf},
472+ ''}
473+
474+ # 52-nixos-default-fonts.conf
475+ r ${defaultFontsConf},
476+
477+ # 53-no-bitmaps.conf
478+ r ${rejectBitmaps},
479+
480+ ${optionalString (!cfg.allowType1) ''
481+ # 53-nixos-reject-type1.conf
482+ r ${rejectType1},
483+ ''}
484+ '';
451485 })
452486 (mkIf cfg.enable {
453487 fonts.fontconfig.confPackages = [ confPkg ];
nixos/modules/config/malloc.nix+7
@@ -87,5 +87,12 @@ in
8787 environment.etc."ld-nix.so.preload".text = ''
8888 ${providerLibPath}
8989 '';
90+ security.apparmor.includes = {
91+ "abstractions/base" = ''
92+ r /etc/ld-nix.so.preload,
93+ r ${config.environment.etc."ld-nix.so.preload".source},
94+ mr ${providerLibPath},
95+ '';
96+ };
9097 };
9198}
nixos/modules/module-list.nix−1
@@ -184,7 +184,6 @@
184184 ./rename.nix
185185 ./security/acme.nix
186186 ./security/apparmor.nix
187− ./security/apparmor-suid.nix
188187 ./security/audit.nix
189188 ./security/auditd.nix
190189 ./security/ca.nix
nixos/modules/security/apparmor-suid.nixdeleted−49
@@ -1,49 +0,0 @@
1−{ config, lib, pkgs, ... }:
2−let
3− cfg = config.security.apparmor;
4−in
5−with lib;
6−{
7− imports = [
8− (mkRenamedOptionModule [ "security" "virtualization" "flushL1DataCache" ] [ "security" "virtualisation" "flushL1DataCache" ])
9− ];
10−
11− options.security.apparmor.confineSUIDApplications = mkOption {
12− type = types.bool;
13− default = true;
14− description = ''
15− Install AppArmor profiles for commonly-used SUID application
16− to mitigate potential privilege escalation attacks due to bugs
17− in such applications.
18−
19− Currently available profiles: ping
20− '';
21− };
22−
23− config = mkIf (cfg.confineSUIDApplications) {
24− security.apparmor.profiles = [ (pkgs.writeText "ping" ''
25− #include <tunables/global>
26− /run/wrappers/bin/ping {
27− #include <abstractions/base>
28− #include <abstractions/consoles>
29− #include <abstractions/nameservice>
30−
31− capability net_raw,
32− capability setuid,
33− network inet raw,
34−
35− ${pkgs.stdenv.cc.libc.out}/lib/*.so mr,
36− ${pkgs.libcap.lib}/lib/libcap.so* mr,
37− ${pkgs.attr.out}/lib/libattr.so* mr,
38−
39− ${pkgs.iputils}/bin/ping mixr,
40−
41− #/etc/modules.conf r,
42−
43− ## Site-specific additions and overrides. See local/README for details.
44− ##include <local/bin.ping>
45− }
46− '') ];
47− };
48−
49−}
nixos/modules/security/apparmor.nix+187−54
@@ -1,65 +1,198 @@
11{ config, lib, pkgs, ... }:
22
33let
4− inherit (lib) mkIf mkOption types concatMapStrings;
4+ inherit (builtins) attrNames head map match readFile;
5+ inherit (lib) types;
6+ inherit (config.environment) etc;
57 cfg = config.security.apparmor;
8+ mkDisableOption = name: lib.mkEnableOption name // {
9+ default = true;
10+ example = false;
11+ };
12+ enabledPolicies = lib.filterAttrs (n: p: p.enable) cfg.policies;
613in
714
815{
9− options = {
10− security.apparmor = {
11− enable = mkOption {
12− type = types.bool;
13− default = false;
14− description = "Enable the AppArmor Mandatory Access Control system.";
15− };
16− profiles = mkOption {
17− type = types.listOf types.path;
18− default = [];
19− description = "List of files containing AppArmor profiles.";
20− };
21− packages = mkOption {
22− type = types.listOf types.package;
23− default = [];
24− description = "List of packages to be added to apparmor's include path";
25− };
26− parserConfig = mkOption {
27− type = types.str;
28− default = "";
29− description = "AppArmor parser configuration file content";
30− };
31− };
32− };
16+ imports = [
17+ (lib.mkRenamedOptionModule [ "security" "virtualization" "flushL1DataCache" ] [ "security" "virtualisation" "flushL1DataCache" ])
18+ (lib.mkRemovedOptionModule [ "security" "apparmor" "confineSUIDApplications" ] "Please use the new options: `security.apparmor.policies.<policy>.enable'.")
19+ (lib.mkRemovedOptionModule [ "security" "apparmor" "profiles" ] "Please use the new option: `security.apparmor.policies'.")
20+ apparmor/includes.nix
21+ apparmor/profiles.nix
22+ ];
3323
34− config = mkIf cfg.enable {
35− environment.systemPackages = [ pkgs.apparmor-utils ];
36− environment.etc."apparmor/parser.conf".text = cfg.parserConfig;
24+ options = {
25+ security.apparmor = {
26+ enable = lib.mkEnableOption ''the AppArmor Mandatory Access Control system.
3727
38− boot.kernelParams = [ "apparmor=1" "security=apparmor" ];
28+ If you're enabling this module on a running system,
29+ note that a reboot will be required to activate AppArmor in the kernel.
3930
40− systemd.services.apparmor = let
41− paths = concatMapStrings (s: " -I ${s}/etc/apparmor.d")
42− ([ pkgs.apparmor-profiles ] ++ cfg.packages);
43− in {
44− after = [ "local-fs.target" ];
45− before = [ "sysinit.target" ];
46− wantedBy = [ "multi-user.target" ];
47− unitConfig = {
48− DefaultDependencies = "no";
49− };
50− serviceConfig = {
51− Type = "oneshot";
52− RemainAfterExit = "yes";
53− ExecStart = map (p:
54− ''${pkgs.apparmor-parser}/bin/apparmor_parser -rKv ${paths} "${p}"''
55− ) cfg.profiles;
56− ExecStop = map (p:
57− ''${pkgs.apparmor-parser}/bin/apparmor_parser -Rv "${p}"''
58− ) cfg.profiles;
59− ExecReload = map (p:
60− ''${pkgs.apparmor-parser}/bin/apparmor_parser --reload ${paths} "${p}"''
61− ) cfg.profiles;
62− };
63− };
64− };
31+ Also, beware that enabling this module will by default
32+ try to kill unconfined but confinable running processes,
33+ in order to obtain a confinement matching what is declared in the NixOS configuration.
34+ This will happen when upgrading to a NixOS revision
35+ introducing an AppArmor profile for the executable of a running process.
36+ This is because enabling an AppArmor profile for an executable
37+ can only confine new or already confined processes of that executable,
38+ but leaves already running processes unconfined.
39+ Set <link linkend="opt-security.apparmor.killUnconfinedConfinables">killUnconfinedConfinables</link>
40+ to <literal>false</literal> if you prefer to leave those processes running'';
41+ policies = lib.mkOption {
42+ description = ''
43+ AppArmor policies.
44+ '';
45+ type = types.attrsOf (types.submodule ({ name, config, ... }: {
46+ options = {
47+ enable = mkDisableOption "loading of the profile into the kernel";
48+ enforce = mkDisableOption "enforcing of the policy or only complain in the logs";
49+ profile = lib.mkOption {
50+ description = "The policy of the profile.";
51+ type = types.lines;
52+ apply = pkgs.writeText name;
53+ };
54+ };
55+ }));
56+ default = {};
57+ };
58+ includes = lib.mkOption {
59+ type = types.attrsOf types.lines;
60+ default = {};
61+ description = ''
62+ List of paths to be added to AppArmor's searched paths
63+ when resolving <literal>include</literal> directives.
64+ '';
65+ apply = lib.mapAttrs pkgs.writeText;
66+ };
67+ packages = lib.mkOption {
68+ type = types.listOf types.package;
69+ default = [];
70+ description = "List of packages to be added to AppArmor's include path";
71+ };
72+ enableCache = lib.mkEnableOption ''caching of AppArmor policies
73+ in <literal>/var/cache/apparmor/</literal>.
74+
75+ Beware that AppArmor policies almost always contain Nix store paths,
76+ and thus produce at each change of these paths
77+ a new cached version accumulating in the cache'';
78+ killUnconfinedConfinables = mkDisableOption ''killing of processes
79+ which have an AppArmor profile enabled
80+ (in <link linkend="opt-security.apparmor.policies">policies</link>)
81+ but are not confined (because AppArmor can only confine new processes).
82+ Beware that due to a current limitation of AppArmor,
83+ only profiles with exact paths (and no name) can enable such kills'';
84+ };
85+ };
86+
87+ config = lib.mkIf cfg.enable {
88+ assertions = map (policy:
89+ { assertion = match ".*/.*" policy == null;
90+ message = "`security.apparmor.policies.\"${policy}\"' must not contain a slash.";
91+ # Because, for instance, aa-remove-unknown uses profiles_names_list() in rc.apparmor.functions
92+ # which does not recurse into sub-directories.
93+ }
94+ ) (attrNames cfg.policies);
95+
96+ environment.systemPackages = [ pkgs.apparmor-utils ];
97+ environment.etc."apparmor.d".source = pkgs.linkFarm "apparmor.d" (
98+ # It's important to put only enabledPolicies here and not all cfg.policies
99+ # because aa-remove-unknown reads profiles from all /etc/apparmor.d/*
100+ lib.mapAttrsToList (name: p: {inherit name; path=p.profile;}) enabledPolicies ++
101+ lib.mapAttrsToList (name: path: {inherit name path;}) cfg.includes
102+ );
103+ environment.etc."apparmor/parser.conf".text = ''
104+ ${if cfg.enableCache then "write-cache" else "skip-cache"}
105+ cache-loc /var/cache/apparmor
106+ Include /etc/apparmor.d
107+ '' +
108+ lib.concatMapStrings (p: "Include ${p}/etc/apparmor.d\n") cfg.packages;
109+ # For aa-logprof
110+ environment.etc."apparmor/apparmor.conf".text = ''
111+ '';
112+ # For aa-logprof
113+ environment.etc."apparmor/severity.db".source = pkgs.apparmor-utils + "/etc/apparmor/severity.db";
114+ environment.etc."apparmor/logprof.conf".text = ''
115+ [settings]
116+ # /etc/apparmor.d/ is read-only on NixOS
117+ profiledir = /var/cache/apparmor/logprof
118+ inactive_profiledir = /etc/apparmor.d/disable
119+ # Use: journalctl -b --since today --grep audit: | aa-logprof
120+ logfiles = /dev/stdin
121+
122+ parser = ${pkgs.apparmor-parser}/bin/apparmor_parser
123+ ldd = ${pkgs.glibc.bin}/bin/ldd
124+ logger = ${pkgs.utillinux}/bin/logger
125+
126+ # customize how file ownership permissions are presented
127+ # 0 - off
128+ # 1 - default of what ever mode the log reported
129+ # 2 - force the new permissions to be user
130+ # 3 - force all perms on the rule to be user
131+ default_owner_prompt = 1
132+
133+ custom_includes = /etc/apparmor.d ${lib.concatMapStringsSep " " (p: "${p}/etc/apparmor.d") cfg.packages}
134+
135+ [qualifiers]
136+ ${pkgs.runtimeShell} = icnu
137+ ${pkgs.bashInteractive}/bin/sh = icnu
138+ ${pkgs.bashInteractive}/bin/bash = icnu
139+ '' + head (match "^.*\\[qualifiers](.*)" # Drop the original [settings] section.
140+ (readFile "${pkgs.apparmor-utils}/etc/apparmor/logprof.conf"));
141+
142+ boot.kernelParams = [ "apparmor=1" "security=apparmor" ];
143+
144+ systemd.services.apparmor = {
145+ after = [
146+ "local-fs.target"
147+ "systemd-journald-audit.socket"
148+ ];
149+ before = [ "sysinit.target" ];
150+ wantedBy = [ "multi-user.target" ];
151+ unitConfig = {
152+ Description="Load AppArmor policies";
153+ DefaultDependencies = "no";
154+ ConditionSecurity = "apparmor";
155+ };
156+ # Reloading instead of restarting enables to load new AppArmor profiles
157+ # without necessarily restarting all services which have Requires=apparmor.service
158+ reloadIfChanged = true;
159+ restartTriggers = [
160+ etc."apparmor/parser.conf".source
161+ etc."apparmor.d".source
162+ ];
163+ serviceConfig = let
164+ killUnconfinedConfinables = pkgs.writeShellScript "apparmor-kill" ''
165+ set -eu
166+ ${pkgs.apparmor-utils}/bin/aa-status --json |
167+ ${pkgs.jq}/bin/jq --raw-output '.processes | .[] | .[] | select (.status == "unconfined") | .pid' |
168+ xargs --verbose --no-run-if-empty --delimiter='\n' \
169+ kill
170+ '';
171+ commonOpts = p: "--verbose --show-cache ${lib.optionalString (!p.enforce) "--complain "}${p.profile}";
172+ in {
173+ Type = "oneshot";
174+ RemainAfterExit = "yes";
175+ ExecStartPre = "${pkgs.apparmor-utils}/bin/aa-teardown";
176+ ExecStart = lib.mapAttrsToList (n: p: "${pkgs.apparmor-parser}/bin/apparmor_parser --add ${commonOpts p}") enabledPolicies;
177+ ExecStartPost = lib.optional cfg.killUnconfinedConfinables killUnconfinedConfinables;
178+ ExecReload =
179+ # Add or replace into the kernel profiles in enabledPolicies
180+ # (because AppArmor can do that without stopping the processes already confined).
181+ lib.mapAttrsToList (n: p: "${pkgs.apparmor-parser}/bin/apparmor_parser --replace ${commonOpts p}") enabledPolicies ++
182+ # Remove from the kernel any profile whose name is not
183+ # one of the names within the content of the profiles in enabledPolicies
184+ # (indirectly read from /etc/apparmor.d/*, without recursing into sub-directory).
185+ # Note that this does not remove profiles dynamically generated by libvirt.
186+ [ "${pkgs.apparmor-utils}/bin/aa-remove-unknown" ] ++
187+ # Optionaly kill the processes which are unconfined but now have a profile loaded
188+ # (because AppArmor can only start to confine new processes).
189+ lib.optional cfg.killUnconfinedConfinables killUnconfinedConfinables;
190+ ExecStop = "${pkgs.apparmor-utils}/bin/aa-teardown";
191+ CacheDirectory = [ "apparmor" "apparmor/logprof" ];
192+ CacheDirectoryMode = "0700";
193+ };
194+ };
195+ };
196+
197+ meta.maintainers = with lib.maintainers; [ julm ];
65198}
nixos/modules/security/apparmor/includes.nixnew file+301
@@ -0,0 +1,301 @@
1+{ config, lib, pkgs, ... }:
2+let
3+ inherit (builtins) attrNames hasAttr isAttrs;
4+ inherit (lib) getLib;
5+ inherit (config.environment) etc;
6+ etcRule = arg:
7+ let go = {path ? null, mode ? "r", trail ? ""}:
8+ lib.optionalString (hasAttr path etc)
9+ "${mode} ${config.environment.etc.${path}.source}${trail},";
10+ in if isAttrs arg
11+ then go arg
12+ else go {path=arg;};
13+in
14+{
15+# FIXME: most of the etcRule calls below have been
16+# written systematically by converting from apparmor-profiles's profiles
17+# without testing nor deep understanding of their uses,
18+# and thus may need more rules or can have less rules;
19+# this remains to be determined case by case,
20+# some may even be completely useless.
21+config.security.apparmor.includes = {
22+ # This one is included by <tunables/global>
23+ # which is usualy included before any profile.
24+ "abstractions/tunables/alias" = ''
25+ alias /bin -> /run/current-system/sw/bin,
26+ alias /lib/modules -> /run/current-system/kernel/lib/modules,
27+ alias /sbin -> /run/current-system/sw/sbin,
28+ alias /usr -> /run/current-system/sw,
29+ '';
30+ "abstractions/audio" = ''
31+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/audio"
32+ ${etcRule "asound.conf"}
33+ ${etcRule "esound/esd.conf"}
34+ ${etcRule "libao.conf"}
35+ ${etcRule {path="pulse"; trail="/";}}
36+ ${etcRule {path="pulse"; trail="/**";}}
37+ ${etcRule {path="sound"; trail="/";}}
38+ ${etcRule {path="sound"; trail="/**";}}
39+ ${etcRule {path="alsa/conf.d"; trail="/";}}
40+ ${etcRule {path="alsa/conf.d"; trail="/*";}}
41+ ${etcRule "openal/alsoft.conf"}
42+ ${etcRule "wildmidi/wildmidi.conf"}
43+ '';
44+ "abstractions/authentication" = ''
45+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/authentication"
46+ # Defined in security.pam
47+ include <abstractions/pam>
48+ ${etcRule "nologin"}
49+ ${etcRule "securetty"}
50+ ${etcRule {path="security"; trail="/*";}}
51+ ${etcRule "shadow"}
52+ ${etcRule "gshadow"}
53+ ${etcRule "pwdb.conf"}
54+ ${etcRule "default/passwd"}
55+ ${etcRule "login.defs"}
56+ '';
57+ "abstractions/base" = ''
58+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/base"
59+ r ${pkgs.stdenv.cc.libc}/share/locale/**,
60+ r ${pkgs.stdenv.cc.libc}/share/locale.alias,
61+ ${lib.optionalString (pkgs.glibcLocales != null) "r ${pkgs.glibcLocales}/lib/locale/locale-archive,"}
62+ ${etcRule "localtime"}
63+ r ${pkgs.tzdata}/share/zoneinfo/**,
64+ r ${pkgs.stdenv.cc.libc}/share/i18n/**,
65+ '';
66+ "abstractions/bash" = ''
67+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/bash"
68+ # system-wide bash configuration
69+ ${etcRule "profile.dos"}
70+ ${etcRule "profile"}
71+ ${etcRule "profile.d"}
72+ ${etcRule {path="profile.d"; trail="/*";}}
73+ ${etcRule "bashrc"}
74+ ${etcRule "bash.bashrc"}
75+ ${etcRule "bash.bashrc.local"}
76+ ${etcRule "bash_completion"}
77+ ${etcRule "bash_completion.d"}
78+ ${etcRule {path="bash_completion.d"; trail="/*";}}
79+ # bash relies on system-wide readline configuration
80+ ${etcRule "inputrc"}
81+ # bash inspects filesystems at startup
82+ # and /etc/mtab is linked to /proc/mounts
83+ @{PROC}/mounts
84+
85+ # run out of /etc/bash.bashrc
86+ ${etcRule "DIR_COLORS"}
87+ '';
88+ "abstractions/cups-client" = ''
89+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/cpus-client"
90+ ${etcRule "cups/cups-client.conf"}
91+ '';
92+ "abstractions/consoles" = ''
93+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/consoles"
94+ '';
95+ "abstractions/dbus-session-strict" = ''
96+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/dbus-session-strict"
97+ ${etcRule "machine-id"}
98+ '';
99+ "abstractions/dconf" = ''
100+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/dconf"
101+ ${etcRule {path="dconf"; trail="/**";}}
102+ '';
103+ "abstractions/dri-common" = ''
104+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/dri-common"
105+ ${etcRule "drirc"}
106+ '';
107+ # The config.fonts.fontconfig NixOS module adds many files to /etc/fonts/
108+ # by symlinking them but without exporting them outside of its NixOS module,
109+ # those are therefore added there to this "abstractions/fonts".
110+ "abstractions/fonts" = ''
111+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/fonts"
112+ ${etcRule {path="fonts"; trail="/**";}}
113+ '';
114+ "abstractions/gnome" = ''
115+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/gnome"
116+ ${etcRule {path="gnome"; trail="/gtkrc*";}}
117+ ${etcRule {path="gtk"; trail="/*";}}
118+ ${etcRule {path="gtk-2.0"; trail="/*";}}
119+ ${etcRule {path="gtk-3.0"; trail="/*";}}
120+ ${etcRule "orbitrc"}
121+ include <abstractions/fonts>
122+ ${etcRule {path="pango"; trail="/*";}}
123+ ${etcRule {path="/etc/gnome-vfs-2.0"; trail="/modules/";}}
124+ ${etcRule {path="/etc/gnome-vfs-2.0"; trail="/modules/*";}}
125+ ${etcRule "papersize"}
126+ ${etcRule {path="cups"; trail="/lpoptions";}}
127+ ${etcRule {path="gnome"; trail="/defaults.list";}}
128+ ${etcRule {path="xdg"; trail="/{,*-}mimeapps.list";}}
129+ ${etcRule "xdg/mimeapps.list"}
130+ '';
131+ "abstractions/kde" = ''
132+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/kde"
133+ ${etcRule {path="qt3"; trail="/kstylerc";}}
134+ ${etcRule {path="qt3"; trail="/qt_plugins_3.3rc";}}
135+ ${etcRule {path="qt3"; trail="/qtrc";}}
136+ ${etcRule "kderc"}
137+ ${etcRule {path="kde3"; trail="/*";}}
138+ ${etcRule "kde4rc"}
139+ ${etcRule {path="xdg"; trail="/kdeglobals";}}
140+ ${etcRule {path="xdg"; trail="/Trolltech.conf";}}
141+ '';
142+ "abstractions/kerberosclient" = ''
143+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/kerberosclient"
144+ ${etcRule {path="krb5.keytab"; mode="rk";}}
145+ ${etcRule "krb5.conf"}
146+ ${etcRule "krb5.conf.d"}
147+ ${etcRule {path="krb5.conf.d"; trail="/*";}}
148+
149+ # config files found via strings on libs
150+ ${etcRule "krb.conf"}
151+ ${etcRule "krb.realms"}
152+ ${etcRule "srvtab"}
153+ '';
154+ "abstractions/ldapclient" = ''
155+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/ldapclient"
156+ ${etcRule "ldap.conf"}
157+ ${etcRule "ldap.secret"}
158+ ${etcRule {path="openldap"; trail="/*";}}
159+ ${etcRule {path="openldap"; trail="/cacerts/*";}}
160+ ${etcRule {path="sasl2"; trail="/*";}}
161+ '';
162+ "abstractions/likewise" = ''
163+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/likewise"
164+ '';
165+ "abstractions/mdns" = ''
166+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/mdns"
167+ ${etcRule "nss_mdns.conf"}
168+ '';
169+ "abstractions/nameservice" = ''
170+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/nameservice"
171+
172+ # Many programs wish to perform nameservice-like operations, such as
173+ # looking up users by name or id, groups by name or id, hosts by name
174+ # or IP, etc. These operations may be performed through files, dns,
175+ # NIS, NIS+, LDAP, hesiod, wins, etc. Allow them all here.
176+ ${etcRule "group"}
177+ ${etcRule "host.conf"}
178+ ${etcRule "hosts"}
179+ ${etcRule "nsswitch.conf"}
180+ ${etcRule "gai.conf"}
181+ ${etcRule "passwd"}
182+ ${etcRule "protocols"}
183+
184+ # libtirpc (used for NIS/YP login) needs this
185+ ${etcRule "netconfig"}
186+
187+ ${etcRule "resolv.conf"}
188+
189+ ${etcRule {path="samba"; trail="/lmhosts";}}
190+ ${etcRule "services"}
191+
192+ ${etcRule "default/nss"}
193+
194+ # libnl-3-200 via libnss-gw-name
195+ ${etcRule {path="libnl"; trail="/classid";}}
196+ ${etcRule {path="libnl-3"; trail="/classid";}}
197+
198+ mr ${getLib pkgs.nss}/lib/libnss_*.so*,
199+ mr ${getLib pkgs.nss}/lib64/libnss_*.so*,
200+ '';
201+ "abstractions/nis" = ''
202+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/nis"
203+ '';
204+ "abstractions/nvidia" = ''
205+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/nvidia"
206+ ${etcRule "vdpau_wrapper.cfg"}
207+ '';
208+ "abstractions/opencl-common" = ''
209+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/opencl-common"
210+ ${etcRule {path="OpenCL"; trail="/**";}}
211+ '';
212+ "abstractions/opencl-mesa" = ''
213+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/opencl-mesa"
214+ ${etcRule "default/drirc"}
215+ '';
216+ "abstractions/openssl" = ''
217+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/openssl"
218+ ${etcRule {path="ssl"; trail="/openssl.cnf";}}
219+ '';
220+ "abstractions/p11-kit" = ''
221+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/p11-kit"
222+ ${etcRule {path="pkcs11"; trail="/";}}
223+ ${etcRule {path="pkcs11"; trail="/pkcs11.conf";}}
224+ ${etcRule {path="pkcs11"; trail="/modules/";}}
225+ ${etcRule {path="pkcs11"; trail="/modules/*";}}
226+ '';
227+ "abstractions/perl" = ''
228+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/perl"
229+ ${etcRule {path="perl"; trail="/**";}}
230+ '';
231+ "abstractions/php" = ''
232+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/php"
233+ ${etcRule {path="php"; trail="/**/";}}
234+ ${etcRule {path="php5"; trail="/**/";}}
235+ ${etcRule {path="php7"; trail="/**/";}}
236+ ${etcRule {path="php"; trail="/**.ini";}}
237+ ${etcRule {path="php5"; trail="/**.ini";}}
238+ ${etcRule {path="php7"; trail="/**.ini";}}
239+ '';
240+ "abstractions/postfix-common" = ''
241+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/postfix-common"
242+ ${etcRule "mailname"}
243+ ${etcRule {path="postfix"; trail="/*.cf";}}
244+ ${etcRule "postfix/main.cf"}
245+ ${etcRule "postfix/master.cf"}
246+ '';
247+ "abstractions/python" = ''
248+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/python"
249+ '';
250+ "abstractions/qt5" = ''
251+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/qt5"
252+ ${etcRule {path="xdg"; trail="/QtProject/qtlogging.ini";}}
253+ ${etcRule {path="xdg/QtProject"; trail="/qtlogging.ini";}}
254+ ${etcRule "xdg/QtProject/qtlogging.ini"}
255+ '';
256+ "abstractions/samba" = ''
257+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/samba"
258+ ${etcRule {path="samba"; trail="/*";}}
259+ '';
260+ "abstractions/ssl_certs" = ''
261+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/ssl_certs"
262+ ${etcRule "ssl/certs/ca-certificates.crt"}
263+ ${etcRule "ssl/certs/ca-bundle.crt"}
264+ ${etcRule "pki/tls/certs/ca-bundle.crt"}
265+
266+ ${etcRule {path="ssl/trust"; trail="/";}}
267+ ${etcRule {path="ssl/trust"; trail="/*";}}
268+ ${etcRule {path="ssl/trust/anchors"; trail="/";}}
269+ ${etcRule {path="ssl/trust/anchors"; trail="/**";}}
270+ ${etcRule {path="pki/trust"; trail="/";}}
271+ ${etcRule {path="pki/trust"; trail="/*";}}
272+ ${etcRule {path="pki/trust/anchors"; trail="/";}}
273+ ${etcRule {path="pki/trust/anchors"; trail="/**";}}
274+
275+ # security.acme NixOS module
276+ r /var/lib/acme/*/cert.pem,
277+ r /var/lib/acme/*/chain.pem,
278+ r /var/lib/acme/*/fullchain.pem,
279+ '';
280+ "abstractions/ssl_keys" = ''
281+ # security.acme NixOS module
282+ r /var/lib/acme/*/full.pem,
283+ r /var/lib/acme/*/key.pem,
284+ '';
285+ "abstractions/vulkan" = ''
286+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/vulkan"
287+ ${etcRule {path="vulkan/icd.d"; trail="/";}}
288+ ${etcRule {path="vulkan/icd.d"; trail="/*.json";}}
289+ '';
290+ "abstractions/winbind" = ''
291+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/winbind"
292+ ${etcRule {path="samba"; trail="/smb.conf";}}
293+ ${etcRule {path="samba"; trail="/dhcp.conf";}}
294+ '';
295+ "abstractions/X" = ''
296+ include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/X"
297+ ${etcRule {path="X11/cursors"; trail="/";}}
298+ ${etcRule {path="X11/cursors"; trail="/**";}}
299+ '';
300+};
301+}
nixos/modules/security/apparmor/profiles.nixnew file+11
@@ -0,0 +1,11 @@
1+{ config, lib, pkgs, ... }:
2+let apparmor = config.security.apparmor; in
3+{
4+config.security.apparmor.packages = [ pkgs.apparmor-profiles ];
5+config.security.apparmor.policies."bin.ping".profile = lib.mkIf apparmor.policies."bin.ping".enable ''
6+ include "${pkgs.iputils.apparmor}/bin.ping"
7+ include "${pkgs.inetutils.apparmor}/bin.ping"
8+ # Note that including those two profiles in the same profile
9+ # would not work if the second one were to re-include <tunables/global>.
10+'';
11+}
nixos/modules/security/pam.nix+57
@@ -836,6 +836,63 @@ in
836836 runuser-l = { rootOK = true; unixAuth = false; };
837837 };
838838
839+ security.apparmor.includes."abstractions/pam" = let
840+ isEnabled = test: fold or false (map test (attrValues config.security.pam.services));
841+ in ''
842+ ${lib.concatMapStringsSep "\n"
843+ (name: "r ${config.environment.etc."pam.d/${name}".source},")
844+ (attrNames config.security.pam.services)}
845+ mr ${getLib pkgs.pam}/lib/security/pam_filter/*,
846+ mr ${getLib pkgs.pam}/lib/security/pam_*.so,
847+ r ${getLib pkgs.pam}/lib/security/,
848+ ${optionalString use_ldap
849+ "mr ${pam_ldap}/lib/security/pam_ldap.so,"}
850+ ${optionalString config.services.sssd.enable
851+ "mr ${pkgs.sssd}/lib/security/pam_sss.so,"}
852+ ${optionalString config.krb5.enable ''
853+ mr ${pam_krb5}/lib/security/pam_krb5.so,
854+ mr ${pam_ccreds}/lib/security/pam_ccreds.so,
855+ ''}
856+ ${optionalString (isEnabled (cfg: cfg.googleOsLoginAccountVerification)) ''
857+ mr ${pkgs.google-compute-engine-oslogin}/lib/pam_oslogin_login.so,
858+ mr ${pkgs.google-compute-engine-oslogin}/lib/pam_oslogin_admin.so,
859+ ''}
860+ ${optionalString (isEnabled (cfg: cfg.googleOsLoginAuthentication))
861+ "mr ${pkgs.google-compute-engine-oslogin}/lib/pam_oslogin_login.so,"}
862+ ${optionalString (config.security.pam.enableSSHAgentAuth && isEnabled (cfg: cfg.sshAgentAuth))
863+ "mr ${pkgs.pam_ssh_agent_auth}/libexec/pam_ssh_agent_auth.so,"}
864+ ${optionalString (isEnabled (cfg: cfg.fprintAuth))
865+ "mr ${pkgs.fprintd}/lib/security/pam_fprintd.so,"}
866+ ${optionalString (isEnabled (cfg: cfg.u2fAuth))
867+ "mr ${pkgs.pam_u2f}/lib/security/pam_u2f.so,"}
868+ ${optionalString (isEnabled (cfg: cfg.usbAuth))
869+ "mr ${pkgs.pam_usb}/lib/security/pam_usb.so,"}
870+ ${optionalString (isEnabled (cfg: cfg.oathAuth))
871+ "mr ${pkgs.oathToolkit}/lib/security/pam_oath.so,"}
872+ ${optionalString (isEnabled (cfg: cfg.yubicoAuth))
873+ "mr ${pkgs.yubico-pam}/lib/security/pam_yubico.so,"}
874+ ${optionalString (isEnabled (cfg: cfg.duoSecurity.enable))
875+ "mr ${pkgs.duo-unix}/lib/security/pam_duo.so,"}
876+ ${optionalString (isEnabled (cfg: cfg.otpwAuth))
877+ "mr ${pkgs.otpw}/lib/security/pam_otpw.so,"}
878+ ${optionalString config.security.pam.enableEcryptfs
879+ "mr ${pkgs.ecryptfs}/lib/security/pam_ecryptfs.so,"}
880+ ${optionalString (isEnabled (cfg: cfg.pamMount))
881+ "mr ${pkgs.pam_mount}/lib/security/pam_mount.so,"}
882+ ${optionalString config.services.samba.syncPasswordsByPam
883+ "mr ${pkgs.samba}/lib/security/pam_smbpass.so,"}
884+ ${optionalString (isEnabled (cfg: cfg.enableGnomeKeyring))
885+ "mr ${pkgs.gnome3.gnome-keyring}/lib/security/pam_gnome_keyring.so,"}
886+ ${optionalString (isEnabled (cfg: cfg.startSession))
887+ "mr ${pkgs.systemd}/lib/security/pam_systemd.so,"}
888+ ${optionalString (isEnabled (cfg: cfg.enableAppArmor) && config.security.apparmor.enable)
889+ "mr ${pkgs.apparmor-pam}/lib/security/pam_apparmor.so,"}
890+ ${optionalString (isEnabled (cfg: cfg.enableKwallet))
891+ "mr ${pkgs.plasma5.kwallet-pam}/lib/security/pam_kwallet5.so,"}
892+ ${optionalString config.virtualisation.lxc.lxcfs.enable
893+ "mr ${pkgs.lxc}/lib/security/pam_cgfs.so"}
894+ '';
895+
839896 };
840897
841898}
nixos/modules/security/wrappers/default.nix+8
@@ -179,6 +179,14 @@ in
179179 export PATH="${wrapperDir}:$PATH"
180180 '';
181181
182+ security.apparmor.includes."nixos/security.wrappers" = ''
183+ include "${pkgs.apparmorRulesFromClosure {} [
184+ securityWrapper
185+ pkgs.stdenv.cc.cc
186+ pkgs.stdenv.cc.libc
187+ ]}"
188+ '';
189+
182190 ###### setcap activation script
183191 system.activationScripts.wrappers =
184192 lib.stringAfter [ "specialfs" "users" ]
nixos/modules/services/torrent/transmission.nix+10−53
@@ -5,7 +5,7 @@ with lib;
55let
66 cfg = config.services.transmission;
77 inherit (config.environment) etc;
8− apparmor = config.security.apparmor.enable;
8+ apparmor = config.security.apparmor;
99 rootDir = "/run/transmission";
1010 homeDir = "/var/lib/transmission";
1111 settingsDir = ".config/transmission-daemon";
@@ -184,8 +184,8 @@ in
184184
185185 systemd.services.transmission = {
186186 description = "Transmission BitTorrent Service";
187− after = [ "network.target" ] ++ optional apparmor "apparmor.service";
188− requires = optional apparmor "apparmor.service";
187+ after = [ "network.target" ] ++ optional apparmor.enable "apparmor.service";
188+ requires = optional apparmor.enable "apparmor.service";
189189 wantedBy = [ "multi-user.target" ];
190190 environment.CURL_CA_BUNDLE = etc."ssl/certs/ca-certificates.crt".source;
191191
@@ -357,61 +357,21 @@ in
357357 })
358358 ];
359359
360− security.apparmor.profiles = mkIf apparmor [
361− (pkgs.writeText "apparmor-transmission-daemon" ''
360+ security.apparmor.policies."bin.transmission-daemon".profile = ''
362361 include <tunables/global>
363−
364362 ${pkgs.transmission}/bin/transmission-daemon {
365363 include <abstractions/base>
366364 include <abstractions/nameservice>
367−
368− # NOTE: https://github.com/NixOS/nixpkgs/pull/93457
369− # will remove the need for these by fixing <abstractions/base>
370− r ${etc."hosts".source},
371− r /etc/ld-nix.so.preload,
372− ${lib.optionalString (builtins.hasAttr "ld-nix.so.preload" etc) ''
373− r ${etc."ld-nix.so.preload".source},
374− ${concatMapStrings (p: optionalString (p != "") ("mr ${p},\n"))
375− (splitString "\n" config.environment.etc."ld-nix.so.preload".text)}
376− ''}
377− r ${etc."ssl/certs/ca-certificates.crt".source},
378− r ${pkgs.tzdata}/share/zoneinfo/**,
379− r ${pkgs.stdenv.cc.libc}/share/i18n/**,
380− r ${pkgs.stdenv.cc.libc}/share/locale/**,
381−
382− mr ${getLib pkgs.stdenv.cc.cc}/lib/*.so*,
383− mr ${getLib pkgs.stdenv.cc.libc}/lib/*.so*,
384− mr ${getLib pkgs.attr}/lib/libattr*.so*,
385− mr ${getLib pkgs.c-ares}/lib/libcares*.so*,
386− mr ${getLib pkgs.curl}/lib/libcurl*.so*,
387− mr ${getLib pkgs.keyutils}/lib/libkeyutils*.so*,
388− mr ${getLib pkgs.libcap}/lib/libcap*.so*,
389− mr ${getLib pkgs.libevent}/lib/libevent*.so*,
390− mr ${getLib pkgs.libgcrypt}/lib/libgcrypt*.so*,
391− mr ${getLib pkgs.libgpgerror}/lib/libgpg-error*.so*,
392− mr ${getLib pkgs.libkrb5}/lib/lib*.so*,
393− mr ${getLib pkgs.libssh2}/lib/libssh2*.so*,
394− mr ${getLib pkgs.lz4}/lib/liblz4*.so*,
395− mr ${getLib pkgs.nghttp2}/lib/libnghttp2*.so*,
396− mr ${getLib pkgs.openssl}/lib/libcrypto*.so*,
397− mr ${getLib pkgs.openssl}/lib/libssl*.so*,
398− mr ${getLib pkgs.systemd}/lib/libsystemd*.so*,
399− mr ${getLib pkgs.utillinuxMinimal.out}/lib/libblkid.so*,
400− mr ${getLib pkgs.utillinuxMinimal.out}/lib/libmount.so*,
401− mr ${getLib pkgs.utillinuxMinimal.out}/lib/libuuid.so*,
402− mr ${getLib pkgs.xz}/lib/liblzma*.so*,
403− mr ${getLib pkgs.zlib}/lib/libz*.so*,
365+ include <abstractions/ssl_certs>
366+ include "${pkgs.apparmorRulesFromClosure {} [pkgs.transmission]}"
367+ include <local/bin.transmission-daemon>
404368
405369 r @{PROC}/sys/kernel/random/uuid,
406370 r @{PROC}/sys/vm/overcommit_memory,
407− # @{pid} is not a kernel variable yet but a regexp
408− #r @{PROC}/@{pid}/environ,
371+ r @{PROC}/@{pid}/environ,
409372 r @{PROC}/@{pid}/mounts,
410373 rwk /tmp/tr_session_id_*,
411−
412− r ${pkgs.openssl.out}/etc/**,
413374 r ${config.systemd.services.transmission.environment.CURL_CA_BUNDLE},
414− r ${pkgs.transmission}/share/transmission/**,
415375
416376 owner rw ${cfg.home}/${settingsDir}/**,
417377 rw ${cfg.settings.download-dir}/**,
@@ -439,12 +399,9 @@ in
439399 # https://gitlab.com/apparmor/apparmor/-/wikis/AppArmorStacking#seccomp-and-no_new_privs
440400 px ${cfg.settings.script-torrent-done-filename} -> &@{dirs},
441401 ''}
442−
443− # FIXME: enable customizing using https://github.com/NixOS/nixpkgs/pull/93457
444− # include <local/transmission-daemon>
445402 }
446− '')
447− ];
403+ '';
404+ security.apparmor.includes."local/bin.transmission-daemon" = "";
448405 };
449406
450407 meta.maintainers = with lib.maintainers; [ julm ];
nixos/modules/tasks/network-interfaces.nix+15
@@ -1089,6 +1089,21 @@ in
10891089 } else {
10901090 ping.source = "${pkgs.iputils.out}/bin/ping";
10911091 };
1092+ security.apparmor.policies."bin.ping".profile = lib.mkIf config.security.apparmor.policies."bin.ping".enable (lib.mkAfter ''
1093+ /run/wrappers/bin/ping {
1094+ include <abstractions/base>
1095+ include <nixos/security.wrappers>
1096+ rpx /run/wrappers/wrappers.*/ping,
1097+ }
1098+ /run/wrappers/wrappers.*/ping {
1099+ include <abstractions/base>
1100+ include <nixos/security.wrappers>
1101+ r /run/wrappers/wrappers.*/ping.real,
1102+ mrpx ${config.security.wrappers.ping.source},
1103+ capability net_raw,
1104+ capability setpcap,
1105+ }
1106+ '');
10921107
10931108 # Set the host and domain names in the activation script. Don't
10941109 # clear it if it's not configured in the NixOS configuration,
nixos/modules/virtualisation/lxc.nix+8−4
@@ -74,9 +74,13 @@ in
7474 systemd.tmpfiles.rules = [ "d /var/lib/lxc/rootfs 0755 root root -" ];
7575
7676 security.apparmor.packages = [ pkgs.lxc ];
77− security.apparmor.profiles = [
78− "${pkgs.lxc}/etc/apparmor.d/lxc-containers"
79− "${pkgs.lxc}/etc/apparmor.d/usr.bin.lxc-start"
80− ];
77+ security.apparmor.policies = {
78+ "bin.lxc-start".profile = ''
79+ include ${pkgs.lxc}/etc/apparmor.d/usr.bin.lxc-start
80+ '';
81+ "lxc-containers".profile = ''
82+ include ${pkgs.lxc}/etc/apparmor.d/lxc-containers
83+ '';
84+ };
8185 };
8286}
nixos/modules/virtualisation/lxd.nix+8−4
@@ -93,11 +93,15 @@ in
9393
9494 security.apparmor = {
9595 enable = true;
96− profiles = [
97− "${cfg.lxcPackage}/etc/apparmor.d/usr.bin.lxc-start"
98− "${cfg.lxcPackage}/etc/apparmor.d/lxc-containers"
99− ];
10096 packages = [ cfg.lxcPackage ];
97+ policies = {
98+ "bin.lxc-start".profile = ''
99+ include ${cfg.lxcPackage}/etc/apparmor.d/usr.bin.lxc-start
100+ '';
101+ "lxc-containers".profile = ''
102+ include ${cfg.lxcPackage}/etc/apparmor.d/lxc-containers
103+ '';
104+ };
101105 };
102106
103107 systemd.services.lxd = {
pkgs/os-specific/linux/apparmor/default.nix+52−2
@@ -10,6 +10,13 @@
1010, pam
1111, libnotify
1212, buildPackages
13+, coreutils
14+, gnugrep
15+, gnused
16+, kmod
17+, writeShellScript
18+, closureInfo
19+, runCommand
1320}:
1421
1522let
@@ -38,6 +45,12 @@ let
3845 sha256 = "0xw028iqp69j9mxv0kbwraplgkj5i5djdlgf0anpkc5cdbsf96r9";
3946 };
4047
48+ aa-teardown = writeShellScript "aa-teardown" ''
49+ PATH="${lib.makeBinPath [coreutils gnused gnugrep]}:$PATH"
50+ . ${apparmor-parser}/lib/apparmor/rc.apparmor.functions
51+ remove_profiles
52+ '';
53+
4154 prePatchCommon = ''
4255 patch -p1 < ${gnumake43Patch}
4356 chmod a+x ./common/list_capabilities.sh ./common/list_af_names.sh
@@ -149,6 +162,15 @@ let
149162 # aa-notify checks its name and does not work named ".aa-notify-wrapped"
150163 mv $out/bin/aa-notify $out/bin/aa-notify-wrapped
151164 makeWrapper ${perl}/bin/perl $out/bin/aa-notify --set PERL5LIB ${libapparmor}/${perl.libPrefix} --add-flags $out/bin/aa-notify-wrapped
165+
166+ substituteInPlace $out/bin/aa-remove-unknown \
167+ --replace "/usr/bin/aa-status" "$out/bin/aa-status" \
168+ --replace "/sbin/modprobe" "${kmod}/bin/modprobe" \
169+ --replace "/lib/apparmor/rc.apparmor.functions" "${apparmor-parser}/lib/apparmor/rc.apparmor.functions"
170+ wrapProgram $out/bin/aa-remove-unknown \
171+ --prefix PATH : ${lib.makeBinPath [gawk]}
172+
173+ ln -s ${aa-teardown} $out/bin/aa-teardown
152174 '';
153175
154176 inherit doCheck;
@@ -197,6 +219,9 @@ let
197219 substituteInPlace ./parser/Makefile --replace "/usr/include/linux/capability.h" "${linuxHeaders}/include/linux/capability.h"
198220 ## techdoc.pdf still doesn't build ...
199221 substituteInPlace ./parser/Makefile --replace "manpages htmlmanpages pdf" "manpages htmlmanpages"
222+ substituteInPlace parser/rc.apparmor.functions \
223+ --replace "/sbin/apparmor_parser" "$out/bin/apparmor_parser"
224+ sed -i parser/rc.apparmor.functions -e '2i . ${./fix-rc.apparmor.functions.sh}'
200225 '';
201226 inherit patches;
202227 postPatch = "cd ./parser";
@@ -258,8 +283,32 @@ let
258283 meta = apparmor-meta "kernel patches";
259284 };
260285
286+ # Generate generic AppArmor rules in a file,
287+ # from the closure of given rootPaths.
288+ # To be included in an AppArmor profile like so:
289+ # include "$(apparmorRulesFromClosure {} [pkgs.hello]}"
290+ apparmorRulesFromClosure =
291+ { # The store path of the derivation is given in $path
292+ additionalRules ? []
293+ # TODO: factorize here some other common paths
294+ # that may emerge from use cases.
295+ , baseRules ? [
296+ "r $path"
297+ "r $path/etc/**"
298+ "r $path/share/**"
299+ # Note that not all libraries are prefixed with "lib",
300+ # eg. glibc-2.30/lib/ld-2.30.so
301+ "mr $path/lib/**.so*"
302+ # eg. glibc-2.30/lib/gconv/gconv-modules
303+ "r $path/lib/**"
304+ ]
305+ }: rootPaths: runCommand "apparmor-closure-rules" {} ''
306+ touch $out
307+ while read -r path
308+ do printf >>$out "%s,\n" ${lib.concatMapStringsSep " " (x: "\"${x}\"") (baseRules ++ additionalRules)}
309+ done <${closureInfo {inherit rootPaths;}}/store-paths
310+ '';
261311in
262−
263312{
264313 inherit
265314 libapparmor
@@ -268,5 +317,6 @@ in
268317 apparmor-parser
269318 apparmor-pam
270319 apparmor-profiles
271− apparmor-kernel-patches;
320+ apparmor-kernel-patches
321+ apparmorRulesFromClosure;
272322}
pkgs/os-specific/linux/apparmor/fix-rc.apparmor.functions.shnew file+32
@@ -0,0 +1,32 @@
1+aa_action() {
2+ STRING=$1
3+ shift
4+ $*
5+ rc=$?
6+ if [ $rc -eq 0 ] ; then
7+ aa_log_success_msg $"$STRING "
8+ else
9+ aa_log_failure_msg $"$STRING "
10+ fi
11+ return $rc
12+}
13+
14+aa_log_success_msg() {
15+ [ -n "$1" ] && echo -n $1
16+ echo ": done."
17+}
18+
19+aa_log_warning_msg() {
20+ [ -n "$1" ] && echo -n $1
21+ echo ": Warning."
22+}
23+
24+aa_log_failure_msg() {
25+ [ -n "$1" ] && echo -n $1
26+ echo ": Failed."
27+}
28+
29+aa_log_skipped_msg() {
30+ [ -n "$1" ] && echo -n $1
31+ echo ": Skipped."
32+}
pkgs/os-specific/linux/iputils/default.nix+22
@@ -1,6 +1,7 @@
11{ stdenv, fetchFromGitHub
22, meson, ninja, pkgconfig, gettext, libxslt, docbook_xsl_ns
33, libcap, systemd, libidn2
4+, apparmorRulesFromClosure
45}:
56
67with stdenv.lib;
@@ -22,6 +23,8 @@ in stdenv.mkDerivation rec {
2223 sha256 = "1jhbcz75a4ij1myyyi110ma1d8d5hpm3scz9pyw7js6qym50xvh4";
2324 };
2425
26+ outputs = ["out" "apparmor"];
27+
2528 mesonFlags = [
2629 "-DBUILD_RARPD=true"
2730 "-DBUILD_TRACEROUTE6=true"
@@ -35,6 +38,25 @@ in stdenv.mkDerivation rec {
3538 nativeBuildInputs = [ meson ninja pkgconfig gettext libxslt.bin docbook_xsl_ns ];
3639 buildInputs = [ libcap systemd ]
3740 ++ optional (!stdenv.hostPlatform.isMusl) libidn2;
41+ postInstall = ''
42+ install -D /dev/stdin $apparmor/bin.ping <<EOF
43+ include <tunables/global>
44+ $out/bin/ping {
45+ include <abstractions/base>
46+ include <abstractions/consoles>
47+ include <abstractions/nameservice>
48+ include "${apparmorRulesFromClosure {}
49+ ([libcap] ++ optional (!stdenv.hostPlatform.isMusl) libidn2)}"
50+ include <local/bin.ping>
51+ capability net_raw,
52+ network inet raw,
53+ network inet6 raw,
54+ mr $out/bin/ping,
55+ r $out/share/locale/**,
56+ r @{PROC}/@{pid}/environ,
57+ }
58+ EOF
59+ '';
3860
3961 meta = {
4062 description = "A set of small useful utilities for Linux networking";
pkgs/tools/networking/inetutils/default.nix+21−1
@@ -1,4 +1,6 @@
1−{ stdenv, lib, fetchurl, ncurses, perl, help2man }:
1+{ stdenv, lib, fetchurl, ncurses, perl, help2man
2+, apparmorRulesFromClosure
3+}:
24
35stdenv.mkDerivation rec {
46 name = "inetutils-1.9.4";
@@ -8,6 +10,8 @@ stdenv.mkDerivation rec {
810 sha256 = "05n65k4ixl85dc6rxc51b1b732gnmm8xnqi424dy9f1nz7ppb3xy";
911 };
1012
13+ outputs = ["out" "apparmor"];
14+
1115 patches = [
1216 ./whois-Update-Canadian-TLD-server.patch
1317 ./service-name.patch
@@ -41,6 +45,22 @@ stdenv.mkDerivation rec {
4145
4246 installFlags = [ "SUIDMODE=" ];
4347
48+ postInstall = ''
49+ install -D /dev/stdin $apparmor/bin.ping <<EOF
50+ $out/bin/ping {
51+ include <abstractions/base>
52+ include <abstractions/consoles>
53+ include <abstractions/nameservice>
54+ include "${apparmorRulesFromClosure {} [stdenv.cc.libc]}"
55+ include <local/bin.ping>
56+ capability net_raw,
57+ network inet raw,
58+ network inet6 raw,
59+ mr $out/bin/ping,
60+ }
61+ EOF
62+ '';
63+
4464 meta = with lib; {
4565 description = "Collection of common network programs";
4666
pkgs/top-level/all-packages.nix+1−1
@@ -17367,7 +17367,7 @@ in
1736717367
1736817368 inherit (callPackages ../os-specific/linux/apparmor { python = python3; })
1736917369 libapparmor apparmor-utils apparmor-bin-utils apparmor-parser apparmor-pam
17370− apparmor-profiles apparmor-kernel-patches;
17370+ apparmor-profiles apparmor-kernel-patches apparmorRulesFromClosure;
1737117371
1737217372 atop = callPackage ../os-specific/linux/atop { };
1737317373