@@ -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+}