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