1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 static void do_prepare (void);
29 static int do_test (void);
30 #define PREPARE(argc, argv) do_prepare ()
31 #define TEST_FUNCTION do_test ()
32 #include "../test-skeleton.c"
37 int temp_fd
= create_temp_file ("tst-chk1.", &temp_filename
);
40 printf ("cannot create temporary file: %m\n");
44 const char *strs
= "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
45 if (write (temp_fd
, strs
, strlen (strs
)) != strlen (strs
))
47 puts ("could not write test strings into file");
48 unlink (temp_filename
);
53 volatile int chk_fail_ok
, ret
;
62 longjmp (chk_fail_buf
, 1);
71 const char *str1
= "JIHGFEDCBA";
72 const char *str2
= "F";
73 const char *str3
= "%s%n%s%n";
74 const char *str4
= "Hello, ";
75 const char *str5
= "World!\n";
81 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
82 #define CHK_FAIL_START \
84 if (! setjmp (chk_fail_buf)) \
86 #define CHK_FAIL_END \
90 #if __USE_FORTIFY_LEVEL >= 2
91 #define CHK_FAIL2_START CHK_FAIL_START
92 #define CHK_FAIL2_END CHK_FAIL_END
94 #define CHK_FAIL2_START
102 sa
.sa_handler
= handler
;
104 sigemptyset (&sa
.sa_mask
);
106 sigaction (SIGABRT
, &sa
, NULL
);
108 struct A
{ char buf1
[9]; char buf2
[1]; } a
;
110 printf ("Test checking routines at fortify level %d\n",
111 #ifdef __USE_FORTIFY_LEVEL
112 (int) __USE_FORTIFY_LEVEL
118 /* These ops can be done without runtime checking of object size. */
119 memcpy (buf
, "abcdefghij", 10);
120 memmove (buf
+ 1, buf
, 9);
121 if (memcmp (buf
, "aabcdefghi", 10))
124 if (mempcpy (buf
+ 5, "abcde", 5) != buf
+ 10 || memcmp (buf
, "aabcdabcde", 10))
127 memset (buf
+ 8, 'j', 2);
128 if (memcmp (buf
, "aabcdabcjj", 10))
131 strcpy (buf
+ 4, "EDCBA");
132 if (memcmp (buf
, "aabcEDCBA", 10))
135 if (stpcpy (buf
+ 8, "F") != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
138 strncpy (buf
+ 6, "X", 4);
139 if (memcmp (buf
, "aabcEDX\0\0", 10))
142 if (sprintf (buf
+ 7, "%s", "67") != 2 || memcmp (buf
, "aabcEDX67", 10))
145 if (snprintf (buf
+ 7, 3, "%s", "987654") != 6
146 || memcmp (buf
, "aabcEDX98", 10))
149 /* These ops need runtime checking, but shouldn't __chk_fail. */
150 memcpy (buf
, "abcdefghij", l0
+ 10);
151 memmove (buf
+ 1, buf
, l0
+ 9);
152 if (memcmp (buf
, "aabcdefghi", 10))
155 if (mempcpy (buf
+ 5, "abcde", l0
+ 5) != buf
+ 10 || memcmp (buf
, "aabcdabcde", 10))
158 memset (buf
+ 8, 'j', l0
+ 2);
159 if (memcmp (buf
, "aabcdabcjj", 10))
162 strcpy (buf
+ 4, str1
+ 5);
163 if (memcmp (buf
, "aabcEDCBA", 10))
166 if (stpcpy (buf
+ 8, str2
) != buf
+ 9 || memcmp (buf
, "aabcEDCBF", 10))
169 strncpy (buf
+ 6, "X", l0
+ 4);
170 if (memcmp (buf
, "aabcEDX\0\0", 10))
173 if (sprintf (buf
+ 7, "%d", num1
) != 2 || memcmp (buf
, "aabcEDX67", 10))
176 if (snprintf (buf
+ 7, 3, "%d", num2
) != 6 || memcmp (buf
, "aabcEDX98", 10))
181 if (memcmp (buf
, "aabcEDX9A", 10))
185 strncat (buf
, "ZYXWV", l0
+ 2);
186 if (memcmp (buf
, "aabcEDXZY", 10))
189 memcpy (a
.buf1
, "abcdefghij", l0
+ 10);
190 memmove (a
.buf1
+ 1, a
.buf1
, l0
+ 9);
191 if (memcmp (a
.buf1
, "aabcdefghi", 10))
194 if (mempcpy (a
.buf1
+ 5, "abcde", l0
+ 5) != a
.buf1
+ 10
195 || memcmp (a
.buf1
, "aabcdabcde", 10))
198 memset (a
.buf1
+ 8, 'j', l0
+ 2);
199 if (memcmp (a
.buf1
, "aabcdabcjj", 10))
202 #if __USE_FORTIFY_LEVEL < 2
203 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
204 and sufficient GCC support, as the string operations overflow
205 from a.buf1 into a.buf2. */
206 strcpy (a
.buf1
+ 4, str1
+ 5);
207 if (memcmp (a
.buf1
, "aabcEDCBA", 10))
210 if (stpcpy (a
.buf1
+ 8, str2
) != a
.buf1
+ 9 || memcmp (a
.buf1
, "aabcEDCBF", 10))
213 strncpy (a
.buf1
+ 6, "X", l0
+ 4);
214 if (memcmp (a
.buf1
, "aabcEDX\0\0", 10))
217 if (sprintf (a
.buf1
+ 7, "%d", num1
) != 2 || memcmp (a
.buf1
, "aabcEDX67", 10))
220 if (snprintf (a
.buf1
+ 7, 3, "%d", num2
) != 6
221 || memcmp (a
.buf1
, "aabcEDX98", 10))
224 a
.buf1
[l0
+ 8] = '\0';
225 strcat (a
.buf1
, "A");
226 if (memcmp (a
.buf1
, "aabcEDX9A", 10))
229 a
.buf1
[l0
+ 7] = '\0';
230 strncat (a
.buf1
, "ZYXWV", l0
+ 2);
231 if (memcmp (a
.buf1
, "aabcEDXZY", 10))
236 #if __USE_FORTIFY_LEVEL >= 1
237 /* Now check if all buffer overflows are caught at runtime. */
240 memcpy (buf
+ 1, "abcdefghij", l0
+ 10);
244 memmove (buf
+ 2, buf
+ 1, l0
+ 9);
248 p
= mempcpy (buf
+ 6, "abcde", l0
+ 5);
252 memset (buf
+ 9, 'j', l0
+ 2);
256 strcpy (buf
+ 5, str1
+ 5);
260 p
= stpcpy (buf
+ 9, str2
);
264 strncpy (buf
+ 7, "X", l0
+ 4);
268 sprintf (buf
+ 8, "%d", num1
);
272 snprintf (buf
+ 8, l0
+ 3, "%d", num2
);
275 memcpy (buf
, str1
+ 2, l0
+ 9);
280 memcpy (buf
, str1
+ 3, l0
+ 8);
282 strncat (buf
, "ZYXWV", l0
+ 3);
286 memcpy (a
.buf1
+ 1, "abcdefghij", l0
+ 10);
290 memmove (a
.buf1
+ 2, a
.buf1
+ 1, l0
+ 9);
294 p
= mempcpy (a
.buf1
+ 6, "abcde", l0
+ 5);
298 memset (a
.buf1
+ 9, 'j', l0
+ 2);
301 #if __USE_FORTIFY_LEVEL >= 2
308 strcpy (a
.buf1
+ (O
+ 4), str1
+ 5);
312 p
= stpcpy (a
.buf1
+ (O
+ 8), str2
);
316 strncpy (a
.buf1
+ (O
+ 6), "X", l0
+ 4);
320 sprintf (a
.buf1
+ (O
+ 7), "%d", num1
);
324 snprintf (a
.buf1
+ (O
+ 7), l0
+ 3, "%d", num2
);
327 memcpy (a
.buf1
, str1
+ (3 - O
), l0
+ 8 + O
);
329 strcat (a
.buf1
, "AB");
332 memcpy (a
.buf1
, str1
+ (4 - O
), l0
+ 7 + O
);
334 strncat (a
.buf1
, "ZYXWV", l0
+ 3);
338 /* Now checks for %n protection. */
340 /* Constant literals passed directly are always ok
341 (even with warnings about possible bugs from GCC). */
343 if (sprintf (buf
, "%s%n%s%n", str2
, &n1
, str2
, &n2
) != 2
344 || n1
!= 1 || n2
!= 2)
347 /* In this case the format string is not known at compile time,
348 but resides in read-only memory, so is ok. */
349 if (snprintf (buf
, 4, str3
, str2
, &n1
, str2
, &n2
) != 2
350 || n1
!= 1 || n2
!= 2)
353 strcpy (buf2
+ 2, "%n%s%n");
354 /* When the format string is writable and contains %n,
355 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
357 if (sprintf (buf
, buf2
, str2
, &n1
, str2
, &n1
) != 2)
362 if (snprintf (buf
, 3, buf2
, str2
, &n1
, str2
, &n1
) != 2)
366 /* But if there is no %n, even writable format string
369 if (sprintf (buf
, buf2
+ 4, str2
) != 1)
372 /* Constant literals passed directly are always ok
373 (even with warnings about possible bugs from GCC). */
374 if (printf ("%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
375 || n1
!= 7 || n2
!= 14)
378 /* In this case the format string is not known at compile time,
379 but resides in read-only memory, so is ok. */
380 if (printf (str3
, str4
, &n1
, str5
, &n2
) != 14
381 || n1
!= 7 || n2
!= 14)
384 strcpy (buf2
+ 2, "%n%s%n");
385 /* When the format string is writable and contains %n,
386 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
388 if (printf (buf2
, str4
, &n1
, str5
, &n1
) != 14)
392 /* But if there is no %n, even writable format string
395 if (printf (buf2
+ 4, str5
) != 7)
400 /* Constant literals passed directly are always ok
401 (even with warnings about possible bugs from GCC). */
402 if (fprintf (fp
, "%s%n%s%n", str4
, &n1
, str5
, &n2
) != 14
403 || n1
!= 7 || n2
!= 14)
406 /* In this case the format string is not known at compile time,
407 but resides in read-only memory, so is ok. */
408 if (fprintf (fp
, str3
, str4
, &n1
, str5
, &n2
) != 14
409 || n1
!= 7 || n2
!= 14)
412 strcpy (buf2
+ 2, "%n%s%n");
413 /* When the format string is writable and contains %n,
414 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
416 if (fprintf (fp
, buf2
, str4
, &n1
, str5
, &n1
) != 14)
420 /* But if there is no %n, even writable format string
423 if (fprintf (fp
, buf2
+ 4, str5
) != 7)
426 if (freopen (temp_filename
, "r", stdin
) == NULL
)
428 puts ("could not open temporary file");
432 if (gets (buf
) != buf
|| memcmp (buf
, "abcdefgh", 9))
434 if (gets (buf
) != buf
|| memcmp (buf
, "ABCDEFGHI", 10))
437 #if __USE_FORTIFY_LEVEL >= 1
439 if (gets (buf
) != buf
)
444 if (freopen (temp_filename
, "r", stdin
) == NULL
)
446 puts ("could not open temporary file");
450 if (fseek (stdin
, 9 + 10 + 11, SEEK_SET
))
452 puts ("could not seek in test file");
456 #if __USE_FORTIFY_LEVEL >= 1
458 if (gets (buf
) != buf
)