setfiles sorting
Hi,
I recently upgraded my UML setup to 2.6, and I created a new root filesystem using Debian woody and Brian May's "unstable" SELinux packages for woody.
However, even after running 'make relabel', a lot of the file contexts were wrong. For example, /sbin/getty was still sbin_t, even after explicitly running setfiles on it. I verified that the regexp for getty came after the generic sbin_t regexp in /etc/selinux/file_contexts/file_contexts.
Staring at the setfiles source code a bit, I came to the conclusion that this line was wrong:
Quoted 2 lines
/* Sort the specifications with most general first */ qsort(spec_arr, nspec, sizeof(struct spec), spec_compare);
As the libc manual says, qsort is not guaranteed to be a stable sort, and it apparently isn't. So I think what was happening is the getty_t rule was being randomly sorted before the generic sbin_t rule.
I don't see why the specifications should be sorted in that way; we should just go by the order in the file_contexts file. I guess I don't understand what the whole bit with looking for metacharacters was trying to accomplish in the first place.
Anyways, applying the attached patch made it work for me.
setfiles/setfiles.c +0 -12
@@ -437,15 +437,6 @@return i; } -/* Used with qsort to sort specs from lowest to highest hasMetaChars value */ -int spec_compare (const void * specA,const void * specB) -{ -return ( - ((const struct spec *)specB)->hasMetaChars - - ((const struct spec *)specA)->hasMetaChars - ); -} -/* * Check for duplicate specifications. If a duplicate specification is found * and the context is the same, give a warning to the user. If a duplicate
@@ -915,9 +906,6 @@ } }fclose (fp); - -/* Sort the specifications with most general first */ -qsort (spec_arr, nspec,sizeof (struct spec ), spec_compare);/* Verify no exact duplicates */ if (nodups_specs () !=0 ) {
Quoted 22 lines · Colin Walters
Hi,
I recently upgraded my UML setup to 2.6, and I created a new root filesystem using Debian woody and Brian May's "unstable" SELinux packages for woody.
However, even after running 'make relabel', a lot of the file contexts were wrong. For example,
/sbin/gettywas stillsbin_t, even after explicitly running setfiles on it. I verified that the regexp for getty came after the genericsbin_tregexp in/etc/selinux/file_contexts/file_contexts.Staring at the setfiles source code a bit, I came to the conclusion that this line was wrong:
Quoted 2 lines
/* Sort the specifications with most general first */ qsort(spec_arr, nspec, sizeof(struct spec), spec_compare);As the libc manual says, qsort is not guaranteed to be a stable sort, and it apparently isn't. So I think what was happening is the
getty_trule was being randomly sorted before the genericsbin_trule.
Quoted 6 lines
I don't see why the specifications should be sorted in that way; we should just go by the order in the
file_contextsfile. I guess I don't understand what the whole bit with looking for metacharacters was trying to accomplish in the first place.Anyways, applying the attached patch made it work for me.
Hi Colin,
Thanks for reporting this problem. The original problem that motivated the addition of this code to setfiles is described at
http://marc.theaimsgroup.com/?l=selinux&m=104488991028531&w=2, and
http://marc.theaimsgroup.com/?l=selinux&m=104768503417910&w=2. While we would prefer to just rely on the specified order, this does not address ordering issues among the program .fc files, and a specific case where this was creating a problem was the generic /usr/sbin/in\..*d entry in inetd.fc vs. specific entries in other program .fc files (e.g. ftpd, fingerd, etc). The intent of the change was to simply move exact specifications to the end of the list so that they are never overridden by more general regex specifications. I had advocated computing a metric of the generality of the pathname regex and ordering based on it, but that was viewed as problematic.
In any event, the fact that qsort does not guarantee stable ordering for entries that compare to equal does make it unsuitable for this purpose, but we still need to address the underlying problem, e.g. we could simply move the entries to the end directly without relying on qsort.
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 4 lines · Stephen Smalley
In any event, the fact that qsort does not guarantee stable ordering for entries that compare to equal does make it unsuitable for this purpose, but we still need to address the underlying problem, e.g. we could simply move the entries to the end directly without relying on qsort.
Patch below.
Index: policycoreutils/setfiles/setfiles.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/policycoreutils/setfiles/setfiles.c,v retrieving revision 1.12
setfiles/setfiles.c +21 -12
@@ -437,15 +437,6 @@return i; } -/* Used with qsort to sort specs from lowest to highest hasMetaChars value */ -int spec_compare (const void * specA,const void * specB) -{ -return ( - ((const struct spec *)specB)->hasMetaChars - - ((const struct spec *)specA)->hasMetaChars - ); -} -/* * Check for duplicate specifications. If a duplicate specification is found * and the context is the same, give a warning to the user. If a duplicate
@@ -685,7 +676,8 @@char line_buf[255 +1 ], *buf_p;char *regex, *type, *context;char *anchored_regex; -int opt, items, len, lineno, pass, regerr, i; +int opt, items, len, lineno, pass, regerr, i, j; +spec_t *spec_copy;/* Process any options. */ while ((opt =getopt (argc, argv,"dnqrsvW" )) >0 ) {
@@ -916,8 +908,25 @@ }fclose (fp); -/* Sort the specifications with most general first */ -qsort (spec_arr, nspec,sizeof (struct spec ), spec_compare); +/* Move exact pathname specifications to the end. */ + spec_copy =malloc (sizeof (spec_t) * nspec); +if (!spec_copy) { +fprintf (stderr, +"%s: insufficient memory for specifications\n" , + argv[0 ]); +exit (1 ); + } + j =0 ; +for (i =0 ; i < nspec; i++) { +if (spec_arr[i].hasMetaChars) +memcpy (&spec_copy[j++], &spec_arr[i],sizeof (spec_t)); + } +for (i =0 ; i < nspec; i++) { +if (!spec_arr[i].hasMetaChars) +memcpy (&spec_copy[j++], &spec_arr[i],sizeof (spec_t)); + } +free (spec_arr); + spec_arr = spec_copy;/* Verify no exact duplicates */ if (nodups_specs () !=0 ) {
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.