X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fpam-module.c;h=e1a1a5001cedc9b438a31546906511949716c703;hp=f668c3b37386d59cf6b613a08b14c84e8264930f;hb=90102b22ba0a9a6aa8ff1b9d32349e100902a8d7;hpb=672c48cc065251ddbdc3926221e8a7718241cccd diff --git a/src/pam-module.c b/src/pam-module.c index f668c3b37..e1a1a5001 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. @@ -35,14 +35,21 @@ #include "cgroup-util.h" #include "macro.h" #include "sd-daemon.h" +#include "strv.h" static int parse_argv(pam_handle_t *handle, int argc, const char **argv, bool *create_session, bool *kill_session, - bool *kill_user) { + bool *kill_user, + char ***controllers, + char ***reset_controllers, + char ***kill_only_users, + char ***kill_exclude_users) { unsigned i; + bool reset_controller_set = false; + bool kill_exclude_users_set = false; assert(argc >= 0); assert(argc == 0 || argv); @@ -58,6 +65,7 @@ static int parse_argv(pam_handle_t *handle, if (create_session) *create_session = k; + } else if (startswith(argv[i], "kill-session=")) { if ((k = parse_boolean(argv[i] + 13)) < 0) { pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument."); @@ -75,15 +83,104 @@ static int parse_argv(pam_handle_t *handle, if (kill_user) *kill_user = k; + + } else if (startswith(argv[i], "controllers=")) { + + if (controllers) { + char **l; + + if (!(l = strv_split(argv[i] + 12, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } + + strv_free(*controllers); + *controllers = l; + } + + } else if (startswith(argv[i], "reset-controllers=")) { + + if (reset_controllers) { + char **l; + + if (!(l = strv_split(argv[i] + 18, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } + + strv_free(*reset_controllers); + *reset_controllers = l; + } + + reset_controller_set = true; + + } else if (startswith(argv[i], "kill-only-users=")) { + + if (kill_only_users) { + char **l; + + if (!(l = strv_split(argv[i] + 16, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } + + strv_free(*kill_only_users); + *kill_only_users = l; + } + + } else if (startswith(argv[i], "kill-exclude-users=")) { + + if (kill_exclude_users) { + char **l; + + if (!(l = strv_split(argv[i] + 19, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } + + strv_free(*kill_exclude_users); + *kill_exclude_users = l; + } + + kill_exclude_users_set = true; + } else { pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]); return -EINVAL; } } + if (!reset_controller_set && reset_controllers) { + char **l; + + if (!(l = strv_new("cpu", NULL))) { + pam_syslog(handle, LOG_ERR, "Out of memory"); + return -ENOMEM; + } + + *reset_controllers = l; + } + + if (controllers) + strv_remove(*controllers, "name=systemd"); + + if (reset_controllers) + strv_remove(*reset_controllers, "name=systemd"); + if (kill_session && *kill_session && kill_user) *kill_user = true; + if (!kill_exclude_users_set && kill_exclude_users) { + char **l; + + if (!(l = strv_new("root", NULL))) { + pam_syslog(handle, LOG_ERR, "Out of memory"); + return -ENOMEM; + } + + *kill_exclude_users = l; + } + return 0; } @@ -101,8 +198,12 @@ static int open_file_and_lock(const char *fn) { * 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; + if (flock(fd, LOCK_EX) < 0) { + int r = -errno; + + close_nointr_nofail(fd); + return r; + } return fd; } @@ -128,7 +229,7 @@ static uint64_t get_session_id(int *mode) { r = safe_atou32(s, &u); free(s); - if (r >= 0 && u != (uint32_t) -1) { + if (r >= 0 && u != (uint32_t) -1 && u > 0) { *mode = SESSION_ID_AUDIT; return (uint64_t) u; } @@ -179,45 +280,70 @@ static int get_user_data( const char **ret_username, struct passwd **ret_pw) { - const char *username; - struct passwd *pw; + const char *username = NULL; + struct passwd *pw = NULL; int r; + bool have_loginuid = false; + char *s; assert(handle); assert(ret_username); assert(ret_pw); - if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) { - pam_syslog(handle, LOG_ERR, "Failed to get user name."); - return r; + if (read_one_line_file("/proc/self/loginuid", &s) >= 0) { + uint32_t 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); + } } - if (!username || !*username) { - pam_syslog(handle, LOG_ERR, "User name not valid."); - return PAM_AUTH_ERR; + if (!have_loginuid) { + if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to get user name."); + return r; + } + + if (!username || !*username) { + pam_syslog(handle, LOG_ERR, "User name not valid."); + return PAM_AUTH_ERR; + } + + pw = pam_modutil_getpwnam(handle, username); } - if (!(pw = pam_modutil_getpwnam(handle, username))) { + if (!pw) { pam_syslog(handle, LOG_ERR, "Failed to get user data."); return PAM_USER_UNKNOWN; } *ret_pw = pw; - *ret_username = username; + *ret_username = username ? username : pw->pw_name; return PAM_SUCCESS; } -static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach, bool remember) { +static int create_user_group( + pam_handle_t *handle, + const char *controller, + const char *group, + struct passwd *pw, + bool attach, + bool remember) { + int r; assert(handle); assert(group); if (attach) - r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, group, 0); + r = cg_create_and_attach(controller, group, 0); else - r = cg_create(SYSTEMD_CGROUP_CONTROLLER, group); + r = cg_create(controller, group); if (r < 0) { pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r)); @@ -235,8 +361,8 @@ static int create_user_group(pam_handle_t *handle, const char *group, struct pas } } - 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) { + if ((r = cg_set_task_access(controller, group, 0644, pw->pw_uid, pw->pw_gid)) < 0 || + (r = cg_set_group_access(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; } @@ -244,6 +370,22 @@ static int create_user_group(pam_handle_t *handle, const char *group, struct pas return PAM_SUCCESS; } +static int reset_group( + pam_handle_t *handle, + const char *controller) { + + int r; + + assert(handle); + + if ((r = cg_attach(controller, "/", 0)) < 0) { + pam_syslog(handle, LOG_ERR, "Failed to reset cgroup for controller %s: %s", controller, strerror(-r)); + return PAM_SESSION_ERR; + } + + return PAM_SUCCESS; +} + _public_ PAM_EXTERN int pam_sm_open_session( pam_handle_t *handle, int flags, @@ -255,18 +397,23 @@ _public_ PAM_EXTERN int pam_sm_open_session( char *buf = NULL; int lock_fd = -1; bool create_session = true; + char **controllers = NULL, **reset_controllers = NULL, **c; assert(handle); /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */ - if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0) - return PAM_SESSION_ERR; - /* Make this a NOP on non-systemd systems */ if (sd_booted() <= 0) return PAM_SUCCESS; + if (parse_argv(handle, + argc, argv, + &create_session, NULL, NULL, + &controllers, &reset_controllers, + NULL, NULL) < 0) + return PAM_SESSION_ERR; + if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS) goto finish; @@ -335,7 +482,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( r = asprintf(&buf, "/user/%s/%s", username, id); } else - r = asprintf(&buf, "/user/%s/no-session", username); + r = asprintf(&buf, "/user/%s/master", username); if (r < 0) { r = PAM_BUF_ERR; @@ -344,9 +491,17 @@ _public_ PAM_EXTERN int pam_sm_open_session( 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) + if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS) goto finish; + /* The additional controllers don't really matter, so we + * ignore the return value */ + STRV_FOREACH(c, controllers) + create_user_group(handle, *c, buf, pw, true, false); + + STRV_FOREACH(c, reset_controllers) + reset_group(handle, *c); + r = PAM_SUCCESS; finish: @@ -355,6 +510,9 @@ finish: if (lock_fd >= 0) close_nointr_nofail(lock_fd); + strv_free(controllers); + strv_free(reset_controllers); + return r; } @@ -369,7 +527,7 @@ static int session_remains(pam_handle_t *handle, const char *user_path) { while ((r = cg_read_subgroup(d, &subgroup)) > 0) { - remains = !streq(subgroup, "no-session"); + remains = !streq(subgroup, "master"); free(subgroup); if (remains) @@ -384,6 +542,54 @@ static int session_remains(pam_handle_t *handle, const char *user_path) { return !!remains; } +static bool check_user_lists( + pam_handle_t *handle, + uid_t uid, + char **kill_only_users, + char **kill_exclude_users) { + + const char *name = NULL; + char **l; + + assert(handle); + + if (uid == 0) + name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */ + else { + struct passwd *pw; + + if ((pw = pam_modutil_getpwuid(handle, uid))) + name = pw->pw_name; + } + + STRV_FOREACH(l, kill_exclude_users) { + uint32_t id; + + if (safe_atou32(*l, &id) >= 0) + if ((uid_t) id == uid) + return false; + + if (name && streq(name, *l)) + return false; + } + + if (strv_isempty(kill_only_users)) + return true; + + STRV_FOREACH(l, kill_only_users) { + uint32_t id; + + if (safe_atou32(*l, &id) >= 0) + if ((uid_t) id == uid) + return true; + + if (name && streq(name, *l)) + return true; + } + + return false; +} + _public_ PAM_EXTERN int pam_sm_close_session( pam_handle_t *handle, int flags, @@ -397,16 +603,21 @@ _public_ PAM_EXTERN int pam_sm_close_session( const char *id; struct passwd *pw; const void *created = NULL; + char **controllers = NULL, **c, **kill_only_users = NULL, **kill_exclude_users = NULL; assert(handle); - if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user) < 0) - return PAM_SESSION_ERR; - /* Make this a NOP on non-systemd systems */ if (sd_booted() <= 0) return PAM_SUCCESS; + if (parse_argv(handle, + argc, argv, + NULL, &kill_session, &kill_user, + &controllers, NULL, + &kill_only_users, &kill_exclude_users) < 0) + return PAM_SESSION_ERR; + if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS) goto finish; @@ -416,10 +627,14 @@ _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 */ + /* We are probably still in some session/user 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)); + STRV_FOREACH(c, controllers) + if ((r = cg_attach(*c, "/user", 0)) < 0) + pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r)); + if (asprintf(&user_path, "/user/%s", username) < 0) { r = PAM_BUF_ERR; goto finish; @@ -430,12 +645,12 @@ _public_ PAM_EXTERN int pam_sm_close_session( if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) { if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 || - asprintf(&nosession_path, "/user/%s/no-session", username) < 0) { + asprintf(&nosession_path, "/user/%s/master", username) < 0) { r = PAM_BUF_ERR; goto finish; } - if (kill_session) { + if (kill_session && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) { pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username); /* Kill processes in session cgroup, and delete it */ @@ -444,15 +659,22 @@ _public_ PAM_EXTERN int pam_sm_close_session( } else { pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path); - /* Migrate processes from session to - * no-session cgroup. First, try to create the - * no-session group in case it doesn't exist - * yet. Also, delete the session group. */ - create_user_group(handle, nosession_path, pw, false, false); + /* Migrate processes from session to user + * cgroup. First, try to create the user group + * in case it doesn't exist yet. Also, delete + * the session group. */ + create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false); 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)); } + + STRV_FOREACH(c, controllers) { + create_user_group(handle, *c, nosession_path, pw, false, false); + + if ((r = cg_migrate_recursive(*c, session_path, nosession_path, false, true)) < 0) + pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r)); + } } /* GC user tree */ @@ -462,9 +684,9 @@ _public_ PAM_EXTERN int pam_sm_close_session( pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r)); /* Kill user processes not attached to any session */ - if (kill_user && r == 0) { + if (kill_user && r == 0 && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) { - /* Kill no-session cgroup */ + /* Kill user cgroup */ 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 { @@ -482,6 +704,9 @@ _public_ PAM_EXTERN int pam_sm_close_session( r = -EBUSY; } + STRV_FOREACH(c, controllers) + cg_trim(*c, user_path, true); + if (r >= 0) { const char *runtime_dir; @@ -502,5 +727,9 @@ finish: free(nosession_path); free(user_path); + strv_free(controllers); + strv_free(kill_exclude_users); + strv_free(kill_only_users); + return r; }