• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
4  */
5 
6 /* Updated: David Caplan, <dac@tresys.com>
7  *
8  * 	Added conditional policy language extensions
9  *
10  *          Jason Tang    <jtang@tresys.com>
11  *
12  *	Added support for binary policy modules
13  *
14  * Copyright (C) 2003-5 Tresys Technology, LLC
15  *	This program is free software; you can redistribute it and/or modify
16  *  	it under the terms of the GNU General Public License as published by
17  *	the Free Software Foundation, version 2.
18  */
19 
20 /* FLASK */
21 
22 %{
23 #include <sys/types.h>
24 #include <limits.h>
25 #include <stdint.h>
26 #include <string.h>
27 
28 typedef int (* require_func_t)();
29 
30 #ifdef ANDROID
31 #include "policy_parse.h"
32 #else
33 #include "y.tab.h"
34 #endif
35 
36 static char linebuf[2][255];
37 static unsigned int lno = 0;
38 int yywarn(char *msg);
39 
40 void set_source_file(const char *name);
41 
42 char source_file[PATH_MAX];
43 unsigned long source_lineno = 1;
44 
45 unsigned long policydb_lineno = 1;
46 
47 unsigned int policydb_errors = 0;
48 %}
49 
50 %option noinput nounput noyywrap
51 
52 %array
53 letter  [A-Za-z]
54 digit   [0-9]
55 alnum   [a-zA-Z0-9]
56 hexval	[0-9A-Fa-f]
57 
58 %%
59 \n.*				{ strncpy(linebuf[lno], yytext+1, 255);
60                                   linebuf[lno][254] = 0;
61                                   lno = 1 - lno;
62                                   policydb_lineno++;
63 				  source_lineno++;
64                                   yyless(1); }
65 CLONE |
66 clone				{ return(CLONE); }
67 COMMON |
68 common				{ return(COMMON); }
69 CLASS |
70 class				{ return(CLASS); }
71 CONSTRAIN |
72 constrain			{ return(CONSTRAIN); }
73 VALIDATETRANS |
74 validatetrans			{ return(VALIDATETRANS); }
75 INHERITS |
76 inherits			{ return(INHERITS); }
77 SID |
78 sid				{ return(SID); }
79 ROLE |
80 role				{ return(ROLE); }
81 ROLES |
82 roles				{ return(ROLES); }
83 ROLEATTRIBUTE |
84 roleattribute			{ return(ROLEATTRIBUTE);}
85 ATTRIBUTE_ROLE |
86 attribute_role			{ return(ATTRIBUTE_ROLE);}
87 TYPES |
88 types				{ return(TYPES); }
89 TYPEALIAS |
90 typealias			{ return(TYPEALIAS); }
91 TYPEATTRIBUTE |
92 typeattribute			{ return(TYPEATTRIBUTE); }
93 TYPEBOUNDS |
94 typebounds			{ return(TYPEBOUNDS); }
95 TYPE |
96 type				{ return(TYPE); }
97 BOOL |
98 bool                            { return(BOOL); }
99 TUNABLE |
100 tunable				{ return(TUNABLE); }
101 IF |
102 if				{ return(IF); }
103 ELSE |
104 else				{ return(ELSE); }
105 ALIAS |
106 alias				{ return(ALIAS); }
107 ATTRIBUTE |
108 attribute			{ return(ATTRIBUTE); }
109 TYPE_TRANSITION |
110 type_transition			{ return(TYPE_TRANSITION); }
111 TYPE_MEMBER |
112 type_member			{ return(TYPE_MEMBER); }
113 TYPE_CHANGE |
114 type_change			{ return(TYPE_CHANGE); }
115 ROLE_TRANSITION |
116 role_transition			{ return(ROLE_TRANSITION); }
117 RANGE_TRANSITION |
118 range_transition		{ return(RANGE_TRANSITION); }
119 SENSITIVITY |
120 sensitivity			{ return(SENSITIVITY); }
121 DOMINANCE |
122 dominance			{ return(DOMINANCE); }
123 CATEGORY |
124 category			{ return(CATEGORY); }
125 LEVEL |
126 level				{ return(LEVEL); }
127 RANGE |
128 range				{ return(RANGE); }
129 MLSCONSTRAIN |
130 mlsconstrain			{ return(MLSCONSTRAIN); }
131 MLSVALIDATETRANS |
132 mlsvalidatetrans		{ return(MLSVALIDATETRANS); }
133 USER |
134 user				{ return(USER); }
135 NEVERALLOW |
136 neverallow		        { return(NEVERALLOW); }
137 ALLOW |
138 allow			        { return(ALLOW); }
139 AUDITALLOW |
140 auditallow		        { return(AUDITALLOW); }
141 AUDITDENY |
142 auditdeny		        { return(AUDITDENY); }
143 DONTAUDIT |
144 dontaudit                       { return(DONTAUDIT); }
145 SOURCE |
146 source			        { return(SOURCE); }
147 TARGET |
148 target			        { return(TARGET); }
149 SAMEUSER |
150 sameuser			{ return(SAMEUSER);}
151 module|MODULE                   { return(MODULE); }
152 require|REQUIRE                 { return(REQUIRE); }
153 optional|OPTIONAL               { return(OPTIONAL); }
154 OR |
155 or     			        { return(OR);}
156 AND |
157 and				{ return(AND);}
158 NOT |
159 not				{ return(NOT);}
160 xor |
161 XOR                             { return(XOR); }
162 eq |
163 EQ				{ return(EQUALS);}
164 true |
165 TRUE                            { return(CTRUE); }
166 false |
167 FALSE                           { return(CFALSE); }
168 dom |
169 DOM				{ return(DOM);}
170 domby |
171 DOMBY				{ return(DOMBY);}
172 INCOMP |
173 incomp				{ return(INCOMP);}
174 fscon |
175 FSCON                           { return(FSCON);}
176 portcon |
177 PORTCON				{ return(PORTCON);}
178 netifcon |
179 NETIFCON			{ return(NETIFCON);}
180 nodecon |
181 NODECON				{ return(NODECON);}
182 pirqcon |
183 PIRQCON  		        { return(PIRQCON);}
184 iomemcon |
185 IOMEMCON            		{ return(IOMEMCON);}
186 ioportcon |
187 IOPORTCON           		{ return(IOPORTCON);}
188 pcidevicecon |
189 PCIDEVICECON           		{ return(PCIDEVICECON);}
190 fs_use_xattr |
191 FS_USE_XATTR			{ return(FSUSEXATTR);}
192 fs_use_task |
193 FS_USE_TASK                     { return(FSUSETASK);}
194 fs_use_trans |
195 FS_USE_TRANS                    { return(FSUSETRANS);}
196 genfscon |
197 GENFSCON                        { return(GENFSCON);}
198 r1 |
199 R1				{ return(R1); }
200 r2 |
201 R2				{ return(R2); }
202 r3 |
203 R3				{ return(R3); }
204 u1 |
205 U1				{ return(U1); }
206 u2 |
207 U2				{ return(U2); }
208 u3 |
209 U3				{ return(U3); }
210 t1 |
211 T1				{ return(T1); }
212 t2 |
213 T2				{ return(T2); }
214 t3 |
215 T3				{ return(T3); }
216 l1 |
217 L1				{ return(L1); }
218 l2 |
219 L2				{ return(L2); }
220 h1 |
221 H1				{ return(H1); }
222 h2 |
223 H2				{ return(H2); }
224 policycap |
225 POLICYCAP			{ return(POLICYCAP); }
226 permissive |
227 PERMISSIVE			{ return(PERMISSIVE); }
228 "/"({alnum}|[_\.\-/])*	        { return(PATH); }
229 \"({alnum}|[_\.\-\~])+\"	{ return(FILENAME); }
230 {letter}({alnum}|[_\-])*([\.]?({alnum}|[_\-]))*	{ return(IDENTIFIER); }
231 {alnum}*{letter}{alnum}*        { return(FILESYSTEM); }
232 {digit}+|0x{hexval}+            { return(NUMBER); }
233 {digit}{1,3}(\.{digit}{1,3}){3}    { return(IPV4_ADDR); }
234 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*  { return(IPV6_ADDR); }
235 {digit}+(\.({alnum}|[_.])*)?    { return(VERSION_IDENTIFIER); }
236 #line[ ]1[ ]\"[^\n]*\"		{ set_source_file(yytext+9); }
237 #line[ ]{digit}+	        { source_lineno = atoi(yytext+6)-1; }
238 #[^\n]*                         { /* delete comments */ }
239 [ \t\f]+			{ /* delete whitespace */ }
240 "==" 				{ return(EQUALS); }
241 "!="				{ return (NOTEQUAL); }
242 "&&"				{ return (AND); }
243 "||"				{ return (OR); }
244 "!"				{ return (NOT); }
245 "^"                             { return (XOR); }
246 "," |
247 ":" |
248 ";" |
249 "(" |
250 ")" |
251 "{" |
252 "}" |
253 "[" |
254 "-" |
255 "." |
256 "]" |
257 "~" |
258 "*"				{ return(yytext[0]); }
259 .                               { yywarn("unrecognized character");}
260 %%
261 int yyerror(char *msg)
262 {
263 	if (source_file[0])
264 		fprintf(stderr, "%s:%ld:",
265 			source_file, source_lineno);
266 	else
267 		fprintf(stderr, "(unknown source)::");
268 	fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n",
269 			msg,
270 			yytext,
271 			policydb_lineno,
272 			linebuf[0], linebuf[1]);
273 	policydb_errors++;
274 	return -1;
275 }
276 
277 int yywarn(char *msg)
278 {
279 	if (source_file[0])
280 		fprintf(stderr, "%s:%ld:",
281 			source_file, source_lineno);
282 	else
283 		fprintf(stderr, "(unknown source)::");
284 	fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n",
285 			msg,
286 			yytext,
287 			policydb_lineno,
288 			linebuf[0], linebuf[1]);
289 	return 0;
290 }
291 
292 void set_source_file(const char *name)
293 {
294 	source_lineno = 1;
295 	strncpy(source_file, name, sizeof(source_file)-1);
296 	source_file[sizeof(source_file)-1] = '\0';
297 }
298