X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fpam-module.c;h=387b77ed56fc0dbae8f6b7752d0f0054027841ef;hp=5157897a71d1512ceacf822faf7dc091d975c780;hb=97e3d13fb4d28e25803f1a6543ae2051b5fcff1d;hpb=8c6db8336536916d0476ff8233e0abf40a2f6aab diff --git a/src/pam-module.c b/src/pam-module.c index 5157897a7..387b77ed5 100644 --- a/src/pam-module.c +++ b/src/pam-module.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -30,8 +31,6 @@ #include #include -#include - #include "util.h" #include "cgroup-util.h" #include "macro.h" @@ -96,45 +95,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, @@ -169,24 +208,35 @@ static int get_user_data( return PAM_SUCCESS; } -static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach) { +static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach, bool remember) { int r; assert(handle); assert(group); if (attach) - r = cg_create_and_attach("name=systemd", group, 0); + r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, group, 0); else - r = cg_create("name=systemd", group); + r = cg_create(SYSTEMD_CGROUP_CONTROLLER, group); if (r < 0) { pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r)); return PAM_SESSION_ERR; } - if ((r = cg_set_task_access("name=systemd", group, 0755, pw->pw_uid, pw->pw_gid)) < 0 || - (r = cg_set_group_access("name=systemd", group, 0755, pw->pw_uid, pw->pw_gid)) < 0) { + if (r > 0 && remember) { + /* Remember that it was us who created this group, and + * that hence we need to remove it too. This is a + * protection against removing the cgroup when run + * recursively. */ + if ((r = pam_set_data(handle, "systemd.created", INT_TO_PTR(1), NULL)) != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to install created variable."); + return r; + } + } + + if ((r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0 || + (r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) { pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r)); return PAM_SESSION_ERR; } @@ -208,7 +258,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( assert(handle); - pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); + /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */ if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0) return PAM_SESSION_ERR; @@ -217,12 +267,6 @@ _public_ PAM_EXTERN int pam_sm_open_session( if (sd_booted() <= 0) return PAM_SUCCESS; - if ((r = cgroup_init()) != 0) { - pam_syslog(handle, LOG_ERR, "libcgroup initialization failed: %s", cgroup_strerror(r)); - r = PAM_SESSION_ERR; - goto finish; - } - if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS) goto finish; @@ -258,28 +302,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); @@ -288,7 +342,9 @@ _public_ PAM_EXTERN int pam_sm_open_session( goto finish; } - if ((r = create_user_group(handle, buf, pw, true)) != PAM_SUCCESS) + pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf); + + if ((r = create_user_group(handle, buf, pw, true, true)) != PAM_SUCCESS) goto finish; r = PAM_SUCCESS; @@ -303,44 +359,29 @@ finish: } static int session_remains(pam_handle_t *handle, const char *user_path) { - struct cgroup_file_info info; - int level = 0, r; - void *iterator = NULL; + int r; bool remains = false; + DIR *d; + char *subgroup; - zero(info); - - r = cgroup_walk_tree_begin("name=systemd", user_path, 0, &iterator, &info, &level); - while (r == 0) { - - if (info.type != CGROUP_FILE_TYPE_DIR) - goto next; - - if (streq(info.path, "")) - goto next; - - if (streq(info.path, "no-session")) - goto next; + if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0) + return r; - remains = true; - break; + while ((r = cg_read_subgroup(d, &subgroup)) > 0) { - next: + remains = !streq(subgroup, "no-session"); + free(subgroup); - r = cgroup_walk_tree_next(0, &iterator, &info, level); + if (remains) + break; } + closedir(d); - if (remains) - r = 1; - else if (r == 0 || r == ECGEOF) - r = 0; - else - r = cg_translate_error(r, errno); - - assert_se(cgroup_walk_tree_end(&iterator) == 0); + if (r < 0) + return r; - return r; + return !!remains; } _public_ PAM_EXTERN int pam_sm_close_session( @@ -353,8 +394,9 @@ _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; + const void *created = NULL; assert(handle); @@ -374,50 +416,47 @@ _public_ PAM_EXTERN int pam_sm_close_session( goto finish; } + /* We are probably still in some session/no-session dir. Move ourselves out of the way as first step */ + if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0) + pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r)); + if (asprintf(&user_path, "/user/%s", username) < 0) { r = PAM_BUF_ERR; goto finish; } - if ((cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) { + pam_get_data(handle, "systemd.created", &created); + + if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) { - 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"); + pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username); - /* Kill processes in session cgroup */ - if ((r = cg_kill_recursive_and_wait("name=systemd", session_path)) < 0) + /* Kill processes in session cgroup, and delete it */ + if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0) pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r)); + } else { + pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path); - pam_syslog(handle, LOG_INFO, "KILLING EXIT"); - - } else { /* Migrate processes from session to * no-session cgroup. First, try to create the * no-session group in case it doesn't exist - * yet. */ - create_user_group(handle, nosession_path, pw, 0); + * yet. Also, delete the session group. */ + create_user_group(handle, nosession_path, pw, false, false); - if ((r = cg_migrate_recursive("name=systemd", session_path, nosession_path, false)) < 0) + if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0) pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r)); } - - /* Delete session cgroup */ - if (r < 0) - pam_syslog(handle, LOG_INFO, "Couldn't empty session cgroup, not deleting."); - else { - if ((r = cg_delete("name=systemd", session_path)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to delete session cgroup: %s", strerror(-r)); - } } /* GC user tree */ - cg_trim("name=systemd", user_path, false); + cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false); if ((r = session_remains(handle, user_path)) < 0) pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r)); @@ -426,32 +465,33 @@ _public_ PAM_EXTERN int pam_sm_close_session( if (kill_user && r == 0) { /* Kill no-session cgroup */ - if ((r = cg_kill_recursive_and_wait("name=systemd", user_path)) < 0) + if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0) pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r)); } else { - if ((r = cg_is_empty_recursive("name=systemd", user_path, true)) < 0) + if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0) pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r)); - /* If we managed to kill somebody, don't cleanup the cgroup. */ - if (r == 0) + /* Remove user cgroup */ + if (r > 0) { + if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0) + pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r)); + + /* If we managed to find somebody, don't cleanup the cgroup. */ + } else if (r == 0) r = -EBUSY; } if (r >= 0) { const char *runtime_dir; - /* Remove user cgroup */ - if ((r = cg_delete("name=systemd", user_path)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r)); - - /* This will migrate us to the /user cgroup. */ - if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR"))) if ((r = rm_rf(runtime_dir, false, true)) < 0) pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r)); } + /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */ + r = PAM_SUCCESS; finish: