Skip to contentSkip to conversation

add source line numbers to checkpolicy

PATCH In review Colin Walters 2 participants 4 messages Dec 15, 2003 – Dec 17, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add source line numbers to checkpolicy

Hi,

Attached are two patches which have checkpolicy emit the source line number where an error occurred, if available. This would have been fairly trivial, except it required rewriting the sample policy Makefile to not invoke m4 multiple times.

It all seems to work fine, but the changes to the Makefile should probably be looked at carefully - I don't understand why it wasn't written like this in the first place, so I'm wondering if I'm missing something.

Here's what it looks like:

walters@nexus> echo spork >> domains/program/mount.te
walters@nexus> make policy CHECKPOLICY=../checkpolicy-1.4/checkpolicy mkdir -p tmp
[...]
mv policy.conf.tmp policy.conf
../checkpolicy-1.4/checkpolicy -o policy.15 policy.conf ../checkpolicy-1.4/checkpolicy: loading policy configuration from policy.conf domains/program/mount.te:62:ERROR 'syntax error' at token 'spork' on line 22895:
allow mount_t unlabeled_t:dir search;
spork
../checkpolicy-1.4/checkpolicy: error(s) encountered while parsing configuration
make: *** [policy.15] Error 1

checkpolicy.c +4 -0
diff -ur checkpolicy-1.4.orig/checkpolicy.c checkpolicy-1.4/checkpolicy.c
@@ -45,6 +45,8 @@
 extern queue_t id_queue;
 extern unsigned int policydb_errors;
 extern unsigned long policydb_lineno;
+extern unsigned long source_lineno;
+extern char source_file[];
 extern unsigned int pass;
 extern unsigned int ss_initialized;
 
@@ -400,6 +402,8 @@
 		}
 		rewind(yyin);
 		policydb_lineno = 1;
+		source_file[0] = '\0';
+		source_lineno = 1;
 		yyrestart(yyin);
 		pass = 2;
 		if (yyparse() || policydb_errors) {
policy_parse.y +2 -0
diff -ur checkpolicy-1.4.orig/policy_parse.y checkpolicy-1.4/policy_parse.y
@@ -18,6 +18,8 @@
 policydb_t *policydbp;
 queue_t id_queue = 0;
 unsigned int pass;
+char *curfile = 0;
+unsigned int curline; 
 
 extern unsigned long policydb_lineno;
 
policy_scan.l +16 -2
diff -ur checkpolicy-1.4.orig/policy_scan.l checkpolicy-1.4/policy_scan.l
@@ -11,6 +11,9 @@
 static char linebuf[2][255];
 static unsigned int lno = 0;
 
+char source_file[255];
+unsigned long source_lineno = 1;
+
 unsigned long policydb_lineno = 1;
 
 unsigned int policydb_errors = 0;
@@ -23,6 +26,7 @@
                                   linebuf[lno][254] = 0;
                                   lno = 1 - lno; 
                                   policydb_lineno++;
+				  source_lineno++;
                                   yyless(1); }
 CLONE |
 clone				{ return(CLONE); }
@@ -130,6 +134,8 @@
 {letter}({letter}|{digit}|_)*	{ return(IDENTIFIER); }
 {letter}({letter}|{digit}|_|"."|"-")*	{ return(USER_IDENTIFIER); }
 {digit}{digit}*                 { return(NUMBER); }
+#line[ ]1[ ]\"[^\n]*\"		{ source_lineno = 1; strncpy(source_file, yytext+9, 255); source_file[strlen(source_file)-1] = '\0'; }
+#line[ ]{digit}{digit}*		{ source_lineno = atoi(yytext+6)-1; }
 #[^\n]*                         { /* delete comments */ }
 [ \t\f]+			{ /* delete whitespace */ }
 "==" 				{ return(EQUALS); }
@@ -154,6 +160,11 @@
 %%
 int yyerror(char *msg)
 {
+	if (source_file[0])
+		fprintf(stderr, "%s:%ld:",
+			source_file, source_lineno);
+	else
+		fprintf(stderr, "(unknown source)::");
 	fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n",
 			msg,
 			yytext,
@@ -165,6 +176,11 @@
 
 int yywarn(char *msg)
 {
+	if (source_file[0])
+		fprintf(stderr, "%s:%ld:",
+			source_file, source_lineno);
+	else
+		fprintf(stderr, "(unknown source)::");
 	fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n",
 			msg,
 			yytext,
@@ -172,5 +188,3 @@
 			linebuf[0], linebuf[1]);
 	return 0;
 }
-
-
Makefile +17 -31

79 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Colin Walters

Attached are two patches which have checkpolicy emit the source line number where an error occurred, if available. This would have been fairly trivial, except it required rewriting the sample policy Makefile to not invoke m4 multiple times.

It all seems to work fine, but the changes to the Makefile should probably be looked at carefully - I don't understand why it wasn't written like this in the first place, so I'm wondering if I'm missing something.

Thanks, this looks useful. If I recall correctly, we originally split up the m4 processing because we were encountering a m4 failure due to the number of arguments. I'm not sure what version of m4 that was, but the m4 on RHEL3 and FC1 appear to work fine with your patch and all existing .te files enabled, so perhaps that limitation was eliminated. Given this change, I would think that we could also factor out tmp/program_unused_flags.te and $(ALL_MACROS) from ALLTEFILES and CONSTRAINT_CONTEXT_MACRO_FILES and only include them once in POLICYFILES prior to TE_RBAC_FILES.

The fact that you retain the policy.conf line information as well as the source line information is important, as some tools now use that information (e.g. Tresys setools), and since the source file and line information doesn't deal with macro expansion.

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.
#2 lore raw
Quoted 5 lines · Stephen Smalley

Thanks, this looks useful. If I recall correctly, we originally split up the m4 processing because we were encountering a m4 failure due to the number of arguments. I'm not sure what version of m4 that was, but the m4 on RHEL3 and FC1 appear to work fine with your patch and all existing .te files enabled, so perhaps that limitation was eliminated.

Ah, I see.

Quoted 4 lines

Given this change, I would think that we could also factor out tmp/program_unused_flags.te and $(ALL_MACROS) from ALLTEFILES and CONSTRAINT_CONTEXT_MACRO_FILES and only include them once in POLICYFILES prior to TE_RBAC_FILES.

Ok, right. New patch attached.

Makefile +16 -31
@@ -29,13 +29,20 @@
 SRCINSTALLDIR = $(INSTALLDIR)/src
 POLICYCONF = $(SRCINSTALLDIR)/policy.conf
 
+ALL_PROGRAM_MACROS := $(wildcard macros/program/*.te)
+ALL_MACROS := $(ALL_PROGRAM_MACROS) $(wildcard macros/*.te)
+ALL_TYPES := $(wildcard types/*.te)
+ALL_DOMAINS := $(wildcard domains/*.te domains/misc/*.te domains/program/*.te)
+ALLTEFILES := attrib.te tmp/program_used_flags.te $(ALL_MACROS) $(ALL_TYPES) $(ALL_DOMAINS) assert.te 
+TE_RBAC_FILES := $(ALLTEFILES) rbac
+
 POLICYFILES = $(addprefix $(FLASKDIR),security_classes initial_sids access_vectors)
 ifeq ($(MLS),y)
 POLICYFILES += mls
 endif
-POLICYFILES += tmp/te-rbac.m4
+POLICYFILES += $(TE_RBAC_FILES)
 POLICYFILES += users 
-POLICYFILES += tmp/constraints-contexts.m4
+POLICYFILES += constraints initial_sid_contexts fs_use genfs_contexts net_contexts
 
 UNUSED_TE_FILES := $(wildcard domains/program/unused/*.te)
 
@@ -86,7 +93,8 @@
 
 policy.conf: $(POLICYFILES) 
 	mkdir -p tmp
-	cat $^ > $@
+	m4 $(M4PARAM) -Imacros -s $^ > [email protected]
+	mv [email protected] $@
 
 install-src: 
 	rm -rf $(SRCINSTALLDIR)/policy.old
@@ -94,35 +102,11 @@
 	mkdir -p $(SRCINSTALLDIR)/policy
 	cp -R . $(SRCINSTALLDIR)/policy
 
-ALLTEFILES := attrib.te tmp/program_used_flags.te tmp/all_macros.te tmp/all_types.te tmp/all_domains.te assert.te 
-TE_RBAC_MACRO_FILES := tmp/all.te rbac
-CONSTRAINT_CONTEXT_MACRO_FILES := tmp/program_used_flags.te tmp/all_macros.te constraints initial_sid_contexts fs_use genfs_contexts net_contexts
-
-tmp/te-rbac.m4: $(TE_RBAC_MACRO_FILES)
-	m4 $(M4PARAM) -Imacros -s $^ > $@
-
-tmp/constraints-contexts.m4: $(CONSTRAINT_CONTEXT_MACRO_FILES)
-	m4 $(M4PARAM) -Imacros -s $^ > $@
-
-tmp/all.te: $(ALLTEFILES)
-	cat $^ > $@
-
 tmp/program_used_flags.te: $(wildcard domains/program/*.te) domains/program
 	mkdir -p tmp
-	( cd domains/program/ ; for n in *.te ; do echo "define(\`$$n')"; done ) > $@
-	( cd domains/misc/ ; for n in *.te ; do echo "define(\`$$n')"; done ) >> $@
-
-tmp/all_macros.te:  macros $(wildcard macros/*.te) tmp/all_program_macros.te
-	cat $(wildcard macros/*.te) tmp/all_program_macros.te > $@
-
-tmp/all_program_macros.te: macros/program $(wildcard macros/program/*.te)
-	cat $(wildcard macros/program/*.te) > $@
-
-tmp/all_types.te: $(wildcard types/*.te) types
-	cat types/*.te > $@
-
-tmp/all_domains.te: $(wildcard domains/*.te domains/misc/*.te domains/program/*.te) domains domains/misc domains/program
-	cat domains/*.te domains/misc/*.te domains/program/*.te > $@
+	( cd domains/program/ ; for n in *.te ; do echo "define(\`$$n')"; done ) > [email protected]
+	( cd domains/misc/ ; for n in *.te ; do echo "define(\`$$n')"; done ) >> [email protected]
+	mv [email protected] $@
 
 relabel:  $(FC) $(SETFILES)
 	$(SETFILES) $(FC) `mount | awk '/ext[23]/{print $$3}'`
@@ -134,7 +118,8 @@
 	mkdir -p file_contexts/misc
 
 $(FC): $(FCFILES) domains/program domains/misc file_contexts/program file_contexts/misc
-	cat $(FCFILES) > $@
+	cat $(FCFILES) > [email protected]
+	mv [email protected] $@
 
 clean:
 	rm -f policy.conf
#3 lore raw
In reply to Colin Walters
Quoted 4 lines · Colin Walters

Attached are two patches which have checkpolicy emit the source line number where an error occurred, if available. This would have been fairly trivial, except it required rewriting the sample policy Makefile to not invoke m4 multiple times.

Thanks, the checkpolicy patch and the updated policy Makefile patch have been merged into our CVS tree.

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.