Skip to content

Commit badffb6

Browse files
authored
Merge pull request OpenSCAP#2210 from Xeicker/fix_file_probe
Fix textfilecontent54_probe behaviour
2 parents f14ef25 + 0a58242 commit badffb6

File tree

4 files changed

+263
-30
lines changed

4 files changed

+263
-30
lines changed

src/OVAL/probes/independent/textfilecontent54_probe.c

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ struct pfdata {
123123
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)
124124
{
125125
int ret = 0, path_len, file_len, cur_inst = 0, fd = -1, substr_cnt,
126-
buf_size = 0, buf_used = 0, ofs = 0, buf_inc = 4096;
126+
buf_size = 0, buf_used = 0, ofs = 0, buf_inc = 4096, instance_count = 0,
127+
want_instance = 1, negative_instance_value = 0;
127128
char **substrs = NULL;
128129
char *whole_path = NULL, *whole_path_with_prefix = NULL, *buf = NULL;
129-
SEXP_t *next_inst = NULL;
130+
SEXP_t *next_inst = NULL, *items = SEXP_list_new(NULL), *instance_value_list = NULL,
131+
*instance_value = NULL;
130132
struct stat st;
131133

134+
132135
if (file == NULL)
133136
goto cleanup;
134137

@@ -210,16 +213,6 @@ static int process_file(const char *prefix, const char *path, const char *file,
210213
buf[buf_used++] = '\0';
211214

212215
do {
213-
int want_instance;
214-
215-
next_inst = SEXP_number_newi_32(cur_inst + 1);
216-
217-
if (probe_entobj_cmp(pfd->instance_ent, next_inst) == OVAL_RESULT_TRUE)
218-
want_instance = 1;
219-
else
220-
want_instance = 0;
221-
222-
SEXP_free(next_inst);
223216
substr_cnt = oscap_pcre_get_substrings(buf, &ofs, pfd->compiled_regex, want_instance, &substrs);
224217

225218
if (substr_cnt < 0) {
@@ -235,27 +228,49 @@ static int process_file(const char *prefix, const char *path, const char *file,
235228
}
236229

237230
if (substr_cnt > 0) {
238-
++cur_inst;
239-
240-
if (want_instance) {
241-
int k;
242-
SEXP_t *item;
243-
244-
item = create_item(path, file, pfd->pattern,
245-
cur_inst, substrs, substr_cnt, over);
246-
247-
for (k = 0; k < substr_cnt; ++k)
248-
free(substrs[k]);
249-
free(substrs);
250-
int pic_ret = probe_item_collect(pfd->ctx, item);
251-
if (pic_ret == 2 || pic_ret == -1) {
252-
ret = -4;
253-
break;
254-
}
255-
}
231+
int k;
232+
instance_count++;
233+
234+
SEXP_list_add(items, create_item(path, file, pfd->pattern,
235+
instance_count, substrs, substr_cnt, over));
236+
237+
for (k = 0; k < substr_cnt; ++k)
238+
free(substrs[k]);
239+
free(substrs);
256240
}
257241
} while (substr_cnt > 0 && ofs < buf_used);
258242

243+
probe_ent_getvals(pfd->instance_ent, &instance_value_list);
244+
instance_value = SEXP_list_first(instance_value_list);
245+
negative_instance_value = SEXP_number_geti_64(instance_value) < 0;
246+
SEXP_free(instance_value_list);
247+
SEXP_free(instance_value);
248+
249+
for(cur_inst = 0; cur_inst < instance_count; cur_inst++){
250+
if (negative_instance_value)
251+
next_inst = SEXP_number_newi_32(cur_inst - instance_count);
252+
253+
else
254+
next_inst = SEXP_number_newi_32(cur_inst + 1);
255+
256+
if (probe_entobj_cmp(pfd->instance_ent, next_inst) == OVAL_RESULT_TRUE)
257+
want_instance = 1;
258+
else
259+
want_instance = 0;
260+
261+
SEXP_free(next_inst);
262+
263+
if (want_instance) {
264+
int pic_ret = probe_item_collect(pfd->ctx, SEXP_list_nth(items, cur_inst + 1));
265+
if (pic_ret == 2 || pic_ret == -1) {
266+
ret = -4;
267+
break;
268+
}
269+
}
270+
else
271+
SEXP_free(SEXP_list_nth(items, cur_inst + 1));
272+
}
273+
259274
cleanup:
260275
if (fd != -1)
261276
close(fd);

tests/probes/textfilecontent54/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ if(ENABLE_PROBES_INDEPENDENT)
66
add_oscap_test("test_recursion_limit.sh")
77
add_oscap_test("test_symlinks.sh")
88
add_oscap_test("test_validation_of_various_oval_versions.sh")
9+
add_oscap_test("test_negative_instance.sh")
910
endif()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
. $builddir/tests/test_common.sh
4+
5+
function test_negative_instance {
6+
7+
probecheck "textfilecontent54" || return 255
8+
9+
local ret_val=0;
10+
local DF="${srcdir}/test_negative_instance.xml"
11+
local RF="results.xml"
12+
13+
[ -f $RF ] && rm -f $RF
14+
15+
local FILE_A="/tmp/test_negative_instance.tmp_file"
16+
17+
touch "$FILE_A"
18+
19+
echo "valid_key = valid_value" > "$FILE_A"
20+
echo "valid_key = valid_value" >> "$FILE_A"
21+
echo "valid_key = valid_value" >> "$FILE_A"
22+
23+
$OSCAP oval eval --results $RF $DF
24+
25+
if [ -f $RF ]; then
26+
verify_results "tst" $DF $RF 13 && verify_results "def" $DF $RF 1
27+
ret_val=$?
28+
else
29+
ret_val=1
30+
fi
31+
32+
rm -f $FILE_A $FILE_B $FILE_C
33+
34+
return $ret_val
35+
}
36+
37+
test_negative_instance
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?xml version="1.0"?>
2+
<oval_definitions xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xmlns:unix-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix" xmlns:lin-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#linux" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#linux linux-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd">
3+
4+
<generator>
5+
<oval:schema_version>5.11.1</oval:schema_version>
6+
<oval:timestamp>2025-03-24T00:00:00-00:00</oval:timestamp>
7+
</generator>
8+
9+
<definitions>
10+
11+
<definition class="compliance" version="1" id="oval:0:def:1"> <!-- comment="true" -->
12+
<metadata>
13+
<title></title>
14+
<description></description>
15+
</metadata>
16+
<criteria operator="AND">
17+
<criterion test_ref="oval:0:tst:1"/>
18+
<criterion test_ref="oval:0:tst:2"/>
19+
<criterion test_ref="oval:0:tst:3"/>
20+
<criterion test_ref="oval:0:tst:4"/>
21+
<criterion test_ref="oval:0:tst:5"/>
22+
<criterion test_ref="oval:0:tst:6"/>
23+
<criterion test_ref="oval:0:tst:7"/>
24+
<criterion test_ref="oval:0:tst:8"/>
25+
<criterion test_ref="oval:0:tst:9"/>
26+
<criterion test_ref="oval:0:tst:10"/>
27+
<criterion test_ref="oval:0:tst:11"/>
28+
<criterion test_ref="oval:0:tst:12"/>
29+
<criterion test_ref="oval:0:tst:13"/>
30+
</criteria>
31+
</definition>
32+
33+
</definitions>
34+
35+
<tests>
36+
37+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:1" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
38+
<object object_ref="oval:0:obj:1"/>
39+
</textfilecontent54_test>
40+
41+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:2" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
42+
<object object_ref="oval:0:obj:2"/>
43+
</textfilecontent54_test>
44+
45+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:3" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
46+
<object object_ref="oval:0:obj:3"/>
47+
</textfilecontent54_test>
48+
49+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:4" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
50+
<object object_ref="oval:0:obj:4"/>
51+
</textfilecontent54_test>
52+
53+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:5" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
54+
<object object_ref="oval:0:obj:5"/>
55+
</textfilecontent54_test>
56+
57+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:6" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
58+
<object object_ref="oval:0:obj:6"/>
59+
</textfilecontent54_test>
60+
61+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:7" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
62+
<object object_ref="oval:0:obj:7"/>
63+
</textfilecontent54_test>
64+
65+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:8" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
66+
<object object_ref="oval:0:obj:8"/>
67+
</textfilecontent54_test>
68+
69+
<textfilecontent54_test check_existence="all_exist" version="1" id="oval:0:tst:9" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
70+
<object object_ref="oval:0:obj:9"/>
71+
</textfilecontent54_test>
72+
73+
<textfilecontent54_test check_existence="none_exist" version="1" id="oval:0:tst:10" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
74+
<object object_ref="oval:0:obj:10"/>
75+
</textfilecontent54_test>
76+
77+
<textfilecontent54_test check_existence="only_one_exists" version="1" id="oval:0:tst:11" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
78+
<object object_ref="oval:0:obj:11"/>
79+
</textfilecontent54_test>
80+
81+
<textfilecontent54_test check_existence="none_exist" version="1" id="oval:0:tst:12" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
82+
<object object_ref="oval:0:obj:12"/>
83+
</textfilecontent54_test>
84+
85+
<textfilecontent54_test check_existence="only_one_exists" version="1" id="oval:0:tst:13" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
86+
<object object_ref="oval:0:obj:4"/>
87+
</textfilecontent54_test>
88+
89+
</tests>
90+
91+
<objects>
92+
93+
<textfilecontent54_object version="1" id="oval:0:obj:1" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
94+
<path>/tmp</path>
95+
<filename>test_negative_instance.tmp_file</filename>
96+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
97+
<instance operation="greater than or equal" datatype="int">1</instance>
98+
</textfilecontent54_object>
99+
100+
<textfilecontent54_object version="1" id="oval:0:obj:2" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
101+
<path>/tmp</path>
102+
<filename>test_negative_instance.tmp_file</filename>
103+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
104+
<instance operation="greater than or equal" datatype="int">2</instance>
105+
</textfilecontent54_object>
106+
107+
<textfilecontent54_object version="1" id="oval:0:obj:3" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
108+
<path>/tmp</path>
109+
<filename>test_negative_instance.tmp_file</filename>
110+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
111+
<instance operation="greater than or equal" datatype="int">3</instance>
112+
</textfilecontent54_object>
113+
114+
<textfilecontent54_object version="1" id="oval:0:obj:4" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
115+
<path>/tmp</path>
116+
<filename>test_negative_instance.tmp_file</filename>
117+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
118+
<instance operation="greater than or equal" datatype="int">-1</instance>
119+
</textfilecontent54_object>
120+
121+
<textfilecontent54_object version="1" id="oval:0:obj:5" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
122+
<path>/tmp</path>
123+
<filename>test_negative_instance.tmp_file</filename>
124+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
125+
<instance operation="greater than or equal" datatype="int">-2</instance>
126+
</textfilecontent54_object>
127+
128+
<textfilecontent54_object version="1" id="oval:0:obj:6" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
129+
<path>/tmp</path>
130+
<filename>test_negative_instance.tmp_file</filename>
131+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
132+
<instance operation="greater than or equal" datatype="int">-3</instance>
133+
</textfilecontent54_object>
134+
135+
<textfilecontent54_object version="1" id="oval:0:obj:7" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
136+
<path>/tmp</path>
137+
<filename>test_negative_instance.tmp_file</filename>
138+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
139+
<instance operation="equals" datatype="int">-1</instance>
140+
</textfilecontent54_object>
141+
142+
<textfilecontent54_object version="1" id="oval:0:obj:8" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
143+
<path>/tmp</path>
144+
<filename>test_negative_instance.tmp_file</filename>
145+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
146+
<instance operation="equals" datatype="int">-2</instance>
147+
</textfilecontent54_object>
148+
149+
<textfilecontent54_object version="1" id="oval:0:obj:9" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
150+
<path>/tmp</path>
151+
<filename>test_negative_instance.tmp_file</filename>
152+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
153+
<instance operation="equals" datatype="int">-3</instance>
154+
</textfilecontent54_object>
155+
156+
<textfilecontent54_object version="1" id="oval:0:obj:10" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
157+
<path>/tmp</path>
158+
<filename>test_negative_instance.tmp_file</filename>
159+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
160+
<instance operation="greater than" datatype="int">-1</instance>
161+
</textfilecontent54_object>
162+
163+
<textfilecontent54_object version="1" id="oval:0:obj:11" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
164+
<path>/tmp</path>
165+
<filename>test_negative_instance.tmp_file</filename>
166+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
167+
<instance operation="less than" datatype="int">-2</instance>
168+
</textfilecontent54_object>
169+
170+
<textfilecontent54_object version="1" id="oval:0:obj:12" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
171+
<path>/tmp</path>
172+
<filename>test_negative_instance.tmp_file</filename>
173+
<pattern operation="pattern match">(\s)*valid_key(\s)*=(\s)*valid_value(\s)*</pattern>
174+
<instance operation="less than" datatype="int">-3</instance>
175+
</textfilecontent54_object>
176+
177+
178+
</objects>
179+
180+
</oval_definitions>

0 commit comments

Comments
 (0)