Commit bd561153 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Andrei Vagin

test: thread_different_uid_gid -- Style nitpicking

CC: Vitaly Ostrosablin <vostrosablin@virtuozzo.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 10576583
...@@ -30,43 +30,48 @@ int done = 0; ...@@ -30,43 +30,48 @@ int done = 0;
void *chg_uid_gid(void *arg) void *chg_uid_gid(void *arg)
{ {
int ret;
cap_t mycaps;
cap_t newcaps; cap_t newcaps;
cap_t mycaps;
int ret;
test_msg("Aux thread runs as UID: %d; GID: %d\n", getuid(), getgid()); test_msg("Aux thread runs as UID: %d; GID: %d\n", getuid(), getgid());
newcaps = cap_from_text("cap_setgid,cap_setuid=+eip"); newcaps = cap_from_text("cap_setgid,cap_setuid=+eip");
if (!newcaps) if (!newcaps) {
{
pr_perror("Failed to get capability struct\n"); pr_perror("Failed to get capability struct\n");
exit(1); exit(1);
} }
ret = cap_set_proc(newcaps); ret = cap_set_proc(newcaps);
if (ret) { if (ret) {
pr_perror("Failed to set capabilities for the process\n"); pr_perror("Failed to set capabilities for the process\n");
exit(1); exit(1);
} }
mycaps = cap_get_proc(); mycaps = cap_get_proc();
if (!mycaps) { if (!mycaps) {
pr_perror("Failed to get child thread capabilities\n"); pr_perror("Failed to get child thread capabilities\n");
exit_group(2); exit_group(2);
} }
test_msg("Child capabilities: %s\n", cap_to_text(mycaps, NULL)); test_msg("Child capabilities: %s\n", cap_to_text(mycaps, NULL));
test_msg("Changing UID/GID in child thread to %d:%d\n", uid, gid); test_msg("Changing UID/GID in child thread to %d:%d\n", uid, gid);
ret = syscall(SYS_setresgid, gid, gid, gid); ret = syscall(SYS_setresgid, gid, gid, gid);
if (ret >= 0) { if (ret >= 0) {
syscall(SYS_setresuid, uid, uid, uid); syscall(SYS_setresuid, uid, uid, uid);
} } else if (ret < 0) {
if (ret < 0) {
pr_perror("Failed to change UID/GID\n"); pr_perror("Failed to change UID/GID\n");
exit_group(2); exit_group(2);
} }
gid = getgid(); gid = getgid();
uid = getuid(); uid = getuid();
test_msg("Now aux thread runs as UID: %d; GID: %d\n", uid, gid); test_msg("Now aux thread runs as UID: %d; GID: %d\n", uid, gid);
test_msg("Child thread is waiting for main thread's signal\n"); test_msg("Child thread is waiting for main thread's signal\n");
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
while (!done) while (!done) {
{
pthread_cond_wait(&cond, &mutex); pthread_cond_wait(&cond, &mutex);
} }
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
...@@ -77,13 +82,13 @@ void *chg_uid_gid(void *arg) ...@@ -77,13 +82,13 @@ void *chg_uid_gid(void *arg)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret;
cap_t newcaps;
pthread_t diff_cred_thread; pthread_t diff_cred_thread;
test_init(argc, argv); cap_t newcaps;
int maingroup; int maingroup;
int mainuser; int mainuser;
int ret;
test_init(argc, argv);
if (getuid() != 0) { if (getuid() != 0) {
fail("Test is expected to be run with root privileges\n"); fail("Test is expected to be run with root privileges\n");
...@@ -94,9 +99,9 @@ int main(int argc, char **argv) ...@@ -94,9 +99,9 @@ int main(int argc, char **argv)
test_msg("Test daemonized\n"); test_msg("Test daemonized\n");
test_msg("Acquiring CAP_SETGID and CAP_SETUID...\n"); test_msg("Acquiring CAP_SETGID and CAP_SETUID...\n");
newcaps = cap_from_text("cap_setgid,cap_setuid=+eip"); newcaps = cap_from_text("cap_setgid,cap_setuid=+eip");
if (!newcaps) if (!newcaps) {
{
pr_perror("Failed to get capability struct\n"); pr_perror("Failed to get capability struct\n");
exit(1); exit(1);
} }
...@@ -119,13 +124,14 @@ int main(int argc, char **argv) ...@@ -119,13 +124,14 @@ int main(int argc, char **argv)
test_msg("Creating thread with different UID/GID\n"); test_msg("Creating thread with different UID/GID\n");
ret = pthread_create(&diff_cred_thread, NULL, &chg_uid_gid, NULL); ret = pthread_create(&diff_cred_thread, NULL, &chg_uid_gid, NULL);
sleep(5); sleep(5);
test_msg("Relinquishing root privileges\n"); test_msg("Relinquishing root privileges\n");
ret = syscall(SYS_setresgid, maingroup, maingroup, maingroup); ret = syscall(SYS_setresgid, maingroup, maingroup, maingroup);
if (ret >= 0) { if (ret >= 0) {
ret = syscall(SYS_setresuid, mainuser, mainuser, mainuser); ret = syscall(SYS_setresuid, mainuser, mainuser, mainuser);
} } else if (ret < 0) {
if (ret < 0) {
pr_perror("Failed to drop privileges\n"); pr_perror("Failed to drop privileges\n");
exit(1); exit(1);
} }
...@@ -142,12 +148,15 @@ int main(int argc, char **argv) ...@@ -142,12 +148,15 @@ int main(int argc, char **argv)
pr_perror("Thread credentials match after restore\n"); pr_perror("Thread credentials match after restore\n");
exit(1); exit(1);
} }
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
done = 1; done = 1;
pthread_cond_signal(&cond); pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
pthread_join(diff_cred_thread, NULL); pthread_join(diff_cred_thread, NULL);
test_msg("Threads joined\n"); test_msg("Threads joined\n");
pass(); pass();
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment