kdm patch
The attached patch makes kdm do all the work of determining the correct SE Linux context. With this patch there is no need for pam_selinux.so for a kdm login.
The reason for this is that currently when kdm creates a ~/.xsession-errors file it uses the default level - IE the low level of the kdm process itself. If the user has a low level that's higher than SystemLow then they won't be able to write to the errors file. My patch makes it call setfscreatecon() before doing that.
Please tell me what you think, both about the patch itself and the concept. If we go ahead with this then I'll probably have to write similar patches for all the other common xdm programs.
Signature · 3 lines
[email protected] http://etbe.coker.com.au/ My Main Blog http://doc.coker.com.au/ My Documents Blog
kdm/backend/client.c +125 -0
@@ -127,6 +127,125 @@# endif #endif +#ifdef WITH_SELINUX +#include <selinux/selinux.h> +#include <selinux/flask.h> +#include <selinux/get_context_list.h> + +/* Wrapper around is_selinux_enabled() to log its return value once only */ +int +kdm_selinux_enabled (void ) +{ +static int enabled =-1 ; + +if (enabled ==-1 ) + enabled =is_selinux_enabled (); + +return (enabled); +} + +/* Return the default security context for the given username */ +static security_context_t +kdm_selinux_getctxbyname (const char *const pwname) +{ +security_context_t sc =NULL , mycon =NULL ; +char *sename =NULL , *lvl =NULL ; +int r; + +if (getseuserbyname (pwname, &sename, &lvl) !=0 ) +return NULL ; + + r =getcon (&mycon); +if (!r) + r =get_default_context_with_level (sename, lvl, mycon, &sc); + +if (r !=0 ) { +if (mycon) +freecon (mycon); + +switch (security_getenforce ()) { +case -1 : +logError ("%s: security_getenforce() failed" , __func__); +exit (1 ); +case 0 : +logError ("%s: Failed to get default SELinux security " +"context for %s" , __func__, pwname); +break ; +default : +logError ("%s: Failed to get default SELinux security " +"context for %s (in enforcing mode)" , + __func__, pwname); +exit (1 ); + } + } + +if (sename !=NULL ) +free (sename); +if (lvl !=NULL ) +free (lvl); + +return (sc); +} + +/* Return the default security context for the given .xsession-errors file */ +void kdm_selinux_set_homefile_ctx (const security_context_t user_ctx,const char *const home) +{ +security_context_t dir_ctx, newcon; +if (getfilecon (home, &dir_ctx) ==-1 ) + { +logError ("%s: getfilecon(\"%s\") failed" , __func__, home); +exit (1 ); + } +if (security_compute_create (user_ctx, dir_ctx,SECCLASS_DIR , &newcon)) + { +logError ("%s: security_compute_create(%s, %s, SECCLASS_DIR) failed" , __func__, user_ctx, dir_ctx); +exit (1 ); + } +freecon (dir_ctx); +if (setfscreatecon (newcon)) + { +logError ("%s: setfscreatecon(\"%s\") failed" , __func__, newcon); +exit (1 ); + } +freecon (newcon); +} + +/* Set the execution context to the default for the specified user */ +void +kdm_selinux_setup_exec_context (const char *const pwname,const char *const home) +{ +security_context_t user_ctx =NULL ; + +if (!kdm_selinux_enabled ()) +return ; + +debug ("%s: setting execution context" , __func__); + + user_ctx =kdm_selinux_getctxbyname (pwname); +if (setexeccon (user_ctx) !=0 ) { +switch (security_getenforce ()) { +case -1 : +logError ("%s: security_getenforce() failed" , __func__); +exit (1 ); +case 0 : +logError ("%s: Failed to set SELinux execution " +"context for %s" , __func__, pwname); +break ; +default : +logError ("%s: Failed to set SELinux execution context " +"for %s (in enforcing mode)" , __func__, pwname); +exit (1 ); + } + } + +if (user_ctx !=NULL ) + { +kdm_selinux_set_homefile_ctx (user_ctx, home); +freecon (user_ctx); + } +} +#endif +static void displayStr (int lv,const char *msg ) {
@@ -1733,6 +1852,9 @@ userEnviron =setEnv ( userEnviron,"HOME" ,"/" ); home =0 ; } +#ifdef WITH_SELINUX +kdm_selinux_setup_exec_context (curuser, home); +#endif if (home || td->clientLogFile[0 ] =='/' ) {if (!createClientLog ( td->clientLogFile )) { logWarn( "Session log file according to %s cannot be created: %m\n",
@@ -1746,6 +1868,9 @@ td->clientLogFallback );/* Could inform the user, but I guess this is only confusing. */ } +#ifdef WITH_SELINUX +setfscreatecon (NULL ); +#endif if (!*dmrcDir)mergeSessionArgs ( home !=0 );if (!(desksess =iniEntry ( curdmrc,"Desktop" ,"Session" ,0 )))
Quoted 10 lines · Russell Coker
The attached patch makes kdm do all the work of determining the correct SE Linux context. With this patch there is no need for pam_selinux.so for a kdm login.
The reason for this is that currently when kdm creates a ~/.xsession-errors file it uses the default level - IE the low level of the kdm process itself. If the user has a low level that's higher than SystemLow then they won't be able to write to the errors file. My patch makes it call
setfscreatecon()before doing that.
Quoted 3 lines
Please tell me what you think, both about the patch itself and the concept. If we go ahead with this then I'll probably have to write similar patches for all the other common xdm programs.
I'd suggest taking the kdm_selinux_* helper functions and turning them into generic libselinux functions that can be reused by all of the *dm programs. That will also allow us to change the internal logic in the future without having to re-patch the *dm programs.
Have you done a detailed comparison of your logic against the latest pam_selinux logic? The pam_selinux logic includes support for user specification or environmental specification of desired role/level. If we are going to directly patch the *dm programs at all, wouldn't it be better to go ahead and support user selection of role/level as was supported in some of the early experimental gdm selinux patches?
Also, it will be important to keep in mind that we have wanted to change the approach to determining user contexts in SELinux for some time (e.g. eliminate the use of /selinux/user altogether, take more of the logic to userspace, simplify the logic), so whatever you do here needs to allow for future replacement without needing to revisit each *dm program.
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.