From b3644707da87d61559f8322771a88d2162a47a4e Mon Sep 17 00:00:00 2001 From: Tomas Mraz Date: Fri, 15 Jun 2007 09:38:11 +0000 Subject: [PATCH] Relevant BUGIDs: Purpose of commit: bugfix, new feature Commit summary: --------------- 2007-06-15 Tomas Mraz * modules/pam_namespace/README.xml: Avoid duplication of documentation. * modules/pam_namespace/namespace.conf: More real life example from MLS support. * modules/pam_namespace/namespace.conf.5.xml: Likewise plus properly describe how instance directory names are formed. * modules/pam_namespace/namespace.init: Preserve euid when called from setuid apps (su, newrole). * modules/pam_namespace/pam_namespace.8.xml: Added option no_unmount_on_close. * modules/pam_namespace/pam_namespace.c (process_line): Polyinst methods are now user, level and context. Fix crash on unknown override user in config file. (ns_override): Add explicit uid parameter. (form_context): Skip for user method. Implement level based polyinstantiation. (poly_name): Initialize contexts. Add level based polyinst, remove 'both' metod. Use raw contexts for instance names, truncate long instance names and add hash. (ns_setup): Hashing moved to poly_name(). (setup_namespace): Handle correctly override users for su (when unmnt_remnt is used). (pam_sm_close_session): Added no_unmount_on_close option. * modules/pam_namespace/pam_namespace.h: Added no_unmount_on_close_option, level method, limit on instance directory name length. --- ChangeLog | 31 +- NEWS | 2 + modules/pam_namespace/README.xml | 155 ++-------- modules/pam_namespace/namespace.conf | 16 +- modules/pam_namespace/namespace.conf.5.xml | 55 ++-- modules/pam_namespace/namespace.init | 2 +- modules/pam_namespace/pam_namespace.8.xml | 19 ++ modules/pam_namespace/pam_namespace.c | 313 +++++++++++++++------ modules/pam_namespace/pam_namespace.h | 7 +- 9 files changed, 356 insertions(+), 244 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2a0a526c..ec0f1269 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,36 @@ +2007-06-15 Tomas Mraz + + * modules/pam_namespace/README.xml: Avoid duplication of + documentation. + * modules/pam_namespace/namespace.conf: More real life example + from MLS support. + * modules/pam_namespace/namespace.conf.5.xml: Likewise plus + properly describe how instance directory names are formed. + * modules/pam_namespace/namespace.init: Preserve euid when + called from setuid apps (su, newrole). + * modules/pam_namespace/pam_namespace.8.xml: Added option + no_unmount_on_close. + * modules/pam_namespace/pam_namespace.c (process_line): Polyinst + methods are now user, level and context. Fix crash on unknown + override user in config file. + (ns_override): Add explicit uid parameter. + (form_context): Skip for user method. Implement level based + polyinstantiation. + (poly_name): Initialize contexts. Add level based polyinst, + remove 'both' metod. Use raw contexts for instance names, + truncate long instance names and add hash. + (ns_setup): Hashing moved to poly_name(). + (setup_namespace): Handle correctly override users for + su (when unmnt_remnt is used). + (pam_sm_close_session): Added no_unmount_on_close option. + * modules/pam_namespace/pam_namespace.h: Added + no_unmount_on_close_option, level method, limit on instance + directory name length. + 2007-05-04 Thorsten Kukuk * xtests/run-xtests.sh: Use SRCDIR to find PAM config files. - * xtests/Makefile.am:Call run-xtests.sh with srcdir as first + * xtests/Makefile.am: Call run-xtests.sh with srcdir as first argument. Based on patch by Bernard Leak . diff --git a/NEWS b/NEWS index 59cc2f9a..955ec0e0 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,8 @@ Release X.XX.X.X * Add translations for ar, ca, da, ru, sv and zu. * Update hungarian translation. * Add support for limits.d directory to pam_limits. +* Improve pam_namespace module tobe more useful + for MLS, fixed crash with bad config files. Release 0.99.7.1 diff --git a/modules/pam_namespace/README.xml b/modules/pam_namespace/README.xml index 98ab7532..4ef99c9f 100644 --- a/modules/pam_namespace/README.xml +++ b/modules/pam_namespace/README.xml @@ -1,139 +1,44 @@ - + +--> + +]> +
+ + <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="pam_namespace.8.xml" xpointer='xpointer(//refnamediv[@id = "pam_namespace-name"]/*)'/> - -
- - -Where: - - is the absolute pathname of the directory to polyinstantiate - Special entry $HOME is supported to designate user's home directory. - This field cannot be blank. - - - is the string prefix used to build the pathname for the - instantiation of . The directory security context, or - optionally its md5sum string (32 hex characters), is appended to - the prefix to generate the final instance directory path. - This directory is created if it did not exist already, and is then - bind mounted on the to provide an instance of - based on the column. The special string $HOME is replaced with - the user's home directory, and $USER with the username. - This field cannot be blank. - - - is the method used for polyinstantiation. It can take 3 different - values; "user" for polyinstantiation based on user name, "context" - for polyinstantiation based on process security context, and "both" - for polyinstantiation based on both user name and security context. - Methods "context" and "both" are only available with SELinux. This - field cannot be blank. - - - is a comma separated list of user names for whom the - polyinstantiation is not performed. If left blank, polyinstantiation - will be performed for all users. - -EXAMPLE /etc/security/namespace.conf configuration file: -======================================================= -# Following three lines will polyinstantiate /tmp, /var/tmp and user's home -# directories. /tmp and /var/tmp will be polyinstantiated based on both -# security context as well as user name, whereas home directory will -# be polyinstantiated based on security context only. Polyinstantiation -# will not be performed for user root and adm for directories /tmp and -# /var/tmp, whereas home directories will be polyinstantiated for all -# users. The user name and/or context is appended to the instance prefix. -# -# Note that instance directories do not have to reside inside the -# polyinstantiated directory. In the examples below, instances of /tmp -# will be created in /tmp-inst directory, where as instances of /var/tmp -# and users home directories will reside within the directories that -# are being polyinstantiated. -# -# Instance parent directories must exist for the polyinstantiation -# mechanism to work. By default, they should be created with the mode -# of 000. pam_namespace module will enforce this mode unless it -# is explicitly called with an argument to ignore the mode of the -# instance parent. System administrators should use this argument with -# caution, as it will reduce security and isolation achieved by -# polyinstantiation. -# -/tmp /tmp-inst/ both root,adm -/var/tmp /var/tmp/tmp-inst/ both root,adm -$HOME $HOME/$USER.inst/inst- context - -ARGUMENTS RECOGNIZED: - debug - Verbose logging by syslog - unmnt_remnt - For programs such as su and newrole, the login session has - already setup a polyinstantiated namespace. For these programs, - polyinstantiation is performed based on new user id or security - context, however the command first needs to undo the - polyinstantiation performed by login. This argument instructs - the command to first undo previous polyinstantiation before - proceeding with new polyinstantiation based on new id/context. - - unmnt_only - For trusted programs that want to undo any existing bind mounts - and process instance directories on their own, this argument - allows them to unmount currently mounted instance directories. - - require_selinux - If selinux is not enabled, return failure. - - gen_hash - Instead of using the security context string for the instance - name, generate and use its md5 hash. - - ignore_config_error - If a line in the configuration file corresponding to a - polyinstantiated directory contains format error, skip that - line process the next line. Without this option, pam will return - an error to the calling program resulting in termination - of the session. - - ignore_instance_parent_mode - Instance parent directories by default are expected to have - the restrictive mode of 000. Using this option, an administrator - can choose to ignore the mode of the instance parent. + -MODULE SERVICES PROVIDED: - session open_session and close_session +
+ +
-USAGE: - For the s you need polyinstantiation (login for example) - put the following line in /etc/pam.d/ as the last line for - session group: +
+ +
- session required pam_namespace.so [arguments] +
+ +
- This module also depends on pam_selinux.so setting the context. -]]> -
+
+
-
+ diff --git a/modules/pam_namespace/namespace.conf b/modules/pam_namespace/namespace.conf index c7305ffe..f973225f 100644 --- a/modules/pam_namespace/namespace.conf +++ b/modules/pam_namespace/namespace.conf @@ -4,12 +4,10 @@ # # Uncommenting the following three lines will polyinstantiate # /tmp, /var/tmp and user's home directories. /tmp and /var/tmp will -# be polyinstantiated based on both security context as well as user -# name, whereas home directory will be polyinstantiated based on -# security context only. Polyinstantion will not be performed for -# user root and adm for directories /tmp and /var/tmp, whereas home -# directories will be polyinstantiated for all users. The user name -# and/or context is appended to the instance prefix. +# be polyinstantiated based on the MLS level part of the security context as well as user +# name, Polyinstantion will not be performed for user root and adm for directories +# /tmp and /var/tmp, whereas home directories will be polyinstantiated for all users. +# The user name and context is appended to the instance prefix. # # Note that instance directories do not have to reside inside the # polyinstantiated directory. In the examples below, instances of /tmp @@ -25,6 +23,6 @@ # caution, as it will reduce security and isolation achieved by # polyinstantiation. # -#/tmp /tmp-inst/ both root,adm -#/var/tmp /var/tmp/tmp-inst/ both root,adm -#$HOME $HOME/$USER.inst/inst- context +#/tmp /tmp-inst/ level root,adm +#/var/tmp /var/tmp/tmp-inst/ level root,adm +#$HOME $HOME/$USER.inst/ level diff --git a/modules/pam_namespace/namespace.conf.5.xml b/modules/pam_namespace/namespace.conf.5.xml index 36a1a085..db48cdcb 100644 --- a/modules/pam_namespace/namespace.conf.5.xml +++ b/modules/pam_namespace/namespace.conf.5.xml @@ -22,7 +22,7 @@ This module allows setup of private namespaces with polyinstantiated directories. Directories can be polyinstantiated based on user name - or, in the case of SELinux, user name, security context or both. If an + or, in the case of SELinux, user name, sensitivity level or complete security context. If an executable script /etc/security/namespace.init exists, it is used to initialize the namespace every time a new instance directory is setup. The script receives the polyinstantiated @@ -56,26 +56,27 @@ The second field, instance_prefix is the string prefix used to build the pathname for the instantiation - of <polydir>. The directory security context, or optionally its - md5sum string (32 hex characters), is appended to the prefix to - generate the final instance directory path. This directory is - created if it did not exist already, and is then bind mounted on the - <polydir> to provide an instance of <polydir> based on the - <method> column. The special string $HOME is replaced with the - user's home directory, and $USER with the username. This field cannot - be blank. The directory where polyinstantiated instances are to be + of <polydir>. Depending on the polyinstantiation + method it is then appended with + "instance differentiation string" to generate the final + instance directory path. This directory is created if it did not exist + already, and is then bind mounted on the <polydir> to provide an + instance of <polydir> based on the <method> column. + The special string $HOME is replaced with the user's home directory, + and $USER with the username. This field cannot be blank. + The directory where polyinstantiated instances are to be created, must exist and must have, by default, the mode of 000. The requirement that the instance parent be of mode 000 can be overridden - with the command line option <ignore_instance_parent_mode> + with the command line option ignore_instance_parent_mode The third field, method, is the method used for polyinstantiation. It can take 3 different values; "user" - for polyinstantiation based on user name, "context" for - polyinstantiation based on process security context, and "both" - for polyinstantiation based on both user name and security context. - Methods "context" and "both" are only available with SELinux. This + for polyinstantiation based on user name, "level" for + polyinstantiation based on process MLS level and user name, and "context" for + polyinstantiation based on process security context and user name + Methods "context" and "level" are only available with SELinux. This field cannot be blank. @@ -86,6 +87,24 @@ for all users. + + In case of context or level polyinstantiation the SELinux context + which is used for polyinstantiation is the context used for executing + a new process as obtained by getexeccon. This context must be set + by the calling application or pam_selinux.so + module. If this context is not set the polyinstatiation will be + based just on user name. + + + + The "instance differentiation string" is <user name> for "user" + method and <user name>_<raw directory context> for "context" + and "level" methods. If the whole string is too long the end of it is + replaced with md5sum of itself. Also when command line option + gen_hash is used the whole string is replaced + with md5sum of itself. + + @@ -98,9 +117,9 @@ # The following three lines will polyinstantiate /tmp, # /var/tmp and user's home directories. /tmp and /var/tmp - # will be polyinstantiated based on both security context + # will be polyinstantiated based on the security level # as well as user name, whereas home directory will be - # polyinstantiated based on security context only. + # polyinstantiated based on the full security context and user name. # Polyinstantiation will not be performed for user root # and adm for directories /tmp and /var/tmp, whereas home # directories will be polyinstantiated for all users. @@ -112,8 +131,8 @@ # will reside within the directories that are being # polyinstantiated. # - /tmp /tmp-inst/ both root,adm - /var/tmp /var/tmp/tmp-inst/ both root,adm + /tmp /tmp-inst/ level root,adm + /var/tmp /var/tmp/tmp-inst/ level root,adm $HOME $HOME/$USER.inst/inst- context diff --git a/modules/pam_namespace/namespace.init b/modules/pam_namespace/namespace.init index 62f8e6e4..0e9be68f 100755 --- a/modules/pam_namespace/namespace.init +++ b/modules/pam_namespace/namespace.init @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -p # This is only a boilerplate for the instance initialization script. # It receives polydir path as $1 and the instance path as $2. # diff --git a/modules/pam_namespace/pam_namespace.8.xml b/modules/pam_namespace/pam_namespace.8.xml index 4c93ecf0..e1b307ae 100644 --- a/modules/pam_namespace/pam_namespace.8.xml +++ b/modules/pam_namespace/pam_namespace.8.xml @@ -43,6 +43,9 @@ ignore_instance_parent_mode + + no_unmount_on_close + @@ -179,6 +182,22 @@ + + + + + + + For certain trusted programs such as newrole, open session + is called from a child process while the parent perfoms + close session and pam end functions. For these commands + use this option to instruct pam_close_session to not + unmount the bind mounted polyinstantiated directory in the + parent. + + + + diff --git a/modules/pam_namespace/pam_namespace.c b/modules/pam_namespace/pam_namespace.c index e4e4a5d8..73d8e591 100644 --- a/modules/pam_namespace/pam_namespace.c +++ b/modules/pam_namespace/pam_namespace.c @@ -244,23 +244,29 @@ static int process_line(char *line, const char *home, } strcpy(poly.dir, dir); strcpy(poly.instance_prefix, instance_prefix); - if (strcmp(method, "user") == 0) - poly.method = USER; + + poly.method = NONE; + if (strcmp(method, "user") == 0) + poly.method = USER; + #ifdef WITH_SELINUX - else if (strcmp(method, "context") == 0) { + if (strcmp(method, "level") == 0) { if (idata->flags & PAMNS_CTXT_BASED_INST) - poly.method = CONTEXT; + poly.method = LEVEL; else poly.method = USER; - } else if (strcmp(method, "both") == 0) { + } + + if (strcmp(method, "context") == 0) { if (idata->flags & PAMNS_CTXT_BASED_INST) - poly.method = BOTH; + poly.method = CONTEXT; else poly.method = USER; } #endif - else { + + if ( poly.method == NONE) { pam_syslog(idata->pamh, LOG_NOTICE, "Illegal method"); goto skipping; } @@ -296,11 +302,14 @@ static int process_line(char *line, const char *home, *tptr = '\0'; pwd = pam_modutil_getpwnam(idata->pamh, ustr); - *uidptr = pwd->pw_uid; - if (i < count - 1) { - ustr = tptr + 1; + if (pwd == NULL) { + pam_syslog(idata->pamh, LOG_ERR, "Unknown user %s in configuration", ustr); + poly.num_uids--; + } else { + *uidptr = pwd->pw_uid; uidptr++; } + ustr = tptr + 1; } } @@ -411,17 +420,18 @@ static int parse_config_file(struct instance_data *idata) * uids for the polyinstantiated directory, polyinstantiation is not * performed for that user for that directory. */ -static int ns_override(struct polydir_s *polyptr, struct instance_data *idata) +static int ns_override(struct polydir_s *polyptr, struct instance_data *idata, + uid_t uid) { unsigned int i; if (idata->flags & PAMNS_DEBUG) pam_syslog(idata->pamh, LOG_DEBUG, "Checking for ns override in dir %s for uid %d", - polyptr->dir, idata->uid); + polyptr->dir, uid); for (i = 0; i < polyptr->num_uids; i++) { - if (idata->uid == polyptr->uid[i]) { + if (uid == polyptr->uid[i]) { return 1; } } @@ -429,6 +439,36 @@ static int ns_override(struct polydir_s *polyptr, struct instance_data *idata) return 0; } +/* + * md5hash generates a hash of the passed in instance directory name. + */ +static char *md5hash(const char *instname, struct instance_data *idata) +{ + int i; + char *md5inst = NULL; + char *to; + unsigned char inst_digest[MD5_DIGEST_LENGTH]; + + /* + * Create MD5 hashes for instance pathname. + */ + + MD5((const unsigned char *)instname, strlen(instname), inst_digest); + + if ((md5inst = malloc(MD5_DIGEST_LENGTH * 2 + 1)) == NULL) { + pam_syslog(idata->pamh, LOG_ERR, "Unable to allocate buffer"); + return NULL; + } + + to = md5inst; + for (i = 0; i < MD5_DIGEST_LENGTH; i++) { + snprintf(to, 3, "%02x", (unsigned int)inst_digest[i]); + to += 2; + } + + return md5inst; +} + #ifdef WITH_SELINUX static int form_context(const struct polydir_s *polyptr, security_context_t *i_context, security_context_t *origcon, @@ -448,19 +488,23 @@ static int form_context(const struct polydir_s *polyptr, return PAM_SESSION_ERR; } + if (polyptr->method == USER) return PAM_SUCCESS; + + rc = getexeccon(&scon); + if (rc < 0 || scon == NULL) { + pam_syslog(idata->pamh, LOG_ERR, + "Error getting exec context, %m"); + return PAM_SESSION_ERR; + } + /* * If polyinstantiating based on security context, get current * process security context, get security class for directories, * and ask the policy to provide security context of the * polyinstantiated instance directory. */ - if ((polyptr->method == CONTEXT) || (polyptr->method == BOTH)) { - rc = getexeccon(&scon); - if (rc < 0 || scon == NULL) { - pam_syslog(idata->pamh, LOG_ERR, - "Error getting exec context, %m"); - return PAM_SESSION_ERR; - } + + if (polyptr->method == CONTEXT) { tclass = string_to_security_class("dir"); if (security_compute_member(scon, *origcon, tclass, @@ -473,7 +517,48 @@ static int form_context(const struct polydir_s *polyptr, pam_syslog(idata->pamh, LOG_DEBUG, "member context returned by policy %s", *i_context); freecon(scon); + return PAM_SUCCESS; } + + /* + * If polyinstantiating based on security level, get current + * process security context, get security class for directories, + * and change the directories MLS Level to match process. + */ + + if (polyptr->method == LEVEL) { + context_t scontext = NULL; + context_t fcontext = NULL; + rc = PAM_SESSION_ERR; + + scontext = context_new(scon); + if (! scontext) { + pam_syslog(idata->pamh, LOG_ERR, "out of memory"); + goto fail; + } + fcontext = context_new(*origcon); + if (! fcontext) { + pam_syslog(idata->pamh, LOG_ERR, "out of memory"); + goto fail; + } + if (context_range_set(fcontext, context_range_get(scontext)) != 0) { + pam_syslog(idata->pamh, LOG_ERR, "Unable to set MLS Componant of context"); + goto fail; + } + *i_context=strdup(context_str(fcontext)); + if (! *i_context) { + pam_syslog(idata->pamh, LOG_ERR, "out of memory"); + goto fail; + } + + rc = PAM_SUCCESS; + fail: + context_free(scontext); + context_free(fcontext); + freecon(scon); + return rc; + } + /* Should never get here */ return PAM_SUCCESS; } #endif @@ -495,12 +580,21 @@ static int poly_name(const struct polydir_s *polyptr, char **i_name, #endif { int rc; + char *hash = NULL; +#ifdef WITH_SELINUX + security_context_t rawcon = NULL; +#endif -# ifdef WITH_SELINUX - rc = form_context(polyptr, i_context, origcon, idata); + *i_name = NULL; +#ifdef WITH_SELINUX + *i_context = NULL; + *origcon = NULL; + if ((rc=form_context(polyptr, i_context, origcon, idata)) != PAM_SUCCESS) { + return rc; + } #endif - rc = PAM_SUCCESS; + rc = PAM_SESSION_ERR; /* * Set the name of the polyinstantiated instance dir based on the * polyinstantiation method. @@ -509,35 +603,70 @@ static int poly_name(const struct polydir_s *polyptr, char **i_name, case USER: if (asprintf(i_name, "%s", idata->user) < 0) { *i_name = NULL; - rc = PAM_SESSION_ERR; - } + goto fail; + } break; #ifdef WITH_SELINUX + case LEVEL: case CONTEXT: - if (asprintf(i_name, "%s", *i_context) < 0) { + if (selinux_trans_to_raw_context(*i_context, &rawcon) < 0) { + pam_syslog(idata->pamh, LOG_ERR, "Error translating directory context"); + goto fail; + } + if (asprintf(i_name, "%s_%s", rawcon, idata->user) < 0) { *i_name = NULL; - rc = PAM_SESSION_ERR; + goto fail; } break; - case BOTH: - if (asprintf(i_name, "%s_%s", *i_context, idata->user) < 0) { - *i_name = NULL; - rc = PAM_SESSION_ERR; - } - break; #endif /* WITH_SELINUX */ default: if (idata->flags & PAMNS_DEBUG) pam_syslog(idata->pamh, LOG_ERR, "Unknown method"); - rc = PAM_SESSION_ERR; + goto fail; } - if ((idata->flags & PAMNS_DEBUG) && rc == PAM_SUCCESS) + if (idata->flags & PAMNS_DEBUG) pam_syslog(idata->pamh, LOG_DEBUG, "poly_name %s", *i_name); + if ((idata->flags & PAMNS_GEN_HASH) || strlen(*i_name) > NAMESPACE_MAX_DIR_LEN) { + hash = md5hash(*i_name, idata); + if (hash == NULL) { + goto fail; + } + if (idata->flags & PAMNS_GEN_HASH) { + free(*i_name); + *i_name = hash; + hash = NULL; + } else { + char *newname; + if (asprintf(&newname, "%.*s_%s", NAMESPACE_MAX_DIR_LEN-1-strlen(hash), + *i_name, hash) < 0) { + goto fail; + } + free(*i_name); + *i_name = newname; + } + } + rc = PAM_SUCCESS; + +fail: + free(hash); +#ifdef WITH_SELINUX + freecon(rawcon); +#endif + if (rc != PAM_SUCCESS) { +#ifdef WITH_SELINUX + freecon(*i_context); + *i_context = NULL; + freecon(*origcon); + *origcon = NULL; +#endif + free(*i_name); + *i_name = NULL; + } return rc; } @@ -784,39 +913,6 @@ inst_init: } -/* - * md5hash generates a hash of the passed in instance directory name. - */ -static int md5hash(char **instname, struct instance_data *idata) -{ - int i; - char *md5inst = NULL; - char *to; - unsigned char inst_digest[MD5_DIGEST_LENGTH]; - - /* - * Create MD5 hashes for instance pathname. - */ - - MD5((unsigned char *)*instname, strlen(*instname), inst_digest); - - if ((md5inst = malloc(MD5_DIGEST_LENGTH * 2 + 1)) == NULL) { - pam_syslog(idata->pamh, LOG_ERR, "Unable to allocate buffer"); - return PAM_SESSION_ERR; - } - - to = md5inst; - for (i = 0; i < MD5_DIGEST_LENGTH; i++) { - snprintf(to, 3, "%02x", (unsigned int)inst_digest[i]); - to += 3; - } - - free(*instname); - *instname = md5inst; - - return PAM_SUCCESS; -} - /* * This function performs the namespace setup for a particular directory * that is being polyinstantiated. It creates an MD5 hash of instance @@ -867,14 +963,6 @@ static int ns_setup(const struct polydir_s *polyptr, #endif } - if (idata->flags & PAMNS_GEN_HASH) { - retval = md5hash(&instname, idata); - if (retval < 0) { - pam_syslog(idata->pamh, LOG_ERR, "Error generating md5 hash"); - goto error_out; - } - } - if (asprintf(&inst_dir, "%s%s", polyptr->instance_prefix, instname) < 0) goto error_out; @@ -967,21 +1055,46 @@ static int setup_namespace(struct instance_data *idata, enum unmnt_op unmnt) int retval = 0, need_poly = 0, changing_dir = 0; char *cptr, *fptr, poly_parent[PATH_MAX]; struct polydir_s *pptr; + uid_t req_uid; + const void *ruser_name; + struct passwd *pwd; if (idata->flags & PAMNS_DEBUG) pam_syslog(idata->pamh, LOG_DEBUG, "Set up namespace for pid %d", getpid()); + retval = pam_get_item(idata->pamh, PAM_RUSER, &ruser_name); + if (ruser_name == NULL || retval != PAM_SUCCESS) { + retval = PAM_SUCCESS; + req_uid = getuid(); + } else { + pwd = pam_modutil_getpwnam(idata->pamh, ruser_name); + if (pwd != NULL) { + req_uid = pwd->pw_uid; + } else { + req_uid = getuid(); + } + } + /* * Cycle through all polyinstantiated directory entries to see if * polyinstantiation is needed at all. */ for (pptr = idata->polydirs_ptr; pptr; pptr = pptr->next) { - if (ns_override(pptr, idata)) { - if (idata->flags & PAMNS_DEBUG) - pam_syslog(idata->pamh, LOG_DEBUG, + if (ns_override(pptr, idata, idata->uid)) { + if (unmnt == NO_UNMNT || ns_override(pptr, idata, req_uid)) { + if (idata->flags & PAMNS_DEBUG) + pam_syslog(idata->pamh, LOG_DEBUG, "Overriding poly for user %d for dir %s", idata->uid, pptr->dir); + } else { + if (idata->flags & PAMNS_DEBUG) + pam_syslog(idata->pamh, LOG_DEBUG, + "Need unmount ns for user %d for dir %s", + idata->uid, pptr->dir); + need_poly = 1; + break; + } continue; } else { if (idata->flags & PAMNS_DEBUG) @@ -1011,15 +1124,20 @@ static int setup_namespace(struct instance_data *idata, enum unmnt_op unmnt) * call ns_setup to setup polyinstantiation for a particular entry. */ for (pptr = idata->polydirs_ptr; pptr; pptr = pptr->next) { - if (ns_override(pptr, idata)) - continue; - else { - if (idata->flags & PAMNS_DEBUG) + enum unmnt_op dir_unmnt = unmnt; + if (ns_override(pptr, idata, idata->uid)) { + if (unmnt == NO_UNMNT || ns_override(pptr, idata, req_uid)) { + continue; + } else { + dir_unmnt = UNMNT_ONLY; + } + } + if (idata->flags & PAMNS_DEBUG) pam_syslog(idata->pamh, LOG_DEBUG, "Setting poly ns for user %d for dir %s", idata->uid, pptr->dir); - if ((unmnt == UNMNT_REMNT) || (unmnt == UNMNT_ONLY)) { + if ((dir_unmnt == UNMNT_REMNT) || (dir_unmnt == UNMNT_ONLY)) { /* * Check to see if process current directory is in the * bind mounted instance_parent directory that we are trying to @@ -1059,13 +1177,12 @@ static int setup_namespace(struct instance_data *idata, enum unmnt_op unmnt) } else if (idata->flags & PAMNS_DEBUG) pam_syslog(idata->pamh, LOG_DEBUG, "Umount succeeded %s", pptr->dir); - } + } - if (unmnt != UNMNT_ONLY) { + if (dir_unmnt != UNMNT_ONLY) { retval = ns_setup(pptr, idata); if (retval != PAM_SUCCESS) break; - } } } @@ -1092,7 +1209,7 @@ static int orig_namespace(struct instance_data *idata) * appropriate polyinstantiated instance directories. */ for (pptr = idata->polydirs_ptr; pptr; pptr = pptr->next) { - if (ns_override(pptr, idata)) + if (ns_override(pptr, idata, idata->uid)) continue; else { if (idata->flags & PAMNS_DEBUG) @@ -1158,7 +1275,7 @@ PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags UNUSED, #ifdef WITH_SELINUX if (is_selinux_enabled()) idata.flags |= PAMNS_SELINUX_ENABLED; - if (ctxt_based_inst_needed()) + if (ctxt_based_inst_needed()) idata.flags |= PAMNS_CTXT_BASED_INST; #endif @@ -1266,12 +1383,30 @@ PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags UNUSED, idata.flags |= PAMNS_DEBUG; if (strcmp(argv[i], "ignore_config_error") == 0) idata.flags |= PAMNS_IGN_CONFIG_ERR; + if (strcmp(argv[i], "no_unmount_on_close") == 0) + idata.flags |= PAMNS_NO_UNMOUNT_ON_CLOSE; } if (idata.flags & PAMNS_DEBUG) pam_syslog(idata.pamh, LOG_DEBUG, "close_session - start"); /* + * For certain trusted programs such as newrole, open session + * is called from a child process while the parent perfoms + * close session and pam end functions. For these commands + * pam_close_session should not perform the unmount of the + * polyinstantiatied directory because it will result in + * undoing of parents polyinstantiatiaion. These commands + * will invoke pam_namespace with the "no_unmount_on_close" + * argument. + */ + if (idata.flags & PAMNS_NO_UNMOUNT_ON_CLOSE) { + if (idata.flags & PAMNS_DEBUG) + pam_syslog(idata.pamh, LOG_DEBUG, "close_session - sucessful"); + return PAM_SUCCESS; + } + + /* * Lookup user and fill struct items */ retval = pam_get_item(idata.pamh, PAM_USER, (void*) &user_name ); diff --git a/modules/pam_namespace/pam_namespace.h b/modules/pam_namespace/pam_namespace.h index c918cff3..0847ec08 100644 --- a/modules/pam_namespace/pam_namespace.h +++ b/modules/pam_namespace/pam_namespace.h @@ -63,6 +63,7 @@ #ifdef WITH_SELINUX #include +#include #endif #ifndef CLONE_NEWNS @@ -86,15 +87,19 @@ #define PAMNS_GEN_HASH 0x00002000 /* Generate md5 hash for inst names */ #define PAMNS_IGN_CONFIG_ERR 0x00004000 /* Ignore format error in conf file */ #define PAMNS_IGN_INST_PARENT_MODE 0x00008000 /* Ignore instance parent mode */ +#define PAMNS_NO_UNMOUNT_ON_CLOSE 0x00010000 /* no unmount at session close */ + +#define NAMESPACE_MAX_DIR_LEN 80 /* * Polyinstantiation method options, based on user, security context * or both */ enum polymethod { + NONE, USER, CONTEXT, - BOTH, + LEVEL, }; /* -- 2.40.0