I have write a program with 3 threads using pthread in C++. When buffer overflow occurs in one of the threads, whole the program terminates and other threads couldn't to be run and this message is shown: *** stack smashing detected ***: ./a.out terminated
I want to stack smashing only kills the thread that BOF occurs in it and other threads remain alive. So, I try to ignore signals, but it didn't solve my problem.
Here is my program:
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <signal.h>
#include <string.h>
using namespace std;
int a = 0;
void sig_func(int sig)
{
}
void *func (void *arg)
{
int c = a++;
cout << "start thread " << c << endl;
if (c == 1)
{
char stuff[8];
strcpy(stuff, "123456789");
}
cout << "end thread " << c << endl;
}
int main ()
{
pthread_t tid1, tid2, tid3;
for(int i = 1; i <=31 ; i++) //this line ignores all signals from 1 to 31.
signal(i,sig_func);
pthread_create (&tid1, 0, func, 0);
sleep (1);
pthread_create (&tid2, 0, func, 0);
sleep (1);
pthread_create (&tid3, 0, func, 0);
sleep (1);
return 0;
}
when I compile it with g++ a.cpp -lpthread
, the output is like this:
start thread 0
end thread 0
start thread 1
end thread 1
*** stack smashing detected ***: ./a.out terminated
Aborted (core dumped)
Is there any way to stack smashing only leads to killing the thread that BOF occurs in it, and the program doesn't terminate?
Note that I don't want to compile my program with -fno-stack-protector
option to avoid protection by canaries.