-1

Consider the following function that is supposed to validate passwords:

char *systemkey = ...... ;


int validate(char* key) { 
 char* k = malloc(16); 
 char* sk = malloc(16); 
 strcpy(sk,systemkey); 
 strcpy(k,key); 
 return (strncmp(k,sk,16) ==0);
}

If k and sk are allocated consecutively, that it's easy to break the function by supplying 2 identical blocks of 16 bytes each.

If I'm the one writing the compiler/malloc/free/OS, is there any way I can identify MOST of these type of hacks and prevent them?

EDIT

One possible solution is to put some sort of canary word between each two different allocations. Is there another way?

4

2 回答 2

0

如果我是编写编译器/malloc/free/OS 的人,有什么方法可以识别大多数此类黑客并防止它们?

一种可能性是使用“声音”静态分析器,如果使用得当,可以保证您的程序不会访问任何无效指针以进行任何执行。这是一个

另一种是使用动态检测。Valgrind就是这种方法的一个例子。

于 2013-07-04T21:12:12.490 回答
0

您希望操作系统使用的最好的方法是 malloc 的实现,它可以随机化它返回的内存。它不能防止溢出,但会使利用它们变得更加困难。对于大型分配,使用的一种技术是返回与页面末尾对齐的分配,并将下一页显式地未映射为保护页面。

您可以在此页面上阅读一些内容以及其中的链接,以了解 OpenBSD 如何实现 malloc 保护。据我所知,这是您可以从广泛使用的操作系统中的 malloc 获得的最佳效果。

于 2013-07-04T13:35:10.160 回答