Skip to content

Commit 1e4b8ae

Browse files
committed
Add ability to block paths
People will be able to tell probes to skip certain paths during content evaluation. They can specify a list of paths by setting the `OSCAP_PROBE_IGNORE_PATHS` environment variable. The paths in this list should be separated by a colon.
1 parent b62069e commit 1e4b8ae

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

docs/manual/manual.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ not considered local by the scanner:
16191619
* `SOURCE_DATE_EPOCH` - Timestamp in seconds since epoch. This timestamp will be used instead of the current time to populate `timestamp` attributes in SCAP source data streams created by `oscap ds sds-compose` sub-module. This is used for reproducible builds of data streams.
16201620
* `OSCAP_PROBE_MEMORY_USAGE_RATIO` - maximum memory usage ratio (used/total) for OpenSCAP probes, default: 0.1
16211621
* `OSCAP_PROBE_MAX_COLLECTED_ITEMS` - maximal count of collected items by OpenSCAP probe for a single OVAL object evaluation
1622+
* `OSCAP_PROBE_IGNORE_PATHS` - Skip given paths during evaluation. If multiple paths should be skipped they need to be separated by a colon.
16221623

16231624
Also, OpenSCAP uses `libcurl` library which also can be configured using environment variables. See https://curl.se/libcurl/c/libcurl-env.html[the list of libcurl environment variables].
16241625

src/OVAL/probes/independent/textfilecontent54_probe.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#include "common/debug_priv.h"
5454
#include "common/util.h"
5555
#include "common/oscap_pcre.h"
56+
#include "common/list.h"
57+
5658
#include "textfilecontent54_probe.h"
5759

5860
#define FILE_SEPARATOR '/'
@@ -118,7 +120,7 @@ struct pfdata {
118120
oscap_pcre_t *compiled_regex;
119121
};
120122

121-
static int process_file(const char *prefix, const char *path, const char *file, struct pfdata *pfd, oval_schema_version_t over)
123+
static int process_file(const char *prefix, const char *path, const char *file, struct pfdata *pfd, oval_schema_version_t over, struct oscap_list *blocked_paths)
122124
{
123125
int ret = 0, path_len, file_len, cur_inst = 0, fd = -1, substr_cnt,
124126
buf_size = 0, buf_used = 0, ofs = 0, buf_inc = 4096;
@@ -143,6 +145,9 @@ static int process_file(const char *prefix, const char *path, const char *file,
143145

144146
memcpy(whole_path + path_len, file, file_len + 1);
145147

148+
if (probe_path_is_blocked(whole_path, blocked_paths)) {
149+
goto cleanup;
150+
}
146151
/*
147152
* If stat() fails, don't report an error and just skip the file.
148153
* This is an expected situation, because the fts_*() functions
@@ -360,7 +365,7 @@ int textfilecontent54_probe_main(probe_ctx *ctx, void *arg)
360365
if (ofts_ent->fts_info == FTS_F
361366
|| ofts_ent->fts_info == FTS_SL) {
362367
// todo: handle return code
363-
process_file(prefix, ofts_ent->path, ofts_ent->file, &pfd, over);
368+
process_file(prefix, ofts_ent->path, ofts_ent->file, &pfd, over, ctx->blocked_paths);
364369
}
365370
oval_ftsent_free(ofts_ent);
366371
}

src/OVAL/probes/probe-api.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,4 +1794,52 @@ SEXP_t *probe_obj_getmask(SEXP_t *obj)
17941794
SEXP_free(objents);
17951795
return (mask);
17961796
}
1797+
1798+
static bool path_startswith(const char *path, const char *prefix)
1799+
{
1800+
bool res = true;
1801+
const char *del = "/";
1802+
char *path_dup = oscap_strdup(path);
1803+
char **path_split = oscap_split(path_dup, del);
1804+
char *prefix_dup = oscap_strdup(prefix);
1805+
char **prefix_split = oscap_split(prefix_dup, del);
1806+
int i = 0, j = 0;
1807+
while (prefix_split[i] && path_split[j]) {
1808+
if (!strcmp(prefix_split[i], "")) {
1809+
++i;
1810+
continue;
1811+
}
1812+
if (!strcmp(path_split[j], "")) {
1813+
++j;
1814+
continue;
1815+
}
1816+
if (strcmp(prefix_split[i], path_split[j])) {
1817+
res = false;
1818+
break;
1819+
}
1820+
++i;
1821+
++j;
1822+
}
1823+
free(path_dup);
1824+
free(path_split);
1825+
free(prefix_dup);
1826+
free(prefix_split);
1827+
return res;
1828+
}
1829+
1830+
bool probe_path_is_blocked(const char *path, struct oscap_list *blocked_paths)
1831+
{
1832+
bool res = false;
1833+
struct oscap_iterator *it = oscap_iterator_new(blocked_paths);
1834+
while (oscap_iterator_has_more(it)) {
1835+
const char *item = oscap_iterator_next(it);
1836+
if (path_startswith(path, item)) {
1837+
res = true;
1838+
break;
1839+
}
1840+
}
1841+
oscap_iterator_free(it);
1842+
return res;
1843+
}
1844+
17971845
/// @}

src/OVAL/probes/probe/probe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct probe_ctx {
9494
int offline_mode;
9595
double max_mem_ratio;
9696
size_t max_collected_items;
97+
struct oscap_list *blocked_paths;
9798
};
9899

99100
typedef enum {

src/OVAL/probes/probe/worker.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,19 @@ static SEXP_t *probe_set_eval(probe_t *probe, SEXP_t *set, size_t depth)
972972
return result;
973973
}
974974

975+
static void _add_blocked_paths(struct oscap_list *bpaths)
976+
{
977+
char *envar = getenv("OSCAP_PROBE_IGNORE_PATHS");
978+
if (envar == NULL) {
979+
return;
980+
}
981+
char **paths = oscap_split(envar, ":");
982+
for (int i = 0; paths[i]; ++i) {
983+
oscap_list_add(bpaths, strdup(paths[i]));
984+
}
985+
free(paths);
986+
}
987+
975988
/**
976989
* Worker thread function. This functions handles the evalution of objects and sets.
977990
* @param msg_in SEAP message with the request which contains the object to be evaluated
@@ -1083,6 +1096,9 @@ SEXP_t *probe_worker(probe_t *probe, SEAP_msg_t *msg_in, int *ret)
10831096
}
10841097
}
10851098

1099+
pctx.blocked_paths = oscap_list_new();
1100+
_add_blocked_paths(pctx.blocked_paths);
1101+
10861102
/* simple object */
10871103
pctx.icache = probe->icache;
10881104
pctx.filters = probe_prepare_filters(probe, probe_in);
@@ -1142,6 +1158,7 @@ SEXP_t *probe_worker(probe_t *probe, SEAP_msg_t *msg_in, int *ret)
11421158
SEXP_free(pctx.filters);
11431159
SEXP_free(probe_in);
11441160
SEXP_free(mask);
1161+
oscap_list_free(pctx.blocked_paths, free);
11451162
*ret = PROBE_EUNKNOWN;
11461163
return (NULL);
11471164
}
@@ -1181,6 +1198,7 @@ SEXP_t *probe_worker(probe_t *probe, SEAP_msg_t *msg_in, int *ret)
11811198
}
11821199

11831200
SEXP_free(pctx.filters);
1201+
oscap_list_free(pctx.blocked_paths, free);
11841202
}
11851203

11861204
SEXP_free(probe_in);

src/OVAL/probes/public/probe-api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include <oval_types.h>
6969
#include "sexp-types.h"
7070
#include "oscap_export.h"
71+
#include "list.h"
7172

7273
/*
7374
* items
@@ -538,4 +539,11 @@ OSCAP_API oval_schema_version_t probe_obj_get_platform_schema_version(const SEXP
538539
*/
539540
OSCAP_API SEXP_t *probe_obj_getmask(SEXP_t *obj);
540541

542+
/**
543+
* Check if the given path matches any of the paths in the blocked paths list
544+
* @param path path to be examined
545+
* @param blocked_paths list of blocked paths
546+
*/
547+
OSCAP_API bool probe_path_is_blocked(const char *path, struct oscap_list *blocked_paths);
548+
541549
/// @}

src/OVAL/probes/unix/file_probe.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static SEXP_t *has_extended_acl(const char *path)
304304
#endif
305305
}
306306

307-
static int file_cb(const char *prefix, const char *p, const char *f, void *ptr, oval_schema_version_t over, struct ID_cache *cache, struct gr_sexps *grs, SEXP_t *gr_lastpath)
307+
static int file_cb(const char *prefix, const char *p, const char *f, void *ptr, oval_schema_version_t over, struct ID_cache *cache, struct gr_sexps *grs, SEXP_t *gr_lastpath, struct oscap_list *blocked_paths)
308308
{
309309
char path_buffer[PATH_MAX];
310310
SEXP_t *item;
@@ -325,6 +325,10 @@ static int file_cb(const char *prefix, const char *p, const char *f, void *ptr,
325325
st_path = path_buffer;
326326
}
327327

328+
if (probe_path_is_blocked(st_path, blocked_paths)) {
329+
return 0;
330+
}
331+
328332
char *st_path_with_prefix = oscap_path_join(prefix, st_path);
329333
if (lstat(st_path_with_prefix, &st) == -1) {
330334
dD("lstat failed when processing %s: errno=%u, %s.", st_path, errno, strerror (errno));
@@ -509,7 +513,7 @@ int file_probe_main(probe_ctx *ctx, void *mutex)
509513

510514
if ((ofts = oval_fts_open_prefixed(prefix, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) {
511515
while ((ofts_ent = oval_fts_read(ofts)) != NULL) {
512-
if (file_cb(prefix, ofts_ent->path, ofts_ent->file, &cbargs, over, cache, grs, &gr_lastpath) != 0) {
516+
if (file_cb(prefix, ofts_ent->path, ofts_ent->file, &cbargs, over, cache, grs, &gr_lastpath, ctx->blocked_paths) != 0) {
513517
oval_ftsent_free(ofts_ent);
514518
break;
515519
}

0 commit comments

Comments
 (0)