| 1 | /** |
| 2 | * @file sce_engine.c |
| 3 | * \brief Script Check Engine |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright 2011--2013 Red Hat Inc., Durham, North Carolina. |
| 8 | * All Rights Reserved. |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2.1 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | * |
| 24 | * Authors: |
| 25 | * "Martin Preisler" <[email protected]> |
| 26 | */ |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include <config.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "common/_error.h" |
| 33 | #include "common/util.h" |
| 34 | #include "common/list.h" |
| 35 | #include "common/oscap_acquire.h" |
| 36 | #include "common/oscap_string.h" |
| 37 | #include "common/debug_priv.h" |
| 38 | #include "sce_engine_api.h" |
| 39 | #include "oscap_helpers.h" |
| 40 | |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <stdio.h> |
| 44 | |
| 45 | #if defined(OS_FREEBSD) |
| 46 | #include <sys/procctl.h> |
| 47 | #include <signal.h> |
| 48 | #include <sys/wait.h> |
| 49 | #else |
| 50 | #include <wait.h> |
| 51 | #endif |
| 52 | |
| 53 | #include <unistd.h> |
| 54 | #include <sys/stat.h> |
| 55 | #include <assert.h> |
| 56 | #include <fcntl.h> |
| 57 | #include <sys/types.h> |
| 58 | #if defined(OS_LINUX) |
| 59 | #include <sys/prctl.h> |
| 60 | #endif |
| 61 | #include <limits.h> |
| 62 | #include <unistd.h> |
| 63 | #include <libgen.h> |
| 64 | |
| 65 | #define SCE_SCRIPT "oscap-run-sce-script" |
| 66 | |
| 67 | struct sce_check_result |
| 68 | { |
| 69 | char* href; |
| 70 | char* basename; |
| 71 | char* std_out; |
| 72 | char* std_err; |
| 73 | int exit_code; |
| 74 | struct oscap_stringlist* environment_variables; |
| 75 | xccdf_test_result_type_t xccdf_result; |
| 76 | }; |
| 77 | |
| 78 | struct sce_check_result* sce_check_result_new(void) |
| 79 | { |
| 80 | struct sce_check_result* ret = malloc(sizeof(struct sce_check_result)); |
| 81 | ret->href = NULL; |
| 82 | ret->basename = NULL; |
| 83 | ret->std_out = NULL; |
| 84 | ret->std_err = NULL; |
| 85 | ret->environment_variables = oscap_stringlist_new(); |
| 86 | ret->xccdf_result = XCCDF_RESULT_UNKNOWN; |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | void sce_check_result_free(struct sce_check_result* v) |
| 92 | { |
| 93 | if (!v) |
| 94 | return; |
| 95 | |
| 96 | free(v->href); |
| 97 | free(v->basename); |
| 98 | free(v->std_out); |
| 99 | free(v->std_err); |
| 100 | |
| 101 | oscap_stringlist_free(v->environment_variables); |
| 102 | |
| 103 | free(v); |
| 104 | } |
| 105 | |
| 106 | void sce_check_result_set_href(struct sce_check_result* v, const char* href) |
| 107 | { |
| 108 | free(v->href); |
| 109 | v->href = oscap_strdup(href); |
| 110 | } |
| 111 | |
| 112 | const char* sce_check_result_get_href(struct sce_check_result* v) |
| 113 | { |
| 114 | return v->href; |
| 115 | } |
| 116 | |
| 117 | void sce_check_result_set_basename(struct sce_check_result* v, const char* base_name) |
| 118 | { |
| 119 | free(v->basename); |
| 120 | v->basename = oscap_strdup(base_name); |
| 121 | } |
| 122 | |
| 123 | const char* sce_check_result_get_basename(struct sce_check_result* v) |
| 124 | { |
| 125 | return v->basename; |
| 126 | } |
| 127 | |
| 128 | void sce_check_result_set_stdout(struct sce_check_result* v, const char* _stdout) |
| 129 | { |
| 130 | free(v->std_out); |
| 131 | v->std_out = oscap_strdup(_stdout); |
| 132 | } |
| 133 | |
| 134 | const char* sce_check_result_get_stdout(struct sce_check_result* v) |
| 135 | { |
| 136 | return v->std_out; |
| 137 | } |
| 138 | |
| 139 | void sce_check_result_set_stderr(struct sce_check_result* v, const char* _stderr) |
| 140 | { |
| 141 | free(v->std_err); |
| 142 | v->std_err = oscap_strdup(_stderr); |
| 143 | } |
| 144 | |
| 145 | const char* sce_check_result_get_stderr(struct sce_check_result* v) |
| 146 | { |
| 147 | return v->std_err; |
| 148 | } |
| 149 | |
| 150 | void sce_check_result_set_exit_code(struct sce_check_result* v, int exit_code) |
| 151 | { |
| 152 | v->exit_code = exit_code; |
| 153 | } |
| 154 | |
| 155 | int sce_check_result_get_exit_code(struct sce_check_result* v) |
| 156 | { |
| 157 | return v->exit_code; |
| 158 | } |
| 159 | |
| 160 | void sce_check_result_reset_environment_variables(struct sce_check_result* v) |
| 161 | { |
| 162 | oscap_stringlist_free(v->environment_variables); |
| 163 | v->environment_variables = oscap_stringlist_new(); |
| 164 | } |
| 165 | |
| 166 | void sce_check_result_add_environment_variable(struct sce_check_result* v, const char* var) |
| 167 | { |
| 168 | oscap_stringlist_add_string(v->environment_variables, var); |
| 169 | } |
| 170 | |
| 171 | void sce_check_result_set_xccdf_result(struct sce_check_result* v, xccdf_test_result_type_t result) |
| 172 | { |
| 173 | v->xccdf_result = result; |
| 174 | } |
| 175 | |
| 176 | xccdf_test_result_type_t sce_check_result_get_xccdf_result(struct sce_check_result* v) |
| 177 | { |
| 178 | return v->xccdf_result; |
| 179 | } |
| 180 | |
| 181 | void sce_check_result_export(struct sce_check_result* v, const char* target_file) |
| 182 | { |
| 183 | FILE* f = fopen(target_file, "w"); |
| 184 | if (!f) |
| 185 | { |
| 186 | oscap_seterr(OSCAP_EFAMILY_SCE, "Can't open file '%s' for writing.", target_file); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); |
| 191 | fprintf(f, "<sceres:sce_results xmlns:sceres=\"http://open-scap.org/page/SCE_result_file\" script-path=\"%s\">\n", sce_check_result_get_basename(v)); |
| 192 | fprintf(f, "\t<sceres:environment>\n"); |
| 193 | struct oscap_string_iterator* it = oscap_stringlist_get_strings(v->environment_variables); |
| 194 | while (oscap_string_iterator_has_more(it)) |
| 195 | { |
| 196 | const char* env = oscap_string_iterator_next(it); |
| 197 | fprintf(f, "\t\t<sceres:entry><![CDATA[%s]]></sceres:entry>\n", env); |
| 198 | } |
| 199 | oscap_string_iterator_free(it); |
| 200 | fprintf(f, "\t</sceres:environment>\n"); |
| 201 | fprintf(f, "\t<sceres:stdout><![CDATA[\n"); |
| 202 | size_t ret = fwrite(v->std_out, 1, strlen(v->std_out), f); |
| 203 | if (ret < strlen(v->std_out)) |
| 204 | oscap_seterr(OSCAP_EFAMILY_SCE, "Failed to write stdout result to %s.\n", target_file); |
| 205 | fprintf(f, "\t]]></sceres:stdout>\n"); |
| 206 | fprintf(f, "\t<sceres:stderr><![CDATA[\n"); |
| 207 | ret = fwrite(v->std_err, 1, strlen(v->std_err), f); |
| 208 | if (ret < strlen(v->std_err)) |
| 209 | oscap_seterr(OSCAP_EFAMILY_SCE, "Failed to write stderr result to %s.\n", target_file); |
| 210 | fprintf(f, "\t]]></sceres:stderr>\n"); |
| 211 | fprintf(f, "\t<sceres:exit_code>%i</sceres:exit_code>\n", sce_check_result_get_exit_code(v)); |
| 212 | fprintf(f, "\t<sceres:result>%s</sceres:result>\n", xccdf_test_result_type_get_text(sce_check_result_get_xccdf_result(v))); |
| 213 | fprintf(f, "</sceres:sce_results>\n"); |
| 214 | fclose(f); |
| 215 | } |
| 216 | |
| 217 | struct sce_session |
| 218 | { |
| 219 | struct oscap_list* results; |
| 220 | }; |
| 221 | |
| 222 | struct sce_session* sce_session_new(void) |
| 223 | { |
| 224 | struct sce_session* ret = malloc(sizeof(struct sce_session)); |
| 225 | ret->results = oscap_list_new(); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | void sce_session_free(struct sce_session* s) |
| 231 | { |
| 232 | if (!s) |
| 233 | return; |
| 234 | |
| 235 | oscap_list_free(s->results, (oscap_destruct_func) sce_check_result_free); |
| 236 | free(s); |
| 237 | } |
| 238 | |
| 239 | void sce_session_reset(struct sce_session* s) |
| 240 | { |
| 241 | oscap_list_free(s->results, (oscap_destruct_func) sce_check_result_free); |
| 242 | s->results = oscap_list_new(); |
| 243 | } |
| 244 | |
| 245 | void sce_session_add_check_result(struct sce_session* s, struct sce_check_result* result) |
| 246 | { |
| 247 | oscap_list_push(s->results, result); |
| 248 | } |
| 249 | |
| 250 | OSCAP_ITERATOR_GEN(sce_check_result) |
| 251 | |
| 252 | struct sce_check_result_iterator *sce_session_get_check_results(struct sce_session* s) |
| 253 | { |
| 254 | return oscap_iterator_new(s->results); |
| 255 | } |
| 256 | |
| 257 | void sce_session_export_to_directory(struct sce_session* s, const char* directory) |
| 258 | { |
| 259 | struct sce_check_result_iterator * it = sce_session_get_check_results(s); |
| 260 | |
| 261 | while(sce_check_result_iterator_has_more(it)) |
| 262 | { |
| 263 | struct sce_check_result * result = sce_check_result_iterator_next(it); |
| 264 | char* target = oscap_sprintf("%s/%s.result.xml", directory, sce_check_result_get_basename(result)); |
| 265 | sce_check_result_export(result, target); |
| 266 | free(target); |
| 267 | } |
| 268 | |
| 269 | sce_check_result_iterator_free(it); |
| 270 | } |
| 271 | |
| 272 | struct sce_parameters |
| 273 | { |
| 274 | char* xccdf_directory; |
| 275 | struct sce_session* session; |
| 276 | }; |
| 277 | |
| 278 | struct sce_parameters* sce_parameters_new(void) |
| 279 | { |
| 280 | struct sce_parameters *ret = malloc(sizeof(struct sce_parameters)); |
| 281 | ret->xccdf_directory = NULL; |
| 282 | ret->session = NULL; |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | void sce_parameters_free(struct sce_parameters* v) |
| 288 | { |
| 289 | if (!v) |
| 290 | return; |
| 291 | |
| 292 | free(v->xccdf_directory); |
| 293 | sce_session_free(v->session); |
| 294 | |
| 295 | free(v); |
| 296 | } |
| 297 | |
| 298 | void sce_parameters_set_xccdf_directory(struct sce_parameters* v, const char* value) |
| 299 | { |
| 300 | if (v->xccdf_directory) |
| 301 | free(v->xccdf_directory); |
| 302 | |
| 303 | v->xccdf_directory = oscap_strdup(value); |
| 304 | } |
| 305 | |
| 306 | const char* sce_parameters_get_xccdf_directory(struct sce_parameters* v) |
| 307 | { |
| 308 | return v->xccdf_directory; |
| 309 | } |
| 310 | |
| 311 | void sce_parameters_set_session(struct sce_parameters* v, struct sce_session* value) |
| 312 | { |
| 313 | sce_session_free(v->session); |
| 314 | v->session = value; |
| 315 | } |
| 316 | |
| 317 | struct sce_session* sce_parameters_get_session(struct sce_parameters* v) |
| 318 | { |
| 319 | return v->session; |
| 320 | } |
| 321 | |
| 322 | void sce_parameters_allocate_session(struct sce_parameters* v) |
| 323 | { |
| 324 | sce_parameters_set_session(v, sce_session_new()); |
| 325 | } |
| 326 | |
| 327 | static void _pipe_try_read_into_string(int fd, struct oscap_string *string, bool *eof) |
| 328 | { |
| 329 | // FIXME: Read by larger chunks in the future |
| 330 | char readbuf; |
| 331 | while (true) { |
| 332 | const int read_status = read(fd, &readbuf, 1); |
| 333 | if (read_status == 1) { // successful read |
| 334 | if (readbuf == '&') { |
| 335 | // & is a special case, we have to "escape" it manually |
| 336 | // (all else will eventually get handled by libxml) |
| 337 | oscap_string_append_string(string, "&"); |
| 338 | } else { |
| 339 | oscap_string_append_char(string, readbuf); |
| 340 | } |
| 341 | } |
| 342 | else if (read_status == 0) { // EOF |
| 343 | *eof = true; |
| 344 | break; |
| 345 | } |
| 346 | else { |
| 347 | if (errno == EAGAIN) { |
| 348 | // NOOP, we are waiting for more input |
| 349 | break; |
| 350 | } |
| 351 | else { |
| 352 | *eof = true; // signal EOF to exit the loops |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | |
| 360 | static void free_env_values(char **env_values, size_t index_of_first_env_value_not_compiled_in, size_t real_env_values_count) { |
| 361 | for (size_t i = index_of_first_env_value_not_compiled_in; i < real_env_values_count; i++) { |
| 362 | free(env_values[i]); |
| 363 | } |
| 364 | free(env_values); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const char *rule_id, const char *id, const char *href, |
| 369 | struct xccdf_value_binding_iterator *value_binding_it, |
| 370 | struct xccdf_check_import_iterator *check_import_it, |
| 371 | void *usr) |
| 372 | { |
| 373 | dI("Executing SCE check '%s'", href); |
| 374 | struct sce_parameters* parameters = (struct sce_parameters*)usr; |
| 375 | const char* xccdf_directory = parameters->xccdf_directory; |
| 376 | bool use_sce_wrapper = false; // use osca-run-sce-script ? |
| 377 | |
| 378 | char* tmp_href = oscap_sprintf("%s/%s", xccdf_directory, href); |
| 379 | |
| 380 | if (access(tmp_href, F_OK)) |
| 381 | { |
| 382 | // we only do this check to provide helpful error message |
| 383 | // there is an inherent race condition, the file might |
| 384 | // not exist anymore at the time we execve it! |
| 385 | |
| 386 | // the script hasn't been found, perhaps another sce instance |
| 387 | // with a different XCCDF directory can find it? |
| 388 | oscap_seterr(OSCAP_EFAMILY_SCE, "SCE couldn't find script file '%s'. " |
| 389 | "Expected location: '%s'.", href, tmp_href); |
| 390 | free(tmp_href); |
| 391 | return XCCDF_RESULT_NOT_CHECKED; |
| 392 | } |
| 393 | |
| 394 | if (access(tmp_href, F_OK | X_OK)) |
| 395 | { |
| 396 | // use the sce wrapper if it's not possible to acquire +x rights |
| 397 | use_sce_wrapper = true; |
| 398 | dI("%s isn't executable, oscap-run-sce-script will be used.", tmp_href); |
| 399 | } |
| 400 | |
| 401 | // all the result codes are shifted by 100, because otherwise syntax errors in scripts |
| 402 | // or even their nonexistence would cause XCCDF_RESULT_PASS to be the result |
| 403 | |
| 404 | char* argvp[3] = { |
| 405 | tmp_href, |
| 406 | tmp_href, // the second tmp_href is added in case we use the wrapper (oscap-run-sce-script) |
| 407 | NULL // which need the path of the script to eval as first parameter. |
| 408 | }; |
| 409 | |
| 410 | // bound values in KEY=VALUE form, ready to be passed as environment variables |
| 411 | char ** env_values = malloc(10 * sizeof(char * )); |
| 412 | size_t env_value_count = 10; |
| 413 | const size_t index_of_first_env_value_not_compiled_in = 10; |
| 414 | |
| 415 | env_values[0] = "PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin"; |
| 416 | |
| 417 | env_values[1] = "XCCDF_RESULT_PASS=101"; |
| 418 | env_values[2] = "XCCDF_RESULT_FAIL=102"; |
| 419 | env_values[3] = "XCCDF_RESULT_ERROR=103"; |
| 420 | env_values[4] = "XCCDF_RESULT_UNKNOWN=104"; |
| 421 | env_values[5] = "XCCDF_RESULT_NOT_APPLICABLE=105"; |
| 422 | env_values[6] = "XCCDF_RESULT_NOT_CHECKED=106"; |
| 423 | env_values[7] = "XCCDF_RESULT_NOT_SELECTED=107"; |
| 424 | env_values[8] = "XCCDF_RESULT_INFORMATIONAL=108"; |
| 425 | env_values[9] = "XCCDF_RESULT_FIXED=109"; |
| 426 | |
| 427 | while (xccdf_value_binding_iterator_has_more(value_binding_it)) |
| 428 | { |
| 429 | struct xccdf_value_binding* binding = xccdf_value_binding_iterator_next(value_binding_it); |
| 430 | |
| 431 | void *new_env_values = realloc(env_values, (env_value_count + 3) * sizeof(char *)); |
| 432 | if (new_env_values == NULL) { |
| 433 | dE("Unable to re-allocate memory"); |
| 434 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 435 | return XCCDF_RESULT_ERROR; |
| 436 | } |
| 437 | env_values = new_env_values; |
| 438 | |
| 439 | char* name = xccdf_value_binding_get_name(binding); |
| 440 | xccdf_value_type_t type = xccdf_value_binding_get_type(binding); |
| 441 | char* value = xccdf_value_binding_get_setvalue(binding); |
| 442 | if (value == NULL) |
| 443 | { |
| 444 | value = xccdf_value_binding_get_value(binding); |
| 445 | if (value == NULL) { |
| 446 | value = ""; |
| 447 | } |
| 448 | } |
| 449 | xccdf_operator_t operator = xccdf_value_binding_get_operator(binding); |
| 450 | |
| 451 | char* type_str; |
| 452 | switch (type) |
| 453 | { |
| 454 | case XCCDF_TYPE_BOOLEAN: |
| 455 | type_str = "BOOLEAN"; |
| 456 | break; |
| 457 | case XCCDF_TYPE_NUMBER: |
| 458 | type_str = "NUMBER"; |
| 459 | break; |
| 460 | case XCCDF_TYPE_STRING: |
| 461 | type_str = "STRING"; |
| 462 | break; |
| 463 | default: |
| 464 | assert(0); |
| 465 | type_str = NULL; |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | char* env_type_entry = oscap_sprintf("XCCDF_TYPE_%s=%s", name, type_str); |
| 470 | char* env_value_entry = oscap_sprintf("XCCDF_VALUE_%s=%s", name, value); |
| 471 | |
| 472 | char* operator_str; |
| 473 | switch (operator) |
| 474 | { |
| 475 | case XCCDF_OPERATOR_EQUALS: |
| 476 | operator_str = "EQUALS"; |
| 477 | break; |
| 478 | case XCCDF_OPERATOR_NOT_EQUAL: |
| 479 | operator_str = "NOT_EQUAL"; |
| 480 | break; |
| 481 | case XCCDF_OPERATOR_GREATER: |
| 482 | operator_str = "GREATER"; |
| 483 | break; |
| 484 | case XCCDF_OPERATOR_GREATER_EQUAL: |
| 485 | operator_str = "GREATER_EQUAL"; |
| 486 | break; |
| 487 | case XCCDF_OPERATOR_LESS: |
| 488 | operator_str = "LESS"; |
| 489 | break; |
| 490 | case XCCDF_OPERATOR_LESS_EQUAL: |
| 491 | operator_str = "LESS_EQUAL"; |
| 492 | break; |
| 493 | case XCCDF_OPERATOR_PATTERN_MATCH: |
| 494 | operator_str = "PATTERN_MATCH"; |
| 495 | break; |
| 496 | default: |
| 497 | assert(0); |
| 498 | operator_str = NULL; |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | char* env_operator_entry = oscap_sprintf("XCCDF_OPERATOR_%s=%s", name, operator_str); |
| 503 | |
| 504 | env_values[env_value_count] = env_type_entry; |
| 505 | env_value_count++; |
| 506 | env_values[env_value_count] = env_value_entry; |
| 507 | env_value_count++; |
| 508 | env_values[env_value_count] = env_operator_entry; |
| 509 | env_value_count++; |
| 510 | } |
| 511 | |
| 512 | void *new_env_values = realloc(env_values, (env_value_count + 1) * sizeof(char*)); |
| 513 | if (new_env_values == NULL) { |
| 514 | dE("Unable to re-allocate memory"); |
| 515 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 516 | return XCCDF_RESULT_ERROR; |
| 517 | } |
| 518 | env_values = new_env_values; |
| 519 | env_values[env_value_count] = NULL; |
| 520 | |
| 521 | // We open a pipe for communication with the forked process |
| 522 | int stdout_pipefd[2]; |
| 523 | int stderr_pipefd[2]; |
| 524 | if (pipe(stdout_pipefd) == -1 || pipe(stderr_pipefd) == -1) |
| 525 | { |
| 526 | dE("Error in pipe"); |
| 527 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 528 | return XCCDF_RESULT_ERROR; |
| 529 | } |
| 530 | |
| 531 | // FIXME: We definitely want to impose security restrictions in the forked child process in the future. |
| 532 | // This would prevent scripts from writing to files or deleting them. |
| 533 | |
| 534 | int fork_result = fork(); |
| 535 | if (fork_result >= 0) |
| 536 | { |
| 537 | // fork successful |
| 538 | |
| 539 | if (fork_result == 0) |
| 540 | { |
| 541 | // we won't read from the pipes, so close the reading fd |
| 542 | close(stdout_pipefd[0]); |
| 543 | close(stderr_pipefd[0]); |
| 544 | |
| 545 | // forward stdout and stderr to our custom opened pipes |
| 546 | dup2(stdout_pipefd[1], fileno(stdout)); |
| 547 | dup2(stderr_pipefd[1], fileno(stderr)); |
| 548 | |
| 549 | // we duplicated the file descriptors twice, we can close the original |
| 550 | // ones now, stdout and stderr will be closed properly after the execved |
| 551 | // script/executable finishes |
| 552 | close(stdout_pipefd[1]); |
| 553 | close(stderr_pipefd[1]); |
| 554 | |
| 555 | // before we execute the script, lets make sure we get SIGTERM when |
| 556 | // oscap is killed, crashes or otherwise terminates |
| 557 | #if defined(PR_SET_PDEATHSIG) |
| 558 | // requires Linux 2.1.57 or later |
| 559 | prctl(PR_SET_PDEATHSIG, SIGTERM); |
| 560 | #elif defined(OS_FREEBSD) |
| 561 | int sig = SIGTERM; |
| 562 | procctl(P_PID, getpid(), PROC_PDEATHSIG_CTL, &sig); |
| 563 | #else |
| 564 | // TODO: Please provide alternatives |
| 565 | #endif |
| 566 | |
| 567 | // we are the child process |
| 568 | if(use_sce_wrapper) { |
| 569 | #if defined(OS_FREEBSD) |
| 570 | size_t k; |
| 571 | |
| 572 | // Setup environment beforehand as FreeBSD does not have execvpe() |
| 573 | for (k = 0; k < env_value_count; k++) { |
| 574 | putenv(env_values[k]); |
| 575 | } |
| 576 | |
| 577 | execvp("oscap-run-sce-script", argvp); |
| 578 | #else |
| 579 | execvpe("oscap-run-sce-script", argvp, env_values); |
| 580 | #endif |
| 581 | } else { |
| 582 | execve(tmp_href, argvp, env_values); |
| 583 | } |
| 584 | |
| 585 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 586 | |
| 587 | // no need to check the return value of execve, if it returned at all we are in trouble |
| 588 | printf("Unexpected error when executing script '%s'. Error message follows.\n", href); |
| 589 | perror("execve"); |
| 590 | |
| 591 | // the parent process considers us a script check, we have to return a value that will mean XCCDF_RESULT_ERROR |
| 592 | exit(103); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | // we won't write to the pipes, so close the writing fd |
| 597 | close(stdout_pipefd[1]); |
| 598 | close(stderr_pipefd[1]); |
| 599 | |
| 600 | const int flag_stdout = fcntl(stdout_pipefd[0], F_GETFL, 0); |
| 601 | if (flag_stdout == -1) { |
| 602 | oscap_seterr(OSCAP_EFAMILY_SCE, "Failed to obtain status of stdout pipe: %s", |
| 603 | strerror(errno)); |
| 604 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 605 | return XCCDF_RESULT_ERROR; |
| 606 | } |
| 607 | int retval = fcntl(stdout_pipefd[0], F_SETFL, flag_stdout | O_NONBLOCK); |
| 608 | if (retval == -1) { |
| 609 | oscap_seterr(OSCAP_EFAMILY_SCE, |
| 610 | "Failed to set nonblocking flag on stdout pipe: %s", |
| 611 | strerror(errno)); |
| 612 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 613 | return XCCDF_RESULT_ERROR; |
| 614 | } |
| 615 | |
| 616 | const int flag_stderr = fcntl(stderr_pipefd[0], F_GETFL, 0); |
| 617 | if (flag_stderr == -1) { |
| 618 | oscap_seterr(OSCAP_EFAMILY_SCE, |
| 619 | "Failed to obtain status of stderr pipe: %s", |
| 620 | strerror(errno)); |
| 621 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 622 | return XCCDF_RESULT_ERROR; |
| 623 | } |
| 624 | retval = fcntl(stderr_pipefd[0], F_SETFL, flag_stderr | O_NONBLOCK); |
| 625 | if (retval == -1) { |
| 626 | oscap_seterr(OSCAP_EFAMILY_SCE, |
| 627 | "Failed to set nonblocking flag on stderr pipe: %s", |
| 628 | strerror(errno)); |
| 629 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 630 | return XCCDF_RESULT_ERROR; |
| 631 | } |
| 632 | |
| 633 | // we have to read from both pipes at the same time to avoid stalling |
| 634 | struct oscap_string *stdout_string = oscap_string_new(); |
| 635 | struct oscap_string *stderr_string = oscap_string_new(); |
| 636 | |
| 637 | bool stdout_eof = false; |
| 638 | bool stderr_eof = false; |
| 639 | |
| 640 | while (!stdout_eof || !stderr_eof) { |
| 641 | if (!stdout_eof) |
| 642 | _pipe_try_read_into_string(stdout_pipefd[0], stdout_string, &stdout_eof); |
| 643 | |
| 644 | if (!stderr_eof) |
| 645 | _pipe_try_read_into_string(stderr_pipefd[0], stderr_string, &stderr_eof); |
| 646 | |
| 647 | // sleep for 10ms to avoid wasting CPU |
| 648 | usleep(10 * 1000); |
| 649 | } |
| 650 | |
| 651 | close(stdout_pipefd[0]); |
| 652 | close(stderr_pipefd[0]); |
| 653 | |
| 654 | char *stdout_buffer = oscap_string_bequeath(stdout_string); |
| 655 | char *stderr_buffer = oscap_string_bequeath(stderr_string); |
| 656 | |
| 657 | // we are the parent process |
| 658 | int wstatus; |
| 659 | waitpid(fork_result, &wstatus, 0); |
| 660 | |
| 661 | // we subtract 100 here to shift the exit code to xccdf_test_result_type_t enum range |
| 662 | int raw_result = WEXITSTATUS(wstatus) - 100; |
| 663 | if (raw_result <= 0 || raw_result > XCCDF_RESULT_FIXED) |
| 664 | { |
| 665 | // the script returned invalid exit code, we need to safeguard us against that |
| 666 | raw_result = XCCDF_RESULT_ERROR; |
| 667 | } |
| 668 | |
| 669 | struct sce_session* session = sce_parameters_get_session(parameters); |
| 670 | if (session) |
| 671 | { |
| 672 | struct sce_check_result* check_result = sce_check_result_new(); |
| 673 | sce_check_result_set_href(check_result, tmp_href); |
| 674 | char *base_name = oscap_basename(tmp_href); |
| 675 | sce_check_result_set_basename(check_result, base_name); |
| 676 | free(base_name); |
| 677 | sce_check_result_set_stdout(check_result, stdout_buffer); |
| 678 | sce_check_result_set_stderr(check_result, stderr_buffer); |
| 679 | sce_check_result_set_exit_code(check_result, WEXITSTATUS(wstatus)); |
| 680 | sce_check_result_set_xccdf_result(check_result, (xccdf_test_result_type_t)raw_result); |
| 681 | |
| 682 | for (size_t i = 0; i < env_value_count; ++i) |
| 683 | { |
| 684 | sce_check_result_add_environment_variable(check_result, env_values[i]); |
| 685 | } |
| 686 | |
| 687 | sce_session_add_check_result(session, check_result); |
| 688 | } |
| 689 | |
| 690 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 691 | |
| 692 | // lets interpret the check imports passed to us |
| 693 | xccdf_check_import_iterator_reset(check_import_it); |
| 694 | while (xccdf_check_import_iterator_has_more(check_import_it)) |
| 695 | { |
| 696 | struct xccdf_check_import * check_import = xccdf_check_import_iterator_next(check_import_it); |
| 697 | const char *name = xccdf_check_import_get_name(check_import); |
| 698 | |
| 699 | if (strcmp(name, "stdout") == 0) |
| 700 | { |
| 701 | xccdf_check_import_set_content(check_import, stdout_buffer); |
| 702 | } |
| 703 | else if (strcmp(name, "stderr") == 0) |
| 704 | { |
| 705 | xccdf_check_import_set_content(check_import, stderr_buffer); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | free(tmp_href); |
| 710 | free(stdout_buffer); |
| 711 | free(stderr_buffer); |
| 712 | |
| 713 | return (xccdf_test_result_type_t)raw_result; |
| 714 | } |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count); |
| 719 | |
| 720 | close(stdout_pipefd[0]); |
| 721 | close(stdout_pipefd[1]); |
| 722 | close(stderr_pipefd[0]); |
| 723 | close(stderr_pipefd[1]); |
| 724 | return XCCDF_RESULT_ERROR; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | bool xccdf_policy_model_register_engine_sce(struct xccdf_policy_model * model, struct sce_parameters *parameters) |
| 729 | { |
| 730 | return xccdf_policy_model_register_engine_and_query_callback(model, |
| 731 | "http://open-scap.org/page/SCE", sce_engine_eval_rule, (void*)parameters, NULL); |
| 732 | } |