CVE-2022-24732: Insufficient Session Expiration (CWE-613)
value: Maddy Mail Server is an open source SMTP compatible email server. Versions of maddy prior to 0.5.4 do not implement password expiry or account expiry checking when authenticating using PAM. Users are advised to upgrade. Users unable to upgrade should manually remove expired accounts via existing filtering mechanisms.
CWE description: According to WASC, "Insufficient Session Expiration is when a web site permits an attacker to reuse old session credentials or session IDs for authorization."
CVSS v3 base score: 8.8
Published: 2022-03-09T20:15Z
Repository: https://github.com/foxcpp/maddy
Language: C
Files changed: cmd/maddy-pam-helper/pam.c, internal/auth/pam/pam.c
Commit date: 2022-03-06 15:52:36 +0300
Commit message: auth/pam: Check for account/password expiry

See GHSA-6cp7-g972-w9m9. Thanks Youssef Rebahi-Gilbert (ysf) for
reporting the issue.

Diff: diff --git a/cmd/maddy-pam-helper/pam.c b/cmd/maddy-pam-helper/pam.c
index f3ff832..0826002 100644
--- a/cmd/maddy-pam-helper/pam.c
+++ b/cmd/maddy-pam-helper/pam.c
@@ -1,5 +1,25 @@
+//+build libpam
+
+/*
+Maddy Mail Server - Composable all-in-one email server.
+Copyright © 2019-2022 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 #define _POSIX_C_SOURCE 200809L
 #include <stdio.h>
 #include <stdlib.h>
 #include <security/pam_appl.h>
 #include "pam.h"
@@ -44,10 +64,23 @@ struct error_obj run_pam_auth(const char *username, char *password) {
         ret_val.func_name = "pam_authenticate";
         ret_val.error_msg = pam_strerror(local_auth, status);
         return ret_val;
     }
 
+    status = pam_acct_mgmt(local_auth, PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK);
+    if (status != PAM_SUCCESS) {
+        struct error_obj ret_val;
+        if (status == PAM_AUTH_ERR || status == PAM_USER_UNKNOWN || status == PAM_NEW_AUTHTOK_REQD) {
+            ret_val.status = 1;
+        } else {
+            ret_val.status = 2;
+        }
+        ret_val.func_name = "pam_acct_mgmt";
+        ret_val.error_msg = pam_strerror(local_auth, status);
+        return ret_val;
+    }
+
     status = pam_end(local_auth, status);
     if (status != PAM_SUCCESS) {
         struct error_obj ret_val;
         ret_val.status = 2;
         ret_val.func_name = "pam_end";
diff --git a/internal/auth/pam/pam.c b/internal/auth/pam/pam.c
index 270307e..0826002 100644
--- a/internal/auth/pam/pam.c
+++ b/internal/auth/pam/pam.c
@@ -1,10 +1,10 @@
 //+build libpam
 
 /*
 Maddy Mail Server - Composable all-in-one email server.
-Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
+Copyright © 2019-2022 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
@@ -64,10 +64,23 @@ struct error_obj run_pam_auth(const char *username, char *password) {
         ret_val.func_name = "pam_authenticate";
         ret_val.error_msg = pam_strerror(local_auth, status);
         return ret_val;
     }
 
+    status = pam_acct_mgmt(local_auth, PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK);
+    if (status != PAM_SUCCESS) {
+        struct error_obj ret_val;
+        if (status == PAM_AUTH_ERR || status == PAM_USER_UNKNOWN || status == PAM_NEW_AUTHTOK_REQD) {
+            ret_val.status = 1;
+        } else {
+            ret_val.status = 2;
+        }
+        ret_val.func_name = "pam_acct_mgmt";
+        ret_val.error_msg = pam_strerror(local_auth, status);
+        return ret_val;
+    }
+
     status = pam_end(local_auth, status);
     if (status != PAM_SUCCESS) {
         struct error_obj ret_val;
         ret_val.status = 2;
         ret_val.func_name = "pam_end";

Vulnerable code: Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
Fixed code: //+build libpam

/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2022 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

    status = pam_acct_mgmt(local_auth, PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK);
    if (status != PAM_SUCCESS) {
        struct error_obj ret_val;
        if (status == PAM_AUTH_ERR || status == PAM_USER_UNKNOWN || status == PAM_NEW_AUTHTOK_REQD) {
            ret_val.status = 1;
        } else {
            ret_val.status = 2;
        }
        ret_val.func_name = "pam_acct_mgmt";
        ret_val.error_msg = pam_strerror(local_auth, status);
        return ret_val;
    }

Copyright © 2019-2022 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
    status = pam_acct_mgmt(local_auth, PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK);
    if (status != PAM_SUCCESS) {
        struct error_obj ret_val;
        if (status == PAM_AUTH_ERR || status == PAM_USER_UNKNOWN || status == PAM_NEW_AUTHTOK_REQD) {
            ret_val.status = 1;
        } else {
            ret_val.status = 2;
        }
        ret_val.func_name = "pam_acct_mgmt";
        ret_val.error_msg = pam_strerror(local_auth, status);
        return ret_val;
    }