tests: test uid_wrapper_syscall_{valid,va} interaction using swrap_fake_uid_wrapper.so
authorStefan Metzmacher <metze@samba.org>
Mon, 16 Jan 2023 20:57:35 +0000 (21:57 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 17 Jan 2023 16:53:44 +0000 (17:53 +0100)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
tests/CMakeLists.txt
tests/swrap_fake_uid_wrapper.c [new file with mode: 0644]
tests/swrap_fake_uid_wrapper.h [new file with mode: 0644]
tests/test_syscall_uwrap.c [new file with mode: 0644]

index 175b7cff81a5b717c653742bbcb69a717af99d25..6c3aae9026b2b33709f2e3162dc093daa1d37443 100644 (file)
@@ -32,6 +32,15 @@ target_link_libraries(${TORTURE_LIBRARY}
     ${SWRAP_REQUIRED_LIBRARIES}
     ${CMAKE_THREAD_LIBS_INIT})
 
+add_library(swrap_fake_uid_wrapper SHARED swrap_fake_uid_wrapper.c)
+target_compile_options(swrap_fake_uid_wrapper
+                       PRIVATE
+                           ${DEFAULT_C_COMPILE_FLAGS}
+                           -D_GNU_SOURCE)
+#target_include_directories(swrap_fake_uid_wrapper
+#        PRIVATE ${CMAKE_BINARY_DIR} ${CMOCKA_INCLUDE_DIR})
+set(SWRAP_FAKE_UID_WRAPPER_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swrap_fake_uid_wrapper${CMAKE_SHARED_LIBRARY_SUFFIX}")
+
 set(SWRAP_THREADED_TESTS
     test_thread_sockets
     test_thread_echo_tcp_connect
@@ -60,6 +69,7 @@ set(SWRAP_TESTS
     test_public_functions
     test_close_failure
     test_tcp_socket_overwrite
+    test_syscall_uwrap
     ${SWRAP_THREADED_TESTS})
 
 if (HAVE_STRUCT_MSGHDR_MSG_CONTROL)
@@ -85,6 +95,7 @@ function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME)
     if (ASAN_LIBRARY)
         list(APPEND PRELOAD_LIBRARIES ${ASAN_LIBRARY})
     endif()
+    list(APPEND PRELOAD_LIBRARIES ${SWRAP_FAKE_UID_WRAPPER_LOCATION})
     list(APPEND PRELOAD_LIBRARIES ${SOCKET_WRAPPER_LOCATION})
 
     if (OSX)
diff --git a/tests/swrap_fake_uid_wrapper.c b/tests/swrap_fake_uid_wrapper.c
new file mode 100644 (file)
index 0000000..286d7ad
--- /dev/null
@@ -0,0 +1,44 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdbool.h>
+#include <string.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <errno.h>
+
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#endif
+
+#include "swrap_fake_uid_wrapper.h"
+
+/* simulate uid_wrapper hooks */
+bool uid_wrapper_syscall_valid(long int sysno)
+{
+       if (sysno == __FAKE_UID_WRAPPER_SYSCALL_NO) {
+               return true;
+       }
+
+       return false;
+}
+
+long int uid_wrapper_syscall_va(long int sysno, va_list va)
+{
+       (void) va; /* unused */
+
+       if (sysno == __FAKE_UID_WRAPPER_SYSCALL_NO) {
+               errno = 0;
+               return __FAKE_UID_WRAPPER_SYSCALL_RC;
+       }
+
+       errno = ENOSYS;
+       return -1;
+}
diff --git a/tests/swrap_fake_uid_wrapper.h b/tests/swrap_fake_uid_wrapper.h
new file mode 100644 (file)
index 0000000..70ac1d0
--- /dev/null
@@ -0,0 +1,7 @@
+#include <stdbool.h>
+
+/* simulate socket_wrapper hooks */
+#define __FAKE_UID_WRAPPER_SYSCALL_NO 123456789
+#define __FAKE_UID_WRAPPER_SYSCALL_RC 987654321
+bool uid_wrapper_syscall_valid(long int sysno);
+long int uid_wrapper_syscall_va(long int sysno, va_list va);
diff --git a/tests/test_syscall_uwrap.c b/tests/test_syscall_uwrap.c
new file mode 100644 (file)
index 0000000..fea0be7
--- /dev/null
@@ -0,0 +1,54 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdbool.h>
+#include <string.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#endif
+
+#include "swrap_fake_uid_wrapper.h"
+
+static void test_swrap_syscall_uwrap(void **state)
+{
+       long int rc;
+
+       (void)state; /* unused */
+
+       rc = syscall(__FAKE_UID_WRAPPER_SYSCALL_NO);
+       assert_int_equal(rc, __FAKE_UID_WRAPPER_SYSCALL_RC);
+
+       /*
+        * FreeBSD raises also a signal SIGSYS, so ignore it or the cmocka
+        * exception handler will catch it.
+        */
+       signal(SIGSYS, SIG_IGN);
+       rc = syscall(__FAKE_UID_WRAPPER_SYSCALL_NO+1);
+       signal(SIGSYS, SIG_DFL);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, ENOSYS);
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest swrap_tests[] = {
+               cmocka_unit_test(test_swrap_syscall_uwrap),
+       };
+
+       rc = cmocka_run_group_tests(swrap_tests, NULL, NULL);
+
+       return rc;
+}