2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
/*
|
2023-07-19 17:36:36 +00:00
|
|
|
* Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
|
2008-08-19 19:30:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Updated: David Caplan, <dac@tresys.com>
|
|
|
|
*
|
|
|
|
* Added conditional policy language extensions
|
|
|
|
*
|
|
|
|
* Jason Tang <jtang@tresys.com>
|
|
|
|
*
|
|
|
|
* Added support for binary policy modules
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-5 Tresys Technology, LLC
|
2017-05-22 13:08:23 +00:00
|
|
|
* Copyright (C) 2017 Mellanox Technologies Inc.
|
2008-08-19 19:30:36 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FLASK */
|
|
|
|
|
|
|
|
%{
|
|
|
|
#include <sys/types.h>
|
2024-04-02 15:29:20 +00:00
|
|
|
#include <ctype.h>
|
2008-08-19 19:30:36 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-14 21:41:49 +00:00
|
|
|
typedef int (* require_func_t)(void);
|
2008-08-19 19:30:36 +00:00
|
|
|
|
2012-02-23 15:14:13 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "policy_parse.h"
|
|
|
|
#else
|
2008-08-19 19:30:36 +00:00
|
|
|
#include "y.tab.h"
|
2012-02-23 15:14:13 +00:00
|
|
|
#endif
|
2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
static char linebuf[2][255];
|
|
|
|
static unsigned int lno = 0;
|
2020-03-05 18:40:34 +00:00
|
|
|
int werror = 0;
|
2020-03-23 18:26:33 +00:00
|
|
|
int yyerror(const char *msg);
|
2014-09-14 21:41:39 +00:00
|
|
|
int yywarn(const char *msg);
|
2008-08-19 19:30:36 +00:00
|
|
|
|
2024-04-02 15:29:21 +00:00
|
|
|
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
|
|
|
/*
|
|
|
|
* Version that does not exit, like yy_fatal_error(),
|
|
|
|
* since fuzz targets must not call exit().
|
|
|
|
*/
|
|
|
|
#include <setjmp.h>
|
|
|
|
extern jmp_buf fuzzing_pre_parse_stack_state;
|
|
|
|
void yyfatal(const char *msg)
|
|
|
|
{
|
|
|
|
yyerror(msg);
|
|
|
|
longjmp(fuzzing_pre_parse_stack_state, 1);
|
|
|
|
}
|
|
|
|
#define YY_FATAL_ERROR(msg) yyfatal(msg)
|
|
|
|
#endif
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
void set_source_file(const char *name);
|
|
|
|
|
|
|
|
char source_file[PATH_MAX];
|
|
|
|
unsigned long source_lineno = 1;
|
|
|
|
|
|
|
|
unsigned long policydb_lineno = 1;
|
|
|
|
|
|
|
|
unsigned int policydb_errors = 0;
|
|
|
|
%}
|
|
|
|
|
2012-02-23 15:14:13 +00:00
|
|
|
%option noinput nounput noyywrap
|
2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
%array
|
|
|
|
letter [A-Za-z]
|
|
|
|
digit [0-9]
|
|
|
|
alnum [a-zA-Z0-9]
|
|
|
|
hexval [0-9A-Fa-f]
|
|
|
|
|
|
|
|
%%
|
2021-09-14 12:48:27 +00:00
|
|
|
\n.* {
|
2021-11-12 15:41:54 +00:00
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 8
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstringop-truncation"
|
|
|
|
#endif
|
2021-09-14 12:48:27 +00:00
|
|
|
strncpy(linebuf[lno], yytext+1, 255);
|
2021-11-12 15:41:54 +00:00
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 8
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2021-09-14 12:48:27 +00:00
|
|
|
linebuf[lno][254] = 0;
|
|
|
|
lno = 1 - lno;
|
|
|
|
policydb_lineno++;
|
|
|
|
if (source_lineno == ULONG_MAX)
|
|
|
|
yywarn("source line number overflow");
|
|
|
|
else
|
|
|
|
source_lineno++;
|
|
|
|
yyless(1);
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
COMMON |
|
|
|
|
common { return(COMMON); }
|
|
|
|
CLASS |
|
|
|
|
class { return(CLASS); }
|
|
|
|
CONSTRAIN |
|
|
|
|
constrain { return(CONSTRAIN); }
|
|
|
|
VALIDATETRANS |
|
|
|
|
validatetrans { return(VALIDATETRANS); }
|
|
|
|
INHERITS |
|
|
|
|
inherits { return(INHERITS); }
|
|
|
|
SID |
|
|
|
|
sid { return(SID); }
|
|
|
|
ROLE |
|
|
|
|
role { return(ROLE); }
|
|
|
|
ROLES |
|
|
|
|
roles { return(ROLES); }
|
2011-07-25 01:23:54 +00:00
|
|
|
ROLEATTRIBUTE |
|
|
|
|
roleattribute { return(ROLEATTRIBUTE);}
|
|
|
|
ATTRIBUTE_ROLE |
|
|
|
|
attribute_role { return(ATTRIBUTE_ROLE);}
|
2008-08-19 19:30:36 +00:00
|
|
|
TYPES |
|
|
|
|
types { return(TYPES); }
|
|
|
|
TYPEALIAS |
|
|
|
|
typealias { return(TYPEALIAS); }
|
|
|
|
TYPEATTRIBUTE |
|
|
|
|
typeattribute { return(TYPEATTRIBUTE); }
|
2008-10-08 10:56:51 +00:00
|
|
|
TYPEBOUNDS |
|
|
|
|
typebounds { return(TYPEBOUNDS); }
|
2008-08-19 19:30:36 +00:00
|
|
|
TYPE |
|
|
|
|
type { return(TYPE); }
|
|
|
|
BOOL |
|
|
|
|
bool { return(BOOL); }
|
2011-09-01 03:29:41 +00:00
|
|
|
TUNABLE |
|
|
|
|
tunable { return(TUNABLE); }
|
2008-08-19 19:30:36 +00:00
|
|
|
IF |
|
|
|
|
if { return(IF); }
|
|
|
|
ELSE |
|
|
|
|
else { return(ELSE); }
|
|
|
|
ALIAS |
|
|
|
|
alias { return(ALIAS); }
|
|
|
|
ATTRIBUTE |
|
|
|
|
attribute { return(ATTRIBUTE); }
|
2017-05-04 21:36:49 +00:00
|
|
|
EXPANDATTRIBUTE |
|
|
|
|
expandattribute { return(EXPANDATTRIBUTE); }
|
2008-08-19 19:30:36 +00:00
|
|
|
TYPE_TRANSITION |
|
|
|
|
type_transition { return(TYPE_TRANSITION); }
|
|
|
|
TYPE_MEMBER |
|
|
|
|
type_member { return(TYPE_MEMBER); }
|
|
|
|
TYPE_CHANGE |
|
|
|
|
type_change { return(TYPE_CHANGE); }
|
|
|
|
ROLE_TRANSITION |
|
|
|
|
role_transition { return(ROLE_TRANSITION); }
|
|
|
|
RANGE_TRANSITION |
|
|
|
|
range_transition { return(RANGE_TRANSITION); }
|
|
|
|
SENSITIVITY |
|
|
|
|
sensitivity { return(SENSITIVITY); }
|
|
|
|
DOMINANCE |
|
|
|
|
dominance { return(DOMINANCE); }
|
|
|
|
CATEGORY |
|
|
|
|
category { return(CATEGORY); }
|
|
|
|
LEVEL |
|
|
|
|
level { return(LEVEL); }
|
|
|
|
RANGE |
|
|
|
|
range { return(RANGE); }
|
|
|
|
MLSCONSTRAIN |
|
|
|
|
mlsconstrain { return(MLSCONSTRAIN); }
|
|
|
|
MLSVALIDATETRANS |
|
|
|
|
mlsvalidatetrans { return(MLSVALIDATETRANS); }
|
|
|
|
USER |
|
|
|
|
user { return(USER); }
|
|
|
|
NEVERALLOW |
|
|
|
|
neverallow { return(NEVERALLOW); }
|
|
|
|
ALLOW |
|
|
|
|
allow { return(ALLOW); }
|
|
|
|
AUDITALLOW |
|
|
|
|
auditallow { return(AUDITALLOW); }
|
|
|
|
AUDITDENY |
|
|
|
|
auditdeny { return(AUDITDENY); }
|
|
|
|
DONTAUDIT |
|
|
|
|
dontaudit { return(DONTAUDIT); }
|
2015-06-12 16:01:12 +00:00
|
|
|
ALLOWXPERM |
|
2015-09-18 19:57:56 +00:00
|
|
|
allowxperm { return(ALLOWXPERM); }
|
2015-06-12 16:01:12 +00:00
|
|
|
AUDITALLOWXPERM |
|
2015-09-18 19:57:56 +00:00
|
|
|
auditallowxperm { return(AUDITALLOWXPERM); }
|
2015-06-12 16:01:12 +00:00
|
|
|
DONTAUDITXPERM |
|
2015-09-18 19:57:56 +00:00
|
|
|
dontauditxperm { return(DONTAUDITXPERM); }
|
|
|
|
NEVERALLOWXPERM |
|
|
|
|
neverallowxperm { return(NEVERALLOWXPERM); }
|
2008-08-19 19:30:36 +00:00
|
|
|
SOURCE |
|
|
|
|
source { return(SOURCE); }
|
|
|
|
TARGET |
|
|
|
|
target { return(TARGET); }
|
|
|
|
SAMEUSER |
|
|
|
|
sameuser { return(SAMEUSER);}
|
|
|
|
module|MODULE { return(MODULE); }
|
|
|
|
require|REQUIRE { return(REQUIRE); }
|
|
|
|
optional|OPTIONAL { return(OPTIONAL); }
|
|
|
|
OR |
|
|
|
|
or { return(OR);}
|
|
|
|
AND |
|
|
|
|
and { return(AND);}
|
|
|
|
NOT |
|
|
|
|
not { return(NOT);}
|
|
|
|
xor |
|
|
|
|
XOR { return(XOR); }
|
|
|
|
eq |
|
|
|
|
EQ { return(EQUALS);}
|
|
|
|
true |
|
|
|
|
TRUE { return(CTRUE); }
|
|
|
|
false |
|
|
|
|
FALSE { return(CFALSE); }
|
|
|
|
dom |
|
|
|
|
DOM { return(DOM);}
|
|
|
|
domby |
|
|
|
|
DOMBY { return(DOMBY);}
|
|
|
|
INCOMP |
|
|
|
|
incomp { return(INCOMP);}
|
|
|
|
fscon |
|
|
|
|
FSCON { return(FSCON);}
|
2017-05-22 13:08:23 +00:00
|
|
|
ibpkeycon |
|
|
|
|
IBPKEYCON { return(IBPKEYCON);}
|
2017-05-22 13:08:26 +00:00
|
|
|
ibendportcon |
|
|
|
|
IBENDPORTCON { return(IBENDPORTCON);}
|
2008-08-19 19:30:36 +00:00
|
|
|
portcon |
|
|
|
|
PORTCON { return(PORTCON);}
|
|
|
|
netifcon |
|
|
|
|
NETIFCON { return(NETIFCON);}
|
|
|
|
nodecon |
|
|
|
|
NODECON { return(NODECON);}
|
2009-09-29 14:06:26 +00:00
|
|
|
pirqcon |
|
|
|
|
PIRQCON { return(PIRQCON);}
|
|
|
|
iomemcon |
|
|
|
|
IOMEMCON { return(IOMEMCON);}
|
|
|
|
ioportcon |
|
|
|
|
IOPORTCON { return(IOPORTCON);}
|
|
|
|
pcidevicecon |
|
|
|
|
PCIDEVICECON { return(PCIDEVICECON);}
|
2015-03-17 20:43:24 +00:00
|
|
|
devicetreecon |
|
|
|
|
DEVICETREECON { return(DEVICETREECON);}
|
2008-08-19 19:30:36 +00:00
|
|
|
fs_use_xattr |
|
|
|
|
FS_USE_XATTR { return(FSUSEXATTR);}
|
|
|
|
fs_use_task |
|
|
|
|
FS_USE_TASK { return(FSUSETASK);}
|
|
|
|
fs_use_trans |
|
|
|
|
FS_USE_TRANS { return(FSUSETRANS);}
|
|
|
|
genfscon |
|
|
|
|
GENFSCON { return(GENFSCON);}
|
|
|
|
r1 |
|
|
|
|
R1 { return(R1); }
|
|
|
|
r2 |
|
|
|
|
R2 { return(R2); }
|
|
|
|
r3 |
|
|
|
|
R3 { return(R3); }
|
|
|
|
u1 |
|
|
|
|
U1 { return(U1); }
|
|
|
|
u2 |
|
|
|
|
U2 { return(U2); }
|
|
|
|
u3 |
|
|
|
|
U3 { return(U3); }
|
|
|
|
t1 |
|
|
|
|
T1 { return(T1); }
|
|
|
|
t2 |
|
|
|
|
T2 { return(T2); }
|
|
|
|
t3 |
|
|
|
|
T3 { return(T3); }
|
|
|
|
l1 |
|
|
|
|
L1 { return(L1); }
|
|
|
|
l2 |
|
|
|
|
L2 { return(L2); }
|
|
|
|
h1 |
|
|
|
|
H1 { return(H1); }
|
|
|
|
h2 |
|
|
|
|
H2 { return(H2); }
|
|
|
|
policycap |
|
|
|
|
POLICYCAP { return(POLICYCAP); }
|
|
|
|
permissive |
|
|
|
|
PERMISSIVE { return(PERMISSIVE); }
|
2011-12-05 18:28:51 +00:00
|
|
|
default_user |
|
|
|
|
DEFAULT_USER { return(DEFAULT_USER); }
|
|
|
|
default_role |
|
|
|
|
DEFAULT_ROLE { return(DEFAULT_ROLE); }
|
2012-12-18 16:41:25 +00:00
|
|
|
default_type |
|
|
|
|
DEFAULT_TYPE { return(DEFAULT_TYPE); }
|
2011-12-05 18:28:51 +00:00
|
|
|
default_range |
|
|
|
|
DEFAULT_RANGE { return(DEFAULT_RANGE); }
|
|
|
|
low-high |
|
|
|
|
LOW-HIGH { return(LOW_HIGH); }
|
|
|
|
high |
|
|
|
|
HIGH { return(HIGH); }
|
|
|
|
low |
|
|
|
|
LOW { return(LOW); }
|
2019-09-09 18:05:57 +00:00
|
|
|
glblub |
|
|
|
|
GLBLUB { return(GLBLUB); }
|
2016-07-14 14:48:47 +00:00
|
|
|
"/"[^ \n\r\t\f]* { return(PATH); }
|
|
|
|
\""/"[^\"\n]*\" { return(QPATH); }
|
|
|
|
\"[^"/"\"\n]+\" { return(FILENAME); }
|
2008-10-14 14:57:24 +00:00
|
|
|
{letter}({alnum}|[_\-])*([\.]?({alnum}|[_\-]))* { return(IDENTIFIER); }
|
2009-09-29 14:06:26 +00:00
|
|
|
{digit}+|0x{hexval}+ { return(NUMBER); }
|
2015-03-10 16:39:17 +00:00
|
|
|
{alnum}*{letter}{alnum}* { return(FILESYSTEM); }
|
2024-05-08 17:04:22 +00:00
|
|
|
{digit}{1,3}(\.{digit}{1,3}){3}"/"{digit}{1,2} { return(IPV4_CIDR); }
|
2008-08-19 19:30:36 +00:00
|
|
|
{digit}{1,3}(\.{digit}{1,3}){3} { return(IPV4_ADDR); }
|
|
|
|
{hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])* { return(IPV6_ADDR); }
|
2024-05-08 17:04:22 +00:00
|
|
|
{hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*"/"{digit}{1,3} { return(IPV6_CIDR); }
|
2008-08-19 19:30:36 +00:00
|
|
|
{digit}+(\.({alnum}|[_.])*)? { return(VERSION_IDENTIFIER); }
|
|
|
|
#line[ ]1[ ]\"[^\n]*\" { set_source_file(yytext+9); }
|
2021-09-14 12:48:27 +00:00
|
|
|
#line[ ]{digit}+ {
|
|
|
|
errno = 0;
|
|
|
|
source_lineno = strtoul(yytext+6, NULL, 10) - 1;
|
|
|
|
if (errno) {
|
|
|
|
yywarn("source line number too big");
|
|
|
|
}
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
#[^\n]* { /* delete comments */ }
|
|
|
|
[ \t\f]+ { /* delete whitespace */ }
|
|
|
|
"==" { return(EQUALS); }
|
|
|
|
"!=" { return (NOTEQUAL); }
|
|
|
|
"&&" { return (AND); }
|
|
|
|
"||" { return (OR); }
|
|
|
|
"!" { return (NOT); }
|
|
|
|
"^" { return (XOR); }
|
|
|
|
"," |
|
|
|
|
":" |
|
|
|
|
";" |
|
|
|
|
"(" |
|
|
|
|
")" |
|
|
|
|
"{" |
|
|
|
|
"}" |
|
|
|
|
"[" |
|
|
|
|
"-" |
|
|
|
|
"." |
|
|
|
|
"]" |
|
|
|
|
"~" |
|
|
|
|
"*" { return(yytext[0]); }
|
2024-03-22 14:50:48 +00:00
|
|
|
. { yyerror("unrecognized character");
|
|
|
|
/* Available since bison 3.6, avoids duplicate error message */
|
|
|
|
#ifdef YYerror
|
|
|
|
return YYerror;
|
|
|
|
#else
|
|
|
|
return INVALID_CHAR;
|
|
|
|
#endif
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
%%
|
2014-09-14 21:41:39 +00:00
|
|
|
int yyerror(const char *msg)
|
2008-08-19 19:30:36 +00:00
|
|
|
{
|
2024-01-22 13:54:53 +00:00
|
|
|
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
2024-03-22 14:50:49 +00:00
|
|
|
const char *token;
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
if (isprint((unsigned char)yytext[0])) {
|
|
|
|
token = yytext;
|
|
|
|
} else {
|
|
|
|
snprintf(buf, sizeof(buf), "%#x", yytext[0]);
|
|
|
|
token = buf;
|
|
|
|
}
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
if (source_file[0])
|
2021-11-12 15:41:50 +00:00
|
|
|
fprintf(stderr, "%s:%lu:",
|
2008-08-19 19:30:36 +00:00
|
|
|
source_file, source_lineno);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "(unknown source)::");
|
2021-11-12 15:41:50 +00:00
|
|
|
fprintf(stderr, "ERROR '%s' at token '%s' on line %lu:\n%s\n%s\n",
|
2008-08-19 19:30:36 +00:00
|
|
|
msg,
|
2024-03-22 14:50:49 +00:00
|
|
|
token,
|
2008-08-19 19:30:36 +00:00
|
|
|
policydb_lineno,
|
|
|
|
linebuf[0], linebuf[1]);
|
2024-01-22 13:54:53 +00:00
|
|
|
#else
|
|
|
|
(void)msg;
|
|
|
|
#endif
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
policydb_errors++;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-14 21:41:39 +00:00
|
|
|
int yywarn(const char *msg)
|
2008-08-19 19:30:36 +00:00
|
|
|
{
|
2020-03-05 18:40:34 +00:00
|
|
|
if (werror)
|
|
|
|
return yyerror(msg);
|
|
|
|
|
2024-01-22 13:54:53 +00:00
|
|
|
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
2008-08-19 19:30:36 +00:00
|
|
|
if (source_file[0])
|
2021-11-12 15:41:50 +00:00
|
|
|
fprintf(stderr, "%s:%lu:",
|
2008-08-19 19:30:36 +00:00
|
|
|
source_file, source_lineno);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "(unknown source)::");
|
2021-11-12 15:41:50 +00:00
|
|
|
fprintf(stderr, "WARNING '%s' at token '%s' on line %lu:\n%s\n%s\n",
|
2008-08-19 19:30:36 +00:00
|
|
|
msg,
|
|
|
|
yytext,
|
|
|
|
policydb_lineno,
|
|
|
|
linebuf[0], linebuf[1]);
|
2024-01-22 13:54:53 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_source_file(const char *name)
|
|
|
|
{
|
|
|
|
source_lineno = 1;
|
|
|
|
strncpy(source_file, name, sizeof(source_file)-1);
|
|
|
|
source_file[sizeof(source_file)-1] = '\0';
|
2014-03-05 21:06:42 +00:00
|
|
|
if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
|
|
|
|
source_file[strlen(source_file)-1] = '\0';
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|