]> granicus.if.org Git - strace/blob - test/skodic.c
Fix the length argument passed from print_iovec to decode_netlink
[strace] / test / skodic.c
1 /*
2  * This demonstrates races: kernel may actually open other file
3  * than you read in the strace output.
4  * If you see a successfull open of /etc/shadow,
5  * you know you've seen a race.
6  *
7  * $ gcc -Wall -O0 skodic.c -o skodic
8  * $ timeout 0.1 ../strace -yeopen -o'|grep "shadow.*= [0-9]"' ./skodic
9  */
10
11 #ifndef _GNU_SOURCE
12 # define  _GNU_SOURCE 1
13 #endif
14
15 #include <errno.h>
16 #include <error.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23
24 int
25 main(void)
26 {
27         FILE *fp = tmpfile();
28         if (!fp)
29                 error(1, errno, "tmpfile");
30
31         int fd = fileno(fp);
32         size_t size = sysconf(_SC_PAGESIZE);
33
34         if (ftruncate(fd, size))
35                 error(1, errno, "ftruncate");
36
37         char *p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
38         if (p == MAP_FAILED)
39                 error(1, errno, "mmap");
40         fclose(fp);
41         fp = NULL;
42         fd = -1;
43
44         strcpy(p, "/etc/shadow");
45         if (open(p, 0) >= 0)
46                 error(1, 0, p);
47
48         pid_t pid = fork();
49         if (pid < 0)
50                 error(1, errno, "fork");
51
52         if (!pid) {
53                 for (;;) {
54                         strcpy(p, "/etc/passwd");
55                         strcpy(p, "/etc/shadow");
56                 }
57         } else {
58                 for (;;) {
59                         if ((fd = open(p, 0)) >= 0)
60                                 close(fd);
61                 }
62         }
63
64         return 0;
65 }