Skip to contentSkip to conversation

runscript_selinux

DISCUSSION Chris PeBenito 1 participant 1 message Jun 28, 2004 SELinux security module

lore
Share & tools
lore
#0 lore raw

This is something I created to eliminate the need of using run_init when doing daemon management, and has been used in Gentoo for a while. Gentoo has a dependency based init script system, and the scripts have a binary (/sbin/runscript) for the script interpreter. Its there that runscript_selinux.so is dlopened and ran, leaving us with an integrated run_init; role transitions and typing 'run_init' isn't needed. I added another config file run_init_type (containing "run_init_t"). When the current domain matches this, runscript_selinux does the pam authentication. Otherwise, the authentication and setexec are skipped, since either it is already initrc_t, or labeling is wrong, and things will break anyway (such as if current context is kernel_t or init_t). I also have it assume defaults if the run_init_type and initrc_context files were missing or empty, since runscript_selinux runs even during boot, and we don't want to fail at that point.

Runscript_selinux is executed in our /sbin/runscript thusly:

	lib_handle = dlopen("/lib/rcscripts/runscript_selinux.so", RTLD_NOW | RTLD_GLOBAL);
	if( lib_handle != NULL ) {
		selinux_run_init = dlsym(lib_handle, "selinux_runscript");
		selinux_run_init();
	}

	[execv-related code here]

and it is compiled:
gcc -Wall -fPIC -DUSE_PAM -shared -o runscript_selinux.so runscript_selinux.c -Wl,-soname,runscript_selinux.so -lpam -lpam_misc -lselinux -ldl

The only policy needed is:
domain_auto_trans(sysadm_t,initrc_exec_t,run_init_t)

Signature · 8 lines
Chris PeBenito
<[email protected]>
Developer,
Hardened Gentoo Linux
Embedded Gentoo Linux
 
Public Key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xE6AF9243
Key fingerprint = B0E6 877A 883F A57A 8E6A  CB00 BC8E E42D E6AF 9243

/*
* Distributed under the terms of the GNU General Public License v2
* $Header: /var/cvsroot/gentoo-projects/hardened/policycoreutils-extra/src/runscript_selinux.c,v 1.5 2004/06/21 16:52:22 pebenito Exp $ */

/*
* This is a modified NSA run_init for use in Gentoo's /sbin/runscript. */

/************************************************************************
* BUILD OPTIONS:
*
* option USE_PAM:
*
* Set the USE_PAM constant if you want to authenticate users via PAM.
* If USE_PAM is not set, users will be authenticated via direct
* access to the shadow password file.
*
* If you decide to use PAM must be told how to handle run_init. A
* good rule-of-thumb might be to tell PAM to handle run_init in the
* same way it handles su, except that you should remove the pam_rootok.so
* entry so that even root must re-authenticate to run the init scripts
* in the proper context.
*
* If you choose not to use PAM, make sure you have a shadow passwd file
* in /etc/shadow. You can use a symlink if your shadow passwd file
* lives in another directory. Example:

 *   su
 *   cd /etc
 *   ln -s /etc/auth/shadow shadow

*
* If you decide not to use PAM, you will also have to make run_init
* setuid root, so that it can read the shadow passwd file. *************************************************************************/

#include <stdio.h>

#include <stdlib.h>               /* for malloc(), realloc(), free() */
#include <pwd.h>                  /* for getpwuid() */
#include <sys/types.h>            /* to make getuid() and getpwuid() happy */

#include <sys/wait.h> /* for wait() */
#include <sys/stat.h> /* for struct stat and friends */
#include <getopt.h> /* for getopt_long() form of getopt() */
#include <selinux/selinux.h>
#include <selinux/get_default_type.h>
#include <selinux/context.h> /* for context-mangling functions */
#include <fcntl.h>
#include <ctype.h>
#include <libintl.h>

#define RUN_INIT_FILE "/etc/security/run_init_type" #define INITRC_FILE "/etc/security/initrc_context" #ifdef USE_PAM

/************************************************************************
*
* All PAM code goes in this section.
* ************************************************************************/

#include <unistd.h> /* for getuid(), exit(), getopt() */

#include <security/pam_appl.h> /* for PAM functions */
#include <security/pam_misc.h> /* for misc_conv PAM utility function */

#define SERVICE_NAME "run_init"   /* the name of this program for PAM */
				  /* The file containing the context to run 
				   * the scripts under.                     */

int authenticate_via_pam( const struct passwd * );

/* authenticate_via_pam()
*
* in: p_passwd_line - struct containing data from our user's line in
* the passwd file.
* out: nothing

 * return: value   condition
 *         -----   ---------
 *           1     PAM thinks that the user authenticated themselves properly
 *           0     otherwise

*
* This function uses PAM to authenticate the user running this
* program. This is the only function in this program that makes PAM
* calls.
*
*/

int authenticate_via_pam( const struct passwd *p_passwd_line ) {

int result = 0; /* our result, set to 0 (not authenticated) by default */
pam_handle_t *pam_handle; /* opaque handle used by all PAM functions */

/* 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 *
* provided for us via pam_misc.h. */
struct pam_conv pam_conversation = {
misc_conv,
NULL
};

/* Make `p_pam_handle' a valid PAM handle so we can use it when *
* calling PAM functions. */
if( PAM_SUCCESS != pam_start( SERVICE_NAME,

				p_passwd_line->pw_name,
				&pam_conversation,
				&pam_handle ) ) {
    fprintf( stderr, "failed to initialize PAM\n" );
    exit( -1 );

}

  /* Ask PAM to authenticate the user running this program */
  if( PAM_SUCCESS == pam_authenticate(pam_handle,0) ) {
    result = 1;  /* user authenticated OK! */
  }

  /* We're done with PAM.  Free `pam_handle'. */
  pam_end( pam_handle, PAM_SUCCESS );
 
  return( result );

} /* authenticate_via_pam() */

#else /* else !USE_PAM */

/************************************************************************
*
* All shadow passwd code goes in this section.
* ************************************************************************/

#include <unistd.h>                         /* for getuid(), exit(), crypt() */
#include <shadow.h>                         /* for shadow passwd functions */
#include <string.h>                         /* for strlen(), memset() */

#define PASSWORD_PROMPT "Password:" /* prompt for getpass() */

int authenticate_via_shadow_passwd( const struct passwd * );

/* authenticate_via_shadow_passwd()
*
* in: p_passwd_line - struct containing data from our user's line in
* the passwd file.
* out: nothing

 * return: value   condition
 *         -----   ---------
 *           1     user authenticated themselves properly according to the
 *                 shadow passwd file.
 *           0     otherwise

*
* This function uses the shadow passwd file to authenticate the user running
* this program.
*
*/

int authenticate_via_shadow_passwd( const struct passwd *p_passwd_line ) {

struct spwd *p_shadow_line; /* struct derived from shadow passwd file line */
char *unencrypted_password_s; /* unencrypted password input by user */
char *encrypted_password_s; /* user's password input after being crypt()ed */

/* Make `p_shadow_line' point to the data from the current user's *
* line in the shadow passwd file. */
setspent(); /* Begin access to the shadow passwd file. */
p_shadow_line = getspnam( p_passwd_line->pw_name );
endspent(); /* End access to the shadow passwd file. */

  if( !( p_shadow_line ) ) {
    fprintf( stderr, "Cannot find your entry in the shadow passwd file.\n" );
    exit( 1 );

}

  /* Ask user to input unencrypted password */
  if( ! ( unencrypted_password_s = getpass( PASSWORD_PROMPT ) ) ) {
    fprintf( stderr, "getpass cannot open /dev/tty\n" );
    exit( 1 );
  }

/* Use crypt() to encrypt user's input password. Clear the *
* unencrypted password as soon as we're done, so it is not *
* visible to memory snoopers. */
encrypted_password_s = crypt( unencrypted_password_s,

				p_shadow_line->sp_pwdp );

memset( unencrypted_password_s, 0, strlen( unencrypted_password_s ) );

/* Return 1 (authenticated) iff the encrypted version of the user's *
* input password matches the encrypted password stored in the *
* shadow password file. */
return( !strcmp( encrypted_password_s, p_shadow_line->sp_pwdp ) );

} /* authenticate_via_shadow_passwd() */

#endif /* if/else USE_PAM */

/*
* authenticate_user()
*
* Authenticate the user.
*
* in: nothing
* out: nothing
* return: 0 When success
* -1 When failure
*/
int authenticate_user() {

#define INITLEN 255
security_context_t old_context; /* original security context as a string */
context_t ocontext; /* manipulatable form of context_s */

struct passwd *p_passwd_line; /* struct derived from passwd file line */

  /* Put our osid into `osid'. */
  if( 0!=(getprevcon(&old_context)) ) {
    fprintf(stderr,"failed to get old_context.\n");
    exit(1);
  }

  ocontext=context_new(old_context);                                
  freecon(old_context);
  if( !(p_passwd_line=getpwnam(context_user_get(ocontext))) ) {
    fprintf(stderr, "cannot find your entry in the passwd file.\n");
    return(-1);
  }
  context_free( ocontext );
  
  printf("Authenticating %s.\n",p_passwd_line->pw_name);

/* Authenticate the user running this program. */
#ifdef USE_PAM
if( !authenticate_via_pam(p_passwd_line) ) {
#else /* !USE_PAM */
if( !authenticate_via_shadow_passwd(p_passwd_line) ) {

#endif /* if/else USE_PAM */
    fprintf(stderr, "runscript: incorrect password for %s\n", p_passwd_line->pw_name);
    return(-1);

}

/* If we reach here, then we have authenticated the user. */
#ifdef CANTSPELLGDB
printf( "You are authenticated!\n" );
#endif

return 0;

} /* authenticate_user() */

/*
* get_init_context()
*
* Get the CONTEXT associated with the context for the init scripts. *
*
* in: nothing
* out: The CONTEXT associated with the context.
* return: 0 on success, -1 on failure.
*/
int get_context_from_file(const char *filename, security_context_t *context) {

  FILE* fp;
  char  buf[255], *bufp;
  int buf_len;
  
  fp = fopen(filename, "r");
  if (!fp) {
    fprintf(stderr, "runscript_selinux.so: Could not open file %s\n", filename);
    return -1;
  }

while (1) { /* loop until we find a non-empty line */

    if (!fgets(buf, sizeof buf, fp))
      break;

    buf_len = strlen(buf);
    if (buf[buf_len-1] == '\n')
      buf[buf_len-1] = 0;

    bufp = buf;
    while (*bufp && isspace(*bufp))
      bufp++;

    if( *bufp ) {
      *context = strdup(bufp);
      if (!(*context))
	goto out;
      fclose(fp);
      return 0;
    }

}
out:
fclose(fp);
fprintf(stderr, "runscript_selinux.so: No context in file %s\n", filename); return -1;

} /* get_context_from_file() */

/*****************************************************************************
* selinux_runscript() *
*****************************************************************************/
void selinux_runscript(){

security_context_t new_context; /* context for the init script context */
security_context_t curr_context; /* context we are currently in */

/* use this to extract the type */
context_t curr_con;
const char *curr_type, *run_init_type;

/*
* Return, if selinux is disabled.
*/
if( is_selinux_enabled() < 1 ) {

#ifdef CANTSPELLGDB
    printf("SELinux is disabled.  Skipping selinux_runscript().\n");
#endif
    return;

}

/*
* Get the run_init type (knowing that security_context_t is char*)
*/
if( get_context_from_file(RUN_INIT_FILE,(security_context_t*)&run_init_type) == 0 ) {

#ifdef CANTSPELLGDB
    printf("run_init type is %s\n", run_init_type);
#endif
  } else {
    /* assume a reasonable default, rather than bailing out */
    run_init_type="run_init_t";
    fprintf(stderr, "runscript_selinux.so: Assuming run_init type is %s\n", run_init_type);

}

/*
* Get our current context.
*/
if ( getcon(&curr_context) == 0 ) {

#ifdef CANTSPELLGDB
    printf("current context is %s\n", curr_context);
#endif
  } else {
    exit(1);

}

  /* extract the type from the context */
  curr_con=context_new(curr_context);
  curr_type=context_type_get(curr_con);

/*
* Authenticate if we're in the run_init domain. If we are not
* in the run_init domain, then either we are already in initrc_t
* or things are mislabeled, and this won't work right anyway. */

  if( strncmp(run_init_type,curr_type,strlen(run_init_type)) == 0 ) {
    if( authenticate_user() != 0 ) {
      fprintf(stderr, "Authentication failed.\n");
      exit(1);
    }

/*
* Get the context for the script to be run in.
*/

    if( get_context_from_file(INITRC_FILE,&new_context) == 0 ) { 
#ifdef CANTSPELLGDB
      printf("initrc context is %s\n", new_context);
#endif  
    } else {
      /* assume a reasonable default, rather than bailing out */
      new_context="system_u:system_r:initrc_t";
      fprintf(stderr, "runscript_selinux.so: Assuming initrc context is %s\n", new_context);
    }

/*

    * Set the correct context since we're not in the right domain.
    */

    if (setexeccon(new_context) < 0) {
       fprintf(stderr, "Could not set exec context to %s.\n", new_context);
       exit(1);
    }

}

  context_free(curr_con);
  freecon(new_context);
  freecon(curr_context);

} /* selinux_runscript() */