bss中的静态指针溢出

时间:2018-06-07 21:50:45

标签: pointers static overflow address-sanitizer

我在bss中使用静态指针溢出的w00w00练习。我将缓冲区和缓冲区指针放入静态结构中以强制溢出指针。否则,它将指针置于缓冲区之前并且不会发生溢出。

   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <string.h>
   #include <errno.h>

   #define BUFSIZE 16
   #define ADDRLEN 4 /* # of bytes in an address */


int main()
   {
        u_long diff;
    struct buf {
    char buf[BUFSIZE];
    char *bufptr  ;
    } ;

    static struct buf a;

    a.bufptr = a.buf, diff = (u_long)a.buf - (u_long)&a.bufptr;

    printf("bufptr (%p) = %p, buf = %p, diff = 0x%x (%d) bytes\n",
            &a.bufptr,a.bufptr, a.buf, diff, diff);

    memset(a.buf, 'A', (u_int)(diff + ADDRLEN));

    printf("bufptr (%p) = %p, buf = %p, diff = 0x%x (%d) bytes\n",
            &a.bufptr, a.bufptr, a.buf, diff, diff);

      return 0;
   }

当我使用AddressSanitizer

时,我正在收到此错误
==27643==ERROR: AddressSanitizer: negative-size-param: (size=-12)

SUMMARY: AddressSanitizer: negative-size-param ??:0 __asan_memset
==27643==ABORTING

是否有标志或方法强制溢出?

修改 我发现了问题。差异结果是-16。 我生气它看起来像这样

diff = (u_long)&a.bufptr - (u_long)a.buf

现在它工作正常。

1 个答案:

答案 0 :(得分:0)

尝试大于结构大小但不足以被解释为负数的大小:

memset(a.buf, 'A', 1024);

还要确保按照Asan FAQ中的说明使用-fno-common进行编译。