2 * pam_limits - impose resource limits when opening a user session
4 * 1.6 - modified for PLD (added process priority settings)
5 * by Marcin Korzonek <mkorz@shadow.eu.org>
6 * 1.5 - Elliot Lee's "max system logins patch"
7 * 1.4 - addressed bug in configuration file parser
8 * 1.3 - modified the configuration file format
9 * 1.2 - added 'debug' and 'conf=' arguments
10 * 1.1 - added @group support
11 * 1.0 - initial release - Linux ONLY
13 * See end for Copyright information
16 #if !defined(linux) && !defined(__linux)
17 #warning THIS CODE IS KNOWN TO WORK ONLY ON LINUX !!!
30 #include <sys/types.h>
32 #include <sys/resource.h>
36 #ifndef UT_USER /* some systems have ut_name instead of ut_user */
37 #define UT_USER ut_user
49 #define LINE_LENGTH 1024
51 #define LIMITS_DEF_USER 0 /* limit was set by an user entry */
52 #define LIMITS_DEF_GROUP 1 /* limit was set by a group entry */
53 #define LIMITS_DEF_ALLGROUP 2 /* limit was set by a group entry */
54 #define LIMITS_DEF_ALL 3 /* limit was set by an default entry */
55 #define LIMITS_DEF_DEFAULT 4 /* limit was set by an default entry */
56 #define LIMITS_DEF_NONE 5 /* this limit was not set yet */
58 static const char *limits_def_names[] = {
68 struct user_limits_struct {
77 int login_limit; /* the max logins limit */
78 int login_limit_def; /* which entry set the login limit */
79 int flag_numsyslogins; /* whether to limit logins only for a
80 specific user or to count all logins */
81 int priority; /* the priority to run user process with */
82 struct user_limits_struct limits[RLIM_NLIMITS];
83 const char *conf_file;
84 int utmp_after_pam_call;
85 char login_group[LINE_LENGTH];
88 #define LIMIT_LOGIN RLIM_NLIMITS+1
89 #define LIMIT_NUMSYSLOGINS RLIM_NLIMITS+2
91 #define LIMIT_PRI RLIM_NLIMITS+3
96 #define PAM_SM_SESSION
98 #include <security/pam_modules.h>
99 #include <security/_pam_macros.h>
100 #include <security/pam_modutil.h>
101 #include <security/pam_ext.h>
103 /* argument parsing */
105 #define PAM_DEBUG_ARG 0x0001
106 #define PAM_UTMP_EARLY 0x0004
107 #define PAM_NO_AUDIT 0x0008
109 /* Limits from globbed files. */
110 #define LIMITS_CONF_GLOB LIMITS_FILE_DIR
112 #define CONF_FILE (pl->conf_file != NULL)?pl->conf_file:LIMITS_FILE
115 _pam_parse (const pam_handle_t *pamh, int argc, const char **argv,
116 struct pam_limit_s *pl)
120 /* step through arguments */
121 for (ctrl=0; argc-- > 0; ++argv) {
123 /* generic options */
125 if (!strcmp(*argv,"debug")) {
126 ctrl |= PAM_DEBUG_ARG;
127 } else if (!strncmp(*argv,"conf=",5)) {
128 pl->conf_file = *argv+5;
129 } else if (!strcmp(*argv,"utmp_early")) {
130 ctrl |= PAM_UTMP_EARLY;
131 } else if (!strcmp(*argv,"noaudit")) {
132 ctrl |= PAM_NO_AUDIT;
134 pam_syslog(pamh, LOG_ERR, "unknown option: %s", *argv);
182 #ifdef RLIMIT_SIGPENDING
183 case RLIMIT_SIGPENDING:
187 #ifdef RLIMIT_MSGQUEUE
188 case RLIMIT_MSGQUEUE:
209 #define LIMITED_OK 0 /* limit setting appeared to work */
210 #define LIMIT_ERR 1 /* error setting a limit */
211 #define LOGIN_ERR 2 /* too many logins err */
213 /* Counts the number of user logins and check against the limit*/
215 check_logins (pam_handle_t *pamh, const char *name, int limit, int ctrl,
216 struct pam_limit_s *pl)
221 if (ctrl & PAM_DEBUG_ARG) {
222 pam_syslog(pamh, LOG_DEBUG,
223 "checking logins for '%s' (maximum of %d)", name, limit);
227 return 0; /* no limits imposed */
228 if (limit == 0) /* maximum 0 logins ? */ {
229 pam_syslog(pamh, LOG_WARNING, "No logins allowed for '%s'", name);
235 /* Because there is no definition about when an application
236 actually adds a utmp entry, some applications bizarrely do the
237 utmp call before the have PAM authenticate them to the system:
238 you're logged it, sort of...? Anyway, you can use the
239 "utmp_early" module argument in your PAM config file to make
240 allowances for this sort of problem. (There should be a PAM
241 standard for this, since if a module wants to actually map a
242 username then any early utmp entry will be for the unmapped
245 if (ctrl & PAM_UTMP_EARLY) {
251 while((ut = getutent())) {
253 if (ut->ut_type != USER_PROCESS) {
257 if (ut->UT_USER[0] == '\0') {
260 if (!pl->flag_numsyslogins) {
261 if (((pl->login_limit_def == LIMITS_DEF_USER)
262 || (pl->login_limit_def == LIMITS_DEF_GROUP)
263 || (pl->login_limit_def == LIMITS_DEF_DEFAULT))
264 && strncmp(name, ut->UT_USER, sizeof(ut->UT_USER)) != 0) {
267 if ((pl->login_limit_def == LIMITS_DEF_ALLGROUP)
268 && !pam_modutil_user_in_group_nam_nam(pamh, ut->UT_USER, pl->login_group)) {
272 if (++count > limit) {
279 pam_syslog(pamh, LOG_WARNING,
280 "Too many logins (max %d) for %s", limit, name);
282 pam_syslog(pamh, LOG_WARNING, "Too many system logins (max %d)", limit);
289 static int init_limits(struct pam_limit_s *pl)
292 int retval = PAM_SUCCESS;
296 for(i = 0; i < RLIM_NLIMITS; i++) {
297 int r = getrlimit(i, &pl->limits[i].limit);
299 pl->limits[i].supported = 0;
300 if (errno != EINVAL) {
301 retval = !PAM_SUCCESS;
304 pl->limits[i].supported = 1;
305 pl->limits[i].src_soft = LIMITS_DEF_NONE;
306 pl->limits[i].src_hard = LIMITS_DEF_NONE;
311 pl->priority = getpriority (PRIO_PROCESS, 0);
312 if (pl->priority == -1 && errno != 0)
313 retval = !PAM_SUCCESS;
314 pl->login_limit = -2;
315 pl->login_limit_def = LIMITS_DEF_NONE;
321 process_limit (const pam_handle_t *pamh, int source, const char *lim_type,
322 const char *lim_item, const char *lim_value,
323 int ctrl, struct pam_limit_s *pl)
328 rlim_t rlimit_value = 0;
330 const char *value_orig = lim_value;
332 if (ctrl & PAM_DEBUG_ARG)
333 pam_syslog(pamh, LOG_DEBUG, "%s: processing %s %s %s for %s",
334 __FUNCTION__, lim_type, lim_item, lim_value,
335 limits_def_names[source]);
337 if (strcmp(lim_item, "cpu") == 0)
338 limit_item = RLIMIT_CPU;
339 else if (strcmp(lim_item, "fsize") == 0)
340 limit_item = RLIMIT_FSIZE;
341 else if (strcmp(lim_item, "data") == 0)
342 limit_item = RLIMIT_DATA;
343 else if (strcmp(lim_item, "stack") == 0)
344 limit_item = RLIMIT_STACK;
345 else if (strcmp(lim_item, "core") == 0)
346 limit_item = RLIMIT_CORE;
347 else if (strcmp(lim_item, "rss") == 0)
348 limit_item = RLIMIT_RSS;
349 else if (strcmp(lim_item, "nproc") == 0)
350 limit_item = RLIMIT_NPROC;
351 else if (strcmp(lim_item, "nofile") == 0)
352 limit_item = RLIMIT_NOFILE;
353 else if (strcmp(lim_item, "memlock") == 0)
354 limit_item = RLIMIT_MEMLOCK;
356 else if (strcmp(lim_item, "as") == 0)
357 limit_item = RLIMIT_AS;
360 else if (strcmp(lim_item, "locks") == 0)
361 limit_item = RLIMIT_LOCKS;
363 #ifdef RLIMIT_SIGPENDING
364 else if (strcmp(lim_item, "sigpending") == 0)
365 limit_item = RLIMIT_SIGPENDING;
367 #ifdef RLIMIT_MSGQUEUE
368 else if (strcmp(lim_item, "msgqueue") == 0)
369 limit_item = RLIMIT_MSGQUEUE;
372 else if (strcmp(lim_item, "nice") == 0)
373 limit_item = RLIMIT_NICE;
376 else if (strcmp(lim_item, "rtprio") == 0)
377 limit_item = RLIMIT_RTPRIO;
379 else if (strcmp(lim_item, "maxlogins") == 0) {
380 limit_item = LIMIT_LOGIN;
381 pl->flag_numsyslogins = 0;
382 } else if (strcmp(lim_item, "maxsyslogins") == 0) {
383 limit_item = LIMIT_NUMSYSLOGINS;
384 pl->flag_numsyslogins = 1;
385 } else if (strcmp(lim_item, "priority") == 0) {
386 limit_item = LIMIT_PRI;
388 pam_syslog(pamh, LOG_DEBUG, "unknown limit item '%s'", lim_item);
392 if (strcmp(lim_type,"soft")==0)
393 limit_type=LIMIT_SOFT;
394 else if (strcmp(lim_type, "hard")==0)
395 limit_type=LIMIT_HARD;
396 else if (strcmp(lim_type,"-")==0)
397 limit_type=LIMIT_SOFT | LIMIT_HARD;
398 else if (limit_item != LIMIT_LOGIN && limit_item != LIMIT_NUMSYSLOGINS) {
399 pam_syslog(pamh, LOG_DEBUG, "unknown limit type '%s'", lim_type);
402 if (limit_item != LIMIT_PRI
404 && limit_item != RLIMIT_NICE
406 && (strcmp(lim_value, "-1") == 0
407 || strcmp(lim_value, "-") == 0 || strcmp(lim_value, "unlimited") == 0
408 || strcmp(lim_value, "infinity") == 0)) {
410 rlimit_value = RLIM_INFINITY;
411 } else if (limit_item == LIMIT_PRI || limit_item == LIMIT_LOGIN ||
413 limit_item == RLIMIT_NICE ||
415 limit_item == LIMIT_NUMSYSLOGINS) {
417 temp = strtol (lim_value, &endptr, 10);
418 temp = temp < INT_MAX ? temp : INT_MAX;
419 int_value = temp > INT_MIN ? temp : INT_MIN;
420 if (int_value == 0 && value_orig == endptr) {
421 pam_syslog(pamh, LOG_DEBUG,
422 "wrong limit value '%s' for limit type '%s'",
423 lim_value, lim_type);
427 #ifdef __USE_FILE_OFFSET64
428 rlimit_value = strtoull (lim_value, &endptr, 10);
430 rlimit_value = strtoul (lim_value, &endptr, 10);
432 if (rlimit_value == 0 && value_orig == endptr) {
433 pam_syslog(pamh, LOG_DEBUG,
434 "wrong limit value '%s' for limit type '%s'",
435 lim_value, lim_type);
440 /* one more special case when limiting logins */
441 if ((source == LIMITS_DEF_ALL || source == LIMITS_DEF_ALLGROUP)
442 && (limit_item != LIMIT_LOGIN)) {
443 if (ctrl & PAM_DEBUG_ARG)
444 pam_syslog(pamh, LOG_DEBUG,
445 "'%%' domain valid for maxlogins type only");
451 if (rlimit_value != RLIM_INFINITY)
453 if (rlimit_value >= RLIM_INFINITY/60)
454 rlimit_value = RLIM_INFINITY;
468 if (rlimit_value != RLIM_INFINITY)
470 if (rlimit_value >= RLIM_INFINITY/1024)
471 rlimit_value = RLIM_INFINITY;
473 rlimit_value *= 1024;
482 rlimit_value = 20 - int_value;
487 if ( (limit_item != LIMIT_LOGIN)
488 && (limit_item != LIMIT_NUMSYSLOGINS)
489 && (limit_item != LIMIT_PRI) ) {
490 if (limit_type & LIMIT_SOFT) {
491 if (pl->limits[limit_item].src_soft < source) {
494 pl->limits[limit_item].limit.rlim_cur = rlimit_value;
495 pl->limits[limit_item].src_soft = source;
498 if (limit_type & LIMIT_HARD) {
499 if (pl->limits[limit_item].src_hard < source) {
502 pl->limits[limit_item].limit.rlim_max = rlimit_value;
503 pl->limits[limit_item].src_hard = source;
507 /* recent kernels support negative priority limits (=raise priority) */
509 if (limit_item == LIMIT_PRI) {
510 pl->priority = int_value;
512 if (pl->login_limit_def < source) {
515 pl->login_limit = int_value;
516 pl->login_limit_def = source;
523 static int parse_config_file(pam_handle_t *pamh, const char *uname, int ctrl,
524 struct pam_limit_s *pl)
527 char buf[LINE_LENGTH];
529 /* check for the LIMITS_FILE */
530 if (ctrl & PAM_DEBUG_ARG)
531 pam_syslog(pamh, LOG_DEBUG, "reading settings from '%s'", CONF_FILE);
532 fil = fopen(CONF_FILE, "r");
534 pam_syslog (pamh, LOG_WARNING,
535 "cannot read settings from %s: %m", CONF_FILE);
536 return PAM_SERVICE_ERR;
540 while (fgets(buf, LINE_LENGTH, fil) != NULL) {
541 char domain[LINE_LENGTH];
542 char ltype[LINE_LENGTH];
543 char item[LINE_LENGTH];
544 char value[LINE_LENGTH];
550 /* skip the leading white space */
551 while (*line && isspace(*line))
554 /* Rip off the comments */
555 tptr = strchr(line,'#');
558 /* Rip off the newline char */
559 tptr = strchr(line,'\n');
562 /* Anything left ? */
566 domain[0] = ltype[0] = item[0] = value[0] = '\0';
568 i = sscanf(line,"%s%s%s%s", domain, ltype, item, value);
569 D(("scanned line[%d]: domain[%s], ltype[%s], item[%s], value[%s]",
570 i, domain, ltype, item, value));
572 for(j=0; j < strlen(ltype); j++)
573 ltype[j]=tolower(ltype[j]);
575 if (i == 4) { /* a complete line */
576 for(j=0; j < strlen(item); j++)
577 item[j]=tolower(item[j]);
578 for(j=0; j < strlen(value); j++)
579 value[j]=tolower(value[j]);
581 if (strcmp(uname, domain) == 0) /* this user have a limit */
582 process_limit(pamh, LIMITS_DEF_USER, ltype, item, value, ctrl, pl);
583 else if (domain[0]=='@') {
584 if (ctrl & PAM_DEBUG_ARG) {
585 pam_syslog(pamh, LOG_DEBUG,
586 "checking if %s is in group %s",
589 if (pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1))
590 process_limit(pamh, LIMITS_DEF_GROUP, ltype, item, value, ctrl,
592 } else if (domain[0]=='%') {
593 if (ctrl & PAM_DEBUG_ARG) {
594 pam_syslog(pamh, LOG_DEBUG,
595 "checking if %s is in group %s",
598 if (strcmp(domain,"%") == 0)
599 process_limit(pamh, LIMITS_DEF_ALL, ltype, item, value, ctrl,
601 else if (pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
602 strcpy(pl->login_group, domain+1);
603 process_limit(pamh, LIMITS_DEF_ALLGROUP, ltype, item, value, ctrl,
606 } else if (strcmp(domain, "*") == 0)
607 process_limit(pamh, LIMITS_DEF_DEFAULT, ltype, item, value, ctrl,
609 } else if (i == 2 && ltype[0] == '-') { /* Probably a no-limit line */
610 if (strcmp(uname, domain) == 0) {
611 if (ctrl & PAM_DEBUG_ARG) {
612 pam_syslog(pamh, LOG_DEBUG, "no limits for '%s'", uname);
616 } else if (domain[0] == '@' && pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
617 if (ctrl & PAM_DEBUG_ARG) {
618 pam_syslog(pamh, LOG_DEBUG,
619 "no limits for '%s' in group '%s'",
626 pam_syslog(pamh, LOG_WARNING, "invalid line '%s' - skipped", line);
633 static int setup_limits(pam_handle_t *pamh,
634 const char *uname, uid_t uid, int ctrl,
635 struct pam_limit_s *pl)
639 int retval = LIMITED_OK;
641 for (i=0, status=LIMITED_OK; i<RLIM_NLIMITS; i++) {
644 if (!pl->limits[i].supported) {
645 /* skip it if its not known to the system */
648 if (pl->limits[i].src_soft == LIMITS_DEF_NONE &&
649 pl->limits[i].src_hard == LIMITS_DEF_NONE) {
650 /* skip it if its not initialized */
653 if (pl->limits[i].limit.rlim_cur > pl->limits[i].limit.rlim_max)
654 pl->limits[i].limit.rlim_cur = pl->limits[i].limit.rlim_max;
655 res = setrlimit(i, &pl->limits[i].limit);
657 pam_syslog(pamh, LOG_ERR, "Could not set limit for '%s': %m",
666 status = setpriority(PRIO_PROCESS, 0, pl->priority);
668 pam_syslog(pamh, LOG_ERR, "Could not set limit for PRIO_PROCESS: %m");
673 D(("skip login limit check for uid=0"));
674 } else if (pl->login_limit > 0) {
675 if (check_logins(pamh, uname, pl->login_limit, ctrl, pl) == LOGIN_ERR) {
677 if (!(ctrl & PAM_NO_AUDIT)) {
678 pam_modutil_audit_write(pamh, AUDIT_ANOM_LOGIN_SESSIONS,
679 "pam_limits", PAM_PERM_DENIED);
680 /* ignore return value as we fail anyway */
685 } else if (pl->login_limit == 0) {
692 /* now the session stuff */
694 pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
695 int argc, const char **argv)
703 struct pam_limit_s plstruct;
704 struct pam_limit_s *pl = &plstruct;
706 const char *oldlocale;
710 memset(pl, 0, sizeof(*pl));
711 memset(&globbuf, 0, sizeof(globbuf));
713 ctrl = _pam_parse(pamh, argc, argv, pl);
714 retval = pam_get_item( pamh, PAM_USER, (void*) &user_name );
715 if ( user_name == NULL || retval != PAM_SUCCESS ) {
716 pam_syslog(pamh, LOG_CRIT, "open_session - error recovering username");
717 return PAM_SESSION_ERR;
720 pwd = pam_modutil_getpwnam(pamh, user_name);
722 if (ctrl & PAM_DEBUG_ARG)
723 pam_syslog(pamh, LOG_WARNING,
724 "open_session username '%s' does not exist", user_name);
725 return PAM_USER_UNKNOWN;
728 retval = init_limits(pl);
729 if (retval != PAM_SUCCESS) {
730 pam_syslog(pamh, LOG_WARNING, "cannot initialize");
734 retval = parse_config_file(pamh, pwd->pw_name, ctrl, pl);
735 if (retval == PAM_IGNORE) {
736 D(("the configuration file ('%s') has an applicable '<domain> -' entry", CONF_FILE));
739 if (retval != PAM_SUCCESS || pl->conf_file != NULL)
740 /* skip reading limits.d if config file explicitely specified */
743 /* Read subsequent *.conf files, if they exist. */
745 /* set the LC_COLLATE so the sorting order doesn't depend
748 oldlocale = setlocale(LC_COLLATE, "C");
749 glob_rc = glob(LIMITS_CONF_GLOB, GLOB_ERR, NULL, &globbuf);
751 if (oldlocale != NULL)
752 setlocale (LC_COLLATE, oldlocale);
755 /* Parse the *.conf files. */
756 for (i = 0; globbuf.gl_pathv[i] != NULL; i++) {
757 pl->conf_file = globbuf.gl_pathv[i];
758 retval = parse_config_file(pamh, pwd->pw_name, ctrl, pl);
759 if (retval == PAM_IGNORE) {
760 D(("the configuration file ('%s') has an applicable '<domain> -' entry", pl->conf_file));
764 if (retval != PAM_SUCCESS)
771 if (retval != PAM_SUCCESS)
773 pam_syslog(pamh, LOG_WARNING, "error parsing the configuration file: '%s' ",CONF_FILE);
777 retval = setup_limits(pamh, pwd->pw_name, pwd->pw_uid, ctrl, pl);
778 if (retval & LOGIN_ERR)
779 pam_error(pamh, _("Too many logins for '%s'."), pwd->pw_name);
780 if (retval != LIMITED_OK) {
781 return PAM_PERM_DENIED;
788 pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
789 int argc UNUSED, const char **argv UNUSED)
797 /* static module data */
799 struct pam_module _pam_limits_modstruct = {
805 pam_sm_close_session,
811 * Copyright (c) Cristian Gafton, 1996-1997, <gafton@redhat.com>
812 * All rights reserved.
814 * Redistribution and use in source and binary forms, with or without
815 * modification, are permitted provided that the following conditions
817 * 1. Redistributions of source code must retain the above copyright
818 * notice, and the entire permission notice in its entirety,
819 * including the disclaimer of warranties.
820 * 2. Redistributions in binary form must reproduce the above copyright
821 * notice, this list of conditions and the following disclaimer in the
822 * documentation and/or other materials provided with the distribution.
823 * 3. The name of the author may not be used to endorse or promote
824 * products derived from this software without specific prior
825 * written permission.
827 * ALTERNATIVELY, this product may be distributed under the terms of
828 * the GNU Public License, in which case the provisions of the GPL are
829 * required INSTEAD OF the above restrictions. (This clause is
830 * necessary due to a potential bad interaction between the GPL and
831 * the restrictions contained in a BSD-style copyright.)
833 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
834 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
835 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
836 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
837 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
838 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
839 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
840 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
841 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
842 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
843 * OF THE POSSIBILITY OF SUCH DAMAGE.