#include #include #include #include #include #include #include static void *worker(void *arg) { security_context_t context = (security_context_t) arg; int rc; rc = setcon(context); printf("%u: setcon(%s) = %d (%s)\n", syscall(SYS_gettid), context, rc, strerror(errno)); if (rc) return NULL; if (getcon(&context)) { printf("%u: getcon() failed (%s)\n", syscall(SYS_gettid), strerror(errno)); return NULL; } printf("%u: Now I'm running in %s\n", syscall(SYS_gettid), context); freecon(context); sleep(1); /* to keep mm->mm_users > 1 for a while */ return NULL; } static char *test_contexts[] = { "unconfined_u:unconfined_r:unconfined_red_t:s0", "unconfined_u:unconfined_r:unconfined_blue_t:s0", "unconfined_u:unconfined_r:unconfined_green_t:s0", "unconfined_u:unconfined_r:unconfined_t:s0", }; #define NUM_TEST (sizeof(test_contexts) / sizeof(test_contexts[0])) int main(int argc, char *argv[]) { security_context_t context; pthread_t thread[NUM_TEST]; int i; if (!getcon(&context)) printf("%u: Now leader is running at %s\n", syscall(SYS_gettid), context); for (i=0; i < NUM_TEST; i++) pthread_create(&thread[i], NULL, worker, test_contexts[i]); for (i=0; i < NUM_TEST; i++) pthread_join(thread[i], NULL); return 0; }