diff options
author | Andreas Schneider <asn@samba.org> | 2015-10-27 10:19:57 +0100 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2015-10-27 14:55:41 +0100 |
commit | a6eb46785f68934376b103796273ffdaf1f6f5a9 (patch) | |
tree | 7f43524ddbe216a830004d9ee0c4fd767b336370 | |
parent | 0006b60255e5acfe1af6bcc0be76e0a5ff2ef3ff (diff) | |
download | uid_wrapper-a6eb46785f68934376b103796273ffdaf1f6f5a9.tar.gz uid_wrapper-a6eb46785f68934376b103796273ffdaf1f6f5a9.tar.xz uid_wrapper-a6eb46785f68934376b103796273ffdaf1f6f5a9.zip |
tests: Create a test for threaded SYS_setuid
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
-rw-r--r-- | tests/CMakeLists.txt | 8 | ||||
-rw-r--r-- | tests/test_glibc_thread_support.c | 67 | ||||
-rw-r--r-- | tests/test_thread_setuid.c | 101 |
3 files changed, 109 insertions, 67 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3af0695..cf8b830 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -79,6 +79,14 @@ set_property( ENVIRONMENT ${TEST_ENVIRONMENT}) if (LINUX OR OSX) + add_cmocka_test(test_thread_setuid test_thread_setuid.c ${CMOCKA_LIBRARY}) + target_link_libraries(test_thread_setuid ${CMAKE_THREAD_LIBS_INIT}) + set_property( + TEST + test_thread_setuid + PROPERTY + ENVIRONMENT ${TEST_ENVIRONMENT};UID_WRAPPER=1;CMOCKA_TEST_ABORT=1) + add_cmocka_test(test_thread_setreuid test_thread_setreuid.c ${CMOCKA_LIBRARY}) target_link_libraries(test_thread_setreuid ${CMAKE_THREAD_LIBS_INIT}) set_property( diff --git a/tests/test_glibc_thread_support.c b/tests/test_glibc_thread_support.c index 7d61cc2..3661314 100644 --- a/tests/test_glibc_thread_support.c +++ b/tests/test_glibc_thread_support.c @@ -136,72 +136,6 @@ static void test_sync_setgid(void **state) } -static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER; -static void *uwrap_getuid_sync(void *arg) -{ - uid_t u; - - (void) arg; /* unused */ - - pthread_mutex_unlock(&sleep_mutex); - pthread_mutex_lock(&wait_mutex); - - u = getuid(); - assert_int_equal(u, 888); - - return NULL; -} - -static void *uwrap_setuid_sync(void *arg) -{ - int rc; - - (void) arg; /* unused */ - - rc = setuid(888); - assert_int_equal(rc, 0); - - return NULL; -} - -static void test_real_sync_setuid(void **state) -{ - pthread_t threads[2]; - uid_t u; - int rc; - - (void) state; /* unused */ - - rc = setuid(222); - assert_int_equal(rc, 0); - - pthread_mutex_lock(&wait_mutex); - pthread_mutex_lock(&sleep_mutex); - - /* Create thread which will wait for change. */ - pthread_create(&threads[0], - NULL, - uwrap_getuid_sync, - NULL); - - pthread_mutex_lock(&sleep_mutex); - - pthread_create(&threads[1], - NULL, - uwrap_setuid_sync, - NULL); - - pthread_join(threads[1], NULL); - - pthread_mutex_unlock(&wait_mutex); - - pthread_join(threads[0], NULL); - - u = getuid(); - assert_int_equal(u, 888); -} - static void *uwrap_setgroups(void *arg) { gid_t glist[] = { 100, 200, 300, 400, 500 }; @@ -333,7 +267,6 @@ int main(void) { const struct CMUnitTest thread_tests[] = { cmocka_unit_test(test_sync_setgid), cmocka_unit_test(test_sync_setgid_syscall), - cmocka_unit_test(test_real_sync_setuid), cmocka_unit_test(test_sync_setgroups), cmocka_unit_test(test_thread_create_thread_setgid), }; diff --git a/tests/test_thread_setuid.c b/tests/test_thread_setuid.c new file mode 100644 index 0000000..8b52c38 --- /dev/null +++ b/tests/test_thread_setuid.c @@ -0,0 +1,101 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#include <pthread.h> + +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <grp.h> + +#ifdef HAVE_SYS_SYSCALL_H +#include <sys/syscall.h> +#endif +#ifdef HAVE_SYSCALL_H +#include <syscall.h> +#endif + +#define NUM_THREADS 10 + +#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) + + +static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void *uwrap_getuid_sync(void *arg) +{ + uid_t u; + + (void) arg; /* unused */ + + pthread_mutex_unlock(&sleep_mutex); + pthread_mutex_lock(&wait_mutex); + + u = getuid(); + assert_int_equal(u, 888); + + return NULL; +} + +static void *uwrap_setuid_sync(void *arg) +{ + int rc; + + (void) arg; /* unused */ + + rc = setuid(888); + assert_int_equal(rc, 0); + + return NULL; +} + +static void test_real_sync_setuid(void **state) +{ + pthread_t threads[2]; + uid_t u; + + (void) state; /* unused */ + + pthread_mutex_lock(&wait_mutex); + pthread_mutex_lock(&sleep_mutex); + + /* Create thread which will wait for change. */ + pthread_create(&threads[0], + NULL, + uwrap_getuid_sync, + NULL); + + pthread_mutex_lock(&sleep_mutex); + + pthread_create(&threads[1], + NULL, + uwrap_setuid_sync, + NULL); + + pthread_join(threads[1], NULL); + + pthread_mutex_unlock(&wait_mutex); + + pthread_join(threads[0], NULL); + + u = getuid(); + assert_int_equal(u, 888); +} + +int main(void) { + int rc; + + const struct CMUnitTest thread_tests[] = { + cmocka_unit_test(test_real_sync_setuid), + }; + + rc = cmocka_run_group_tests(thread_tests, NULL, NULL); + + return rc; +} |