1 /* test34: chmod() chown() Author: Jan-Mark Wams (jms@cs.vu.nl) */
3 /* There is a problem getting valid uids and gids, so we use the passwd
4 ** file (ie. /etc/passwd). I don't like this, but I see no other way.
5 ** The read-only-device-error (EROFS) is not checked!
6 ** Supplementary group IDs are ignored.
29 #define ALL_RWXB (S_IRWXU | S_IRWXG | S_IRWXO)
30 #define ALL_SETB (S_ISUID | S_ISGID)
31 #define ALL_BITS (ALL_RWXB | ALL_SETB)
33 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
34 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
35 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
36 #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
37 #define Mkdir(f) if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
38 #define Creat(f) if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
40 /* This program uses /etc/passwd and assumes things about it's contents. */
41 #define PASSWD_FILE "/etc/passwd"
45 char *MaxName
; /* Name of maximum length */
46 char MaxPath
[PATH_MAX
]; /* Same for path */
47 char *NameTooLong
; /* Name of maximum +1 length */
48 char PathTooLong
[PATH_MAX
+ 1]; /* Same for path, both too long */
53 mode_t
mode(char *file_name
);
54 void makelongnames(void);
55 void getids(uid_t
* uid
, gid_t
* gid
);
57 int main(int argc
, char *argv
[])
62 if (argc
== 2) m
= atoi(argv
[1]);
66 superuser
= (geteuid() == (uid_t
) 0);
68 #ifdef _POSIX_CHOWN_RESTRICTED
69 I_can_chown
= superuser
;
75 for (i
= 1; i
< ITERATIONS
; i
++) {
76 if (m
& 0001) test34a();
77 if (m
& 0002) test34b();
78 if (m
& 0004) test34c();
82 return(-1); /* Unreachable */
86 { /* Test normal operation. */
97 /* Make scratch file. */
100 for (mod
= 0; mod
<= ALL_BITS
; mod
++) {
101 if ((mod
& ALL_BITS
) != mod
) /* If not a valid mod next. */
104 if (time(&time1
) == (time_t) - 1) e(1);
105 if (chmod("foo", mod
) != 0) e(2);
107 if (time(&time2
) == (time_t) - 1) e(3);
109 if ((st2
.st_mode
& ALL_BITS
) != mod
) e(4);
111 if ((st2
.st_mode
& ALL_RWXB
) != (mod
& ALL_RWXB
)) e(5);
113 /* Test the C time feald. */
114 if (st1
.st_ctime
> st2
.st_ctime
) e(6);
115 if (st1
.st_ctime
> time1
) e(7);
116 if (st1
.st_ctime
> time2
) e(8);
117 #ifndef V1_FILESYSTEM
118 if (st2
.st_ctime
< time1
) e(9);
120 if (st2
.st_ctime
> time2
) e(10);
121 if (st1
.st_atime
!= st2
.st_atime
) e(11);
122 if (st1
.st_mtime
!= st2
.st_mtime
) e(12);
123 } /* End for loop. */
125 /* Check if chown(file, geteuid(), getegid()) works. */
126 for (cnt
= 0; cnt
< 20; cnt
++) {
127 /* Set all rights on foo, including the set .id bits. */
128 if (chmod("foo", ALL_BITS
) != 0) e(13);
130 if (time(&time1
) == (time_t) -1) e(14);
132 if (chown("foo", geteuid(), getegid()) != 0) e(15);
134 if (time(&time2
) == (time_t) -1) e(16);
136 /* Check ``chown()'' killed the set .id bits. */
138 if ((st1
.st_mode
& ALL_RWXB
) != ALL_RWXB
) e(17);
139 if ((st2
.st_mode
& ALL_BITS
) != ALL_RWXB
) e(18);
142 if ((st1
.st_mode
& ALL_BITS
) != ALL_BITS
) e(19);
143 if ((st1
.st_mode
& ALL_RWXB
) != ALL_RWXB
) e(20);
146 /* Check the timing. */
147 if (st1
.st_ctime
> st2
.st_ctime
) e(21);
148 if (st1
.st_ctime
> time1
) e(22);
149 if (st1
.st_ctime
> time2
) e(23);
150 #ifndef V1_FILESYSTEM
151 if (st2
.st_ctime
< time1
) e(24);
153 if (st2
.st_ctime
> time2
) e(25);
154 if (st1
.st_atime
!= st2
.st_atime
) e(26);
155 if (st1
.st_mtime
!= st2
.st_mtime
) e(27);
156 } /* End for loop. */
158 /* Make scratch file. */
159 if (chmod("foo", ALL_RWXB
) != 0) e(28);
162 /* Do a 20 tests on a gid and uid. */
163 for (cnt
= 0; cnt
< 20; cnt
++) {
164 /* Get a uid and a gid, test chown. */
167 if (time(&time1
) == (time_t) -1) e(29);
168 if (chown("foo", (uid_t
) 0, (gid_t
) 0) != 0) e(30);
170 if (time(&time2
) == (time_t) -1) e(31);
172 /* Test the C time field. */
173 if (st1
.st_ctime
> st2
.st_ctime
) e(32);
174 if (st1
.st_ctime
> time1
) e(33);
175 if (st1
.st_ctime
> time2
) e(34);
176 if (st2
.st_ctime
< time1
) e(35);
177 if (st2
.st_ctime
> time2
) e(36);
178 if (st1
.st_atime
!= st2
.st_atime
) e(37);
179 if (st1
.st_mtime
!= st2
.st_mtime
) e(38);
181 /* Do aditional tests. */
182 if (chown("foo", (uid_t
) 0, gid
) != 0) e(39);
183 if (chown("foo", uid
, (gid_t
) 0) != 0) e(40);
184 if (chown("foo", uid
, gid
) != 0) e(41);
188 /* Check if a non-superuser can change a files gid to gid2 *
189 * if gid2 is the current process gid. */
190 for (cnt
= 0; cnt
< 5; cnt
++) {
193 printf("Can't fork\n");
203 getids(&uid2
, &gid2
);
204 if (gid
== gid2
) e(43);
206 /* Creat boo and bar for user uid of group gid. */
208 if (chown("boo", uid
, gid
) != 0) e(44);
209 if (chmod("boo", ALL_BITS
) != 0) e(45);
211 if (chown("bar", uid
, gid
) != 0) e(46);
212 if (chmod("bar", ALL_BITS
) != 0) e(47);
214 /* We now become user uid of group gid2. */
219 if (time(&time1
) == (time_t) -1) e(48);
220 if (chown("bar", uid
, gid2
) != 0) e(49);
222 if (time(&time2
) == (time_t) -1) e(50);
224 /* Check if the SET_BITS are cleared. */
225 if ((st1
.st_mode
& ALL_BITS
) != ALL_BITS
) e(51);
226 if ((st2
.st_mode
& ALL_BITS
) != ALL_RWXB
) e(52);
228 /* Check the st_times. */
229 if (st1
.st_ctime
> st2
.st_ctime
) e(53);
230 if (st1
.st_ctime
> time1
) e(54);
231 if (st1
.st_ctime
> time2
) e(55);
232 if (st2
.st_ctime
< time1
) e(56);
233 if (st2
.st_ctime
> time2
) e(57);
234 if (st1
.st_atime
!= st2
.st_atime
) e(58);
235 if (st1
.st_mtime
!= st2
.st_mtime
) e(59);
238 if (chmod("boo", ALL_BITS
) != 0) e(60);
241 /* Check if the set gid bit is cleared. */
242 if ((st1
.st_mode
& ALL_RWXB
) != ALL_RWXB
) e(61);
243 if ((st2
.st_mode
& S_ISGID
) != 0) e(62);
245 if (chown("boo", uid
, gid2
) != 0) e(63);
248 /* Check if the set uid bit is cleared. */
249 if ((st1
.st_mode
& S_ISUID
) != 0) e(64);
254 if (stat_loc
!= 0) e(65); /* Alarm? */
256 } /* end for loop. */
257 } /* end if (superuser). */
258 if (chmod("foo", ALL_BITS
) != 0) e(66);
260 if (chown("foo", geteuid(), getegid()) != 0) e(67);
262 if ((st1
.st_mode
& ALL_BITS
) != ALL_BITS
) e(68); /* See intro! */
264 if ((st2
.st_mode
& ALL_RWXB
) != ALL_RWXB
) e(69);
266 if ((st2
.st_mode
& ALL_BITS
) != ALL_RWXB
) e(70);
268 (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
269 System("rm -rf ../DIR_34/*");
276 struct stat st1
, st2
;
280 /* Test chmod() and chown() on non regular files and on MaxName and
281 * MaxPath. * Funny, but dirs should also have S_IS.ID bits.
286 MaxPath
[strlen(MaxPath
) - 2] = '/';
287 MaxPath
[strlen(MaxPath
) - 1] = 'a'; /* make ././.../a */
290 for (mod
= 1; mod
<= ALL_BITS
; mod
<<= 1) {
291 if ((mod
& ALL_BITS
) != mod
) continue; /* bad mod */
293 if (time(&time1
) == (time_t) -1) e(1);
294 if (chmod("dir", mod
) != 0) e(2);
296 if (time(&time2
) == (time_t) -1) e(3);
298 if ((st2
.st_mode
& ALL_BITS
) != mod
) e(4);
300 if ((st2
.st_mode
& ALL_RWXB
) != (mod
& ALL_RWXB
)) e(5);
302 /* Test the C time field. */
303 if (st1
.st_ctime
> st2
.st_ctime
) e(6);
304 if (st1
.st_ctime
> time1
) e(7);
305 if (st1
.st_ctime
> time2
) e(8);
306 #ifndef V1_FILESYSTEM
307 if (st2
.st_ctime
< time1
) e(9);
309 if (st2
.st_ctime
> time2
) e(10);
310 if (st1
.st_atime
!= st2
.st_atime
) e(11);
311 if (st1
.st_mtime
!= st2
.st_mtime
) e(12);
314 if (time(&time1
) == (time_t) -1) e(13);
315 if (chmod("fifo", mod
) != 0) e(14);
317 if (time(&time2
) == (time_t) -1) e(15);
319 if ((st2
.st_mode
& ALL_BITS
) != mod
) e(16);
321 if ((st2
.st_mode
& ALL_RWXB
) != (mod
& ALL_RWXB
)) e(17);
323 /* Test the C time field. */
324 if (st1
.st_ctime
> st2
.st_ctime
) e(18);
325 if (st1
.st_ctime
> time1
) e(19);
326 if (st1
.st_ctime
> time2
) e(20);
327 #ifndef V1_FILESYSTEM
328 if (st2
.st_ctime
< time1
) e(21);
330 if (st2
.st_ctime
> time2
) e(22);
331 if (st1
.st_atime
!= st2
.st_atime
) e(23);
332 if (st1
.st_mtime
!= st2
.st_mtime
) e(24);
335 if (time(&time1
) == (time_t) -1) e(25);
336 if (chmod(MaxName
, mod
) != 0) e(26);
338 if (time(&time2
) == (time_t) -1) e(27);
340 if ((st2
.st_mode
& ALL_BITS
) != mod
) e(28);
342 if ((st2
.st_mode
& ALL_RWXB
) != (mod
& ALL_RWXB
)) e(29);
344 /* Test the C time field. */
345 if (st1
.st_ctime
> st2
.st_ctime
) e(30);
346 if (st1
.st_ctime
> time1
) e(31);
347 if (st1
.st_ctime
> time2
) e(32);
348 #ifndef V1_FILESYSTEM
349 if (st2
.st_ctime
< time1
) e(33);
351 if (st2
.st_ctime
> time2
) e(34);
352 if (st1
.st_atime
!= st2
.st_atime
) e(35);
353 if (st1
.st_mtime
!= st2
.st_mtime
) e(36);
356 if (time(&time1
) == (time_t) -1) e(37);
357 if (chmod(MaxPath
, mod
) != 0) e(38);
359 if (time(&time2
) == (time_t) -1) e(39);
361 if ((st2
.st_mode
& ALL_BITS
) != mod
) e(40);
363 if ((st2
.st_mode
& ALL_RWXB
) != (mod
& ALL_RWXB
)) e(41);
365 /* Test the C time field. */
366 if (st1
.st_ctime
> st2
.st_ctime
) e(42);
367 if (st1
.st_ctime
> time1
) e(43);
368 if (st1
.st_ctime
> time2
) e(44);
369 #ifndef V1_FILESYSTEM
370 if (st2
.st_ctime
< time1
) e(45);
372 if (st2
.st_ctime
> time2
) e(46);
373 if (st1
.st_atime
!= st2
.st_atime
) e(47);
374 if (st1
.st_mtime
!= st2
.st_mtime
) e(48);
377 if (chmod("dir", 0777) != 0) e(49);
378 if (chmod("fifo", 0777) != 0) e(50);
379 if (chmod(MaxName
, 0777) != 0) e(51);
380 if (chmod(MaxPath
, 0777) != 0) e(52);
382 (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
383 System("rm -rf ../DIR_34/*");
391 int fd
, does_truncate
, stat_loc
;
398 /* Disalow search permission and see if chmod() and chown() return
401 if (chmod("dir", ALL_BITS
& ~S_IXUSR
) != 0) e(1);
403 if (chmod("dir/try_me", 0) != -1) e(2);
404 if (errno
!= EACCES
) e(3);
406 if (chown("dir/try_me", geteuid(), getegid()) != -1) e(4);
407 if (errno
!= EACCES
) e(5);
413 if (chmod("fifo/try_me", 0) != -1) e(6);
414 if (errno
!= ENOTDIR
) e(7);
415 if (chown("fifo/try_me", geteuid(), getegid()) != -1) e(8);
416 if (errno
!= ENOTDIR
) e(9);
419 if (chmod("file/try_me", 0) != -1) e(10);
420 if (errno
!= ENOTDIR
) e(11);
421 if (chown("file/try_me", geteuid(), getegid()) != -1) e(12);
422 if (errno
!= ENOTDIR
) e(13);
424 /* Check empty path. */
425 if (chmod("", 0) != -1) e(14);
426 if (errno
!= ENOENT
) e(15);
427 if (chown("", geteuid(), getegid()) != -1) e(16);
428 if (errno
!= ENOENT
) e(17);
430 /* Check non existing file name. */
431 if (chmod("non_exist", 0) != -1) e(18);
432 if (errno
!= ENOENT
) e(19);
433 if (chown("non_exist", geteuid(), getegid()) != -1) e(20);
434 if (errno
!= ENOENT
) e(21);
436 /* Check what we get if we do not have permisson. */
439 if (st
.st_uid
== geteuid()) e(22);
441 /* First I had 0, I changed it to st.st_mode 8-). */
442 if (chmod("/", st
.st_mode
) != -1) e(23);
443 if (errno
!= EPERM
) e(24);
447 if (st
.st_uid
== geteuid()) e(25);
448 if (chown("/", geteuid(), getegid()) != -1) e(26);
449 if (errno
!= EPERM
) e(27);
452 /* If we are superuser, we can test all id combinations. */
455 case -1: printf("Can't fork\n"); break;
464 getids(&uid2
, &gid2
);
465 if (gid
== gid2
) e(29);
466 if (uid
== uid2
) e(30);
468 /* Creat boo, owned by root. */
470 if (chmod("boo", ALL_BITS
) != 0) e(31);
472 /* Creat boo for user uid2 of group gid2. */
474 if (chown("bar", uid2
, gid2
) != 0) e(32);
475 if (chmod("bar", ALL_BITS
) != 0) e(33);
477 /* Creat my_gid for user uid2 of group gid. */
479 if (chown("my_gid", uid2
, gid
) != 0) e(34);
480 if (chmod("my_gid", ALL_BITS
) != 0) e(35);
482 /* Creat my_uid for user uid of uid gid. */
484 if (chown("my_uid", uid
, gid
) != 0) e(36);
485 if (chmod("my_uid", ALL_BITS
) != 0) e(37);
487 /* We now become user uid of uid gid. */
491 if (chown("boo", uid
, gid
) != -1) e(38);
492 if (errno
!= EPERM
) e(39);
493 if (chown("bar", uid
, gid
) != -1) e(40);
494 if (errno
!= EPERM
) e(41);
495 if (chown("my_gid", uid
, gid
) != -1) e(42);
496 if (errno
!= EPERM
) e(43);
497 if (chown("my_uid", uid
, gid2
) != -1) e(44);
499 /* The EPERM is not strict POSIX. */
500 if (errno
!= EPERM
) e(45);
502 if (chmod("boo", 0) != -1) e(46);
503 if (errno
!= EPERM
) e(47);
504 if (chmod("bar", 0) != -1) e(48);
505 if (errno
!= EPERM
) e(49);
506 if (chmod("my_gid", 0) != -1) e(50);
507 if (errno
!= EPERM
) e(51);
512 if (stat_loc
!= 0) e(52); /* Alarm? */
516 /* Check too long path ed. */
517 does_truncate
= does_fs_truncate();
518 fd
= creat(NameTooLong
, 0777);
521 if (close(fd
) != 0) e(54);
522 if (chmod(NameTooLong
, 0777) != 0) e(55);
523 if (chown(NameTooLong
, geteuid(), getegid()) != 0) e(56);
526 if (errno
!= ENAMETOOLONG
) e(58);
527 (void) close(fd
); /* Just in case */
530 /* Make PathTooLong contain ././.../a */
531 PathTooLong
[strlen(PathTooLong
) - 2] = '/';
532 PathTooLong
[strlen(PathTooLong
) - 1] = 'a';
534 if (chmod(PathTooLong
, 0777) != -1) e(59);
535 if (errno
!= ENAMETOOLONG
) e(60);
536 if (chown(PathTooLong
, geteuid(), getegid()) != -1) e(61);
537 if (errno
!= ENAMETOOLONG
) e(62);
539 (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
540 System("rm -rf ../DIR_34/*");
548 max_name_length
= name_max("."); /* Aka NAME_MAX, but not every FS supports
549 * the same length, hence runtime check */
550 MaxName
= malloc(max_name_length
+ 1);
551 NameTooLong
= malloc(max_name_length
+ 1 + 1); /* Name of maximum +1 length */
552 memset(MaxName
, 'a', max_name_length
);
553 MaxName
[max_name_length
] = '\0';
555 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
559 MaxPath
[PATH_MAX
- 1] = '\0';
561 strcpy(NameTooLong
, MaxName
); /* copy them Max to ToLong */
562 strcpy(PathTooLong
, MaxPath
);
564 NameTooLong
[max_name_length
] = 'a';
565 NameTooLong
[max_name_length
+1] = '\0';/* extend ToLongName by one too many */
566 PathTooLong
[PATH_MAX
- 1] = '/';
567 PathTooLong
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */
570 /* Getids returns a valid uid and gid. Is used PASSWD FILE.
571 * It assumes the following format for a passwd file line:
572 * <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
573 * If no uids and gids can be found, it will only return 0 ids.
575 void getids(r_uid
, r_gid
)
586 static uid_t a_uid
[N
]; /* Array for uids. */
587 static gid_t a_gid
[N
]; /* Array for gids. */
588 static int nuid
= 0, ngid
= 0;/* The number of user & group ids. */
589 static int cuid
= 0, cgid
= 0;/* The current id index. */
591 /* If we don't have any uids go read some from the passwd file. */
593 a_uid
[nuid
++] = 0; /* Root uid and gid. */
595 if ((fp
= fopen(PASSWD_FILE
, "r")) == NULL
) {
596 printf("Can't open ");
599 while (fp
!= NULL
&& fgets(line
, sizeof(line
), fp
) != NULL
) {
600 p
= (unsigned char *)strchr(line
, ':');
601 if (p
!= NULL
) p
= (unsigned char *)strchr((char *)p
+ 1, ':');
605 while (isdigit(*p
)) {
607 uid
+= (uid_t
) (*p
- '0');
610 if (*p
!= ':') continue;
613 while (isdigit(*p
)) {
615 gid
+= (gid_t
) (*p
- '0');
618 if (*p
!= ':') continue;
620 for (i
= 0; i
< nuid
; i
++)
621 if (a_uid
[i
] == uid
) break;
622 if (i
== nuid
) a_uid
[nuid
++] = uid
;
625 for (i
= 0; i
< ngid
; i
++)
626 if (a_gid
[i
] == gid
) break;
627 if (i
== ngid
) a_gid
[ngid
++] = gid
;
629 if (nuid
>= N
&& ngid
>= N
) break;
632 if (fp
!= NULL
) fclose(fp
);
635 /* We now have uids and gids in a_uid and a_gid. */
636 if (cuid
>= nuid
) cuid
= 0;
637 if (cgid
>= ngid
) cgid
= 0;
638 *r_uid
= a_uid
[cuid
++];
639 *r_gid
= a_gid
[cgid
++];