From 45e5d726b22db783731f0460870e9ab4f06bc5c4 Mon Sep 17 00:00:00 2001 From: Julien Moutinho Date: Sat, 27 Feb 2021 21:26:47 +0100 Subject: nixos/apparmor: improve code readability --- nixos/modules/security/apparmor/includes.nix | 322 ++++++++++++++------------- 1 file changed, 169 insertions(+), 153 deletions(-) (limited to 'nixos/modules/security/apparmor/includes.nix') diff --git a/nixos/modules/security/apparmor/includes.nix b/nixos/modules/security/apparmor/includes.nix index 498d7e77650..e3dd410b3bb 100644 --- a/nixos/modules/security/apparmor/includes.nix +++ b/nixos/modules/security/apparmor/includes.nix @@ -3,13 +3,15 @@ let inherit (builtins) attrNames hasAttr isAttrs; inherit (lib) getLib; inherit (config.environment) etc; + # Utility to generate an AppArmor rule + # only when the given path exists in config.environment.etc etcRule = arg: - let go = {path ? null, mode ? "r", trail ? ""}: + let go = { path ? null, mode ? "r", trail ? "" }: lib.optionalString (hasAttr path etc) "${mode} ${config.environment.etc.${path}.source}${trail},"; in if isAttrs arg then go arg - else go {path=arg;}; + else go { path = arg; }; in { # FIXME: most of the etcRule calls below have been @@ -29,76 +31,80 @@ config.security.apparmor.includes = { ''; "abstractions/audio" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/audio" - ${etcRule "asound.conf"} - ${etcRule "esound/esd.conf"} - ${etcRule "libao.conf"} - ${etcRule {path="pulse"; trail="/";}} - ${etcRule {path="pulse"; trail="/**";}} - ${etcRule {path="sound"; trail="/";}} - ${etcRule {path="sound"; trail="/**";}} - ${etcRule {path="alsa/conf.d"; trail="/";}} - ${etcRule {path="alsa/conf.d"; trail="/*";}} - ${etcRule "openal/alsoft.conf"} - ${etcRule "wildmidi/wildmidi.conf"} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + "asound.conf" + "esound/esd.conf" + "libao.conf" + { path = "pulse"; trail = "/"; } + { path = "pulse"; trail = "/**"; } + { path = "sound"; trail = "/"; } + { path = "sound"; trail = "/**"; } + { path = "alsa/conf.d"; trail = "/"; } + { path = "alsa/conf.d"; trail = "/*"; } + "openal/alsoft.conf" + "wildmidi/wildmidi.conf" + ]; "abstractions/authentication" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/authentication" # Defined in security.pam include - ${etcRule "nologin"} - ${etcRule "securetty"} - ${etcRule {path="security"; trail="/*";}} - ${etcRule "shadow"} - ${etcRule "gshadow"} - ${etcRule "pwdb.conf"} - ${etcRule "default/passwd"} - ${etcRule "login.defs"} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + "nologin" + "securetty" + { path = "security"; trail = "/*"; } + "shadow" + "gshadow" + "pwdb.conf" + "default/passwd" + "login.defs" + ]; "abstractions/base" = '' - include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/base" - r ${pkgs.stdenv.cc.libc}/share/locale/**, - r ${pkgs.stdenv.cc.libc}/share/locale.alias, - ${lib.optionalString (pkgs.glibcLocales != null) "r ${pkgs.glibcLocales}/lib/locale/locale-archive,"} - ${etcRule "localtime"} - r ${pkgs.tzdata}/share/zoneinfo/**, - r ${pkgs.stdenv.cc.libc}/share/i18n/**, + include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/base" + r ${pkgs.stdenv.cc.libc}/share/locale/**, + r ${pkgs.stdenv.cc.libc}/share/locale.alias, + ${lib.optionalString (pkgs.glibcLocales != null) "r ${pkgs.glibcLocales}/lib/locale/locale-archive,"} + ${etcRule "localtime"} + r ${pkgs.tzdata}/share/zoneinfo/**, + r ${pkgs.stdenv.cc.libc}/share/i18n/**, ''; "abstractions/bash" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/bash" - # system-wide bash configuration - ${etcRule "profile.dos"} - ${etcRule "profile"} - ${etcRule "profile.d"} - ${etcRule {path="profile.d"; trail="/*";}} - ${etcRule "bashrc"} - ${etcRule "bash.bashrc"} - ${etcRule "bash.bashrc.local"} - ${etcRule "bash_completion"} - ${etcRule "bash_completion.d"} - ${etcRule {path="bash_completion.d"; trail="/*";}} - # bash relies on system-wide readline configuration - ${etcRule "inputrc"} + # bash inspects filesystems at startup # and /etc/mtab is linked to /proc/mounts @{PROC}/mounts - # run out of /etc/bash.bashrc - ${etcRule "DIR_COLORS"} + # system-wide bash configuration + '' + lib.concatMapStringsSep "\n" etcRule [ + "profile.dos" + "profile" + "profile.d" + { path = "profile.d"; trail = "/*"; } + "bashrc" + "bash.bashrc" + "bash.bashrc.local" + "bash_completion" + "bash_completion.d" + { path = "bash_completion.d"; trail = "/*"; } + # bash relies on system-wide readline configuration + "inputrc" + # run out of /etc/bash.bashrc + "DIR_COLORS" + ]; + "abstractions/consoles" = '' + include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/consoles" ''; "abstractions/cups-client" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/cpus-client" ${etcRule "cups/cups-client.conf"} ''; - "abstractions/consoles" = '' - include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/consoles" - ''; "abstractions/dbus-session-strict" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/dbus-session-strict" ${etcRule "machine-id"} ''; "abstractions/dconf" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/dconf" - ${etcRule {path="dconf"; trail="/**";}} + ${etcRule { path = "dconf"; trail = "/**"; }} ''; "abstractions/dri-common" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/dri-common" @@ -109,62 +115,66 @@ config.security.apparmor.includes = { # those are therefore added there to this "abstractions/fonts". "abstractions/fonts" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/fonts" - ${etcRule {path="fonts"; trail="/**";}} + ${etcRule { path = "fonts"; trail = "/**"; }} ''; "abstractions/gnome" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/gnome" - ${etcRule {path="gnome"; trail="/gtkrc*";}} - ${etcRule {path="gtk"; trail="/*";}} - ${etcRule {path="gtk-2.0"; trail="/*";}} - ${etcRule {path="gtk-3.0"; trail="/*";}} - ${etcRule "orbitrc"} include - ${etcRule {path="pango"; trail="/*";}} - ${etcRule {path="/etc/gnome-vfs-2.0"; trail="/modules/";}} - ${etcRule {path="/etc/gnome-vfs-2.0"; trail="/modules/*";}} - ${etcRule "papersize"} - ${etcRule {path="cups"; trail="/lpoptions";}} - ${etcRule {path="gnome"; trail="/defaults.list";}} - ${etcRule {path="xdg"; trail="/{,*-}mimeapps.list";}} - ${etcRule "xdg/mimeapps.list"} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + { path = "gnome"; trail = "/gtkrc*"; } + { path = "gtk"; trail = "/*"; } + { path = "gtk-2.0"; trail = "/*"; } + { path = "gtk-3.0"; trail = "/*"; } + "orbitrc" + { path = "pango"; trail = "/*"; } + { path = "/etc/gnome-vfs-2.0"; trail = "/modules/"; } + { path = "/etc/gnome-vfs-2.0"; trail = "/modules/*"; } + "papersize" + { path = "cups"; trail = "/lpoptions"; } + { path = "gnome"; trail = "/defaults.list"; } + { path = "xdg"; trail = "/{,*-}mimeapps.list"; } + "xdg/mimeapps.list" + ]; "abstractions/kde" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/kde" - ${etcRule {path="qt3"; trail="/kstylerc";}} - ${etcRule {path="qt3"; trail="/qt_plugins_3.3rc";}} - ${etcRule {path="qt3"; trail="/qtrc";}} - ${etcRule "kderc"} - ${etcRule {path="kde3"; trail="/*";}} - ${etcRule "kde4rc"} - ${etcRule {path="xdg"; trail="/kdeglobals";}} - ${etcRule {path="xdg"; trail="/Trolltech.conf";}} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + { path = "qt3"; trail = "/kstylerc"; } + { path = "qt3"; trail = "/qt_plugins_3.3rc"; } + { path = "qt3"; trail = "/qtrc"; } + "kderc" + { path = "kde3"; trail = "/*"; } + "kde4rc" + { path = "xdg"; trail = "/kdeglobals"; } + { path = "xdg"; trail = "/Trolltech.conf"; } + ]; "abstractions/kerberosclient" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/kerberosclient" - ${etcRule {path="krb5.keytab"; mode="rk";}} - ${etcRule "krb5.conf"} - ${etcRule "krb5.conf.d"} - ${etcRule {path="krb5.conf.d"; trail="/*";}} + '' + lib.concatMapStringsSep "\n" etcRule [ + { path = "krb5.keytab"; mode="rk"; } + "krb5.conf" + "krb5.conf.d" + { path = "krb5.conf.d"; trail = "/*"; } # config files found via strings on libs - ${etcRule "krb.conf"} - ${etcRule "krb.realms"} - ${etcRule "srvtab"} - ''; + "krb.conf" + "krb.realms" + "srvtab" + ]; "abstractions/ldapclient" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/ldapclient" - ${etcRule "ldap.conf"} - ${etcRule "ldap.secret"} - ${etcRule {path="openldap"; trail="/*";}} - ${etcRule {path="openldap"; trail="/cacerts/*";}} - ${etcRule {path="sasl2"; trail="/*";}} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + "ldap.conf" + "ldap.secret" + { path = "openldap"; trail = "/*"; } + { path = "openldap"; trail = "/cacerts/*"; } + { path = "sasl2"; trail = "/*"; } + ]; "abstractions/likewise" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/likewise" ''; "abstractions/mdns" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/mdns" - ${etcRule "nss_mdns.conf"} + ${etcRule "nss_mdns.conf"} ''; "abstractions/nameservice" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/nameservice" @@ -173,31 +183,31 @@ config.security.apparmor.includes = { # looking up users by name or id, groups by name or id, hosts by name # or IP, etc. These operations may be performed through files, dns, # NIS, NIS+, LDAP, hesiod, wins, etc. Allow them all here. - ${etcRule "group"} - ${etcRule "host.conf"} - ${etcRule "hosts"} - ${etcRule "nsswitch.conf"} - ${etcRule "gai.conf"} - ${etcRule "passwd"} - ${etcRule "protocols"} - - # libtirpc (used for NIS/YP login) needs this - ${etcRule "netconfig"} + mr ${getLib pkgs.nss}/lib/libnss_*.so*, + mr ${getLib pkgs.nss}/lib64/libnss_*.so*, + '' + lib.concatMapStringsSep "\n" etcRule [ + "group" + "host.conf" + "hosts" + "nsswitch.conf" + "gai.conf" + "passwd" + "protocols" - ${etcRule "resolv.conf"} + # libtirpc (used for NIS/YP login) needs this + "netconfig" - ${etcRule {path="samba"; trail="/lmhosts";}} - ${etcRule "services"} + "resolv.conf" - ${etcRule "default/nss"} + { path = "samba"; trail = "/lmhosts"; } + "services" - # libnl-3-200 via libnss-gw-name - ${etcRule {path="libnl"; trail="/classid";}} - ${etcRule {path="libnl-3"; trail="/classid";}} + "default/nss" - mr ${getLib pkgs.nss}/lib/libnss_*.so*, - mr ${getLib pkgs.nss}/lib64/libnss_*.so*, - ''; + # libnl-3-200 via libnss-gw-name + { path = "libnl"; trail = "/classid"; } + { path = "libnl-3"; trail = "/classid"; } + ]; "abstractions/nis" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/nis" ''; @@ -207,7 +217,7 @@ config.security.apparmor.includes = { ''; "abstractions/opencl-common" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/opencl-common" - ${etcRule {path="OpenCL"; trail="/**";}} + ${etcRule { path = "OpenCL"; trail = "/**"; }} ''; "abstractions/opencl-mesa" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/opencl-mesa" @@ -215,68 +225,74 @@ config.security.apparmor.includes = { ''; "abstractions/openssl" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/openssl" - ${etcRule {path="ssl"; trail="/openssl.cnf";}} + ${etcRule { path = "ssl"; trail = "/openssl.cnf"; }} ''; "abstractions/p11-kit" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/p11-kit" - ${etcRule {path="pkcs11"; trail="/";}} - ${etcRule {path="pkcs11"; trail="/pkcs11.conf";}} - ${etcRule {path="pkcs11"; trail="/modules/";}} - ${etcRule {path="pkcs11"; trail="/modules/*";}} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + { path = "pkcs11"; trail = "/"; } + { path = "pkcs11"; trail = "/pkcs11.conf"; } + { path = "pkcs11"; trail = "/modules/"; } + { path = "pkcs11"; trail = "/modules/*"; } + ]; "abstractions/perl" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/perl" - ${etcRule {path="perl"; trail="/**";}} + ${etcRule { path = "perl"; trail = "/**"; }} ''; "abstractions/php" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/php" - ${etcRule {path="php"; trail="/**/";}} - ${etcRule {path="php5"; trail="/**/";}} - ${etcRule {path="php7"; trail="/**/";}} - ${etcRule {path="php"; trail="/**.ini";}} - ${etcRule {path="php5"; trail="/**.ini";}} - ${etcRule {path="php7"; trail="/**.ini";}} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + { path = "php"; trail = "/**/"; } + { path = "php5"; trail = "/**/"; } + { path = "php7"; trail = "/**/"; } + { path = "php"; trail = "/**.ini"; } + { path = "php5"; trail = "/**.ini"; } + { path = "php7"; trail = "/**.ini"; } + ]; "abstractions/postfix-common" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/postfix-common" - ${etcRule "mailname"} - ${etcRule {path="postfix"; trail="/*.cf";}} - ${etcRule "postfix/main.cf"} - ${etcRule "postfix/master.cf"} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + "mailname" + { path = "postfix"; trail = "/*.cf"; } + "postfix/main.cf" + "postfix/master.cf" + ]; "abstractions/python" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/python" ''; "abstractions/qt5" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/qt5" - ${etcRule {path="xdg"; trail="/QtProject/qtlogging.ini";}} - ${etcRule {path="xdg/QtProject"; trail="/qtlogging.ini";}} - ${etcRule "xdg/QtProject/qtlogging.ini"} - ''; + '' + lib.concatMapStringsSep "\n" etcRule [ + { path = "xdg"; trail = "/QtProject/qtlogging.ini"; } + { path = "xdg/QtProject"; trail = "/qtlogging.ini"; } + "xdg/QtProject/qtlogging.ini" + ]; "abstractions/samba" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/samba" - ${etcRule {path="samba"; trail="/*";}} + ${etcRule { path = "samba"; trail = "/*"; }} ''; "abstractions/ssl_certs" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/ssl_certs" - ${etcRule "ssl/certs/ca-certificates.crt"} - ${etcRule "ssl/certs/ca-bundle.crt"} - ${etcRule "pki/tls/certs/ca-bundle.crt"} - ${etcRule {path="ssl/trust"; trail="/";}} - ${etcRule {path="ssl/trust"; trail="/*";}} - ${etcRule {path="ssl/trust/anchors"; trail="/";}} - ${etcRule {path="ssl/trust/anchors"; trail="/**";}} - ${etcRule {path="pki/trust"; trail="/";}} - ${etcRule {path="pki/trust"; trail="/*";}} - ${etcRule {path="pki/trust/anchors"; trail="/";}} - ${etcRule {path="pki/trust/anchors"; trail="/**";}} - - # security.acme NixOS module + # For the NixOS module: security.acme r /var/lib/acme/*/cert.pem, r /var/lib/acme/*/chain.pem, r /var/lib/acme/*/fullchain.pem, - ''; + + '' + lib.concatMapStringsSep "\n" etcRule [ + "ssl/certs/ca-certificates.crt" + "ssl/certs/ca-bundle.crt" + "pki/tls/certs/ca-bundle.crt" + + { path = "ssl/trust"; trail = "/"; } + { path = "ssl/trust"; trail = "/*"; } + { path = "ssl/trust/anchors"; trail = "/"; } + { path = "ssl/trust/anchors"; trail = "/**"; } + { path = "pki/trust"; trail = "/"; } + { path = "pki/trust"; trail = "/*"; } + { path = "pki/trust/anchors"; trail = "/"; } + { path = "pki/trust/anchors"; trail = "/**"; } + ]; "abstractions/ssl_keys" = '' # security.acme NixOS module r /var/lib/acme/*/full.pem, @@ -284,18 +300,18 @@ config.security.apparmor.includes = { ''; "abstractions/vulkan" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/vulkan" - ${etcRule {path="vulkan/icd.d"; trail="/";}} - ${etcRule {path="vulkan/icd.d"; trail="/*.json";}} + ${etcRule { path = "vulkan/icd.d"; trail = "/"; }} + ${etcRule { path = "vulkan/icd.d"; trail = "/*.json"; }} ''; "abstractions/winbind" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/winbind" - ${etcRule {path="samba"; trail="/smb.conf";}} - ${etcRule {path="samba"; trail="/dhcp.conf";}} + ${etcRule { path = "samba"; trail = "/smb.conf"; }} + ${etcRule { path = "samba"; trail = "/dhcp.conf"; }} ''; "abstractions/X" = '' include "${pkgs.apparmor-profiles}/etc/apparmor.d/abstractions/X" - ${etcRule {path="X11/cursors"; trail="/";}} - ${etcRule {path="X11/cursors"; trail="/**";}} + ${etcRule { path = "X11/cursors"; trail = "/"; }} + ${etcRule { path = "X11/cursors"; trail = "/**"; }} ''; }; } -- cgit 1.4.1