chiark / gitweb /
pam: use /proc/self/sessionid only if CAP_AUDIT_CONTROL is set
[elogind.git] / src / pam-module.c
index 3a5404db4d685168e1dcbe0a96cbd255086399db..93eb92956987709681f36b562da6257508527986 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/file.h>
 #include <pwd.h>
 #include <endian.h>
+#include <sys/capability.h>
 
 #include <security/pam_modules.h>
 #include <security/_pam_macros.h>
@@ -220,18 +221,19 @@ static uint64_t get_session_id(int *mode) {
 
         /* First attempt: let's use the session ID of the audit
          * system, if it is available. */
-        if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
-                uint32_t u;
-                int r;
+        if (have_effective_cap(CAP_AUDIT_CONTROL) > 0)
+                if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
+                        uint32_t u;
+                        int r;
 
-                r = safe_atou32(s, &u);
-                free(s);
+                        r = safe_atou32(s, &u);
+                        free(s);
 
-                if (r >= 0 && u != (uint32_t) -1 && u > 0) {
-                        *mode = SESSION_ID_AUDIT;
-                        return (uint64_t) u;
+                        if (r >= 0 && u != (uint32_t) -1 && u > 0) {
+                                *mode = SESSION_ID_AUDIT;
+                                return (uint64_t) u;
+                        }
                 }
-        }
 
         /* Second attempt, use our own counter. */
         if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
@@ -239,11 +241,10 @@ static uint64_t get_session_id(int *mode) {
                 ssize_t r;
 
                 /* We do a bit of endianess swapping here, just to be
-                 * sure. /var should be machine specific anyway, and
-                 * /var/run even mounted from tmpfs, so this
-                 * byteswapping should really not be necessary. But
-                 * then again, you never know, so let's avoid any
-                 * risk. */
+                 * sure. /run should be machine specific anyway, and
+                 * even mounted from tmpfs, so this byteswapping
+                 * should really not be necessary. But then again, you
+                 * never know, so let's avoid any risk. */
 
                 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
                         counter = 1;
@@ -289,15 +290,24 @@ static int get_user_data(
         assert(ret_username);
         assert(ret_pw);
 
-        if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
-                uint32_t u;
+        if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) {
+                /* Only use audit login uid if we are executed with
+                 * sufficient capabilities so that pam_loginuid could
+                 * do its job. If we are lacking the CAP_AUDIT_CONTROL
+                 * capabality we most likely are being run in a
+                 * container and /proc/self/loginuid is useless since
+                 * it probably contains a uid of the host system. */
 
-                r = safe_atou32(s, &u);
-                free(s);
+                if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
+                        uint32_t u;
 
-                if (r >= 0 && u != (uint32_t) -1 && u > 0) {
-                        have_loginuid = true;
-                        pw = pam_modutil_getpwuid(handle, u);
+                        r = safe_atou32(s, &u);
+                        free(s);
+
+                        if (r >= 0 && u != (uint32_t) -1 && u > 0) {
+                                have_loginuid = true;
+                                pw = pam_modutil_getpwuid(handle, u);
+                        }
                 }
         }
 
@@ -435,7 +445,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 goto finish;
         }
 
-        /* Create /var/run/$USER */
+        /* Create /run/user/$USER */
         free(buf);
         if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
                 r = PAM_BUF_ERR;