Buffer Overflow and Format String Overflow Vulnerabilities

Size: px
Start display at page:

Download "Buffer Overflow and Format String Overflow Vulnerabilities"

Transcription

1 Syracuse University SURFACE Electrical Engineering and Computer Science College of Engineering and Computer Science 2002 Buffer Overflow and Format String Overflow Vulnerabilities Kyung-suk Lhee Syracuse University Steve J. Chapin Syracuse University Follow this and additional works at: Part of the Computer Sciences Commons Recommended Citation Lhee, Kyung-suk and Chapin, Steve J., "Buffer Overflow and Format String Overflow Vulnerabilities" (2002). Electrical Engineering and Computer Science This Article is brought to you for free and open access by the College of Engineering and Computer Science at SURFACE. It has been accepted for inclusion in Electrical Engineering and Computer Science by an authorized administrator of SURFACE. For more information, please contact

2 SOFTWARE PRACTICE AND EXPERIENCE Softw. Pract. Exper. 2002; 00:1 7 Buffer Overflow and Format String Overflow Vulnerabilities Kyung-Suk Lhee and Steve J. Chapin, Center for Systems Assurance, Syracuse University, Syracuse, NY SUMMARY Buffer overflow vulnerabilities are among the most widespread of security problems. Numerous incidents of buffer overflow attacks have been reported and many solutions have been proposed, but a solution that is both complete and highly practical is yet to be found. Another kind of vulnerability called format string overflow has recently been found, and though not as popular as buffer overflow, format string overflow attacks are no less dangerous. This article surveys representative techniques of exploiting buffer overflow and format string overflow vulnerabilities and their currently available defensive measures. We also describe our buffer overflow detection technique that range checks the referenced buffers at run time. We augment executable files with type information of automatic buffers (local variables and parameters of functions) and static buffers (global variables in the data/bss section), and maintain the sizes of allocated heap buffers in order to detect an actual occurrence of buffer overflow. We describe a simple implementation with which we currently protect vulnerable copy functions in the C library. key words: Buffer overflow; format string overflow; array and pointer range checking; Linux, ELF 1. INTRODUCTION Buffer overflow vulnerability is currently one of the most serious security problems. Various buffer overflow techniques have been discovered and numerous incidents of buffer overflow attacks have been reported to date [9, 7, 43]. Many solutions have been proposed, but a solution that completely eliminates the buffer overflow vulnerability that is also compatible with the existing environment and performs well is yet to be found. Moreover, another kind of vulnerability to what is called format string overflow has recently been found. Although not as Correspondence to: Center for Systems Assurance, Syracuse University, Syracuse, NY klhee@ecs.syr.edu, chapin@ecs.syr.edu Received 31 May 2002 Copyright c 2002 John Wiley & Sons, Ltd. Revised 13 August 2002 Accepted 31 August 2002

3 2 K. LHEE AND S. J. CHAPIN popular as buffer overflow, format string overflow attacks are no less dangerous in effect than buffer overflow attacks. Vulnerability to buffer overflow and format string overflow is due to the characteristics of the C language. For example, an array in C is not a first-class object and is represented as a pointer. Hence, it is difficult for a compiler to check on whether an operation will overflow an array. C allows variadic functions such as string format functions. Since the number of arguments is not known at compile time, a string format function has to rely on the format string to figure it out at run time. Such characteristics are geared toward convenience and performance, and are therefore favored for numerous legacy applications. Such vulnerabilities can be eliminated by programmers through careful programming and by rigorous checking of array bounds and the like. However, it is unrealistic to assume that all programmers will follow such a strict programming practice since it would decrease the benefits of convenience and performance, just as it is unrealistic to assume that they will not make any programming mistakes. This article presents some of the well known techniques of exploiting buffer overflow and format string overflow vulnerabilities and their currently available defensive measures. Many exploitation techniques of buffer overflow and format string overflow vulnerabilities have been studied and published, but each of them focuses on a particular technique with platformspecific details and examples. We discuss them here collectively, considering only their core ideas, as an introduction to buffer overflow and format string overflow. The exploitation techniques in this article are by no means exhaustive (which would be impossible), but they are representative techniques that show the most important classes of attacks. Like the classification in [13], we loosely categorize the buffer overflow exploitation techniques based on the data structures and their associated algorithms that are frequent targets of most exploits. Most of those data structures are code pointers such as function activation record and function pointers (those used by programmers and those introduced implicitly by compiler and system libraries). We also discuss the internal data structure of the dynamic memory allocator (malloc), which is not a code pointer but can be used to influence code pointers. Defensive techniques against buffer overflow are divided into run-time techniques and compile-time analysis techniques. Run-time techniques include array/pointer range-checking techniques and techniques that check at certain points the integrity of systems (such as the integrity of memory space). Compile-time analysis techniques analyze source codes to detect possible buffer overflow. We discuss the advantages and disadvantages of run-time and compiletime techniques. We also describe our buffer overflow detection technique that range checks the referenced buffers at run time. We augment executable files with type information of automatic buffers (local variables and parameters of functions) and static buffers (global variables in the data/bss section), and maintain the sizes of allocated heap buffers in order to detect the actual occurrence of buffer overflow. Our discussion of format string overflow vulnerability then follows similarly, but in a smaller scale. Examples throughout this article assume Linux operating system on x86 architecture.

4 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 3 void func (char *string) { char buffer[32]; strcpy(buffer, string); } int main(int argc, char **argv) func(argv[1]); return 0; } Figure 1. A simple buffer overflow vulnerability. The calling function: 1. Pushes parameters onto the stack 2. Pushes the ess Function prologue of the called function: 1. Pushes the frame pointer 2. Frame pointer is assigned the stack pointer value 3. Stack pointer is advanced to make room for local variables Function epilogue of the called function: 1. Stack pointer is assigned the frame pointer value 2. Saved frame pointer is popped and assigned to the frame pointer 3. Return address is popped and assigned to the instruction counter Figure 2. A function-calling mechanism. 2. BUFFER OVERFLOW VULNERABILITY AND EXPLOITS Buffer overflow vulnerability in a program can be exploited to overwrite other important data that is adjacent to the buffer, eventually leading to the changing of normal program behavior. If the string in Figure 1 is longer than 31, then strcpy not only modifies the buffer but also the memory area next to it. The memory area next to the buffer holds important information, the ess. By overflowing the buffer we can alter the ess and thus the program flow. This example shows that, with the knowledge of the structure of memory space

5 4 K. LHEE AND S. J. CHAPIN and the algorithm associated with it, buffer overflow vulnerability can be used to change the intended program behavior. A buffer overflow exploitation requires: 1. Buffer overflow vulnerability in a program, which consists of buffers and operations that can overflow them. In this example it is the buffer and strcpy. 2. Some knowledge of memory structure of the program and algorithms associated with it. In this example it is the layout of function activation record in the stack and the function-calling mechanism in Figure 2. The knowledge of memory structure and the accompanying algorithm offer an opportunity to alter the program behavior, whereas the buffer overflow vulnerability provides a means to achieve it. Techniques in this section basically discuss these two requirements. We categorize buffer overflow exploitation techniques based on data structures on which most exploits target, as follows. 1. Function activation record (the ess and the saved frame pointer) 2. Function pointers (function pointer variables and other function pointers that are implicitly used by the system) 3. Internal data structure in dynamic memory allocator (malloc) Those data structures are code pointers except for the malloc. We focus on exploiting code pointers (mostly in order to spawn a root shell). Other data structures can be the primary target, but such cases are rarer in practice and exploits that alter code pointers are more illustrative since they immediately take control of the target system Exploits on function activation record Stack smashing attack This is the most popular technique, by [1, 26], for exploiting the simple vulnerability in Figure 1 (a vulnerable strcpy). It overflows the buffer with an attack string that consists of 1) the shellcode, and 2) the memory address where the shellcode is to be copied. The shellcode is an array of character-coded assembly instructions that performs execve( /bin/sh ) to spawn a shell. The memory address (address of the shellcode) needs to be aligned to overwrite the ess. The result is that when the function returns it will jump to the shellcode and spawn a shell. If the process has a root privilege, then it becomes a root shell. Figure 3 illustrates this. For a local exploitation, an attack can be performed by a small program that spawns the vulnerable program with the attack code as its command-line argument (or an environment variable). For a remote exploitation, the attack code can be delivered through an I/O channel (if a vulnerable function accepts a string from I/O). For the exploit in Figure 3 to succeed, we need to know the address of the buffer, because it is the memory address with which we overwrite the ess. Although the address of the buffer on the stack is usually unknown, there are ways to find (or guess) it. For example, we can observe it by running the vulnerable program in a debugger. For the same set of input in a similar environment, the

6 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 5 addr of buffer addr of buffer stack grows down shellcode shellcode nop s argv[1] nop s addr of buffer shellcode address grows up buffer MSB LSB addr of buffer nop s Figure 3. The stack smashing attack that exploits the program in Figure 1. It shows the stack frame before and after strcpy(buffer, argv[1]). We assume that the stack grows down and the address of buffer is that of its least significant byte (little-endian architecture). program is likely to follow the same path and yield the same address for the buffer. Even if not, the observed value would still provide a good starting point for guessing the right value. Guessing the value is made easier by prepending no-op s at the shellcode so that a small margin of error in guessing the exact address would not matter much. In general, a successful exploitation may require detailed information about the target program and its run-time behavior. Such information can be discovered in various ways, including the following. 1. Documentation (manuals, technical reports, etc.) 2. Source code 3. Reading binary file or core dump using utility programs such as objdump and nm 4. Using operating system facilities such as /proc file system, ptrace and strace 5. Running programs in a debugger Even dynamic information such as the ess on the stack or the address of a shared library function can be observed or guessed by running the program in a similar environment, since programs usually follow deterministic algorithms. For example, the stack is likely to grow in the same pattern if the same input is given to the program. A program normally maps its shared libraries in the same order at the same starting address, yielding the same addresses.

7 6 K. LHEE AND S. J. CHAPIN LSB bogus ret addr buffer placeholder shellcode Figure 4. The frame pointer overwrite exploit, showing the stack frame before and after the attack. The attack code overwrites the buffer and the least significant byte of the saved frame pointer The frame pointer overwrite This technique, by [21], is similar to the stack-smashing attack, but it alters the function activation record in a different way. It is based on the fact that a function locates the return address using the frame pointer (function epilogue in Figure 2). By altering the frame pointer we can trick the function into popping a wrong word instead of the ess. Since we cannot directly change the frame pointer (a register), we alter the saved frame pointer in the stack instead. Note that in the stack-smashing attack we alter the ess, since we cannot directly change the program counter. Figure 4 illustrates this exploit. After a buffer is overflowed and the saved frame pointer is altered, the overflowed function returns normally to its calling function, but the calling function now has the wrong frame pointer. When the caller itself returns, it will pop the word of our choosing into the program counter (for example, the address of the shellcode). The attack code consists of the shellcode, a bogus ess (address to the shellcode), and a byte that alters the least significant byte of the saved frame pointer. Since we need to change the saved frame pointer by a only small margin, altering its least significant byte suffices. In this example the buffer is the first local variable that is adjacent to the saved frame pointer. Assuming little endian byte-ordering in x86, the least significant byte of the saved frame pointer is the next byte after the buffer. Therefore, such an off-by-one vulnerability can be exploitable. In the stack-smashing attack we do not need to know the exact address of the buffer, by prepending no-op s at the shellcode. However, for this exploit we need to know the address of the placeholder, which implies that we need to know the exact address of the buffer. Also, a function with a wrong frame pointer cannot locate its local variables correctly, so it may crash before the exploitation takes effect. Note, however, that such an instability is inherent in most exploits, since they usually corrupt the state of the process while overflowing buffers.

8 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 7 void main (int argc, char **argv) { char buf1[1024]; char buf2[256]; strncpy(buf1, argv[1], 1024); strncpy(buf2, argv[2], 256);... func(buf2); } void func (char *p) { char buf3[263]; sprintf(buf3, %s, p); } Figure 5. A vulnerable program for the non-terminated memory space exploit. If the buf2 is nullterminated, then the buf3 cannot be overflowed by sprintf, since the size of the buf3 is bigger than the size of the buf2. However, if the buf2 is not null-terminated, then the buf3 can be overflowed because the maximum size of the string p is not the size of the buf2 but the sum of the size of buf1 and the buf2 (and more if buf1 is also not null-terminated). The stack-smashing attack, for example, not only overwrites the buffer and the ess, but everything between them Non-terminated adjacent memory spaces Most buffer overflow attacks exploit unsafe string functions in the C library such as strcpy. There is a safe subset of string functions such as strncpy that limits the copy operation up to the number given by programmers. Those functions are definitely safer, but they can give a false sense of security if not used carefully. The technique in this section shows how to exploit them [42]. Figure 5 is an example of using strncpy in an unsafe way. The problem is that strncpy does not null terminate the output string if the source string is longer than the given maximum size. In the main in Figure 5, buf1 and buf2 cannot be overflowed, but they will not be null-terminated either if their source strings are longer than their limits; func takes buf2 as the source string and, since buf2 is smaller than buf3, users might think that buf3 is safe as long as buf2 is not overflowed. However, that will not be true if buf2 is not null terminated Return-into-libc Return-into-libc exploitation techniques by [37, 45, 28] also alter the ess, but the control is directed to a C library function rather than to a shellcode. Figure 6 shows an example

9 8 K. LHEE AND S. J. CHAPIN & of "/bin/sh" placeholder parameter buffer addr of system padding stack pointer before overflow after overflow, right before returning into system() right after returning into system(); system() treats the stack this way Figure 6. Return-into-libc example that exploits the program in Figure 1. that exploits the vulnerable program in Figure 1 to spawn a shell by overwriting the return address with the address of system. The attack code includes the address of system and the parameter to system (pointer to string /bin/sh ). This exploit needs to know the exact address of the string /bin/sh and the address of system. The string /bin/sh can be supplied through a command-line argument or an environment variable. In most cases where the C library is linked dynamically, finding the address of system requires finding out where in the address space the C library is mapped, and to find the offset to system within the C library [45]. The address where the C library is mapped can be found at the /proc directory or by running the program on a debugger. The offset to system within the C library can be read from the C library object file. This scheme is valid unless shared libraries are mapped at random addresses [29]. If the attack depends on string functions (such as strcpy) in delivering the attack code, then the attack code cannot contain a null byte. In fact, defensive techniques in [36, 22] map shared libraries such that their addresses always contain null bytes. Nonetheless, return-intolibc can still be effective with a small modification based on the following observation. An ELF program contains the Procedure Linkage Table (PLT) [41], which is used to call shared library functions. When calling a shared library function, the corresponding PLT entry is used instead of the real address of the function since the real address is not known at compile time. Unlike shared libraries, PLT is in a fixed location with not much chance of having null bytes. Therefore, we can use the PLT entry instead of the real address [45] to bypass such protection. The requirement is that system (or any other function in a shared library to which we wish to direct the control) has to be called somewhere in the program so that it does have an entry in the PLT.

10 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 9 & of "/bin/sh" & of 0 addr of system param (system) param (setuid) ret addr (setuid) buffer addr of setuid padding stack pointer before overflow right before returning into seuid() right after returning into setuid(); setuid() treats the stack this way Figure 7. Return-into-libc example that chains two functions in a row; setuid is called to regain the root privilege, and system is called to spawn a shell. & of shellcode & data segment & data segment param 2 (strcpy) param 1 (strcpy) ret addr (strcpy) buffer addr of strcpy padding stack pointer Figure 8. Return-into-libc example with shellcode. When returned to strcpy, strcpy copies the shellcode to a data segment and then returns to the data segment. It is also possible to call two functions in a row by supplying a valid address in the return address placeholder in Figure 6. Such a function chaining is illustrated in Figure 7, in which setuid and system are called in a row. A C library function can also be chained with a shellcode [45]. In the exploit in Figure 8, strcpy copies the shellcode into data segment and returns to the shellcode. Since the shellcode is executed from a non-stack area, this technique bypasses the non-executable stack patch from the Openwall Project [36].

11 10 K. LHEE AND S. J. CHAPIN stack pointer param 1 (func2) (4) addr of epilogue (3) addr of func2 function epilogue without frame pointer add 0x1c, %esp ret (2) padding param 2 (func1) param 1 (func1) 28 (0x1c) bytes available for parameters to func1 (1) addr of epilogue addr of func1 buffer padding Figure 9. The stack pointer lifting method. The first instruction of the function epilogue moves up the stack pointer, and the second instruction is a return. The program returns to func1 since the ess is overwritten. (1) the stack pointer right after the program returns to the func1 ; (2) after func1 returns to the epilogue; (3) after the add instruction of the epilogue; (4) after the return instruction of the epilogue (returned to func2 ). Generally, chaining multiple functions this way is limited due to the difficulty in placing their esses and parameters. For example, calling the second function is not possible if both functions need more than two parameters; system in Figure 7 will crash when it returns, since its ess placeholder is occupied by the parameter of setuid. The following two methods in [28] show how to chain multiple functions without such limitation by moving the stack pointer to accommodate parameters each time a function in the chain is called. The first method, the stack pointer lifting method, exploits a function epilogue that does not use the frame pointer (i.e., the program was compiled with such optimization flag turned on). A function epilogue without a frame pointer does the following: 1. Pops local variables by increasing the stack pointer by the total size of the local variables 2. Return address is popped The idea is that a function returns to such epilogue instead of returning to the next function in the chain in order to first lift the stack pointer to skip the parameters. Figure 9 illustrates this.

12 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 11 addr of epilogue (4) rtn addr of epilogue address of func 2 (4) pop address of func 2 fake frame ptr 2 (4) lift fake frame ptr 2 (2), (3) pop param 2 (func1) param 2 (func1) function epilogue with frame pointer leave ret param 1 (func1) addr of epilogue addr of func 1 points to (3) rtn (2) rtn, (3) pop (2) pop, (3) lift param 1 (func1) addr of epilogue addr of func 1 fake frame ptr 1 (1) rtn, (2) lift fake frame ptr 1 (1) pop addr of epilogue fake frame ptr 0 points to (1) pop (1) lift addr of epilogue fake frame ptr 0 buffer padding stack pointer value, right after the named operation padding frame pointer Figure 10. The frame faking method. The first instruction of the function epilogue (leave) assigns the stack pointer with the frame pointer value (thus effectively lifting the stack pointer) and restores the previous frame pointer from the stack. The lifting the stack pointer and the popping the frame pointer are named as lift and pop in the diagram. The second instruction is a return (named as rtn). (1) The corrupted function pops the frame pointer and returns to the epilogue; (2) The epilogue lifts the stack pointer, pops the frame pointer and returns to func1 ; (3) func1 begins execution by pushing the frame pointer. It lifts the stack pointer, pops the frame pointer and returns to the epilogue; (4) The epilogue lifts the stack pointer, pops the frame pointer and returns to func2. The second method, the frame faking method, exploits a function epilogue that uses a frame pointer (program that was compiled without such optimization). A function epilogue with a frame pointer does the following: 1. The stack pointer is assigned the current frame pointer value 2. Previous frame pointer is popped and assigned to the frame pointer 3. Return address is popped Since the stack pointer is assigned the frame pointer value, by changing the frame pointer we can again lift the stack pointer. As with the first method, a function returns to an epilogue instead of to the next function. Figure 10 illustrates this Exploits on function pointers Below we discuss exploitation techniques that target function pointers rather than the return address. Function pointers are convenient as low level constructs and so are used in many C programs. They are also used implicitly by the compiler, such as in the procedure linkage table

13 12 K. LHEE AND S. J. CHAPIN char buffer[64] = ; int (fptr)(char*) = NULL; void main (int argc, char **argv) {... strcpy(buffer, argv[1]); (void)(*fptr)(argv[2]); } Figure 11. A vulnerable function pointer in the (initialized) data section. in ELF binaries or the virtual function pointer in C++ programs. The function pointers can be anywhere in the memory space and not just the stack, which opens other possibilities of exploitation User function pointers in heap and data section This section discusses a technique, by [10], that overflows non-stack area. Figure 11 shows a program that allocates a buffer and a function pointer in the data section. This program is vulnerable since strcpy can overflow the buffer and alter the function pointer. The function pointer is overwritten with a code pointer (address of a shellcode or system). The attack takes effect when the function pointer is called. In this example the corrupted function is called right after the attack code has been delivered through strcpy, but it does not have to be so immediate. If the function pointer is a global variable, then it can be corrupted inside one function and called by another. In contrast, exploits that target the ess always have function scope The dtors section This technique, by [30], exploits function pointers created implicitly by the GNU C compiler; gcc provides a number of C language extensions that include function attributes which specify special attributes when making function declarations [39]. For example, we can declare a destructor function as follows: static void end(void) attribute ((destructor)); The function declared as a destructor is automatically called after main has completed or exit has been called; gcc implements it by storing pointers to destructor functions in the dtors section, which are walked through when the process exits. To exploit this, we overflow a buffer to overwrite an entry in the dtors section with a code pointer; the dtors section always exists even if no destructor functions are declared by the programmer. Figure 12 shows a vulnerable program to this technique and Figure 13 shows some of the sections in the executable file. From

14 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 13 static char buffer[32] = ; int main(int argc, char **argv) { strcpy(buffer, argv[1]); return 0; } Figure 12. A vulnerable program for a dtors exploit. We overflow the buffer placed in the data section to overwrite an entry in the dtors section. We supply the argv[1] with 82 bytes (the first destructor pointer would be stored four bytes after the beginning of the dtors section) of padding characters + the address of a shellcode. Idx Name Size V MA LMA F ileoff Algn rodata data dtors c c c got bss ALLOC... Figure 13. The sections of the vulnerable program in Figure 12. Note that the data section precedes the dtors section by 78 bytes and the got section by 84 bytes; the bss (uninitialized static data) section follows dtors and the got sections, so variables in bss cannot overflow the dtor or the got sections. Figure 13 it is apparent that the buffer in the data section is adjacent to the dtors section, hence can overflow the dtors section Global offset table, exit handler functions and setjmp/longjmp buffer Figure 13 shows that next to the dtors section is the got section, which holds the global offset table that stores addresses that need to be resolved at run time (addresses of objects in shared libraries, such as the C library). In particular, the global offset table contains addresses of shared library functions. As mentioned in Section 2.1.4, programs call the procedure linkage table entry instead of the real function in shared library. In an entry in the procedure linkage table is a jump instruction to the corresponding global offset table entry that holds the real address of the shared library function. Therefore, it is clear that the got section is as vulnerable

15 14 K. LHEE AND S. J. CHAPIN static char buffer[32] = ; int main(int argc, char **argv) { strcpy(buf, argv[1]); printf(buf); return 0; } Figure 14. A vulnerable program for got exploit. We overflow the buffer in the data section to overwrite the printf entry in got. Specifically, we supply the argv[1] with 84 bytes of padding characters and the address of a shellcode. as the dtors section. Unlike destructor functions, however, functions in the got section are not called automatically. In order for an attack to take effect, a corrupted entry in got has to be called somewhere in the program after the overflow. For example, we can exploit the vulnerable program in Figure 14 by overflowing the buffer in the data section to overwrite the got entry of printf that is called after strcpy. Besides the destructor functions, programmers can register exit handler functions [5] using the C library function atexit. Exit handler functions, too, are automatically called when exit is called or when main returns, and implemented similarly as the destructor functions (a table of function pointers is maintained). The table of pointers to exit handler functions is as vulnerable as the dtors section or the got section. However, the vulnerable program in Figure 12 may not be exploited since the table of pointers would be located far from the buffer. The table of pointers to exit handler functions is a C library object, and the C library is usually mapped in the address space too far away to be reached directly from the buffer in the data section (unless the C library is statically linked) [5]. We discuss this exploit later in Section 2.4, where we discuss overwriting data indirectly via pointers. setjmp/longjmp uses a buffer (jmp buf ) to save register values for performing nonlocal goto. Since jmp buf saves the program counter, we can exploit this by overflowing jmp buf and overwriting the saved program counter with, for example, the address of the shellcode. This exploit will take effect when longjmp is called with the overwritten jmp buf [10] C++ virtual function pointer This technique, by [31], exploits a table of function pointers and a pointer to that table, VTABLE and VPTR, respectively, created implicitly by the C++ compiler. VTABLE and VPTR are used to implement virtual functions in C++ programs. Pointers to the virtual functions defined in a class are stored in the VTABLE. An object instantiated from the class contains a VPTR (a pointer to the VTABLE) through which it calls virtual functions. For example, a call to the virtual function whose pointer is in the third entry of the VTABLE would be compiled as the code below. Figure 15 illustrates this.

16 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 15 object variables VPTR addr of virtual func 1 addr of virtual func 2... Figure 15. A conceptual view of an allocated object (with VPTR) and the VTABLE of the corresponding class. buffer VPTR * * * shellcode VPTR Figure 16. An example of a virtual function pointer exploit. An object is overflowed with a bogus VTABLE, a shellcode, and a pointer that overwrites the VPTR. call *(VPTR + 8) The exploit in Figure 16 overflows a variable in the object in order to alter the VPTR to make it point to the supplied bogus VTABLE. The bogus VTABLE contains pointers to the shellcode so that when a virtual function is called the shellcode is executed Exploits on malloc internal data structure This section discusses a vulnerability in the dynamic memory allocator in the GNU C library (Doug Lea s Malloc), by [20]. Memory blocks (called chunks) that are allocated and deallocated dynamically via malloc and free are managed by linked lists. Each memory block carries its own management data, such as its size and pointers to other chunks, as a typical linked list data structure would do (Figure 17). The vulnerability is that the user data and the management data are adjacent in a chunk. This is comparable to the vulnerability in the stack, in which local variables and the return address are adjacent. Just as we can alter the ess by overflowing a stack variable, we can alter the management data by overflowing a heap variable. This is referred to as a channeling problem [33] or a problem of storing management information in-band [2]. Unlike the ess, however, those management data are not code pointers, so altering them does not directly change the program control. Here we exploit the management data to ultimately change other code pointers.

17 16 K. LHEE AND S. J. CHAPIN user data bk fd size prev_size next chunk user data bk fd size prev_size addr that is returned to user chunk Figure 17. Memory block (chunk) managed by malloc. It shows that the memory block also includes internal data structure such as size of the chunk, size of the adjacent chunk, forward pointer, and backward pointer to other chunks. Free chunks are managed by doubly linked lists called bins, each of which contains chunks of a certain size range. When a free chunk is taken from a bin, a macro unlink is called to adjust the forward and backward pointers of the neighboring chunks. When a free chunk is placed into a bin, a macro frontlink is called 1) to locate the correct bin, 2) to locate the neighboring chunks, and 3) to adjust forward and backward pointers of the neighbors and itself. The next two examples exploit the unlink and frontlink macro Unlink exploit The unlink macro and the exploit are shown in Figure 18. The idea is that the fd and bk field of a free chunk P is altered so that when unlink is called with P, a code pointer is overwritten with the address of the shellcode. The exploit will take effect when the corrupted code pointer is called. A vulnerable program, for example, calls free(p ) where P is a chunk physically adjacent to the corrupted chunk P; free(p ) in turn calls unlink(p) in order to merge them into a bigger free chunk, since P is a free chunk.

18 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 17 shellcode shellcode BK BK >fd = FD shellcode (4) BK (3) 12 code pointer code pointer #define unlink(p, BK, FD) { BK = P >bk; (1) FD = P >fd; (2) FD >bk = BK; (3) BK >fd = FD; (4) } 0 FD (2) (1) FD corrupted chunk 12 8 bk fd bk fd 4 size size 0 prev_size P prev_size P (1), (2) (3), (4) Figure 18. The unlink macro and the exploit. The fd field of the free chunk P is altered to point to a code pointer (minus 12), and the bk field is altered to point to the shellcode. Note that, after the exploit, a word in the shellcode is also overwritten at (4). For this, the first instruction of the shellcode should be a jump over the overwritten word Frontlink exploit Figure 19 shows the frontlink macro and the exploit. The idea is to alter the fd field of a free chunk (to make it point to a fake chunk) so that when frontlink(p) is called (to place a free chunk P in a free list), a code pointer is overwritten with P. The vulnerable program, for example, calls free(p ) where P is in the same size range as the corrupted free chunk P. In addition, we need to be able to alter the word at P (with, for example, a jump instruction to the shellcode), since the corrupted code pointer is overwritten with P Indirect alteration via pointer In this section we discuss techniques, by [8], that exploit pointers to overwrite indirectly the code pointers discussed earlier. For such an exploit we need to be able to overwrite a pointer as

19 18 K. LHEE AND S. J. CHAPIN #define frontlink(a, P, S, IDX, BK, FD) { if (S < MAX_SMALLBIN_SIZE) { /* P goes to smallbin */ } else { /* locate the bin of right size for P */ /* the bin should contain the corrupted free chunk */... if (FD == BK) { mark_binlock(a, IDX); } else { while (FD!= BK && S < chunksize(fd)) { FD = FD >fd; (1) } BK = FD >bk; (2) } P >bk = BK; P >fd = FD; FD >bk = BK >fd = P; (3) } } code pointer code pointer P BK BK a fake chunk bk fd bk fd bk fd FD size prev_size size prev_size size prev_size (1) FD = FD >fd (2) BK = FD >bk (3) BK >fd = P Figure 19. The frontlink macro and the exploit. fd field of a free chunk is altered to point to a fake chunk, so that when while loop exits FD also points to the fake chunk (1). Note that the bk field of the fake chunk points to a code pointer (to be altered) 8. For this exploit, the size of the fake chunk should not be larger than the size of P (S in the while loop).

20 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 19 void f (char **argv) { char *p; char buffer[32];... strcpy(buffer, argv[1]); strcpy(p, argv[2]); } Figure 20. A program vulnerable to the indirect ess exploit and the indirect exit handler exploit. well as to overwrite a code pointer through that pointer. Such vulnerability is hard to find in real programs. On the other hand, such vulnerability is more subtle, thus harder to discover. Also, memory space of the target process is more vulnerable since we can alter memory area that is far from the overflowed buffer Altering the ess indirectly This exploit is similar to the stack-smashing attack, but it overwrites the ess indirectly through a pointer. For such an exploit we need two copy operations as in the vulnerable program in Figure 20. Figure 21 illustrates this exploit that, since it overwrites only the ess and not the StackGuard canary word, will not be detected by StackGuard Exploiting exit handler functions indirectly As noted in Section 2.2.3, pointers to exit handler functions are located too far away to be altered directly. The program in Figure 20 can, however, be used to exploit pointers to exit handler functions. This is the same as the indirect ess exploit except that the pointer p is altered to point to the exit handler table rather than to the ess. This exploit does not alter the ess, so neither StackGuard nor StackShield can detect this Exploiting global offset table indirectly The global offset table can be overwritten indirectly by exploiting the vulnerable program in Figure 22 (a vulnerable printf ). We overwrite the got entry of printf with the address of system so that when printf( print some string ) is called, system( print some string ) is called instead (Figure 23). For this exploit we need to create a shell script file named print in the current working directory, which performs the actual attack. The rest of the string some string is passed as the program arguments to the script file and does no harm unless

21 20 K. LHEE AND S. J. CHAPIN shellcode shellcode shellcode canary p canary & of & of shellcode canary & of buffer padding padding before strcpy strcpy(buffer, argv[1]) strcpy(p, argv[2]) Figure 21. The indirect ess exploit on the program in Figure 20. The first strcpy overflows the buffer and overwrites the pointer p with the address of the ess, so that the second strcpy overwrites the ess with the address of a shellcode; argv[1] points to the attack code (thick round box), and argv[2] points to the string, which is the address of the shellcode. void f (char **argv) { char *p; char buffer[32]; strcpy(buffer, argv[1]); strcpy(p, argv[2]); printf( print some string ); } Figure 22. A program vulnerable to the indirect got exploits. it contains a special shell character. This exploit does not alter the ess, so neither StackGuard or StackShield can detect this. Note that this exploit does not require executable stack or heap. If it uses the address of the PLT entry of system instead of the real address of system, it can also bypass the Openwall project (where the address of system can contain zero bytes) and PaX (where the address of system is unknown in advance due to the random mapping of shared libraries) [45]. Alternatively, an exploit that is illustrated in Figure 24 alters the got entry of printf so that it points to the shellcode. Since this exploit does not alter the ess it bypasses the

22 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 21 p & of GOT printf & of GOT printf buffer padding padding system() system() system() printf() printf() printf() GOT system: printf: system: printf: system: system: before strcpy strcpy(buffer, argv[1]) strcpy(p, argv[2]) Figure 23. Global offset table entry of printf is altered indirectly such that it points to system instead; argv[1] points to the attack code (thick round box), and argv[2] points to an address of system. StackGuard and StackShield. It also bypasses the non-executable stack by Openwall Project since the shellcode is copied into the got section. 3. DEFENSIVE TECHNIQUES AGAINST BUFFER OVERFLOW 3.1. Run-time detection systems StackGuard The stack-smashing attack overwrites the buffer, the ess and everything in between. StackGuard [11] is a GNU C compiler extension that inserts a canary word between the return address and the buffer so that an attempt to alter the ess can be detected by inspecting the canary word before returning from a function. Programs need to be recompiled with StackGuard in order to be protected StackShield StackShield [38] is also a GNU C compiler extension that protects the ess. When a function is called, StackShield copies away the ess to a non-overflowable area it restores the ess upon returning from a function. Even if the ess on

23 22 K. LHEE AND S. J. CHAPIN p & of GOT printf & of GOT printf buffer shellcode shellcode & GOT printf + 4 & GOT printf + 4 GOT shellcode printf: printf: & GOT printf + 4 before strcpy strcpy(buffer, argv[1]) strcpy(p, argv[2]) Figure 24. Global offset table entry of printf is altered indirectly such that it points to the shellcode; argv[1] and argv[2] point to the attack code (thick round box). the stack is altered, it has no effect since the original ess is remembered. As with StackGuard, programs need to be recompiled Libsafe Libsafe [4] is an implementation of vulnerable copy functions in the C library such as strcpy. In addition to the original functionality of those functions, it imposes a limit on the involved copy operations such that they do not overwrite the ess. The limit is determined based on the notion that the buffer cannot extend beyond its stack frame. Thus the maximum size of a buffer is the distance between the address of the buffer and the corresponding frame pointer. Libsafe is implemented as a shared library that is preloaded to intercept C library function calls. Programs are protected without recompilation unless they are statically linked with the C library or have been compiled to run without the frame pointer (it needs to walk up the stack using the saved frame pointers in the stack). Libsafe protects only those C library functions, whereas StackGuard and StackShield protect all functions Linux kernel patch from the Openwall Project The stack-smashing attack injects an attack code into the stack, which is executed when the function returns. One of the core features of the Linux kernel patch from the Openwall Project [36] is to make the stack segment non-executable. Another feature is to map into the address space the shared libraries such that their addresses always contain zero bytes, in

24 BUFFER OVERFLOW AND FORMAT STRING OVERFLOW VULNERABILITIES 23 order to defend from return-into-libc attacks. If the address of a function is to be delivered through a null-terminated string function (such as strcpy), the zero byte in the middle of the function address will terminate the copying [37]. It does not impose any performance penalty or require program recompilation except for the kernel. There are a few occasions that require the stack to be executable, which include nested functions (a C language extension by the GNU C compiler) and Linux signal handler (both are emulated by the Linux kernel patch). Programs that require executable stack can be made to run individually, using the included utility program PaX, RSX and knox PaX [29] is a page-based protection mechanism that marks data pages as non-executable. Unlike the Linux kernel patch from the Openwall Project, PaX protects the heap as well as the stack. Since there is no execution permission bit on pages in the x86 processor, PaX overloads the supervisor/user bit on pages and augments the page fault handler to distinguish the page faults due to the attempts to execute code in data pages. As a result, it imposes a run-time overhead due to the extra page faults. PaX is also available as a Linux kernel patch. As with the Openwall Project, Pax is not completely transparent to existing programs since some programs require the heap or the stack to be executable. For example, an interpreter such as Java might cache machine instructions in the heap and execute from there for performance. Pax also can map the first loaded library at a random location in the address space in order to defend from return-into-libc exploits (since the address of a C library function cannot be known in advance) [28]. Other systems that provide non-executable stack and heap on Linux are RSX [32] and knox [22]. They also share the disadvantage of Openwall Project and PaX that programs requiring executable stack or heap cannot run transparently Range-checking systems The advantage of the array bounds checking approach is that it completely eliminates the buffer overflow vulnerability. However, it is also the most expensive solution, particularly for the pointer and array intensive programs, since every pointer and array operation must be checked. This may not be suitable for a production system. The pointer and array access checking technique by Austin et al. [3] is a source-to-source translator that transforms C pointers into the extended pointer representation called safe pointer, and inserts access checks before pointer or array dereferences. The safe pointer contains fields such as the base address, its size, and the scope of the pointer. Those fields are used by access checks to determine whether the pointer is valid and within the range. Since it changes pointer representation, it is not compatible with existing programs. The array bounds and pointer checking technique by Jones and Kelly [19] is an extension to the GNU C compiler that imposes an access check on C pointers and arrays. Instead of changing pointer representation, it maintains a table of all the valid storage objects that hold such information as base address, size, etc. Information concerning the heap variables is entered into the table via the modified malloc and deleted from the table via the modified

25 24 K. LHEE AND S. J. CHAPIN free. Information about the stack variables is entered into/deleted from the table by the constructor/destructor function, which is inserted inside a function definition at the point at which stack variables enter/leave the scope. The access check is done by substituting pointer and array operations with the functions that perform bounds checks using the table in addition to the original operation. Since native C pointers are used, this technique is compatible with existing programs. Purify, by Hastings and Joyce [16], is a commercially available run-time memory access error-checking tool. An advantage of Purify is that it inserts access-checking code into the object code without requiring source code access. It checks all the memory access, memory allocation/deallocation, and function calls, and it maintains states of memory blocks (allocated, initialized, etc.) to catch temporal errors such as dangling pointers. Array bounds are checked by marking both ends of a memory block returned by malloc. Purify, however, lacks type or scope information that is available only at the source level, so it cannot detect some the errors, such as buffer overflow within a malloc memory block Static analysis techniques Static analysis techniques have several advantages over run-time techniques. They do not incur run-time overhead and they narrow down the vulnerabilities specific to the source program being analyzed, yielding a more secure program before it is deployed. However, a pure static analysis can produce many false alarms due to the lack of run-time information. For example, gets reads its input string from stdin, so the size of the string is not known at compile time. For such a case a warning is issued as a possible buffer overflow. In fact, all the legitimate copy operations that accept their strings from unknown sources (such as a command line argument or an I/O channel) are flagged as possible buffer overflows (since they are indeed vulnerable). Without further action, those vulnerabilities are identified, but still open to attack. The integer range analysis by Wagner et al. [43] is a technique that detects possible buffer overflow in the vulnerable C library functions. A string buffer is modeled as a pair of integer ranges (lower bound, upper bound) for its allocated size and its current length. A set of integer constraints is predefined for a set of string operations (e.g., character array declaration, vulnerable C library functions, and assignment statements involving them). Using those integer constraints, the technique analyzes the source code by checking each string buffer to find out if its inferred allocated size is at least as large as its inferred maximum length. The annotation-assisted static analysis technique by Larochelle and Evans [23] based on LCLint [14] uses semantic comments, called annotations, provided by programmers to detect possible buffer overflow. For example, annotations for strcpy contain an assertion that the destination buffer must be allocated to hold at least as many characters as are readable in the source buffer. This technique protects any annotated functions, whereas the integer range analysis only protects C library functions. However, it requires programmers to provide annotations.

CSE 565 Computer Security Fall 2018

CSE 565 Computer Security Fall 2018 CSE 565 Computer Security Fall 2018 Lecture 14: Software Security Department of Computer Science and Engineering University at Buffalo 1 Software Security Exploiting software vulnerabilities is paramount

More information

Runtime Defenses against Memory Corruption

Runtime Defenses against Memory Corruption CS 380S Runtime Defenses against Memory Corruption Vitaly Shmatikov slide 1 Reading Assignment Cowan et al. Buffer overflows: Attacks and defenses for the vulnerability of the decade (DISCEX 2000). Avijit,

More information

Secure Coding in C and C++

Secure Coding in C and C++ Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Sept 21, 2017 Acknowledgement: These slides are based on author Seacord s original presentation Issues Dynamic Memory Management Common Dynamic

More information

Beyond Stack Smashing: Recent Advances in Exploiting. Jonathan Pincus(MSR) and Brandon Baker (MS)

Beyond Stack Smashing: Recent Advances in Exploiting. Jonathan Pincus(MSR) and Brandon Baker (MS) Beyond Stack Smashing: Recent Advances in Exploiting Buffer Overruns Jonathan Pincus(MSR) and Brandon Baker (MS) Buffer Overflows and How they Occur Buffer is a contiguous segment of memory of a fixed

More information

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks Runtime attacks are major threats to today's applications Control-flow of an application is compromised at runtime Typically, runtime attacks include injection of malicious code Reasons for runtime attacks

More information

Buffer overflow prevention, and other attacks

Buffer overflow prevention, and other attacks Buffer prevention, and other attacks Comp Sci 3600 Security Outline 1 2 Two approaches to buffer defense Aim to harden programs to resist attacks in new programs Run time Aim to detect and abort attacks

More information

Secure Software Programming and Vulnerability Analysis

Secure Software Programming and Vulnerability Analysis Secure Software Programming and Vulnerability Analysis Christopher Kruegel chris@auto.tuwien.ac.at http://www.auto.tuwien.ac.at/~chris Heap Buffer Overflows and Format String Vulnerabilities Secure Software

More information

Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Jan 29, 2013

Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Jan 29, 2013 Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Jan 29, 2013 Acknowledgement: These slides are based on author Seacord s original presentation Issues Dynamic Memory Management Common Dynamic

More information

Buffer Overflows Defending against arbitrary code insertion and execution

Buffer Overflows Defending against arbitrary code insertion and execution www.harmonysecurity.com info@harmonysecurity.com Buffer Overflows Defending against arbitrary code insertion and execution By Stephen Fewer Contents 1 Introduction 2 1.1 Where does the problem lie? 2 1.1.1

More information

malloc() is often used to allocate chunk of memory dynamically from the heap region. Each chunk contains a header and free space (the buffer in which

malloc() is often used to allocate chunk of memory dynamically from the heap region. Each chunk contains a header and free space (the buffer in which Heap Overflow malloc() is often used to allocate chunk of memory dynamically from the heap region. Each chunk contains a header and free space (the buffer in which data are placed). The header contains

More information

Buffer Overflow Vulnerability

Buffer Overflow Vulnerability Buffer Overflow Vulnerability 1 Buffer Overflow Vulnerability Copyright c 2006 2014 Wenliang Du, Syracuse University. The development of this document is/was funded by three grants from the US National

More information

Proceedings of the 11 th USENIX Security Symposium

Proceedings of the 11 th USENIX Security Symposium USENIX Association Proceedings of the 11 th USENIX Security Symposium San Francisco, California, USA August 5-9, 2002 THE ADVANCED COMPUTING SYSTEMS ASSOCIATION 2002 by The USENIX Association All Rights

More information

Buffer overflow background

Buffer overflow background and heap buffer background Comp Sci 3600 Security Heap Outline and heap buffer Heap 1 and heap 2 3 buffer 4 5 Heap Outline and heap buffer Heap 1 and heap 2 3 buffer 4 5 Heap Address Space and heap buffer

More information

Software Security: Buffer Overflow Defenses

Software Security: Buffer Overflow Defenses CSE 484 / CSE M 584: Computer Security and Privacy Software Security: Buffer Overflow Defenses Fall 2017 Franziska (Franzi) Roesner franzi@cs.washington.edu Thanks to Dan Boneh, Dieter Gollmann, Dan Halperin,

More information

Buffer Overflow Vulnerabilities

Buffer Overflow Vulnerabilities Buffer Overflow Vulnerabilities Exploits and Defensive Techniques Adam Butcher and Peter Buchlovsky 8. March 2004 University of Birmingham 1 Contents 1. Call-stacks, shellcode, gets 2. Exploits stack-based,

More information

CMPSC 497 Buffer Overflow Vulnerabilities

CMPSC 497 Buffer Overflow Vulnerabilities Systems and Internet Infrastructure Security Network and Security Research Center Department of Computer Science and Engineering Pennsylvania State University, University Park PA CMPSC 497 Buffer Overflow

More information

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows Betriebssysteme und Sicherheit Sicherheit Buffer Overflows Software Vulnerabilities Implementation error Input validation Attacker-supplied input can lead to Corruption Code execution... Even remote exploitation

More information

Lecture 08 Control-flow Hijacking Defenses

Lecture 08 Control-flow Hijacking Defenses Lecture 08 Control-flow Hijacking Defenses Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Slides adapted from Miller, Bailey, and Brumley Control Flow Hijack: Always control + computation

More information

Security and Privacy in Computer Systems. Lecture 5: Application Program Security

Security and Privacy in Computer Systems. Lecture 5: Application Program Security CS 645 Security and Privacy in Computer Systems Lecture 5: Application Program Security Buffer overflow exploits More effective buffer overflow attacks Preventing buffer overflow attacks Announcement Project

More information

Intrusion Detection and Malware Analysis

Intrusion Detection and Malware Analysis Intrusion Detection and Malware Analysis Host Based Attacks Pavel Laskov Wilhelm Schickard Institute for Computer Science Software security threats Modification of program code viruses and self-replicating

More information

Topics in Software Security Vulnerability

Topics in Software Security Vulnerability Topics in Software Security Vulnerability Software vulnerability What are software vulnerabilities? Types of vulnerabilities E.g., Buffer Overflows How to find these vulnerabilities and prevent them? Classes

More information

Software Vulnerabilities August 31, 2011 / CS261 Computer Security

Software Vulnerabilities August 31, 2011 / CS261 Computer Security Software Vulnerabilities August 31, 2011 / CS261 Computer Security Software Vulnerabilities...1 Review paper discussion...2 Trampolining...2 Heap smashing...2 malloc/free...2 Double freeing...4 Defenses...5

More information

Buffer Overflow Defenses

Buffer Overflow Defenses Buffer Overflow Defenses Some examples, pros, and cons of various defenses against buffer overflows. Caveats: 1. Not intended to be a complete list of products that defend against buffer overflows. 2.

More information

ECS 153 Discussion Section. April 6, 2015

ECS 153 Discussion Section. April 6, 2015 ECS 153 Discussion Section April 6, 2015 1 What We ll Cover Goal: To discuss buffer overflows in detail Stack- based buffer overflows Smashing the stack : execution from the stack ARC (or return- to- libc)

More information

Software Security II: Memory Errors - Attacks & Defenses

Software Security II: Memory Errors - Attacks & Defenses 1 Software Security II: Memory Errors - Attacks & Defenses Chengyu Song Slides modified from Dawn Song 2 Administrivia Lab1 Writeup 3 Buffer overflow Out-of-bound memory writes (mostly sequential) Allow

More information

Other array problems. Integer overflow. Outline. Integer overflow example. Signed and unsigned

Other array problems. Integer overflow. Outline. Integer overflow example. Signed and unsigned Other array problems CSci 5271 Introduction to Computer Security Day 4: Low-level attacks Stephen McCamant University of Minnesota, Computer Science & Engineering Missing/wrong bounds check One unsigned

More information

Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Review

Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Review Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Review Network Security Instructor:Dr. Shishir Nagaraja Submitted By: Jyoti Leeka September 24, 2011. 1 Introduction to the topic

More information

CS 161 Computer Security

CS 161 Computer Security Paxson Spring 2017 CS 161 Computer Security Discussion 2 Question 1 Software Vulnerabilities (15 min) For the following code, assume an attacker can control the value of basket passed into eval basket.

More information

Basic Buffer Overflows

Basic Buffer Overflows Operating Systems Security Basic Buffer Overflows (Stack Smashing) Computer Security & OS lab. Cho, Seong-je ( 조성제 ) Fall, 2018 sjcho at dankook.ac.kr Chapter 10 Buffer Overflow 2 Contents Virtual Memory

More information

Buffer Overflow Attacks

Buffer Overflow Attacks Buffer Overflow Attacks 1. Smashing the Stack 2. Other Buffer Overflow Attacks 3. Work on Preventing Buffer Overflow Attacks Smashing the Stack An Evil Function void func(char* inp){ } char buffer[16];

More information

Outline. Format string attack layout. Null pointer dereference

Outline. Format string attack layout. Null pointer dereference CSci 5271 Introduction to Computer Security Day 5: Low-level defenses and counterattacks Stephen McCamant University of Minnesota, Computer Science & Engineering Null pointer dereference Format string

More information

CSE 509: Computer Security

CSE 509: Computer Security CSE 509: Computer Security Date: 2.16.2009 BUFFER OVERFLOWS: input data Server running a daemon Attacker Code The attacker sends data to the daemon process running at the server side and could thus trigger

More information

Countermeasures in Modern Operating Systems. Yves Younan, Vulnerability Research Team (VRT)

Countermeasures in Modern Operating Systems. Yves Younan, Vulnerability Research Team (VRT) Countermeasures in Modern Operating Systems Yves Younan, Vulnerability Research Team (VRT) Introduction Programs in C/C++: memory error vulnerabilities Countermeasures (mitigations): make exploitation

More information

Secure Programming I. Steven M. Bellovin September 28,

Secure Programming I. Steven M. Bellovin September 28, Secure Programming I Steven M. Bellovin September 28, 2014 1 If our software is buggy, what does that say about its security? Robert H. Morris Steven M. Bellovin September 28, 2014 2 The Heart of the Problem

More information

Lecture 1: Buffer Overflows

Lecture 1: Buffer Overflows CS5431 Computer Security Practicum Spring 2017 January 27, 2017 1 Conficker Lecture 1: Buffer Overflows Instructor: Eleanor Birrell In November 2008, a new piece of malware was observed in the wild. This

More information

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2 CIS 551 / TCOM 401 Computer and Network Security Spring 2007 Lecture 2 Announcements First project is on the web Due: Feb. 1st at midnight Form groups of 2 or 3 people If you need help finding a group,

More information

Advanced Buffer Overflow

Advanced Buffer Overflow Pattern Recognition and Applications Lab Advanced Buffer Overflow Ing. Davide Maiorca, Ph.D. davide.maiorca@diee.unica.it Computer Security A.Y. 2016/2017 Department of Electrical and Electronic Engineering

More information

Università Ca Foscari Venezia

Università Ca Foscari Venezia Stack Overflow Security 1 2018-19 Università Ca Foscari Venezia www.dais.unive.it/~focardi secgroup.dais.unive.it Introduction Buffer overflow is due to careless programming in unsafe languages like C

More information

Heap Off by 1 Overflow Illustrated. Eric Conrad October 2007

Heap Off by 1 Overflow Illustrated. Eric Conrad October 2007 Heap Off by 1 Overflow Illustrated Eric Conrad October 2007 1 The Attack Older CVS versions are vulnerable to an Off by 1 attack, where an attacker may insert one additional character into the heap CVS

More information

Buffer Overflow Vulnerability Lab Due: September 06, 2018, Thursday (Noon) Submit your lab report through to

Buffer Overflow Vulnerability Lab Due: September 06, 2018, Thursday (Noon) Submit your lab report through  to CPSC 8810 Fall 2018 Lab 1 1 Buffer Overflow Vulnerability Lab Due: September 06, 2018, Thursday (Noon) Submit your lab report through email to lcheng2@clemson.edu Copyright c 2006-2014 Wenliang Du, Syracuse

More information

Software Security: Buffer Overflow Attacks (continued)

Software Security: Buffer Overflow Attacks (continued) CSE 484 / CSE M 584: Computer Security and Privacy Software Security: Buffer Overflow Attacks (continued) Spring 2015 Franziska (Franzi) Roesner franzi@cs.washington.edu Thanks to Dan Boneh, Dieter Gollmann,

More information

Buffer Overflow & Format Strings

Buffer Overflow & Format Strings & Seminar on Computer Security Threats and Counter Measures Dominik Zindel 19.01.2007 Content 1 Technical Basics 2 3 4 Discussion References Goals Technical Basics Common goal of attacks using a buffer

More information

CSE 127 Computer Security

CSE 127 Computer Security CSE 127 Computer Security Alex Gantman, Spring 2018, Lecture 4 Low Level Software Security II: Format Strings, Shellcode, & Stack Protection Review Function arguments and local variables are stored on

More information

Lecture 07 Heap control data. Stephen Checkoway University of Illinois at Chicago

Lecture 07 Heap control data. Stephen Checkoway University of Illinois at Chicago Lecture 07 Heap control data Stephen Checkoway University of Illinois at Chicago Layout of program memory Heap is managed by malloc - Many different malloc implementations - glibc uses a modified version

More information

CNIT 127: Exploit Development. Ch 14: Protection Mechanisms. Updated

CNIT 127: Exploit Development. Ch 14: Protection Mechanisms. Updated CNIT 127: Exploit Development Ch 14: Protection Mechanisms Updated 3-25-17 Topics Non-Executable Stack W^X (Either Writable or Executable Memory) Stack Data Protection Canaries Ideal Stack Layout AAAS:

More information

Lecture Embedded System Security A. R. Darmstadt, Runtime Attacks

Lecture Embedded System Security A. R. Darmstadt, Runtime Attacks 2 ARM stands for Advanced RISC Machine Application area: Embedded systems Mobile phones, smartphones (Apple iphone, Google Android), music players, tablets, and some netbooks Advantage: Low power consumption

More information

Fundamentals of Computer Security

Fundamentals of Computer Security Fundamentals of Computer Security Spring 2015 Radu Sion Software Errors Buffer Overflow TOCTTOU 2005-15 Portions copyright by Bogdan Carbunar and Wikipedia. Used with permission Why Security Vulnerabilities?

More information

Buffer Overflows. A brief Introduction to the detection and prevention of buffer overflows for intermediate programmers.

Buffer Overflows. A brief Introduction to the detection and prevention of buffer overflows for intermediate programmers. Buffer Overflows A brief Introduction to the detection and prevention of buffer overflows for intermediate programmers. By: Brian Roberts What is a buffer overflow? In languages that deal with data structures

More information

Agenda. Dynamic Memory Management. Robert C. Seacord. Secure Coding in C and C++

Agenda. Dynamic Memory Management. Robert C. Seacord. Secure Coding in C and C++ Dynamic Memory Management Secure Coding in C and C++ Robert C. Seacord CERT Coordination Center Software Engineering Institute Carnegie Mellon University Pittsburgh, PA 15213-3890 The CERT Coordination

More information

Program Security and Vulnerabilities Class 2

Program Security and Vulnerabilities Class 2 Program Security and Vulnerabilities Class 2 CEN-5079: 28.August.2017 1 Secure Programs Programs Operating System Device Drivers Network Software (TCP stack, web servers ) Database Management Systems Integrity

More information

Lecture 4 September Required reading materials for this class

Lecture 4 September Required reading materials for this class EECS 261: Computer Security Fall 2007 Lecture 4 September 6 Lecturer: David Wagner Scribe: DK Moon 4.1 Required reading materials for this class Beyond Stack Smashing: Recent Advances in Exploiting Buffer

More information

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security CSE543 - Introduction to Computer and Network Security Module: Program Vulnerabilities Professor Trent Jaeger 1 1 Programming Why do we write programs? Function What functions do we enable via our programs?

More information

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated CNIT 127: Exploit Development Ch 1: Before you begin Updated 1-14-16 Basic Concepts Vulnerability A flaw in a system that allows an attacker to do something the designer did not intend, such as Denial

More information

CS 161 Computer Security

CS 161 Computer Security Paxson Spring 2011 CS 161 Computer Security Discussion 1 January 26, 2011 Question 1 Buffer Overflow Mitigations Buffer overflow mitigations generally fall into two categories: (i) eliminating the cause

More information

Outline. Heap meta-data. Non-control data overwrite

Outline. Heap meta-data. Non-control data overwrite Outline CSci 5271 Introduction to Computer Security Day 5: Low-level defenses and counterattacks Stephen McCamant University of Minnesota, Computer Science & Engineering Non-control data overwrite Heap

More information

CSE 127: Computer Security. Memory Integrity. Kirill Levchenko

CSE 127: Computer Security. Memory Integrity. Kirill Levchenko CSE 127: Computer Security Memory Integrity Kirill Levchenko November 18, 2014 Stack Buffer Overflow Stack buffer overflow: writing past end of a stackallocated buffer Also called stack smashing One of

More information

CSc 466/566. Computer Security. 20 : Operating Systems Application Security

CSc 466/566. Computer Security. 20 : Operating Systems Application Security 1/68 CSc 466/566 Computer Security 20 : Operating Systems Application Security Version: 2014/11/20 13:07:28 Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2014 Christian

More information

Foundations of Network and Computer Security

Foundations of Network and Computer Security Foundations of Network and Computer Security John Black Lecture #19 Nov 2 nd 2004 CSCI 6268/TLEN 5831, Fall 2004 Announcements Quiz #3 This Thursday Covers material from midterm through today Project #3

More information

1/31/2007 C. Edward Chow. CS591 Page 1

1/31/2007 C. Edward Chow. CS591 Page 1 Page 1 History of Buffer Overflow Attacks Buffer Overflow Attack and related Background Knowledge Linux VirtualMemory Map Shellcode Egg: No-ops/shellcode/returnAddresses Countermeasures: StackGuard StackShield

More information

Advanced Buffer Overflow

Advanced Buffer Overflow Pattern Recognition and Applications Lab Advanced Buffer Overflow Ing. Davide Maiorca, Ph.D. davide.maiorca@diee.unica.it Computer Security A.Y. 2017/2018 Department of Electrical and Electronic Engineering

More information

Secure Software Development: Theory and Practice

Secure Software Development: Theory and Practice Secure Software Development: Theory and Practice Suman Jana MW 2:40-3:55pm 415 Schapiro [SCEP] *Some slides are borrowed from Dan Boneh and John Mitchell Software Security is a major problem! Why writing

More information

Practical Malware Analysis

Practical Malware Analysis Practical Malware Analysis Ch 4: A Crash Course in x86 Disassembly Revised 1-16-7 Basic Techniques Basic static analysis Looks at malware from the outside Basic dynamic analysis Only shows you how the

More information

One-Slide Summary. Lecture Outline. Language Security

One-Slide Summary. Lecture Outline. Language Security Language Security Or: bringing a knife to a gun fight #1 One-Slide Summary A language s design principles and features have a strong influence on the security of programs written in that language. C s

More information

C and C++: vulnerabilities, exploits and countermeasures

C and C++: vulnerabilities, exploits and countermeasures C and C++: vulnerabilities, exploits and countermeasures Yves Younan DistriNet, Department of Computer Science Katholieke Universiteit Leuven Belgium Yves.Younan@cs.kuleuven.ac.be Introduction C/C++ programs:

More information

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security CSE543 - Introduction to Computer and Network Security Module: Program Vulnerabilities Professor Trent Jaeger 1 Programming Why do we write programs? Function What functions do we enable via our programs?

More information

Biography. Background

Biography. Background From Over ow to Shell An Introduction to low-level exploitation Carl Svensson @ KTH, January 2019 1 / 28 Biography MSc in Computer Science, KTH Head of Security, KRY/LIVI CTF: HackingForSoju E-mail: calle.svensson@zeta-two.com

More information

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface Secure Coding CYSE 411/AIT681 Secure Software Engineering Topic #9. Secure Coding: Dynamic Memory Instructor: Dr. Kun Sun String management Pointer Subterfuge Dynamic memory management Integer security

More information

Control Hijacking Attacks

Control Hijacking Attacks Control Hijacking Attacks Alexandros Kapravelos kapravelos@ncsu.edu (Derived from slides from Chris Kruegel) Attacker s mindset Take control of the victim s machine Hijack the execution flow of a running

More information

Buffer Overflow Attack

Buffer Overflow Attack Chapter 4 This is a sample chapter in the book titled "Computer Security: A Hands-on Approach" authored by Wenliang Du. Buffer Overflow Attack From Morris worm in 1988, Code Red worm in 2001, SQL Slammer

More information

CS 161 Computer Security

CS 161 Computer Security Paxson Spring 2011 CS 161 Computer Security Homework 1 Due: Wednesday, February 9, at 9:59pm Instructions. Submit your solution by Wednesday, February 9, at 9:59pm, in the drop box labelled CS161 in 283

More information

Architecture-level Security Vulnerabilities

Architecture-level Security Vulnerabilities Architecture-level Security Vulnerabilities Björn Döbel Outline How stacks work Smashing the stack for fun and profit Preventing stack smashing attacks Circumventing stack smashing prevention The Battlefield:

More information

CS 645: Lecture 3 Software Vulnerabilities. Rachel Greenstadt July 3, 2013

CS 645: Lecture 3 Software Vulnerabilities. Rachel Greenstadt July 3, 2013 CS 645: Lecture 3 Software Vulnerabilities Rachel Greenstadt July 3, 2013 Project 1: Software exploits Individual project - done in virtual machine environment This assignment is hard. Don t leave it until

More information

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security CSE543 - Introduction to Computer and Network Security Module: Program Vulnerabilities Professor Trent Jaeger 1 Programming Why do we write programs? Function What functions do we enable via our programs?

More information

Language Security. Lecture 40

Language Security. Lecture 40 Language Security Lecture 40 (from notes by G. Necula) Prof. Hilfinger CS 164 Lecture 40 1 Lecture Outline Beyond compilers Looking at other issues in programming language design and tools C Arrays Exploiting

More information

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction Outline CSci 5271 Introduction to Computer Security Day 3: Low-level vulnerabilities Stephen McCamant University of Minnesota, Computer Science & Engineering Race conditions Classic races: files in /tmp

More information

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated CNIT 127: Exploit Development Ch 3: Shellcode Updated 1-30-17 Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object files strace System Call Tracer Removing

More information

CSE / / 60567: Computer Security. Software Security 4

CSE / / 60567: Computer Security. Software Security 4 CSE 40567 / 44567 / 60567: Computer Security Software Security 4 91 Homework #5 Due: Tonight at 11:59PM Eastern Time (ND) / Pacific Time (SV) See Assignments Page on the course website for details 92 Notes

More information

SoK: Eternal War in Memory

SoK: Eternal War in Memory SoK: Eternal War in Memory László Szekeres, Mathias Payer, Tao Wei, Dawn Song Presenter: Wajih 11/7/2017 Some slides are taken from original S&P presentation 1 What is SoK paper? Systematization of Knowledge

More information

Buffer Overflow Vulnerability Lab

Buffer Overflow Vulnerability Lab SEED Labs Buffer Overflow Vulnerability Lab 1 Buffer Overflow Vulnerability Lab Copyright c 2006-2013 Wenliang Du, Syracuse University. The development of this document is/was funded by three grants from

More information

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I Solutions

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I Solutions Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.893 Fall 2009 Quiz I Solutions All problems are open-ended questions. In order to receive credit you must

More information

Smashing the Buffer. Miroslav Štampar

Smashing the Buffer. Miroslav Štampar Smashing the Buffer Miroslav Štampar (mstampar@zsis.hr) Summary BSidesVienna 2014, Vienna (Austria) November 22nd, 2014 2 Buffer overflow (a.k.a.) Buffer overrun An anomaly where a program, while writing

More information

Return-orientated Programming

Return-orientated Programming Return-orientated Programming or The Geometry of Innocent Flesh on the Bone: Return-into-libc without Function Calls (on the x86) Hovav Shacham, CCS '07 Return-Oriented oriented Programming programming

More information

Stack Vulnerabilities. CS4379/5375 System Security Assurance Dr. Jaime C. Acosta

Stack Vulnerabilities. CS4379/5375 System Security Assurance Dr. Jaime C. Acosta 1 Stack Vulnerabilities CS4379/5375 System Security Assurance Dr. Jaime C. Acosta Part 1 2 3 An Old, yet Still Valid Vulnerability Buffer/Stack Overflow ESP Unknown Data (unused) Unknown Data (unused)

More information

Writing Exploits. Nethemba s.r.o.

Writing Exploits. Nethemba s.r.o. Writing Exploits Nethemba s.r.o. norbert.szetei@nethemba.com Motivation Basic code injection W^X (DEP), ASLR, Canary (Armoring) Return Oriented Programming (ROP) Tools of the Trade Metasploit A Brief History

More information

CSC 405 Computer Security Stack Canaries & ASLR

CSC 405 Computer Security Stack Canaries & ASLR CSC 405 Computer Security Stack Canaries & ASLR Alexandros Kapravelos akaprav@ncsu.edu How can we prevent a buffer overflow? Check bounds Programmer Language Stack canaries [...more ] Buffer overflow defenses

More information

Is stack overflow still a problem?

Is stack overflow still a problem? Morris Worm (1998) Code Red (2001) Secure Programming Lecture 4: Memory Corruption II (Stack Overflows) David Aspinall, Informatics @ Edinburgh 31st January 2017 Memory corruption Buffer overflow remains

More information

Code with red border means vulnerable code. Code with green border means corrected code. This program asks the user for a password with the function

Code with red border means vulnerable code. Code with green border means corrected code. This program asks the user for a password with the function 1 Code with red border means vulnerable code. Code with green border means corrected code. This program asks the user for a password with the function IsPasswordOK(), and compares it with the correct password.

More information

Buffer Overflow Attack (AskCypert CLaaS)

Buffer Overflow Attack (AskCypert CLaaS) Buffer Overflow Attack (AskCypert CLaaS) ---------------------- BufferOverflow.c code 1. int main(int arg c, char** argv) 2. { 3. char name[64]; 4. printf( Addr;%p\n, name); 5. strcpy(name, argv[1]); 6.

More information

1 Recommended Readings

1 Recommended Readings CSC 482/582 Assignment #5 Buffer Overflow Due: November 14, 2013 The learning objective of this assignment is for students to gain first-hand experience with a buffer overflow vulnerability, applying what

More information

CSE 127 Computer Security

CSE 127 Computer Security CSE 127 Computer Security Stefan Savage, Fall 2018, Lecture 4 Low Level Software Security II: Format Strings, Shellcode, & Stack Protection Review Function arguments and local variables are stored on the

More information

Buffer overflow is still one of the most common vulnerabilities being discovered and exploited in commodity software.

Buffer overflow is still one of the most common vulnerabilities being discovered and exploited in commodity software. Outline Morris Worm (1998) Infamous attacks Secure Programming Lecture 4: Memory Corruption II (Stack Overflows) David Aspinall, Informatics @ Edinburgh 23rd January 2014 Recap Simple overflow exploit

More information

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask This time We will continue Buffer overflows By looking at Overflow Defenses and other memory safety vulnerabilities Everything you ve always wanted to know about gdb but were too afraid to ask Overflow

More information

Architecture-level Security Vulnerabilities. Julian Stecklina

Architecture-level Security Vulnerabilities. Julian Stecklina Architecture-level Security Vulnerabilities Julian Stecklina Outline How stacks work Smashing the stack for fun and profit Preventing stack smashing attacks Circumventing stack smashing prevention The

More information

20: Exploits and Containment

20: Exploits and Containment 20: Exploits and Containment Mark Handley Andrea Bittau What is an exploit? Programs contain bugs. These bugs could have security implications (vulnerabilities) An exploit is a tool which exploits a vulnerability

More information

From Over ow to Shell

From Over ow to Shell From Over ow to Shell An Introduction to low-level exploitation Carl Svensson @ Google, December 2018 1 / 25 Biography MSc in Computer Science, KTH Head of Security, KRY/LIVI CTF: HackingForSoju E-mail:

More information

CSE 127 Computer Security

CSE 127 Computer Security CSE 127 Computer Security Stefan Savage, Spring 2018, Lecture 4 Low Level Software Security II: Format Strings, Shellcode, & Stack Protection Review Function arguments and local variables are stored on

More information

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong This is CS50. Harvard University. Fall 2014. Cheng Gong Table of Contents News... 1 Buffer Overflow... 1 Malloc... 6 Linked Lists... 7 Searching... 13 Inserting... 16 Removing... 19 News Good news everyone!

More information

Buffer Overflow and Protection Technology. Department of Computer Science,

Buffer Overflow and Protection Technology. Department of Computer Science, Buffer Overflow and Protection Technology Department of Computer Science, Lorenzo Cavallaro Andrea Lanzi Table of Contents Introduction

More information

CSE 127 Computer Security

CSE 127 Computer Security CSE 127 Computer Security Stefan Savage, Spring 2018, Lecture 6 Low Level Software Security IV: Heap Corruption Memory management in C The C programming language uses explicit memory management Data is

More information

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

More information