我如何强制SIGILL被发送到我的计划? [英] How do I force a SIGILL to be sent to my program?

查看:106
本文介绍了我如何强制SIGILL被发送到我的计划?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着做一些讨厌的东西哈克与动态生成的code和我想要的操作系统,当它到达一个未知的运算code给我一个SIGILL。这将让我补充的元信息的层关于我的程序等。

不过,对于我的小测试程序,它似乎OS没有发送SIGILL,而是发送无论是SIGBUS或SIGSEGV。我猜这意味着该内存所在的页面有一个NX位设置就可以了。

如何使内存可执行的任何提示吗?

有关参考,在这里是我的测试程序:

 的#include<&stdio.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;无效SIGILL_handler(INT SIG)
{
    的printf(处理SIGILL \\ n);
}无效的typedef(* FUNC)(无效);诠释的main()
{
    信号(SIGILL,SIGILL_handler);    为int *坏=的malloc(16);
    memset的(坏,255,16);
    ((FUNC)坏)();    的printf(返回喜欢它没什么大不了的\\ n);    返回0;
}


解决方案

mprotect的 是你的朋友在这里。这是POSIX兼容(SVR4,POSIX.1-2001),所以应该在OS X和Linux的工作。

  INT页面大小=的sysconf(_SC_PAGE_SIZE);
如果(PAGESIZE == -1){
    PERROR(的sysconf);
    出口(1);
}/ *分配16对齐页* /
void *的坏= memalign可(页面大小,16页大小*);
如果(NULL ==坏){
    fprintf中(啊哈,出来的MEM: - (\\ n);
    出口(1);
}如果(-1 ==则mprotect(坏,16 *页面大小,PROT_READ | PROT_WRITE | PROT_EXEC)){
    PERROR(则mprotect);
    出口(1);
}

应该这样做。

2日编辑: memalign可的兼容性似乎不那么容易。我想尝试 memalign可 valloc 在OS X和Linux,如果没有工作,只要使用正规的的malloc 和足够的字节添加到返回的指针,以便它被对准:-)。

I'm try to do some nasty hacky things with dynamically generated code, and I want the OS to send me a SIGILL when it reaches an unknown opcode. This would let me add a layer of meta-information about my program and so on.

However, for my little test program, it seems the OS is not sending the SIGILL, but rather sends either a SIGBUS, or a SIGSEGV. I'm guessing this means that the page in which the memory is located has an NX bit set on it.

Any tips on how to make memory executable?

For reference, here is my test program:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

void SIGILL_handler(int sig)
{
    printf("Handling SIGILL\n");
}

typedef void(*FUNC)(void);

int main()
{
    signal(SIGILL, SIGILL_handler);

    int *bad = malloc(16);
    memset(bad, 255, 16);
    ((FUNC)bad)();

    printf("Returning like it's no big deal\n");

    return 0;
}

解决方案

mprotect is your friend here. It is POSIX compatible (SVr4, POSIX.1-2001), so it should work under OS X and Linux.

int pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == -1) {
    perror("sysconf");
    exit(1);
}

/* allocate 16 aligned pages */
void *bad = memalign(pagesize, 16 * pagesize);
if (NULL == bad) {
    fprintf("aah, out of mem :-(\n");
    exit(1);
}

if (-1 == mprotect(bad, 16 * pagesize, PROT_READ | PROT_WRITE | PROT_EXEC)) {
    perror("mprotect");
    exit(1);
}

should do it.

2nd edit: The compatibility of memalign seems not to be that easy. I'd try memalign, valloc under OS X and Linux and if neither work, just use regular malloc and add enough bytes to the returned pointer so that it is aligned :-).

这篇关于我如何强制SIGILL被发送到我的计划?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆