sortix-mirror/libc/ssp/__stack_chk_fail.c

61 lines
1.7 KiB
C
Raw Normal View History

2014-10-21 22:00:33 +00:00
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014, 2015.
This file is part of the Sortix C Library.
The Sortix 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 3 of the License, or (at your
option) any later version.
The Sortix 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 Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
2016-02-28 11:11:02 +00:00
ssp/__stack_chk_fail.c
2014-10-21 22:00:33 +00:00
Abnormally terminate the process on stack smashing.
*******************************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <scram.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <__/wordsize.h>
2016-02-28 11:11:02 +00:00
#ifdef __is_sortix_libk
#include <libk.h>
2014-10-21 22:00:33 +00:00
#endif
#if __WORDSIZE == 32
#define STACK_CHK_GUARD 0x01234567
#elif __WORDSIZE == 64
#define STACK_CHK_GUARD 0x0123456789ABCDEF
#endif
2016-02-28 11:11:02 +00:00
#ifdef __is_sortix_libk
2014-10-21 22:00:33 +00:00
/* TODO: Have this filled in by the program loader. */
#else
/* TODO: Have this filled in by the boot loader. */
#endif
2016-02-28 11:11:02 +00:00
uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
2014-10-21 22:00:33 +00:00
2016-02-28 11:11:02 +00:00
__attribute__((noreturn))
2014-10-21 22:00:33 +00:00
void __stack_chk_fail(void)
{
2016-02-28 11:11:02 +00:00
#ifdef __is_sortix_libk
libk_stack_chk_fail();
2014-10-21 22:00:33 +00:00
#else
2016-02-28 11:11:02 +00:00
scram(SCRAM_STACK_SMASH, NULL);
2014-10-21 22:00:33 +00:00
#endif
}