Fix newrole to not drop capabilities from the bounding set.
PATCH 1/3 Fix newrole to not drop capabilities from the bounding set.
From: Dan Walsh <[email protected]>
Stop dropping capabilities from its children.
Add better error messages.
--- policycoreutils/newrole/newrole.c | 60 +++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 21 deletions(-)
policycoreutils/newrole/newrole.c +39 -21
@@ -546,9 +546,7 @@ static int drop_capabilities(int full)if (!uid)return 0 ;capng_setpid (getpid ()); -capng_clear (CAPNG_SELECT_BOTH ); -if (capng_lock () <0 ) -return -1 ; +capng_clear (CAPNG_SELECT_CAPS );/* Change uid */ if (setresuid (uid, uid, uid)) {
@@ -557,7 +555,7 @@ static int drop_capabilities(int full) }if (! full)capng_update (CAPNG_ADD ,CAPNG_EFFECTIVE |CAPNG_PERMITTED ,CAP_AUDIT_WRITE ); -return capng_apply (CAPNG_SELECT_BOTH ); +return capng_apply (CAPNG_SELECT_CAPS ); }#elif defined(NAMESPACE_PRIV)/**
@@ -575,20 +573,21 @@ static int drop_capabilities(int full)*/ static int drop_capabilities (int full) { +uid_t uid =getuid (); +if (!uid)return 0 ; +capng_setpid (getpid ()); -capng_clear (CAPNG_SELECT_BOTH ); -if (capng_lock () <0 ) -return -1 ; +capng_clear (CAPNG_SELECT_CAPS ); -uid_t uid =getuid ();/* Change uid */ if (setresuid (uid, uid, uid)) {fprintf (stderr,_ ("Error changing uid, aborting.\n" ));return -1 ; }if (! full) -capng_updatev (CAPNG_ADD ,CAPNG_EFFECTIVE |CAPNG_PERMITTED ,CAP_SYS_ADMIN ,CAP_FOWNER ,CAP_CHOWN ,CAP_DAC_OVERRIDE ,CAP_SETPCAP ,-1 ); -return capng_apply (CAPNG_SELECT_BOTH ); +capng_updatev (CAPNG_ADD ,CAPNG_EFFECTIVE |CAPNG_PERMITTED ,CAP_SYS_ADMIN ,CAP_FOWNER ,CAP_CHOWN ,CAP_DAC_OVERRIDE ,CAP_AUDIT_WRITE ,-1 ); + +return capng_apply (CAPNG_SELECT_CAPS ); }#else
@@ -679,7 +678,7 @@ static int relabel_tty(const char *ttyn, security_context_t new_context, security_context_t * tty_context, security_context_t * new_tty_context) { -int fd; +int fd, rc;int enforcing =security_getenforce ();security_context_t tty_con =NULL ;security_context_t new_tty_con =NULL ;
@@ -698,7 +697,13 @@ static int relabel_tty(const char *ttyn, security_context_t new_context,fprintf (stderr,_ ("Error! Could not open %s.\n" ), ttyn);return fd; } -fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +/* this craziness is to make sure we cann't block on open and deadlock */ + rc =fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +if (rc) { +fprintf (stderr,_ ("Error! Could not clear O_NONBLOCK on %s\n" ), ttyn); +close (fd); +return rc; + }if (fgetfilecon (fd, &tty_con) <0 ) { fprintf(stderr, _("%s! Could not get current context "
@@ -1009,9 +1014,9 @@ int main(int argc, char *argv[])int fd;pid_t childPid =0 ;char *shell_argv0 =NULL ; +int rc;#ifdef USE_PAM -int rc;int pam_status;/* pam return code */ pam_handle_t *pam_handle;/* opaque handle used by all PAM functions */
@@ -1222,18 +1227,26 @@ int main(int argc, char *argv[])fprintf (stderr,_ ("Could not close descriptors.\n" )); goto err_close_pam; } - fd =open (ttyn,O_RDWR |O_NONBLOCK ); + fd =open (ttyn,O_RDONLY |O_NONBLOCK );if (fd !=0 ) goto err_close_pam; -fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); + rc =fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +if (rc) + goto err_close_pam; + fd =open (ttyn,O_RDWR |O_NONBLOCK );if (fd !=1 ) goto err_close_pam; -fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); + rc =fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +if (rc) + goto err_close_pam; + fd =open (ttyn,O_RDWR |O_NONBLOCK );if (fd !=2 ) goto err_close_pam; -fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); + rc =fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +if (rc) + goto err_close_pam; }/*
@@ -1267,19 +1280,24 @@ int main(int argc, char *argv[]) }#endif -if (send_audit_message (1 , old_context, new_context, ttyn)) +if (send_audit_message (1 , old_context, new_context, ttyn)) { +fprintf (stderr,_ ("Failed to send audit message" )); goto err_close_pam_session; + }freecon (old_context); old_context=NULL ;freecon (new_context); new_context=NULL ;#ifdef NAMESPACE_PRIV -if (transition_to_caller_uid ()) +if (transition_to_caller_uid ()) { +fprintf (stderr,_ ("Failed to transition to namespace\n" )); goto err_close_pam_session; + }#endif -if (drop_capabilities (TRUE)) +if (drop_capabilities (TRUE)) { +fprintf (stderr,_ ("Failed to drop capabilities %m\n" )); goto err_close_pam_session; - + }/* Handle environment changes */ if (restore_environment (preserve_environment, old_environ, &pw)) { fprintf(stderr, _("Unable to restore the environment, "
Signature · 1 line
2.1.0
PATCH 2/3 policycoreutils/newrole: Set keepcaps around setresuid calls.
Set the "keep capabilities" flag around the setresuid() calls in drop_capabilities() so that we do not simultaneously drop all capabilities (when newrole is setuid).
--- policycoreutils/newrole/newrole.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
policycoreutils/newrole/newrole.c +22 -0
@@ -548,11 +548,22 @@ static int drop_capabilities(int full)capng_setpid (getpid ());capng_clear (CAPNG_SELECT_CAPS ); +if (prctl (PR_SET_KEEPCAPS ,1 ,0 ,0 ,0 ) <0 ) { +fprintf (stderr,_ ("Error resetting KEEPCAPS, aborting\n" )); +return -1 ; + } +/* Change uid */ if (setresuid (uid, uid, uid)) {fprintf (stderr,_ ("Error changing uid, aborting.\n" ));return -1 ; } + +if (prctl (PR_SET_KEEPCAPS ,0 ,0 ,0 ,0 ) <0 ) { +fprintf (stderr,_ ("Error resetting KEEPCAPS, aborting\n" )); +return -1 ; + } +if (! full)capng_update (CAPNG_ADD ,CAPNG_EFFECTIVE |CAPNG_PERMITTED ,CAP_AUDIT_WRITE );return capng_apply (CAPNG_SELECT_CAPS );
@@ -579,11 +590,22 @@ static int drop_capabilities(int full)capng_setpid (getpid ());capng_clear (CAPNG_SELECT_CAPS ); +if (prctl (PR_SET_KEEPCAPS ,1 ,0 ,0 ,0 ) <0 ) { +fprintf (stderr,_ ("Error resetting KEEPCAPS, aborting\n" )); +return -1 ; + } +/* Change uid */ if (setresuid (uid, uid, uid)) {fprintf (stderr,_ ("Error changing uid, aborting.\n" ));return -1 ; } + +if (prctl (PR_SET_KEEPCAPS ,0 ,0 ,0 ,0 ) <0 ) { +fprintf (stderr,_ ("Error resetting KEEPCAPS, aborting\n" )); +return -1 ; + } +if (! full)capng_updatev (CAPNG_ADD ,CAPNG_EFFECTIVE |CAPNG_PERMITTED ,CAP_SYS_ADMIN ,CAP_FOWNER ,CAP_CHOWN ,CAP_DAC_OVERRIDE ,CAP_AUDIT_WRITE ,-1 );
Signature · 1 line
2.1.0
PATCH 3/3 Open stdin as read/write
In reply to Stephen SmalleyFrom: Sven Vermeulen <[email protected]>
As per the discussion on the selinux development mailinglist, the tmux application expects the stdin to be writeable. Although perhaps not the most proper way, having newrole opening the descriptor in read/write keeps the behaviour in line with what applications expect.
See also http://marc.info/?l=selinux&m=136518126930710&w=2
--- policycoreutils/newrole/newrole.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
policycoreutils/newrole/newrole.c +1 -1
@@ -1249,7 +1249,7 @@ int main(int argc, char *argv[])fprintf (stderr,_ ("Could not close descriptors.\n" )); goto err_close_pam; } - fd =open (ttyn,O_RDONLY |O_NONBLOCK ); + fd =open (ttyn,O_RDWR |O_NONBLOCK );if (fd !=0 ) goto err_close_pam; rc =fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK );
Signature · 1 line
2.1.0