A couple of patches to setfiles.
Added -e flag to exclude directories.
setfiles -e /home -e /tmp -v -n /etc/selinux/strict/contexts/files/file_context /
Also added syslog to log differences.
I would like to move the fixfiles/setfiles stuff to a more central logging capability. Need to cleanup the way it currently works.
Dan
restorecon/restorecon.c renamed from restorecon/restorecon.c.rhat +41 -11
@@ -8,7 +8,7 @@* to match the specification returned by matchpathcon. * * USAGE: -* restorecon [-nv] pathname... +* restorecon [-Rnv] pathname... * * -n Do not change any file labels. * -v Show changes in file labels.
@@ -33,19 +33,25 @@#include <sys/stat.h> #include <fcntl.h> #include <stdio.h> +#define __USE_XOPEN_EXTENDED 1/* nftw */ +#include <ftw.h> -char *progname; +static int change=1 ; +static int verbose=0 ; +static FILE *outfile=NULL ; +static char *progname; +static int errors=0 ; +static int recurse;void usage (const char *const name) {fprintf (stderr, -"usage: %s [-nv] [-f filename | pathname... ]\n" , name); +"usage: %s [-Rnv] [-f filename | pathname... ]\n" , name);exit (1 ); } -int restore (char *filename,int change,int verbose,FILE *outfile) { +int restore (char *filename) {int retcontext=0 ;int retval=0 ; -int errors=0 ;security_context_t scontext;security_context_t prev_context;int len=strlen (filename);
@@ -135,15 +141,36 @@freecon (scontext);return errors; } +static int apply_spec (const char *file, +const struct stat *sb_unused,int flag,struct FTW *s_unused) +{ +if (flag ==FTW_DNR ) { +fprintf (stderr,"%s: unable to read directory %s\n" , + progname, file); +return 0 ; + } + errors=errors+restore ((char *)file); +return 0 ; +} +void process (char *buf) { +if (recurse) { +if (nftw + (buf, apply_spec,1024 ,FTW_PHYS |FTW_MOUNT )) { +fprintf (stderr, +"%s: error while labeling files under %s\n" , + progname, buf); +exit (1 ); + } + } +else + errors=errors+restore (buf); +}int main (int argc,char **argv) {int i=0 ;char *file_name=NULL ;int errors=0 ; -int change=1 ; -int verbose=0 ;int file=0 ;int opt; -FILE *outfile=NULL ;char buf[PATH_MAX ]; progname=argv[0 ];
@@ -152,11 +179,14 @@memset (buf,0 ,sizeof (buf)); -while ((opt =getopt (argc, argv,"nvf:o:" )) >0 ) { +while ((opt =getopt (argc, argv,"Rnvf:o:" )) >0 ) {switch (opt) {case 'n' : change =0 ;break ; +case 'R' : + recurse =1 ; +break ;case 'o' : outfile =fopen (optarg,"w" );if (!outfile) {
@@ -187,14 +217,14 @@ }while (fgets (buf,PATH_MAX ,f)) { buf[strlen (buf)-1 ]=0 ; - errors=errors+restore (buf, change, verbose, outfile); +process (buf); }if (strcmp (file_name,"-" )!=0 )fclose (f); }else {for (i=optind; i< argc; i++) { - errors=errors+restore (argv[i], change, verbose, outfile); +process (argv[i]); } }if (outfile)
restorecon/restorecon.8 renamed from restorecon/restorecon.8.rhat +5 -2
@@ -4,10 +4,10 @@ .SH "SYNOPSIS" .B restorecon -.I [\-o outfilename ] [\-n] [\-v] pathname... +.I [\-o outfilename ] [\-R] [\-n] [\-v] pathname... .P .B restorecon -.I \-f infilename [\-o outfilename ] [\-n] [\-v] +.I \-f infilename [\-o outfilename ] [\-R] [\-n] [\-v] .SH "DESCRIPTION" This manual page describes the
@@ -26,6 +26,9 @@ .B \-f infilename infilename contains a list of files to be processed by application. Use \- for stdin. .TP +.B \-R +change files and directories file labels recursively +.TP .B \-n don't change any file labels. .TP
scripts/fixfiles renamed from scripts/fixfiles.rhat +48 -23
151 lines not on this page · raw message
setfiles/setfiles.8 renamed from setfiles/setfiles.8.rhat +4 -1
@@ -4,7 +4,7 @@ .SH "SYNOPSIS" .B setfiles -.I [\-d] [\-n] [\-o filename ] [\-q] [\-s] [\-v] [\-vv] [\-W] spec_file pathname... +.I [\-d] [\-n] [\-e directory ] [\-o filename ] [\-q] [\-s] [\-v] [\-vv] [\-W] spec_file pathname... .SH "DESCRIPTION" This manual page describes the .BR setfiles
@@ -29,6 +29,9 @@ .B \-q suppress non-error output. .TP +.B \-e directory +directory to exclude (repeat option for more than one directory.) +.TP .B \-o filename save list of files with incorrect context in filename. .TP
setfiles/setfiles.c renamed from setfiles/setfiles.c.rhat +65 -2
121 lines not on this page · raw message
Quoted 6 lines · Daniel J Walsh
Added -e flag to exclude directories.
setfiles -e /home -e /tmp -v -n
/etc/selinux/strict/contexts/files/file_context/Also added syslog to log differences.
You want to log always? Should be based on an option, like the existing verbose option, and you likely only want to log if !only_changed_user as with the verbose case.
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 3 lines · Stephen Smalley
You want to log always? Should be based on an option, like the existing verbose option, and you likely only want to log if !only_changed_user as with the verbose case.
Actually, there is already a -o option to log to a specified file. Why add syslog calls as well?
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 14 lines · Stephen Smalley
Quoted 5 lines · Stephen Smalley
You want to log always? Should be based on an option, like the existing verbose option, and you likely only want to log if !only_changed_user as with the verbose case.
Actually, there is already a -o option to log to a specified file. Why add syslog calls as well?
Steve Grub has pointed out that the way we are doing setfiles is all wrong, and that we should be logging to syslog. That way a management infrastructure could monitor and manage the environment. Writing temporary or log files in arbitrary places is wrong. Tools like logrotate are going to clean them up. ETC. So the -o flag should go and we should use syslog.
So I think we need to relook at setfiles/fixfiles and plug them better into standard architecture.
I think we need a mechanism to tell us when a file actually has the wrong context versus one that just does not match the context in the file_context file. Russell pointed out that the contents of the homedirectory should not have an automatic tool looking at their context because it might open up a security hole if you were to arbitrarily clean up the false positives.
Dan
--
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 7 lines · Daniel J Walsh
Steve Grub has pointed out that the way we are doing setfiles is all wrong, and that we should be logging
to syslog. That way a management infrastructure could monitor and manage the environment. Writing
temporary or log files in arbitrary places is wrong. Tools like logrotate are going to clean them up. ETC.
So the -o flag should go and we should use syslog.
Shrug. For now, I've added a -l option to enable logging via syslog, and only doing so if !only_changed_user, as with the existing -v and -o options. Also merged -R option for restorecon and -e option for setfiles, and removed tee'ing of output from fixfiles. But you still need to change fixfiles to use -l if that is what you want, and you have the same issue for restorecon.
Quoted 7 lines
I think we need a mechanism to tell us when a file actually has the wrong context versus one that just does not
match the context in thefile_contextfile. Russell pointed out that the contents of the homedirectory should
not have an automatic tool looking at their context because it might open up a security hole if you were to
arbitrarily clean up the false positives.
setfiles likely shouldn't be applied to /home at all after initial labeling.
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.