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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* Copyright (C) 2004, 2005 Free Software Foundation.
Ensure builtin __vsnprintf_chk performs correctly. */
#include <stdarg.h>
extern void abort (void);
typedef __SIZE_TYPE__ size_t;
extern size_t strlen(const char *);
extern void *memcpy (void *, const void *, size_t);
extern char *strcpy (char *, const char *);
extern int memcmp (const void *, const void *, size_t);
extern void *memset (void *, int, size_t);
extern int vsnprintf (char *, size_t, const char *, va_list);
#include "chk.h"
const char s1[] = "123";
char p[32] = "";
char *s2 = "defg";
char *s3 = "FGH";
char *s4;
size_t l1 = 1;
static char buffer[32];
char * volatile ptr = "barf"; /* prevent constant propagation to happen when whole program assumptions are made. */
int
__attribute__((noinline))
test1_sub (int i, ...)
{
int ret = 0;
va_list ap;
va_start (ap, i);
switch (i)
{
case 0:
vsnprintf (buffer, 4, "foo", ap);
break;
case 1:
ret = vsnprintf (buffer, 4, "foo bar", ap);
break;
case 2:
vsnprintf (buffer, 32, "%s", ap);
break;
case 3:
ret = vsnprintf (buffer, 21, "%s", ap);
break;
case 4:
ret = vsnprintf (buffer, 4, "%d%d%d", ap);
break;
case 5:
ret = vsnprintf (buffer, 32, "%d%d%d", ap);
break;
case 6:
ret = vsnprintf (buffer, strlen (ptr) + 1, "%s", ap);
break;
case 7:
vsnprintf (buffer, l1 + 31, "%d - %c", ap);
break;
case 8:
vsnprintf (s4, l1 + 6, "%d - %c", ap);
break;
}
va_end (ap);
return ret;
}
void
__attribute__((noinline))
test1 (void)
{
chk_calls = 0;
/* vsnprintf_disallowed = 1; */
memset (buffer, 'A', 32);
test1_sub (0);
if (memcmp (buffer, "foo", 4) || buffer[4] != 'A')
abort ();
memset (buffer, 'A', 32);
if (test1_sub (1) != 7)
abort ();
if (memcmp (buffer, "foo", 4) || buffer[4] != 'A')
abort ();
vsnprintf_disallowed = 0;
memset (buffer, 'A', 32);
test1_sub (2, "bar");
if (memcmp (buffer, "bar", 4) || buffer[4] != 'A')
abort ();
memset (buffer, 'A', 32);
if (test1_sub (3, "bar") != 3)
abort ();
if (memcmp (buffer, "bar", 4) || buffer[4] != 'A')
abort ();
memset (buffer, 'A', 32);
if (test1_sub (4, (int) l1, (int) l1 + 1, (int) l1 + 12) != 4)
abort ();
if (memcmp (buffer, "121", 4) || buffer[4] != 'A')
abort ();
memset (buffer, 'A', 32);
if (test1_sub (5, (int) l1, (int) l1 + 1, (int) l1 + 12) != 4)
abort ();
if (memcmp (buffer, "1213", 5) || buffer[5] != 'A')
abort ();
if (chk_calls)
abort ();
memset (buffer, 'A', 32);
test1_sub (6, ptr);
if (memcmp (buffer, "barf", 5) || buffer[5] != 'A')
abort ();
memset (buffer, 'A', 32);
test1_sub (7, (int) l1 + 27, *ptr);
if (memcmp (buffer, "28 - b\0AAAAA", 12))
abort ();
if (chk_calls != 2)
abort ();
chk_calls = 0;
memset (s4, 'A', 32);
test1_sub (8, (int) l1 - 17, ptr[1]);
if (memcmp (s4, "-16 - \0AAA", 10))
abort ();
if (chk_calls)
abort ();
}
void
__attribute__((noinline))
test2_sub (int i, ...)
{
va_list ap;
struct A { char buf1[10]; char buf2[10]; } a;
char *r = l1 == 1 ? &a.buf1[5] : &a.buf2[4];
char buf3[20];
int j;
va_start (ap, i);
/* The following calls should do runtime checking
- length is not known, but destination is. */
switch (i)
{
case 0:
vsnprintf (a.buf1 + 2, l1, "%s", ap);
break;
case 1:
vsnprintf (r, l1 + 4, "%s%c", ap);
break;
case 2:
r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
vsnprintf (r, strlen (s2) - 2, "%c %s", ap);
break;
case 3:
r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
vsnprintf (r + 2, l1, s3 + 3, ap);
break;
case 4:
case 7:
r = buf3;
for (j = 0; j < 4; ++j)
{
if (j == l1 - 1)
r = &a.buf1[1];
else if (j == l1)
r = &a.buf2[7];
else if (j == l1 + 1)
r = &buf3[5];
else if (j == l1 + 2)
r = &a.buf1[9];
}
if (i == 4)
vsnprintf (r, l1, s2 + 4, ap);
else
vsnprintf (r, 1, "a", ap);
break;
case 5:
r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
vsnprintf (r, l1 + 3, "%s", ap);
break;
case 6:
vsnprintf (a.buf1 + 2, 4, "", ap);
break;
case 8:
vsnprintf (s4, 3, "%s %d", ap);
break;
}
va_end (ap);
}
/* Test whether compile time checking is done where it should
and so is runtime object size checking. */
void
__attribute__((noinline))
test2 (void)
{
/* The following calls should do runtime checking
- length is not known, but destination is. */
chk_calls = 0;
test2_sub (0, s3 + 3);
test2_sub (1, s3 + 3, s3[3]);
test2_sub (2, s2[2], s2 + 4);
test2_sub (3);
test2_sub (4);
test2_sub (5, s1 + 1);
if (chk_calls != 6)
abort ();
/* Following have known destination and known source length,
so if optimizing certainly shouldn't result in the checking
variants. */
chk_calls = 0;
/* vsnprintf_disallowed = 1; */
test2_sub (6);
test2_sub (7);
vsnprintf_disallowed = 0;
/* Unknown destination and source, no checking. */
test2_sub (8, s3, 0);
if (chk_calls)
abort ();
}
void
__attribute__((noinline))
test3_sub (int i, ...)
{
va_list ap;
struct A { char buf1[10]; char buf2[10]; } a;
char buf3[20];
va_start (ap, i);
/* The following calls should do runtime checking
- source length is not known, but destination is. */
switch (i)
{
case 0:
vsnprintf (&a.buf2[9], l1 + 1, "%c%s", ap);
break;
case 1:
vsnprintf (&a.buf2[7], l1 + 30, "%s%c", ap);
break;
case 2:
vsnprintf (&a.buf2[7], l1 + 3, "%d", ap);
break;
case 3:
vsnprintf (&buf3[17], l1 + 3, "%s", ap);
break;
case 4:
vsnprintf (&buf3[19], 2, "a", ap);
break;
case 5:
vsnprintf (&buf3[16], 5, "a", ap);
break;
}
va_end (ap);
}
/* Test whether runtime and/or compile time checking catches
buffer overflows. */
void
__attribute__((noinline))
test3 (void)
{
chk_fail_allowed = 1;
/* Runtime checks. */
if (__builtin_setjmp (chk_fail_buf) == 0)
{
test3_sub (0, s2[3], s2 + 4);
abort ();
}
if (__builtin_setjmp (chk_fail_buf) == 0)
{
test3_sub (1, s3 + strlen (s3) - 2, *s3);
abort ();
}
if (__builtin_setjmp (chk_fail_buf) == 0)
{
test3_sub (2, (int) l1 + 9999);
abort ();
}
if (__builtin_setjmp (chk_fail_buf) == 0)
{
test3_sub (3, "abc");
abort ();
}
/* This should be detectable at compile time already. */
if (__builtin_setjmp (chk_fail_buf) == 0)
{
test3_sub (4);
abort ();
}
if (__builtin_setjmp (chk_fail_buf) == 0)
{
test3_sub (5);
abort ();
}
chk_fail_allowed = 0;
}
void
main_test (void)
{
#ifndef __OPTIMIZE__
/* Object size checking is only intended for -O[s123]. */
return;
#endif
__asm ("" : "=r" (s2) : "0" (s2));
__asm ("" : "=r" (s3) : "0" (s3));
__asm ("" : "=r" (l1) : "0" (l1));
s4 = p;
test1 ();
test2 ();
test3 ();
}
|