Giuseppe Pes
Giuseppe Pes

Reputation: 7912

Code dump if I try to invoke a open system within signal handler

I need to emulate system call when I receive a signal. I tried to invoke a open system call but the reuslt is a core dump and I dont understand why.

the code I am using is this :

static void emulator(int nr, siginfo_t *siginfo, void *void_context)
{
        ucontext_t *ctx = (ucontext_t *)(void_context);

        int syscall_n;
        char *buf;
        ssize_t bytes;
        size_t len;
        long long int syscall_addr;

        long int arg0=0,arg1=0,arg2=0,arg3=0,arg4=0,arg5=0, ret=0;


        if (siginfo->si_code != SYS_SECCOMP)
                return;
        if (!ctx)
                return;

        syscall_n = ctx->uc_mcontext.gregs[REG_SYSCALL];
        arg0 = ctx->uc_mcontext.gregs[REG_ARG0];
        arg1 = ctx->uc_mcontext.gregs[REG_ARG1];
        arg2 = ctx->uc_mcontext.gregs[REG_ARG2];
        arg3 = ctx->uc_mcontext.gregs[REG_ARG3];
        arg4 = ctx->uc_mcontext.gregs[REG_ARG4];
        arg5 = ctx->uc_mcontext.gregs[REG_ARG5];


        syscall_addr= (long int)siginfo->si_call_addr;

        printf("Arg0 = %d \n", arg0);
        printf("Arg1 = %d \n", arg1);
        printf("Arg2 = %d \n", arg2);
        printf("Arg3 = %d \n", arg3);
        printf("Arg4 = %d \n", arg4);
        printf("Arg5 = %d \n", arg5);


        //ret=syscall(syscall_n, arg0,arg1,arg2,arg3,arg4,arg5);                                                                                                                                                                                                                

        //ret=syscall(SYS_open,"testfile.txt", 2);                                                                                                                                                                                                                              

        open("test.p", O_RDWR);

        getpid();
        ctx->uc_mcontext.gregs[REG_RESULT]=ret;

        return;
}

static int install_emulator(void)
{
        struct sigaction act;
        sigset_t mask;
        memset(&act, 0, sizeof(act));
        sigemptyset(&mask);
        sigaddset(&mask, SIGSYS);

        act.sa_sigaction = &emulator;
        /*specify to use sigaction as handler*/
        act.sa_flags = SA_SIGINFO;
        if (sigaction(SIGSYS, &act, NULL) < 0) {
                perror("sigaction");
                return -1;
        }
        if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
                perror("sigprocmask");
                return -1;
        }
        return 0;

}

Upvotes: 0

Views: 197

Answers (2)

Demi
Demi

Reputation: 3611

You have invoked undefined behavior by calling an unsafe function in a signal handler, unless you the signal did not interrupt an unsafe function.

Upvotes: 0

joop
joop

Reputation: 4503

  • First: you should not use printf() inside a signal handler. printf() is not signal safe.

  • Second: your call to open() makes no sense. You are leaking the filedescriptor, and you are not even using it before not closing it.

[posted as an answer, since I cannot comment yet]

Upvotes: 2

Related Questions