richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
b677e7
From fcd9a141d08d521c01dc1a1c06a8d43a2337a392 Mon Sep 17 00:00:00 2001
b677e7
From: Lennart Poettering <lennart@poettering.net>
b677e7
Date: Mon, 4 Feb 2019 10:23:43 +0100
b677e7
Subject: [PATCH] pam-systemd: use secure_getenv() rather than getenv()
b677e7
b677e7
And explain why in a comment.
b677e7
b677e7
(cherry picked from commit 83d4ab55336ff8a0643c6aa627b31e351a24040a)
b677e7
b677e7
CVE-2019-3842
b677e7
b677e7
Resolves: #1687514
b677e7
---
b677e7
 src/login/pam_systemd.c | 55 ++++++++++++++++++++++++-----------------
b677e7
 1 file changed, 32 insertions(+), 23 deletions(-)
b677e7
b677e7
diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c
b677e7
index 1fbf6ba585..78ddb7d398 100644
b677e7
--- a/src/login/pam_systemd.c
b677e7
+++ b/src/login/pam_systemd.c
b677e7
@@ -274,6 +274,33 @@ static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, con
b677e7
         return 0;
b677e7
 }
b677e7
 
b677e7
+static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) {
b677e7
+        const char *v;
b677e7
+
b677e7
+        assert(handle);
b677e7
+        assert(key);
b677e7
+
b677e7
+        /* Looks for an environment variable, preferrably in the environment block associated with the
b677e7
+         * specified PAM handle, falling back to the process' block instead. Why check both? Because we want
b677e7
+         * to permit configuration of session properties from unit files that invoke PAM services, so that
b677e7
+         * PAM services don't have to be reworked to set systemd-specific properties, but these properties
b677e7
+         * can still be set from the unit file Environment= block. */
b677e7
+
b677e7
+        v = pam_getenv(handle, key);
b677e7
+        if (!isempty(v))
b677e7
+                return v;
b677e7
+
b677e7
+        /* We use secure_getenv() here, since we might get loaded into su/sudo, which are SUID. Ideally
b677e7
+         * they'd clean up the environment before invoking foreign code (such as PAM modules), but alas they
b677e7
+         * currently don't (to be precise, they clean up the environment they pass to their children, but
b677e7
+         * not their own environ[]). */
b677e7
+        v = secure_getenv(key);
b677e7
+        if (!isempty(v))
b677e7
+                return v;
b677e7
+
b677e7
+        return fallback;
b677e7
+}
b677e7
+
b677e7
 _public_ PAM_EXTERN int pam_sm_open_session(
b677e7
                 pam_handle_t *handle,
b677e7
                 int flags,
b677e7
@@ -352,29 +379,11 @@ _public_ PAM_EXTERN int pam_sm_open_session(
b677e7
         pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
b677e7
         pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
b677e7
 
b677e7
-        seat = pam_getenv(handle, "XDG_SEAT");
b677e7
-        if (isempty(seat))
b677e7
-                seat = getenv("XDG_SEAT");
b677e7
-
b677e7
-        cvtnr = pam_getenv(handle, "XDG_VTNR");
b677e7
-        if (isempty(cvtnr))
b677e7
-                cvtnr = getenv("XDG_VTNR");
b677e7
-
b677e7
-        type = pam_getenv(handle, "XDG_SESSION_TYPE");
b677e7
-        if (isempty(type))
b677e7
-                type = getenv("XDG_SESSION_TYPE");
b677e7
-        if (isempty(type))
b677e7
-                type = type_pam;
b677e7
-
b677e7
-        class = pam_getenv(handle, "XDG_SESSION_CLASS");
b677e7
-        if (isempty(class))
b677e7
-                class = getenv("XDG_SESSION_CLASS");
b677e7
-        if (isempty(class))
b677e7
-                class = class_pam;
b677e7
-
b677e7
-        desktop = pam_getenv(handle, "XDG_SESSION_DESKTOP");
b677e7
-        if (isempty(desktop))
b677e7
-                desktop = getenv("XDG_SESSION_DESKTOP");
b677e7
+        seat = getenv_harder(handle, "XDG_SEAT", NULL);
b677e7
+        cvtnr = getenv_harder(handle, "XDG_VTNR", NULL);
b677e7
+        type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam);
b677e7
+        class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam);
b677e7
+        desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", NULL);
b677e7
 
b677e7
         tty = strempty(tty);
b677e7