16cd6a6acSopenharmony_ci 26cd6a6acSopenharmony_ci/* 36cd6a6acSopenharmony_ci * Author : Stephen Smalley, <sds@tycho.nsa.gov> 46cd6a6acSopenharmony_ci */ 56cd6a6acSopenharmony_ci 66cd6a6acSopenharmony_ci/* Updated: David Caplan, <dac@tresys.com> 76cd6a6acSopenharmony_ci * 86cd6a6acSopenharmony_ci * Added conditional policy language extensions 96cd6a6acSopenharmony_ci * 106cd6a6acSopenharmony_ci * Jason Tang <jtang@tresys.com> 116cd6a6acSopenharmony_ci * 126cd6a6acSopenharmony_ci * Added support for binary policy modules 136cd6a6acSopenharmony_ci * 146cd6a6acSopenharmony_ci * Copyright (C) 2003-5 Tresys Technology, LLC 156cd6a6acSopenharmony_ci * Copyright (C) 2017 Mellanox Technologies Inc. 166cd6a6acSopenharmony_ci * This program is free software; you can redistribute it and/or modify 176cd6a6acSopenharmony_ci * it under the terms of the GNU General Public License as published by 186cd6a6acSopenharmony_ci * the Free Software Foundation, version 2. 196cd6a6acSopenharmony_ci */ 206cd6a6acSopenharmony_ci 216cd6a6acSopenharmony_ci/* FLASK */ 226cd6a6acSopenharmony_ci 236cd6a6acSopenharmony_ci%{ 246cd6a6acSopenharmony_ci#include <sys/types.h> 256cd6a6acSopenharmony_ci#include <limits.h> 266cd6a6acSopenharmony_ci#include <stdint.h> 276cd6a6acSopenharmony_ci#include <string.h> 286cd6a6acSopenharmony_ci 296cd6a6acSopenharmony_citypedef int (* require_func_t)(void); 306cd6a6acSopenharmony_ci 316cd6a6acSopenharmony_ci#ifdef ANDROID 326cd6a6acSopenharmony_ci#include "policy_parse.h" 336cd6a6acSopenharmony_ci#else 346cd6a6acSopenharmony_ci#include "y.tab.h" 356cd6a6acSopenharmony_ci#endif 366cd6a6acSopenharmony_ci 376cd6a6acSopenharmony_cistatic char linebuf[2][255]; 386cd6a6acSopenharmony_cistatic unsigned int lno = 0; 396cd6a6acSopenharmony_ciint werror = 0; 406cd6a6acSopenharmony_ciint yyerror(const char *msg); 416cd6a6acSopenharmony_ciint yywarn(const char *msg); 426cd6a6acSopenharmony_ci 436cd6a6acSopenharmony_civoid set_source_file(const char *name); 446cd6a6acSopenharmony_ci 456cd6a6acSopenharmony_cichar source_file[PATH_MAX]; 466cd6a6acSopenharmony_ciunsigned long source_lineno = 1; 476cd6a6acSopenharmony_ci 486cd6a6acSopenharmony_ciunsigned long policydb_lineno = 1; 496cd6a6acSopenharmony_ci 506cd6a6acSopenharmony_ciunsigned int policydb_errors = 0; 516cd6a6acSopenharmony_ci%} 526cd6a6acSopenharmony_ci 536cd6a6acSopenharmony_ci%option noinput nounput noyywrap 546cd6a6acSopenharmony_ci 556cd6a6acSopenharmony_ci%array 566cd6a6acSopenharmony_ciletter [A-Za-z] 576cd6a6acSopenharmony_cidigit [0-9] 586cd6a6acSopenharmony_cialnum [a-zA-Z0-9] 596cd6a6acSopenharmony_cihexval [0-9A-Fa-f] 606cd6a6acSopenharmony_ci 616cd6a6acSopenharmony_ci%% 626cd6a6acSopenharmony_ci\n.* { 636cd6a6acSopenharmony_ci#if defined(__GNUC__) && __GNUC__ >= 8 646cd6a6acSopenharmony_ci#pragma GCC diagnostic push 656cd6a6acSopenharmony_ci#pragma GCC diagnostic ignored "-Wstringop-truncation" 666cd6a6acSopenharmony_ci#endif 676cd6a6acSopenharmony_ci strncpy(linebuf[lno], yytext+1, 255); 686cd6a6acSopenharmony_ci#if defined(__GNUC__) && __GNUC__ >= 8 696cd6a6acSopenharmony_ci#pragma GCC diagnostic pop 706cd6a6acSopenharmony_ci#endif 716cd6a6acSopenharmony_ci linebuf[lno][254] = 0; 726cd6a6acSopenharmony_ci lno = 1 - lno; 736cd6a6acSopenharmony_ci policydb_lineno++; 746cd6a6acSopenharmony_ci if (source_lineno == ULONG_MAX) 756cd6a6acSopenharmony_ci yywarn("source line number overflow"); 766cd6a6acSopenharmony_ci else 776cd6a6acSopenharmony_ci source_lineno++; 786cd6a6acSopenharmony_ci yyless(1); 796cd6a6acSopenharmony_ci } 806cd6a6acSopenharmony_ciCLONE | 816cd6a6acSopenharmony_ciclone { return(CLONE); } 826cd6a6acSopenharmony_ciCOMMON | 836cd6a6acSopenharmony_cicommon { return(COMMON); } 846cd6a6acSopenharmony_ciCLASS | 856cd6a6acSopenharmony_ciclass { return(CLASS); } 866cd6a6acSopenharmony_ciCONSTRAIN | 876cd6a6acSopenharmony_ciconstrain { return(CONSTRAIN); } 886cd6a6acSopenharmony_ciVALIDATETRANS | 896cd6a6acSopenharmony_civalidatetrans { return(VALIDATETRANS); } 906cd6a6acSopenharmony_ciINHERITS | 916cd6a6acSopenharmony_ciinherits { return(INHERITS); } 926cd6a6acSopenharmony_ciSID | 936cd6a6acSopenharmony_cisid { return(SID); } 946cd6a6acSopenharmony_ciROLE | 956cd6a6acSopenharmony_cirole { return(ROLE); } 966cd6a6acSopenharmony_ciROLES | 976cd6a6acSopenharmony_ciroles { return(ROLES); } 986cd6a6acSopenharmony_ciROLEATTRIBUTE | 996cd6a6acSopenharmony_ciroleattribute { return(ROLEATTRIBUTE);} 1006cd6a6acSopenharmony_ciATTRIBUTE_ROLE | 1016cd6a6acSopenharmony_ciattribute_role { return(ATTRIBUTE_ROLE);} 1026cd6a6acSopenharmony_ciTYPES | 1036cd6a6acSopenharmony_citypes { return(TYPES); } 1046cd6a6acSopenharmony_ciTYPEALIAS | 1056cd6a6acSopenharmony_citypealias { return(TYPEALIAS); } 1066cd6a6acSopenharmony_ciTYPEATTRIBUTE | 1076cd6a6acSopenharmony_citypeattribute { return(TYPEATTRIBUTE); } 1086cd6a6acSopenharmony_ciTYPEBOUNDS | 1096cd6a6acSopenharmony_citypebounds { return(TYPEBOUNDS); } 1106cd6a6acSopenharmony_ciTYPE | 1116cd6a6acSopenharmony_citype { return(TYPE); } 1126cd6a6acSopenharmony_ciBOOL | 1136cd6a6acSopenharmony_cibool { return(BOOL); } 1146cd6a6acSopenharmony_ciTUNABLE | 1156cd6a6acSopenharmony_citunable { return(TUNABLE); } 1166cd6a6acSopenharmony_ciIF | 1176cd6a6acSopenharmony_ciif { return(IF); } 1186cd6a6acSopenharmony_ciELSE | 1196cd6a6acSopenharmony_cielse { return(ELSE); } 1206cd6a6acSopenharmony_ciALIAS | 1216cd6a6acSopenharmony_cialias { return(ALIAS); } 1226cd6a6acSopenharmony_ciATTRIBUTE | 1236cd6a6acSopenharmony_ciattribute { return(ATTRIBUTE); } 1246cd6a6acSopenharmony_ciEXPANDATTRIBUTE | 1256cd6a6acSopenharmony_ciexpandattribute { return(EXPANDATTRIBUTE); } 1266cd6a6acSopenharmony_ciTYPE_TRANSITION | 1276cd6a6acSopenharmony_citype_transition { return(TYPE_TRANSITION); } 1286cd6a6acSopenharmony_ciTYPE_MEMBER | 1296cd6a6acSopenharmony_citype_member { return(TYPE_MEMBER); } 1306cd6a6acSopenharmony_ciTYPE_CHANGE | 1316cd6a6acSopenharmony_citype_change { return(TYPE_CHANGE); } 1326cd6a6acSopenharmony_ciROLE_TRANSITION | 1336cd6a6acSopenharmony_cirole_transition { return(ROLE_TRANSITION); } 1346cd6a6acSopenharmony_ciRANGE_TRANSITION | 1356cd6a6acSopenharmony_cirange_transition { return(RANGE_TRANSITION); } 1366cd6a6acSopenharmony_ciSENSITIVITY | 1376cd6a6acSopenharmony_cisensitivity { return(SENSITIVITY); } 1386cd6a6acSopenharmony_ciDOMINANCE | 1396cd6a6acSopenharmony_cidominance { return(DOMINANCE); } 1406cd6a6acSopenharmony_ciCATEGORY | 1416cd6a6acSopenharmony_cicategory { return(CATEGORY); } 1426cd6a6acSopenharmony_ciLEVEL | 1436cd6a6acSopenharmony_cilevel { return(LEVEL); } 1446cd6a6acSopenharmony_ciRANGE | 1456cd6a6acSopenharmony_cirange { return(RANGE); } 1466cd6a6acSopenharmony_ciMLSCONSTRAIN | 1476cd6a6acSopenharmony_cimlsconstrain { return(MLSCONSTRAIN); } 1486cd6a6acSopenharmony_ciMLSVALIDATETRANS | 1496cd6a6acSopenharmony_cimlsvalidatetrans { return(MLSVALIDATETRANS); } 1506cd6a6acSopenharmony_ciUSER | 1516cd6a6acSopenharmony_ciuser { return(USER); } 1526cd6a6acSopenharmony_ciNEVERALLOW | 1536cd6a6acSopenharmony_cineverallow { return(NEVERALLOW); } 1546cd6a6acSopenharmony_ciALLOW | 1556cd6a6acSopenharmony_ciallow { return(ALLOW); } 1566cd6a6acSopenharmony_ciAUDITALLOW | 1576cd6a6acSopenharmony_ciauditallow { return(AUDITALLOW); } 1586cd6a6acSopenharmony_ciAUDITDENY | 1596cd6a6acSopenharmony_ciauditdeny { return(AUDITDENY); } 1606cd6a6acSopenharmony_ciDONTAUDIT | 1616cd6a6acSopenharmony_cidontaudit { return(DONTAUDIT); } 1626cd6a6acSopenharmony_ciALLOWXPERM | 1636cd6a6acSopenharmony_ciallowxperm { return(ALLOWXPERM); } 1646cd6a6acSopenharmony_ciAUDITALLOWXPERM | 1656cd6a6acSopenharmony_ciauditallowxperm { return(AUDITALLOWXPERM); } 1666cd6a6acSopenharmony_ciDONTAUDITXPERM | 1676cd6a6acSopenharmony_cidontauditxperm { return(DONTAUDITXPERM); } 1686cd6a6acSopenharmony_ciNEVERALLOWXPERM | 1696cd6a6acSopenharmony_cineverallowxperm { return(NEVERALLOWXPERM); } 1706cd6a6acSopenharmony_ciSOURCE | 1716cd6a6acSopenharmony_cisource { return(SOURCE); } 1726cd6a6acSopenharmony_ciTARGET | 1736cd6a6acSopenharmony_citarget { return(TARGET); } 1746cd6a6acSopenharmony_ciSAMEUSER | 1756cd6a6acSopenharmony_cisameuser { return(SAMEUSER);} 1766cd6a6acSopenharmony_cimodule|MODULE { return(MODULE); } 1776cd6a6acSopenharmony_cirequire|REQUIRE { return(REQUIRE); } 1786cd6a6acSopenharmony_cioptional|OPTIONAL { return(OPTIONAL); } 1796cd6a6acSopenharmony_ciOR | 1806cd6a6acSopenharmony_cior { return(OR);} 1816cd6a6acSopenharmony_ciAND | 1826cd6a6acSopenharmony_ciand { return(AND);} 1836cd6a6acSopenharmony_ciNOT | 1846cd6a6acSopenharmony_cinot { return(NOT);} 1856cd6a6acSopenharmony_cixor | 1866cd6a6acSopenharmony_ciXOR { return(XOR); } 1876cd6a6acSopenharmony_cieq | 1886cd6a6acSopenharmony_ciEQ { return(EQUALS);} 1896cd6a6acSopenharmony_citrue | 1906cd6a6acSopenharmony_ciTRUE { return(CTRUE); } 1916cd6a6acSopenharmony_cifalse | 1926cd6a6acSopenharmony_ciFALSE { return(CFALSE); } 1936cd6a6acSopenharmony_cidom | 1946cd6a6acSopenharmony_ciDOM { return(DOM);} 1956cd6a6acSopenharmony_cidomby | 1966cd6a6acSopenharmony_ciDOMBY { return(DOMBY);} 1976cd6a6acSopenharmony_ciINCOMP | 1986cd6a6acSopenharmony_ciincomp { return(INCOMP);} 1996cd6a6acSopenharmony_cifscon | 2006cd6a6acSopenharmony_ciFSCON { return(FSCON);} 2016cd6a6acSopenharmony_ciibpkeycon | 2026cd6a6acSopenharmony_ciIBPKEYCON { return(IBPKEYCON);} 2036cd6a6acSopenharmony_ciibendportcon | 2046cd6a6acSopenharmony_ciIBENDPORTCON { return(IBENDPORTCON);} 2056cd6a6acSopenharmony_ciportcon | 2066cd6a6acSopenharmony_ciPORTCON { return(PORTCON);} 2076cd6a6acSopenharmony_cinetifcon | 2086cd6a6acSopenharmony_ciNETIFCON { return(NETIFCON);} 2096cd6a6acSopenharmony_cinodecon | 2106cd6a6acSopenharmony_ciNODECON { return(NODECON);} 2116cd6a6acSopenharmony_cipirqcon | 2126cd6a6acSopenharmony_ciPIRQCON { return(PIRQCON);} 2136cd6a6acSopenharmony_ciiomemcon | 2146cd6a6acSopenharmony_ciIOMEMCON { return(IOMEMCON);} 2156cd6a6acSopenharmony_ciioportcon | 2166cd6a6acSopenharmony_ciIOPORTCON { return(IOPORTCON);} 2176cd6a6acSopenharmony_cipcidevicecon | 2186cd6a6acSopenharmony_ciPCIDEVICECON { return(PCIDEVICECON);} 2196cd6a6acSopenharmony_cidevicetreecon | 2206cd6a6acSopenharmony_ciDEVICETREECON { return(DEVICETREECON);} 2216cd6a6acSopenharmony_cifs_use_xattr | 2226cd6a6acSopenharmony_ciFS_USE_XATTR { return(FSUSEXATTR);} 2236cd6a6acSopenharmony_cifs_use_task | 2246cd6a6acSopenharmony_ciFS_USE_TASK { return(FSUSETASK);} 2256cd6a6acSopenharmony_cifs_use_trans | 2266cd6a6acSopenharmony_ciFS_USE_TRANS { return(FSUSETRANS);} 2276cd6a6acSopenharmony_cigenfscon | 2286cd6a6acSopenharmony_ciGENFSCON { return(GENFSCON);} 2296cd6a6acSopenharmony_cir1 | 2306cd6a6acSopenharmony_ciR1 { return(R1); } 2316cd6a6acSopenharmony_cir2 | 2326cd6a6acSopenharmony_ciR2 { return(R2); } 2336cd6a6acSopenharmony_cir3 | 2346cd6a6acSopenharmony_ciR3 { return(R3); } 2356cd6a6acSopenharmony_ciu1 | 2366cd6a6acSopenharmony_ciU1 { return(U1); } 2376cd6a6acSopenharmony_ciu2 | 2386cd6a6acSopenharmony_ciU2 { return(U2); } 2396cd6a6acSopenharmony_ciu3 | 2406cd6a6acSopenharmony_ciU3 { return(U3); } 2416cd6a6acSopenharmony_cit1 | 2426cd6a6acSopenharmony_ciT1 { return(T1); } 2436cd6a6acSopenharmony_cit2 | 2446cd6a6acSopenharmony_ciT2 { return(T2); } 2456cd6a6acSopenharmony_cit3 | 2466cd6a6acSopenharmony_ciT3 { return(T3); } 2476cd6a6acSopenharmony_cil1 | 2486cd6a6acSopenharmony_ciL1 { return(L1); } 2496cd6a6acSopenharmony_cil2 | 2506cd6a6acSopenharmony_ciL2 { return(L2); } 2516cd6a6acSopenharmony_cih1 | 2526cd6a6acSopenharmony_ciH1 { return(H1); } 2536cd6a6acSopenharmony_cih2 | 2546cd6a6acSopenharmony_ciH2 { return(H2); } 2556cd6a6acSopenharmony_cipolicycap | 2566cd6a6acSopenharmony_ciPOLICYCAP { return(POLICYCAP); } 2576cd6a6acSopenharmony_cipermissive | 2586cd6a6acSopenharmony_ciPERMISSIVE { return(PERMISSIVE); } 2596cd6a6acSopenharmony_cidefault_user | 2606cd6a6acSopenharmony_ciDEFAULT_USER { return(DEFAULT_USER); } 2616cd6a6acSopenharmony_cidefault_role | 2626cd6a6acSopenharmony_ciDEFAULT_ROLE { return(DEFAULT_ROLE); } 2636cd6a6acSopenharmony_cidefault_type | 2646cd6a6acSopenharmony_ciDEFAULT_TYPE { return(DEFAULT_TYPE); } 2656cd6a6acSopenharmony_cidefault_range | 2666cd6a6acSopenharmony_ciDEFAULT_RANGE { return(DEFAULT_RANGE); } 2676cd6a6acSopenharmony_cilow-high | 2686cd6a6acSopenharmony_ciLOW-HIGH { return(LOW_HIGH); } 2696cd6a6acSopenharmony_cihigh | 2706cd6a6acSopenharmony_ciHIGH { return(HIGH); } 2716cd6a6acSopenharmony_cilow | 2726cd6a6acSopenharmony_ciLOW { return(LOW); } 2736cd6a6acSopenharmony_ciglblub | 2746cd6a6acSopenharmony_ciGLBLUB { return(GLBLUB); } 2756cd6a6acSopenharmony_ci"/"[^ \n\r\t\f]* { return(PATH); } 2766cd6a6acSopenharmony_ci\""/"[^\"\n]*\" { return(QPATH); } 2776cd6a6acSopenharmony_ci\"[^"/"\"\n]+\" { return(FILENAME); } 2786cd6a6acSopenharmony_ci{letter}({alnum}|[_\-])*([\.]?({alnum}|[_\-]))* { return(IDENTIFIER); } 2796cd6a6acSopenharmony_ci{digit}+|0x{hexval}+ { return(NUMBER); } 2806cd6a6acSopenharmony_ci{alnum}*{letter}{alnum}* { return(FILESYSTEM); } 2816cd6a6acSopenharmony_ci{digit}{1,3}(\.{digit}{1,3}){3} { return(IPV4_ADDR); } 2826cd6a6acSopenharmony_ci{hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])* { return(IPV6_ADDR); } 2836cd6a6acSopenharmony_ci{digit}+(\.({alnum}|[_.])*)? { return(VERSION_IDENTIFIER); } 2846cd6a6acSopenharmony_ci#line[ ]1[ ]\"[^\n]*\" { set_source_file(yytext+9); } 2856cd6a6acSopenharmony_ci#line[ ]{digit}+ { 2866cd6a6acSopenharmony_ci errno = 0; 2876cd6a6acSopenharmony_ci source_lineno = strtoul(yytext+6, NULL, 10) - 1; 2886cd6a6acSopenharmony_ci if (errno) { 2896cd6a6acSopenharmony_ci yywarn("source line number too big"); 2906cd6a6acSopenharmony_ci } 2916cd6a6acSopenharmony_ci } 2926cd6a6acSopenharmony_ci#[^\n]* { /* delete comments */ } 2936cd6a6acSopenharmony_ci[ \t\f]+ { /* delete whitespace */ } 2946cd6a6acSopenharmony_ci"==" { return(EQUALS); } 2956cd6a6acSopenharmony_ci"!=" { return (NOTEQUAL); } 2966cd6a6acSopenharmony_ci"&&" { return (AND); } 2976cd6a6acSopenharmony_ci"||" { return (OR); } 2986cd6a6acSopenharmony_ci"!" { return (NOT); } 2996cd6a6acSopenharmony_ci"^" { return (XOR); } 3006cd6a6acSopenharmony_ci"," | 3016cd6a6acSopenharmony_ci":" | 3026cd6a6acSopenharmony_ci";" | 3036cd6a6acSopenharmony_ci"(" | 3046cd6a6acSopenharmony_ci")" | 3056cd6a6acSopenharmony_ci"{" | 3066cd6a6acSopenharmony_ci"}" | 3076cd6a6acSopenharmony_ci"[" | 3086cd6a6acSopenharmony_ci"-" | 3096cd6a6acSopenharmony_ci"." | 3106cd6a6acSopenharmony_ci"]" | 3116cd6a6acSopenharmony_ci"~" | 3126cd6a6acSopenharmony_ci"*" { return(yytext[0]); } 3136cd6a6acSopenharmony_ci. { yyerror("unrecognized character");} 3146cd6a6acSopenharmony_ci%% 3156cd6a6acSopenharmony_ciint yyerror(const char *msg) 3166cd6a6acSopenharmony_ci{ 3176cd6a6acSopenharmony_ci if (source_file[0]) 3186cd6a6acSopenharmony_ci fprintf(stderr, "%s:%lu:", 3196cd6a6acSopenharmony_ci source_file, source_lineno); 3206cd6a6acSopenharmony_ci else 3216cd6a6acSopenharmony_ci fprintf(stderr, "(unknown source)::"); 3226cd6a6acSopenharmony_ci fprintf(stderr, "ERROR '%s' at token '%s' on line %lu:\n%s\n%s\n", 3236cd6a6acSopenharmony_ci msg, 3246cd6a6acSopenharmony_ci yytext, 3256cd6a6acSopenharmony_ci policydb_lineno, 3266cd6a6acSopenharmony_ci linebuf[0], linebuf[1]); 3276cd6a6acSopenharmony_ci policydb_errors++; 3286cd6a6acSopenharmony_ci return -1; 3296cd6a6acSopenharmony_ci} 3306cd6a6acSopenharmony_ci 3316cd6a6acSopenharmony_ciint yywarn(const char *msg) 3326cd6a6acSopenharmony_ci{ 3336cd6a6acSopenharmony_ci if (werror) 3346cd6a6acSopenharmony_ci return yyerror(msg); 3356cd6a6acSopenharmony_ci 3366cd6a6acSopenharmony_ci if (source_file[0]) 3376cd6a6acSopenharmony_ci fprintf(stderr, "%s:%lu:", 3386cd6a6acSopenharmony_ci source_file, source_lineno); 3396cd6a6acSopenharmony_ci else 3406cd6a6acSopenharmony_ci fprintf(stderr, "(unknown source)::"); 3416cd6a6acSopenharmony_ci fprintf(stderr, "WARNING '%s' at token '%s' on line %lu:\n%s\n%s\n", 3426cd6a6acSopenharmony_ci msg, 3436cd6a6acSopenharmony_ci yytext, 3446cd6a6acSopenharmony_ci policydb_lineno, 3456cd6a6acSopenharmony_ci linebuf[0], linebuf[1]); 3466cd6a6acSopenharmony_ci return 0; 3476cd6a6acSopenharmony_ci} 3486cd6a6acSopenharmony_ci 3496cd6a6acSopenharmony_civoid set_source_file(const char *name) 3506cd6a6acSopenharmony_ci{ 3516cd6a6acSopenharmony_ci source_lineno = 1; 3526cd6a6acSopenharmony_ci strncpy(source_file, name, sizeof(source_file)-1); 3536cd6a6acSopenharmony_ci source_file[sizeof(source_file)-1] = '\0'; 3546cd6a6acSopenharmony_ci if (strlen(source_file) && source_file[strlen(source_file)-1] == '"') 3556cd6a6acSopenharmony_ci source_file[strlen(source_file)-1] = '\0'; 3566cd6a6acSopenharmony_ci} 357