forked from google/kafel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
408 lines (356 loc) · 10.6 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Kafel - parser
-----------------------------------------
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
%output "parser.c"
%defines "parser.h"
%{
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#include "lexer.h"
void yyerror(YYLTYPE * loc, struct kafel_ctxt* ctxt, yyscan_t scanner,
const char *msg);
#define emit_error(loc, fmt, ...) \
do { \
append_error(ctxt, "%d:%d: "fmt, (loc).first_line, \
(loc).first_column, ##__VA_ARGS__); \
} while(0) \
#define INVALID_ARG_SIZE -1
%}
%code requires {
#include <stdint.h>
#include "context.h"
#include "expression.h"
#include "policy.h"
#include "syscall.h"
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
}
%code provides {
#define YYSTYPE KAFEL_YYSTYPE
typedef union YYSTYPE YYSTYPE;
#define YYLTYPE KAFEL_YYLTYPE
typedef struct YYLTYPE YYLTYPE;
#define YY_DECL \
int kafel_yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param, \
struct kafel_ctxt* ctxt, yyscan_t yyscanner)
YY_DECL;
}
%define parse.error verbose
%define api.pure full
%define api.prefix {kafel_yy}
%locations
%lex-param {struct kafel_ctxt* ctxt}
%lex-param {yyscan_t scanner}
%parse-param {struct kafel_ctxt* ctxt}
%parse-param {yyscan_t scanner}
%union {
char *id;
uint32_t syscall_nr;
uint32_t action;
uint64_t number;
struct expr_tree *expr;
struct syscall_descriptor* syscall_desc;
struct syscall_filter *filter;
struct filterslist filters;
struct policy_entry *entry;
struct entrieslist entries;
struct policy *policy;
}
%token BIT_AND BIT_OR LOGIC_OR LOGIC_AND
%token IDENTIFIER NUMBER
%token POLICY USE DEFAULT SYSCALL DEFINE
%token ALLOW KILL DENY ERRNO TRAP TRACE
%token GT LT GE LE EQ NEQ
%type <id> IDENTIFIER
%type <number> NUMBER
%type <policy> policy
%type <entries> policy_statements
%type <entry> policy_statement use_statement action_block
%type <action> action
%type <filters> syscall_filters
%type <filter> syscall_filter
%type <syscall_desc> syscall_id
%type <syscall_nr> syscall
%type <expr> bool_expr or_bool_expr and_bool_expr primary_bool_expr masked_op
%type <expr> operand
%destructor { policy_destroy(&$$); } <policy>
%destructor { policy_entry_destroy(&$$); } <entry>
%destructor { policy_entries_destroy(&$$); } <entries>
%destructor { syscall_filter_destroy(&$$); } <filter>
%destructor { syscall_filters_destroy(&$$); } <filters>
%destructor { syscall_descriptor_destroy(&$$); } <syscall_desc>
%destructor { expr_destroy(&$$); } <expr>
%destructor { free($$); } <id>
%%
program
: maybe_const_defs policies
| maybe_const_defs policies use_policy
;
policies
: policy
{
register_policy(ctxt, $1);
}
| policies policy
{
if (lookup_policy(ctxt, $2->name) != NULL) {
emit_error(@2, "Redefinition of policy `%s'", $2->name);
policy_destroy(&$2);
YYERROR;
}
register_policy(ctxt, $2);
}
;
use_policy
: USE IDENTIFIER DEFAULT action
{
struct policy* used = lookup_policy(ctxt, $2);
if (used == NULL) {
emit_error(@2, "Undefined policy `%s'", $2);
free($2); $2 = NULL;
YYERROR;
}
ctxt->used_policy = used;
ctxt->default_action = $4;
free($2);
}
;
policy
: POLICY IDENTIFIER '{' '}' { $$ = policy_create($2, NULL); free($2); }
| POLICY IDENTIFIER '{' policy_statements '}'
{
$$ = policy_create($2, &$4);
free($2);
}
;
policy_statements
: policy_statement { TAILQ_INIT(&$$); TAILQ_INSERT_TAIL(&$$, $1, entries); }
| policy_statements ',' policy_statement
{
TAILQ_INIT(&$$);
TAILQ_CONCAT(&$$, &$1, entries);
TAILQ_INSERT_TAIL(&$$, $3, entries);
}
;
policy_statement
: use_statement
| action_block
;
use_statement
: USE IDENTIFIER
{
struct policy* used = lookup_policy(ctxt, $2);
if (used == NULL) {
emit_error(@2, "Undefined policy `%s'", $2);
free($2); $2 = NULL;
YYERROR;
}
$$ = policy_use_create(used);
free($2);
}
;
action_block
: action '{' '}' { $$ = policy_action_create($1, NULL); }
| action '{' syscall_filters '}' { $$ = policy_action_create($1, &$3); }
;
action
: ALLOW { $$ = ACTION_ALLOW; }
| DENY { $$ = ACTION_KILL; }
| KILL { $$ = ACTION_KILL; }
| ERRNO '(' NUMBER ')'
{
if ($3 > 0xffff) {
emit_error(@3, "Errno value %"PRIu64" out of range (0-65535)", $3);
YYERROR;
}
$$ = ACTION_ERRNO | ($3 & 0xffff);
}
| TRACE '(' NUMBER ')'
{
if ($3 > 0xffff) {
emit_error(@3, "Trace value %"PRIu64" out of range (0-65535)", $3);
YYERROR;
}
$$ = ACTION_TRACE | ($3 & 0xffff);
}
| TRAP '(' NUMBER ')'
{
if ($3 > 0xffff) {
emit_error(@3, "Trap value %"PRIu64" out of range (0-65535)", $3);
YYERROR;
}
$$ = ACTION_TRAP | ($3 & 0xffff);
}
;
syscall_filters
: syscall_filter { TAILQ_INIT(&$$); TAILQ_INSERT_TAIL(&$$, $1, filters); }
| syscall_filters ',' syscall_filter
{
TAILQ_INIT(&$$);
TAILQ_CONCAT(&$$, &$1, filters);
TAILQ_INSERT_TAIL(&$$, $3, filters);
}
;
syscall_filter
: syscall { $$ = syscall_filter_create($1, NULL); }
| syscall '{' '}' { $$ = syscall_filter_create($1, NULL); }
| syscall '{' bool_expr '}' { $$ = syscall_filter_create($1, $3); }
;
syscall_id
: IDENTIFIER
{
uint64_t value = 0;
if (lookup_const(ctxt, $1, &value) == 0) {
$$ = syscall_custom(value);
} else {
$$ = (struct syscall_descriptor*)
syscall_lookup(ctxt->syscalls, $1);
if ($$ == NULL) {
emit_error(@1, "Undefined syscall `%s'", $1);
free($1); $1 = NULL;
YYERROR;
}
}
free($1);
}
| SYSCALL '[' NUMBER ']'
{
$$ = syscall_custom($3);
}
;
syscall
: syscall_id
{
$$ = $1->nr;
register_ftrace_args(ctxt, $1);
syscall_descriptor_destroy(&$1);
}
| syscall_id '(' ')'
{
$$ = $1->nr;
clean_args(ctxt);
syscall_descriptor_destroy(&$1);
}
| syscall_id '(' syscall_args ')'
{
$$ = $1->nr;
for (int i = 0; i < SYSCALL_MAX_ARGS; ++i) {
if (ctxt->syscall.args[i].size == INVALID_ARG_SIZE) {
ctxt->syscall.args[i].size = 8;
if ($1->args[i].name != NULL) {
ctxt->syscall.args[i].size = $1->args[i].size;
}
}
}
syscall_descriptor_destroy(&$1);
}
;
syscall_args
: IDENTIFIER
{
register_first_arg(ctxt, $1, INVALID_ARG_SIZE);
free($1);
}
| syscall_args ',' IDENTIFIER
{
if (lookup_var(ctxt, $3) >= 0) {
emit_error(@3, "Redefinition of argument `%s'", $3);
free($3); $3 = NULL;
YYERROR;
}
if (register_arg(ctxt, $3, INVALID_ARG_SIZE)) {
emit_error(@3, "Too many arguments defined for syscall");
free($3); $3 = NULL;
YYERROR;
}
free($3);
}
;
bool_expr
: bool_expr ',' or_bool_expr { $$ = expr_create_binary(EXPR_OR, $1, $3); }
| or_bool_expr { $$ = $1; }
;
or_bool_expr
: or_bool_expr LOGIC_OR and_bool_expr
{ $$ = expr_create_binary(EXPR_OR, $1, $3); }
| and_bool_expr { $$ = $1; }
;
and_bool_expr
: and_bool_expr LOGIC_AND primary_bool_expr
{ $$ = expr_create_binary(EXPR_AND, $1, $3); }
| primary_bool_expr { $$ = $1; }
;
primary_bool_expr
: '(' bool_expr ')' { $$ = $2; }
| '!' primary_bool_expr { $$ = expr_create_unary(EXPR_NOT, $2); }
| masked_op GT masked_op { $$ = expr_create_binary(EXPR_GT, $1, $3); }
| masked_op LT masked_op { $$ = expr_create_binary(EXPR_LT, $1, $3); }
| masked_op GE masked_op { $$ = expr_create_binary(EXPR_GE, $1, $3); }
| masked_op LE masked_op { $$ = expr_create_binary(EXPR_LE, $1, $3); }
| masked_op EQ masked_op { $$ = expr_create_binary(EXPR_EQ, $1, $3); }
| masked_op NEQ masked_op { $$ = expr_create_binary(EXPR_NEQ, $1, $3); }
;
masked_op
: operand BIT_AND operand { $$ = expr_create_binary(EXPR_BIT_AND, $1, $3); }
| operand { $$ = $1; }
| '(' masked_op ')' { $$ = $2; }
;
operand
: NUMBER { $$ = expr_create_number($1); }
| IDENTIFIER
{
uint64_t value = 0;
if (lookup_const(ctxt, $1, &value) == 0) {
$$ = expr_create_number(value);
} else {
int var = lookup_var(ctxt, $1);
if (var < 0) {
emit_error(@1, "Undefined argument `%s'", $1);
free($1); $1 = NULL;
YYERROR;
}
$$ = expr_create_var(var, ctxt->syscall.args[var].size);
}
free($1);
}
;
maybe_const_defs
: %empty
| const_defs
;
const_defs
: const_def
| const_defs const_def
;
const_def
: '#' DEFINE IDENTIFIER NUMBER
{
if (lookup_const(ctxt, $3, NULL) == 0) {
emit_error(@1, "Redefinition of constant `%s'", $3);
}
register_const(ctxt, $3, $4);
free($3);
}
;
%%
void yyerror(YYLTYPE * loc, struct kafel_ctxt* ctxt, yyscan_t scanner,
const char *msg) {
if (!ctxt->lexical_error) {
append_error(ctxt, "%d:%d: %s", loc->first_line, loc->first_column, msg);
}
}