• 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)(void);
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(const 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 ALLOWXPERM |
146 allowxperm			{ return(ALLOWXPERM); }
147 AUDITALLOWXPERM |
148 auditallowxperm			{ return(AUDITALLOWXPERM); }
149 DONTAUDITXPERM |
150 dontauditxperm			{ return(DONTAUDITXPERM); }
151 NEVERALLOWXPERM |
152 neverallowxperm			{ return(NEVERALLOWXPERM); }
153 SOURCE |
154 source			        { return(SOURCE); }
155 TARGET |
156 target			        { return(TARGET); }
157 SAMEUSER |
158 sameuser			{ return(SAMEUSER);}
159 module|MODULE                   { return(MODULE); }
160 require|REQUIRE                 { return(REQUIRE); }
161 optional|OPTIONAL               { return(OPTIONAL); }
162 OR |
163 or     			        { return(OR);}
164 AND |
165 and				{ return(AND);}
166 NOT |
167 not				{ return(NOT);}
168 xor |
169 XOR                             { return(XOR); }
170 eq |
171 EQ				{ return(EQUALS);}
172 true |
173 TRUE                            { return(CTRUE); }
174 false |
175 FALSE                           { return(CFALSE); }
176 dom |
177 DOM				{ return(DOM);}
178 domby |
179 DOMBY				{ return(DOMBY);}
180 INCOMP |
181 incomp				{ return(INCOMP);}
182 fscon |
183 FSCON                           { return(FSCON);}
184 portcon |
185 PORTCON				{ return(PORTCON);}
186 netifcon |
187 NETIFCON			{ return(NETIFCON);}
188 nodecon |
189 NODECON				{ return(NODECON);}
190 pirqcon |
191 PIRQCON  		        { return(PIRQCON);}
192 iomemcon |
193 IOMEMCON            		{ return(IOMEMCON);}
194 ioportcon |
195 IOPORTCON           		{ return(IOPORTCON);}
196 pcidevicecon |
197 PCIDEVICECON           		{ return(PCIDEVICECON);}
198 devicetreecon |
199 DEVICETREECON           	{ return(DEVICETREECON);}
200 fs_use_xattr |
201 FS_USE_XATTR			{ return(FSUSEXATTR);}
202 fs_use_task |
203 FS_USE_TASK                     { return(FSUSETASK);}
204 fs_use_trans |
205 FS_USE_TRANS                    { return(FSUSETRANS);}
206 genfscon |
207 GENFSCON                        { return(GENFSCON);}
208 r1 |
209 R1				{ return(R1); }
210 r2 |
211 R2				{ return(R2); }
212 r3 |
213 R3				{ return(R3); }
214 u1 |
215 U1				{ return(U1); }
216 u2 |
217 U2				{ return(U2); }
218 u3 |
219 U3				{ return(U3); }
220 t1 |
221 T1				{ return(T1); }
222 t2 |
223 T2				{ return(T2); }
224 t3 |
225 T3				{ return(T3); }
226 l1 |
227 L1				{ return(L1); }
228 l2 |
229 L2				{ return(L2); }
230 h1 |
231 H1				{ return(H1); }
232 h2 |
233 H2				{ return(H2); }
234 policycap |
235 POLICYCAP			{ return(POLICYCAP); }
236 permissive |
237 PERMISSIVE			{ return(PERMISSIVE); }
238 default_user |
239 DEFAULT_USER			{ return(DEFAULT_USER); }
240 default_role |
241 DEFAULT_ROLE			{ return(DEFAULT_ROLE); }
242 default_type |
243 DEFAULT_TYPE			{ return(DEFAULT_TYPE); }
244 default_range |
245 DEFAULT_RANGE			{ return(DEFAULT_RANGE); }
246 low-high |
247 LOW-HIGH			{ return(LOW_HIGH); }
248 high |
249 HIGH				{ return(HIGH); }
250 low |
251 LOW				{ return(LOW); }
252 "/"({alnum}|[_\.\-/])*	        { return(PATH); }
253 \""/"[ !#-~]*\" 		{ return(QPATH); }
254 \"({alnum}|[_\.\-\+\~\: ])+\"	{ return(FILENAME); }
255 {letter}({alnum}|[_\-])*([\.]?({alnum}|[_\-]))*	{ return(IDENTIFIER); }
256 {digit}+|0x{hexval}+            { return(NUMBER); }
257 {alnum}*{letter}{alnum}*        { return(FILESYSTEM); }
258 {digit}{1,3}(\.{digit}{1,3}){3}    { return(IPV4_ADDR); }
259 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*  { return(IPV6_ADDR); }
260 {digit}+(\.({alnum}|[_.])*)?    { return(VERSION_IDENTIFIER); }
261 #line[ ]1[ ]\"[^\n]*\"		{ set_source_file(yytext+9); }
262 #line[ ]{digit}+	        { source_lineno = atoi(yytext+6)-1; }
263 #[^\n]*                         { /* delete comments */ }
264 [ \t\f]+			{ /* delete whitespace */ }
265 "==" 				{ return(EQUALS); }
266 "!="				{ return (NOTEQUAL); }
267 "&&"				{ return (AND); }
268 "||"				{ return (OR); }
269 "!"				{ return (NOT); }
270 "^"                             { return (XOR); }
271 "," |
272 ":" |
273 ";" |
274 "(" |
275 ")" |
276 "{" |
277 "}" |
278 "[" |
279 "-" |
280 "." |
281 "]" |
282 "~" |
283 "*"				{ return(yytext[0]); }
284 .                               { yywarn("unrecognized character");}
285 %%
286 int yyerror(const char *msg)
287 {
288 	if (source_file[0])
289 		fprintf(stderr, "%s:%ld:",
290 			source_file, source_lineno);
291 	else
292 		fprintf(stderr, "(unknown source)::");
293 	fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n",
294 			msg,
295 			yytext,
296 			policydb_lineno,
297 			linebuf[0], linebuf[1]);
298 	policydb_errors++;
299 	return -1;
300 }
301 
302 int yywarn(const char *msg)
303 {
304 	if (source_file[0])
305 		fprintf(stderr, "%s:%ld:",
306 			source_file, source_lineno);
307 	else
308 		fprintf(stderr, "(unknown source)::");
309 	fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n",
310 			msg,
311 			yytext,
312 			policydb_lineno,
313 			linebuf[0], linebuf[1]);
314 	return 0;
315 }
316 
317 void set_source_file(const char *name)
318 {
319 	source_lineno = 1;
320 	strncpy(source_file, name, sizeof(source_file)-1);
321 	source_file[sizeof(source_file)-1] = '\0';
322 	if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
323 		source_file[strlen(source_file)-1] = '\0';
324 }
325