| 1 | /* |
| 2 | * Copyright 2009--2014 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 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | |
| 29 | #include "common/list.h" |
| 30 | #include "common/_error.h" |
| 31 | #include "common/debug_priv.h" |
| 32 | #include "public/xccdf_benchmark.h" |
| 33 | #include "XCCDF/item.h" |
| 34 | #include "XCCDF/result_scoring_priv.h" |
| 35 | |
| 36 | /** |
| 37 | * XCCDF Default score structure represents Default XCCDF Score model |
| 38 | * for each rule |
| 39 | */ |
| 40 | struct xccdf_default_score { |
| 41 | float score; |
| 42 | float accumulator; |
| 43 | float weight_score; |
| 44 | int count; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * XCCDF Flat score structure represents Flat XCCDF Score model |
| 49 | * for each rule |
| 50 | */ |
| 51 | struct xccdf_flat_score { |
| 52 | float score; |
| 53 | float weight; |
| 54 | |
| 55 | }; |
| 56 | |
| 57 | static struct xccdf_default_score * xccdf_item_get_default_score(struct xccdf_item * item, struct xccdf_result * test_result) |
| 58 | { |
| 59 | // Implements algorithm as described in NISTIR-7275-r4 |
| 60 | // Table 40: Default Model Algorithm Sub-Steps |
| 61 | struct xccdf_default_score *score; |
| 62 | struct xccdf_default_score *ch_score; |
| 63 | struct xccdf_rule_result *rule_result; |
| 64 | struct xccdf_item *child; |
| 65 | |
| 66 | xccdf_type_t itype = xccdf_item_get_type(item); |
| 67 | |
| 68 | switch (itype) { |
| 69 | case XCCDF_RULE: { |
| 70 | /* Rule */ |
| 71 | const char *rule_id = xccdf_rule_get_id((const struct xccdf_rule *) item); |
| 72 | rule_result = xccdf_result_get_rule_result_by_id(test_result, rule_id); |
| 73 | if (rule_result == NULL) { |
| 74 | dE("Rule result ID(%s) not fount", rule_id); |
| 75 | return NULL; |
| 76 | } |
| 77 | if (xccdf_rule_result_get_role(rule_result) == XCCDF_ROLE_UNSCORED) { |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | /* Ignore these rules */ |
| 82 | if ((xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_NOT_SELECTED) || |
| 83 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_NOT_APPLICABLE) || |
| 84 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_INFORMATIONAL) || |
| 85 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_NOT_CHECKED)) |
| 86 | return NULL; |
| 87 | |
| 88 | score = malloc(sizeof(struct xccdf_default_score)); |
| 89 | |
| 90 | /* Count with this rule */ |
| 91 | score->count = 1; |
| 92 | |
| 93 | /* If the test result is 'pass', assign the node a score of 100, otherwise assign a score of 0 */ |
| 94 | if ((xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_PASS) || |
| 95 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_FIXED)) |
| 96 | score->score = 100.0; |
| 97 | else |
| 98 | score->score = 0.0; |
| 99 | |
| 100 | /* Default weight */ |
| 101 | score->weight_score = score->score * xccdf_item_get_weight(item); |
| 102 | } break; |
| 103 | |
| 104 | case XCCDF_BENCHMARK: |
| 105 | case XCCDF_GROUP: { |
| 106 | /* Init */ |
| 107 | score = malloc(sizeof(struct xccdf_default_score)); |
| 108 | score->count = 0; |
| 109 | score->score = 0.0; |
| 110 | score->accumulator = 0.0; |
| 111 | |
| 112 | /* Recurse */ |
| 113 | struct xccdf_item_iterator * child_it; |
| 114 | if (itype == XCCDF_GROUP) |
| 115 | child_it = xccdf_group_get_content((const struct xccdf_group *)item); |
| 116 | else |
| 117 | child_it = xccdf_benchmark_get_content((const struct xccdf_benchmark *)item); |
| 118 | |
| 119 | while (xccdf_item_iterator_has_more(child_it)) { |
| 120 | child = xccdf_item_iterator_next(child_it); |
| 121 | ch_score = xccdf_item_get_default_score(child, test_result); |
| 122 | |
| 123 | if (ch_score == NULL) /* we got item that can't be processed */ |
| 124 | continue; |
| 125 | |
| 126 | if (ch_score->count == 0) { /* we got item that has no selected items */ |
| 127 | free(ch_score); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | /* If child's count value is not 0, then add the child's wighted score to this node's score */ |
| 132 | score->score += ch_score->weight_score; |
| 133 | score->count++; |
| 134 | score->accumulator += xccdf_item_get_weight(child); |
| 135 | |
| 136 | free(ch_score); |
| 137 | } |
| 138 | |
| 139 | /* Normalize */ |
| 140 | if (score->count && score->accumulator) |
| 141 | score->score = score->score / score->accumulator; |
| 142 | /* Default weight */ |
| 143 | score->weight_score = score->score * xccdf_item_get_weight(item); |
| 144 | |
| 145 | xccdf_item_iterator_free(child_it); |
| 146 | } break; |
| 147 | |
| 148 | default: { |
| 149 | dE("Unsupported item type: %d", itype); |
| 150 | score=NULL; |
| 151 | } break; |
| 152 | |
| 153 | } /* switch */ |
| 154 | return score; |
| 155 | } |
| 156 | |
| 157 | static struct xccdf_flat_score * xccdf_item_get_flat_score(struct xccdf_item * item, struct xccdf_result * test_result, bool unweighted) |
| 158 | { |
| 159 | // Implements algorithm as described in NISTIR-7275-r4 |
| 160 | // Table 41: Flat Model Algorithm Sub-Steps |
| 161 | struct xccdf_flat_score *score; |
| 162 | struct xccdf_flat_score *ch_score; |
| 163 | struct xccdf_rule_result *rule_result; |
| 164 | struct xccdf_item *child; |
| 165 | |
| 166 | xccdf_type_t itype = xccdf_item_get_type(item); |
| 167 | |
| 168 | switch (itype) { |
| 169 | case XCCDF_RULE:{ |
| 170 | /* Rule */ |
| 171 | const char *rule_id = xccdf_rule_get_id((const struct xccdf_rule *) item); |
| 172 | rule_result = xccdf_result_get_rule_result_by_id(test_result, rule_id); |
| 173 | if (rule_result == NULL) { |
| 174 | dE("Rule result ID(%s) not fount", rule_id); |
| 175 | return NULL; |
| 176 | } |
| 177 | if (xccdf_rule_result_get_role(rule_result) == XCCDF_ROLE_UNSCORED) { |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | /* Ignore these rules */ |
| 182 | if ((xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_NOT_SELECTED) || |
| 183 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_NOT_APPLICABLE) || |
| 184 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_INFORMATIONAL) || |
| 185 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_NOT_CHECKED)) |
| 186 | return NULL; |
| 187 | |
| 188 | score = malloc(sizeof(struct xccdf_flat_score)); |
| 189 | |
| 190 | /* max possible score = sum of weights*/ |
| 191 | if (unweighted) |
| 192 | score->weight = 1.0; |
| 193 | else score->weight = |
| 194 | xccdf_item_get_weight(item); |
| 195 | |
| 196 | /* score = sum of weights of rules that pass */ |
| 197 | if ((xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_PASS) || |
| 198 | (xccdf_rule_result_get_result(rule_result) == XCCDF_RESULT_FIXED)) { |
| 199 | if (unweighted) |
| 200 | score->score = 1.0; |
| 201 | else |
| 202 | score->score = xccdf_item_get_weight(item); |
| 203 | } else |
| 204 | score->score = 0.0; /* fail */ |
| 205 | } break; |
| 206 | case XCCDF_BENCHMARK: |
| 207 | case XCCDF_GROUP:{ |
| 208 | /* Init */ |
| 209 | score = malloc(sizeof(struct xccdf_flat_score)); |
| 210 | score->score = 0; |
| 211 | score->weight = 0.0; |
| 212 | |
| 213 | /* Recurse */ |
| 214 | struct xccdf_item_iterator * child_it; |
| 215 | if (itype == XCCDF_GROUP) |
| 216 | child_it = xccdf_group_get_content((const struct xccdf_group *)item); |
| 217 | else |
| 218 | child_it = xccdf_benchmark_get_content((const struct xccdf_benchmark *)item); |
| 219 | |
| 220 | while (xccdf_item_iterator_has_more(child_it)) { |
| 221 | child = xccdf_item_iterator_next(child_it); |
| 222 | ch_score = xccdf_item_get_flat_score(child, test_result, unweighted); |
| 223 | |
| 224 | if (ch_score == NULL) /* we got item that can't be processed */ |
| 225 | continue; |
| 226 | |
| 227 | if (ch_score->weight == 0) { /* we got item that has no selected items */ |
| 228 | free(ch_score); |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | /* If child's count value is not 0, then add the child's wighted score to this node's score */ |
| 233 | score->score += ch_score->score; |
| 234 | score->weight += ch_score->weight; |
| 235 | |
| 236 | free(ch_score); |
| 237 | } |
| 238 | |
| 239 | xccdf_item_iterator_free(child_it); |
| 240 | } break; |
| 241 | |
| 242 | default: { |
| 243 | dE("Unsupported item type: %d", itype); |
| 244 | score=NULL; |
| 245 | } break; |
| 246 | |
| 247 | } /* switch */ |
| 248 | return score; |
| 249 | } |
| 250 | |
| 251 | struct xccdf_score *xccdf_result_calculate_score(struct xccdf_result *test_result, struct xccdf_item *benchmark, const char *score_system) |
| 252 | { |
| 253 | struct xccdf_score *score = xccdf_score_new(); |
| 254 | xccdf_score_set_system(score, score_system); |
| 255 | if (oscap_streq(score_system, "urn:xccdf:scoring:default")) { |
| 256 | struct xccdf_default_score * item_score = xccdf_item_get_default_score(benchmark, test_result); |
| 257 | xccdf_score_set_score(score, item_score->score); |
| 258 | free(item_score); |
| 259 | } else if (oscap_streq(score_system, "urn:xccdf:scoring:flat")) { |
| 260 | struct xccdf_flat_score * item_score = xccdf_item_get_flat_score(benchmark, test_result, false); |
| 261 | xccdf_score_set_maximum(score, item_score->weight); |
| 262 | xccdf_score_set_score(score, item_score->score); |
| 263 | free(item_score); |
| 264 | } else if (oscap_streq(score_system, "urn:xccdf:scoring:flat-unweighted")) { |
| 265 | struct xccdf_flat_score * item_score = xccdf_item_get_flat_score(benchmark, test_result, true); |
| 266 | xccdf_score_set_maximum(score, item_score->weight); |
| 267 | xccdf_score_set_score(score, item_score->score); |
| 268 | free(item_score); |
| 269 | } else if (oscap_streq(score_system, "urn:xccdf:scoring:absolute")) { |
| 270 | int absolute; |
| 271 | struct xccdf_flat_score * item_score = xccdf_item_get_flat_score(benchmark, test_result, false); |
| 272 | xccdf_score_set_maximum(score, item_score->weight); |
| 273 | absolute = (item_score->score == item_score->weight); |
| 274 | xccdf_score_set_score(score, absolute); |
| 275 | free(item_score); |
| 276 | } else { |
| 277 | xccdf_score_free(score); |
| 278 | dE("Scoring system \"%s\" is not supported.", score_system); |
| 279 | return NULL; |
| 280 | } |
| 281 | return score; |
| 282 | } |
| 283 | |
| 284 | int xccdf_result_recalculate_scores(struct xccdf_result *result, struct xccdf_item *benchmark) |
| 285 | { |
| 286 | struct oscap_list *new_scores = oscap_list_new(); |
| 287 | struct xccdf_score_iterator *score_it = xccdf_result_get_scores(result); |
| 288 | while (xccdf_score_iterator_has_more(score_it)) { |
| 289 | struct xccdf_score *old = xccdf_score_iterator_next(score_it); |
| 290 | struct xccdf_score *new = xccdf_result_calculate_score(result, benchmark, |
| 291 | xccdf_score_get_system(old)); |
| 292 | if (new == NULL) { |
| 293 | oscap_list_free(new_scores, (oscap_destruct_func) xccdf_score_free); |
| 294 | xccdf_score_iterator_free(score_it); |
| 295 | return 1; |
| 296 | } |
| 297 | oscap_list_add(new_scores, new); |
| 298 | } |
| 299 | xccdf_score_iterator_free(score_it); |
| 300 | oscap_list_free(((struct xccdf_item *)result)->sub.result.scores, (oscap_destruct_func) xccdf_score_free); |
| 301 | ((struct xccdf_item *)result)->sub.result.scores = new_scores; |
| 302 | return 0; |
| 303 | } |