chiark / gitweb /
pam: use /proc/self/sessionid only if CAP_AUDIT_CONTROL is set
[elogind.git] / src / pam-module.c
index 52507a4cbdb94b0a0516be3e277450c7b36ccf1a..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>
@@ -42,11 +43,14 @@ static int parse_argv(pam_handle_t *handle,
                       bool *create_session,
                       bool *kill_session,
                       bool *kill_user,
-                      bool *keep_root,
-                      char ***controllers) {
+                      char ***controllers,
+                      char ***reset_controllers,
+                      char ***kill_only_users,
+                      char ***kill_exclude_users) {
 
         unsigned i;
-        bool controller_set = false;
+        bool reset_controller_set = false;
+        bool kill_exclude_users_set = false;
 
         assert(argc >= 0);
         assert(argc == 0 || argv);
@@ -81,15 +85,6 @@ static int parse_argv(pam_handle_t *handle,
                         if (kill_user)
                                 *kill_user = k;
 
-                } else if (startswith(argv[i], "keep-root=")) {
-                        if ((k = parse_boolean(argv[i] + 10)) < 0) {
-                                pam_syslog(handle, LOG_ERR, "Failed to parse keep-root= argument.");
-                                return k;
-                        }
-
-                        if (keep_root)
-                                *keep_root = k;
-
                 } else if (startswith(argv[i], "controllers=")) {
 
                         if (controllers) {
@@ -104,7 +99,51 @@ static int parse_argv(pam_handle_t *handle,
                                 *controllers = l;
                         }
 
-                        controller_set = true;
+                } 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]);
@@ -112,8 +151,7 @@ static int parse_argv(pam_handle_t *handle,
                 }
         }
 
-#if 0
-        if (!controller_set && controllers) {
+        if (!reset_controller_set && reset_controllers) {
                 char **l;
 
                 if (!(l = strv_new("cpu", NULL))) {
@@ -121,16 +159,29 @@ static int parse_argv(pam_handle_t *handle,
                         return -ENOMEM;
                 }
 
-                *controllers = l;
+                *reset_controllers = l;
         }
-#endif
 
         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;
 }
 
@@ -148,8 +199,10 @@ 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)
+        if (flock(fd, LOCK_EX) < 0) {
+                close_nointr_nofail(fd);
                 return -errno;
+        }
 
         return fd;
 }
@@ -168,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) {
@@ -187,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;
@@ -221,6 +274,7 @@ static uint64_t get_session_id(int *mode) {
         /* Last attempt, pick a random value */
         return (uint64_t) random_ull();
 }
+
 static int get_user_data(
                 pam_handle_t *handle,
                 const char **ret_username,
@@ -236,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. */
+
+                if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
+                        uint32_t u;
 
-                r = safe_atou32(s, &u);
-                free(s);
+                        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 (r >= 0 && u != (uint32_t) -1 && u > 0) {
+                                have_loginuid = true;
+                                pw = pam_modutil_getpwuid(handle, u);
+                        }
                 }
         }
 
@@ -316,6 +379,22 @@ static int create_user_group(
         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,
@@ -327,7 +406,8 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         char *buf = NULL;
         int lock_fd = -1;
         bool create_session = true;
-        char **controllers = NULL, **c;
+        char **controllers = NULL, **reset_controllers = NULL, **c;
+        char *cgroup_user_tree = NULL;
 
         assert(handle);
 
@@ -337,12 +417,22 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (sd_booted() <= 0)
                 return PAM_SUCCESS;
 
-        if (parse_argv(handle, argc, argv, &create_session, NULL, NULL, NULL, &controllers) < 0)
+        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;
 
+        if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
+                pam_syslog(handle, LOG_ERR, "Failed to determine user cgroup tree: %s", strerror(-r));
+                r = PAM_SYSTEM_ERR;
+                goto finish;
+        }
+
         if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
                 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
                 r = PAM_SYSTEM_ERR;
@@ -355,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;
@@ -406,9 +496,9 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                         }
                 }
 
-                r = asprintf(&buf, "/user/%s/%s", username, id);
+                r = asprintf(&buf, "%s/%s/%s", cgroup_user_tree, username, id);
         } else
-                r = asprintf(&buf, "/user/%s/master", username);
+                r = asprintf(&buf, "%s/%s/master", cgroup_user_tree, username);
 
         if (r < 0) {
                 r = PAM_BUF_ERR;
@@ -425,6 +515,9 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         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:
@@ -434,6 +527,9 @@ finish:
                 close_nointr_nofail(lock_fd);
 
         strv_free(controllers);
+        strv_free(reset_controllers);
+
+        free(cgroup_user_tree);
 
         return r;
 }
@@ -464,6 +560,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,
@@ -472,13 +616,13 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         const char *username = NULL;
         bool kill_session = false;
         bool kill_user = false;
-        bool keep_root = true;
         int lock_fd = -1, r;
         char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
         const char *id;
         struct passwd *pw;
         const void *created = NULL;
-        char **controllers = NULL, **c;
+        char **controllers = NULL, **c, **kill_only_users = NULL, **kill_exclude_users = NULL;
+        char *cgroup_user_tree = NULL;
 
         assert(handle);
 
@@ -486,12 +630,22 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         if (sd_booted() <= 0)
                 return PAM_SUCCESS;
 
-        if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user, &keep_root, &controllers) < 0)
+        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;
 
+        if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
+                pam_syslog(handle, LOG_ERR, "Failed to determine user cgroup tree: %s", strerror(-r));
+                r = PAM_SYSTEM_ERR;
+                goto finish;
+        }
+
         if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
                 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
                 r = PAM_SYSTEM_ERR;
@@ -499,14 +653,14 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         }
 
         /* 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)
+        if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, cgroup_user_tree, 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)
+                if ((r = cg_attach(*c, cgroup_user_tree, 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) {
+        if (asprintf(&user_path, "%s/%s", cgroup_user_tree, username) < 0) {
                 r = PAM_BUF_ERR;
                 goto finish;
         }
@@ -515,13 +669,13 @@ _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/master", username) < 0) {
+                if (asprintf(&session_path, "%s/%s/%s", cgroup_user_tree, username, id) < 0 ||
+                    asprintf(&nosession_path, "%s/%s/master", cgroup_user_tree, username) < 0) {
                         r = PAM_BUF_ERR;
                         goto finish;
                 }
 
-                if (kill_session && (pw->pw_uid != 0 || !keep_root))  {
+                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 */
@@ -555,7 +709,7 @@ _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 && (pw->pw_uid != 0 || !keep_root)) {
+        if (kill_user && r == 0 && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
 
                 /* Kill user cgroup */
                 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
@@ -599,6 +753,10 @@ finish:
         free(user_path);
 
         strv_free(controllers);
+        strv_free(kill_exclude_users);
+        strv_free(kill_only_users);
+
+        free(cgroup_user_tree);
 
         return r;
 }