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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* test35: utime() Author: Jan-Mark Wams (jms@cs.vu.nl) */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <utime.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
int max_error = 1;
#include "common.h"
#define ITERATIONS 10
#define N 100
#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
#define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
#define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
#define Mkdir(f) if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
#define Creat(f) if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
#define Time(t) if (time(t) == (time_t)-1) printf("Time error\n")
#define Chown(f,u,g) if (chown(f,u,g) != 0) printf("Can't chown %s\n", f)
#define Chmod(f,m) if (chmod(f,m) != 0) printf("Can't chmod %s\n", f)
#define PASSWD_FILE "/etc/passwd"
int I_can_chown;
int superuser;
char *MaxName; /* Name of maximum length */
char MaxPath[PATH_MAX]; /* Same for path */
char *NameTooLong; /* Name of maximum +1 length */
char PathTooLong[PATH_MAX + 1]; /* Same for path, both too long */
void test35a(void);
void test35b(void);
void test35c(void);
void makelongnames(void);
void getids(uid_t * uid, gid_t * gid);
int main(int argc, char *argv[])
{
int i, m = 0xFFFF;
sync();
if (argc == 2) m = atoi(argv[1]);
start(35);
makelongnames();
superuser = (geteuid() == 0);
#ifdef _POSIX_CHOWN_RESTRICTED
# if _POSIX_CHOWN_RESTRICTED - 0 != -1
I_can_chown = superuser;
# else
I_can_chown = 1;
# endif
#else
# include "error, this case requires dynamic checks and is not handled"
#endif
for (i = 0; i < ITERATIONS; i++) {
if (m & 0001) test35a();
if (m & 0002) test35b();
if (m & 0004) test35c();
}
quit();
return 1;
}
void test35a()
{ /* Test normal operation. */
struct stat st;
struct utimbuf ub;
time_t time1, time2;
int cnt;
subtest = 1;
/* Creat scratch file. */
Creat("foo");
/* Set file times back two seconds. */
Stat("foo", &st);
ub.actime = st.st_atime - 2;
ub.modtime = st.st_mtime - 2;
Time(&time1);
utime("foo", &ub);
Time(&time2);
Stat("foo", &st);
if (ub.actime != st.st_atime) e(1);
if (ub.modtime != st.st_mtime) e(2);
/* The status changed time sould be changed. */
#ifndef V1_FILESYSTEM
if (st.st_ctime < time1) e(3);
#endif
if (st.st_ctime > time2) e(4);
/* Add twenty seconds. */
Stat("foo", &st);
ub.actime = st.st_atime + 20;
ub.modtime = st.st_mtime + 20;
Time(&time1);
utime("foo", &ub);
Time(&time2);
Stat("foo", &st);
if (ub.actime != st.st_atime) e(5);
if (ub.modtime != st.st_mtime) e(6);
if (st.st_ctime < time1) e(7);
#ifndef V1_FILESYSTEM
if (st.st_ctime > time2) e(8);
#endif
/* Try 100 times to do utime in less than one second. */
cnt = 0;
do {
Time(&time1);
utime("foo", (struct utimbuf *) NULL);
Time(&time2);
} while (time1 != time2 && cnt++ < 100);
if (time1 == time2) {
Stat("foo", &st);
Time(&time2);
if (st.st_atime != time1) e(9);
if (st.st_mtime != time1) e(10);
} else {
Stat("foo", &st);
if (st.st_atime > time2) e(11);
if (st.st_mtime > time2) e(12);
Time(&time2);
if (st.st_atime < time1) e(13);
if (st.st_mtime < time1) e(14);
}
if (st.st_ctime < time1) e(15);
if (st.st_ctime > time2) e(16);
System("rm -rf ../DIR_35/*");
}
void test35b()
{
subtest = 2;
/* MaxPath and MaxName checkup. */
Creat(MaxName);
MaxPath[strlen(MaxPath) - 2] = '/';
MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
Creat(MaxPath);
if (utime(MaxName, NULL) != 0) e(1);
if (utime(MaxPath, NULL) != 0) e(2);
/* The owner doesn't need write permisson to set times. */
Creat("foo");
if (chmod("foo", 0) != 0) e(3);
if (utime("foo", NULL) != 0) e(4);
if (chmod("foo", 0777) != 0) e(5);
if (utime("foo", NULL) != 0) e(6);
System("rm -rf ../DIR_35/*");
}
void test35c()
{
gid_t gid, gid2;
uid_t uid, uid2;
struct utimbuf ub;
int fd, does_truncate, stat_loc;
subtest = 3;
/* Access problems. */
Mkdir("bar");
Creat("bar/tryme");
if (superuser) {
Chmod("bar", 0000); /* No search permisson at all. */
if (utime("bar/tryme", NULL) != 0) e(1);
}
if (!superuser) {
Chmod("bar", 0677); /* No search permisson. */
if (utime("bar/tryme", NULL) != -1) e(2);
if (errno != EACCES) e(3);
}
Chmod("bar", 0777);
if (I_can_chown) {
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20);
/* Get two differend non root uids. */
if (superuser) {
getids(&uid, &gid);
if (uid == 0) getids(&uid, &gid);
if (uid == 0) e(4);
}
if (!superuser) {
uid = geteuid();
gid = getegid();
}
getids(&uid2, &gid);
if (uid == uid2) getids(&uid2, &gid2);
if (uid == uid2) e(5);
/* Creat a number of files for root, user and user2. */
Creat("rootfile"); /* Owned by root. */
Chmod("rootfile", 0600);
Chown("rootfile", 0, 0);
Creat("user2file"); /* Owned by user 2, writeable. */
Chmod("user2file", 0020);
Chown("user2file", uid2, gid);
Creat("user2private"); /* Owned by user 2, privately. */
Chmod("user2private", 0600);
Chown("user2private", uid2, gid);
if (superuser) {
setgid(gid);
setuid(uid);
}
/* We now are user ``uid'' from group ``gid''. */
ub.actime = (time_t) 12345L;
ub.modtime = (time_t) 12345L;
if (utime("rootfile", NULL) != -1) e(6);
if (errno != EACCES) e(7);
if (utime("rootfile", &ub) != -1) e(8);
if (errno != EPERM) e(9);
if (utime("user2file", NULL) != 0) e(10);
if (utime("user2file", &ub) != -1) e(11);
if (errno != EPERM) e(12);
if (utime("user2private", NULL) != -1) e(13);
if (errno != EACCES) e(14);
if (utime("user2private", &ub) != -1) e(15);
if (errno != EPERM) e(16);
exit(errct ? 1 : 0);
default:
wait(&stat_loc);
if (stat_loc != 0) e(17); /* Alarm? */
}
}
/* Test names that are too long. */
does_truncate = does_fs_truncate();
fd = creat(NameTooLong, 0777);
if (does_truncate) {
if (utime(NameTooLong, NULL) != 0) e(18);
} else {
if (utime(NameTooLong, NULL) != -1) e(19);
if (errno != ENAMETOOLONG) e(20);
}
(void) close(fd);
/* Make PathTooLong contain ././.../a */
PathTooLong[strlen(PathTooLong) - 2] = '/';
PathTooLong[strlen(PathTooLong) - 1] = 'a';
Creat("a");
if (utime(PathTooLong, NULL) != -1) e(21);
if (errno != ENAMETOOLONG) e(22);
/* Non existing file name. */
if (utime("nonexist", NULL) != -1) e(23);
if (errno != ENOENT) e(24);
/* Empty file name. */
if (utime("", NULL) != -1) e(25);
if (errno != ENOENT) e(26);
System("rm -rf ../DIR_35/*");
}
void makelongnames()
{
register int i;
int max_name_length;
max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
* the same length, hence runtime check */
MaxName = malloc(max_name_length + 1);
NameTooLong = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
memset(MaxName, 'a', max_name_length);
MaxName[max_name_length] = '\0';
for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
MaxPath[i++] = '.';
MaxPath[i] = '/';
}
MaxPath[PATH_MAX - 1] = '\0';
strcpy(NameTooLong, MaxName); /* copy them Max to ToLong */
strcpy(PathTooLong, MaxPath);
NameTooLong[max_name_length] = 'a';
NameTooLong[max_name_length+1] = '\0';/* extend ToLongName by one too many */
PathTooLong[PATH_MAX - 1] = '/';
PathTooLong[PATH_MAX] = '\0'; /* inc ToLongPath by one */
}
/* Getids returns a valid uid and gid. Is used PASSWD FILE.
** It assumes the following format for a passwd file line:
** <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
** If no uids and gids can be found, it will only return 0 ids.
*/
void getids(r_uid, r_gid)
uid_t *r_uid;
gid_t *r_gid;
{
char line[N];
unsigned char *p;
uid_t uid;
gid_t gid;
FILE *fp;
int i;
static uid_t a_uid[N]; /* Array for uids. */
static gid_t a_gid[N]; /* Array for gids. */
static int nuid = 0, ngid = 0;/* The number of user & group ids. */
static int cuid = 0, cgid = 0;/* The current id index. */
/* If we don't have any uids go read some from the passwd file. */
if (nuid == 0) {
a_uid[nuid++] = 0; /* Root uid and gid. */
a_gid[ngid++] = 0;
if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
printf("Can't open ");
perror(PASSWD_FILE);
}
while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
p = (unsigned char *)strchr(line, ':');
if (p != NULL) p = (unsigned char *)strchr((char *)p + 1, ':');
if (p != NULL) {
p++;
uid = 0;
while (isdigit(*p)) {
uid *= 10;
uid += (uid_t) (*p - '0');
p++;
}
if (*p != ':') continue;
p++;
gid = 0;
while (isdigit(*p)) {
gid *= 10;
gid += (gid_t) (*p - '0');
p++;
}
if (*p != ':') continue;
if (nuid < N) {
for (i = 0; i < nuid; i++)
if (a_uid[i] == uid) break;
if (i == nuid) a_uid[nuid++] = uid;
}
if (ngid < N) {
for (i = 0; i < ngid; i++)
if (a_gid[i] == gid) break;
if (i == ngid) a_gid[ngid++] = gid;
}
if (nuid >= N && ngid >= N) break;
}
}
if (fp != NULL) fclose(fp);
}
/* We now have uids and gids in a_uid and a_gid. */
if (cuid >= nuid) cuid = 0;
if (cgid >= ngid) cgid = 0;
*r_uid = a_uid[cuid++];
*r_gid = a_gid[cgid++];
}
|