Monday, November 10, 2008

Writing to stdin

Remember, stdin is file descriptor 0.

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

int main(void)
{
char *output = "Hello, Stdin World!\n";

write(0, output, strlen(output));
return 0;
}


[prompt]$ gcc -o stdin-write stdin-write.c
[prompt]$ ./stdin-write 1>/dev/null 2>/dev/null
Hello, Stdin World!
[prompt]$

So you can write to stdin because it is opened Read/Write on linux. Probably also on other platforms as well. std{in,out,err} are just conventions for the names and uses of file descriptors 0, 1, and 2, and there is no technical restriction in place on them. Still, writing to stdin is unusual and unexpected.

Tuesday, January 22, 2008

Null Pointers

I've meant to write about this for a while. I wrote it, saved it as a draft and waited even longer to publish it.

Everyone learns dereferencing a NULL pointer crashes a program. However, I was reading a phrack (or maybe something else) article and a comment about an exploit made reference to a NULL pointer.

Curious, I messed around and wrote this:


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

int main(int argc, char *argv[]) {
long *addr;
addr = (long *)mmap((void *)0, 4096,
PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED | MAP_ANONYMOUS,
-1, 0);
addr = NULL;
*addr = 75;
return 0;
}


Typically, there is no memory mapped to address 0. Hence using a NULL pointer results in a Seg Fault. Above, we map a page of memory to address 0. Then we can reference it without a Seg Fault.

The above mentioned exploit was to have a Kernel structure set to a NULL pointer. Then when it is dereferenced, it actually accesses page zero inside the exploiting application's address space. Pretty nifty.

Like many others, I took for granted that dereferencing a NULL pointer equals a seg fault. However, that is just because typically there is no memory mapped to address 0. Typical doesn't mean never.

Shortly after making this discovery (this was a while ago) I found this: "Alan Cox indicated that it was desirable to be able to dynamically disable the feature because some (very) rare programs map the NULL pointer to speed up their walk through linked lists by avoiding NULL pointer checks." by Willy Tarreau http://kerneltrap.org/Linux/2.4.36-pre1_Preventing_NULL_Dereferences