I was reading an lwn article about an exploit: http://lwn.net/Articles/341773/

Being that I am writing posts this week about programming, and about my Fedora run down, thought people might find this interesting.

I wrote a little test code that fails on Ubuntu but works on Fedora 11 (based off lwn post):

#include <stdio.h>
#include <sys/mman.h>

int main(int argc, char **argv)
{
    // Try to write to memory location 0
    void *mem;
    mem = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);

    if(mem != NULL)
    {
        printf("Could not write to memory position 0\n");
    } else
    {
        printf("We can write to memory location 0\n");
    }

    sprintf((char *) mem, " This is a test\n");
    printf("Memory contents: %s\n", (char *)(mem + (sizeof(char))));
    return 0;
}

Fedora 11 results:

./a.out
We can write to memory location 0
Memory contents: This is a test

Ubuntu 9.04 results:

./a.out
Could not write to memory position 0
Segmentation fault

What does this mean?
As far as I can understand it, userspace programs segfault when trying to access data in the NULL (or 0) memory region. The kernel does not have this limitation. The author of the exploit said this is because GCC optimises out the null check. So if there is kernel code which references a pointer to 0, then you can have it run whatever you want. And in atleast 2.6.30, there is kernel code that does that.

Ubuntu does not let the userspace programs write to 0, but in F11 you can. Interesting stuff.

Related posts:

  1. Why?
  2. Python and real time graphical analysis
  3. Programming using IOCTL to interface with Linux kernel drivers
  4. Fedora 11 vs. Ubuntu 9.04
  5. Programming in C: Converting an Integer to Binary (int to bin)