File: test-c5a7a0f0.c

package info (click to toggle)
proot 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,012 kB
  • sloc: ansic: 18,627; sh: 1,662; python: 108; asm: 41; makefile: 16; awk: 6
file content (95 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

static void *setuid_124(void *unused)
{
	int status;

	status = setuid(124);
	if (status < 0) {
		perror("setuid");
		pthread_exit((void *)-1);
	}

	if (getuid() != 124) {
		perror("getuid");
		pthread_exit((void *)-1);
	}

	pthread_exit(NULL);
}

static void pterror(const char *message, int error)
{
	fprintf(stderr, "%s: %s\n", message, strerror(error));
}

int main(void)
{
	pthread_t thread;
	int child_status;
	void *result;
	int status;

	switch(fork()) {
	case -1:
		perror("fork");
		exit(EXIT_FAILURE);

	case 0: /* child */
		status = setuid(123);
		if (status < 0) {
			perror("setuid");
			exit(EXIT_FAILURE);
		}

		if (getuid() != 123) {
			perror("getuid");
			exit(EXIT_FAILURE);
		}
		exit(EXIT_SUCCESS);

	default: /* parent */
		break;
	}

	status = wait(&child_status);
	if (status < 0 || child_status != 0) {
		perror("wait()");
		exit(EXIT_FAILURE);
	}

	if (getuid() != 0) {
		fprintf(stderr, "getuid() == %d != 0\n", getuid());
		exit(EXIT_FAILURE);
	}

	status = pthread_create(&thread, NULL, setuid_124, NULL);
	if (status != 0) {
		pterror("pthread_create", status);
		exit(EXIT_FAILURE);
	}

	status = pthread_join(thread, &result);
	if (status != 0) {
		pterror("pthread_create", status);
		exit(EXIT_FAILURE);
	}

	if (result != NULL) {
		fprintf(stderr, "result != NULL\n");
		exit(EXIT_FAILURE);
	}

	if (getuid() != 124) {
		fprintf(stderr, "getuid() == %d != 124\n", getuid());
		exit(EXIT_FAILURE);
	}

	exit(EXIT_SUCCESS);
}