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
)
24 snprintf(cmd
, sizeof(cmd
), "./%s", prog
);
27 if (childpid
== (pid_t
) -1) {
29 } else if (childpid
== 0) {
30 if (execl(cmd
, prog
, arg
, NULL
) == -1) {
33 return(-2); /* Never reached */
38 return(WEXITSTATUS(status
));
41 void test_setgid(void)
43 /* Execve a new process that has setgid bits set */
46 /* When we exec a new process which has setgid set, that process should
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 */
75 /* When we exec a new process which has setuid set, that process should
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 */
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 */
132 /* Start with effective uid */
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);
151 /* We're the parent, wait for the child to finish */
155 /* Now test effective gid */
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);
174 /* We're the parent, wait for the child to finish */
179 void test_setnone(void)
181 /* Execve a new process that does not have setuid or setgid bits set */
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);
207 /* We're supposed to be setuid. Verify. */
214 if (issetugid() != 1) e(1);
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);
231 /* We're the parent. Wait for the child to finish */
236 void switch_to_su(void)
239 if (setuid(0) != 0) e(1);
242 int main(int argc
, char **argv
)
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 */
264 return(-1); /* Never reached */