572a44
From 281100560056e06f69d2cdb4dcc854bd3717734f Mon Sep 17 00:00:00 2001
572a44
From: Michal Sekletar <msekleta@redhat.com>
572a44
Date: Mon, 10 Feb 2014 17:41:32 +0100
572a44
Subject: [PATCH] pam: do not set XDG_RUNTIME_DIR unconditionally
572a44
572a44
If the session's original user is not the same as the newly logged in one we
572a44
will not set XDG_RUNTIME_DIR.
572a44
572a44
Patch based on upstream commit baae0358f349870544884e405e82e4be7d8add9f
572a44
---
572a44
 src/login/logind-dbus.c         |  2 ++
572a44
 src/login/logind-session-dbus.c |  1 +
572a44
 src/login/pam-module.c          | 52 ++++++++++++++++++++---------------------
572a44
 3 files changed, 29 insertions(+), 26 deletions(-)
572a44
572a44
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
572a44
index bb85c7d..69e94aa 100644
572a44
--- a/src/login/logind-dbus.c
572a44
+++ b/src/login/logind-dbus.c
572a44
@@ -90,6 +90,7 @@
572a44
         "   <arg name=\"path\" type=\"o\" direction=\"out\"/>\n"        \
572a44
         "   <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
572a44
         "   <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n"          \
572a44
+        "   <arg name=\"uid\" type=\"u\" direction=\"out\"/>\n"         \
572a44
         "   <arg name=\"seat\" type=\"s\" direction=\"out\"/>\n"        \
572a44
         "   <arg name=\"vtnr\" type=\"u\" direction=\"out\"/>\n"        \
572a44
         "   <arg name=\"existing\" type=\"b\" direction=\"out\"/>\n"    \
572a44
@@ -523,6 +524,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message) {
572a44
                                 DBUS_TYPE_OBJECT_PATH, &path,
572a44
                                 DBUS_TYPE_STRING, &session->user->runtime_path,
572a44
                                 DBUS_TYPE_UNIX_FD, &fifo_fd,
572a44
+                                DBUS_TYPE_UINT32, &session->user->uid,
572a44
                                 DBUS_TYPE_STRING, &cseat,
572a44
                                 DBUS_TYPE_UINT32, &vtnr,
572a44
                                 DBUS_TYPE_BOOLEAN, &exists,
572a44
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
572a44
index be4e01c..86b0746 100644
572a44
--- a/src/login/logind-session-dbus.c
572a44
+++ b/src/login/logind-session-dbus.c
572a44
@@ -755,6 +755,7 @@ int session_send_create_reply(Session *s, DBusError *error) {
572a44
                                     DBUS_TYPE_OBJECT_PATH, &path,
572a44
                                     DBUS_TYPE_STRING, &s->user->runtime_path,
572a44
                                     DBUS_TYPE_UNIX_FD, &fifo_fd,
572a44
+                                    DBUS_TYPE_UINT32, &s->user->uid,
572a44
                                     DBUS_TYPE_STRING, &cseat,
572a44
                                     DBUS_TYPE_UINT32, &vtnr,
572a44
                                     DBUS_TYPE_BOOLEAN, &exists,
572a44
diff --git a/src/login/pam-module.c b/src/login/pam-module.c
572a44
index 02f1618..22d9733 100644
572a44
--- a/src/login/pam-module.c
572a44
+++ b/src/login/pam-module.c
572a44
@@ -86,31 +86,24 @@ static int get_user_data(
572a44
 
572a44
         const char *username = NULL;
572a44
         struct passwd *pw = NULL;
572a44
-        uid_t uid;
572a44
         int r;
572a44
 
572a44
         assert(handle);
572a44
         assert(ret_username);
572a44
         assert(ret_pw);
572a44
 
572a44
-        r = audit_loginuid_from_pid(0, &uid);
572a44
-        if (r >= 0)
572a44
-                pw = pam_modutil_getpwuid(handle, uid);
572a44
-        else {
572a44
-                r = pam_get_user(handle, &username, NULL);
572a44
-                if (r != PAM_SUCCESS) {
572a44
-                        pam_syslog(handle, LOG_ERR, "Failed to get user name.");
572a44
-                        return r;
572a44
-                }
572a44
-
572a44
-                if (isempty(username)) {
572a44
-                        pam_syslog(handle, LOG_ERR, "User name not valid.");
572a44
-                        return PAM_AUTH_ERR;
572a44
-                }
572a44
+        r = pam_get_user(handle, &username, NULL);
572a44
+        if (r != PAM_SUCCESS) {
572a44
+                pam_syslog(handle, LOG_ERR, "Failed to get user name.");
572a44
+                return r;
572a44
+        }
572a44
 
572a44
-                pw = pam_modutil_getpwnam(handle, username);
572a44
+        if (isempty(username)) {
572a44
+                pam_syslog(handle, LOG_ERR, "User name not valid.");
572a44
+                return PAM_AUTH_ERR;
572a44
         }
572a44
 
572a44
+        pw = pam_modutil_getpwnam(handle, username);
572a44
         if (!pw) {
572a44
                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
572a44
                 return PAM_USER_UNKNOWN;
572a44
@@ -123,16 +116,14 @@ static int get_user_data(
572a44
 }
572a44
 
572a44
 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
572a44
-        _cleanup_free_ char *p = NULL;
572a44
-        int r;
572a44
+        _cleanup_free_ char *p = NULL, *tty=NULL;
572a44
         _cleanup_close_ int fd = -1;
572a44
         union sockaddr_union sa = {
572a44
                 .un.sun_family = AF_UNIX,
572a44
         };
572a44
         struct ucred ucred;
572a44
         socklen_t l;
572a44
-        _cleanup_free_ char *tty = NULL;
572a44
-        int v;
572a44
+        int v, r;
572a44
 
572a44
         assert(display);
572a44
         assert(vtnr);
572a44
@@ -186,14 +177,14 @@ _public_ PAM_EXTERN int pam_sm_open_session(
572a44
         bool debug = false;
572a44
         const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type = NULL, *class = NULL, *class_pam = NULL, *cvtnr = NULL;
572a44
         DBusError error;
572a44
-        uint32_t uid, pid;
572a44
         DBusMessageIter iter;
572a44
         int session_fd = -1;
572a44
         DBusConnection *bus = NULL;
572a44
         DBusMessage *m = NULL, *reply = NULL;
572a44
         dbus_bool_t remote, existing;
572a44
         int r;
572a44
-        uint32_t vtnr = 0;
572a44
+        uint32_t uid, pid, vtnr = 0;
572a44
+        uid_t original_uid;
572a44
 
572a44
         assert(handle);
572a44
 
572a44
@@ -389,6 +380,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
572a44
                                    DBUS_TYPE_OBJECT_PATH, &object_path,
572a44
                                    DBUS_TYPE_STRING, &runtime_path,
572a44
                                    DBUS_TYPE_UNIX_FD, &session_fd,
572a44
+                                   DBUS_TYPE_UINT32, &original_uid,
572a44
                                    DBUS_TYPE_STRING, &seat,
572a44
                                    DBUS_TYPE_UINT32, &vtnr,
572a44
                                    DBUS_TYPE_BOOLEAN, &existing,
572a44
@@ -409,10 +401,18 @@ _public_ PAM_EXTERN int pam_sm_open_session(
572a44
                 goto finish;
572a44
         }
572a44
 
572a44
-        r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
572a44
-        if (r != PAM_SUCCESS) {
572a44
-                pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
572a44
-                goto finish;
572a44
+        if (original_uid == pw->pw_uid) {
572a44
+               /* Don't set $XDG_RUNTIME_DIR if the user we now
572a44
+                * authenticated for does not match the original user
572a44
+                * of the session. We do this in order not to result
572a44
+                * in privileged apps clobbering the runtime directory
572a44
+                * unnecessarily. */
572a44
+
572a44
+                r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
572a44
+                if (r != PAM_SUCCESS) {
572a44
+                        pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
572a44
+                        return r;
572a44
+                }
572a44
         }
572a44
 
572a44
         if (!isempty(seat)) {