代码转储,如果我试图调用一个开放的系统在信号处理程序



我需要在接收信号时模拟系统调用。我试图调用一个开放的系统调用,但结果是一个核心转储,我不明白为什么。

我使用的代码是:

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;
}
  • 首先:你不应该在信号处理程序中使用printf()。printf()不是信号安全的

  • 第二:你对open()的调用没有意义。您正在泄漏文件描述符,并且您甚至没有在之前使用而不是关闭它。

你通过调用信号处理程序中的不安全函数调用了未定义行为,除非你的信号没有中断不安全函数。

相关内容

最新更新