make minix lwip make explicit use of 'int'
[minix3.git] / test / test60.c
blob3375a6065b49bef58de6ce08fe860d2cafe67ca3
1 #include <sys/types.h>
2 #include <sys/wait.h>
3 #include <stdio.h>
4 #include <unistd.h>
6 #define MAX_ERROR 5
7 #include "common.c"
9 int subtest = -1;
11 void test_self(void);
12 void test_setnone(void);
13 void test_setuid(void);
14 void test_setgid(void);
15 void test_effugid(void);
16 int execute(const char *prog, const char *arg);
18 int execute(const char *prog, const char *arg)
20 pid_t childpid;
21 int status;
22 char cmd[30];
24 snprintf(cmd, sizeof(cmd), "./%s", prog);
26 childpid = fork();
27 if (childpid == (pid_t) -1) {
28 return(-2);
29 } else if (childpid == 0) {
30 if (execl(cmd, prog, arg, NULL) == -1) {
31 exit(-2);
33 return(-2); /* Never reached */
34 } else {
35 wait(&status);
38 return(WEXITSTATUS(status));
41 void test_setgid(void)
43 /* Execve a new process that has setgid bits set */
44 subtest = 3;
46 /* When we exec a new process which has setgid set, that process should
47 * be tainted.
49 system("chmod 2755 setgid");
50 if (execute("setgid", "0000") != 1) e(2);
52 /* When we exec a new process which has setgid set, but unsets that bit
53 * before calling issetugid() should still be tainted
55 system("chmod 2755 setgid");
56 if (execute("setgid", "0755") != 1) e(3);
58 /* When we exec a new process which has setgid set, and then also sets
59 * setuid before calling issetugid() should still be tainted
61 system("chmod 2755 setgid");
62 if (execute("setgid", "06755") != 1) e(4);
64 /* When we exec a new process that has setgid set, and which upon
65 * execution forks, the forked child should also be tainted */
66 system("chmod 2755 setgidfork");
67 if (execute("setgidfork", "0000") != 1) e(5);
70 void test_setuid(void)
72 /* Execve a new process that has setuid bits set */
73 subtest = 4;
75 /* When we exec a new process which has setuid set, that process should
76 * be tainted.
78 system("chmod 4755 setuid");
79 if (execute("setuid", "0000") != 1) e(1);
81 /* When we exec a new process which has setuid set, but unsets that bit
82 * before calling issetugid() should still be tainted
84 system("chmod 4755 setuid");
85 if (execute("setuid", "0755") != 1) e(2);
87 /* When we exec a new process which has setuid set, and then also sets
88 * setgid before calling issetugid() should still be tainted
90 system("chmod 4755 setuid");
91 if (execute("setuid", "06755") != 1) e(3);
93 /* When we exec a new process that has setgid set, and which upon
94 * execution forks, the forked child should also be tainted */
95 system("chmod 4755 setuidfork");
96 if (execute("setuidfork", "0000") != 1) e(4);
100 void test_setugid(void)
102 /* Execve a new process that has setuid and setgid bits set */
103 subtest = 5;
105 /* When we exec a new process which has setugid set, that
106 * process should be tainted.
108 system("chmod 6755 setugid");
109 if (execute("setugid", "0000") != 1) e(1);
111 /* When we exec a new process which has setugid set, but unsets those bits
112 * before calling issetugid() should still be tainted
114 system("chmod 6755 setugid");
115 if (execute("setugid", "0755") != 1) e(2);
117 /* When we exec a new process that has setugid set, and which upon
118 * execution forks, the forked child should also be tainted */
119 system("chmod 6755 setugidfork");
120 if (execute("setugidfork", "0000") != 1) e(4);
124 void test_effugid(void)
126 /* Test taint status with different effective uid and gid */
127 pid_t childpid;
128 int status;
130 subtest = 6;
132 /* Start with effective uid */
133 childpid = fork();
134 if (childpid == (pid_t) -1) e(1);
135 else if (childpid == 0) {
136 /* We're the child */
138 /* We should be tainted */
139 if (issetugid() != 1) e(2);
141 /* Now execute a program without set{u,g}id; should not be tainted */
142 system("chmod 755 nobits");
143 if (execute("nobits", "0000") != 0) e(3);
145 /* Change effective uid into current+42 and try nobits again. This time
146 * it should be tainted */
147 if (seteuid(geteuid() + 42) != 0) e(4);
148 if (execute("nobits", "0000") != 1) e(5);
149 exit(EXIT_SUCCESS);
150 } else {
151 /* We're the parent, wait for the child to finish */
152 wait(&status);
155 /* Now test effective gid */
156 childpid = fork();
157 if (childpid == (pid_t) -1) e(1);
158 else if (childpid == 0) {
159 /* We're the child */
161 /* We should be tainted */
162 if (issetugid() != 1) e(2);
164 /* Now execute a program without set{u,g}id; should not be tainted */
165 system("chmod 755 nobits");
166 if (execute("nobits", "0000") != 0) e(3);
168 /* Change effective gid into current+42 and try nobits again. This time
169 * it should be tainted */
170 if (seteuid(getegid() + 42) != 0) e(4);
171 if (execute("nobits", "0000") != 1) e(5);
172 exit(EXIT_SUCCESS);
173 } else {
174 /* We're the parent, wait for the child to finish */
175 wait(&status);
179 void test_setnone(void)
181 /* Execve a new process that does not have setuid or setgid bits set */
182 subtest = 2;
184 /* When we exec a new process which doesn't have set{u,g}id set, that
185 * process should not be tainted */
186 system("chmod 755 nobits");
187 if (execute("nobits", "0000") != 0) e(2);
189 /* When we exec a new process which doesn't have set{u,g}id set, but
190 * sets them after execution, the process should still not be tainted
192 system("chmod 755 nobits");
193 if (execute("nobits", "02755") != 0) e(4);
194 system("chmod 755 nobits");
195 if (execute("nobits", "04755") != 0) e(3);
196 system("chmod 755 nobits");
197 if (execute("nobits", "06755") != 0) e(5);
199 /* When we exec a new process that doesn't have setugid set, and which upon
200 * execution forks, the forked child should not be tainted either */
201 system("chmod 755 nobitsfork");
202 if (execute("nobitsfork", "0000") != 0) e(6);
205 void test_self(void)
207 /* We're supposed to be setuid. Verify. */
209 int status;
210 pid_t childpid;
212 subtest = 1;
214 if (issetugid() != 1) e(1);
215 childpid = fork();
216 if (childpid == -1) e(2);
217 else if (childpid == 0) {
218 /* We're the child and should inherit the tainted status of the parent
220 if (issetugid() != 1) e(3);
222 /* Let's change to the bin user */
223 if (setuid((uid_t) 2) != 0) e(4);
224 if (getuid() != (uid_t) 2) e(5);
226 /* At this point, taint status should not have changed. */
227 if (issetugid() != 1) e(6);
229 exit(EXIT_SUCCESS);
230 } else {
231 /* We're the parent. Wait for the child to finish */
232 wait(&status);
236 void switch_to_su(void)
238 subtest = 0;
239 if (setuid(0) != 0) e(1);
242 int main(int argc, char **argv)
244 start(60);
245 system("cp ../t60a nobits");
246 system("cp ../t60a setgid");
247 system("cp ../t60a setuid");
248 system("cp ../t60a setugid");
249 system("cp ../t60b nobitsfork");
250 system("cp ../t60b setuidfork");
251 system("cp ../t60b setgidfork");
252 system("cp ../t60b setugidfork");
254 switch_to_su(); /* We have to be root to perform this test */
255 test_self();
256 test_setnone();
257 test_setuid();
258 test_setgid();
259 test_setugid();
260 test_effugid();
262 quit();
264 return(-1); /* Never reached */