Note: I think this is a mistake in the article.
The article's code:
Code: Select all
void * __stack_chk_guard = NULL;
void __stack_chk_guard_setup()
{
unsigned char * p;
p = (unsigned char *)&__stack_chk_guard; // *** Notice that this takes the address of __stack_chk_guard ***
/* If you have the ability to generate random numbers in your kernel then use them,
otherwise for 32-bit code: */
*p = 0x00000aff; // *** p is &__stack_chk_guard so *p writes to __stack_chk_guard rather than *__stack_chk_guard ***
}
void __attribute__((noreturn)) __stack_chk_fail()
{
/* put your panic function or similar in here */
unsigned char * vid = (unsigned char *)0xB8000;
vid[1] = 7;
for(;;)
vid[0]++;
}
Here is the code that I'm using:
Code: Select all
void* __stack_chk_guard = NULL;
void __stack_chk_guard_setup(void)
{
(*(uint32_t*)__stack_chk_guard) = 0x00000AFF; // Notice that this sets the value of __stack_chk_guard.
// Also notice that it's uint32_t, not unsigned char (as in the article) which causes overflow.
}
void __noreturn __stack_chk_fail()
{
panic("stack smashing attempt detected.\n");
}