-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for valid stack frame in longjmp.
If longjmp restores the stack frame to an address which is beyond the stack frame at the time of the longjmp call it would install an uninitialized stack frame. If compiled with _FORTIFY_SOURCE defined, longjmp will now bail out in this situation.
- Loading branch information
Ulrich Drepper
committed
May 16, 2009
1 parent
f1342e0
commit b50f8e4
Showing
17 changed files
with
306 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ libc { | |
GLIBC_2.8 | ||
GLIBC_2.9 | ||
GLIBC_2.10 | ||
GLIBC_2.11 | ||
%ifdef USE_IN_LIBIO | ||
HURD_CTHREADS_0.3 | ||
%endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Copyright (C) 2009 Free Software Foundation, Inc. | ||
This file is part of the GNU C Library. | ||
The GNU C Library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
The GNU C Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with the GNU C Library; if not, write to the Free | ||
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
02111-1307 USA. */ | ||
|
||
#include <setjmp.h> | ||
|
||
// XXX Should move to include/setjmp.h | ||
extern void ____longjmp_chk (__jmp_buf __env, int __val) | ||
__attribute__ ((__noreturn__)); | ||
|
||
#define __longjmp ____longjmp_chk | ||
#define __libc_siglongjmp __longjmp_chk | ||
|
||
#include <setjmp/longjmp.c> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <paths.h> | ||
#include <setjmp.h> | ||
#include <signal.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
static jmp_buf b; | ||
|
||
|
||
static void | ||
__attribute__ ((noinline)) | ||
f (void) | ||
{ | ||
char buf[1000]; | ||
asm volatile ("" : "=m" (buf)); | ||
|
||
if (setjmp (b) != 0) | ||
{ | ||
puts ("second longjmp succeeded"); | ||
exit (1); | ||
} | ||
} | ||
|
||
|
||
static bool expected_to_fail; | ||
|
||
|
||
static void | ||
handler (int sig) | ||
{ | ||
if (expected_to_fail) | ||
_exit (0); | ||
else | ||
{ | ||
static const char msg[] = "unexpected longjmp failure\n"; | ||
TEMP_FAILURE_RETRY (write (STDOUT_FILENO, msg, sizeof (msg) - 1)); | ||
_exit (1); | ||
} | ||
} | ||
|
||
|
||
int | ||
main (void) | ||
{ | ||
struct sigaction sa; | ||
sa.sa_handler = handler; | ||
sa.sa_flags = 0; | ||
sigemptyset (&sa.sa_mask); | ||
|
||
sigaction (SIGABRT, &sa, NULL); | ||
|
||
/* Avoid all the buffer overflow messages on stderr. */ | ||
int fd = open (_PATH_DEVNULL, O_WRONLY); | ||
if (fd == -1) | ||
close (STDERR_FILENO); | ||
else | ||
{ | ||
dup2 (fd, STDERR_FILENO); | ||
close (fd); | ||
} | ||
setenv ("LIBC_FATAL_STDERR_", "1", 1); | ||
|
||
|
||
expected_to_fail = false; | ||
|
||
if (setjmp (b) == 0) | ||
{ | ||
longjmp (b, 1); | ||
/* NOTREACHED */ | ||
printf ("first longjmp returned\n"); | ||
return 1; | ||
} | ||
|
||
|
||
expected_to_fail = true; | ||
|
||
f (); | ||
longjmp (b, 1); | ||
|
||
puts ("second longjmp returned"); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include <setjmp/bits/setjmp2.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* Checking macros for setjmp functions. | ||
* Copyright (C) 2009 Free Software Foundation, Inc. | ||
* This file is part of the GNU C Library. | ||
* | ||
* The GNU C Library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* The GNU C Library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with the GNU C Library; if not, write to the Free | ||
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
* 02111-1307 USA. */ | ||
|
||
#ifndef _SETJMP_H | ||
# error "Never include <bits/setjmp2.h> directly; use <setjmp.h> instead." | ||
#endif | ||
|
||
/* Variant of the longjmp functions which perform some sanity checking. */ | ||
#ifdef __REDIRECT_NTH | ||
extern void __REDIRECT_NTH (longjmp, | ||
(struct __jmp_buf_tag __env[1], int __val), | ||
__longjmp_chk) __attribute__ ((__noreturn__)); | ||
extern void __REDIRECT_NTH (_longjmp, | ||
(struct __jmp_buf_tag __env[1], int __val), | ||
__longjmp_chk) __attribute__ ((__noreturn__)); | ||
extern void __REDIRECT_NTH (siglongjmp, | ||
(struct __jmp_buf_tag __env[1], int __val), | ||
__longjmp_chk) __attribute__ ((__noreturn__)); | ||
#else | ||
extern void __longjmp_chk (struct __jmp_buf_tag __env[1], int __val), | ||
__THROW __attribute__ ((__noreturn__)); | ||
# define longjmp __longjmp_chk | ||
# define _longjmp __longjmp_chk | ||
# define siglongjmp __longjmp_chk | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* Copyright (C) 2001,2004,2005,2006,2009 Free Software Foundation, Inc. | ||
This file is part of the GNU C Library. | ||
The GNU C Library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
The GNU C Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with the GNU C Library; if not, write to the Free | ||
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
02111-1307 USA. */ | ||
|
||
.section .rodata.str1.1,"aMS",@progbits,1 | ||
.type longjmp_msg,@object | ||
longjmp_msg: | ||
.string "longjmp causes uninitialized stack frame" | ||
.size longjmp_msg, .-longjmp_msg | ||
|
||
|
||
#define __longjmp ____longjmp_chk | ||
|
||
#ifdef PIC | ||
# define CALL_FAIL movl %ebx, %ecx; \ | ||
cfi_register(%ebx,%ecx); \ | ||
LOAD_PIC_REG (bx); \ | ||
leal longjmp_msg@GOTOFF(%ebx), %eax; \ | ||
call __GI___fortify_fail@PLT | ||
#else | ||
# define CALL_FAIL movl $longjmp_msg, %eax; \ | ||
call __fortify_fail | ||
#endif | ||
|
||
#define CHECK_ESP(reg) \ | ||
cmpl reg, %esp; \ | ||
jbe .Lok; \ | ||
CALL_FAIL; \ | ||
.Lok: | ||
|
||
#include "__longjmp.S" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.