Skip to content

Commit c987667

Browse files
committed
Modified test suite to make use of setcap-blessed oscap binary.
Some offline-mode tests require chroot syscalls during execution. The build system and test suite are now aware of this and support build/usage of oscap buddy binary that is oscap with setcap chroot blessing.
1 parent 007de74 commit c987667

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

docs/developer/developer.adoc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,21 @@ It's also possible to use `ctest` to test any other oscap binary present in the
169169
$ export CUSTOM_OSCAP=/usr/bin/oscap; ctest
170170
----
171171

172-
Not every check tests the oscap tool, however, when the CUSTOM_OSCAP variable is set, only the checks which do are executed.
172+
Some tests that use the so-called offline mode of probes need to chroot during the test execution.
173+
Some of those probes use the chroot syscall, which an unprivileged process is not allowed to do.
174+
This is not a problem during the scanning itself, as oscap is usually scanning as root.
175+
However, we don't want to run oscap as root during tests, as the whole test suite would have to use root privileges to clean up.
176+
177+
Instead, build the `oscap-chrootable` target as superuser.
178+
This target creates the chroot-enabled binary that the test suite will use for some of those offline tests.
179+
Internally, the binary is stored under `OSCAP_CHROOTABLE_EXEC` variable, and the invocation suitable for tests can be done using by unquoted expansion of the `OSCAP_CHROOTABLE` variable.
180+
Therefore, it is recommended to run
181+
182+
----
183+
$ sudo make oscap-chrootable
184+
----
185+
186+
Not every check tests the oscap tool, however, when the `CUSTOM_OSCAP` variable is set, only the checks which do are executed.
173187

174188
To enable the MITRE tests, use the `ENABLE_MITRE` flag:
175189

tests/probes/symlink/test_offline_mode_symlink.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ function test_offline_mode_symlink {
4545

4646
bash ${srcdir}/test_offline_mode_symlink.xml.sh "" > "$DF"
4747
export OSCAP_PROBE_ROOT="$tmpdir"
48-
$OSCAP oval eval --results $RF $DF
48+
if test -x "$OSCAP_CHROOTABLE_EXEC"; then
49+
$OSCAP_CHROOTABLE oval eval --results $RF $DF
50+
else
51+
echo "Skipping test '${FUNCNAME[0]}' as '$OSCAP_CHROOTABLE_EXEC' oscap with chroot capability doesn't exist."
52+
return
53+
fi
4954

5055
result=$RF
5156

tests/test_common.sh.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ if [ -z ${CUSTOM_OSCAP+x} ] ; then
2929
else
3030
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP="bash @CMAKE_BINARY_DIR@/run @CMAKE_BINARY_DIR@/utils/oscap"
3131
fi
32+
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP_CHROOTABLE_EXEC="@CMAKE_BINARY_DIR@/utils/oscap-chrootable"
33+
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP_CHROOTABLE="bash @CMAKE_BINARY_DIR@/run $OSCAP_CHROOTABLE_EXEC"
3234
else
3335
export OSCAP=${CUSTOM_OSCAP}
3436
fi

utils/CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ if(ENABLE_OSCAP_UTIL)
3333
DESTINATION "${CMAKE_INSTALL_MANDIR}/man8"
3434
)
3535
endif()
36+
37+
add_custom_target(oscap-chrootable-nocap
38+
COMMAND cp oscap oscap-chrootable
39+
COMMENT "Copying oscap binary to a buddy binary that awaits chroot blessing by setcap"
40+
DEPENDS oscap
41+
)
42+
43+
add_custom_target(oscap-chrootable
44+
COMMAND setcap cap_sys_chroot+ep oscap-chrootable
45+
COMMENT "Generating chroot-capable oscap buddy"
46+
DEPENDS oscap-chrootable
47+
)
3648
endif()
3749
if(ENABLE_OSCAP_UTIL_CHROOT)
3850
install(PROGRAMS "oscap-chroot"
@@ -49,15 +61,15 @@ if(ENABLE_OSCAP_UTIL_DOCKER)
4961
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
5062
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
5163
)
52-
64+
5365
if(NOT PYTHON_SITE_PACKAGES_INSTALL_DIR)
5466
execute_process(COMMAND
5567
${OSCAP_DOCKER_PYTHON} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(False, False, prefix='${CMAKE_INSTALL_PREFIX}'))"
5668
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES_INSTALL_DIR
5769
OUTPUT_STRIP_TRAILING_WHITESPACE
5870
)
5971
endif()
60-
72+
6173
install(DIRECTORY oscap_docker_python
6274
DESTINATION ${PYTHON_SITE_PACKAGES_INSTALL_DIR}
6375
)

0 commit comments

Comments
 (0)