26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);<br />

}<br />

printf("\t[-] freeing int_ptr's heap memory...\n");<br />

free(int_ptr); // Freeing heap memory<br />

printf("\t[-] freeing char_ptr's heap memory...\n");<br />

free(char_ptr); // Freeing <strong>the</strong> o<strong>the</strong>r block <strong>of</strong> heap memory<br />

void *errorchecked_malloc(unsigned int size) { // An error-checked malloc() function<br />

void *ptr;<br />

ptr = malloc(size);<br />

if(ptr == NULL) {<br />

fprintf(stderr, "Error: could not allocate heap memory.\n");<br />

exit(-1);<br />

}<br />

return ptr;<br />

}<br />

The errorchecked_heap.c program is basically equivalent to <strong>the</strong><br />

previous heap_example.c code, except <strong>the</strong> heap memory allocation and<br />

error checking has been ga<strong>the</strong>red into a single function. The first line <strong>of</strong> code<br />

[void *errorchecked_malloc(unsigned int);] is <strong>the</strong> function prototype. This lets<br />

<strong>the</strong> compiler know that <strong>the</strong>re will be a function called errorchecked_malloc() that<br />

expects a single, unsigned integer argument and returns a void pointer. The<br />

actual function can <strong>the</strong>n be anywhere; in this case it is after <strong>the</strong> main() function.<br />

The function itself is quite simple; it just accepts <strong>the</strong> size in bytes to<br />

allocate and attempts to allocate that much memory using malloc(). If <strong>the</strong><br />

allocation fails, <strong>the</strong> error-checking code displays an error and <strong>the</strong> program<br />

exits; o<strong>the</strong>rwise, it returns <strong>the</strong> pointer to <strong>the</strong> newly allocated heap memory.<br />

This way, <strong>the</strong> custom errorchecked_malloc() function can be used in place <strong>of</strong><br />

anormal malloc(), eliminating <strong>the</strong> need for repetitious error checking afterward.<br />

This should begin to highlight <strong>the</strong> usefulness <strong>of</strong> programming with<br />

functions.<br />

0x280<br />

Building on Basics<br />

Once you understand <strong>the</strong> basic concepts <strong>of</strong> C programming, <strong>the</strong> rest is pretty<br />

easy. The bulk <strong>of</strong> <strong>the</strong> power <strong>of</strong> C comes from using o<strong>the</strong>r functions. In fact,<br />

if <strong>the</strong> functions were removed from any <strong>of</strong> <strong>the</strong> preceding programs, all that<br />

would remain are very basic statements.<br />

0x281<br />

File Access<br />

There are two primary ways to access files in C: file descriptors and filestreams.<br />

File descriptors use a set <strong>of</strong> low-level I/O functions, and filestreams are<br />

a higher-level form <strong>of</strong> buffered I/O that is built on <strong>the</strong> lower-level functions.<br />

Some consider <strong>the</strong> filestream functions easier to program with; however, file<br />

descriptors are more direct. In this book, <strong>the</strong> focus will be on <strong>the</strong> low-level<br />

I/O functions that use file descriptors.<br />

Programming 81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!