diff --git a/CMakeLists.txt b/CMakeLists.txt index 61c57d7a3e..a163a1fe57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,6 +105,7 @@ find_package(DBUS) find_package(Doxygen) find_package(GConf) find_package(Ldap) +find_package(Libmagic REQUIRED) find_package(OpenDbx) find_package(PCRE REQUIRED) find_package(PerlLibs) diff --git a/cmake/FindLibmagic.cmake b/cmake/FindLibmagic.cmake new file mode 100644 index 0000000000..426cae9d14 --- /dev/null +++ b/cmake/FindLibmagic.cmake @@ -0,0 +1,29 @@ +# - Try to find libmagic +# Once done, this will define +# +# LIBMAGIC_FOUND - system has libmagic +# LIBMAGIC_INCLUDE_DIRS - the libmagic include directories +# LIBMAGIC_LIBRARIES - link these to use libmagic + +include(LibFindMacros) + +# Use pkg-config to get hints about paths +libfind_pkg_check_modules(LIBMAGIC_PKGCONF magic-0.1) + +# Include dir +find_path(LIBMAGIC_INCLUDE_DIR + NAMES magic.h + PATHS ${LIBMAGIC_PKGCONF_INCLUDE_DIRS} +) + +# Finally the library itself +find_library(LIBMAGIC_LIBRARY + NAMES libmagic.so + PATHS ${LIBMAGIC_PKGCONF_LIBRARY_DIRS} +) + +# Set the include dir variables and the libraries and let libfind_process do the rest. +# NOTE: Singular variables for this library, plural for libraries this this lib depends on. +set(LIBMAGIC_PROCESS_INCLUDES LIBMAGIC_INCLUDE_DIR) +set(LIBMAGIC_PROCESS_LIBS LIBMAGIC_LIBRARY) +libfind_process(LIBMAGIC) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e9339c62ed..718d1f1a96 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -81,7 +81,7 @@ set_target_properties(openscap PROPERTIES C_VISIBILITY_PRESET hidden ) -target_link_libraries(openscap ${LIBXML2_LIBRARIES} ${LIBXSLT_LIBRARIES} ${XMLSEC_LIBRARIES} ${OPENSSL_LIBRARIES} ${LIBXSLT_EXSLT_LIBRARIES} ${PCRE_LIBRARIES} ${CURL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(openscap ${LIBXML2_LIBRARIES} ${LIBXSLT_LIBRARIES} ${XMLSEC_LIBRARIES} ${OPENSSL_LIBRARIES} ${LIBXSLT_EXSLT_LIBRARIES} ${PCRE_LIBRARIES} ${CURL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${LIBMAGIC_LIBRARIES}) if (BZIP2_FOUND) target_link_libraries(openscap ${BZIP2_LIBRARIES}) endif() diff --git a/src/OVAL/probes/independent/textfilecontent54_probe.c b/src/OVAL/probes/independent/textfilecontent54_probe.c index 322977418c..51d6f23510 100644 --- a/src/OVAL/probes/independent/textfilecontent54_probe.c +++ b/src/OVAL/probes/independent/textfilecontent54_probe.c @@ -44,6 +44,7 @@ #include #include #include +#include #include "_seap.h" #include @@ -118,6 +119,22 @@ struct pfdata { pcre *compiled_regex; }; +static bool is_text_file(const char *filepath) +{ + bool result = false; + magic_t cookie = magic_open(MAGIC_MIME); + if (cookie == NULL) + goto cleanup; + if (magic_load(cookie, NULL) != 0) + goto cleanup; + const char *magic_full = magic_file(cookie, filepath); + if (oscap_str_startswith(magic_full, "text/")) + result = true; +cleanup: + magic_close(cookie); + return result; +} + static int process_file(const char *prefix, const char *path, const char *file, void *arg, oval_schema_version_t over) { struct pfdata *pfd = (struct pfdata *) arg; @@ -159,6 +176,9 @@ static int process_file(const char *prefix, const char *path, const char *file, if (!S_ISREG(st.st_mode)) goto cleanup; + if (!is_text_file(whole_path_with_prefix)) + goto cleanup; + fd = open(whole_path_with_prefix, O_RDONLY); if (fd == -1) { SEXP_t *msg; diff --git a/tests/probes/textfilecontent54/CMakeLists.txt b/tests/probes/textfilecontent54/CMakeLists.txt index 9577016ba1..01e5939389 100644 --- a/tests/probes/textfilecontent54/CMakeLists.txt +++ b/tests/probes/textfilecontent54/CMakeLists.txt @@ -1,5 +1,6 @@ if(ENABLE_PROBES_INDEPENDENT) add_oscap_test("test_behavior_multiline.sh") + add_oscap_test("test_binary_file.sh") add_oscap_test("test_filecontent_non_utf.sh") add_oscap_test("test_offline_mode_textfilecontent54.sh") add_oscap_test("test_probes_textfilecontent54.sh") diff --git a/tests/probes/textfilecontent54/binary_file b/tests/probes/textfilecontent54/binary_file new file mode 100644 index 0000000000..b6e0fe8aa7 --- /dev/null +++ b/tests/probes/textfilecontent54/binary_file @@ -0,0 +1 @@ +4Ä‹Ñ H¦ÃÚWÆuw4^Sý€…¶˜úy¿Ê÷upË~•¿QÌ_«úÇØ¿ ÈÈ †@\¦«¿Æžèa Ê–ÿ \ No newline at end of file diff --git a/tests/probes/textfilecontent54/test_binary_file.oval.xml b/tests/probes/textfilecontent54/test_binary_file.oval.xml new file mode 100755 index 0000000000..ed90eae747 --- /dev/null +++ b/tests/probes/textfilecontent54/test_binary_file.oval.xml @@ -0,0 +1,37 @@ + + + + 5.11.1 + 0001-01-01T00:00:00+00:00 + + + + + + A simple test OVAL for textfilecontent54 test. + x + + x + + + + + + + + + + + + + + + + + /tmp/binary_file + ^.*$ + 1 + + + + diff --git a/tests/probes/textfilecontent54/test_binary_file.sh b/tests/probes/textfilecontent54/test_binary_file.sh new file mode 100755 index 0000000000..cccdbe3197 --- /dev/null +++ b/tests/probes/textfilecontent54/test_binary_file.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -e +set -o pipefail +set -x + +# Regression test for https://bugzilla.redhat.com/show_bug.cgi?id=2033246 + +. $builddir/tests/test_common.sh + +result=$(mktemp) +stderr=$(mktemp) +cp "$srcdir/binary_file" /tmp/ + +$OSCAP oval eval --results "$result" "$srcdir/test_binary_file.oval.xml" + +# previous versions of OpenSCAP produce result="error" +assert_exists 1 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:1" and @result="true"]' +assert_exists 0 '/oval_results/results/system/definitions/definition[@definition_id="oval:x:def:1" and @result="error"]' +! grep "Function pcre_exec() failed to match a regular expression with return code -10 on string .*" $stderr + +rm -f "$result" +rm -f "$stderr" +rm -f /tmp/binary_file