Code Coherency

Posted on Fri 17 June 2005 in general

Have you ever written a piece of code that doesn't behave like it should? A failure thats so unexpected you start to doubt your own abilities? The majority of this morning has been taken up with following up on some oddness I saw last
night which made me blink. In the end I had 2 other people look at my code to make sure I wasn't going mad. Consider the following generic Un*x code:
/*
** Code that don't behave quite what like it ought to
*/

#include
#include      /* memset() */
#include    /* mmap() */

int main(int argc, char **argv)
{
    int pg_sz = getpagesize();
    void *page;

    page  = mmap(0,pg_sz,PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);

    memset(page,'w',pg_sz);

    printf("data = %c\n", *(char *) (page));
}

Try it on your own favourite POSIX machine and let me know if it doesn't segfault. I am still trying to work out if this is exploitable in some way.