-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
397 lines (363 loc) · 9.68 KB
/
grammar.js
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
module.exports = grammar({
name: "selinux",
extras: ($) => [
/\s|\\r?\\n/, // whitespace
$.comment,
],
word: ($) => $.word,
// externals: ($) => [
// // Mark comments as external tokens so that the external scanner is always
// // invoked, even if no external token is expected. This allows for better
// // error recovery, because the external scanner can maintain the overall
// // structure by returning dedent tokens whenever a dedent occurs, even
// // if no dedent is expected.
// $.comment,
//
// // Allow the external scanner to check for the validity of closing brackets
// // so that it can avoid returning dedent tokens between brackets.
// "]",
// ")",
// "}",
// ],
rules: {
// INFO:
// gen_require inside macros
// highlit of `'. Match?
// TODO:
// interface macro
// User statements
// Role statements: role, attribute_role, roleattribute, allow, role_transition
// Bounds rules: typebounds
// Extended rules
// Object class
// Conditional statements: bool, if/else
// Constraint statements: constrain, validatetrans, mlsconstrain, mlsvalidatetrans
// MLS statements. Good for Android
// SID statement
// Filesystem labeling statements
// Modular policy support statements: module, require, optional
// File context configuration files (*.fc)
source_file: ($) => repeat($._definition),
_definition: ($) =>
choice(
$.macro_declaration,
$.macro_usage,
// Type statements
$.type_declaration,
$.attribute_declaration,
$.expandattribute_declaration,
$.typeattribute_declaration,
$.typealias_declaration,
$.permissive_declaration,
$.type_transition_declaration,
$.type_change_declaration,
$.type_member_declaration,
// ...
$.role_declaration,
$.rule_declaration,
$.boolean_declaration,
),
comment: ($) => token(seq("#", /.*/)),
/*
* Type statements
*/
// TODO: add tests for type statements. See examples in the SELinux notebook
// Improve the highlights
type_declaration: ($) =>
seq(
"type",
field("type_id", $.identifier),
optional(seq("alias", field("alias_id", $.alias))),
repeat(seq(",", field("attribute_id", $.identifier))),
";",
),
attribute_declaration: ($) =>
seq("attribute", field("attribute_id", $.identifier), ";"),
expandattribute_declaration: ($) =>
seq(
"expandattribute",
field("attribute_id", $.attribute),
field("expand_value", $.boolean),
";",
),
typeattribute_declaration: ($) =>
seq(
"typeattribute",
field("type_id", $.identifier),
field("attribute_id", $.identifier),
repeat(seq(",", field("attribute_id", $.identifier))),
";",
),
typealias_declaration: ($) =>
seq(
"typealias",
field("type_id", $.identifier),
"alias",
field("alias_id", $.alias),
";",
),
permissive_declaration: ($) =>
seq("permissive", field("type_id", $.identifier), ";"),
type_transition_declaration: ($) =>
seq(
"type_transition",
field("source_type", $.type),
field("target_type", $.type),
":",
field("class", $.class),
field("default_type", $.identifier),
optional(field("object_name", $.identifier)),
";",
),
type_change_declaration: ($) =>
seq(
"type_change",
field("source_type", $.type),
field("target_type", $.type),
":",
field("class", $.class),
field("change_type", $.identifier),
";",
),
type_member_declaration: ($) =>
seq(
"type_member",
field("source_type", $.type),
field("target_type", $.type),
":",
field("class", $.class),
field("member_type", $.identifier),
";",
),
/*
* ...
*/
role_declaration: ($) =>
seq(
"role",
field("name", $.identifier),
"types",
field("type", $.identifier),
";",
),
class: ($) =>
choice(
seq("{", repeat1(field("class", $.classes)), "}"),
field("class", $.classes),
),
type: ($) =>
choice(
seq(
optional($.complement),
"{",
seq(
field("type", $.identifier),
repeat1(seq(optional($.negative), field("type", $.identifier))),
),
"}",
),
seq(optional($.complement), field("type", $.identifier)),
),
// TODO: Maybe simplify this since I have multiple identical definitions
// (only difference is the name, which is not that relevant)
alias: ($) =>
choice(
seq("{", seq(repeat1(field("alias", $.identifier))), "}"),
field("alias", $.identifier),
),
attribute: ($) =>
choice(
seq("{", seq(repeat1(field("attribute", $.identifier))), "}"),
field("attribute", $.identifier),
),
permission: ($) =>
choice(
seq(
optional($.complement),
"{",
seq(
field("perm_set", $.permissions),
repeat1(field("perm_set", $.permissions)),
),
"}",
),
seq(optional($.complement), field("perm_set", $.permissions)),
),
// https://selinuxproject.org/page/AVCRules
rule_declaration: ($) =>
seq(
field("rule_name", $.rule_name),
field("source_type", $.type),
field("target_type", $.type),
":",
field("class", $.class),
field("perm_set", $.permission),
";",
),
boolean_declaration: ($) =>
seq("bool", field("name", $.word), field("value", $.boolean), ";"),
argument: ($) =>
choice(
seq(
"{",
seq(
field("arg", $.identifier),
repeat1(seq(optional($.negative), field("arg", $.identifier))),
),
"}",
),
field("arg", $.identifier),
),
arguments: ($) =>
seq(
field("argument", $.argument),
repeat(seq(",", field("argument", $.argument))),
),
macro_declaration: ($) =>
seq(
"interface",
"(",
"`",
field("name", $.word),
"'",
",",
"`",
field(
"body",
choice(
$.type_declaration,
$.typeattribute_declaration,
$.rule_declaration,
$.macro_usage,
),
),
"'",
")",
),
macro_usage: ($) =>
seq(
field("name", $.word),
"(",
field("arguments", optional($.arguments)),
")",
),
negative: ($) => "-",
complement: ($) => "~",
boolean: ($) => choice("true", "false"),
word: ($) => /[a-zA-Z][a-zA-Z0-9_-]*/,
expansion: ($) => seq("$", /[0-9]+/),
identifier: ($) => choice($.word, $.expansion),
permissions: ($) => choice($.identifier, "*"),
rule_name: ($) => choice("allow", "auditallow", "dontaudit", "neverallow"),
// I guess this is a bit overkill, but allows me to know if I'm using a valid class
// From the SELinux Notebook
classes: ($) =>
choice(
"anon_inode",
"appletalk_socket",
"association",
"binder",
"blk_file",
"bpf",
"cap2_userns",
"cap_userns",
"capability",
"capability2",
"chr_file",
"context",
"db_blob",
"db_column",
"db_database",
"db_language",
"db_procedure",
"db_schema",
"db_sequence",
"db_table",
"db_tuple",
"db_view",
"dbus",
"dccp_socket",
"dir",
"fd",
"fifo_file",
"file",
"filesystem",
"icmp_socket",
"infiniband_endport",
"infiniband_pkey",
"io_uring",
"ipc",
"kernel_service",
"key",
"key_socket",
"lnk_file",
"lockdown",
"memprotect",
"msg",
"msgq",
"netif",
"netlink_audit_socket",
"netlink_connector_socket",
"netlink_crypto_socket",
"netlink_dnrt_socket",
"netlink_fib_lookup_socket",
"netlink_firewall_socket",
"netlink_generic_socket",
"netlink_ip6fw_socket",
"netlink_iscsi_socket",
"netlink_kobject_uevent_socket",
"netlink_netfilter_socket",
"netlink_nflog_socket",
"netlink_rdma_socket",
"netlink_route_socket",
"netlink_scsitransport_socket",
"netlink_selinux_socket",
"netlink_socket",
"netlink_tcpdiag_socket",
"netlink_xfrm_socket",
"node",
"nscd",
"packet",
"packet_socket",
"passwd",
"peer",
"perf_event",
"process",
"process2",
"proxy",
"rawip_socket",
"sctp_socket",
"security",
"sem",
"service",
"shm",
"sock_file",
"socket",
"system",
"tcp_socket",
"tun_socket",
"udp_socket",
"unix_dgram_socket",
"unix_stream_socket",
"user_namespace",
"x_application_data",
"x_client",
"x_colormap",
"x_cursor",
"x_device",
"x_drawable",
"x_event",
"x_extension",
"x_font",
"x_gc",
"x_keyboard",
"x_pointer",
"x_property",
"x_resource",
"x_screen",
"x_selection",
"x_server",
"x_synthetic_event",
),
},
});