-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathxccdf_policy_remediate.c
More file actions
2071 lines (1882 loc) · 68.2 KB
/
xccdf_policy_remediate.c
File metadata and controls
2071 lines (1882 loc) · 68.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef OSCAP_UNIX
#include <sys/wait.h>
#endif
#ifdef OS_WINDOWS
#include <io.h>
#else
#include <unistd.h>
#endif
#include <libxml/tree.h>
#include "XCCDF/item.h"
#include "common/_error.h"
#include "common/debug_priv.h"
#include "common/oscap_acquire.h"
#include "common/oscap_pcre.h"
#include "xccdf_policy_priv.h"
#include "xccdf_policy_model_priv.h"
#include "public/xccdf_policy.h"
#include "oscap_helpers.h"
struct kickstart_commands {
struct oscap_list *package_install;
struct oscap_list *package_remove;
struct oscap_list *service_enable;
struct oscap_list *service_disable;
struct oscap_list *pre;
struct oscap_list *post;
struct oscap_list *logvol;
struct oscap_list *bootloader;
struct oscap_list *firewall_enable;
struct oscap_list *firewall_disable;
bool enable_kdump;
};
struct logvol_cmd {
char *path;
char *size;
};
struct bootc_commands {
struct oscap_list *dnf_install;
struct oscap_list *dnf_remove;
};
static int _rule_add_info_message(struct xccdf_rule_result *rr, ...)
{
va_list ap;
const char *fmt;
char *text;
struct xccdf_message *msg;
va_start(ap, rr);
fmt = va_arg(ap, const char *);
text = oscap_vsprintf(fmt, ap);
va_end(ap);
msg = xccdf_message_new();
xccdf_message_set_content(msg, text);
dI("[%s]->msg: %s", xccdf_rule_result_get_idref(rr), text);
free(text);
xccdf_message_set_severity(msg, XCCDF_MSG_INFO);
xccdf_rule_result_add_message(rr, msg);
return 0;
}
static inline bool _file_exists(const char *file)
{
struct stat sb;
return file != NULL && stat(file, &sb) == 0;
}
static int _write_text_to_fd(int output_fd, const char* text) {
ssize_t written = 0;
const ssize_t length = strlen(text);
while (written < length) {
ssize_t w = write(output_fd, text + written, length - written);
if (w < 0)
break;
written += w;
}
return written != length;
}
static int _write_text_to_fd_and_free(int output_fd, char *text)
{
const int ret = _write_text_to_fd(output_fd, text);
free(text);
return ret;
}
static int _write_remediation_to_fd_and_free(int output_fd, const char* template, char* text)
{
if (text == NULL)
return 0;
if (oscap_streq(template, "urn:xccdf:fix:script:ansible")) {
// Add required indentation in front of every single line
const char delim = '\n';
const char *indentation = " ";
char *current = text;
char *next_delim = NULL;
char *end = NULL;
do {
next_delim = strchr(current, delim);
if (next_delim != NULL) {
*next_delim = '\0';
}
// remove all trailing whitespaces
size_t len = strlen(current);
if (len > 0) {
end = current + len - 1;
while (isspace(*end)) {
*end = '\0';
if (end == current)
break;
end--;
}
}
if (strlen(current) > 0) {
// write indentation
if (_write_text_to_fd(output_fd, indentation) != 0) {
free(text);
return 1;
}
if (_write_text_to_fd(output_fd, current) != 0) {
free(text);
return 1;
}
}
if (_write_text_to_fd(output_fd, "\n") != 0) {
free(text);
return 1;
}
if (next_delim != NULL) {
// text is NULL terminated to this is guaranteed to point to valid memory
current = next_delim + 1;
}
} while (next_delim != NULL);
if (_write_text_to_fd(output_fd, "\n") != 0) {
free(text);
return 1;
}
free(text);
return 0;
} else {
// no extra processing is needed
return _write_text_to_fd_and_free(output_fd, text);
}
}
struct _interpret_map {
const char *sys;
const char *interpret;
};
typedef const char * (*_search_interpret_map_fn) (const char *, const struct _interpret_map *);
static const char *_search_interpret_map(const char *sys, const struct _interpret_map *map)
{
const struct _interpret_map *mapptr;
for (mapptr = map; mapptr->sys != NULL; ++mapptr)
if (oscap_streq(mapptr->sys, sys))
return mapptr->interpret;
return NULL;
}
static const char *_get_supported_interpret(const char *sys, const struct _interpret_map *unused)
{
static const struct _interpret_map _openscap_supported_interprets[] = {
#if defined(OS_FREEBSD)
{"urn:xccdf:fix:commands", "/usr/local/bin/bash"},
{"urn:xccdf:fix:script:sh", "/usr/local/bin/bash"},
{"urn:xccdf:fix:script:perl", "/usr/local/bin/perl"},
#else
{"urn:xccdf:fix:commands", "/bin/bash"},
{"urn:xccdf:fix:script:sh", "/bin/bash"},
{"urn:xccdf:fix:script:perl", "/usr/bin/perl"},
#endif
#ifdef PREFERRED_PYTHON_PATH
{"urn:xccdf:fix:script:python", PREFERRED_PYTHON_PATH},
#endif
#ifdef PYTHON2_PATH
{"urn:xccdf:fix:script:python2", PYTHON2_PATH},
#endif
#ifdef PYTHON3_PATH
{"urn:xccdf:fix:script:python3", PYTHON3_PATH},
#endif
{"urn:xccdf:fix:script:csh", "/bin/csh"},
{"urn:xccdf:fix:script:tclsh", "/usr/bin/tclsh"},
{"urn:xccdf:fix:script:javascript", "/usr/bin/js"},
// Current Ansible remediations are only Ansible snippets and are
// not runnable without header.
// {"urn:xccdf:fix:script:ansible", "/usr/bin/ansible-playbook"},
{NULL, NULL}
};
const char *interpret = _search_interpret_map(sys, _openscap_supported_interprets);
return _file_exists(interpret) ? interpret : NULL;
}
static inline struct xccdf_rule *_lookup_rule_by_rule_result(const struct xccdf_policy *policy, const struct xccdf_rule_result *rr)
{
const struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(policy);
if (benchmark == NULL)
return NULL;
return (struct xccdf_rule *) xccdf_benchmark_get_item(benchmark, xccdf_rule_result_get_idref(rr));
}
static inline bool _is_platform_applicable(struct xccdf_policy *policy, const char *platform)
{
if (oscap_streq("", platform))
return true;
struct oscap_stringlist *platform_list = oscap_stringlist_new();
oscap_stringlist_add_string(platform_list, platform);
struct oscap_string_iterator *platform_it = oscap_stringlist_get_strings(platform_list);
bool ret = xccdf_policy_model_platforms_are_applicable(xccdf_policy_get_model(policy), platform_it);
oscap_string_iterator_free(platform_it);
oscap_stringlist_free(platform_list);
return ret;
}
static struct oscap_list *_get_fixes(struct xccdf_policy *policy, const struct xccdf_rule *rule)
{
struct oscap_list *result = oscap_list_new();
struct xccdf_fix_iterator *fix_it = xccdf_rule_get_fixes(rule);
while (xccdf_fix_iterator_has_more(fix_it)) {
struct xccdf_fix *fix = xccdf_fix_iterator_next(fix_it);
oscap_list_add(result, fix);
}
xccdf_fix_iterator_free(fix_it);
return result;
}
static struct oscap_list *_filter_fixes_by_applicability(struct xccdf_policy *policy, const struct xccdf_rule *rule)
{
/* Filters out the fixes which are not applicable */
struct oscap_list *result = oscap_list_new();
if (!xccdf_policy_model_item_is_applicable(xccdf_policy_get_model(policy), (struct xccdf_item *) rule))
/* The fix element is applicable only when the all the parent elements are. */
return result;
struct xccdf_fix_iterator *fix_it = xccdf_rule_get_fixes(rule);
while (xccdf_fix_iterator_has_more(fix_it)) {
struct xccdf_fix *fix = xccdf_fix_iterator_next(fix_it);
const char *platform = xccdf_fix_get_platform(fix);
if (_is_platform_applicable(policy, platform))
oscap_list_add(result, fix);
}
xccdf_fix_iterator_free(fix_it);
return result;
}
static struct oscap_list *_filter_fixes_by_system(struct oscap_list *fixes, _search_interpret_map_fn filter, const struct _interpret_map *allowed_systems)
{
struct oscap_iterator *fix_it = oscap_iterator_new(fixes);
while (oscap_iterator_has_more(fix_it)) {
struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it);
const char *sys = xccdf_fix_get_system(fix);
if (sys == NULL)
sys = "";
if (filter(sys, allowed_systems) == NULL)
oscap_iterator_detach(fix_it);
}
oscap_iterator_free(fix_it);
return fixes;
}
static struct oscap_list *_filter_fixes_by_distruption_and_reboot(struct oscap_list *fixes)
{
bool reboot = true; // Let's assuming worse case and flip when fix/@rebot=false is found
xccdf_level_t disruption = XCCDF_HIGH;
struct oscap_iterator *fix_it = oscap_iterator_new(fixes);
while (oscap_iterator_has_more(fix_it)) {
struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it);
if (!xccdf_fix_get_reboot(fix))
reboot = false;
}
oscap_iterator_reset(fix_it);
while (oscap_iterator_has_more(fix_it)) {
struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it);
if (reboot == false && xccdf_fix_get_reboot(fix)) {
oscap_iterator_detach(fix_it);
} else {
xccdf_level_t dis = xccdf_fix_get_disruption(fix);
if (dis == XCCDF_MEDIUM || dis == XCCDF_LOW)
// Preferring "medium" and "low" over any other
disruption = dis;
}
}
if (disruption == XCCDF_MEDIUM || disruption == XCCDF_LOW) {
oscap_iterator_reset(fix_it);
while (oscap_iterator_has_more(fix_it)) {
struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it);
if (disruption != xccdf_fix_get_disruption(fix))
oscap_iterator_detach(fix_it);
}
}
oscap_iterator_free(fix_it);
return fixes;
}
static inline struct xccdf_fix *_find_suitable_fix(struct xccdf_policy *policy, struct xccdf_rule_result *rr)
{
/* In XCCDF 1.2, there is nothing like a default fix. However we use
* the following heuristics to find out some suitable fix:
* - remove fixes which are not appplicable (CPE)
* - remove fixes we cannot execute
* - choose fixes with the least disruption
* - choose fixes which do not require reboot
* - choose the first fix
*/
struct xccdf_fix *fix = NULL;
const struct xccdf_rule *rule = _lookup_rule_by_rule_result(policy, rr);
if (rule == NULL)
return NULL;
struct oscap_list *fixes = _filter_fixes_by_applicability(policy, rule);
fixes = _filter_fixes_by_system(fixes, _get_supported_interpret, NULL);
fixes = _filter_fixes_by_distruption_and_reboot(fixes);
struct xccdf_fix_iterator *fix_it = oscap_iterator_new(fixes);
if (xccdf_fix_iterator_has_more(fix_it))
fix = xccdf_fix_iterator_next(fix_it);
xccdf_fix_iterator_free(fix_it);
oscap_list_free0(fixes);
return fix;
}
static inline int _xccdf_fix_decode_xml(struct xccdf_fix *fix, char **result)
{
/* We need to decode & and similar sequences. That is a process reverse
* to the xmlEncodeSpecialChars()). Further we need to drop XML commentaries
* and expand CDATA blobs.
*/
*result = NULL;
char *str = oscap_sprintf("<x xmlns:xhtml='http://www.w3.org/1999/xhtml'>%s</x>",
xccdf_fix_get_content(fix));
xmlDoc *doc = xmlReadMemory(str, strlen(str), NULL, NULL, XML_PARSE_RECOVER |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET | XML_PARSE_NSCLEAN);
dI("Following script will be executed: '''%s'''", str);
free(str);
xmlBuffer *buff = xmlBufferCreate();
xmlNodePtr child = xmlDocGetRootElement(doc)->children;
while (child != NULL) {
switch (child->type) {
case XML_ELEMENT_NODE:{
/* Remaining child elements are suspicious. Perhaps it is an unresolved
* substitution element The execution would be dangerous, i.e. bash could
* interpret < and > characters of the element as pipe commands. */
xmlFreeDoc(doc);
xmlBufferFree(buff);
return 1;
}; break;
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:{
xmlNodeBufGetContent(buff, child);
}; break;
default:
break;
}
child = child->next;
}
xmlFreeDoc(doc);
*result = oscap_strdup((char *)xmlBufferContent(buff));
xmlBufferFree(buff);
return 0;
}
#if defined(unix) || defined(__unix__) || defined(__unix)
static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_fix *fix)
{
if (rr == NULL) {
return 1;
}
if (fix == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) {
_rule_add_info_message(rr, "No fix available.");
return 1;
}
const char *interpret = NULL;
if ((interpret = _get_supported_interpret(xccdf_fix_get_system(fix), NULL)) == NULL) {
_rule_add_info_message(rr, "Not supported xccdf:fix/@system='%s' or missing interpreter.",
xccdf_fix_get_system(fix) == NULL ? "" : xccdf_fix_get_system(fix));
return 1;
}
char *fix_text = NULL;
if (_xccdf_fix_decode_xml(fix, &fix_text) != 0) {
_rule_add_info_message(rr, "Fix element contains unresolved child elements.");
return 1;
}
int result = 1;
char *temp_dir = oscap_acquire_temp_dir();
if (temp_dir == NULL)
goto cleanup;
// TODO: Directory and files shall be labeled with SELinux to prevent
// confined processes with less priviledges to transit to oscap domain
// and become basically unconfined.
char *temp_file = NULL;
int fd = oscap_acquire_temp_file(temp_dir, "fix-XXXXXXXX", &temp_file);
if (fd == -1) {
_rule_add_info_message(rr, "mkstemp failed: %s", strerror(errno));
free(temp_file);
goto cleanup;
}
if (_write_text_to_fd(fd, fix_text) != 0) {
_rule_add_info_message(rr, "Could not write to the temp file: %s", strerror(errno));
(void) close(fd);
free(temp_file);
goto cleanup;
}
if (close(fd) != 0)
_rule_add_info_message(rr, "Could not close temp file: %s", strerror(errno));
int pipefd[2];
if (pipe(pipefd) == -1) {
_rule_add_info_message(rr, "Could not create pipe: %s", strerror(errno));
free(temp_file);
goto cleanup;
}
int fork_result = fork();
if (fork_result >= 0) {
/* fork succeeded */
if (fork_result == 0) {
/* Execute fix and forward output to the parent. */
close(pipefd[0]);
dup2(pipefd[1], fileno(stdout));
dup2(pipefd[1], fileno(stderr));
close(pipefd[1]);
char *const argvp[3] = {
(char *)interpret,
temp_file,
NULL
};
// We are inheriting openscap environment
execve(interpret, argvp, environ);
/* Wow, execve returned. In this special case, we failed to execute the fix
* and we return 0 from function. At least the following error message will
* indicate the problem in xccdf:message. */
printf("Error while executing fix script: execve returned: %s\n", strerror(errno));
exit(42);
} else {
free(temp_file);
close(pipefd[1]);
char *stdout_buff = oscap_acquire_pipe_to_string(pipefd[0]);
int wstatus;
waitpid(fork_result, &wstatus, 0);
_rule_add_info_message(rr, "Fix execution completed and returned: %d", WEXITSTATUS(wstatus));
if (stdout_buff != NULL && stdout_buff[0] != '\0')
_rule_add_info_message(rr, stdout_buff);
free(stdout_buff);
/* We return zero to indicate success. Rather than returning the exit code. */
result = 0;
}
} else {
_rule_add_info_message(rr, "Failed to fork. %s", strerror(errno));
free(temp_file);
}
cleanup:
oscap_acquire_cleanup_dir(&temp_dir);
free(fix_text);
return result;
}
#else
static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_fix *fix)
{
if (rr == NULL) {
return 1;
}
if (fix == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) {
_rule_add_info_message(rr, "No fix available.");
return 1;
} else {
_rule_add_info_message(rr, "Cannot execute the fix script: not implemented");
}
return 1;
}
#endif
int xccdf_policy_rule_result_remediate(struct xccdf_policy *policy, struct xccdf_rule_result *rr, struct xccdf_fix *fix, struct xccdf_result *test_result)
{
if (policy == NULL || rr == NULL)
return 1;
if (xccdf_rule_result_get_result(rr) != XCCDF_RESULT_FAIL)
return 0;
// if a miscellaneous error happens (fix unsuitable or if we want to skip it for any reason
// we set misc_error to one, and the fix will be reported as error (and not skipped without log like before)
int misc_error=0;
if (fix == NULL) {
fix = _find_suitable_fix(policy, rr);
if (fix == NULL) {
// We want to append xccdf:message about missing fix.
_rule_add_info_message(rr, "No suitable fix found.");
xccdf_rule_result_set_result(rr, XCCDF_RESULT_FAIL);
misc_error=1;
}
}
struct xccdf_check *check = NULL;
struct xccdf_check_iterator *check_it = xccdf_rule_result_get_checks(rr);
while (xccdf_check_iterator_has_more(check_it))
check = xccdf_check_iterator_next(check_it);
xccdf_check_iterator_free(check_it);
if(misc_error == 0){
/* Initialize the fix. */
struct xccdf_fix *cfix = xccdf_fix_clone(fix);
int res = xccdf_policy_resolve_fix_substitution(policy, cfix, rr, test_result);
xccdf_rule_result_add_fix(rr, cfix);
if (res != 0) {
_rule_add_info_message(rr, "Fix execution was aborted: Text substitution failed.");
xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR);
misc_error=1;
}else{
/* Execute the fix. */
res = _xccdf_fix_execute(rr, cfix);
if (res != 0) {
_rule_add_info_message(rr, "Fix was not executed. Execution was aborted.");
xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR);
misc_error=1;
}
}
}
/* We report rule during remediation even if fix isn't executed due to a miscellaneous error */
int report = 0;
struct xccdf_rule *rule = _lookup_rule_by_rule_result(policy, rr);
if (rule == NULL) {
// Sadly, we cannot handle this since b9d123d53140c6e369b7f2206e4e3e63dc556fd1.
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find xccdf:Rule/@id=%s.", xccdf_rule_result_get_idref(rr));
}
else {
report = xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_START, (void *) rule);
if (report != 0)
return report;
}
if(misc_error == 0){
/* Verify fix if applied by calling OVAL again */
if (check == NULL) {
xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR);
_rule_add_info_message(rr, "Failed to verify applied fix: Missing xccdf:check.");
} else {
int new_result = xccdf_policy_check_evaluate(policy, check);
if (new_result == XCCDF_RESULT_PASS)
xccdf_rule_result_set_result(rr, XCCDF_RESULT_FIXED);
else {
xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR);
_rule_add_info_message(rr, "Failed to verify applied fix: Checking engine returns: %s",
new_result <= 0 ? "internal error" : xccdf_test_result_type_get_text(new_result));
}
}
}
xccdf_rule_result_set_time_current(rr);
return rule == NULL ? 0 : xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_END, (void *) rr);
}
int xccdf_policy_remediate(struct xccdf_policy *policy, struct xccdf_result *result)
{
__attribute__nonnull__(result);
struct xccdf_rule_result_iterator *rr_it = xccdf_result_get_rule_results(result);
while (xccdf_rule_result_iterator_has_more(rr_it)) {
struct xccdf_rule_result *rr = xccdf_rule_result_iterator_next(rr_it);
xccdf_policy_rule_result_remediate(policy, rr, NULL, result);
}
xccdf_rule_result_iterator_free(rr_it);
xccdf_result_set_end_time_current(result);
return 0;
}
/* --- Follows functions for generating XCCDF:Fix script --- */
static const struct xccdf_fix *_find_fix_for_template(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template)
{
struct xccdf_fix *fix = NULL;
struct oscap_list *fixes = _get_fixes(policy, rule);
if (template) {
const struct _interpret_map map[] = { {template, "Cloud!"},
{NULL, NULL}};
fixes = _filter_fixes_by_system(fixes, _search_interpret_map, map);
}
fixes = _filter_fixes_by_distruption_and_reboot(fixes);
struct xccdf_fix_iterator *fix_it = oscap_iterator_new(fixes);
if (xccdf_fix_iterator_has_more(fix_it))
fix = xccdf_fix_iterator_next(fix_it);
xccdf_fix_iterator_free(fix_it);
oscap_list_free0(fixes);
return fix;
}
static int _write_fix_header_to_fd(const char *sys, int output_fd, struct xccdf_rule *rule, unsigned int current, unsigned int total)
{
if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) {
char *fix_header = oscap_sprintf(
"###############################################################################\n"
"# BEGIN fix (%i / %i) for '%s'\n"
"###############################################################################\n"
"(>&2 echo \"Remediating rule %i/%i: '%s'\"); (\n",
current, total, xccdf_rule_get_id(rule), current, total, xccdf_rule_get_id(rule));
return _write_text_to_fd_and_free(output_fd, fix_header);
} else {
return 0;
}
}
static int _write_fix_footer_to_fd(const char *sys, int output_fd, struct xccdf_rule *rule)
{
if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) {
char *fix_footer = oscap_sprintf("\n) # END fix for '%s'\n\n", xccdf_rule_get_id(rule));
return _write_text_to_fd_and_free(output_fd, fix_footer);
} else {
return 0;
}
}
static int _write_fix_missing_warning_to_fd(const char *sys, int output_fd, struct xccdf_rule *rule)
{
if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) {
char *fix_footer = oscap_sprintf("(>&2 echo \"FIX FOR THIS RULE '%s' IS MISSING!\")\n", xccdf_rule_get_id(rule));
return _write_text_to_fd_and_free(output_fd, fix_footer);
} else {
return 0;
}
}
struct blueprint_entries {
const char *pattern;
struct oscap_list *list;
oscap_pcre_t *re;
};
struct blueprint_customizations {
struct oscap_list *generic;
struct oscap_list *services_enable;
struct oscap_list *services_disable;
struct oscap_list *services_mask;
struct oscap_list *kernel_append;
};
static inline int _parse_blueprint_fix(const char *fix_text, struct blueprint_customizations *customizations)
{
char *err = NULL;
int errofs;
int ret = 0;
struct blueprint_entries tab[] = {
{"\\[customizations\\.services\\]\\s+enabled[=\\s]+\\[([^\\]]+)\\]\\s+", customizations->services_enable, NULL},
{"\\[customizations\\.services\\]\\s+disabled[=\\s]+\\[([^\\]]+)\\]\\s+", customizations->services_disable, NULL},
{"\\[customizations\\.services\\]\\s+masked[=\\s]+\\[([^\\]]+)\\]\\s+", customizations->services_mask, NULL},
{"\\[customizations\\.kernel\\]\\s+append[=\\s\"]+([^\"]+)[\\s\"]+", customizations->kernel_append, NULL},
// We do this only to pop the 'distro' entry to the top of the generic list,
// effectively placing it to the root of the TOML document.
{"\\s+(distro[=\\s\"]+[^\"]+[\\s\"]+)", customizations->generic, NULL},
{NULL, NULL, NULL}
};
for (int i = 0; tab[i].pattern != NULL; i++) {
tab[i].re = oscap_pcre_compile(tab[i].pattern, OSCAP_PCRE_OPTS_UTF8, &err, &errofs);
if (tab[i].re == NULL) {
dE("Unable to compile /%s/ regex pattern, oscap_pcre_compile() returned error (offset: %d): '%s'.\n", tab[i].pattern, errofs, err);
ret = 1;
goto exit;
}
}
const size_t fix_text_len = strlen(fix_text);
size_t start_offset = 0;
int ovector[6] = {0};
for (int i = 0; tab[i].pattern != NULL; i++) {
while (true) {
const int match = oscap_pcre_exec(tab[i].re, fix_text, fix_text_len, start_offset,
0, ovector, sizeof(ovector) / sizeof(ovector[0]));
if (match == -1)
break;
if (match != 2) {
dE("Expected 1 capture group matches per entry. Found %i!", match - 1);
ret = 1;
goto exit;
}
char *val = malloc((ovector[3] - ovector[2] + 1) * sizeof(char));
memcpy(val, &fix_text[ovector[2]], ovector[3] - ovector[2]);
val[ovector[3] - ovector[2]] = '\0';
if (!oscap_list_contains(tab[i].list, val, (oscap_cmp_func) oscap_streq)) {
oscap_list_prepend(tab[i].list, val);
} else {
free(val);
}
start_offset = ovector[1];
}
}
if (start_offset < fix_text_len-1) {
oscap_list_add(customizations->generic, strdup(fix_text + start_offset));
}
exit:
oscap_pcre_err_free(err);
for (int i = 0; tab[i].pattern != NULL; i++)
oscap_pcre_free(tab[i].re);
return ret;
}
static inline int _parse_ansible_fix(const char *fix_text, struct oscap_list *variables, struct oscap_list *tasks)
{
// TODO: Tolerate different indentation styles in this regex
const char *pattern =
"- name: XCCDF Value [^ ]+ # promote to variable\n set_fact:\n"
" ([^:]+): (.+)\n tags:\n - always\n";
char *err;
int errofs;
oscap_pcre_t *re = oscap_pcre_compile(pattern, OSCAP_PCRE_OPTS_UTF8, &err, &errofs);
if (re == NULL) {
dE("Unable to compile regex pattern, "
"oscap_pcre_compile() returned error (offset: %d): '%s'.\n", errofs, err);
oscap_pcre_err_free(err);
return 1;
}
// ovector sizing:
// 2 elements are used for the whole needle,
// 4 elements are used for the 2 capture groups
// pcre documentation says we should allocate a third extra for additional
// workspace.
// (2 + 4) * (3 / 2) = 9
int ovector[9];
const size_t fix_text_len = strlen(fix_text);
size_t start_offset = 0;
while (true) {
const int match = oscap_pcre_exec(re, fix_text, fix_text_len, start_offset,
0, ovector, sizeof(ovector) / sizeof(ovector[0]));
if (match == -1)
break;
if (match != 3) {
dE("Expected 2 capture group matches per XCCDF variable. Found %i!",
match - 1);
oscap_pcre_free(re);
return 1;
}
// ovector[0] and [1] hold the start and end of the whole needle match
// ovector[2] and [3] hold the start and end of the first capture group
// ovector[4] and [5] hold the start and end of the second capture group
char *variable_name = malloc((ovector[3] - ovector[2] + 1) * sizeof(char));
memcpy(variable_name, &fix_text[ovector[2]], ovector[3] - ovector[2]);
variable_name[ovector[3] - ovector[2]] = '\0';
char *variable_value = malloc((ovector[5] - ovector[4] + 1) * sizeof(char));
memcpy(variable_value, &fix_text[ovector[4]], ovector[5] - ovector[4]);
variable_value[ovector[5] - ovector[4]] = '\0';
char *var_line = oscap_sprintf(" %s: %s\n", variable_name, variable_value);
free(variable_name);
free(variable_value);
if (!oscap_list_contains(variables, var_line, (oscap_cmp_func) oscap_streq)) {
oscap_list_add(variables, var_line);
}
// Remarks: ovector doesn't contain values relative to start_offset, it contains
// absolute indices of fix_text.
const int length_between_matches = ovector[0] - start_offset;
char *remediation_part = malloc((length_between_matches + 1) * sizeof(char));
memcpy(remediation_part, &fix_text[start_offset], length_between_matches);
remediation_part[length_between_matches] = '\0';
oscap_list_add(tasks, remediation_part);
start_offset = ovector[1]; // next time start after the entire pattern
}
if (fix_text_len - start_offset > 0) {
char *remediation_part = malloc((fix_text_len - start_offset + 1) * sizeof(char));
memcpy(remediation_part, &fix_text[start_offset], fix_text_len - start_offset);
remediation_part[fix_text_len - start_offset] = '\0';
oscap_list_add(tasks, remediation_part);
}
oscap_pcre_free(re);
return 0;
}
static int _xccdf_policy_rule_get_fix_text(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, char **fix_text)
{
// Ensure that given Rule is selected and applicable (CPE).
const bool is_selected = xccdf_policy_is_item_selected(policy, xccdf_rule_get_id(rule));
if (!is_selected) {
dI("Skipping unselected Rule/@id=\"%s\"", xccdf_rule_get_id(rule));
return 0;
}
// Find the most suitable fix.
const struct xccdf_fix *fix = _find_fix_for_template(policy, rule, template);
if (fix == NULL) {
dI("No fix element was found for Rule/@id=\"%s\"", xccdf_rule_get_id(rule));
return 0;
}
dI("Processing a fix for Rule/@id=\"%s\"", xccdf_rule_get_id(rule));
// Process Text Substitute within the fix
struct xccdf_fix *cfix = xccdf_fix_clone(fix);
int res = xccdf_policy_resolve_fix_substitution(policy, cfix, NULL, NULL);
if (res != 0) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "A fix for Rule/@id=\"%s\" was skipped: Text substitution failed.",
xccdf_rule_get_id(rule));
xccdf_fix_free(cfix);
return res == 1; // Value 2 indicates warning.
}
// Refine. Resolve XML comments, CDATA and remaining elements
if (_xccdf_fix_decode_xml(cfix, fix_text) != 0) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "A fix element for Rule/@id=\"%s\" contains unresolved child elements.",
xccdf_rule_get_id(rule));
xccdf_fix_free(cfix);
return 1;
}
xccdf_fix_free(cfix);
return 0;
}
static int _xccdf_policy_rule_generate_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, int output_fd, unsigned int current, unsigned int total)
{
int ret = _write_fix_header_to_fd(template, output_fd, rule, current, total);
if (ret != 0) {
return ret;
}
char *fix_text = NULL;
ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text);
if (fix_text == NULL || ret != 0) {
ret = _write_fix_missing_warning_to_fd(template, output_fd, rule);
} else {
ret = _write_remediation_to_fd_and_free(output_fd, template, fix_text);
}
if (ret != 0) {
return ret;
}
ret = _write_fix_footer_to_fd(template, output_fd, rule);
return ret;
}
static int _xccdf_policy_rule_generate_blueprint_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, struct blueprint_customizations *customizations)
{
char *fix_text = NULL;
int ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text);
if (fix_text == NULL) {
return ret;
}
ret = _parse_blueprint_fix(fix_text, customizations);
free(fix_text);
return ret;
}
static int _parse_line(const char *line, struct kickstart_commands *cmds)
{
int ret = 0;
char *dup = strdup(line);
char **words = oscap_split(dup, " ");
enum states {
KS_START,
KS_PACKAGE,
KS_PACKAGE_INSTALL,
KS_PACKAGE_REMOVE,
KS_SERVICE,
KS_SERVICE_ENABLE,
KS_SERVICE_DISABLE,
KS_LOGVOL,
KS_LOGVOL_SIZE,
KS_BOOTLOADER,
KS_KDUMP,
KS_FIREWALL,
KS_FIREWALL_ENABLE,
KS_FIREWALL_DISABLE,
KS_ERROR
};
int state = KS_START;
struct logvol_cmd *current_logvol_cmd = NULL;
for (unsigned int i = 0; words[i] != NULL; i++) {
char *word = oscap_trim(words[i]);
if (*word == '\0')
continue;
switch (state) {
case KS_START:
if (!strcmp(word, "package")) {
state = KS_PACKAGE;
} else if (!strcmp(word, "service")) {
state = KS_SERVICE;
} else if (!strcmp(word, "logvol")) {
state = KS_LOGVOL;
} else if (!strcmp(word, "bootloader")) {
state = KS_BOOTLOADER;
} else if (!strcmp(word, "kdump")) {
state = KS_KDUMP;
} else if (!strcmp(word, "firewall")) {
state = KS_FIREWALL;
} else {
ret = 1;
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported command keyword '%s' in command: '%s'", word, line);
goto cleanup;
}
break;
case KS_PACKAGE:
if (!strcmp(word, "install")) {
state = KS_PACKAGE_INSTALL;
} else if (!strcmp(word, "remove")) {
state = KS_PACKAGE_REMOVE;
} else {
ret = 1;
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'package' command keyword '%s' in command:'%s'", word, line);
goto cleanup;
}
break;
case KS_PACKAGE_INSTALL:
oscap_list_add(cmds->package_install, strdup(word));
break;
case KS_PACKAGE_REMOVE:
oscap_list_add(cmds->package_remove, strdup(word));
break;
case KS_SERVICE:
if (!strcmp(word, "enable")) {
state = KS_SERVICE_ENABLE;
} else if (!strcmp(word, "disable")) {
state = KS_SERVICE_DISABLE;
} else {
ret = 1;
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'service' command keyword '%s' in command: '%s'", word, line);
goto cleanup;
}
break;
case KS_SERVICE_ENABLE:
oscap_list_add(cmds->service_enable, strdup(word));
break;
case KS_SERVICE_DISABLE:
oscap_list_add(cmds->service_disable, strdup(word));
break;
case KS_LOGVOL:
current_logvol_cmd = malloc(sizeof(struct logvol_cmd));
current_logvol_cmd->path = strdup(word);