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
|
/* test whether ftruncte() can truncate a file as non-root */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#define DATA "conftest.truncroot"
static int sys_waitpid(pid_t pid,int *status,int options)
{
#ifdef HAVE_WAITPID
return waitpid(pid,status,options);
#else /* USE_WAITPID */
return wait4(pid, status, options, NULL);
#endif /* USE_WAITPID */
}
main()
{
int fd;
char buf[1024];
pid_t pid;
if (getuid() != 0) {
fprintf(stderr,"ERROR: This test must be run as root - assuming \
ftruncate doesn't need root.\n");
exit(1);
}
fd = open(DATA,O_RDWR|O_CREAT|O_TRUNC,0666);
if(!fd)
exit(1);
if(write(fd, buf, 1024) != 1024)
exit(1);
if((pid = fork()) < 0)
exit(1);
if(pid) {
/* Parent. */
int status = 1;
if(sys_waitpid(pid, &status, 0) != pid) {
unlink(DATA);
exit(1);
}
unlink(DATA);
exit(WEXITSTATUS(status));
}
/* Child. */
if(setuid(500) < 0)
exit(1);
if(ftruncate(fd, 0) < 0) {
if(errno == EPERM || errno == EACCES)
exit(0);
}
exit(1);
}
|