Skip to content

Commit 7f23880

Browse files
wrapp sce scripts when tmp partition is noexec
1 parent 149785d commit 7f23880

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

src/SCE/sce_engine.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
362362
{
363363
struct sce_parameters* parameters = (struct sce_parameters*)usr;
364364
const char* xccdf_directory = parameters->xccdf_directory;
365+
bool use_sce_wrapper = false; // use osca-run-sce-script ?
365366

366367
char* tmp_href = oscap_sprintf("%s/%s", xccdf_directory, href);
367368

@@ -381,27 +382,26 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
381382

382383
if (access(tmp_href, F_OK | X_OK))
383384
{
384-
// again, only to provide helpful error message
385-
oscap_seterr(OSCAP_EFAMILY_SCE, "SCE has found script file '%s' at '%s' "
386-
"but it isn't executable!", href, tmp_href);
387-
free(tmp_href);
388-
return XCCDF_RESULT_ERROR;
385+
// use the sce wrapper if it's not possible to acquire +x rights
386+
use_sce_wrapper = true;
387+
dI("%s isn't executable, oscap-run-sce-script will be use.", tmp_href);
389388
}
390389

391390
// all the result codes are shifted by 100, because otherwise syntax errors in scripts
392391
// or even their nonexistence would cause XCCDF_RESULT_PASS to be the result
393392

394-
char* argvp[1 + 1] = {
393+
char* argvp[3] = {
394+
tmp_href,
395395
tmp_href,
396396
NULL
397397
};
398-
398+
dI("arvp[0] = %s", argvp[0]);
399399
// bound values in KEY=VALUE form, ready to be passed as environment variables
400400
char ** env_values = malloc(10 * sizeof(char * ));
401401
size_t env_value_count = 10;
402402
const size_t index_of_first_env_value_not_compiled_in = 10;
403403

404-
env_values[0] = "PATH=/bin:/sbin:/usr/bin:/usr/sbin";
404+
env_values[0] = "PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin";
405405

406406
env_values[1] = "XCCDF_RESULT_PASS=101";
407407
env_values[2] = "XCCDF_RESULT_FAIL=102";
@@ -491,7 +491,7 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
491491
env_values[env_value_count] = env_operator_entry;
492492
env_value_count++;
493493
}
494-
494+
dI("debug0 ..");
495495
env_values = realloc(env_values, (env_value_count + 1) * sizeof(char*));
496496
env_values[env_value_count] = NULL;
497497

@@ -508,27 +508,30 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
508508
// FIXME: We definitely want to impose security restrictions in the forked child process in the future.
509509
// This would prevent scripts from writing to files or deleting them.
510510

511+
dI("debug1 ..");
512+
511513
int fork_result = fork();
512514
if (fork_result >= 0)
513515
{
514516
// fork successful
515517

516518
if (fork_result == 0)
517519
{
520+
dI("debug2 ..");
518521
// we won't read from the pipes, so close the reading fd
519522
close(stdout_pipefd[0]);
520523
close(stderr_pipefd[0]);
521-
524+
dI("debug2.1 ..");
522525
// forward stdout and stderr to our custom opened pipes
523-
dup2(stdout_pipefd[1], fileno(stdout));
524-
dup2(stderr_pipefd[1], fileno(stderr));
525-
526+
//dup2(stdout_pipefd[1], fileno(stdout));
527+
//dup2(stderr_pipefd[1], fileno(stderr));
528+
dI("debug2.2 ..");
526529
// we duplicated the file descriptors twice, we can close the original
527530
// ones now, stdout and stderr will be closed properly after the execved
528531
// script/executable finishes
529532
close(stdout_pipefd[1]);
530533
close(stderr_pipefd[1]);
531-
534+
dI("debug3 ..");
532535
// before we execute the script, lets make sure we get SIGTERM when
533536
// oscap is killed, crashes or otherwise terminates
534537
#ifdef PR_SET_PDEATHSIG
@@ -539,7 +542,13 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
539542
#endif
540543

541544
// we are the child process
542-
execve(tmp_href, argvp, env_values);
545+
dI("argvp[0<] = %s", argvp[0]);
546+
dI("env<(0 %s", env_values[0]);
547+
548+
if(use_sce_wrapper)
549+
execvp("oscap-run-sce-script", argvp, env_values);
550+
else
551+
execve(tmp_href, argvp, env_values);
543552

544553
free_env_values(env_values, index_of_first_env_value_not_compiled_in, env_value_count);
545554

utils/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
install(PROGRAMS "oscap-run-sce-script"
2+
DESTINATION ${CMAKE_INSTALL_BINDIR}
3+
)
4+
15
if(ENABLE_OSCAP_UTIL)
26
file(GLOB UTILS_SOURCES "*.c")
37
if(HAVE_GETOPT_H)

utils/oscap-run-sce-script

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
#
3+
# Authors:
4+
# Dominique Blaze <[email protected]>
5+
#
6+
# use by oscap for evaluate a SCE file when +x rights are missing
7+
8+
if [ ! -z $1 ] && [ -f $1 ]
9+
then
10+
# file exists. first check if shebang is here
11+
12+
firstline=$(head -n1 $1)
13+
if [ ${firstline:0:2} = "#!" ]
14+
then # it's a shebang
15+
cmd=${firstline:2} # remove the begin (#!)
16+
cmd=${cmd##*( )} # trim whitespaces
17+
eval $cmd $1 > /dev/stdout
18+
else # no shebang, trying bash by default ...
19+
/usr/bin/env bash $1 > /dev/stdout
20+
fi
21+
22+
else
23+
echo "Script file not found: $1" > /dev/stderr
24+
fi

0 commit comments

Comments
 (0)