chiark / gitweb /
pam: do not leak file descriptor if flock fails
[elogind.git] / src / pam-module.c
index fc3aac185d72b618e4ffd24b58fc6cc11770afe7..e1a1a5001cedc9b438a31546906511949716c703 100644 (file)
@@ -42,13 +42,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;
-#if 0
-        bool controller_set = false;
-#endif
+        bool reset_controller_set = false;
+        bool kill_exclude_users_set = false;
 
         assert(argc >= 0);
         assert(argc == 0 || argv);
@@ -83,15 +84,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) {
@@ -106,9 +98,51 @@ static int parse_argv(pam_handle_t *handle,
                                 *controllers = l;
                         }
 
-#if 0
-                        controller_set = true;
-#endif
+                } 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]);
@@ -116,8 +150,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))) {
@@ -125,16 +158,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;
 }
 
@@ -152,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;
 }
@@ -320,6 +370,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,
@@ -331,7 +397,7 @@ _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;
 
         assert(handle);
 
@@ -341,7 +407,11 @@ _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)
@@ -429,6 +499,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:
@@ -438,6 +511,7 @@ finish:
                 close_nointr_nofail(lock_fd);
 
         strv_free(controllers);
+        strv_free(reset_controllers);
 
         return r;
 }
@@ -468,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,
@@ -476,13 +598,12 @@ _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;
 
         assert(handle);
 
@@ -490,7 +611,11 @@ _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)
@@ -525,7 +650,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                         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 */
@@ -559,7 +684,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)
@@ -603,6 +728,8 @@ finish:
         free(user_path);
 
         strv_free(controllers);
+        strv_free(kill_exclude_users);
+        strv_free(kill_only_users);
 
         return r;
 }