X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fpam-module.c;h=1834dd92e6b8d81272c851c3ebade3d8486ae473;hp=5157897a71d1512ceacf822faf7dc091d975c780;hb=4e1e43c8f066c356aa5337156a1501b8ae4aef04;hpb=8c6db8336536916d0476ff8233e0abf40a2f6aab diff --git a/src/pam-module.c b/src/pam-module.c index 5157897a7..1834dd92e 100644 --- a/src/pam-module.c +++ b/src/pam-module.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -96,45 +97,85 @@ static int open_file_and_lock(const char *fn) { if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0) return -errno; + /* The BSD socket semantics are a lot nicer than those of + * POSIX locks. Which is why we use flock() here. BSD locking + * does not work across NFS which however is not needed here + * as the filesystems in question should be local, and only + * locally accessible, and most likely even tmpfs. */ + if (flock(fd, LOCK_EX) < 0) return -errno; return fd; } -static uint32_t combine32(unsigned long long u) { - uint32_t r = 0; - unsigned i; +enum { + SESSION_ID_AUDIT = 'a', + SESSION_ID_COUNTER = 'c', + SESSION_ID_RANDOM = 'r' +}; - for (i = 0; i < sizeof(u)/4; i ++) - r ^= (uint32_t) ((u >> (i*32)) & 0xFFFFFFFFULL); +static uint64_t get_session_id(int *mode) { + char *s; + int fd; - return r; -} + assert(mode); -static char *generate_session_cookie(void) { - char *machine; - char *cookie; - unsigned long long r; - usec_t u; - int k; + /* 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 (getmachineid_malloc(&machine) < 0) - if (!(machine = gethostname_malloc())) - return NULL; + r = safe_atou32(s, &u); + free(s); - r = random_ull(); - u = now(CLOCK_REALTIME); + if (r >= 0 && u != (uint32_t) -1) { + *mode = SESSION_ID_AUDIT; + return (uint64_t) u; + } + } - k = asprintf(&cookie, "%s-%lu-%lu", - machine, - (unsigned long) combine32(r), - (unsigned long) combine32((unsigned long long) u)); - free(machine); + /* Second attempt, use our own counter. */ + if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) { + uint64_t counter; + ssize_t r; - return k < 0 ? NULL : cookie; -} + /* 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. */ + + if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter)) + counter = 1; + else + counter = le64toh(counter) + 1; + + if (lseek(fd, 0, SEEK_SET) == 0) { + uint64_t swapped = htole64(counter); + + r = loop_write(fd, &swapped, sizeof(swapped), false); + if (r != sizeof(swapped)) + r = -EIO; + } else + r = -errno; + + close_nointr_nofail(fd); + + if (r >= 0) { + *mode = SESSION_ID_COUNTER; + return counter; + } + } + + *mode = SESSION_ID_RANDOM; + + /* Last attempt, pick a random value */ + return (uint64_t) random_ull(); +} static int get_user_data( pam_handle_t *handle, const char **ret_username, @@ -258,28 +299,38 @@ _public_ PAM_EXTERN int pam_sm_open_session( buf = NULL; if (create_session) { - const char *cookie; + const char *id; /* Reuse or create XDG session ID */ - if (!(cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) { - if (!(buf = generate_session_cookie())) { + if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) { + int mode; + + if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) { r = PAM_BUF_ERR; goto finish; } - if ((r = pam_misc_setenv(handle, "XDG_SESSION_COOKIE", buf, 0)) != PAM_SUCCESS) { - pam_syslog(handle, LOG_ERR, "Failed to set cookie."); + /* To avoid id clashes we add the session id + * source to our session ids. Note that the + * session id source might change during + * runtime, because a filesystem became + * writable or the system reconfigured. */ + buf[strlen(buf)-1] = + mode != SESSION_ID_AUDIT ? (char) mode : 0; + + if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to set session id."); goto finish; } - if (!(cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) { - pam_syslog(handle, LOG_ERR, "Failed to get cookie."); + if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) { + pam_syslog(handle, LOG_ERR, "Failed to get session id."); r = PAM_SESSION_ERR; goto finish; } } - r = asprintf(&buf, "/user/%s/%s", username, cookie); + r = asprintf(&buf, "/user/%s/%s", username, id); } else r = asprintf(&buf, "/user/%s/no-session", username); @@ -353,7 +404,7 @@ _public_ PAM_EXTERN int pam_sm_close_session( bool kill_user = false; int lock_fd = -1, r; char *session_path = NULL, *nosession_path = NULL, *user_path = NULL; - const char *cookie; + const char *id; struct passwd *pw; assert(handle); @@ -379,23 +430,19 @@ _public_ PAM_EXTERN int pam_sm_close_session( goto finish; } - if ((cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) { + if ((id = pam_getenv(handle, "XDG_SESSION_ID"))) { - if (asprintf(&session_path, "/user/%s/%s", username, cookie) < 0 || + if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 || asprintf(&nosession_path, "/user/%s/no-session", username) < 0) { r = PAM_BUF_ERR; goto finish; } if (kill_session) { - pam_syslog(handle, LOG_INFO, "KILLING ENTER"); - /* Kill processes in session cgroup */ if ((r = cg_kill_recursive_and_wait("name=systemd", session_path)) < 0) pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r)); - pam_syslog(handle, LOG_INFO, "KILLING EXIT"); - } else { /* Migrate processes from session to * no-session cgroup. First, try to create the