1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <security/pam_modules.h>
29 #include <security/_pam_macros.h>
30 #include <security/pam_modutil.h>
31 #include <security/pam_ext.h>
32 #include <security/pam_misc.h>
35 #include "cgroup-util.h"
37 #include "sd-daemon.h"
40 static int parse_argv(pam_handle_t
*handle
,
41 int argc
, const char **argv
,
46 char ***reset_controllers
,
47 char ***kill_only_users
,
48 char ***kill_exclude_users
) {
51 bool reset_controller_set
= false;
52 bool kill_exclude_users_set
= false;
55 assert(argc
== 0 || argv
);
57 for (i
= 0; i
< (unsigned) argc
; i
++) {
60 if (startswith(argv
[i
], "create-session=")) {
61 if ((k
= parse_boolean(argv
[i
] + 15)) < 0) {
62 pam_syslog(handle
, LOG_ERR
, "Failed to parse create-session= argument.");
69 } else if (startswith(argv
[i
], "kill-session=")) {
70 if ((k
= parse_boolean(argv
[i
] + 13)) < 0) {
71 pam_syslog(handle
, LOG_ERR
, "Failed to parse kill-session= argument.");
78 } else if (startswith(argv
[i
], "kill-user=")) {
79 if ((k
= parse_boolean(argv
[i
] + 10)) < 0) {
80 pam_syslog(handle
, LOG_ERR
, "Failed to parse kill-user= argument.");
87 } else if (startswith(argv
[i
], "controllers=")) {
92 if (!(l
= strv_split(argv
[i
] + 12, ","))) {
93 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
97 strv_free(*controllers
);
101 } else if (startswith(argv
[i
], "reset-controllers=")) {
103 if (reset_controllers
) {
106 if (!(l
= strv_split(argv
[i
] + 18, ","))) {
107 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
111 strv_free(*reset_controllers
);
112 *reset_controllers
= l
;
115 reset_controller_set
= true;
117 } else if (startswith(argv
[i
], "kill-only-users=")) {
119 if (kill_only_users
) {
122 if (!(l
= strv_split(argv
[i
] + 16, ","))) {
123 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
127 strv_free(*kill_only_users
);
128 *kill_only_users
= l
;
131 } else if (startswith(argv
[i
], "kill-exclude-users=")) {
133 if (kill_exclude_users
) {
136 if (!(l
= strv_split(argv
[i
] + 19, ","))) {
137 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
141 strv_free(*kill_exclude_users
);
142 *kill_exclude_users
= l
;
145 kill_exclude_users_set
= true;
148 pam_syslog(handle
, LOG_ERR
, "Unknown parameter '%s'.", argv
[i
]);
153 if (!reset_controller_set
&& reset_controllers
) {
156 if (!(l
= strv_new("cpu", NULL
))) {
157 pam_syslog(handle
, LOG_ERR
, "Out of memory");
161 *reset_controllers
= l
;
165 strv_remove(*controllers
, "name=systemd");
167 if (reset_controllers
)
168 strv_remove(*reset_controllers
, "name=systemd");
170 if (kill_session
&& *kill_session
&& kill_user
)
173 if (!kill_exclude_users_set
&& kill_exclude_users
) {
176 if (!(l
= strv_new("root", NULL
))) {
177 pam_syslog(handle
, LOG_ERR
, "Out of memory");
181 *kill_exclude_users
= l
;
187 static int open_file_and_lock(const char *fn
) {
192 if ((fd
= open(fn
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
|O_CREAT
, 0600)) < 0)
195 /* The BSD socket semantics are a lot nicer than those of
196 * POSIX locks. Which is why we use flock() here. BSD locking
197 * does not work across NFS which however is not needed here
198 * as the filesystems in question should be local, and only
199 * locally accessible, and most likely even tmpfs. */
201 if (flock(fd
, LOCK_EX
) < 0) {
202 close_nointr_nofail(fd
);
210 SESSION_ID_AUDIT
= 'a',
211 SESSION_ID_COUNTER
= 'c',
212 SESSION_ID_RANDOM
= 'r'
215 static uint64_t get_session_id(int *mode
) {
221 /* First attempt: let's use the session ID of the audit
222 * system, if it is available. */
223 if (read_one_line_file("/proc/self/sessionid", &s
) >= 0) {
227 r
= safe_atou32(s
, &u
);
230 if (r
>= 0 && u
!= (uint32_t) -1 && u
> 0) {
231 *mode
= SESSION_ID_AUDIT
;
236 /* Second attempt, use our own counter. */
237 if ((fd
= open_file_and_lock(RUNTIME_DIR
"/user/.pam-systemd-session")) >= 0) {
241 /* We do a bit of endianess swapping here, just to be
242 * sure. /var should be machine specific anyway, and
243 * /var/run even mounted from tmpfs, so this
244 * byteswapping should really not be necessary. But
245 * then again, you never know, so let's avoid any
248 if (loop_read(fd
, &counter
, sizeof(counter
), false) != sizeof(counter
))
251 counter
= le64toh(counter
) + 1;
253 if (lseek(fd
, 0, SEEK_SET
) == 0) {
254 uint64_t swapped
= htole64(counter
);
256 r
= loop_write(fd
, &swapped
, sizeof(swapped
), false);
258 if (r
!= sizeof(swapped
))
263 close_nointr_nofail(fd
);
266 *mode
= SESSION_ID_COUNTER
;
271 *mode
= SESSION_ID_RANDOM
;
273 /* Last attempt, pick a random value */
274 return (uint64_t) random_ull();
277 static int get_user_data(
278 pam_handle_t
*handle
,
279 const char **ret_username
,
280 struct passwd
**ret_pw
) {
282 const char *username
= NULL
;
283 struct passwd
*pw
= NULL
;
285 bool have_loginuid
= false;
289 assert(ret_username
);
292 if (read_one_line_file("/proc/self/loginuid", &s
) >= 0) {
295 r
= safe_atou32(s
, &u
);
298 if (r
>= 0 && u
!= (uint32_t) -1 && u
> 0) {
299 have_loginuid
= true;
300 pw
= pam_modutil_getpwuid(handle
, u
);
304 if (!have_loginuid
) {
305 if ((r
= pam_get_user(handle
, &username
, NULL
)) != PAM_SUCCESS
) {
306 pam_syslog(handle
, LOG_ERR
, "Failed to get user name.");
310 if (!username
|| !*username
) {
311 pam_syslog(handle
, LOG_ERR
, "User name not valid.");
315 pw
= pam_modutil_getpwnam(handle
, username
);
319 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
320 return PAM_USER_UNKNOWN
;
324 *ret_username
= username
? username
: pw
->pw_name
;
329 static int create_user_group(
330 pam_handle_t
*handle
,
331 const char *controller
,
343 r
= cg_create_and_attach(controller
, group
, 0);
345 r
= cg_create(controller
, group
);
348 pam_syslog(handle
, LOG_ERR
, "Failed to create cgroup: %s", strerror(-r
));
349 return PAM_SESSION_ERR
;
352 if (r
> 0 && remember
) {
353 /* Remember that it was us who created this group, and
354 * that hence we need to remove it too. This is a
355 * protection against removing the cgroup when run
357 if ((r
= pam_set_data(handle
, "systemd.created", INT_TO_PTR(1), NULL
)) != PAM_SUCCESS
) {
358 pam_syslog(handle
, LOG_ERR
, "Failed to install created variable.");
363 if ((r
= cg_set_task_access(controller
, group
, 0644, pw
->pw_uid
, pw
->pw_gid
)) < 0 ||
364 (r
= cg_set_group_access(controller
, group
, 0755, pw
->pw_uid
, pw
->pw_gid
)) < 0) {
365 pam_syslog(handle
, LOG_ERR
, "Failed to change access modes: %s", strerror(-r
));
366 return PAM_SESSION_ERR
;
372 static int reset_group(
373 pam_handle_t
*handle
,
374 const char *controller
) {
380 if ((r
= cg_attach(controller
, "/", 0)) < 0) {
381 pam_syslog(handle
, LOG_ERR
, "Failed to reset cgroup for controller %s: %s", controller
, strerror(-r
));
382 return PAM_SESSION_ERR
;
388 _public_ PAM_EXTERN
int pam_sm_open_session(
389 pam_handle_t
*handle
,
391 int argc
, const char **argv
) {
393 const char *username
= NULL
;
398 bool create_session
= true;
399 char **controllers
= NULL
, **reset_controllers
= NULL
, **c
;
400 char *cgroup_user_tree
= NULL
;
404 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
406 /* Make this a NOP on non-systemd systems */
407 if (sd_booted() <= 0)
410 if (parse_argv(handle
,
412 &create_session
, NULL
, NULL
,
413 &controllers
, &reset_controllers
,
415 return PAM_SESSION_ERR
;
417 if ((r
= get_user_data(handle
, &username
, &pw
)) != PAM_SUCCESS
)
420 if ((r
= cg_get_user_path(&cgroup_user_tree
)) < 0) {
421 pam_syslog(handle
, LOG_ERR
, "Failed to determine user cgroup tree: %s", strerror(-r
));
426 if (safe_mkdir(RUNTIME_DIR
"/user", 0755, 0, 0) < 0) {
427 pam_syslog(handle
, LOG_ERR
, "Failed to create runtime directory: %m");
432 if ((lock_fd
= open_file_and_lock(RUNTIME_DIR
"/user/.pam-systemd-lock")) < 0) {
433 pam_syslog(handle
, LOG_ERR
, "Failed to lock runtime directory: %m");
438 /* Create /var/run/$USER */
440 if (asprintf(&buf
, RUNTIME_DIR
"/user/%s", username
) < 0) {
445 if (safe_mkdir(buf
, 0700, pw
->pw_uid
, pw
->pw_gid
) < 0) {
446 pam_syslog(handle
, LOG_WARNING
, "Failed to create runtime directory: %m");
449 } else if ((r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", buf
, 0)) != PAM_SUCCESS
) {
450 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
457 if (create_session
) {
460 /* Reuse or create XDG session ID */
461 if (!(id
= pam_getenv(handle
, "XDG_SESSION_ID"))) {
464 if (asprintf(&buf
, "%llux", (unsigned long long) get_session_id(&mode
)) < 0) {
469 /* To avoid id clashes we add the session id
470 * source to our session ids. Note that the
471 * session id source might change during
472 * runtime, because a filesystem became
473 * writable or the system reconfigured. */
475 mode
!= SESSION_ID_AUDIT
? (char) mode
: 0;
477 if ((r
= pam_misc_setenv(handle
, "XDG_SESSION_ID", buf
, 0)) != PAM_SUCCESS
) {
478 pam_syslog(handle
, LOG_ERR
, "Failed to set session id.");
482 if (!(id
= pam_getenv(handle
, "XDG_SESSION_ID"))) {
483 pam_syslog(handle
, LOG_ERR
, "Failed to get session id.");
489 r
= asprintf(&buf
, "%s/%s/%s", cgroup_user_tree
, username
, id
);
491 r
= asprintf(&buf
, "%s/%s/master", cgroup_user_tree
, username
);
498 pam_syslog(handle
, LOG_INFO
, "Moving new user session for %s into control group %s.", username
, buf
);
500 if ((r
= create_user_group(handle
, SYSTEMD_CGROUP_CONTROLLER
, buf
, pw
, true, true)) != PAM_SUCCESS
)
503 /* The additional controllers don't really matter, so we
504 * ignore the return value */
505 STRV_FOREACH(c
, controllers
)
506 create_user_group(handle
, *c
, buf
, pw
, true, false);
508 STRV_FOREACH(c
, reset_controllers
)
509 reset_group(handle
, *c
);
517 close_nointr_nofail(lock_fd
);
519 strv_free(controllers
);
520 strv_free(reset_controllers
);
522 free(cgroup_user_tree
);
527 static int session_remains(pam_handle_t
*handle
, const char *user_path
) {
529 bool remains
= false;
533 if ((r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, user_path
, &d
)) < 0)
536 while ((r
= cg_read_subgroup(d
, &subgroup
)) > 0) {
538 remains
= !streq(subgroup
, "master");
553 static bool check_user_lists(
554 pam_handle_t
*handle
,
556 char **kill_only_users
,
557 char **kill_exclude_users
) {
559 const char *name
= NULL
;
565 name
= "root"; /* Avoid obvious NSS requests, to suppress network traffic */
569 if ((pw
= pam_modutil_getpwuid(handle
, uid
)))
573 STRV_FOREACH(l
, kill_exclude_users
) {
576 if (safe_atou32(*l
, &id
) >= 0)
577 if ((uid_t
) id
== uid
)
580 if (name
&& streq(name
, *l
))
584 if (strv_isempty(kill_only_users
))
587 STRV_FOREACH(l
, kill_only_users
) {
590 if (safe_atou32(*l
, &id
) >= 0)
591 if ((uid_t
) id
== uid
)
594 if (name
&& streq(name
, *l
))
601 _public_ PAM_EXTERN
int pam_sm_close_session(
602 pam_handle_t
*handle
,
604 int argc
, const char **argv
) {
606 const char *username
= NULL
;
607 bool kill_session
= false;
608 bool kill_user
= false;
610 char *session_path
= NULL
, *nosession_path
= NULL
, *user_path
= NULL
;
613 const void *created
= NULL
;
614 char **controllers
= NULL
, **c
, **kill_only_users
= NULL
, **kill_exclude_users
= NULL
;
615 char *cgroup_user_tree
= NULL
;
619 /* Make this a NOP on non-systemd systems */
620 if (sd_booted() <= 0)
623 if (parse_argv(handle
,
625 NULL
, &kill_session
, &kill_user
,
627 &kill_only_users
, &kill_exclude_users
) < 0)
628 return PAM_SESSION_ERR
;
630 if ((r
= get_user_data(handle
, &username
, &pw
)) != PAM_SUCCESS
)
633 if ((r
= cg_get_user_path(&cgroup_user_tree
)) < 0) {
634 pam_syslog(handle
, LOG_ERR
, "Failed to determine user cgroup tree: %s", strerror(-r
));
639 if ((lock_fd
= open_file_and_lock(RUNTIME_DIR
"/user/.pam-systemd-lock")) < 0) {
640 pam_syslog(handle
, LOG_ERR
, "Failed to lock runtime directory: %m");
645 /* We are probably still in some session/user dir. Move ourselves out of the way as first step */
646 if ((r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, cgroup_user_tree
, 0)) < 0)
647 pam_syslog(handle
, LOG_ERR
, "Failed to move us away: %s", strerror(-r
));
649 STRV_FOREACH(c
, controllers
)
650 if ((r
= cg_attach(*c
, cgroup_user_tree
, 0)) < 0)
651 pam_syslog(handle
, LOG_ERR
, "Failed to move us away in %s hierarchy: %s", *c
, strerror(-r
));
653 if (asprintf(&user_path
, "%s/%s", cgroup_user_tree
, username
) < 0) {
658 pam_get_data(handle
, "systemd.created", &created
);
660 if ((id
= pam_getenv(handle
, "XDG_SESSION_ID")) && created
) {
662 if (asprintf(&session_path
, "%s/%s/%s", cgroup_user_tree
, username
, id
) < 0 ||
663 asprintf(&nosession_path
, "%s/%s/master", cgroup_user_tree
, username
) < 0) {
668 if (kill_session
&& check_user_lists(handle
, pw
->pw_uid
, kill_only_users
, kill_exclude_users
)) {
669 pam_syslog(handle
, LOG_INFO
, "Killing remaining processes of user session %s of %s.", id
, username
);
671 /* Kill processes in session cgroup, and delete it */
672 if ((r
= cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER
, session_path
, true)) < 0)
673 pam_syslog(handle
, LOG_ERR
, "Failed to kill session cgroup: %s", strerror(-r
));
675 pam_syslog(handle
, LOG_INFO
, "Moving remaining processes of user session %s of %s into control group %s.", id
, username
, nosession_path
);
677 /* Migrate processes from session to user
678 * cgroup. First, try to create the user group
679 * in case it doesn't exist yet. Also, delete
680 * the session group. */
681 create_user_group(handle
, SYSTEMD_CGROUP_CONTROLLER
, nosession_path
, pw
, false, false);
683 if ((r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, session_path
, nosession_path
, false, true)) < 0)
684 pam_syslog(handle
, LOG_ERR
, "Failed to migrate session cgroup: %s", strerror(-r
));
687 STRV_FOREACH(c
, controllers
) {
688 create_user_group(handle
, *c
, nosession_path
, pw
, false, false);
690 if ((r
= cg_migrate_recursive(*c
, session_path
, nosession_path
, false, true)) < 0)
691 pam_syslog(handle
, LOG_ERR
, "Failed to migrate session cgroup in hierarchy %s: %s", *c
, strerror(-r
));
696 cg_trim(SYSTEMD_CGROUP_CONTROLLER
, user_path
, false);
698 if ((r
= session_remains(handle
, user_path
)) < 0)
699 pam_syslog(handle
, LOG_ERR
, "Failed to determine whether a session remains: %s", strerror(-r
));
701 /* Kill user processes not attached to any session */
702 if (kill_user
&& r
== 0 && check_user_lists(handle
, pw
->pw_uid
, kill_only_users
, kill_exclude_users
)) {
704 /* Kill user cgroup */
705 if ((r
= cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER
, user_path
, true)) < 0)
706 pam_syslog(handle
, LOG_ERR
, "Failed to kill user cgroup: %s", strerror(-r
));
709 if ((r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, user_path
, true)) < 0)
710 pam_syslog(handle
, LOG_ERR
, "Failed to check user cgroup: %s", strerror(-r
));
712 /* Remove user cgroup */
714 if ((r
= cg_delete(SYSTEMD_CGROUP_CONTROLLER
, user_path
)) < 0)
715 pam_syslog(handle
, LOG_ERR
, "Failed to delete user cgroup: %s", strerror(-r
));
717 /* If we managed to find somebody, don't cleanup the cgroup. */
722 STRV_FOREACH(c
, controllers
)
723 cg_trim(*c
, user_path
, true);
726 const char *runtime_dir
;
728 if ((runtime_dir
= pam_getenv(handle
, "XDG_RUNTIME_DIR")))
729 if ((r
= rm_rf(runtime_dir
, false, true)) < 0)
730 pam_syslog(handle
, LOG_ERR
, "Failed to remove runtime directory: %s", strerror(-r
));
733 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
739 close_nointr_nofail(lock_fd
);
742 free(nosession_path
);
745 strv_free(controllers
);
746 strv_free(kill_exclude_users
);
747 strv_free(kill_only_users
);
749 free(cgroup_user_tree
);