status tool
Over the last couple weeks I've been working on a SELinux status tool. I originally intended it for use as a support tool, so we could get much of SELinux's status in one shot. Since not being able to log in is a common problem, I aimed the tool to be helpful with figuring out the problem. After some suggestions from Russell, the result is the attached sestatus.
With no options, it gives short status info:
pebenito@gorn src $ sestatus
SELinux status: enabled SELinuxfs mount: /selinux Current Mode: permissive Policy version: 16
For verbose info, add -v:
pebenito@gorn src $ sestatus -v
SELinux status: enabled SELinuxfs mount: /selinux Current Mode: permissive Policy version: 16
Process contexts:
Current context: pebenito:staff_r:staff_t /sbin/init system_u:system_r:init_t /sbin/mingetty system_u:system_r:getty_t /usr/sbin/sshd system_u:system_r:sshd_t
File contexts:
Controlling Term: pebenito:object_r:staff_devpts_t /etc/passwd system_u:object_r:etc_t /etc/shadow system_u:object_r:shadow_t /bin/bash system_u:object_r:shell_exec_t /bin/login system_u:object_r:login_exec_t /bin/sh system_u:object_r:bin_t -> system_u:object_r:shell_exec_t /sbin/agetty system_u:object_r:getty_exec_t /sbin/init system_u:object_r:init_exec_t /sbin/mingetty system_u:object_r:getty_exec_t /usr/sbin/sshd system_u:object_r:sshd_exec_t /lib/libc.so.6 system_u:object_r:lib_t -> system_u:object_r:shlib_t /lib/ld-linux.so.2 system_u:object_r:lib_t -> system_u:object_r:ld_so_t
So, the verbose checks the contexts of a pile of files, and a few processes. It also checks the context of the target, in cases of symlinks.
Attached is the source, and a /etc/sestatus.conf, which defines which processes and files to context check. Its CVS repo:
Suggestions/patches are welcome. I just threw this together, so I'm sure someone will find something :)
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
/*
* Copyright 1999-2004 Gentoo Technologies, Inc.
* Distributed under the terms of the GNU General Public License v2
* $Header: /home/cvsroot/gentoo-projects/hardened/policycoreutils-extra/src/sestatus.c,v 1.7 2004/03/22 05:50:24 pebenito Exp $ */
#include <stdio.h>
#include <errno.h>
#include <selinux/selinux.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#define PROC_BASE "/proc"
#define MAX_CHECK 50
#define CONF "/etc/sestatus.conf"
/* conf file sections */
#define PROCS "[process]"
#define FILES "[files]"
extern char *selinux_mnt;
int cmp_cmdline(const char *command, int pid) {
/* this function is adapted from run_init.c */
FILE* fp; char buf[255], *bufp; int buf_len, rc; char filename[255];
/* first read the proc entry */ sprintf(filename,"%s/%d/cmdline",PROC_BASE,pid);
fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Could not open file %s\n", filename);
return 0;
}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 ) {
if (!(*bufp))
goto out;
fclose(fp);
/* check if this is the command we're looking for. *
* this breaks if the command line does not ahve the *
* entire path of the executable :( */
rc = strncmp(command,bufp, strlen(command));
if( rc == 0 )
return 1;
else
return 0;
} }
out:
fclose(fp);
return 0;
}
int pidof(const char *command) {
/* inspired by killall5.c from psmisc */
DIR *dir;
struct dirent *de;
int pid, ret=-1, self=getpid();
if (!(dir = opendir(PROC_BASE))) {
perror(PROC_BASE);
return -1;
}
while ( (de = readdir(dir)) != NULL) {
if (!(pid = atoi (de->d_name)) || pid == self)
continue;
if(cmp_cmdline(command,pid)) {
ret=pid;
break;
}
}
closedir(dir);
return ret;
}void load_checks(char *pc[], int *npc, char *fc[], int *nfc) {
FILE *fp=fopen(CONF,"r");
char buf[255], *bufp;
int buf_len, section=-1;
int proclen=strlen(PROCS);
int filelen=strlen(FILES);
if( fp == NULL ) {
printf("\nUnable to open %s.\n",CONF);
return;
}
while (!feof(fp)) {
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 == '#')
/* skip comments */
continue;
if( *bufp ) {
if (!(*bufp))
goto out;
if(strncmp(bufp,PROCS,proclen) == 0)
section=0;
else if(strncmp(bufp,FILES,filelen) == 0)
section=1;
else {
switch( section ) {
case 0:
pc[*npc]=(char*)malloc((buf_len)*sizeof(char));
memcpy(pc[*npc],bufp,buf_len);
(*npc)++;
bufp=NULL;
break;
case 1:
fc[*nfc]=(char*)malloc((buf_len)*sizeof(char));
memcpy(fc[*nfc],bufp,buf_len);
(*nfc)++;
bufp=NULL;
break;
default:
/* ignore lines before a section */
printf("Line not in a section: %s.\n",buf);
break;
}
}
}
}
out:
fclose(fp);
return;
}int main(int argc, char **argv) {
/* these vars are reused several times */
int rc, i,j,c;
char *context;
/* files that need context checks */
char *fc[MAX_CHECK];
char *cterm=ttyname(0);
int nfc=0;
struct stat m;
/* processes that need context checks */
char *pc[MAX_CHECK];
int npc=0;
c=getopt(argc, argv, "v");
printf("SELinux status:\t\t");
rc=is_selinux_enabled();
switch(rc) {
case 1:
printf("enabled\n");
break;
case 0:
printf("disabled\n");
return 0;
break;
case -1:
perror("unknown");
return 0;
break;
default:
printf("Unknown is_selinux_enabled() return code: %d\n",rc);
return 0;
break;
}
printf("SELinuxfs mount:\t");
if( selinux_mnt != NULL) {
printf("%s\n",selinux_mnt);
} else {
printf("not mounted\n\n");
printf("Please mount selinuxfs for proper results.\n");
return -1;
}
printf("Current Mode:\t\t");
rc=security_getenforce();
switch(rc) {
case 1:
printf("enforcing\n");
break;
case 0:
printf("permissive\n");
break;
default:
printf("unknown\n");
break;
}
rc = security_policyvers();
printf("Policy version:\t\t%d\n",rc);
/* only show contexts if -v is given */
if(c != 'v') return 0;
load_checks(pc,&npc,fc,&nfc);
printf("\nProcess contexts:\n");
getcon(&context);
printf("Current context:\t%s\n",context);
freecon(context);
getpidcon(1,&context);
printf("/sbin/init\t\t%s\n",context);
freecon(context);
for( i=0 ; i<npc ; i++ ) {
rc=pidof(pc[i]);
if(rc > 0) {
getpidcon(rc,&context);
printf("%s",pc[i]);
/* insert the right number of tabs */
rc=((32-strlen(pc[i]))/8);
for( j=0 ; j<rc ; j++ )
printf("\t");
printf("%s\n",context);
freecon(context);
}
}
printf("\nFile contexts:\n");
/* controlling term */
lgetfilecon(cterm,&context);
printf("Controlling Term:\t%s\n",context);
freecon(context);
for( i=0 ; i<nfc ; i++ ) {
if( lgetfilecon(fc[i],&context) >= 0 ) {
printf("%s",fc[i]);
/* insert the right number of tabs */
rc=((32-strlen(fc[i]))/8);
for( j=0 ; j<rc ; j++ )
printf("\t");
/* check if this is a symlink */
lstat(fc[i],&m);
if( S_ISLNK(m.st_mode) ) {
/* print link target context */
printf("%s -> ",context);
freecon(context);
getfilecon(fc[i],&context);
}
printf("%s\n",context);
}
freecon(context);
}
return 0;
}[files]/etc/passwd/etc/shadow/bin/bash/bin/login/bin/sh/sbin/agetty/sbin/init/sbin/mingetty/usr/sbin/sshd/lib/libc.so.6/lib/ld-linux.so.2/lib/ld.so.1
[process]/sbin/mingetty/sbin/agetty/usr/sbin/sshd
Quoted 6 lines · Chris PeBenito
Over the last couple weeks I've been working on a SELinux status tool. I originally intended it for use as a support tool, so we could get much of SELinux's status in one shot. Since not being able to log in is a common problem, I aimed the tool to be helpful with figuring out the problem. After some suggestions from Russell, the result is the attached sestatus.
Thanks, this seems like a good idea. Did you consider readlink of /proc/pid/exe rather than read of /proc/pid/cmdline when trying to find the process for a given program? I suppose that might be a problem if they can't become root on the system, but it would offer a more reliable path for the executable. Another possibility would be to scan /proc/pid/maps.
Signature · 8 lines
Stephen Smalley <[email protected]> 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.
Quoted 9 lines · Stephen Smalley
Quoted 1 line · Chris PeBenito
Over the last couple weeks I've been working on a SELinux status tool.
Thanks, this seems like a good idea. Did you consider readlink of
/proc/pid/exerather than read of/proc/pid/cmdlinewhen trying to find the process for a given program? I suppose that might be a problem if they can't become root on the system, but it would offer a more reliable path for the executable. Another possibility would be to scan/proc/pid/maps.
Good idea; I revised it to readlink /proc/pid/exe. Problems from not being root doesn't really concern me. Its more of an administrative tool anyway, and you already have to be sysadm_r to read selinuxfs entries.
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
/*
* Copyright 1999-2004 Gentoo Technologies, Inc.
* Distributed under the terms of the GNU General Public License v2
* $Header: /home/cvsroot/gentoo-projects/hardened/policycoreutils-extra/src/sestatus.c,v 1.10 2004/03/26 19:25:52 pebenito Exp $ */
#include <stdio.h>
#include <errno.h>
#include <selinux/selinux.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#define PROC_BASE "/proc"
#define MAX_CHECK 50
#define CONF "/etc/sestatus.conf"
/* conf file sections */
#define PROCS "[process]"
#define FILES "[files]"
/* buffer size for cmp_cmdline */
#define BUFSIZE 255
extern char *selinux_mnt;
int cmp_cmdline(const char *command, int pid) {
char buf[BUFSIZE]; char filename[BUFSIZE]; memset(buf, '\0', BUFSIZE); /* first read the proc entry */ sprintf(filename,"%s/%d/exe",PROC_BASE,pid); if( readlink(filename,buf,BUFSIZE) < 0 ) return 0; if( buf[BUFSIZE-1] != '\0' ) buf[BUFSIZE-1] = '\0'; /* check if this is the command we're looking for. */ if( strncmp(command,buf,strlen(command)) == 0 ) return 1; else return 0; }
int pidof(const char *command) {
/* inspired by killall5.c from psmisc */
DIR *dir;
struct dirent *de;
int pid, ret=-1, self=getpid();
if(!(dir = opendir(PROC_BASE))) {
perror(PROC_BASE);
return -1;
}
while( (de = readdir(dir)) != NULL) {
if (!(pid = atoi (de->d_name)) || pid == self)
continue;
if(cmp_cmdline(command,pid)) {
ret=pid;
break;
}
}
closedir(dir);
return ret;
}void load_checks(char *pc[], int *npc, char *fc[], int *nfc) {
FILE *fp=fopen(CONF,"r");
char buf[255], *bufp;
int buf_len, section=-1;
int proclen=strlen(PROCS);
int filelen=strlen(FILES);
if( fp == NULL ) {
printf("\nUnable to open %s.\n",CONF);
return;
}
while(!feof(fp)) {
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 == '#')
/* skip comments */
continue;
if( *bufp ) {
if(!(*bufp))
goto out;
if(strncmp(bufp,PROCS,proclen) == 0)
section=0;
else if(strncmp(bufp,FILES,filelen) == 0)
section=1;
else {
switch( section ) {
case 0:
pc[*npc]=(char*)malloc((buf_len)*sizeof(char));
memcpy(pc[*npc],bufp,buf_len);
(*npc)++;
bufp=NULL;
break;
case 1:
fc[*nfc]=(char*)malloc((buf_len)*sizeof(char));
memcpy(fc[*nfc],bufp,buf_len);
(*nfc)++;
bufp=NULL;
break;
default:
/* ignore lines before a section */
printf("Line not in a section: %s.\n",buf);
break;
}
}
}
}
out:
fclose(fp);
return;
}int main(int argc, char **argv) {
/* these vars are reused several times */
int rc, i,j,c;
char *context;
/* files that need context checks */
char *fc[MAX_CHECK];
char *cterm=ttyname(0);
int nfc=0;
struct stat m;
/* processes that need context checks */
char *pc[MAX_CHECK];
int npc=0;
c=getopt(argc, argv, "v");
printf("SELinux status:\t\t");
rc=is_selinux_enabled();
switch(rc) {
case 1:
printf("enabled\n");
break;
case 0:
printf("disabled\n");
return 0;
break;
case -1:
perror("unknown");
return 0;
break;
default:
printf("Unknown is_selinux_enabled() return code: %d\n",rc);
return 0;
break;
}
printf("SELinuxfs mount:\t");
if( selinux_mnt != NULL) {
printf("%s\n",selinux_mnt);
} else {
printf("not mounted\n\n");
printf("Please mount selinuxfs for proper results.\n");
return -1;
}
printf("Current Mode:\t\t");
rc=security_getenforce();
switch(rc) {
case 1:
printf("enforcing\n");
break;
case 0:
printf("permissive\n");
break;
default:
printf("unknown\n");
break;
}
rc = security_policyvers();
printf("Policy version:\t\t%d\n",rc);
/* only show contexts if -v is given */
if(c != 'v') return 0;
load_checks(pc,&npc,fc,&nfc);
printf("\nProcess contexts:\n");
getcon(&context);
printf("Current context:\t%s\n",context);
freecon(context);
getpidcon(1,&context);
printf("/sbin/init\t\t%s\n",context);
freecon(context);
for( i=0 ; i<npc ; i++ ) {
rc=pidof(pc[i]);
if(rc > 0) {
getpidcon(rc,&context);
printf("%s",pc[i]);
/* insert the right number of tabs */
rc=((32-strlen(pc[i]))/8);
for( j=0 ; j<rc ; j++ )
printf("\t");
printf("%s\n",context);
freecon(context);
}
}
printf("\nFile contexts:\n");
/* controlling term */
lgetfilecon(cterm,&context);
printf("Controlling Term:\t%s\n",context);
freecon(context);
for( i=0 ; i<nfc ; i++ ) {
if( lgetfilecon(fc[i],&context) >= 0 ) {
printf("%s",fc[i]);
/* insert the right number of tabs */
rc=((32-strlen(fc[i]))/8);
for( j=0 ; j<rc ; j++ )
printf("\t");
/* check if this is a symlink */
lstat(fc[i],&m);
if( S_ISLNK(m.st_mode) ) {
/* print link target context */
printf("%s -> ",context);
freecon(context);
getfilecon(fc[i],&context);
}
printf("%s\n",context);
}
freecon(context);
}
return 0;
}