Debian Codegraph @

src/OVAL/probes/probe/worker.c

raw symbols SCIP
1 /*
2 * Copyright 2011 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 * Daniel Kopecek <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "_seap.h"
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <pthread.h>
33 #include <errno.h>
34
35 #if defined(OS_FREEBSD)
36 #include <pthread_np.h>
37 #endif
38
39 #if defined(OS_OSX)
40 /* XXX: please see https://github.com/OpenSCAP/openscap/pull/1626 for the
41 * reason of explicitly defining an external here instead of simply
42 * including the unistd.h header file.
43 */
44 extern int chroot(const char *);
45 #endif
46
47 #include "probe-api.h"
48 #include "common/debug_priv.h"
49 #include "entcmp.h"
50
51 #include "worker.h"
52 #include "probe-table.h"
53 #include "probe.h"
54
55 extern bool OSCAP_GSYM(varref_handling);
56 extern void *OSCAP_GSYM(probe_arg);
57
58 // Dummy pthread routine
59 static void *dummy_routine(void *dummy_param)
60 {
61 return NULL;
62 }
63
64 static void preload_libraries_before_chroot()
65 {
66 // Force to load dynamic libraries used by pthread_cancel
67 pthread_t t;
68 if (pthread_create(&t, NULL, dummy_routine, NULL)) {
69 dE("pthread_create failed: %s", strerror(errno));
70 }
71 pthread_cancel(t);
72 pthread_join(t, NULL);
73 }
74
75 static void pthread_pair_cleanup_handler(void *arg)
76 {
77 probe_pwpair_t *pair = (probe_pwpair_t *)arg;
78 dW("Probe worker thread finished unxpectedly, trying to avoid deadlock now...");
79 SEAP_replyerr(pair->probe->SEAP_ctx, pair->probe->sd, pair->pth->msg, -100);
80 }
81
82 void *probe_worker_runfn(void *arg)
83 {
84 dD("probe_worker_runfn has started");
85 probe_pwpair_t *pair = (probe_pwpair_t *)arg;
86
87 SEXP_t *probe_res, *obj, *oid;
88 int probe_ret;
89
90 #if defined(HAVE_PTHREAD_SETNAME_NP)
91 # if defined(OS_APPLE)
92 pthread_setname_np("probe_worker");
93 # else
94 pthread_setname_np(pthread_self(), "probe_worker");
95 # endif
96 #endif
97 dD("handling SEAP message ID %u", pair->pth->sid);
98 pthread_cleanup_push(pthread_pair_cleanup_handler, (void *)pair);
99
100 //
101 probe_ret = -1;
102 probe_res = pair->pth->msg_handler(pair->probe, pair->pth->msg, &probe_ret);
103 //
104 dD("handler result = %p, return code = %d", probe_res, probe_ret);
105
106 if (probe_res == NULL) {
107 dE("An error was returned by the input message handler: %s.", strerror(errno));
108 exit(1);
109 }
110
111 /* Assuming that the red-black tree API is doing locking for us... */
112 if (rbt_i32_del(pair->probe->workers, pair->pth->sid, NULL) != 0) {
113 dW("thread not found in the probe thread tree, probably canceled by an external signal");
114 /*
115 * XXX: this is a possible deadlock; we can't send anything from
116 * here because the signal handler replied to the message
117 */
118 arg = NULL;
119
120 SEAP_msg_free(pair->pth->msg);
121 SEXP_free(probe_res);
122 free(pair);
123
124 dD("probe_worker_runfn has finished");
125 return (NULL);
126 } else {
127 SEXP_t *items;
128
129 dD("probe thread deleted");
130
131 obj = SEAP_msg_get(pair->pth->msg);
132 oid = probe_obj_getattrval(obj, "id");
133 items = probe_cobj_get_items(probe_res);
134
135 if (items != NULL) {
136 SEXP_list_sort(items, SEXP_refcmp);
137 SEXP_free(items);
138 }
139
140 if (probe_rcache_sexp_add(pair->probe->rcache, oid, probe_res) != 0) {
141 /* TODO */
142 abort();
143 }
144 SEXP_free(obj);
145 SEXP_free(oid);
146 }
147
148 if (probe_ret != 0) {
149 /*
150 * Something bad happened. A hint of the cause is stored as a error code in
151 * probe_ret (should be). We'll send it to the library using a SEAP error packet.
152 */
153 if (SEAP_replyerr(pair->probe->SEAP_ctx, pair->probe->sd, pair->pth->msg, probe_ret) == -1) {
154 int ret = errno;
155
156 dE("An error ocured while sending error status. errno=%u, %s.", errno, strerror(errno));
157 SEXP_free(probe_res);
158
159 /* FIXME */
160 exit(ret);
161 }
162 SEXP_free(probe_res);
163 } else {
164 SEAP_msg_t *seap_reply;
165 /*
166 * OK, the probe actually returned something, let's send it to the library.
167 */
168 seap_reply = SEAP_msg_new();
169 SEAP_msg_set(seap_reply, probe_res);
170
171 if (SEAP_reply(pair->probe->SEAP_ctx, pair->probe->sd, seap_reply, pair->pth->msg) == -1) {
172 int ret = errno;
173
174 SEAP_msg_free(seap_reply);
175 SEXP_free(probe_res);
176
177 exit(ret);
178 }
179
180 SEAP_msg_free(seap_reply);
181 SEXP_free(probe_res);
182 }
183
184 SEAP_msg_free(pair->pth->msg);
185 free(pair->pth);
186 free(pair);
187 pthread_detach(pthread_self());
188
189 pthread_cleanup_pop(0);
190
191 dD("probe_worker_runfn has finished");
192 return (NULL);
193 }
194
195 probe_worker_t *probe_worker_new(void)
196 {
197 probe_worker_t *pth = malloc(sizeof(probe_worker_t));
198
199 pth->sid = 0;
200 #ifndef OS_WINDOWS
201 pth->tid = 0;
202 #endif
203 pth->msg_handler = NULL;
204 pth->msg = NULL;
205
206 return (pth);
207 }
208
209 struct probe_varref_ctx {
210 SEXP_t *pi2;
211 unsigned int ent_cnt;
212 struct probe_varref_ctx_ent *ent_lst;
213 };
214
215 struct probe_varref_ctx_ent {
216 SEXP_t *ent_name_sref;
217 unsigned int val_cnt;
218 unsigned int next_val_idx;
219 };
220
221 static void probe_varref_destroy_ctx(struct probe_varref_ctx *ctx);
222
223 static int probe_varref_create_ctx(const SEXP_t *probe_in, SEXP_t *varrefs, struct probe_varref_ctx **octx)
224 {
225 unsigned int i, ent_cnt, val_cnt;
226 SEXP_t *ent_name, *ent, *varref, *val_lst;
227 SEXP_t *r0, *r1, *r2, *r3;
228 SEXP_t *vid, *vidx_name, *vidx_val;
229
230 /* varref_cnt = SEXP_number_getu_32(r0 = SEXP_list_nth(varrefs, 2)); */
231 ent_cnt = SEXP_number_getu_32(r1 = SEXP_list_nth(varrefs, 3));
232 SEXP_free(r1);
233
234 if (ent_cnt == UINT32_MAX)
235 return -1;
236
237 struct probe_varref_ctx *ctx = malloc(sizeof(struct probe_varref_ctx));
238 ctx->pi2 = SEXP_softref((SEXP_t *)probe_in);
239 ctx->ent_cnt = ent_cnt;
240 ctx->ent_lst = malloc(ent_cnt * sizeof (ctx->ent_lst[0]));
241
242 vidx_name = SEXP_string_new(":val_idx", 8);
243 vidx_val = SEXP_number_newu(0);
244
245 /* entities that use var_refs are stored at the begining of an object */
246 for (i = 0; i < ent_cnt; ++i) {
247 /*
248 * add variable values to entities and insert
249 * them into the new probe_in object
250 */
251 r0 = SEXP_list_nth(ctx->pi2, i + 2);
252 vid = probe_ent_getattrval(r0, "var_ref");
253 r1 = SEXP_list_first(r0);
254 r2 = SEXP_list_first(r1);
255
256 r3 = SEXP_list_new(r2, vidx_name, vidx_val, NULL);
257 SEXP_free(r0);
258 r0 = SEXP_list_rest(r1);
259 ent_name = SEXP_list_join(r3, r0);
260 SEXP_free(r0);
261 SEXP_free(r1);
262 SEXP_free(r2);
263 SEXP_free(r3);
264
265 SEXP_sublist_foreach(varref, varrefs, 4, SEXP_LIST_END) {
266 r0 = SEXP_list_first(varref);
267 if (!SEXP_string_cmp(vid, r0)) {
268 SEXP_free(r0);
269 break;
270 }
271 SEXP_free(r0);
272 }
273
274 if (varref == NULL) {
275 char *var_id = SEXP_string_cstr(vid);
276 dE("Unexpected error: variable id \"%s\" not found in varrefs.", var_id);
277 free(var_id);
278 SEXP_free(vid);
279 SEXP_free(ent_name);
280 SEXP_free(vidx_name);
281 SEXP_free(vidx_val);
282 probe_varref_destroy_ctx(ctx);
283 return -1;
284 }
285
286 SEXP_free(vid);
287
288 r0 = SEXP_list_nth(varref, 2);
289 val_cnt = SEXP_number_getu_32(r0);
290 val_lst = SEXP_list_nth(varref, 3);
291 SEXP_free(varref);
292 SEXP_free(r0);
293
294 ent = SEXP_list_new(ent_name, val_lst, NULL);
295 SEXP_free(ent_name);
296 SEXP_free(val_lst);
297
298 r0 = SEXP_list_replace(ctx->pi2, i + 2, ent);
299 SEXP_free(r0);
300 SEXP_free(ent);
301
302 r0 = SEXP_listref_nth(ctx->pi2, i + 2);
303 ctx->ent_lst[i].ent_name_sref = SEXP_listref_first(r0);
304 SEXP_free(r0);
305 ctx->ent_lst[i].val_cnt = val_cnt;
306 ctx->ent_lst[i].next_val_idx = 0;
307 }
308
309 SEXP_free(vidx_name);
310 SEXP_free(vidx_val);
311
312 *octx = ctx;
313
314 return 0;
315 }
316
317 static void probe_varref_destroy_ctx(struct probe_varref_ctx *ctx)
318 {
319 struct probe_varref_ctx_ent *ent, *ent_end;
320
321 SEXP_free(ctx->pi2);
322
323 ent = ctx->ent_lst;
324 ent_end = ent + ctx->ent_cnt;
325
326 while (ent != ent_end) {
327 SEXP_free(ent->ent_name_sref);
328 ++ent;
329 }
330
331 free(ctx->ent_lst);
332 free(ctx);
333 }
334
335 static int probe_varref_iterate_ctx(struct probe_varref_ctx *ctx)
336 {
337 unsigned int val_cnt, *next_val_idx;
338 SEXP_t *ent_name_sref;
339 SEXP_t *r0, *r1, *r2;
340 struct probe_varref_ctx_ent *ent, *ent_end;
341
342 ent = ctx->ent_lst;
343 ent_end = ent + ctx->ent_cnt;
344 val_cnt = ent->val_cnt;
345 next_val_idx = &ent->next_val_idx;
346 ent_name_sref = ent->ent_name_sref;
347
348 r0 = SEXP_number_newu(0);
349
350 while (++(*next_val_idx) >= val_cnt) {
351 if (++ent == ent_end) {
352 SEXP_free(r0);
353 return 0;
354 }
355
356 *next_val_idx = 0;
357 r1 = SEXP_list_replace(ent_name_sref, 3, r0);
358 SEXP_free(r1);
359
360 val_cnt = ent->val_cnt;
361 next_val_idx = &ent->next_val_idx;
362 ent_name_sref = ent->ent_name_sref;
363 }
364 r1 = SEXP_list_replace(ent_name_sref, 3, r2 = SEXP_number_newu(*next_val_idx));
365
366 SEXP_free(r0);
367 SEXP_free(r1);
368 SEXP_free(r2);
369
370 return 1;
371 }
372
373 #define MAX_EVAL_DEPTH 8 /**< maximum recursion depth for set evaluation */
374
375 /**
376 * Fetch states from the library. This function performs the state fetch operation
377 * that is used by the set evaluation function to fetch states that aren't available
378 * in the probe cache. The operation is implemented using a remote synchronous SEAP
379 * command. The fetched states are added to the probe cache by this function.
380 *
381 * @param id_list list of requested state ids
382 * @retval list list of fetched states, the possition in the list corresponds to the
383 * possition in the id_list
384 * @retval NULL on failure or when some of the ids do not refer to existing states in
385 * the associated OVAL document
386 */
387 static SEXP_t *probe_ste_fetch(probe_t *probe, SEXP_t *id_list)
388 {
389 SEXP_t *res, *ste, *id;
390 uint32_t i_len, r_len;
391
392 i_len = SEXP_list_length(id_list);
393
394 if (i_len == 0)
395 return SEXP_list_new(NULL);
396
397 res = SEAP_cmd_exec(probe->SEAP_ctx, probe->sd, 0, PROBECMD_STE_FETCH, id_list, SEAP_CMDTYPE_SYNC, NULL, NULL);
398
399 r_len = SEXP_list_length(res);
400
401 if (i_len != r_len) {
402 SEXP_free(res);
403 return (NULL);
404 }
405
406 for (; i_len > 0; --i_len) {
407 ste = SEXP_list_nth(res, i_len);
408 id = SEXP_list_nth(id_list, i_len);
409
410 _A(id != NULL);
411 _A(ste != NULL);
412
413 if (probe_rcache_sexp_add(probe->rcache, id, ste) != 0) {
414
415 SEXP_free(res);
416 SEXP_free(ste);
417 SEXP_free(id);
418
419 return (NULL);
420 }
421
422 SEXP_free(ste);
423 SEXP_free(id);
424 }
425
426 return (res);
427 }
428
429 /**
430 * Evaluate an OVAL object identified by its id. Using a remote
431 * synchronous SEAP command, this function executes evaluation of an
432 * OVAL object which results weren't found in the probe cache. This
433 * indirectly spawns a new thread in the probe process which evaluates
434 * the object and stores the result in the probe cache. That result is
435 * not send to the library because it doesn't know how to handle
436 * it. Instead, the result is fetched by this function from the cache
437 * and returned to the caller.
438 * @param id the id of the OVAL object to be evaluated
439 * @return the result of the evaluation of the object or NULL on failure
440 */
441 static SEXP_t *probe_obj_eval(probe_t *probe, SEXP_t *id)
442 {
443 SEXP_t *res, *rid;
444
445 res = SEAP_cmd_exec(probe->SEAP_ctx, probe->sd, 0, PROBECMD_OBJ_EVAL, id, SEAP_CMDTYPE_SYNC, NULL, NULL);
446
447 rid = SEXP_list_first(res);
448 if (SEXP_string_cmp(id, rid) != 0) {
449 SEXP_free(res);
450 SEXP_free(rid);
451 return NULL;
452 }
453 SEXP_free(res);
454 SEXP_free(rid);
455
456 return probe_rcache_sexp_get(probe->rcache, id);
457 }
458
459 static SEXP_t *probe_prepare_filters(probe_t *probe, SEXP_t *obj)
460 {
461 SEXP_t *filters;
462 int i;
463
464 filters = SEXP_list_new(NULL);
465
466 for (i = 1; ; ++i) {
467 SEXP_t *of, *f, *ste, *ste_id, *act;
468
469 of = probe_obj_getent(obj, "filter", i);
470
471 if (of == NULL)
472 break;
473
474 act = probe_ent_getattrval(of, "action");
475 ste_id = probe_ent_getval(of);
476 ste = probe_rcache_sexp_get(probe->rcache, ste_id);
477
478 if (ste == NULL) {
479 SEXP_t *r0, *r1;
480
481 r0 = SEXP_list_new(ste_id, NULL);
482 r1 = probe_ste_fetch(probe, r0);
483 ste = SEXP_list_first(r1);
484
485 SEXP_free(r0);
486 SEXP_free(r1);
487 }
488 SEXP_free(of);
489 SEXP_free(ste_id);
490
491 f = SEXP_list_new(act, ste, NULL);
492 SEXP_list_add(filters, f);
493
494 SEXP_free(act);
495 SEXP_free(ste);
496 SEXP_free(f);
497 }
498
499 return filters;
500 }
501
502 /**
503 * Combine two collections of items using an operation.
504 * @param cobj1 item collection
505 * @param cobj2 item collection
506 * @param op operation
507 * @return the result of the operation
508 */
509 static SEXP_t *probe_set_combine(SEXP_t *cobj0, SEXP_t *cobj1, oval_setobject_operation_t op)
510 {
511 SEXP_t *set0, *set1, *res_cobj, *cobj0_mask, *cobj1_mask, *res_mask;
512 register int cmp;
513 register SEXP_t *item0, *item1, *res;
514 register SEXP_list_it *sit0, *sit1;
515 oval_syschar_collection_flag_t res_flag;
516
517 if (cobj0 == NULL)
518 return SEXP_ref(cobj1);
519 if (cobj1 == NULL)
520 return SEXP_ref(cobj0);
521
522 set0 = probe_cobj_get_items(cobj0);
523 set1 = probe_cobj_get_items(cobj1);
524 cobj0_mask = probe_cobj_get_mask(cobj0);
525 cobj1_mask = probe_cobj_get_mask(cobj1);
526
527 /* prepare storage for results */
528 res = SEXP_list_new(NULL);
529 res_flag = probe_cobj_combine_flags(probe_cobj_get_flag(cobj0),
530 probe_cobj_get_flag(cobj1), op);
531 res_mask = SEXP_list_join(cobj0_mask, cobj1_mask);
532
533 /* prepare iterators & first items */
534 sit0 = SEXP_list_it_new(set0);
535 sit1 = SEXP_list_it_new(set1);
536 item0 = SEXP_list_it_next(sit0);
537 item1 = SEXP_list_it_next(sit1);
538
539 /* perform the set operation */
540 switch(op) {
541 case OVAL_SET_OPERATION_UNION:
542 while (item0 != NULL && item1 != NULL) {
543 cmp = SEXP_refcmp(item0, item1);
544
545 if (cmp < 0) {
546 SEXP_list_add(res, item0);
547 item0 = SEXP_list_it_next(sit0);
548 } else if (cmp > 0) {
549 SEXP_list_add(res, item1);
550 item1 = SEXP_list_it_next(sit1);
551 } else {
552 SEXP_list_add(res, item0);
553 item0 = SEXP_list_it_next(sit0);
554 item1 = SEXP_list_it_next(sit1);
555 }
556 }
557
558 if (item0 != NULL) {
559 do {
560 SEXP_list_add(res, item0);
561 } while((item0 = SEXP_list_it_next(sit0)) != NULL);
562 } else if (item1 != NULL) {
563 do {
564 SEXP_list_add(res, item1);
565 } while((item1 = SEXP_list_it_next(sit1)) != NULL);
566 }
567
568 break;
569 case OVAL_SET_OPERATION_INTERSECTION:
570 while (item0 != NULL && item1 != NULL) {
571 cmp = SEXP_refcmp(item0, item1);
572
573 if (cmp < 0)
574 item0 = SEXP_list_it_next(sit0);
575 else if (cmp > 0)
576 item1 = SEXP_list_it_next(sit1);
577 else {
578 SEXP_list_add(res, item0);
579 item0 = SEXP_list_it_next(sit0);
580 item1 = SEXP_list_it_next(sit1);
581 }
582 }
583
584 break;
585 case OVAL_SET_OPERATION_COMPLEMENT:
586 while (item0 != NULL && item1 != NULL) {
587 cmp = SEXP_refcmp(item0, item1);
588
589 if (cmp < 0) {
590 SEXP_list_add(res, item0);
591 item0 = SEXP_list_it_next(sit0);
592 } else if (cmp > 0) {
593 item1 = SEXP_list_it_next(sit1);
594 } else {
595 item0 = SEXP_list_it_next(sit0);
596 item1 = SEXP_list_it_next(sit1);
597 }
598 }
599
600 if (item0 != NULL) {
601 do {
602 SEXP_list_add(res, item0);
603 } while((item0 = SEXP_list_it_next(sit0)) != NULL);
604 }
605
606 break;
607 default:
608 dE("Unknown set operation: %d", op);
609 abort();
610 }
611
612 SEXP_list_it_free(sit0);
613 SEXP_list_it_free(sit1);
614
615 /*
616 * If the collected information is complete but all the items are
617 * removed, the flag is set to SYSCHAR_FLAG_DOES_NOT_EXIST
618 */
619 if (res_flag == SYSCHAR_FLAG_COMPLETE && SEXP_list_length(res) == 0)
620 res_flag = SYSCHAR_FLAG_DOES_NOT_EXIST;
621
622 res_cobj = probe_cobj_new(res_flag, NULL, res, res_mask);
623
624 SEXP_free(set0);
625 SEXP_free(set1);
626 SEXP_free(res);
627 SEXP_free(res_mask);
628 SEXP_free(cobj0_mask);
629 SEXP_free(cobj1_mask);
630
631 // todo: variables
632
633 return (res_cobj);
634 }
635
636 /**
637 * Apply a set of filters to a collected object.
638 * @param cobj item collection
639 * @param filter set (list) of filters
640 * @return collection of items without items that match any of the filters in the input set
641 */
642 static SEXP_t *probe_set_apply_filters(SEXP_t *cobj, SEXP_t *filters)
643 {
644 SEXP_t *result_items, *items, *item, *mask;
645 oval_syschar_status_t item_status;
646 oval_syschar_collection_flag_t flag;
647
648 result_items = SEXP_list_new(NULL);
649 flag = probe_cobj_get_flag(cobj);
650 items = probe_cobj_get_items(cobj);
651 mask = probe_cobj_get_mask(cobj);
652
653 SEXP_list_foreach(item, items) {
654 item_status = probe_ent_getstatus(item);
655
656 switch (item_status) {
657 case SYSCHAR_STATUS_DOES_NOT_EXIST:
658 continue;
659 case SYSCHAR_STATUS_ERROR:
660 break;
661 case SYSCHAR_STATUS_NOT_COLLECTED:
662 {
663 SEXP_t *r0, *r1;
664
665 r0 = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
666 "Supplied item has an invalid status: %d.", item_status);
667 r1 = SEXP_list_new(r0, NULL);
668 cobj = probe_cobj_new(SYSCHAR_FLAG_ERROR, r1, NULL, NULL);
669 SEXP_free(items);
670 SEXP_free(item);
671 SEXP_free(result_items);
672 SEXP_free(r0);
673 SEXP_free(r1);
674 SEXP_free(mask);
675 return cobj;
676 }
677 default:
678 break;
679 }
680
681 if (!probe_item_filtered(item, filters)) {
682 SEXP_list_add(result_items, item);
683 }
684 }
685
686 /*
687 * If the collected information is complete but all the items are
688 * filtered out, the flag is set to SYSCHAR_FLAG_DOES_NOT_EXIST
689 */
690 if (flag == SYSCHAR_FLAG_COMPLETE
691 && SEXP_list_length(result_items) == 0)
692 flag = SYSCHAR_FLAG_DOES_NOT_EXIST;
693
694 cobj = probe_cobj_new(flag, NULL, result_items, mask);
695 SEXP_free(items);
696 SEXP_free(result_items);
697 SEXP_free(mask);
698
699 return cobj;
700 }
701
702 /**
703 * Evaluate a set. This function takes care of evaluating a set of either two other sets
704 * or an object and 0..n filters. Objects are evaluated using the probe_obj_eval function
705 * and filters are fetched using the probe_ste_fetch function.
706 * @param set the set to be evaluated
707 * @param depth maximum recursion depth
708 * @return the result of the evaluation or NULL on failure
709 */
710 static SEXP_t *probe_set_eval(probe_t *probe, SEXP_t *set, size_t depth)
711 {
712 SEXP_t *filters_u, *filters_a, *filters_req;
713
714 SEXP_t *s_subset[2];
715 size_t s_subset_i;
716 SEXP_t *o_subset[2];
717 size_t o_subset_i;
718
719 SEXP_t *member;
720 char member_name[24];
721
722 SEXP_t *op_val;
723 int op_num;
724
725 SEXP_t *r0, *r1, *result, *Omsg = NULL;
726
727 if (depth > MAX_EVAL_DEPTH) {
728 char *fmt = "probe_set_eval: Too many levels: max=%zu.";
729 #ifndef NDEBUG
730 dD(fmt, (size_t) MAX_EVAL_DEPTH);
731 abort();
732 #endif
733 r0 = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, fmt, (size_t) MAX_EVAL_DEPTH);
734 r1 = SEXP_list_new(r0, NULL);
735 result = probe_cobj_new(SYSCHAR_FLAG_ERROR, r1, NULL, NULL);
736 SEXP_free(r0);
737 SEXP_free(r1);
738 return result;
739 }
740
741 filters_u = SEXP_list_new(NULL); /* unavailable filters */
742 filters_a = SEXP_list_new(NULL); /* available filters (cached) */
743 filters_req = SEXP_list_new(NULL); /* request list for probe_ste_fetch() */
744
745 s_subset[0] = NULL;
746 s_subset[1] = NULL;
747 s_subset_i = 0;
748
749 o_subset[0] = NULL;
750 o_subset[1] = NULL;
751 o_subset_i = 0;
752
753 result = NULL;
754
755 op_val = probe_ent_getattrval(set, "operation");
756
757 if (op_val != NULL)
758 op_num = SEXP_number_geti_32(op_val);
759 else
760 op_num = OVAL_SET_OPERATION_UNION;
761
762 SEXP_free(op_val);
763
764 _A(op_num == OVAL_SET_OPERATION_UNION ||
765 op_num == OVAL_SET_OPERATION_COMPLEMENT || op_num == OVAL_SET_OPERATION_INTERSECTION);
766
767 #define probe_set_foreach(elm_var, set_list) SEXP_sublist_foreach (elm_var, set_list, 2, 1000)
768 #define CASE(__c1, __rest) case (__c1): if (strcmp (__rest, member_name + 1) == 0)
769
770 probe_set_foreach(member, set) {
771 if (probe_ent_getname_r(member, member_name, sizeof member_name) == 0) {
772 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
773 "probe_set_eval: Invalid set element: ptr=%p, type=%s.", member, SEXP_strtype(member));
774 goto eval_fail;
775 }
776
777 if (strcmp("set", member_name) == 0) {
778 /*
779 * Handle a (sub)set entity
780 */
781 if (s_subset_i < 2) {
782 s_subset[s_subset_i] = probe_set_eval(probe, member, depth + 1);
783
784 if (s_subset[s_subset_i] == NULL) {
785 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
786 "probe_set_eval: Recursive set evaluation failed: m=%p, d=%zu.",
787 member, depth + 1);
788 goto eval_fail;
789 }
790
791 ++s_subset_i;
792 } else {
793 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
794 "probe_set_eval: More than 2 \"set\".");
795 goto eval_fail;
796 }
797 } else if (strcmp("obj_ref", member_name) == 0) {
798 /*
799 * Handle an object reference
800 */
801 SEXP_t *OID; /**< OVAL object ID */
802 SEXP_t *objres; /**< Result of the evaluation */
803 char OID_cstr[128];
804
805 dD("Handling object_reference");
806
807 if ((OID = probe_ent_getval(member)) == NULL) {
808 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
809 "%s: missing object_reference entity value!", __FUNCTION__);
810 goto eval_fail;
811 }
812 #ifndef NDEBUG
813 SEXP_string_cstr_r(OID, OID_cstr, sizeof OID_cstr);
814 dD("Looking for the result in cache: OID=%s", OID_cstr);
815 #endif
816 if ((objres = probe_rcache_sexp_get(probe->rcache, OID)) == NULL) {
817 dD("MISS => requesting object evaluation from the library");
818
819 objres = probe_obj_eval(probe, OID);
820
821 dD("EVAL: result=%p", objres);
822 if (objres != NULL) {
823 dO(OSCAP_DEBUGOBJ_SEXP, objres);
824 } else {
825 SEXP_string_cstr_r(OID, OID_cstr, sizeof OID_cstr);
826 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
827 "%s: evaluation failed: OID=%s", __FUNCTION__, OID_cstr);
828 SEXP_free(OID);
829 goto eval_fail;
830 }
831 }
832
833 SEXP_free(OID);
834
835 if (o_subset_i < 2) {
836 o_subset[o_subset_i] = objres;
837 ++o_subset_i;
838 } else {
839 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
840 "%s: more than 2 obj_refs.", __FUNCTION__);
841 SEXP_free(objres);
842 goto eval_fail;
843 }
844 } else if (strcmp("filter", member_name) == 0) {
845 /*
846 * Retrieve cached filters (states), remember unavailable ones so that
847 * we can fetch them all in one request.
848 */
849 SEXP_t *SID, *action, *state;
850
851 if ((SID = probe_ent_getval(member)) == NULL) {
852 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
853 "%s: missing filter entity value.", __FUNCTION__);
854 goto eval_fail;
855 }
856
857 if ((action = probe_ent_getattrval(member, "action")) == NULL) {
858 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
859 "%s: missing filter action.", __FUNCTION__);
860 SEXP_free(SID);
861 goto eval_fail;
862 }
863
864 if ((state = probe_rcache_sexp_get(probe->rcache, SID)) == NULL) {
865 /* MISS - remember the ID */
866 SEXP_list_add(filters_req, SID);
867 r0 = SEXP_list_new(action, SID, NULL);
868 SEXP_list_add(filters_u, r0);
869 } else {
870 /* HIT */
871 r0 = SEXP_list_new(action, state, NULL);
872 SEXP_list_add(filters_a, r0);
873 SEXP_free(state);
874 }
875
876 SEXP_free(SID);
877 SEXP_free(action);
878 SEXP_free(r0);
879 } else {
880 /*
881 * Unexpected entity
882 */
883 abort();
884 }
885 }
886
887 member = NULL;
888
889 /* request filters */
890 result = probe_ste_fetch(probe, filters_req);
891
892 if (result == NULL) {
893 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
894 "%s: Can't get unavailable filters.", __FUNCTION__);
895 goto eval_fail;
896 }
897 SEXP_free(filters_req);
898 SEXP_free(result);
899
900 SEXP_list_foreach(member, filters_u) {
901 SEXP_t *id, *act, *ste;
902
903 act = SEXP_list_first(member);
904 id = SEXP_list_nth(member, 2);
905 ste = probe_rcache_sexp_get(probe->rcache, id);
906 r0 = SEXP_list_new(act, ste, NULL);
907 SEXP_list_add(filters_a, r0);
908
909 SEXP_free(act);
910 SEXP_free(id);
911 SEXP_free(ste);
912 SEXP_free(r0);
913 }
914
915 SEXP_free(filters_u);
916
917 _A((s_subset_i > 0 && o_subset_i == 0) || (s_subset_i == 0 && o_subset_i > 0));
918
919 if (o_subset_i > 0) {
920 for (s_subset_i = 0; s_subset_i < o_subset_i; ++s_subset_i) {
921 s_subset[s_subset_i] = probe_set_apply_filters(o_subset[s_subset_i], filters_a);
922
923 #ifndef NDEBUG
924 if (s_subset[s_subset_i] == NULL) {
925 Omsg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
926 "%s: apply_filters returned NULL: set=%p, filters=%p.",
927 __FUNCTION__, o_subset[s_subset_i], filters_a);
928 goto eval_fail;
929 }
930 #endif
931 SEXP_free(o_subset[s_subset_i]);
932 o_subset[s_subset_i] = NULL;
933 }
934 }
935
936 #ifndef NDEBUG
937 {
938 unsigned int i;
939
940 for (i = 0; i < s_subset_i; ++i) {
941 if (s_subset[i] != NULL) {
942 dD("=== s_subset[%d] ===", i);
943 dO(OSCAP_DEBUGOBJ_SEXP, s_subset[i]);
944 }
945 }
946
947 for (i = 0; i < o_subset_i; ++i) {
948 if (o_subset[i] != NULL) {
949 dD("=== o_subset[%d] ===", i);
950 dO(OSCAP_DEBUGOBJ_SEXP, o_subset[i]);
951 }
952 }
953 }
954
955 dD("OP= %d", op_num);
956 #endif
957
958 SEXP_free(filters_a);
959 result = probe_set_combine(s_subset[0], s_subset[1], op_num);
960
961 _A(result != NULL);
962
963 SEXP_free(s_subset[0]);
964 SEXP_free(s_subset[1]);
965
966 dD("=== RESULT ===");
967 dO(OSCAP_DEBUGOBJ_SEXP, result);
968
969 return (result);
970 eval_fail:
971 SEXP_free(member);
972
973 for (; s_subset_i > 0; --s_subset_i)
974 SEXP_free(s_subset[s_subset_i - 1]);
975
976 for (; o_subset_i > 0; --o_subset_i)
977 SEXP_free(o_subset[o_subset_i - 1]);
978
979 SEXP_free(filters_u);
980 SEXP_free(filters_a);
981 SEXP_free(filters_req);
982 SEXP_free(result);
983
984 r1 = SEXP_list_new(Omsg, NULL);
985 result = probe_cobj_new(SYSCHAR_FLAG_ERROR, r1, NULL, NULL);
986 SEXP_free(Omsg);
987 SEXP_free(r1);
988 return result;
989 }
990
991 static void _add_blocked_paths(struct oscap_list *bpaths)
992 {
993 char *envar = oscap_strdup(getenv("OSCAP_PROBE_IGNORE_PATHS"));
994 if (envar == NULL) {
995 return;
996 }
997 #ifdef OS_WINDOWS
998 dW("OSCAP_PROBE_IGNORE_PATHS isn't effective on Windows.");
999 #else
1000 char **paths = oscap_split(envar, ":");
1001 for (int i = 0; paths[i]; ++i) {
1002 oscap_list_add(bpaths, strdup(paths[i]));
1003 }
1004 free(envar);
1005 free(paths);
1006 #endif
1007 }
1008
1009 /**
1010 * Worker thread function. This functions handles the evalution of objects and sets.
1011 * @param msg_in SEAP message with the request which contains the object to be evaluated
1012 * @param ret pointer to the return code storage
1013 */
1014 SEXP_t *probe_worker(probe_t *probe, SEAP_msg_t *msg_in, int *ret)
1015 {
1016 #ifndef OS_WINDOWS
1017 char *rootdir = NULL;
1018 probe_offline_mode_function_t offline_mode_function = probe_table_get_offline_mode_function(probe->subtype);
1019 if (offline_mode_function != NULL) {
1020 probe->supported_offline_mode = offline_mode_function();
1021 } else {
1022 probe->supported_offline_mode = PROBE_OFFLINE_NONE;
1023 }
1024
1025 /*
1026 * Setup offline mode(s)
1027 */
1028 rootdir = getenv("OSCAP_PROBE_ROOT");
1029 if ((rootdir != NULL) && (strlen(rootdir) > 0)) {
1030 preload_libraries_before_chroot(); // todo - maybe useless for own mode
1031
1032 if (probe->supported_offline_mode == PROBE_OFFLINE_NONE) {
1033 dW("Requested offline mode is not supported by %s probe.", oval_subtype_get_text(probe->subtype));
1034 *ret = 0;
1035 return probe_cobj_new(SYSCHAR_FLAG_NOT_APPLICABLE, NULL, NULL, NULL);
1036
1037 } else if (probe->supported_offline_mode & PROBE_OFFLINE_OWN) {
1038 dI("Switching probe to PROBE_OFFLINE_OWN mode.");
1039 probe->offline_mode = true;
1040 probe->selected_offline_mode = PROBE_OFFLINE_OWN;
1041
1042 } else if (probe->supported_offline_mode & PROBE_OFFLINE_CHROOT) {
1043 probe->real_root_fd = open("/", O_RDONLY);
1044 if (probe->real_root_fd == -1) {
1045 dE("open(\"/\") failed: %s", strerror(errno));
1046 return NULL;
1047 }
1048 probe->real_cwd_fd = open(".", O_RDONLY);
1049 if (probe->real_cwd_fd == -1) {
1050 close(probe->real_root_fd);
1051 dE("open(\".\") failed: %s", strerror(errno));
1052 return NULL;
1053 }
1054
1055 if (chroot(rootdir) != 0) {
1056 dE("chroot failed: %s", strerror(errno));
1057 }
1058 if (chdir("/") != 0) {
1059 dE("chdir failed: %s", strerror(errno));
1060 }
1061 /* NOTE: We're running in a different root directory.
1062 * Unless /proc, /sys are somehow emulated for the new
1063 * environment, they are not relevant and so are other
1064 * runtime only things (e.g. getenv, uname, ...).
1065 * Switch to offline mode. We may add a separate
1066 * mechanism to control this behaviour in the future.
1067 */
1068 dI("Switching probe to PROBE_OFFLINE_CHROOT mode.");
1069 probe->offline_mode = true;
1070 probe->selected_offline_mode = PROBE_OFFLINE_CHROOT;
1071 }
1072 }
1073 #endif
1074
1075 SEXP_t *probe_in, *probe_out, *set;
1076
1077 if (msg_in == NULL) {
1078 *ret = PROBE_EINVAL;
1079 return (NULL);
1080 }
1081
1082 probe_in = SEAP_msg_get(msg_in);
1083 probe_out = NULL;
1084
1085 if (probe_in == NULL) {
1086 *ret = PROBE_ENOOBJ;
1087 return (NULL);
1088 }
1089
1090 set = probe_obj_getent(probe_in, "set", 1);
1091
1092 if (set != NULL) {
1093 /* set object */
1094 probe_out = probe_set_eval(probe, set, 0);
1095 SEXP_free(set);
1096 // todo: in case of an internal error set probe_ret accordingly
1097 *ret = 0;
1098 } else {
1099 struct probe_ctx pctx;
1100 SEXP_t *varrefs, *mask;
1101
1102 pctx.offline_mode = probe->selected_offline_mode;
1103
1104 pctx.max_mem_ratio = OSCAP_PROBE_MEMORY_USAGE_RATIO_DEFAULT;
1105 char *max_ratio_str = getenv("OSCAP_PROBE_MEMORY_USAGE_RATIO");
1106 if (max_ratio_str != NULL) {
1107 double max_ratio = strtod(max_ratio_str, NULL);
1108 if (max_ratio > 0)
1109 pctx.max_mem_ratio = max_ratio;
1110 }
1111 pctx.collected_items = 0;
1112 pctx.max_collected_items = OSCAP_PROBE_COLLECT_UNLIMITED;
1113 char *max_collected_items_str = getenv("OSCAP_PROBE_MAX_COLLECTED_ITEMS");
1114 if (max_collected_items_str != NULL) {
1115 int max_collected_items = strtol(max_collected_items_str, NULL, 0);
1116 if (max_collected_items > 0) {
1117 pctx.max_collected_items = max_collected_items;
1118 }
1119 }
1120
1121 pctx.blocked_paths = oscap_list_new();
1122 _add_blocked_paths(pctx.blocked_paths);
1123
1124 /* simple object */
1125 pctx.icache = probe->icache;
1126 pctx.filters = probe_prepare_filters(probe, probe_in);
1127 mask = probe_obj_getmask(probe_in);
1128
1129 if (OSCAP_GSYM(varref_handling))
1130 varrefs = probe_obj_getent(probe_in, "varrefs", 1);
1131 else
1132 varrefs = NULL;
1133
1134 oval_subtype_t subtype = probe->subtype;
1135 probe_main_function_t probe_main_function = probe_table_get_main_function(subtype);
1136 const char *subtype_str = oval_subtype_get_text(subtype);
1137
1138 if (varrefs == NULL || !OSCAP_GSYM(varref_handling)) {
1139 /*
1140 * Prepare the collected object
1141 */
1142 probe_out = probe_cobj_new(SYSCHAR_FLAG_UNKNOWN, NULL, NULL, mask);
1143 SEXP_free(mask);
1144
1145 pctx.probe_in = probe_in;
1146 pctx.probe_out = probe_out;
1147
1148 /*
1149 * Run the main function of the probe implementation. Set thread
1150 * cancelation type to ASYNC to prevent the code in probe_main to
1151 * defer the cancelation for too long.
1152 */
1153 int __unused_oldstate;
1154 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &__unused_oldstate);
1155
1156
1157
1158 dI("I will run %s_probe_main:", subtype_str);
1159 *ret = probe_main_function(&pctx, probe->probe_arg);
1160
1161 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &__unused_oldstate);
1162
1163 /*
1164 * Synchronize
1165 */
1166 probe_icache_nop(probe->icache);
1167
1168 probe_cobj_compute_flag(probe_out);
1169 } else {
1170 /*
1171 * there are variable references in the object.
1172 * create ctx, iterate through all variable combinations
1173 */
1174 struct probe_varref_ctx *ctx;
1175
1176 dD("handling varrefs in object");
1177
1178 if (probe_varref_create_ctx(probe_in, varrefs, &ctx) != 0) {
1179 SEXP_free(varrefs);
1180 SEXP_free(pctx.filters);
1181 SEXP_free(probe_in);
1182 SEXP_free(mask);
1183 oscap_list_free(pctx.blocked_paths, free);
1184 *ret = PROBE_EUNKNOWN;
1185 return (NULL);
1186 }
1187
1188 SEXP_free(varrefs);
1189
1190 do {
1191 SEXP_t *cobj, *r0;
1192 /*
1193 * Prepare the collected object
1194 */
1195 cobj = probe_cobj_new(SYSCHAR_FLAG_UNKNOWN, NULL, NULL, mask);
1196
1197 pctx.probe_in = ctx->pi2;
1198 pctx.probe_out = cobj;
1199 /*
1200 * Run the main function of the probe implementation
1201 */
1202 dI("I will run %s_probe_main:", subtype_str);
1203 *ret = probe_main_function(&pctx, probe->probe_arg);
1204
1205 /*
1206 * Synchronize
1207 */
1208 probe_icache_nop(probe->icache);
1209
1210 probe_cobj_compute_flag(cobj);
1211 r0 = probe_out;
1212 probe_out = probe_set_combine(r0, cobj, OVAL_SET_OPERATION_UNION);
1213 SEXP_free(cobj);
1214 SEXP_free(r0);
1215 } while (*ret == 0
1216 && probe_varref_iterate_ctx(ctx));
1217
1218 SEXP_free(mask);
1219 probe_varref_destroy_ctx(ctx);
1220 }
1221
1222 SEXP_free(pctx.filters);
1223 oscap_list_free(pctx.blocked_paths, free);
1224 }
1225
1226 SEXP_free(probe_in);
1227
1228 #ifndef OS_WINDOWS
1229 /* Revert chroot */
1230 if (probe->real_root_fd != -1) {
1231 if (fchdir(probe->real_root_fd) != 0) {
1232 dE("fchdir failed: %s", strerror(errno));
1233 close(probe->real_root_fd);
1234 close(probe->real_cwd_fd);
1235 probe->real_root_fd = -1;
1236 probe->real_cwd_fd = -1;
1237 SEXP_free(probe_out);
1238 return NULL;
1239 }
1240 close(probe->real_root_fd);
1241 probe->real_root_fd = -1;
1242 dI("Leaving chroot mode");
1243 if (chroot(".") == -1) {
1244 dE("chroot(\".\") failed: %s", strerror(errno));
1245 close(probe->real_cwd_fd);
1246 probe->real_cwd_fd = -1;
1247 SEXP_free(probe_out);
1248 return NULL;
1249 }
1250 if (fchdir(probe->real_cwd_fd) != 0) {
1251 dE("fchdir failed: %s", strerror(errno));
1252 close(probe->real_cwd_fd);
1253 probe->real_cwd_fd = -1;
1254 SEXP_free(probe_out);
1255 return NULL;
1256 }
1257 close(probe->real_cwd_fd);
1258 probe->real_cwd_fd = -1;
1259 }
1260 #endif
1261
1262 SEXP_VALIDATE(probe_out);
1263
1264 return (probe_out);
1265 }