-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestuidgid.c
118 lines (100 loc) · 2.67 KB
/
testuidgid.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Example for testing part of CS333 P2.
// This is by NO MEANS a complete test.
#ifdef CS333_P2
#include "types.h"
#include "user.h"
static void
uidTest(uint nval)
{
uint uid = getuid();
printf(1, "Current UID is: %d\n", uid);
printf(1, "Setting UID to %d\n", nval);
if (setuid(nval) < 0)
printf(2, "Error. Invalid UID: %d\n", nval);
uid = getuid();
printf(1, "Current UID is: %d\n", uid);
sleep(5 * TPS); // now type control-p
}
static void
gidTest(uint nval)
{
uint gid = getgid();
printf(1, "Current GID is: %d\n", gid);
printf(1, "Setting GID to %d\n", nval);
if (setgid(nval) < 0)
printf(2, "Error. Invalid GID: %d\n", nval);
setgid(nval);
gid = getgid();
printf(1, "Current GID is: %d\n", gid);
sleep(5 * TPS); // now type control-p
}
static void
forkTest(uint nval)
{
uint uid, gid;
int pid;
printf(1, "Setting UID to %d and GID to %d before fork(). Value"
" should be inherited\n", nval, nval);
if (setuid(nval) < 0)
printf(2, "Error. Invalid UID: %d\n", nval);
if (setgid(nval) < 0)
printf(2, "Error. Invalid UID: %d\n", nval);
printf(1, "Before fork(), UID = %d, GID = %d\n", getuid(), getgid());
pid = fork();
if (pid == 0) { // child
uid = getuid();
gid = getgid();
printf(1, "Child: UID is: %d, GID is: %d\n", uid, gid);
sleep(5 * TPS); // now type control-p
exit();
}
else
sleep(10 * TPS); // wait for child to exit before proceeding
}
static void
invalidTest(uint nval)
{
printf(1, "Setting UID to %d. This test should FAIL\n", nval);
if (setuid(nval) < 0)
printf(1, "SUCCESS! The setuid sytem call indicated failure\n");
else
printf(2, "FAILURE! The setuid system call indicates success\n");
printf(1, "Setting GID to %d. This test should FAIL\n", nval);
if (setgid(nval) < 0)
printf(1, "SUCCESS! The setgid sytem call indicated failure\n");
else
printf(2, "FAILURE! The setgid system call indicates success\n");
printf(1, "Setting UID to %d. This test should FAIL\n", -1);
if (setuid(-1) < 0)
printf(1, "SUCCESS! The setuid sytem call indicated failure\n");
else
printf(2, "FAILURE! The setgid system call indicates success\n");
}
static int
testuidgid(void)
{
uint nval, ppid;
// get/set uid test
nval = 100;
uidTest(nval);
// get/set gid test
nval = 200;
gidTest(nval);
// getppid test
ppid = getppid();
printf(1, "My parent process is: %d\n", ppid);
// fork tests to demonstrate UID/GID inheritance
nval = 111;
forkTest(nval);
// tests for invalid values for uid and gid
nval = 32800; // 32767 is max value
invalidTest(nval);
printf(1, "Done!\n");
return 0;
}
int
main() {
testuidgid();
exit();
}
#endif