Skip to contentSkip to conversation

setfiles performance

PATCH Russell Coker 1 participant 1 message Oct 2, 2003 SELinux security module

lore
Share & tools
lore
#0 lore raw

I have gone through all the file contexts entries and put in types wherever possible. I have also made a modified version of setfiles that checks the type before doing a regex check.

Here's a matrix of times:

                  old file contexts  |  new file contexts
setfiles-stem          83.2s         |      78.7s
setfiles-latest        83.5s         |      72.2s

The results are for the user time on a 2075MHz Athlon (XP 2600) with a root file system of 122461 Inodes.

Here's a matrix for a P2-400 with 168477 Inodes:

                  old file contexts  |  new file contexts
setfiles-stem         202.6s         |      201.8s
setfiles-latest       199.7s         |      181.7s

setfiles-stem is setfiles with the stem compression patch, setfiles-latest is with stem compression and the type check.

When adding type specifiers to most entries in the file_contexts I changed the labeling of some nodes. For consistent testing I did not want any relabeling so I added some matching entries to the old file contexts file, so what I describe as "old file contexts" is something like 5% of the way towards having all the types specified (as it specifies some of the most important entries). Therefore the performance difference between the old and new file contexts is not as great as it might be. I believe that this is why the -latest version is slightly faster than the -stem version for the "old file contexts" on the P2-400. I can only guess that the relative cost of a regex on an Athlon is not as great as on a P2-400.

I can't understand why the new file contexts would give marginally better performance than the old file contexts with setfiles-stem. However I have them both providing the same results (setfiles -v indicates that when I run one and then the other nothing changes), so the results are OK. I have tested this on both Debian and Red Hat machines (very important, some file names are different).

The new setfiles is between 8% and 14% faster with the new file contexts depending on how you are counting. This combined with the fact that the file contexts conversion resulted in finding some minor bugs in file specs (and makes it less likely that such bugs will appear in future) makes me reasonably happy with the result. Also my work in changing the file contexts is not complete as I don't know the types of some of the files in question, I expect that with some more changes to the file_contexts we could make it another few percent faster.

NB The Athlon runs the old SE Linux, but I don't think that affects the results.

I have attached a patch for the setfiles in new SE Linux named setfiles.diff, and a patch for the setfiles in the old SE Linux named setfiles-old.diff. Both patches expect that my stem compression patch will already be applied.

I have also attached my latest file_contexts tree as it's easier to attach an archive than a huge diff file. It does reference some a few types that aren't defined in the policy yet, so a few minor tweaks will be needed if you want to just replace your file contexts.

Signature · 4 lines
http://www.coker.com.au/selinux/   My NSA Security Enhanced Linux packages
http://www.coker.com.au/bonnie++/  Bonnie++ hard drive benchmark
http://www.coker.com.au/postal/    Postal SMTP/POP benchmark
http://www.coker.com.au/~russell/  My home page
setfiles.c renamed from setfiles.new/setfiles.c +17 -15
diff -ru ../setfiles.new/setfiles.c ./setfiles.c
@@ -383,26 +383,28 @@
 	 */
 	for (i = nspec - 1; i >= 0; i--)
 	{
-		if(spec[i].stem_id == -1 || spec[i].stem_id == file_stem)
+		/* if the spec in question matches no stem or has the same
+		 * stem as the file AND if the spec in question has no mode
+		 * specified or if the mode matches the file mode then we do
+		 * a regex check	*/
+		if( (spec[i].stem_id == -1 || spec[i].stem_id == file_stem)
+		  && (!spec[i].mode || ( (sb->st_mode & S_IFMT) == spec[i].mode ) ) )
 		{
 			if(spec[i].stem_id == -1)
 				ret = regexec(&spec[i].regex, name, 0, NULL, 0);
 			else
 				ret = regexec(&spec[i].regex, buf, 0, NULL, 0);
-			if (ret == 0 &&
-		    	(!spec[i].mode
-		     	|| (sb->st_mode & S_IFMT) == spec[i].mode)) break;
-			if (ret) {
-				if (ret == REG_NOMATCH)
-					continue;
-				regerror(ret, &spec[i].regex, errbuf,
-				 	sizeof errbuf);
-				fprintf(stderr,
-					"%s:  unable to match %s against %s:  %s\n",
-					progname, name, spec[i].regex_str,
-					errbuf);
-				return -1;
-			}
+			if (ret == 0)
+				break;
+
+			if (ret == REG_NOMATCH)
+				continue;
+			/* else it's an error */
+			regerror(ret, &spec[i].regex, errbuf, sizeof errbuf);
+			fprintf(stderr,
+				"%s:  unable to match %s against %s:  %s\n",
+				progname, name, spec[i].regex_str, errbuf);
+			return -1;
 		}
 	}
 
setfiles.c renamed from home/rjc/selinux/selinux-small-2003081307/setfiles/setfiles.c +16 -15
diff -ru /home/rjc/selinux/selinux-small-2003081307/setfiles/setfiles.c ./setfiles.c
@@ -419,26 +419,27 @@
 	 */
 	for (i = nspec - 1; i >= 0; i--)
 	{
-		if(spec[i].stem_id == -1 || spec[i].stem_id == file_stem)
+		/* if the spec in question matches no stem or has the same
+		 * stem as the file AND if the spec in question has no mode
+		 * specified or if the mode matches the file mode then we do
+		 * a regex check        */
+		if( (spec[i].stem_id == -1 || spec[i].stem_id == file_stem)
+		  && (!spec[i].mode || ( (sb->st_mode & S_IFMT) == spec[i].mode ) ) )
+
 		{
 			if(spec[i].stem_id == -1)
 				ret = regexec(&spec[i].regex, name, 0, NULL, 0);
 			else
 				ret = regexec(&spec[i].regex, buf, 0, NULL, 0);
-			if (ret == 0 &&
-		    	(!spec[i].mode
-		     	|| (sb->st_mode & S_IFMT) == spec[i].mode)) break;
-			if (ret) {
-				if (ret == REG_NOMATCH)
-					continue;
-				regerror(ret, &spec[i].regex, errbuf,
-				 	sizeof errbuf);
-				fprintf(stderr,
-					"%s:  unable to match %s against %s:  %s\n",
-					progname, name, spec[i].regex_str,
-					errbuf);
-				return -1;
-			}
+			if (ret == 0)
+				break;
+			if (ret == REG_NOMATCH)
+				continue;
+			regerror(ret, &spec[i].regex, errbuf, sizeof errbuf);
+			fprintf(stderr,
+				"%s:  unable to match %s against %s:  %s\n",
+				progname, name, spec[i].regex_str, errbuf);
+			return -1;
 		}
 	}