Debian Codegraph @

src/OVAL/probes/probe/icache.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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pthread.h>
27 #include <stddef.h>
28 #include <sexp.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <inttypes.h>
32 #include <stdlib.h>
33
34 #if defined(OS_FREEBSD)
35 #include <pthread_np.h>
36 #endif
37
38 #include "../SEAP/generic/rbt/rbt.h"
39 #include "probe-api.h"
40 #include "common/debug_priv.h"
41 #include "common/memusage.h"
42 #include "oscap_helpers.h"
43
44 #include "probe.h"
45 #include "icache.h"
46 #include "_sexp-ID.h"
47
48 static volatile uint32_t next_ID = 0;
49
50 #if !defined(HAVE_ATOMIC_FUNCTIONS)
51 pthread_mutex_t next_ID_mutex = PTHREAD_MUTEX_INITIALIZER;
52 #endif
53
54 static void probe_icache_item_setID(SEXP_t *item, SEXP_ID_t item_ID)
55 {
56 SEXP_t *name_ref, *prev_id;
57 SEXP_t uniq_id;
58 uint32_t local_id;
59
60 /* ((foo_item :id "<int>") ... ) */
61
62 if (item == NULL) {
63 return;
64 }
65 if (!SEXP_listp(item)) {
66 return;
67 }
68
69 #if defined(HAVE_ATOMIC_FUNCTIONS)
70 local_id = __sync_fetch_and_add(&next_ID, 1);
71 #else
72 if (pthread_mutex_lock(&next_ID_mutex) != 0) {
73 dE("Can't lock the next_ID_mutex: %u, %s", errno, strerror(errno));
74 abort();
75 }
76
77 local_id = ++next_ID;
78
79 if (pthread_mutex_unlock(&next_ID_mutex) != 0) {
80 dE("Can't unlock the next_ID_mutex: %u, %s", errno, strerror(errno));
81 abort();
82 }
83 #endif
84 SEXP_string_newf_r(&uniq_id, "1%05u%u", getpid(), local_id);
85
86 name_ref = SEXP_listref_first(item);
87 prev_id = SEXP_list_replace(name_ref, 3, &uniq_id);
88
89 SEXP_free(prev_id);
90 SEXP_free_r(&uniq_id);
91 SEXP_free(name_ref);
92
93 return;
94 }
95
96 static int icache_lookup(rbt_t *tree, int64_t item_id, probe_iqpair_t *pair) {
97
98 probe_citem_t *cached = NULL;
99
100 if (rbt_i64_get(tree, item_id, (void**)&cached) != 0) {
101 return -1;
102 }
103
104 /*
105 * Maybe a cache HIT
106 */
107 dD("cache HIT #1");
108
109 register uint16_t i;
110 for (i = 0; i < cached->count; ++i) {
111 SEXP_t rest1;
112 SEXP_t* rest_r1 = SEXP_list_rest_r(&rest1, pair->p.item);
113
114 SEXP_t rest2;
115 SEXP_t* rest_r2 = SEXP_list_rest_r(&rest2, cached->item[i]);
116
117 if (SEXP_deepcmp(rest_r1, rest_r2)) {
118 SEXP_free_r(&rest1);
119 SEXP_free_r(&rest2);
120 break;
121 }
122
123 SEXP_free_r(&rest1);
124 SEXP_free_r(&rest2);
125 }
126
127 if (i == cached->count) {
128 /*
129 * Cache MISS
130 */
131 dD("cache MISS");
132
133 void *new_item = realloc(cached->item, sizeof(SEXP_t *) * (cached->count + 1));
134 if (new_item == NULL) {
135 dE("Unable to re-allocate memory for cache");
136 return -1;
137 }
138 cached->count++;
139 cached->item = new_item;
140 cached->item[cached->count - 1] = pair->p.item;
141
142 /* Assign an unique item ID */
143 probe_icache_item_setID(pair->p.item, item_id);
144 } else {
145 /*
146 * Cache HIT
147 */
148 dD("cache HIT #2 -> real HIT");
149 SEXP_free(pair->p.item);
150 pair->p.item = cached->item[i];
151 }
152 return 0;
153 }
154
155 static void icache_add_to_tree(rbt_t *tree, int64_t item_id, probe_iqpair_t *pair) {
156
157 probe_citem_t *cached = malloc(sizeof(probe_citem_t));
158 cached->item = malloc(sizeof(SEXP_t *));
159 cached->item[0] = pair->p.item;
160 cached->count = 1;
161
162 /* Assign an unique item ID */
163 probe_icache_item_setID(pair->p.item, item_id);
164
165 if (rbt_i64_add(tree, (int64_t)item_id, (void **)cached, NULL) != 0) {
166 dE("Can't add item (k=%"PRIi64" to the cache (%p)", item_id, tree);
167
168 free(cached->item);
169 free(cached);
170
171 /* now what? */
172 abort();
173 }
174 }
175
176 static void *probe_icache_worker(void *arg)
177 {
178 probe_icache_t *cache = (probe_icache_t *)(arg);
179 probe_iqpair_t *pair, pair_mem;
180 SEXP_ID_t item_ID;
181
182 if (cache == NULL) {
183 return NULL;
184 }
185
186 #if defined(HAVE_PTHREAD_SETNAME_NP)
187 const char* thread_name = "icache_worker";
188 # if defined(OS_APPLE)
189 pthread_setname_np(thread_name);
190 # else
191 pthread_setname_np(pthread_self(), thread_name);
192 # endif
193 #endif
194
195 if (pthread_mutex_lock(&cache->queue_mutex) != 0) {
196 dE("An error ocured while locking the queue mutex: %u, %s",
197 errno, strerror(errno));
198 return (NULL);
199 }
200
201 pair = &pair_mem;
202 dD("icache worker ready");
203
204 switch (errno = pthread_barrier_wait(cache->th_barrier))
205 {
206 case 0:
207 case PTHREAD_BARRIER_SERIAL_THREAD:
208 break;
209 default:
210 dE("pthread_barrier_wait: %d, %s.",
211 errno, strerror(errno));
212 pthread_mutex_unlock(&cache->queue_mutex);
213 return (NULL);
214 }
215
216 while(pthread_cond_wait(&cache->queue_notempty, &cache->queue_mutex) == 0) {
217 if (cache->queue_cnt <= 0) {
218 pthread_mutex_unlock(&cache->queue_mutex);
219 return NULL;
220 }
221 do {
222 dD("Extracting item from the cache queue: cnt=%"PRIu16", beg=%"PRIu16"", cache->queue_cnt, cache->queue_beg);
223 /*
224 * Extract an item from the queue and update queue beg, end & cnt
225 */
226 pair_mem = cache->queue[cache->queue_beg];
227 #ifndef NDEBUG
228 memset(cache->queue + cache->queue_beg, 0, sizeof(probe_iqpair_t));
229 #endif
230 --cache->queue_cnt;
231 ++cache->queue_beg;
232
233 if (cache->queue_beg == cache->queue_max)
234 cache->queue_beg = 0;
235
236 if (cache->queue_cnt == 0 ?
237 cache->queue_end != cache->queue_beg :
238 cache->queue_end == cache->queue_beg) {
239 pthread_mutex_unlock(&cache->queue_mutex);
240 return NULL;
241 }
242
243
244 dD("Signaling `notfull'");
245
246 if (pthread_cond_signal(&cache->queue_notfull) != 0) {
247 dE("An error ocured while signaling the `notfull' condition: %u, %s",
248 errno, strerror(errno));
249 abort();
250 }
251 /*
252 * Release the mutex
253 */
254 if (pthread_mutex_unlock(&cache->queue_mutex) != 0) {
255 dE("An error ocured while unlocking the queue mutex: %u, %s",
256 errno, strerror(errno));
257 abort();
258 }
259
260 if (pair->cobj == NULL) {
261 /*
262 * Handle NOP case (synchronization)
263 */
264 if (pair->p.cond == NULL) {
265 return NULL;
266 }
267
268 dD("Handling NOP");
269
270 if (pthread_cond_signal(pair->p.cond) != 0) {
271 dE("An error ocured while signaling NOP condition: %u, %s",
272 errno, strerror(errno));
273 abort();
274 }
275 } else {
276
277 dD("Handling cache request");
278
279 /*
280 * Compute item ID
281 */
282 dD("pair address: %"PRIu64, (uint64_t) pair);
283 dD("item address: %"PRIu64, (uint64_t) pair->p.item);
284 item_ID = SEXP_ID_v(pair->p.item);
285 dD("item ID=%"PRIu64"", item_ID);
286
287 if (icache_lookup(cache->tree, item_ID, pair) != 0) {
288 /*
289 * Cache MISS
290 */
291 dD("cache MISS");
292 icache_add_to_tree(cache->tree, item_ID, pair);
293 }
294
295 if (probe_cobj_add_item(pair->cobj, pair->p.item) != 0) {
296 dW("An error ocured while adding the item to the collected object");
297 }
298 }
299
300 if (pthread_mutex_lock(&cache->queue_mutex) != 0) {
301 dE("An error ocured while re-locking the queue mutex: %u, %s",
302 errno, strerror(errno));
303 abort();
304 }
305
306 } while (cache->queue_cnt > 0);
307 }
308
309 return (NULL);
310 }
311
312 probe_icache_t *probe_icache_new(pthread_barrier_t *th_barrier)
313 {
314 probe_icache_t *cache = malloc(sizeof(probe_icache_t));
315 cache->tree = rbt_i64_new();
316
317 if (pthread_mutex_init(&cache->queue_mutex, NULL) != 0) {
318 dE("Can't initialize icache mutex: %u, %s", errno, strerror(errno));
319 goto fail;
320 }
321
322 cache->queue_beg = 0;
323 cache->queue_end = 0;
324 cache->queue_cnt = 0;
325 cache->queue_max = PROBE_IQUEUE_CAPACITY;
326 cache->th_barrier = th_barrier;
327
328 if (pthread_cond_init(&cache->queue_notempty, NULL) != 0) {
329 dE("Can't initialize icache queue condition variable (notempty): %u, %s",
330 errno, strerror(errno));
331 goto fail;
332 }
333
334 if (pthread_cond_init(&cache->queue_notfull, NULL) != 0) {
335 dE("Can't initialize icache queue condition variable (notfull): %u, %s",
336 errno, strerror(errno));
337 goto fail;
338 }
339
340 if (pthread_create(&cache->thid, NULL,
341 probe_icache_worker, (void *)cache) != 0)
342 {
343 dE("Can't start the icache worker: %u, %s", errno, strerror(errno));
344 goto fail;
345 }
346
347 return (cache);
348 fail:
349 if (cache->tree != NULL)
350 rbt_i64_free(cache->tree);
351
352 pthread_mutex_destroy(&cache->queue_mutex);
353 pthread_cond_destroy(&cache->queue_notempty);
354 free(cache);
355
356 return (NULL);
357 }
358
359 static int __probe_icache_add_nolock(probe_icache_t *cache, SEXP_t *cobj, SEXP_t *item, pthread_cond_t *cond)
360 {
361 if (!((cond == NULL) ^ (item == NULL))) {
362 return -1;
363 }
364 retry:
365 if (cache->queue_cnt < cache->queue_max) {
366 cache->queue[cache->queue_end].cobj = cobj;
367
368 if (item != NULL) {
369 if (cobj == NULL) {
370 return -1;
371 }
372 cache->queue[cache->queue_end].p.item = item;
373 } else {
374 if (item != NULL || cobj != NULL) {
375 return -1;
376 }
377 cache->queue[cache->queue_end].p.cond = cond;
378 }
379
380 ++cache->queue_cnt;
381 ++cache->queue_end;
382
383 if (cache->queue_end == cache->queue_max)
384 cache->queue_end = 0;
385 } else {
386 /*
387 * The queue is full, we have to wait
388 */
389 if (pthread_cond_wait(&cache->queue_notfull, &cache->queue_mutex) == 0)
390 goto retry;
391 else {
392 dE("An error ocured while waiting for the `notfull' queue condition: %u, %s",
393 errno, strerror(errno));
394 return (-1);
395 }
396 }
397
398 return (0);
399 }
400
401 int probe_icache_add(probe_icache_t *cache, SEXP_t *cobj, SEXP_t *item)
402 {
403 int ret;
404
405 if (cache == NULL || cobj == NULL || item == NULL)
406 return (-1); /* XXX: EFAULT */
407
408 if (pthread_mutex_lock(&cache->queue_mutex) != 0) {
409 dE("An error ocured while locking the queue mutex: %u, %s",
410 errno, strerror(errno));
411 return (-1);
412 }
413
414 ret = __probe_icache_add_nolock(cache, cobj, item, NULL);
415
416 if (pthread_cond_signal(&cache->queue_notempty) != 0) {
417 dE("An error ocured while signaling the `notempty' condition: %u, %s",
418 errno, strerror(errno));
419 pthread_mutex_unlock(&cache->queue_mutex);
420 return (-1);
421 }
422
423 if (pthread_mutex_unlock(&cache->queue_mutex) != 0) {
424 dE("An error ocured while unlocking the queue mutex: %u, %s",
425 errno, strerror(errno));
426 abort();
427 }
428
429 if (ret != 0)
430 return (-1);
431
432 return (0);
433 }
434
435 int probe_icache_nop(probe_icache_t *cache)
436 {
437 pthread_cond_t cond;
438
439 dD("NOP");
440
441 if (pthread_mutex_lock(&cache->queue_mutex) != 0) {
442 dE("An error ocured while locking the queue mutex: %u, %s",
443 errno, strerror(errno));
444 return (-1);
445 }
446
447 if (pthread_cond_init(&cond, NULL) != 0) {
448 dE("Can't initialize icache queue condition variable (NOP): %u, %s",
449 errno, strerror(errno));
450 pthread_mutex_unlock(&cache->queue_mutex);
451 return (-1);
452 }
453
454 if (__probe_icache_add_nolock(cache, NULL, NULL, &cond) != 0) {
455 if (pthread_mutex_unlock(&cache->queue_mutex) != 0) {
456 dE("An error ocured while unlocking the queue mutex: %u, %s",
457 errno, strerror(errno));
458 abort();
459 }
460
461 pthread_cond_destroy(&cond);
462 return (-1);
463 }
464
465 dD("Signaling `notempty'");
466
467 if (pthread_cond_signal(&cache->queue_notempty) != 0) {
468 dE("An error ocured while signaling the `notempty' condition: %u, %s",
469 errno, strerror(errno));
470
471 pthread_cond_destroy(&cond);
472 pthread_mutex_unlock(&cache->queue_mutex);
473 return (-1);
474 }
475
476 dD("Waiting for icache worker to handle the NOP");
477
478 if (pthread_cond_wait(&cond, &cache->queue_mutex) != 0) {
479 dE("An error ocured while waiting for the `NOP' queue condition: %u, %s",
480 errno, strerror(errno));
481 return (-1);
482 }
483
484 dD("Sync");
485
486 if (pthread_mutex_unlock(&cache->queue_mutex) != 0) {
487 dE("An error ocured while unlocking the queue mutex: %u, %s",
488 errno, strerror(errno));
489 abort();
490 }
491
492 pthread_cond_destroy(&cond);
493
494 return (0);
495 }
496
497 #define PROBE_RESULT_MEMCHECK_CTRESHOLD 1000 /* item count */
498
499 /**
500 * Returns 0 if the memory constraints are not reached. Otherwise, 1 is returned.
501 * In case of an error, -1 is returned.
502 */
503 static int probe_cobj_memcheck(size_t item_cnt, double max_ratio)
504 {
505 if (item_cnt > PROBE_RESULT_MEMCHECK_CTRESHOLD) {
506 struct proc_memusage mu_proc;
507 struct sys_memusage mu_sys;
508 double c_ratio;
509
510 memset(&mu_proc, 0, sizeof(mu_proc));
511 memset(&mu_sys, 0, sizeof(mu_sys));
512
513 if (oscap_proc_memusage (&mu_proc) != 0)
514 return (-1);
515
516 if (oscap_sys_memusage (&mu_sys) != 0)
517 return (-1);
518
519 c_ratio = (double)mu_proc.mu_rss/(double)(mu_sys.mu_total);
520
521 if (c_ratio > max_ratio) {
522 dW("Memory usage ratio limit reached! limit=%f, current=%f, used=%ld MB, free=%ld MB, total=%ld MB, count of items=%ld",
523 max_ratio, c_ratio, mu_proc.mu_rss / 1024, mu_sys.mu_realfree / 1024, mu_sys.mu_total / 1024, item_cnt);
524 errno = ENOMEM;
525 return (1);
526 }
527
528 }
529
530 return (0);
531 }
532
533 static int _mark_collected_object_as_incomplete(struct probe_ctx *ctx, const char *message)
534 {
535 /*
536 * Don't set the message again if the collected object is
537 * already flagged as incomplete.
538 */
539 if (probe_cobj_get_flag(ctx->probe_out) == SYSCHAR_FLAG_INCOMPLETE) {
540 return 0;
541 }
542 /*
543 * Sync with the icache thread before modifying the
544 * collected object.
545 */
546 if (probe_icache_nop(ctx->icache) != 0) {
547 return -1;
548 }
549
550 SEXP_t *sexp_msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_WARNING, (char *) message);
551 probe_cobj_add_msg(ctx->probe_out, sexp_msg);
552 probe_cobj_set_flag(ctx->probe_out, SYSCHAR_FLAG_INCOMPLETE);
553 SEXP_free(sexp_msg);
554 return 0;
555 }
556
557 /**
558 * Collect an item
559 * This function adds an item the collected object assosiated
560 * with the given probe context.
561 *
562 * Returns:
563 * 0 ... the item was succesfully added to the collected object
564 * 1 ... the item was filtered out
565 * 2 ... the item was not added because of memory constraints
566 * and the collected object was flagged as incomplete
567 *-1 ... unexpected/internal error
568 *
569 * The caller must not free the item, it's freed automatically
570 * by this function or by the icache worker thread.
571 */
572 int probe_item_collect(struct probe_ctx *ctx, SEXP_t *item)
573 {
574 if (ctx == NULL || ctx->probe_out == NULL || item == NULL) {
575 return -1;
576 }
577
578 if (ctx->max_collected_items != OSCAP_PROBE_COLLECT_UNLIMITED && ctx->collected_items >= ctx->max_collected_items) {
579 char *message = oscap_sprintf("Object is incomplete because the object matches more than %ld items.", ctx->max_collected_items);
580 if (_mark_collected_object_as_incomplete(ctx, message) != 0) {
581 free(message);
582 return -1;
583 }
584 free(message);
585 return 2;
586 }
587
588 int memcheck_ret = probe_cobj_memcheck(ctx->collected_items, ctx->max_mem_ratio);
589 if (memcheck_ret == -1) {
590 dE("Failed to check available memory");
591 SEXP_free(item);
592 return -1;
593 }
594 if (memcheck_ret == 1) {
595 SEXP_free(item);
596 const char *message = "Object is incomplete due to memory constraints.";
597 if (_mark_collected_object_as_incomplete(ctx, message) != 0) {
598 return -1;
599 }
600
601 return 2;
602 }
603
604 if (ctx->filters != NULL && probe_item_filtered(item, ctx->filters)) {
605 SEXP_free(item);
606 return (1);
607 }
608
609 if (probe_icache_add(ctx->icache, ctx->probe_out, item) != 0) {
610 dE("Can't add item (%p) to the item cache (%p)", item, ctx->icache);
611 SEXP_free(item);
612 return (-1);
613 }
614
615 ctx->collected_items++;
616 return (0);
617 }
618
619 static void probe_icache_free_node(struct rbt_i64_node *n)
620 {
621 probe_citem_t *ci = (probe_citem_t *)n->data;
622
623 for ( ; ci->count > 0 ; --ci->count ) {
624 SEXP_free(ci->item[ci->count - 1]);
625 }
626
627 free(ci->item);
628 free(ci);
629 return;
630 }
631
632 void probe_icache_free(probe_icache_t *cache)
633 {
634 void *ret = NULL;
635
636 pthread_cancel(cache->thid);
637 pthread_join(cache->thid, &ret);
638 pthread_mutex_destroy(&cache->queue_mutex);
639 pthread_cond_destroy(&cache->queue_notempty);
640 pthread_cond_destroy(&cache->queue_notfull);
641
642 rbt_i64_free_cb(cache->tree, &probe_icache_free_node);
643 free(cache);
644 return;
645 }