1 2 /* 3 * Author : Stephen Smalley, <sds (at) epoch.ncsc.mil> 4 */ 5 6 /* Updated: David Caplan, <dac (at) tresys.com> 7 * 8 * Added conditional policy language extensions 9 * 10 * Jason Tang <jtang (at) 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 default_user | 229 DEFAULT_USER { return(DEFAULT_USER); } 230 default_role | 231 DEFAULT_ROLE { return(DEFAULT_ROLE); } 232 default_type | 233 DEFAULT_TYPE { return(DEFAULT_TYPE); } 234 default_range | 235 DEFAULT_RANGE { return(DEFAULT_RANGE); } 236 low-high | 237 LOW-HIGH { return(LOW_HIGH); } 238 high | 239 HIGH { return(HIGH); } 240 low | 241 LOW { return(LOW); } 242 "/"({alnum}|[_\.\-/])* { return(PATH); } 243 \"({alnum}|[_\.\-\+\~\: ])+\" { return(FILENAME); } 244 {letter}({alnum}|[_\-])*([\.]?({alnum}|[_\-]))* { return(IDENTIFIER); } 245 {alnum}*{letter}{alnum}* { return(FILESYSTEM); } 246 {digit}+|0x{hexval}+ { return(NUMBER); } 247 {digit}{1,3}(\.{digit}{1,3}){3} { return(IPV4_ADDR); } 248 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])* { return(IPV6_ADDR); } 249 {digit}+(\.({alnum}|[_.])*)? { return(VERSION_IDENTIFIER); } 250 #line[ ]1[ ]\"[^\n]*\" { set_source_file(yytext+9); } 251 #line[ ]{digit}+ { source_lineno = atoi(yytext+6)-1; } 252 #[^\n]* { /* delete comments */ } 253 [ \t\f]+ { /* delete whitespace */ } 254 "==" { return(EQUALS); } 255 "!=" { return (NOTEQUAL); } 256 "&&" { return (AND); } 257 "||" { return (OR); } 258 "!" { return (NOT); } 259 "^" { return (XOR); } 260 "," | 261 ":" | 262 ";" | 263 "(" | 264 ")" | 265 "{" | 266 "}" | 267 "[" | 268 "-" | 269 "." | 270 "]" | 271 "~" | 272 "*" { return(yytext[0]); } 273 . { yywarn("unrecognized character");} 274 %% 275 int yyerror(char *msg) 276 { 277 if (source_file[0]) 278 fprintf(stderr, "%s:%ld:", 279 source_file, source_lineno); 280 else 281 fprintf(stderr, "(unknown source)::"); 282 fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n", 283 msg, 284 yytext, 285 policydb_lineno, 286 linebuf[0], linebuf[1]); 287 policydb_errors++; 288 return -1; 289 } 290 291 int yywarn(char *msg) 292 { 293 if (source_file[0]) 294 fprintf(stderr, "%s:%ld:", 295 source_file, source_lineno); 296 else 297 fprintf(stderr, "(unknown source)::"); 298 fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n", 299 msg, 300 yytext, 301 policydb_lineno, 302 linebuf[0], linebuf[1]); 303 return 0; 304 } 305 306 void set_source_file(const char *name) 307 { 308 source_lineno = 1; 309 strncpy(source_file, name, sizeof(source_file)-1); 310 source_file[sizeof(source_file)-1] = '\0'; 311 if (strlen(source_file) && source_file[strlen(source_file)-1] == '"') 312 source_file[strlen(source_file)-1] = '\0'; 313 } 314