Skip to content

Commit dc98bc8

Browse files
authored
Merge pull request #1432 from matejak/oscap_setcap_awareness
Modified test suite to make use of setcap-blessed oscap binary.
2 parents 007de74 + d21b809 commit dc98bc8

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
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, or build `oscap-chrootable-nocap` first and then grant the capability manually.
178+
This target creates the binary that the test suite will use for some of those offline tests.
179+
In offline tests, use the `set_offline_test_mode [chroot directory]` and `unset_offline_test_mode` functions from the common test module - those will set variables in such way that the unquoted `$OSCAP` invocation will use the chroot-capable binary, or it will exit with an error code, aborting the test.
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/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
if(ENABLE_PROBES_UNIX)
22
add_oscap_test("all.sh")
33
add_oscap_test("test_offline_mode_symlink.sh")
4-
set_tests_properties("probes/symlink/test_offline_mode_symlink.sh" PROPERTIES WILL_FAIL true)
54
endif()

tests/probes/symlink/test_offline_mode_symlink.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ function test_offline_mode_symlink {
4444

4545

4646
bash ${srcdir}/test_offline_mode_symlink.xml.sh "" > "$DF"
47-
export OSCAP_PROBE_ROOT="$tmpdir"
47+
48+
set_chroot_offline_test_mode "$tmpdir"
49+
4850
$OSCAP oval eval --results $RF $DF
4951

52+
unset_chroot_offline_test_mode
53+
5054
result=$RF
5155

5256
rm -f $DF

tests/test_common.sh.in

Lines changed: 43 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
@@ -182,4 +184,45 @@ assert_exists() {
182184
return 1
183185
fi
184186
}
187+
188+
# $1: The chroot directory
189+
set_chroot_offline_test_mode() {
190+
if test -n "$_OSCAP_BEFORE"; then
191+
echo "Already in offline test mode!" >&2
192+
return
193+
fi
194+
if test -x "$OSCAP_CHROOTABLE_EXEC"; then
195+
if ! getcap "$OSCAP_CHROOTABLE_EXEC" | grep -q 'cap_sys_chroot+ep'; then
196+
echo "Skipping test '${FUNCNAME[1]}' as '$OSCAP_CHROOTABLE_EXEC' doesn't have the chroot capability." >&2
197+
return 255
198+
fi
199+
else
200+
echo "Skipping test '${FUNCNAME[1]}' as '$OSCAP_CHROOTABLE_EXEC' oscap which is supposed to have chroot capability doesn't exist." >&2
201+
return 255
202+
fi
203+
_OSCAP_BEFORE="$OSCAP"
204+
OSCAP="$OSCAP_CHROOTABLE"
205+
set_offline_chroot_dir "$1"
206+
return 0
207+
}
208+
209+
# $1: The chroot directory. If empty, unset the OSCAP_PROBE_ROOT variable
210+
set_offline_chroot_dir() {
211+
if test -n "$1"; then
212+
export OSCAP_PROBE_ROOT="$1"
213+
else
214+
unset OSCAP_PROBE_ROOT
215+
fi
216+
}
217+
218+
unset_chroot_offline_test_mode() {
219+
if ! test -n "$_OSCAP_BEFORE"; then
220+
echo "Not in the offline test mode!" >&2
221+
return
222+
fi
223+
OSCAP="$_OSCAP_BEFORE"
224+
set_offline_chroot_dir ""
225+
_OSCAP_BEFORE=
226+
}
227+
185228
export -f assert_exists

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)