git: 2d2950c88933 - main - pam_xdg: Check asprintf return value

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Tue, 27 Feb 2024 06:43:00 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=2d2950c889335b24af7a92f3aaf9946de47bb0bc

commit 2d2950c889335b24af7a92f3aaf9946de47bb0bc
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2024-02-27 06:41:59 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2024-02-27 06:42:47 +0000

    pam_xdg: Check asprintf return value
    
    Reported by:    Shawn Webb <shawn.webb@hardenedbsd.org>
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
    Fixes:          6e69612d5df1 ("pam: Add pam_xdg module")
---
 lib/libpam/modules/pam_xdg/pam_xdg.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/libpam/modules/pam_xdg/pam_xdg.c b/lib/libpam/modules/pam_xdg/pam_xdg.c
index d218bc0425f2..d8ba6899da2f 100644
--- a/lib/libpam/modules/pam_xdg/pam_xdg.c
+++ b/lib/libpam/modules/pam_xdg/pam_xdg.c
@@ -130,7 +130,12 @@ _pam_xdg_open(pam_handle_t *pamh, int flags __unused,
 	}
 
 	/* Setup the environment variable */
-	asprintf(&runtime_dir, "XDG_RUNTIME_DIR=%s/%s", RUNTIME_DIR_PREFIX, user);
+	rv = asprintf(&runtime_dir, "XDG_RUNTIME_DIR=%s/%s", RUNTIME_DIR_PREFIX, user);
+	if (rv < 0) {
+		PAM_VERBOSE_ERROR("asprintf failed %d\n", rv);
+		rv = PAM_SESSION_ERR;
+		goto out;
+	}
 	rv = pam_putenv(pamh, runtime_dir);
 	if (rv != PAM_SUCCESS) {
 		PAM_VERBOSE_ERROR("pam_putenv: failed (%d)", rv);
@@ -140,7 +145,12 @@ _pam_xdg_open(pam_handle_t *pamh, int flags __unused,
 
 	/* Setup the session count file */
 	for (i = 0; i < XDG_MAX_SESSION; i++) {
-		asprintf(&xdg_session_file, "%s/xdg_session.%d", user, i);
+		rv = asprintf(&xdg_session_file, "%s/xdg_session.%d", user, i);
+		if (rv < 0) {
+			PAM_VERBOSE_ERROR("asprintf failed %d\n", rv);
+			rv = PAM_SESSION_ERR;
+			goto out;
+		}
 		session_file = openat(rt_dir_prefix, xdg_session_file, O_CREAT | O_EXCL, RUNTIME_DIR_MODE);
 		free(xdg_session_file);
 		if (session_file >= 0)
@@ -256,7 +266,12 @@ _pam_xdg_close(pam_handle_t *pamh __unused, int flags __unused,
 
 	/* Get the last session file created */
 	for (i = XDG_MAX_SESSION; i >= 0; i--) {
-		asprintf(&xdg_session_file, "%s/xdg_session.%d", user, i);
+		rv = asprintf(&xdg_session_file, "%s/xdg_session.%d", user, i);
+		if (rv < 0) {
+			PAM_VERBOSE_ERROR("asprintf failed %d\n", rv);
+			rv = PAM_SESSION_ERR;
+			goto out;
+		}
 		session_file = openat(rt_dir_prefix, xdg_session_file, 0);
 		if (session_file >= 0) {
 			unlinkat(rt_dir_prefix, xdg_session_file, 0);