Skip to contentSkip to conversation

newrole cleanup patch

PATCH Steve G 2 participants 6 messages Dec 20, 2005 – Dec 21, 2005 SELinux security module

lore
Share & tools
lore

Steve G

author
#0 lore raw

Hi,

I've been reviewing newrole in preparation for another patch to add auditing to it. The patch is fairly self-explanatory. Some highlights:

- little const cleanup
- pass tty to pam
- add a function to verify the shell is infact known to /etc/shells
- make sure no signals are blocked
- If in enforcing mode and error: getting tty context, computing new context, or setting new context then exit with -1.
- Loop the wait call to handle EINTR
- open the tty read only or write only depending on stdin, stdout, stderr.

The patch is attached.

-Steve

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

newrole/newrole.c +111 -26
diff -urp policycoreutils-1.29.2.orig/newrole/newrole.c policycoreutils-1.29.2/newrole/newrole.c
@@ -93,7 +93,8 @@ char *xstrdup(const char *s)
 static char *
 build_new_range(char *newlevel, const char *range)
 {
-  char *newrangep = NULL, *tmpptr;
+  char *newrangep = NULL;
+  const char *tmpptr;
   size_t len;
 
   /* a missing or empty string */
@@ -147,7 +148,7 @@ build_new_range(char *newlevel, const ch
 
 #define SERVICE_NAME "newrole"    /* the name of this program for PAM */
 
-int authenticate_via_pam( const struct passwd * );
+int authenticate_via_pam( const struct passwd *, const char * );
 
 /* authenticate_via_pam()
  *
@@ -165,10 +166,11 @@ int authenticate_via_pam( const struct p
  *
  */
 
-int authenticate_via_pam( const struct passwd *pw ) {
+int authenticate_via_pam( const struct passwd *pw, const char *ttyn ) {
 
   int result = 0;    /* our result, set to 0 (not authenticated) by default */
   pam_handle_t *pam_handle;      /* opaque handle used by all PAM functions */
+  const char *tty_name;
 
   /* This is a jump table of functions for PAM to use when it wants to *
    * communicate with the user.  We'll be using misc_conv(), which is  *
@@ -188,6 +191,16 @@ int authenticate_via_pam( const struct p
     exit( -1 );
   }
 
+  if( strncmp(ttyn, "/dev/", 5) == 0 )
+    tty_name = ttyn+5;
+  else
+    tty_name = ttyn;
+
+  if( PAM_SUCCESS != pam_set_item( pam_handle, PAM_TTY, tty_name ) ) {
+    fprintf( stderr, _("failed to set PAM_TTY\n") );
+    goto out;
+  }
+
   /* Ask PAM to authenticate the user running this program */
   if( PAM_SUCCESS == pam_authenticate(pam_handle,0) ) {
     result = 1;  /* user authenticated OK! */
@@ -199,6 +212,7 @@ int authenticate_via_pam( const struct p
   }
 
   /* We're done with PAM.  Free `pam_handle'. */
+out:
   pam_end( pam_handle, PAM_SUCCESS );
  
   return( result );
@@ -277,6 +291,48 @@ int authenticate_via_shadow_passwd( cons
 
 #endif /* if/else USE_PAM */
 
+/*
+ * This function checks to see if the shell is known in /etc/shells.
+ * If so, it returns 1. On error or illegal shell, it returns 0.
+ */
+static int verify_shell(const char *shell_name)
+{
+  FILE *fp;
+  int found = 0, len;
+  char buf[FILENAME_MAX];
+
+  if (!shell_name)
+    return found;
+
+  fp = fopen("/etc/shells", "r");
+  if (!fp) {
+    fprintf(stderr,_("Can't open /etc/shells.\n"));
+    return found;
+  }
+
+  while (fgets (buf, sizeof (buf), fp) != NULL) {
+    /* ignore comments */
+    if (*buf == '#')
+      continue;
+    len = strlen (buf);
+
+    /* strip the ending newline */
+    if (buf[len - 1] == '\n')
+      buf[len - 1] = 0;
+
+    /* ignore lines that are too long */
+    else continue;
+
+    /* check the shell */
+    if (! strcmp (shell_name, buf)) {
+      found = 1;
+      break;
+    }
+  }
+  fclose (fp);
+  return found;
+}
+
 /************************************************************************
  *
  * All code used for both PAM and shadow passwd goes in this section.
@@ -285,13 +341,13 @@ int authenticate_via_shadow_passwd( cons
 
 int main( int argc, char *argv[] ) {
 
-  security_context_t new_context=NULL;          /* our target security context */
+  security_context_t new_context=NULL;	   /* our target security context */
   security_context_t old_context=NULL;	   /* our original securiy context */
   security_context_t tty_context=NULL;	   /* The current context of tty file */
-  security_context_t new_tty_context=NULL;	   /* The new context of tty file */
+  security_context_t new_tty_context=NULL; /* The new context of tty file */
   security_context_t chk_tty_context= NULL;
 
-  context_t context;		 	   /* manipulatable form of new_context */
+  context_t context;		 	 /* manipulatable form of new_context */
 
 
   const char *se_username;  /* SELinux user identity */
@@ -300,7 +356,7 @@ int main( int argc, char *argv[] ) {
 
   int clflag;                        /* holds codes for command line flags */
   int flag_index;                    /* flag index in argv[] */
-  struct option long_options[] = {   /* long option flags for getopt() */
+  const struct option long_options[] = {   /* long option flags for getopt() */
     { "role", 1, 0, 'r' },
     { "type", 1, 0, 't' },
     { "level", 1, 0, 'l' },
@@ -311,7 +367,13 @@ int main( int argc, char *argv[] ) {
   char *level_s = NULL;              /* level spec'd by user in argv[] */
   char *ttyn   = NULL;		     /* tty path */
   pid_t childPid=0;			     
-  int fd=0;
+  int fd;
+  int enforcing;
+  sigset_t empty;
+
+  /* Empty the signal mask in case someone is blocking a signal */
+  sigemptyset( &empty );
+  (void) sigprocmask( SIG_SETMASK, &empty, NULL );
 
   /* Terminate on SIGHUP. */
   signal(SIGHUP, SIG_DFL);
@@ -334,6 +396,11 @@ int main( int argc, char *argv[] ) {
 	     _("Sorry, newrole may be used only on a SELinux kernel.\n") );
     exit(-1);
   }
+  enforcing = security_getenforce();
+  if (enforcing < 0) {
+    fprintf(stderr, _("Could not determine enforcing mode.\n"));
+    exit(-1);
+  }
 
   while (1) {
     clflag=getopt_long(argc,argv,"r:t:l:",long_options,&flag_index);
@@ -452,11 +519,23 @@ int main( int argc, char *argv[] ) {
   pw->pw_dir = xstrdup(pw->pw_dir);
   pw->pw_shell = xstrdup(pw->pw_shell);
 
+  if (verify_shell(pw->pw_shell) == 0) {
+	fprintf(stderr, _("Error!  Shell is not valid.\n"));
+	exit(-1);
+  }
+
+  /* Get the tty name. Pam will need it. */
+  ttyn=ttyname(0);
+  if (!ttyn || *ttyn == '\0') {
+	fprintf(stderr, _("Warning!  Could not retrieve tty information.\n"));
+	exit(-1);
+  }
+
   printf(_("Authenticating %s.\n"),pw->pw_name);
 
   /* Authenticate the user running this program. */
 #ifdef USE_PAM
-  if( !authenticate_via_pam(pw) ) 
+  if( !authenticate_via_pam(pw, ttyn) ) 
 #else /* !USE_PAM */
   if( !authenticate_via_shadow_passwd(pw) ) 
 #endif /* if/else USE_PAM */
@@ -548,13 +627,7 @@ int main( int argc, char *argv[] ) {
    *
    */
 
-  /* Fetch TTY information */
-  ttyn=ttyname(0);
-  if (!ttyn || *ttyn == '\0') {
-	fprintf(stderr, _("Warning!  Could not retrieve tty information.\n"));
-	exit(-1);
-  }
-
+  /* Re-open TTY descriptor */
   fd = open(ttyn, O_RDWR);
   if (fd < 0) {
 	fprintf(stderr, _("Warning!  Could not open %s.\n"), ttyn);
@@ -562,17 +635,22 @@ int main( int argc, char *argv[] ) {
   }
 
   tty_context = NULL;
-  if (fgetfilecon(fd, &tty_context) < 0)
-	fprintf(stderr, _("Warning!  Could not get current context for %s, not relabeling.\n"), ttyn);
-    
+  if (fgetfilecon(fd, &tty_context) < 0) {
+	fprintf(stderr, _("Error!  Could not get current context for %s, not relabeling tty.\n"), ttyn);
+        if (enforcing)
+	  exit(-1);
+  }
 #ifdef CANTSPELLGDB
   if (tty_context)
     printf("Your tty %s was labeled with context %s\n", ttyn, tty_context);
 #endif
 
   new_tty_context = NULL;
-  if (tty_context && (security_compute_relabel(new_context,tty_context,SECCLASS_CHR_FILE,&new_tty_context) < 0))
-       fprintf(stderr, _("Warning!  Could not get new context for %s, not relabeling.\n"), ttyn);
+  if (tty_context && (security_compute_relabel(new_context,tty_context,SECCLASS_CHR_FILE,&new_tty_context) < 0)) {
+       fprintf(stderr, _("Error!  Could not get new context for %s, not relabeling tty.\n"), ttyn);
+       if (enforcing)
+         exit(-1);
+  }
 
 #ifdef CANTSPELLGDB
   if (new_tty_context)
@@ -584,6 +662,8 @@ int main( int argc, char *argv[] ) {
       fprintf(stderr, _("Warning!  Could not set new context for %s\n"), ttyn);
       freecon(new_tty_context);
       new_tty_context = NULL;
+      if (enforcing)
+        exit(-1);
     }
   }
 
@@ -592,12 +672,18 @@ int main( int argc, char *argv[] ) {
   if( childPid<0 ) {
     int errsv=errno;
     fprintf(stderr,_("newrole: failure forking: %s"),strerror(errsv));
+    if (fsetfilecon(fd,tty_context) < 0)
+      fprintf(stderr, _("Warning!  Could not restore context for %s\n"), ttyn);
+    freecon(tty_context);
     exit(-1);
   } else if (childPid) {
     /* PARENT */
-    wait(NULL);
+    int rc;
+    do {
+      rc = wait(NULL);
+    } while (rc < 0 && errno == EINTR);
 
-    if (!new_tty_context)
+    if (!new_tty_context||!tty_context)
       exit(0);
 
     /* Verify that the tty still has the context set by newrole. */
@@ -634,15 +720,15 @@ int main( int argc, char *argv[] ) {
       fprintf(stderr,_("Could not close descriptors.\n"));
       exit(-1);
     }
-  fd = open(ttyn,O_RDWR);
+  fd = open(ttyn,O_RDONLY);
   if (fd != 0) {
       exit(-1);
   }
-  fd = open(ttyn,O_RDWR);  
+  fd = open(ttyn,O_WRONLY);  
   if (fd != 1) {
       exit(-1);
   }
-  fd = open(ttyn,O_RDWR);  
+  fd = open(ttyn,O_WRONLY);  
   if (fd != 2) {
       exit(-1);
   }
#1 lore raw
Quoted 14 lines · Steve G

Hi,

I've been reviewing newrole in preparation for another patch to add auditing to it. The patch is fairly self-explanatory. Some highlights:

- little const cleanup
- pass tty to pam
- add a function to verify the shell is infact known to /etc/shells
- make sure no signals are blocked
- If in enforcing mode and error: getting tty context, computing new context, or setting new context then exit with -1.
- Loop the wait call to handle EINTR
- open the tty read only or write only depending on stdin, stdout, stderr.

Quoted 1 line

The patch is attached.

@@ -188,6 +191,16 @@ int authenticate_via_pam( const struct p

     exit( -1 );

}

+ if( strncmp(ttyn, "/dev/", 5) == 0 )
+ tty_name = ttyn+5;
+ else
+ tty_name = ttyn;

I have seen this idiom before, but am not clear on the rationale. If the tty isn't under /dev, then what are the real implications? Possibly this should just be an error condition?

+
+ if( PAM_SUCCESS != pam_set_item( pam_handle, PAM_TTY, tty_name ) ) {
+ fprintf( stderr, _("failed to set PAM_TTY\n") );
+ goto out;
+ }
+
<snip>
@@ -199,6 +212,7 @@ int authenticate_via_pam( const struct p
}

/* We're done with PAM. Free `pam_handle'. */
+out:
pam_end( pam_handle, PAM_SUCCESS );

This isn't really new to your patch, but it seems like this code should be cleaned up in general to save the pam error status from the pam_* functions and pass that status to pam_end(). This applies both to your new call to pam_set_item as well as the existing calls to pam_authenticate() and pam_acct_mgmt(), and we likely should be jumping to out upon a failure in pam_authenticate() rather than calling pam_acct_mgmt() at all in that case, right?

@@ -562,17 +635,22 @@ int main( int argc, char *argv[] ) {
}

tty_context = NULL;
- if (fgetfilecon(fd, &tty_context) < 0)
- fprintf(stderr, _("Warning! Could not get current context for %s, not relabeling.\n"), ttyn);
-
+ if (fgetfilecon(fd, &tty_context) < 0) {
+ fprintf(stderr, _("Error! Could not get current context for %s, not relabeling tty.\n"), ttyn);
+ if (enforcing)
+ exit(-1);
+ }

I'm a little concerned that if we report "Error!" rather than "Warning!" and then proceed (if permissive), then the user could get confused (i.e. they may think that they are back in the original shell due to the "Error" message). So we likely want to distinguish the error message based on which error handling path we will take (i.e. Error if treating it as a fatal error and Warning otherwise), and should likely clean up other error messages to be consistent. Or perhaps newrole should be more verbose about successfully launching the new shell.

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.
#3 lore raw
In reply to Steve G
Quoted 4 lines · Steve G

Hi,

I've been reviewing newrole in preparation for another patch to add auditing to it. The patch is fairly self-explanatory. Some highlights:

<snip>

Quoted 1 line

- add a function to verify the shell is infact known to /etc/shells

<snip>

Any particular reason you didn't use the getusershell(3) function for this purpose? It seems that it has as fallback in case /etc/shells doesn't exist at all on the system (still permitting /bin/sh and /bin/csh).

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.
author
#2 lore raw
In reply to Stephen Smalley
Quoted 9 lines
newrole/newrole.c +4 -0
+  if( strncmp(ttyn, "/dev/", 5) == 0 )
+    tty_name = ttyn+5;
+  else
+    tty_name = ttyn;

I have seen this idiom before, but am not clear on the rationale. If the tty isn't under /dev, then what are the real implications? Possibly this should just be an error condition?

Its so that we get the tty's name rather than the path.

Quoted 14 lines
newrole/newrole.c +7 -0
+
+  if( PAM_SUCCESS != pam_set_item( pam_handle, PAM_TTY, tty_name ) ) {
+    fprintf( stderr, _("failed to set PAM_TTY\n") );
+    goto out;
+  }
+
<snip>
@@ -199,6 +212,7 @@ int authenticate_via_pam( const struct p
  }
 
  /* We're done with PAM.  Free `pam_handle'. */
+out:
  pam_end( pam_handle, PAM_SUCCESS );
 
Quoted 7 lines

This isn't really new to your patch, but it seems like this code should be cleaned up in general to save the pam error status from the pam_* functions and pass that status to pam_end(). This applies both to your new call to pam_set_item as well as the existing calls to pam_authenticate() and pam_acct_mgmt(), and we likely should be jumping to out upon a failure in pam_authenticate() rather than calling pam_acct_mgmt() at all in that case, right?

Right. I'll make those adjustments.

Quoted 2 lines

I'm a little concerned that if we report "Error!" rather than "Warning!" and then proceed (if permissive), then the user could get confused

I'll make adjustments to switch between Error & Warning.

-Steve

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

--
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.

author
#4 lore raw
In reply to Stephen Smalley
Quoted 3 lines

Any particular reason you didn't use the getusershell(3) function for this purpose?

Yep. Didn't know about it. :)

Would you prefer the patch be modified to use it?

-Steve

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

--
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.

#5 lore raw
Quoted 6 lines · Steve G
Quoted 2 lines

Any particular reason you didn't use the getusershell(3) function for this purpose?

Yep. Didn't know about it. :)

Would you prefer the patch be modified to use it?

I think so, unless there is some known problem with it. It abstracts away the details of where the shell information comes from and what the default values are, so it seems useful.

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.