policy_parse.y revision b42e15ffd5163effe3b2cb910685a5956a00defc
1
2/*
3 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
4 */
5
6/*
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
8 *
9 *	Support for enhanced MLS infrastructure.
10 *
11 * Updated: David Caplan, <dac@tresys.com>
12 *
13 * 	Added conditional policy language extensions
14 *
15 * Updated: Joshua Brindle <jbrindle@tresys.com>
16 *	    Karl MacMillan <kmacmillan@mentalrootkit.com>
17 *          Jason Tang     <jtang@tresys.com>
18 *
19 *	Added support for binary policy modules
20 *
21 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2008 Tresys Technology, LLC
23 * Copyright (C) 2007 Red Hat Inc.
24 *	This program is free software; you can redistribute it and/or modify
25 *  	it under the terms of the GNU General Public License as published by
26 *	the Free Software Foundation, version 2.
27 */
28
29/* FLASK */
30
31%{
32#include <sys/types.h>
33#include <assert.h>
34#include <stdarg.h>
35#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/socket.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <stdlib.h>
43
44#include <sepol/policydb/expand.h>
45#include <sepol/policydb/policydb.h>
46#include <sepol/policydb/services.h>
47#include <sepol/policydb/conditional.h>
48#include <sepol/policydb/flask.h>
49#include <sepol/policydb/hierarchy.h>
50#include <sepol/policydb/polcaps.h>
51#include "queue.h"
52#include "checkpolicy.h"
53#include "module_compiler.h"
54#include "policy_define.h"
55
56extern policydb_t *policydbp;
57extern unsigned int pass;
58
59extern char yytext[];
60extern int yylex(void);
61extern int yywarn(char *msg);
62extern int yyerror(char *msg);
63
64typedef int (* require_func_t)();
65
66%}
67
68%union {
69	unsigned int val;
70	uintptr_t valptr;
71	void *ptr;
72        require_func_t require_func;
73}
74
75%type <ptr> cond_expr cond_expr_prim cond_pol_list cond_else
76%type <ptr> cond_allow_def cond_auditallow_def cond_auditdeny_def cond_dontaudit_def
77%type <ptr> cond_transition_def cond_te_avtab_def cond_rule_def
78%type <ptr> role_def roles
79%type <valptr> cexpr cexpr_prim op role_mls_op
80%type <val> ipv4_addr_def number
81%type <require_func> require_decl_def
82
83%token PATH
84%token FILENAME
85%token CLONE
86%token COMMON
87%token CLASS
88%token CONSTRAIN
89%token VALIDATETRANS
90%token INHERITS
91%token SID
92%token ROLE
93%token ROLES
94%token TYPEALIAS
95%token TYPEATTRIBUTE
96%token TYPEBOUNDS
97%token TYPE
98%token TYPES
99%token ALIAS
100%token ATTRIBUTE
101%token BOOL
102%token IF
103%token ELSE
104%token TYPE_TRANSITION
105%token TYPE_MEMBER
106%token TYPE_CHANGE
107%token ROLE_TRANSITION
108%token RANGE_TRANSITION
109%token SENSITIVITY
110%token DOMINANCE
111%token DOM DOMBY INCOMP
112%token CATEGORY
113%token LEVEL
114%token RANGE
115%token MLSCONSTRAIN
116%token MLSVALIDATETRANS
117%token USER
118%token NEVERALLOW
119%token ALLOW
120%token AUDITALLOW
121%token AUDITDENY
122%token DONTAUDIT
123%token SOURCE
124%token TARGET
125%token SAMEUSER
126%token FSCON PORTCON NETIFCON NODECON
127%token PIRQCON IOMEMCON IOPORTCON PCIDEVICECON
128%token FSUSEXATTR FSUSETASK FSUSETRANS
129%token GENFSCON
130%token U1 U2 U3 R1 R2 R3 T1 T2 T3 L1 L2 H1 H2
131%token NOT AND OR XOR
132%token CTRUE CFALSE
133%token IDENTIFIER
134%token NUMBER
135%token EQUALS
136%token NOTEQUAL
137%token IPV4_ADDR
138%token IPV6_ADDR
139%token MODULE VERSION_IDENTIFIER REQUIRE OPTIONAL
140%token POLICYCAP
141%token PERMISSIVE
142
143%left OR
144%left XOR
145%left AND
146%right NOT
147%left EQUALS NOTEQUAL
148%%
149policy			: base_policy
150                        | module_policy
151                        ;
152base_policy             : { if (define_policy(pass, 0) == -1) return -1; }
153                          classes initial_sids access_vectors
154                          { if (pass == 1) { if (policydb_index_classes(policydbp)) return -1; }
155                            else if (pass == 2) { if (policydb_index_others(NULL, policydbp, 0)) return -1; }}
156			  opt_mls te_rbac users opt_constraints
157                         { if (pass == 1) { if (policydb_index_bools(policydbp)) return -1;}
158			   else if (pass == 2) { if (policydb_index_others(NULL, policydbp, 0)) return -1;}}
159			  initial_sid_contexts opt_fs_contexts opt_fs_uses opt_genfs_contexts net_contexts opt_dev_contexts
160			;
161classes			: class_def
162			| classes class_def
163			;
164class_def		: CLASS identifier
165			{if (define_class()) return -1;}
166			;
167initial_sids 		: initial_sid_def
168			| initial_sids initial_sid_def
169			;
170initial_sid_def		: SID identifier
171                        {if (define_initial_sid()) return -1;}
172			;
173access_vectors		: opt_common_perms av_perms
174			;
175opt_common_perms        : common_perms
176                        |
177                        ;
178common_perms		: common_perms_def
179			| common_perms common_perms_def
180			;
181common_perms_def	: COMMON identifier '{' identifier_list '}'
182			{if (define_common_perms()) return -1;}
183			;
184av_perms		: av_perms_def
185			| av_perms av_perms_def
186			;
187av_perms_def		: CLASS identifier '{' identifier_list '}'
188			{if (define_av_perms(FALSE)) return -1;}
189                        | CLASS identifier INHERITS identifier
190			{if (define_av_perms(TRUE)) return -1;}
191                        | CLASS identifier INHERITS identifier '{' identifier_list '}'
192			{if (define_av_perms(TRUE)) return -1;}
193			;
194opt_mls			: mls
195                        |
196			;
197mls			: sensitivities dominance opt_categories levels mlspolicy
198			;
199sensitivities	 	: sensitivity_def
200			| sensitivities sensitivity_def
201			;
202sensitivity_def		: SENSITIVITY identifier alias_def ';'
203			{if (define_sens()) return -1;}
204			| SENSITIVITY identifier ';'
205			{if (define_sens()) return -1;}
206	                ;
207alias_def		: ALIAS names
208			;
209dominance		: DOMINANCE identifier
210			{if (define_dominance()) return -1;}
211                        | DOMINANCE '{' identifier_list '}'
212			{if (define_dominance()) return -1;}
213			;
214opt_categories          : categories
215                        |
216                        ;
217categories 		: category_def
218			| categories category_def
219			;
220category_def		: CATEGORY identifier alias_def ';'
221			{if (define_category()) return -1;}
222			| CATEGORY identifier ';'
223			{if (define_category()) return -1;}
224			;
225levels	 		: level_def
226			| levels level_def
227			;
228level_def		: LEVEL identifier ':' id_comma_list ';'
229			{if (define_level()) return -1;}
230			| LEVEL identifier ';'
231			{if (define_level()) return -1;}
232			;
233mlspolicy		: mlspolicy_decl
234			| mlspolicy mlspolicy_decl
235			;
236mlspolicy_decl		: mlsconstraint_def
237			| mlsvalidatetrans_def
238			;
239mlsconstraint_def	: MLSCONSTRAIN names names cexpr ';'
240			{ if (define_constraint((constraint_expr_t*)$4)) return -1; }
241			;
242mlsvalidatetrans_def	: MLSVALIDATETRANS names cexpr ';'
243			{ if (define_validatetrans((constraint_expr_t*)$3)) return -1; }
244			;
245te_rbac			: te_rbac_decl
246			| te_rbac te_rbac_decl
247			;
248te_rbac_decl		: te_decl
249			| rbac_decl
250                        | cond_stmt_def
251			| optional_block
252			| policycap_def
253			| ';'
254                        ;
255rbac_decl		: role_type_def
256                        | role_dominance
257                        | role_trans_def
258 			| role_allow_def
259			;
260te_decl			: attribute_def
261                        | type_def
262                        | typealias_def
263                        | typeattribute_def
264                        | typebounds_def
265                        | bool_def
266                        | transition_def
267                        | range_trans_def
268                        | te_avtab_def
269			| permissive_def
270			;
271attribute_def           : ATTRIBUTE identifier ';'
272                        { if (define_attrib()) return -1;}
273                        ;
274type_def		: TYPE identifier alias_def opt_attr_list ';'
275                        {if (define_type(1)) return -1;}
276	                | TYPE identifier opt_attr_list ';'
277                        {if (define_type(0)) return -1;}
278    			;
279typealias_def           : TYPEALIAS identifier alias_def ';'
280			{if (define_typealias()) return -1;}
281			;
282typeattribute_def	: TYPEATTRIBUTE identifier id_comma_list ';'
283			{if (define_typeattribute()) return -1;}
284			;
285typebounds_def          : TYPEBOUNDS identifier id_comma_list ';'
286                        {if (define_typebounds()) return -1;}
287                        ;
288opt_attr_list           : ',' id_comma_list
289			|
290			;
291bool_def                : BOOL identifier bool_val ';'
292                        {if (define_bool()) return -1;}
293                        ;
294bool_val                : CTRUE
295 			{ if (insert_id("T",0)) return -1; }
296                        | CFALSE
297			{ if (insert_id("F",0)) return -1; }
298                        ;
299cond_stmt_def           : IF cond_expr '{' cond_pol_list '}' cond_else
300                        { if (pass == 2) { if (define_conditional((cond_expr_t*)$2, (avrule_t*)$4, (avrule_t*)$6) < 0) return -1;  }}
301                        ;
302cond_else		: ELSE '{' cond_pol_list '}'
303			{ $$ = $3; }
304			| /* empty */
305			{ $$ = NULL; }
306cond_expr               : '(' cond_expr ')'
307			{ $$ = $2;}
308			| NOT cond_expr
309			{ $$ = define_cond_expr(COND_NOT, $2, 0);
310			  if ($$ == 0) return -1; }
311			| cond_expr AND cond_expr
312			{ $$ = define_cond_expr(COND_AND, $1, $3);
313			  if ($$ == 0) return  -1; }
314			| cond_expr OR cond_expr
315			{ $$ = define_cond_expr(COND_OR, $1, $3);
316			  if ($$ == 0) return   -1; }
317			| cond_expr XOR cond_expr
318			{ $$ = define_cond_expr(COND_XOR, $1, $3);
319			  if ($$ == 0) return  -1; }
320			| cond_expr EQUALS cond_expr
321			{ $$ = define_cond_expr(COND_EQ, $1, $3);
322			  if ($$ == 0) return  -1; }
323			| cond_expr NOTEQUAL cond_expr
324			{ $$ = define_cond_expr(COND_NEQ, $1, $3);
325			  if ($$ == 0) return  -1; }
326			| cond_expr_prim
327			{ $$ = $1; }
328			;
329cond_expr_prim          : identifier
330                        { $$ = define_cond_expr(COND_BOOL,0, 0);
331			  if ($$ == COND_ERR) return   -1; }
332                        ;
333cond_pol_list           : cond_pol_list cond_rule_def
334                        { $$ = define_cond_pol_list((avrule_t *)$1, (avrule_t *)$2); }
335			| /* empty */
336			{ $$ = NULL; }
337			;
338cond_rule_def           : cond_transition_def
339                        { $$ = $1; }
340                        | cond_te_avtab_def
341                        { $$ = $1; }
342			| require_block
343			{ $$ = NULL; }
344                        ;
345cond_transition_def	: TYPE_TRANSITION names names ':' names identifier filename ';'
346                        { $$ = define_cond_filename_trans() ;
347                          if ($$ == COND_ERR) return -1;}
348			| TYPE_TRANSITION names names ':' names identifier ';'
349                        { $$ = define_cond_compute_type(AVRULE_TRANSITION) ;
350                          if ($$ == COND_ERR) return -1;}
351                        | TYPE_MEMBER names names ':' names identifier ';'
352                        { $$ = define_cond_compute_type(AVRULE_MEMBER) ;
353                          if ($$ ==  COND_ERR) return -1;}
354                        | TYPE_CHANGE names names ':' names identifier ';'
355                        { $$ = define_cond_compute_type(AVRULE_CHANGE) ;
356                          if ($$ == COND_ERR) return -1;}
357    			;
358cond_te_avtab_def	: cond_allow_def
359                          { $$ = $1; }
360			| cond_auditallow_def
361			  { $$ = $1; }
362			| cond_auditdeny_def
363			  { $$ = $1; }
364			| cond_dontaudit_def
365			  { $$ = $1; }
366			;
367cond_allow_def		: ALLOW names names ':' names names  ';'
368			{ $$ = define_cond_te_avtab(AVRULE_ALLOWED) ;
369                          if ($$ == COND_ERR) return -1; }
370		        ;
371cond_auditallow_def	: AUDITALLOW names names ':' names names ';'
372			{ $$ = define_cond_te_avtab(AVRULE_AUDITALLOW) ;
373                          if ($$ == COND_ERR) return -1; }
374		        ;
375cond_auditdeny_def	: AUDITDENY names names ':' names names ';'
376			{ $$ = define_cond_te_avtab(AVRULE_AUDITDENY) ;
377                          if ($$ == COND_ERR) return -1; }
378		        ;
379cond_dontaudit_def	: DONTAUDIT names names ':' names names ';'
380			{ $$ = define_cond_te_avtab(AVRULE_DONTAUDIT);
381                          if ($$ == COND_ERR) return -1; }
382		        ;
383			;
384transition_def		: TYPE_TRANSITION  names names ':' names identifier filename ';'
385			{if (define_filename_trans()) return -1; }
386			| TYPE_TRANSITION names names ':' names identifier ';'
387                        {if (define_compute_type(AVRULE_TRANSITION)) return -1;}
388                        | TYPE_MEMBER names names ':' names identifier ';'
389                        {if (define_compute_type(AVRULE_MEMBER)) return -1;}
390                        | TYPE_CHANGE names names ':' names identifier ';'
391                        {if (define_compute_type(AVRULE_CHANGE)) return -1;}
392    			;
393range_trans_def		: RANGE_TRANSITION names names mls_range_def ';'
394			{ if (define_range_trans(0)) return -1; }
395			| RANGE_TRANSITION names names ':' names mls_range_def ';'
396			{ if (define_range_trans(1)) return -1; }
397			;
398te_avtab_def		: allow_def
399			| auditallow_def
400			| auditdeny_def
401			| dontaudit_def
402			| neverallow_def
403			;
404allow_def		: ALLOW names names ':' names names  ';'
405			{if (define_te_avtab(AVRULE_ALLOWED)) return -1; }
406		        ;
407auditallow_def		: AUDITALLOW names names ':' names names ';'
408			{if (define_te_avtab(AVRULE_AUDITALLOW)) return -1; }
409		        ;
410auditdeny_def		: AUDITDENY names names ':' names names ';'
411			{if (define_te_avtab(AVRULE_AUDITDENY)) return -1; }
412		        ;
413dontaudit_def		: DONTAUDIT names names ':' names names ';'
414			{if (define_te_avtab(AVRULE_DONTAUDIT)) return -1; }
415		        ;
416neverallow_def		: NEVERALLOW names names ':' names names  ';'
417			{if (define_te_avtab(AVRULE_NEVERALLOW)) return -1; }
418		        ;
419role_type_def		: ROLE identifier TYPES names ';'
420			{if (define_role_types()) return -1;}
421 			| ROLE identifier';'
422 			{if (define_role_types()) return -1;}
423                        ;
424role_dominance		: DOMINANCE '{' roles '}'
425			;
426role_trans_def		: ROLE_TRANSITION names names identifier ';'
427			{if (define_role_trans(0)) return -1; }
428			| ROLE_TRANSITION names names ':' names identifier ';'
429			{if (define_role_trans(1)) return -1;}
430			;
431role_allow_def		: ALLOW names names ';'
432			{if (define_role_allow()) return -1; }
433			;
434roles			: role_def
435			{ $$ = $1; }
436			| roles role_def
437			{ $$ = merge_roles_dom((role_datum_t*)$1, (role_datum_t*)$2); if ($$ == 0) return -1;}
438			;
439role_def		: ROLE identifier_push ';'
440                        {$$ = define_role_dom(NULL); if ($$ == 0) return -1;}
441			| ROLE identifier_push '{' roles '}'
442                        {$$ = define_role_dom((role_datum_t*)$4); if ($$ == 0) return -1;}
443			;
444opt_constraints         : constraints
445                        |
446                        ;
447constraints		: constraint_decl
448			| constraints constraint_decl
449			;
450constraint_decl		: constraint_def
451			| validatetrans_def
452			;
453constraint_def		: CONSTRAIN names names cexpr ';'
454			{ if (define_constraint((constraint_expr_t*)$4)) return -1; }
455			;
456validatetrans_def	: VALIDATETRANS names cexpr ';'
457			{ if (define_validatetrans((constraint_expr_t*)$3)) return -1; }
458			;
459cexpr			: '(' cexpr ')'
460			{ $$ = $2; }
461			| NOT cexpr
462			{ $$ = define_cexpr(CEXPR_NOT, $2, 0);
463			  if ($$ == 0) return -1; }
464			| cexpr AND cexpr
465			{ $$ = define_cexpr(CEXPR_AND, $1, $3);
466			  if ($$ == 0) return -1; }
467			| cexpr OR cexpr
468			{ $$ = define_cexpr(CEXPR_OR, $1, $3);
469			  if ($$ == 0) return -1; }
470			| cexpr_prim
471			{ $$ = $1; }
472			;
473cexpr_prim		: U1 op U2
474			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_USER, $2);
475			  if ($$ == 0) return -1; }
476			| R1 role_mls_op R2
477			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_ROLE, $2);
478			  if ($$ == 0) return -1; }
479			| T1 op T2
480			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_TYPE, $2);
481			  if ($$ == 0) return -1; }
482			| U1 op { if (insert_separator(1)) return -1; } names_push
483			{ $$ = define_cexpr(CEXPR_NAMES, CEXPR_USER, $2);
484			  if ($$ == 0) return -1; }
485			| U2 op { if (insert_separator(1)) return -1; } names_push
486			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_USER | CEXPR_TARGET), $2);
487			  if ($$ == 0) return -1; }
488			| U3 op { if (insert_separator(1)) return -1; } names_push
489			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_USER | CEXPR_XTARGET), $2);
490			  if ($$ == 0) return -1; }
491			| R1 op { if (insert_separator(1)) return -1; } names_push
492			{ $$ = define_cexpr(CEXPR_NAMES, CEXPR_ROLE, $2);
493			  if ($$ == 0) return -1; }
494			| R2 op { if (insert_separator(1)) return -1; } names_push
495			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_ROLE | CEXPR_TARGET), $2);
496			  if ($$ == 0) return -1; }
497			| R3 op { if (insert_separator(1)) return -1; } names_push
498			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_ROLE | CEXPR_XTARGET), $2);
499			  if ($$ == 0) return -1; }
500			| T1 op { if (insert_separator(1)) return -1; } names_push
501			{ $$ = define_cexpr(CEXPR_NAMES, CEXPR_TYPE, $2);
502			  if ($$ == 0) return -1; }
503			| T2 op { if (insert_separator(1)) return -1; } names_push
504			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_TYPE | CEXPR_TARGET), $2);
505			  if ($$ == 0) return -1; }
506			| T3 op { if (insert_separator(1)) return -1; } names_push
507			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_TYPE | CEXPR_XTARGET), $2);
508			  if ($$ == 0) return -1; }
509			| SAMEUSER
510			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_USER, CEXPR_EQ);
511			  if ($$ == 0) return -1; }
512			| SOURCE ROLE { if (insert_separator(1)) return -1; } names_push
513			{ $$ = define_cexpr(CEXPR_NAMES, CEXPR_ROLE, CEXPR_EQ);
514			  if ($$ == 0) return -1; }
515			| TARGET ROLE { if (insert_separator(1)) return -1; } names_push
516			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_ROLE | CEXPR_TARGET), CEXPR_EQ);
517			  if ($$ == 0) return -1; }
518			| ROLE role_mls_op
519			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_ROLE, $2);
520			  if ($$ == 0) return -1; }
521			| SOURCE TYPE { if (insert_separator(1)) return -1; } names_push
522			{ $$ = define_cexpr(CEXPR_NAMES, CEXPR_TYPE, CEXPR_EQ);
523			  if ($$ == 0) return -1; }
524			| TARGET TYPE { if (insert_separator(1)) return -1; } names_push
525			{ $$ = define_cexpr(CEXPR_NAMES, (CEXPR_TYPE | CEXPR_TARGET), CEXPR_EQ);
526			  if ($$ == 0) return -1; }
527			| L1 role_mls_op L2
528			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_L1L2, $2);
529			  if ($$ == 0) return -1; }
530			| L1 role_mls_op H2
531			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_L1H2, $2);
532			  if ($$ == 0) return -1; }
533			| H1 role_mls_op L2
534			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_H1L2, $2);
535			  if ($$ == 0) return -1; }
536			| H1 role_mls_op H2
537			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_H1H2, $2);
538			  if ($$ == 0) return -1; }
539			| L1 role_mls_op H1
540			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_L1H1, $2);
541			  if ($$ == 0) return -1; }
542			| L2 role_mls_op H2
543			{ $$ = define_cexpr(CEXPR_ATTR, CEXPR_L2H2, $2);
544			  if ($$ == 0) return -1; }
545			;
546op			: EQUALS
547			{ $$ = CEXPR_EQ; }
548			| NOTEQUAL
549			{ $$ = CEXPR_NEQ; }
550			;
551role_mls_op		: op
552			{ $$ = $1; }
553			| DOM
554			{ $$ = CEXPR_DOM; }
555			| DOMBY
556			{ $$ = CEXPR_DOMBY; }
557			| INCOMP
558			{ $$ = CEXPR_INCOMP; }
559			;
560users			: user_def
561			| users user_def
562			;
563user_def		: USER identifier ROLES names opt_mls_user ';'
564	                {if (define_user()) return -1;}
565			;
566opt_mls_user		: LEVEL mls_level_def RANGE mls_range_def
567			|
568			;
569initial_sid_contexts	: initial_sid_context_def
570			| initial_sid_contexts initial_sid_context_def
571			;
572initial_sid_context_def	: SID identifier security_context_def
573			{if (define_initial_sid_context()) return -1;}
574			;
575opt_dev_contexts	: dev_contexts |
576			;
577dev_contexts		: dev_context_def
578			| dev_contexts dev_context_def
579			;
580dev_context_def		: pirq_context_def |
581			  iomem_context_def |
582			  ioport_context_def |
583			  pci_context_def
584			;
585pirq_context_def 	: PIRQCON number security_context_def
586		        {if (define_pirq_context($2)) return -1;}
587		        ;
588iomem_context_def	: IOMEMCON number security_context_def
589		        {if (define_iomem_context($2,$2)) return -1;}
590		        | IOMEMCON number '-' number security_context_def
591		        {if (define_iomem_context($2,$4)) return -1;}
592		        ;
593ioport_context_def	: IOPORTCON number security_context_def
594			{if (define_ioport_context($2,$2)) return -1;}
595			| IOPORTCON number '-' number security_context_def
596			{if (define_ioport_context($2,$4)) return -1;}
597			;
598pci_context_def  	: PCIDEVICECON number security_context_def
599		        {if (define_pcidevice_context($2)) return -1;}
600		        ;
601opt_fs_contexts         : fs_contexts
602                        |
603                        ;
604fs_contexts		: fs_context_def
605			| fs_contexts fs_context_def
606			;
607fs_context_def		: FSCON number number security_context_def security_context_def
608			{if (define_fs_context($2,$3)) return -1;}
609			;
610net_contexts		: opt_port_contexts opt_netif_contexts opt_node_contexts
611			;
612opt_port_contexts       : port_contexts
613                        |
614                        ;
615port_contexts		: port_context_def
616			| port_contexts port_context_def
617			;
618port_context_def	: PORTCON identifier number security_context_def
619			{if (define_port_context($3,$3)) return -1;}
620			| PORTCON identifier number '-' number security_context_def
621			{if (define_port_context($3,$5)) return -1;}
622			;
623opt_netif_contexts      : netif_contexts
624                        |
625                        ;
626netif_contexts		: netif_context_def
627			| netif_contexts netif_context_def
628			;
629netif_context_def	: NETIFCON identifier security_context_def security_context_def
630			{if (define_netif_context()) return -1;}
631			;
632opt_node_contexts       : node_contexts
633                        |
634                        ;
635node_contexts		: node_context_def
636			| node_contexts node_context_def
637			;
638node_context_def	: NODECON ipv4_addr_def ipv4_addr_def security_context_def
639			{if (define_ipv4_node_context()) return -1;}
640			| NODECON ipv6_addr ipv6_addr security_context_def
641			{if (define_ipv6_node_context()) return -1;}
642			;
643opt_fs_uses             : fs_uses
644                        |
645                        ;
646fs_uses                 : fs_use_def
647                        | fs_uses fs_use_def
648                        ;
649fs_use_def              : FSUSEXATTR identifier security_context_def ';'
650                        {if (define_fs_use(SECURITY_FS_USE_XATTR)) return -1;}
651                        | FSUSETASK identifier security_context_def ';'
652                        {if (define_fs_use(SECURITY_FS_USE_TASK)) return -1;}
653                        | FSUSETRANS identifier security_context_def ';'
654                        {if (define_fs_use(SECURITY_FS_USE_TRANS)) return -1;}
655                        ;
656opt_genfs_contexts      : genfs_contexts
657                        |
658                        ;
659genfs_contexts          : genfs_context_def
660                        | genfs_contexts genfs_context_def
661                        ;
662genfs_context_def	: GENFSCON identifier path '-' identifier security_context_def
663			{if (define_genfs_context(1)) return -1;}
664			| GENFSCON identifier path '-' '-' {insert_id("-", 0);} security_context_def
665			{if (define_genfs_context(1)) return -1;}
666                        | GENFSCON identifier path security_context_def
667			{if (define_genfs_context(0)) return -1;}
668			;
669ipv4_addr_def		: IPV4_ADDR
670			{ if (insert_id(yytext,0)) return -1; }
671			;
672security_context_def	: identifier ':' identifier ':' identifier opt_mls_range_def
673	                ;
674opt_mls_range_def	: ':' mls_range_def
675			|
676			;
677mls_range_def		: mls_level_def '-' mls_level_def
678			{if (insert_separator(0)) return -1;}
679	                | mls_level_def
680			{if (insert_separator(0)) return -1;}
681	                ;
682mls_level_def		: identifier ':' id_comma_list
683			{if (insert_separator(0)) return -1;}
684	                | identifier
685			{if (insert_separator(0)) return -1;}
686	                ;
687id_comma_list           : identifier
688			| id_comma_list ',' identifier
689			;
690tilde			: '~'
691			;
692asterisk		: '*'
693			;
694names           	: identifier
695			{ if (insert_separator(0)) return -1; }
696			| nested_id_set
697			{ if (insert_separator(0)) return -1; }
698			| asterisk
699                        { if (insert_id("*", 0)) return -1;
700			  if (insert_separator(0)) return -1; }
701			| tilde identifier
702                        { if (insert_id("~", 0)) return -1;
703			  if (insert_separator(0)) return -1; }
704			| tilde nested_id_set
705	 		{ if (insert_id("~", 0)) return -1;
706			  if (insert_separator(0)) return -1; }
707                        | identifier '-' { if (insert_id("-", 0)) return -1; } identifier
708			{ if (insert_separator(0)) return -1; }
709			;
710tilde_push              : tilde
711                        { if (insert_id("~", 1)) return -1; }
712			;
713asterisk_push           : asterisk
714                        { if (insert_id("*", 1)) return -1; }
715			;
716names_push		: identifier_push
717			| '{' identifier_list_push '}'
718			| asterisk_push
719			| tilde_push identifier_push
720			| tilde_push '{' identifier_list_push '}'
721			;
722identifier_list_push	: identifier_push
723			| identifier_list_push identifier_push
724			;
725identifier_push		: IDENTIFIER
726			{ if (insert_id(yytext, 1)) return -1; }
727			;
728identifier_list		: identifier
729			| identifier_list identifier
730			;
731nested_id_set           : '{' nested_id_list '}'
732                        ;
733nested_id_list          : nested_id_element | nested_id_list nested_id_element
734                        ;
735nested_id_element       : identifier | '-' { if (insert_id("-", 0)) return -1; } identifier | nested_id_set
736                        ;
737identifier		: IDENTIFIER
738			{ if (insert_id(yytext,0)) return -1; }
739			;
740path     		: PATH
741			{ if (insert_id(yytext,0)) return -1; }
742			;
743filename		: FILENAME
744			{ yytext[strlen(yytext) - 1] = '\0'; if (insert_id(yytext + 1,0)) return -1; }
745			;
746number			: NUMBER
747			{ $$ = strtoul(yytext,NULL,0); }
748			;
749ipv6_addr		: IPV6_ADDR
750			{ if (insert_id(yytext,0)) return -1; }
751			;
752policycap_def		: POLICYCAP identifier ';'
753			{if (define_polcap()) return -1;}
754			;
755permissive_def		: PERMISSIVE identifier ';'
756			{if (define_permissive()) return -1;}
757
758/*********** module grammar below ***********/
759
760module_policy           : module_def avrules_block
761                        { if (end_avrule_block(pass) == -1) return -1;
762                          if (policydb_index_others(NULL, policydbp, 0)) return -1;
763                        }
764                        ;
765module_def              : MODULE identifier version_identifier ';'
766                        { if (define_policy(pass, 1) == -1) return -1; }
767                        ;
768version_identifier      : VERSION_IDENTIFIER
769                        { if (insert_id(yytext,0)) return -1; }
770			| number
771                        { if (insert_id(yytext,0)) return -1; }
772                        | ipv4_addr_def /* version can look like ipv4 address */
773                        ;
774avrules_block           : avrule_decls avrule_user_defs
775                        ;
776avrule_decls            : avrule_decls avrule_decl
777                        | avrule_decl
778                        ;
779avrule_decl             : rbac_decl
780                        | te_decl
781                        | cond_stmt_def
782                        | require_block
783                        | optional_block
784                        | ';'
785                        ;
786require_block           : REQUIRE '{' require_list '}'
787                        ;
788require_list            : require_list require_decl
789                        | require_decl
790                        ;
791require_decl            : require_class ';'
792                        | require_decl_def require_id_list ';'
793                        ;
794require_class           : CLASS identifier names
795                        { if (require_class(pass)) return -1; }
796                        ;
797require_decl_def        : ROLE        { $$ = require_role; }
798                        | TYPE        { $$ = require_type; }
799                        | ATTRIBUTE   { $$ = require_attribute; }
800                        | USER        { $$ = require_user; }
801                        | BOOL        { $$ = require_bool; }
802                        | SENSITIVITY { $$ = require_sens; }
803                        | CATEGORY    { $$ = require_cat; }
804                        ;
805require_id_list         : identifier
806                        { if ($<require_func>0 (pass)) return -1; }
807                        | require_id_list ',' identifier
808                        { if ($<require_func>0 (pass)) return -1; }
809                        ;
810optional_block          : optional_decl '{' avrules_block '}'
811                        { if (end_avrule_block(pass) == -1) return -1; }
812                          optional_else
813                        { if (end_optional(pass) == -1) return -1; }
814                        ;
815optional_else           : else_decl '{' avrules_block '}'
816                        { if (end_avrule_block(pass) == -1) return -1; }
817                        | /* empty */
818                        ;
819optional_decl           : OPTIONAL
820                        { if (begin_optional(pass) == -1) return -1; }
821                        ;
822else_decl               : ELSE
823                        { if (begin_optional_else(pass) == -1) return -1; }
824                        ;
825avrule_user_defs        : user_def avrule_user_defs
826                        | /* empty */
827                        ;
828