Unverified Commit 73064231 authored by Colin's avatar Colin Committed by John Titor
Browse files

nixos/pam: fully-qualify `modulePath`

this ensures PAM users always get the intended version of a module when
multiple versions of the same module exist on a system.

most packages which consume `pam` and link against `libpam.so` do so only
to access its API, and not because they care about the specific
`pam_<xyz>.so` modules provided by that `pam`. but when specifying
modules by name only, PAM-capable applications may well load the
`pam_<xyz>.so` from the `pam` they were compiled against instead of the
pam declared in `security.pam.package`. by fully qualifying `modulePath`
we ensure that users can actually swap out pam modules without rebuilding
the world.
parent 7511ed26
Loading
Loading
Loading
Loading
+20 −19
Original line number Diff line number Diff line
@@ -649,16 +649,16 @@ let
          # The required pam_unix.so module has to come after all the sufficient modules
          # because otherwise, the account lookup will fail if the user does not exist
          # locally, for example with MySQL- or LDAP-auth.
          { name = "unix"; control = "required"; modulePath = "pam_unix.so"; }
          { name = "unix"; control = "required"; modulePath = "${package}/lib/security/pam_unix.so"; }
        ];

        auth = autoOrderRules ([
          { name = "oslogin_login"; enable = cfg.googleOsLoginAuthentication; control = "[success=done perm_denied=die default=ignore]"; modulePath = "${pkgs.google-guest-oslogin}/lib/security/pam_oslogin_login.so"; }
          { name = "rootok"; enable = cfg.rootOK; control = "sufficient"; modulePath = "pam_rootok.so"; }
          { name = "wheel"; enable = cfg.requireWheel; control = "required"; modulePath = "pam_wheel.so"; settings = {
          { name = "rootok"; enable = cfg.rootOK; control = "sufficient"; modulePath = "${package}/lib/security/pam_rootok.so"; }
          { name = "wheel"; enable = cfg.requireWheel; control = "required"; modulePath = "${package}/lib/security/pam_wheel.so"; settings = {
            use_uid = true;
          }; }
          { name = "faillock"; enable = cfg.logFailures; control = "required"; modulePath = "pam_faillock.so"; }
          { name = "faillock"; enable = cfg.logFailures; control = "required"; modulePath = "${package}/lib/security/pam_faillock.so"; }
          { name = "mysql"; enable = cfg.mysqlAuth; control = "sufficient"; modulePath = "${pkgs.pam_mysql}/lib/security/pam_mysql.so"; settings = {
            config_file = "/etc/security/pam_mysql.conf";
          }; }
@@ -711,7 +711,7 @@ let
              || cfg.zfs))
            [
              { name = "systemd_home-early"; enable = config.services.homed.enable; control = "optional"; modulePath = "${config.systemd.package}/lib/security/pam_systemd_home.so"; }
              { name = "unix-early"; enable = cfg.unixAuth; control = "optional"; modulePath = "pam_unix.so"; settings = {
              { name = "unix-early"; enable = cfg.unixAuth; control = "optional"; modulePath = "${package}/lib/security/pam_unix.so"; settings = {
                nullok = cfg.allowNullPassword;
                inherit (cfg) nodelay;
                likeauth = true;
@@ -741,7 +741,7 @@ let
              { name = "duo"; enable = cfg.duoSecurity.enable; control = "required"; modulePath = "${pkgs.duo-unix}/lib/security/pam_duo.so"; }
            ]) ++ [
          { name = "systemd_home"; enable = config.services.homed.enable; control = "sufficient"; modulePath = "${config.systemd.package}/lib/security/pam_systemd_home.so"; }
          { name = "unix"; enable = cfg.unixAuth; control = "sufficient"; modulePath = "pam_unix.so"; settings = {
          { name = "unix"; enable = cfg.unixAuth; control = "sufficient"; modulePath = "${package}/lib/security/pam_unix.so"; settings = {
            nullok = cfg.allowNullPassword;
            inherit (cfg) nodelay;
            likeauth = true;
@@ -769,12 +769,12 @@ let
            action = "store";
            use_first_pass = true;
          }; }
          { name = "deny"; control = "required"; modulePath = "pam_deny.so"; }
          { name = "deny"; control = "required"; modulePath = "${package}/lib/security/pam_deny.so"; }
        ]);

        password = autoOrderRules [
          { name = "systemd_home"; enable = config.services.homed.enable; control = "sufficient"; modulePath = "${config.systemd.package}/lib/security/pam_systemd_home.so"; }
          { name = "unix"; control = "sufficient"; modulePath = "pam_unix.so"; settings = {
          { name = "unix"; control = "sufficient"; modulePath = "${package}/lib/security/pam_unix.so"; settings = {
            nullok = true;
            yescrypt = true;
          }; }
@@ -799,12 +799,12 @@ let
        ];

        session = autoOrderRules [
          { name = "env"; enable = cfg.setEnvironment; control = "required"; modulePath = "pam_env.so"; settings = {
          { name = "env"; enable = cfg.setEnvironment; control = "required"; modulePath = "${package}/lib/security/pam_env.so"; settings = {
            conffile = "/etc/pam/environment";
            readenv = 0;
          }; }
          { name = "unix"; control = "required"; modulePath = "pam_unix.so"; }
          { name = "loginuid"; enable = cfg.setLoginUid; control = if config.boot.isContainer then "optional" else "required"; modulePath = "pam_loginuid.so"; }
          { name = "unix"; control = "required"; modulePath = "${package}/lib/security/pam_unix.so"; }
          { name = "loginuid"; enable = cfg.setLoginUid; control = if config.boot.isContainer then "optional" else "required"; modulePath = "${package}/lib/security/pam_loginuid.so"; }
          { name = "tty_audit"; enable = cfg.ttyAudit.enable; control = "required"; modulePath = "${package}/lib/security/pam_tty_audit.so"; settings = {
            open_only = cfg.ttyAudit.openOnly;
            enable = cfg.ttyAudit.enablePattern;
@@ -824,11 +824,11 @@ let
          # Skips the pam_fscrypt module for systemd-user sessions which do not have a password
          # anyways.
          # See also https://github.com/google/fscrypt/issues/95
          { name = "fscrypt-skip-systemd"; enable = config.security.pam.enableFscrypt; control = "[success=1 default=ignore]"; modulePath = "pam_succeed_if.so"; args = [
          { name = "fscrypt-skip-systemd"; enable = config.security.pam.enableFscrypt; control = "[success=1 default=ignore]"; modulePath = "${package}/lib/security/pam_succeed_if.so"; args = [
            "service" "=" "systemd-user"
          ]; }
          { name = "fscrypt"; enable = config.security.pam.enableFscrypt; control = "optional"; modulePath = "${pkgs.fscrypt-experimental}/lib/security/pam_fscrypt.so"; }
          { name = "zfs_key-skip-systemd"; enable = cfg.zfs; control = "[success=1 default=ignore]"; modulePath = "pam_succeed_if.so"; args = [
          { name = "zfs_key-skip-systemd"; enable = cfg.zfs; control = "[success=1 default=ignore]"; modulePath = "${package}/lib/security/pam_succeed_if.so"; args = [
            "service" "=" "systemd-user"
          ]; }
          { name = "zfs_key"; enable = cfg.zfs; control = "optional"; modulePath = "${config.boot.zfs.package}/lib/security/pam_zfs_key.so"; settings = {
@@ -847,7 +847,7 @@ let
          { name = "krb5"; enable = config.security.pam.krb5.enable; control = "optional"; modulePath = "${pam_krb5}/lib/security/pam_krb5.so"; }
          { name = "otpw"; enable = cfg.otpwAuth; control = "optional"; modulePath = "${pkgs.otpw}/lib/security/pam_otpw.so"; }
          { name = "systemd"; enable = cfg.startSession; control = "optional"; modulePath = "${config.systemd.package}/lib/security/pam_systemd.so"; }
          { name = "xauth"; enable = cfg.forwardXAuth; control = "optional"; modulePath = "pam_xauth.so"; settings = {
          { name = "xauth"; enable = cfg.forwardXAuth; control = "optional"; modulePath = "${package}/lib/security/pam_xauth.so"; settings = {
            xauthpath = "${pkgs.xorg.xauth}/bin/xauth";
            systemuser = 99;
          }; }
@@ -1577,11 +1577,6 @@ in
      lib.concatMapStrings
        (name: "r ${config.environment.etc."pam.d/${name}".source},\n")
        (attrNames config.security.pam.services) +
      ''
      mr ${getLib package}/lib/security/pam_filter/*,
      mr ${getLib package}/lib/security/pam_*.so,
      r ${getLib package}/lib/security/,
      '' +
      (with lib; pipe config.security.pam.services [
        attrValues
        (catAttrs "rules")
@@ -1589,6 +1584,12 @@ in
        (concatMap attrValues)
        (filter (rule: rule.enable))
        (catAttrs "modulePath")
        # TODO(@uninsane): replace this warning + filter with just an assertion
        (map (modulePath: lib.warnIfNot
          (hasPrefix "/" modulePath)
          ''non-absolute PAM modulePath "${modulePath}" is unsupported by apparmor and will be treated as an error by future versions of nixpkgs; see <https://github.com/NixOS/nixpkgs/pull/314791>''
          modulePath
        ))
        (filter (hasPrefix "/"))
        unique
        (map (module: "mr ${module},"))