-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatcheck.c
More file actions
194 lines (161 loc) · 4.65 KB
/
Copy pathformatcheck.c
File metadata and controls
194 lines (161 loc) · 4.65 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
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
// LD_PRELOAD-library to find writable scanf/printf format strings
// license: public domain
// information about writable memory locations is taken from /proc/self/maps
// compile test: cc -std=c99 -D_GNU_SOURCE -DFORMATCHECK_TEST -Wall -Wextra formatcheck.c -ldl -o formatcheck_test
// compile lib: cc -std=c99 -D_GNU_SOURCE -Wall -Wextra formatcheck.c -ldl -fPIC -shared -o libformatcheck.so
// _GNU_SOURCE is for dlsym(RTLD_NEXT, ...) to load wrapped function
// use lib: env LD_PRELOAD=$PWD/libformatcheck.so $SHELL
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int is_mapping_writable(const void *ptr){
FILE *maps = fopen("/proc/self/maps", "r");
int res = -1;
if(maps == NULL) return -1;
for(;;){
size_t a, b;
char flags[5];
if(fscanf(maps, "%zx-%zx %4s", &a, &b, flags) != 3) break;
if (a <= (size_t)ptr && (size_t)ptr < b){
res = flags[1] == 'w';
}
for(;;){
int c = fgetc(maps);
if(c == '\n' || c == EOF) break;
}
}
fclose(maps);
return res;
}
static void check(const char *format, const char *fname){
int mw = is_mapping_writable(format);
if(mw == -1){
fprintf(stderr, "FORMATCHECK: ERROR could not determine if %p is writable\n", format);
}
else if(mw == 1){
fprintf(stderr, "FORMATCHECK: ATTENTION: %s format string at %p is writable!\n", fname, format);
fprintf(stderr, "FORMATCHECK: VALUE: \"%s\"\n", format);
}
}
// a guard to avoid recursion
static __thread bool already_running = false;
// printing
// vfprintf and vsprintf do the check and use dlsym to call original function
// others are wrappers around vfprintf and vsprintf
int vfprintf(FILE *stream, const char *format, va_list ap){
if(!already_running){
already_running = true;
check(format, "printf");
already_running = false;
}
int (*vfprintf_ori)(FILE *, const char*, va_list) = dlsym(RTLD_NEXT, "vfprintf");
return vfprintf_ori(stream, format, ap);
}
int fprintf(FILE *stream, const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vfprintf(stream, format, ap);
va_end(ap);
return ret;
}
int printf(const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vfprintf(stdout, format, ap);
va_end(ap);
return ret;
}
int vprintf(const char *format, va_list ap){
return vfprintf(stdout, format, ap);
}
int vsnprintf(char *str, size_t size, const char *format, va_list ap){
FILE *stream = fmemopen(str, size, "w");
int ret = vfprintf(stream, format, ap);
fclose(stream);
return ret;
}
int snprintf(char *str, size_t size, const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vsnprintf(str, size, format, ap);
va_end(ap);
return ret;
}
// sprintf can not be trivially realized with fmemopen + fprintf
// because of unbounded output size
// open_memstream + strcpy would be possible but copies all output
int vsprintf(char *str, const char *format, va_list ap){
if(!already_running){
already_running = true;
check(format, "printf");
already_running = false;
}
int (*vsprintf_ori)(char *, const char*, va_list) = dlsym(RTLD_NEXT, "vsprintf");
return vsprintf_ori(str, format, ap);
}
int sprintf(char *str, const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vsprintf(str, format, ap);
va_end(ap);
return ret;
}
// Scanning
// vfscanf and vsscanf do check and dlsym
int vfscanf(FILE *stream, const char *format, va_list ap){
if(!already_running){
already_running = true;
check(format, "scanf");
already_running = false;
}
int (*vfscanf_ori)(FILE *, const char*, va_list) = dlsym(RTLD_NEXT, "vfscanf");
return vfscanf_ori(stream, format, ap);
}
int fscanf(FILE *stream, const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vfscanf(stream, format, ap);
va_end(ap);
return ret;
}
int vscanf(const char *format, va_list ap){
return fscanf(stdin, format, ap);
}
int scanf(const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vfscanf(stdin, format, ap);
va_end(ap);
return ret;
}
int vsscanf(const char *str, const char *format, va_list ap){
if(!already_running){
already_running = true;
check(format, "scanf");
already_running = false;
}
int (*vsscanf_ori)(const char *, const char *, va_list) = dlsym(RTLD_NEXT, "vsscanf");
return vsscanf_ori(str, format, ap);
}
int sscanf(const char *str, const char *format, ...){
va_list ap;
va_start(ap, format);
int ret = vsscanf(str, format, ap);
va_end(ap);
return ret;
}
#ifdef FORMATCHECK_TEST
int main(){
printf("%d. safe!\n", 1);
char format[] = "%d. unsafe\n";
printf(format, 2);
char sformat[4], buf[4];
sprintf(sformat, "%%s");
sscanf(format, sformat, buf);
return 0;
}
#endif