[CRIU] [PATCH 1/2] test: thread_different_uid_gid -- Style nitpicking
Cyrill Gorcunov
gorcunov at openvz.org
Sat Oct 14 00:26:31 MSK 2017
CC: Vitaly Ostrosablin <vostrosablin at virtuozzo.com>
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
test/zdtm/static/thread_different_uid_gid.c | 41 ++++++++++++++++++-----------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/test/zdtm/static/thread_different_uid_gid.c b/test/zdtm/static/thread_different_uid_gid.c
index 2a5e4bdd8e14..7964b6893702 100644
--- a/test/zdtm/static/thread_different_uid_gid.c
+++ b/test/zdtm/static/thread_different_uid_gid.c
@@ -30,43 +30,48 @@ int done = 0;
void *chg_uid_gid(void *arg)
{
- int ret;
- cap_t mycaps;
cap_t newcaps;
+ cap_t mycaps;
+ int ret;
+
test_msg("Aux thread runs as UID: %d; GID: %d\n", getuid(), getgid());
+
newcaps = cap_from_text("cap_setgid,cap_setuid=+eip");
- if (!newcaps)
- {
+ if (!newcaps) {
pr_perror("Failed to get capability struct\n");
exit(1);
}
+
ret = cap_set_proc(newcaps);
if (ret) {
pr_perror("Failed to set capabilities for the process\n");
exit(1);
}
+
mycaps = cap_get_proc();
if (!mycaps) {
pr_perror("Failed to get child thread capabilities\n");
exit_group(2);
}
+
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);
+
ret = syscall(SYS_setresgid, gid, gid, gid);
if (ret >= 0) {
syscall(SYS_setresuid, uid, uid, uid);
- }
- if (ret < 0) {
+ } else if (ret < 0) {
pr_perror("Failed to change UID/GID\n");
exit_group(2);
}
+
gid = getgid();
uid = getuid();
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");
pthread_mutex_lock(&mutex);
- while (!done)
- {
+ while (!done) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
@@ -77,13 +82,13 @@ void *chg_uid_gid(void *arg)
int main(int argc, char **argv)
{
-
- int ret;
- cap_t newcaps;
pthread_t diff_cred_thread;
- test_init(argc, argv);
+ cap_t newcaps;
int maingroup;
int mainuser;
+ int ret;
+
+ test_init(argc, argv);
if (getuid() != 0) {
fail("Test is expected to be run with root privileges\n");
@@ -94,9 +99,9 @@ int main(int argc, char **argv)
test_msg("Test daemonized\n");
test_msg("Acquiring CAP_SETGID and CAP_SETUID...\n");
+
newcaps = cap_from_text("cap_setgid,cap_setuid=+eip");
- if (!newcaps)
- {
+ if (!newcaps) {
pr_perror("Failed to get capability struct\n");
exit(1);
}
@@ -119,13 +124,14 @@ int main(int argc, char **argv)
test_msg("Creating thread with different UID/GID\n");
ret = pthread_create(&diff_cred_thread, NULL, &chg_uid_gid, NULL);
+
sleep(5);
+
test_msg("Relinquishing root privileges\n");
ret = syscall(SYS_setresgid, maingroup, maingroup, maingroup);
if (ret >= 0) {
ret = syscall(SYS_setresuid, mainuser, mainuser, mainuser);
- }
- if (ret < 0) {
+ } else if (ret < 0) {
pr_perror("Failed to drop privileges\n");
exit(1);
}
@@ -142,12 +148,15 @@ int main(int argc, char **argv)
pr_perror("Thread credentials match after restore\n");
exit(1);
}
+
pthread_mutex_lock(&mutex);
done = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
pthread_join(diff_cred_thread, NULL);
test_msg("Threads joined\n");
+
pass();
+
return 0;
}
--
2.7.5
More information about the CRIU
mailing list