Following this Interfacing Linux Signals article, i have been trying to use sys_rt_sigaction
in amd64, but always get memory access error when sending the signal. struct sigaction works when using C/C++ function sigaction
.
What is wrong in sys_rt_sigaction
call?
C/C++ with ASM code:
#include<signal.h>
#include<stdio.h>
#include<time.h>
void handler(int){printf("handler\n");}
void restorer(){asm volatile("mov $15,%%rax\nsyscall":::"rax");}
struct sigaction act{handler};
timespec ts{10,0};
int main(){
act.sa_flags=0x04000000;
act.sa_restorer=&restorer;
//*
asm volatile("\
mov $13,%%rax\n\
mov %0,%%rdi\n\
mov %1,%%rsi\n\
mov %2,%%rdx\n\
mov $8,%%r10\n\
syscall\n\
mov %%rax,%%rdi\n\
mov $60,%%rax\n\
#syscall\n\
"::"i"(7),"p"(&act),"p"(0):"rax","rdi","rsi","rdx","r10");
/**/
/*
sigaction(7,&act,0);
/**/
nanosleep(&ts,0);
}
Compile
g++ -o bin -std=c++11
g++ -o bin -std=c++11 -no-pie
Send signal
kill -7 `pidof bin`
In x86-64 linux, it's mandatory to supply a
sa_restorer
and you haven't done so.The relevant part of kernel source:
The C library wrapper does this for you:
With the updated code you do indeed have a restorer, but you have two problems: it's broken and you pass it wrong. Looking at the above mentioned C library source you can find this comment:
Also, you can't have a C++ function as restorer due to the function prologue. Furthermore, calling
printf
from a signal handler is not supported (but works here). Finally, as David Wohlferd pointed out, your clobbers are wrong. All in all, the following could be a reworked version:It's still hacky, and you shouldn't really be doing it this way, obviously.