Debian Codegraph @

src/OVAL/probes/independent/yamlfilecontent_probe.c

raw symbols SCIP
1 /*
2 * Copyright 2020 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Jan Černý <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <math.h>
28 #include <errno.h>
29 #include <yaml.h>
30 #include <yaml-path.h>
31
32 #include "yamlfilecontent_probe.h"
33 #include "sexp-manip.h"
34 #include "debug_priv.h"
35 #include "oval_fts.h"
36 #include "list.h"
37 #include "probe/probe.h"
38 #include "common/oscap_pcre.h"
39
40 #define OSCAP_YAML_STRING_TAG "tag:yaml.org,2002:str"
41 #define OSCAP_YAML_BOOL_TAG "tag:yaml.org,2002:bool"
42 #define OSCAP_YAML_FLOAT_TAG "tag:yaml.org,2002:float"
43 #define OSCAP_YAML_INT_TAG "tag:yaml.org,2002:int"
44 #define OSCAP_YAML_NULL_TAG "tag:yaml.org,2002:null"
45
46 #define OVECCOUNT 30 /* should be a multiple of 3 */
47
48 int yamlfilecontent_probe_offline_mode_supported()
49 {
50 return PROBE_OFFLINE_OWN;
51 }
52
53 static bool match_regex(const char *pattern, const char *value)
54 {
55 char *errptr;
56 int erroroffset;
57 oscap_pcre_t *re = oscap_pcre_compile(pattern, 0, &errptr, &erroroffset);
58 if (re == NULL) {
59 dE("oscap_pcre_compile failed on pattern '%s': %s at %d", pattern,
60 errptr, erroroffset);
61 oscap_pcre_err_free(errptr);
62 return false;
63 }
64 int ovector[OVECCOUNT];
65 int rc = oscap_pcre_exec(re, value, strlen(value), 0, 0, ovector, OVECCOUNT);
66 oscap_pcre_free(re);
67 if (rc > 0) {
68 return true;
69 }
70 return false;
71 }
72
73 static SEXP_t *yaml_scalar_event_to_sexp(yaml_event_t *event)
74 {
75 char *tag = (char *) event->data.scalar.tag;
76 char *value = (char *) event->data.scalar.value;
77
78 /* Nodes lacking an explicit tag are given a non-specific tag:
79 * “!” for non-plain scalars, and “?” for all other nodes
80 */
81 if (tag == NULL) {
82 if (event->data.scalar.style != YAML_PLAIN_SCALAR_STYLE) {
83 tag = "!";
84 } else {
85 tag = "?";
86 }
87 }
88
89 /* Nodes with "!" tag can be sequences, maps or strings, but we process
90 * only scalars in this functions, so they can only be strings. */
91 if (!strcmp(tag, "!")) {
92 tag = OSCAP_YAML_STRING_TAG;
93 }
94
95 bool question = !strcmp(tag, "?");
96
97 /* Regular expressions based on https://yaml.org/spec/1.2/spec.html#id2804923 */
98
99 if (question || !strcmp(tag, OSCAP_YAML_BOOL_TAG)) {
100 if (match_regex("^(true|True|TRUE)$", value)) {
101 return SEXP_number_newb(true);
102 } else if (match_regex("^(false|False|FALSE)$", value)) {
103 return SEXP_number_newb(false);
104 } else if (!question) {
105 return NULL;
106 }
107 }
108 if (question || !strcmp(tag, OSCAP_YAML_INT_TAG)) {
109 if (match_regex("^[+-]?[0-9]+$", value)) {
110 int int_value = strtol(value, NULL, 10);
111 return SEXP_number_newi(int_value);
112 } else if (match_regex("^0o[0-7]+$", value)) {
113 /* strtol doesn't understand 0o as octal prefix, it wants 0 */
114 int int_value = strtol(value + 2, NULL, 8);
115 return SEXP_number_newi(int_value);
116 } else if (match_regex("^0x[0-9a-fA-F]+$", value)) {
117 int int_value = strtol(value, NULL, 16);
118 return SEXP_number_newi(int_value);
119 } else if (!question) {
120 return NULL;
121 }
122 }
123 if (question || !strcmp(tag, OSCAP_YAML_FLOAT_TAG)) {
124 if (match_regex("^[-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$", value)) {
125 double double_value = strtod(value, NULL);
126 return SEXP_number_newf(double_value);
127 } else if (match_regex("^[-+]?(\\.inf|\\.Inf|\\.INF)$", value)) {
128 double double_value = INFINITY;
129 if (value[0] == '-') {
130 double_value = -INFINITY;
131 }
132 return SEXP_number_newf(double_value);
133 } else if (match_regex("^(\\.nan|\\.NaN|\\.NAN)$", value)) {
134 double double_value = NAN;
135 return SEXP_number_newf(double_value);
136 } else if (!question) {
137 return NULL;
138 }
139 }
140 if (question || !strcmp(tag, OSCAP_YAML_NULL_TAG)) {
141 if (match_regex("^(null|Null|NULL|~|)$", value)) {
142 // TODO: Return real NULL when record's field will support nil="true"
143 return SEXP_string_new("(null)", strlen("(null)"));
144 } else if (!question) {
145 return NULL;
146 }
147 }
148
149 return SEXP_string_new(value, strlen(value));
150 }
151
152 static char *escape_key(char *key)
153 {
154 if (key == NULL)
155 return NULL;
156
157 size_t cap_letters = 0;
158 size_t key_len = strlen(key);
159 for (size_t i = 0; i < key_len; i++)
160 if ((key[i] >= 'A' && key[i] <= 'Z') || key[i] == '^')
161 cap_letters++;
162
163 if (cap_letters == 0)
164 return key;
165
166 char *new_key = realloc(key, key_len + 1 + cap_letters);
167 if (new_key == NULL)
168 return key;
169 new_key[key_len + cap_letters] = '\0';
170
171 for (ssize_t i = key_len; i >= 0; i--) {
172 if ((new_key[i] >= 'A' && new_key[i] <= 'Z') || new_key[i] == '^') {
173 if (new_key[i] != '^')
174 new_key[i] += 32;
175 memmove(new_key + i + cap_letters, new_key + i, key_len - i);
176 new_key[i + cap_letters - 1] = '^';
177 cap_letters--;
178 key_len = i;
179 }
180 }
181
182 return new_key;
183 }
184
185 #define result_error(fmt, args...) \
186 do { \
187 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, fmt, args); \
188 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg); \
189 SEXP_free(msg); \
190 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); \
191 ret = -1; \
192 } while (0)
193
194 static int yaml_path_query(yaml_parser_t *parser, const char *yaml_path_cstr, struct oscap_list *values, probe_ctx *ctx)
195 {
196 int ret = 0;
197
198 yaml_path_t *yaml_path = yaml_path_create();
199 if (yaml_path_parse(yaml_path, (char *) yaml_path_cstr)) {
200 result_error("Invalid YAML path '%s': %s", yaml_path_cstr, yaml_path_error_get(yaml_path)->message);
201 yaml_path_destroy(yaml_path);
202 return ret;
203 };
204
205 yaml_event_t event;
206 yaml_event_type_t event_type;
207 bool sequence = false;
208 bool mapping = false;
209 bool fake_mapping = false;
210 int index = 0;
211 char *key = strdup("#");
212
213 struct oscap_htable *record = NULL;
214
215 do {
216 if (!yaml_parser_parse(parser, &event)) {
217 result_error("YAML parser error: %s", parser->problem);
218 goto cleanup;
219 }
220
221 event_type = event.type;
222
223 if (yaml_path_filter_event(yaml_path, parser, &event) == YAML_PATH_FILTER_RESULT_OUT) {
224 goto next;
225 }
226
227 if (sequence) {
228 if (event_type == YAML_SEQUENCE_END_EVENT) {
229 if (fake_mapping) {
230 fake_mapping = false;
231 if (record && record->itemcount > 0) {
232 oscap_list_add(values, record);
233 } else {
234 // Do not collect empty records
235 oscap_htable_free0(record);
236 }
237 record = NULL;
238 } else {
239 sequence = false;
240 }
241 } else if (event_type == YAML_SEQUENCE_START_EVENT) {
242 if (mapping || fake_mapping) {
243 result_error("YAML path '%s' points to a multi-dimensional structure (a map or a sequence containing other sequences)", yaml_path_cstr);
244 goto cleanup;
245 } else {
246 fake_mapping = true;
247 record = oscap_htable_new();
248 }
249 }
250 } else {
251 if (event_type == YAML_SEQUENCE_START_EVENT) {
252 sequence = true;
253 if (mapping)
254 index++;
255 }
256 }
257
258 if (mapping) {
259 if (event_type == YAML_MAPPING_END_EVENT) {
260 mapping = false;
261 if (record && record->itemcount > 0) {
262 oscap_list_add(values, record);
263 } else {
264 // Do not collect empty records
265 oscap_htable_free0(record);
266 }
267 record = NULL;
268 } else if (event_type == YAML_MAPPING_START_EVENT) {
269 result_error("YAML path '%s' points to a multi-dimensional structure (map containing another map)", yaml_path_cstr);
270 goto cleanup;
271 }
272 } else {
273 if (event_type == YAML_MAPPING_START_EVENT) {
274 if (record) {
275 result_error("YAML path '%s' points to an invalid structure (map containing another map)", yaml_path_cstr);
276 goto cleanup;
277 }
278 if (fake_mapping) {
279 result_error("YAML path '%s' points to a multi-dimensional structure (two-dimensional sequence containing a map)", yaml_path_cstr);
280 goto cleanup;
281 }
282 mapping = true;
283 sequence = false;
284 index = 0;
285 record = oscap_htable_new();
286 }
287 }
288
289 if (event_type == YAML_SCALAR_EVENT) {
290 if (mapping) {
291 if (!sequence) {
292 if (index++ % 2 == 0) {
293 free(key);
294 key = escape_key(strdup((const char *) event.data.scalar.value));
295 goto next;
296 }
297 }
298 }
299
300 SEXP_t *sexp = yaml_scalar_event_to_sexp(&event);
301 if (sexp == NULL) {
302 result_error("Can't convert '%s %s' to SEXP", event.data.scalar.tag, event.data.scalar.value);
303 goto cleanup;
304 }
305
306 if (!record)
307 record = oscap_htable_new();
308 struct oscap_list *field = oscap_htable_get(record, key);
309 if (!field) {
310 field = oscap_list_new();
311 oscap_htable_add(record, key, field);
312 }
313
314 oscap_list_add(field, sexp);
315 }
316 next:
317 yaml_event_delete(&event);
318 } while (event_type != YAML_STREAM_END_EVENT);
319
320 cleanup:
321 if (event.type != 0)
322 yaml_event_delete(&event);
323 if (record)
324 oscap_list_add(values, record);
325 free(key);
326 yaml_path_destroy(yaml_path);
327
328 return ret;
329 }
330
331 static void record_free(struct oscap_list *items)
332 {
333 oscap_list_free(items, (oscap_destruct_func) SEXP_free);
334 }
335
336 static void values_free(struct oscap_htable *record)
337 {
338 oscap_htable_free(record, (oscap_destruct_func) record_free);
339 }
340
341 static int process_yaml(yaml_parser_t *parser, const char *yamlpath, SEXP_t *item, probe_ctx *ctx)
342 {
343 int ret = 0;
344
345 if (SEXP_typeof(item) != SEXP_TYPE_LIST)
346 return -1;
347
348 struct oscap_list *values = oscap_list_new();
349
350 if (yaml_path_query(parser, yamlpath, values, ctx)) {
351 ret = -1;
352 goto cleanup;
353 }
354
355 struct oscap_iterator *values_it = oscap_iterator_new(values);
356 if (oscap_iterator_has_more(values_it)) {
357 while (oscap_iterator_has_more(values_it)) {
358 SEXP_t *result_ent = probe_ent_creat1("value", NULL, NULL);
359 probe_ent_setdatatype(result_ent, OVAL_DATATYPE_RECORD);
360 struct oscap_htable *record = oscap_iterator_next(values_it);
361 struct oscap_htable_iterator *record_it = oscap_htable_iterator_new(record);
362 while(oscap_htable_iterator_has_more(record_it)) {
363 const struct oscap_htable_item *record_item = oscap_htable_iterator_next(record_it);
364 struct oscap_iterator *item_value_it = oscap_iterator_new(record_item->value);
365 SEXP_t se_tmp_mem;
366 SEXP_t *key = SEXP_string_new_r(&se_tmp_mem, record_item->key, strlen(record_item->key));
367 while(oscap_iterator_has_more(item_value_it)) {
368 SEXP_t *value_sexp = oscap_iterator_next(item_value_it);
369 SEXP_t *field = probe_ent_creat1("field", NULL, value_sexp);
370 probe_item_attr_add(field, "name", key);
371 SEXP_list_add(result_ent, field);
372 SEXP_free(field);
373 }
374 oscap_iterator_free(item_value_it);
375 SEXP_free_r(&se_tmp_mem);
376 }
377 oscap_htable_iterator_free(record_it);
378 SEXP_list_add(item, result_ent);
379 SEXP_free(result_ent);
380 }
381 probe_item_collect(ctx, item);
382 }
383 oscap_iterator_free(values_it);
384
385 cleanup:
386 oscap_list_free(values, (oscap_destruct_func) values_free);
387
388 return ret;
389 }
390
391 static int process_yaml_file(const char *prefix, const char *path, const char *filename, const char *yamlpath, probe_ctx *ctx)
392 {
393 int ret = 0;
394
395 char *filepath = oscap_path_join(path, filename);
396 if (probe_path_is_blocked(filepath, ctx->blocked_paths)) {
397 free(filepath);
398 return 0;
399 }
400
401 yaml_parser_t parser;
402 yaml_parser_initialize(&parser);
403
404 char *filepath_with_prefix = oscap_path_join(prefix, filepath);
405
406 FILE *yaml_file = fopen(filepath_with_prefix, "r");
407 if (yaml_file == NULL) {
408 result_error("Unable to open file '%s': %s", filepath_with_prefix, strerror(errno));
409 goto cleanup;
410 }
411
412 yaml_parser_set_input_file(&parser, yaml_file);
413
414 SEXP_t *item = probe_item_create(
415 OVAL_INDEPENDENT_YAML_FILE_CONTENT,
416 NULL,
417 "filepath", OVAL_DATATYPE_STRING, filepath,
418 "path", OVAL_DATATYPE_STRING, path,
419 "filename", OVAL_DATATYPE_STRING, filename,
420 "yamlpath", OVAL_DATATYPE_STRING, yamlpath,
421 // TODO: Implement "windows_view",
422 NULL
423 );
424
425 if (process_yaml(&parser, yamlpath, item, ctx)) {
426 SEXP_free(item);
427 ret = -1;
428 goto cleanup;
429 }
430
431 cleanup:
432 if (yaml_file != NULL)
433 fclose(yaml_file);
434 yaml_parser_delete(&parser);
435 free(filepath_with_prefix);
436 free(filepath);
437
438 return ret;
439 }
440
441 static int process_yaml_content(const char *content, const char *yamlpath, probe_ctx *ctx)
442 {
443 int ret = 0;
444
445 yaml_parser_t parser;
446 yaml_parser_initialize(&parser);
447 yaml_parser_set_input_string(&parser, (unsigned char *) content, strlen(content));
448
449 SEXP_t *item = probe_item_create(
450 OVAL_INDEPENDENT_YAML_FILE_CONTENT,
451 NULL,
452 "content", OVAL_DATATYPE_STRING, content,
453 "yamlpath", OVAL_DATATYPE_STRING, yamlpath,
454 NULL
455 );
456
457 if (process_yaml(&parser, yamlpath, item, ctx)) {
458 SEXP_free(item);
459 ret = -1;
460 goto cleanup;
461 }
462
463 cleanup:
464 yaml_parser_delete(&parser);
465
466 return ret;
467 }
468
469 int yamlfilecontent_probe_main(probe_ctx *ctx, void *arg)
470 {
471 SEXP_t *probe_in = probe_ctx_getobject(ctx);
472 SEXP_t *behaviors_ent = probe_obj_getent(probe_in, "behaviors", 1);
473 SEXP_t *filepath_ent = probe_obj_getent(probe_in, "filepath", 1);
474 SEXP_t *path_ent = probe_obj_getent(probe_in, "path", 1);
475 SEXP_t *filename_ent = probe_obj_getent(probe_in, "filename", 1);
476 SEXP_t *yamlpath_ent = probe_obj_getent(probe_in, "yamlpath", 1);
477 SEXP_t *yamlpath_val = probe_ent_getval(yamlpath_ent);
478 char *yamlpath_str = SEXP_string_cstr(yamlpath_val);
479 SEXP_t *content_ent = probe_obj_getent(probe_in, "content", 1);
480 SEXP_t *content_val = probe_ent_getval(content_ent);
481 char *content_str = SEXP_string_cstr(content_val);
482
483 if (content_str != NULL) {
484 process_yaml_content(content_str, yamlpath_str, ctx);
485 } else {
486 probe_filebehaviors_canonicalize(&behaviors_ent);
487 const char *prefix = getenv("OSCAP_PROBE_ROOT");
488 OVAL_FTS *ofts = oval_fts_open_prefixed(
489 prefix, path_ent, filename_ent, filepath_ent, behaviors_ent,
490 probe_ctx_getresult(ctx));
491 if (ofts != NULL) {
492 OVAL_FTSENT *ofts_ent;
493 while ((ofts_ent = oval_fts_read(ofts)) != NULL) {
494 if (ofts_ent->fts_info == FTS_F
495 || ofts_ent->fts_info == FTS_SL) {
496 process_yaml_file(prefix, ofts_ent->path, ofts_ent->file,
497 yamlpath_str, ctx);
498 }
499 oval_ftsent_free(ofts_ent);
500 }
501 oval_fts_close(ofts);
502 }
503 }
504
505 free(content_str);
506 SEXP_free(content_val);
507 SEXP_free(content_ent);
508 free(yamlpath_str);
509 SEXP_free(yamlpath_val);
510 SEXP_free(yamlpath_ent);
511 SEXP_free(filename_ent);
512 SEXP_free(path_ent);
513 SEXP_free(filepath_ent);
514 SEXP_free(behaviors_ent);
515 return 0;
516 }