SlideShare uma empresa Scribd logo
1 de 59
Baixar para ler offline
Large memory management vulnerabilities
Large memory management
Large memory management
vulnerabilities
vulnerabilities
System, compiler, and application issues
System, compiler, and application issues
Gaël Delalleau
gael.delalleau@beijaflore.com
gael.delalleau+csw@m4x.org
Security consultant from
http://www.beijaflore.com
CancSecWest 2005
Vancouver – May 4-6
Large memory management vulnerabilities
Agenda
Agenda
▐ Dynamic map of a process virtual memory space
►Operating systems & compilers security issues
►Exploiting unexploitable bugs
►Application flaws dealing with large data sizes
▐ Easy to exploit? Easy to protect from?
OS, cc
issues
Exploiting
more bugs
Application
flaws
Large memory management vulnerabilities
Introduction (1/2)
Introduction (1/2)
▐ Large (N GB) memory sizes now common
► Memory size = RAM + swap
► Servers
► Desktop (games, multiple apps)
▐ A process can alloc 1 to 3 GB depending on OS
► An application may need to handle large (N GB) data sizes
► 32 bits CPUs : the whole virtual memory space can be filled
► “Out of memory” situations (OOM) in a process are not fatal
▐ 64 bits CPUs give much more virtual space
Large memory management vulnerabilities
Introduction (2/2)
Introduction (2/2)
▐ Big memory usage situations are badly handled,
introducing exploitable holes in applications
► Operating systems : break the usual behavior rules about stack, heap,
mappings at page 0...
► Compilers : introduce security flaws in valid application code
► Applications : 32 bits counters overflow and sign problems
▐ “Unexploitable” bugs may be exploited to run
arbitrary code in a process
► NULL pointers dereference, common in OOM conditions
► Buffer overflows and underflows may corrupt an adjacent memory
area
Large memory management vulnerabilities
Dynamic map of a process
Dynamic map of a process
virtual memory space
virtual memory space
Large memory management vulnerabilities
► Naive view
► Naive view memory protections
► Solaris 10
► FreeBSD 5.3
► Linux 2.6
Dynamic map of a process virtual
Dynamic map of a process virtual
memory space
memory space
Large memory management vulnerabilities
Virtual memory space: naive view
Virtual memory space: naive view
00000000
FFFFFFFF
(32 bits)
Protected memory reserved
for the OS (kernel)
Executable code
Static data
Dynamically allocated data: “heap”
Memory holding environment, arguments,
local variables and execution flow control
structures: “stack”
brk()
Unmapped memory
Unmapped memory
Mapped from executable
file at a static address
Large memory management vulnerabilities
Memory protections (naive)
Memory protections (naive)
00000000
FFFFFFFF
(32 bits)
Protected memory reserved
for the OS (kernel)
Executable code
Static data
Dynamically allocated data: “heap”
Memory holding environment, arguments,
local variables and execution flow control
structures: “stack”
brk()
Unmapped memory
Mapped from executable
file at a static address
A “guard page” prevents heap and stack from
colliding
Trying to access unmapped memory throws an
exception or a segmentation fault signal
=> protects against NULL pointer dereference
Large memory management vulnerabilities
Let's dive into the real world
Let's dive into the real world
▐ No standard for memory allocation behavior
▐ Major changes between vendors and versions of:
► OS
► Libc
► Threading library
► Compiler and linker
▐ Additional mappings
► Dynamic libraries: code and data
► Additional heaps and stacks (threads...)
► Anonymous memory (mmap, VirtualAlloc...)
► Shared memory (IPC)
► Files mapped in memory
► System mappings: PEB, TEB (Windows), vsyscall (Linux), ...
▐ Next slides show the behavior of real systems
Large memory management vulnerabilities
Solaris 10 / x86
Solaris 10 / x86
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08050000
Mapping allowed, but can't be reached
with default limits
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [continuum]
Gap area, mapping impossible
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Upper and lower limits of the heap
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128 M)
Large memory management vulnerabilities
FreeBSD 5.3
FreeBSD 5.3
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Stack growing down [continuum]
Heap growing up [continuum]
Mmap area “from bottom to top”
[fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Limit between heap and mmap area
Status of unallocated memory :
Mapping allowed
Mapping forbidden
Large memory management vulnerabilities
Operating systems and compilers
Operating systems and compilers
security issues
security issues
Large memory management vulnerabilities
►Heap / stack overlap
►Jumping the stack gap
►Example of a memory management kernel bug
Operating systems and compilers
Operating systems and compilers
security issues
security issues
Large memory management vulnerabilities
Heap / stack overlap
Heap / stack overlap
▐ Can heap and stack “collide”?
► Heap grows up... stack grows down...
► Collision or not: depends on process VM map
► Two protections mechanisms at bottom of stack
 Gap page(s): mappings forbidden
 Guard page(s): PROT_NONE mapping
▐ Linux 2.6
► No gap, no guard page!
► mmap() allocates close to bottom of stack if low mem
(kernel>=2.6.9 ?)
► Heap allocations use mmap if size>128K or if low memory condition
► Thus heap and stack can be contiguous!
OS, cc
issues
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
Heap and stack are contiguous.
There is no unmapped memory at bottom
of stack.
Stack growth and stack overflow signaling
are no longer handled by the kernel since
they rely on page faults access at bottom
of stack.
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
%esp lies in
heap mapping
(for purists: the
stack and heap
VMAs don't
overlap)
Stack growth
needed by
application
Large memory management vulnerabilities
Heap / stack overlap Demo
Heap / stack overlap Demo
▐ Exploiting mod_php 4.3.0 on Apache 2.0.53
► Goal: execute assembly code from a restricted PHP script
► Allows for breaking out of safe_mode
► Needs ability to allocate ~3 GB of memory
 Enough RAM + swap
 Disabled PHP memory_limit option, or use a memory leak
▐ Exploit scenario
► Allocate large blocks of memory with emalloc() => malloc()
► Call recursive function many times
 the stack “goes down” and overlap with one of the allocated block
 R/W access to this block == R/W access to stack memory :-)
 Modify a saved EIP address in stack to point to shellcode and return
OS, cc
issues
Large memory management vulnerabilities
Vulnerability Status
Vulnerability Status
for heap / stack overlap
for heap / stack overlap
► Linux 2.4
► Linux 2.6
► FreeBSD 5.3
► OpenBSD 3.6
► Linux emulation on FreeBSD 5.3
► Linux emulation on OpenBSD 3.6
► Solaris 10 / x86
► Solaris 9 / Sparc
► Windows XP SP1
► Any OS with certain uncommon
threading libraries
SAFE
UNSAFE
MMAP UNSAFE
SAFE (but...)
UNSAFE
SAFE (but...)
SAFE
SAFE
SAFE
UNSAFE
OS, cc
issues
Large memory management vulnerabilities
Jumping the stack gap
Jumping the stack gap
▐ Protection with gap or guard page: unsafe
► A few KB under the stack are protected by the OS
 No other mapping can lie there
 OS grows the stack mapping if a GP fault happens below the stack
 If the stack can't be grown a SIGSEGV is delivered
► BUT: the application controls the stack pointer, not the OS
 Local (“automatic”) variables allocation on function calls
 Usage of alloca()
▐ Vulnerability is not in the application C code...
▐ ... but may be introduced by the compiler
OS, cc
issues
Large memory management vulnerabilities
Normal behavior
Normal behavior
Gap or guard page(s) : a few KB
Stack (MAP_GROWSDOWN
mapping on Linux)
Other mappings
int
f(char *src) {
char buffer1[5000];
char buffer2[5000];
memcpy(buffer2, src, 5000);
...
return 0;
}
%esp
%esp - 10008
OS, cc
issues
Large memory management vulnerabilities
Normal behavior
Normal behavior
Gap or guard page(s) : a few KB
Stack (MAP_GROWSDOWN
mapping on Linux)
Other mappings
int
f(char *src) {
char buffer1[5000];
char buffer2[5000];
memcpy(buffer2, src, 5000);
...
return 0;
}
%esp
%esp - 10008 %esp
OS, cc
issues
Large memory management vulnerabilities
Jumping the gap
Jumping the gap
Gap or guard page(s) : a few KB
Stack
Heap or
other
interesting
mappings
allocated
just under
the stack
int
f(char *src) {
char buffer1[5000];
char buffer2[5000];
memcpy(buffer2, src, 5000);
...
return 0;
}
%esp
%esp - 10008
OS, cc
issues
Large memory management vulnerabilities
Jumping the gap
Jumping the gap
Gap or guard page(s) : a few KB
Stack
Heap or
other
interesting
mappings
allocated
just under
the stack
int
f(char *src) {
char buffer1[5000];
char buffer2[5000];
memcpy(buffer2, src, 5000);
...
return 0;
}
%esp
%esp - 10008 %esp
Heap or
other
interesting
mappings
OVERLAP
with stack !
OS, cc
issues
Large memory management vulnerabilities
GCC default behaviour unsafe
GCC default behaviour unsafe
▐ Allocation of local variables on stack
Prologue: sub $size, %esp
► $size > size of protected gap >= size of a page (4096 B)
► Area not accessed until told by the application
► If a mapping exists at %esp = %esp_old - $size, then access to
%esp doesn't create a GP fault: “jumping the gap”
▐ Same problem with alloca(size)
► Inlined as sub $size, %esp
► No sanity check on $size
 $size can be larger than gap size
 $size can be negative
OS, cc
issues
Large memory management vulnerabilities
Example on Solaris 9 / Sparc
Example on Solaris 9 / Sparc
▐ Big gap, between 16 KB and 64 KB
► depends on stack size limit
▐ ld.so is mapped below the stack size limit
► If we jump the gap, stack variables will overwrite the .data and .bss
sections of ld.so
▐ Most applications will not be vulnerable
► Need for a function with a huge unused local variable or alloca()
► We must access this function when %esp is close from stack limit
▐ We can control %pc on vulnerable applications
► ld.so data section has pointers to function pointers, which are called
OS, cc
issues
Large memory management vulnerabilities
Forcing safer stack allocations
Forcing safer stack allocations
▐ Use gcc flag -fstack-check
► A NULL byte is written every 4096 bytes in the allocated area
► The gap or guard page will be hit, forcing stack growth
► If stack is unable to grow the kernel delivers a signal to the process
► This is the default on Windows
 the kernel uses only guard page accesses to grow the stack
 access below the guard page would trigger a 0xC0000005 exception
▐ alloca(size) also checked...
▐ ...yet negative sizes (>2G) are still unsafe
OS, cc
issues
Large memory management vulnerabilities
Vulnerability Status
Vulnerability Status
for “gap jump”
for “gap jump”
► GCC on UNIX (default)
► GCC on UNIX (with -fstack-check)
► Other compilers on UNIX
► Any good compiler on Windows
UNSAFE
SAFE
UNTESTED
SAFE
OS, cc
issues
Large memory management vulnerabilities
Example of a memory
Example of a memory
management bug in kernel
management bug in kernel
▐ Small flaw in mmap() allocations on OpenBSD
► size = 3,9G => error, mmap() returns -1 (OOM)
► 4G-4096 < size <= 4G => success, but allocates nothing
▐ The flaw lies in kernel code
#define round_page(x) (((x) + PAGE_MASK) & ~PAGE_MASK)
In sys_mmap(p, v, retval):
pageoff = (pos & PAGE_MASK);
size += pageoff; /* add offset */
size = (vsize_t) round_page(size); /* round up */
if ((ssize_t) size < 0) return (EINVAL); /* don't allow wrap */
▐ Some applications might be at risk
► If mmap() call with a size parameter we can control (file mapping?)
► Exploitation: access to other mappings instead of the expected one
OS, cc
issues
Large memory management vulnerabilities
Exploiting unexploitable bugs
Exploiting unexploitable bugs
Large memory management vulnerabilities
►Exploiting NULL pointers (OOM crashes)
►Exploiting other bugs using mapping overflows
Exploiting unexploitable bugs
Exploiting unexploitable bugs
Large memory management vulnerabilities
Exploiting NULL pointers
Exploiting NULL pointers
Using OOM “crashes” to run arbitrary code
Using OOM “crashes” to run arbitrary code
▐ malloc(size) returns NULL (00000000) if OOM
► Flawed applications fail to check this return value
► Dereferencing the NULL pointer access unallocated memory => OS
sends SIGSEGV or exception
► This is the expected behavior (documented)... BUT!
▐ We might be able to exploit this to run ASM code
► On some OSes we can map the first page at address 0
► On some applications the address really accessed is not 0
Exploiting
more bugs
Large memory management vulnerabilities
Creating a mapping
Creating a mapping
at address 0
at address 0
▐ On Linux 2.6.x mmap() can allocate the first page
► So malloc() can too
► We just need to fill the available memory space
▐ On Solaris 10/x86, the stack can “grow” down to 0
► But only if the default stack size limit has been increased
Exploiting
more bugs
Large memory management vulnerabilities
Solaris 10 / x86
Solaris 10 / x86
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08050000
Mapping allowed, but can't be reached
with default limits
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [continuum]
Gap area, mapping impossible
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Upper and lower limits of the heap
Large memory management vulnerabilities
Linux 2.6
Linux 2.6
00000000
FFFFFFFF
heap
ELF
mmap
stack
libs
4 Gb
3 Gb
2 Gb
1 Gb
08048000
Mapping allowed
Mapping forbidden
Status of unallocated memory :
Stack growing down [continuum]
Heap growing up [fragmented]
“top down” mmap area [fragmented]
ELF mapping : code segment [r-x]
ELF mapping : data segment [rwx]
Lower limit of the stack (default 128
M)
Large memory management vulnerabilities
Example
Example
▐ Sample vulnerable code
/* Let's copy 'userdata' into 'buffer' */
size = strlen(userdata) + 1;
buffer = (char *) malloc(size); // no check of return value
memcpy(buffer, userdata, size); // buffer may be 00000000
▐ On Linux 2.6 heap overflow situation
▐ On Solaris 10/x86 stack overflow situation
Exploiting
more bugs
Large memory management vulnerabilities
Vulnerability Status
Vulnerability Status
for memory allocation at 0
for memory allocation at 0
► Linux 2.4
► Linux 2.6
► FreeBSD 5.3
► OpenBSD 3.6
► Linux emulation on FreeBSD 5.3
► Linux emulation on OpenBSD 3.6
► Solaris 10 / x86
► Solaris 9 / Sparc
► Windows XP SP1
SAFE
UNSAFE
SAFE
SAFE
SAFE
SAFE
UNSAFE
SAFE
SAFE
OS, cc
issues
Large memory management vulnerabilities
Table offsets
Table offsets
▐ Access to buffer[i] == *(buffer+i) instead of *buffer
► Means access to *(i) if buffer is NULL
► Can be in a valid mapping!
 Depends on how much control we have on the index i
 Depends on how close to address 0 we can put a mapping
▐ Vulnerable code sample
numberOfMessages += 1;
buffer = realloc(buffer, sizeof(imapFlags) * numberOfMessages);
(...) // no check to see if buffer resizing has failed
buffer[numberOfMessages-1] = messageFlags;
// could be a write to *(0+numberOfMessages-1)
Exploiting
more bugs
Large memory management vulnerabilities
C++ “NULL” objects
C++ “NULL” objects
▐ A high-level allocation function might return a
“NULL” instance of a C++ class on failure
► Static object stored in .(ro)data
► Heap corruption may happen if return value is not checked
▐ Vulnerable code real-life example (Mozilla)
When it fails to allocate memory, the ReplacePrep function "nullifies" the string :
mData = NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer);
mLength = 0;
But in nsTSubstring_CharT::Replace the return value was not checked:
size_type length = tuple.Length();
cutStart = PR_MIN(cutStart, Length());
ReplacePrep(cutStart, cutLength, length);
if (length > 0) tuple.WriteTo(mData + cutStart, length);
Exploiting
more bugs
Large memory management vulnerabilities
Exploiting other bugs
Exploiting other bugs
with mapping overflows
with mapping overflows
▐ No gap / guard page enforced between mappings
► Not enough protections on most systems
► Contiguous mappings happen if large memory usage
► Allows us to turn an overflow or underflow...
► ... into corruption of another memory area
▐ May help to solve some exploitation problems
 Difficult heap buffer overflows (end of heap, new GLibc and XP SP2
protections)
 Large size memcpy heap overflows (-1 == 4G) that would trigger a crash
 If a mapping is allowed on top of stack (threads, grsec random stack...):
• Stack buffer overflow in argv() or env, in main() when main() never returns,
big overflows with propolice-like protection...
 Buffer underflows (also on stack on Linux 2.6)
Exploiting
more bugs
Large memory management vulnerabilities
Example: exploiting Windows XP
Example: exploiting Windows XP
heap overflows with a mapping overflow
heap overflows with a mapping overflow
► By filling the memory we may
allocate heap next to Thread
Environment Blocks (TEB)...
Exploiting
more bugs
Heap
TEB 1
TEB 2
TEB N-1
TEB N
TEB 3
Overflow of
controlled
size
Large size
overflow
(memcpy with
size -1...)
Top of VM
Large memory management vulnerabilities
Example: exploiting Windows XP
Example: exploiting Windows XP
heap overflows with a mapping overflow
heap overflows with a mapping overflow
► By filling the memory we may
allocate heap next to Thread
Environment Blocks (TEB)...
Exploiting
more bugs
Heap
TEB 1
TEB 2
TEB N-1
TEB N
TEB 3
Overflow of
controlled
size
Large size
overflow
(memcpy with
size -1...)
...
other
TEB data
...
bottom of stack
top of stack
SEH chain ptr
Heap
Top of VM
FFFFFFFF
ptr to code
“shellcode”
► ...and
overwrite the
SEH chain
pointer of our
thread's TEB
to redirect
execution flow
Sanity checks in
ntdll.dll can be
bypassed by tuning
the “top of stack”
and “bottom of
stack” pointers
Large memory management vulnerabilities
Application flaws
Application flaws
dealing with large data sizes
dealing with large data sizes
Large memory management vulnerabilities
►Analysis of integer overflows in 32 bits counters
►Handling of library calls in OOM situations
►The MMAP_FIXED aberration
Application flaws
Application flaws
dealing with large data sizes
dealing with large data sizes
Large memory management vulnerabilities
Integer overflows and sign
Integer overflows and sign
problems in 32 bits counters
problems in 32 bits counters
▐ 32 bits integer overflows when handling large data
► Impossible to allocate 4G on 32 bits CPU
► Nevertheless math calculations can make integer wrap
 Multiply by 3 (base64 decoding...): if we can allocate 1.3G contiguous
int len = strlen(data);
bufLen = (len * 3)/4;
 Multiply by 2 (string escape, buffer growth...): 2.7 G cont. max on Lin. 2.6
▐ 32 bits integer sign problems
► Appear when len > 2G
int len = strlen(ptr);
if (len > buf_length) buf = realloc(buf, len+1);
strcpy(buf, ptr); // overflow if reallocation was not done
Application
flaws
Large memory management vulnerabilities
Handling of library calls
Handling of library calls
in OOM situations
in OOM situations
▐ Idea: applications do not check return values for
library calls that are trivial or that “always” success,
but some of them will not do their job in OOM
situations
► Applications make wrong assumptions about the actions they took
► It may create application malfunction and errors, some potentially
exploitable
▐ Needs more research!
Application
flaws
Large memory management vulnerabilities
Usage of MMAP_FIXED
Usage of MMAP_FIXED
▐ Calling mmap() with the MMAP_FIXED parameter
destroys any previous mapping at the address
▐ It is impossible to safely predict an address where a
hole will exist in memory
► Example for Linux programs:
 Differences in allocation behavior between Linux 2.4 and 2.6
 Linux emulation layers on other OSes has a different behavior
 User interaction (big memory allocations)
▐ Thus, its use is unsafe
Application
flaws
Large memory management vulnerabilities
Easy to exploit?
Easy to exploit?
Easy to protect from?
Easy to protect from?
Large memory management vulnerabilities
►System limitations
►Network limitations
►Protecting ourselves
Easy to exploit?
Easy to exploit?
Easy to protect from?
Easy to protect from?
Large memory management vulnerabilities
System limitations
System limitations
▐ Memory size
► But VM can be overcommited if it is not accessed
▐ Resource limitations
► Stack size, data size, total VM size...
▐ Allocation speed
► RAM is quick even for GBs
► Becomes quite slow (minutes) when switching to disk swap
Large memory management vulnerabilities
Network limitations
Network limitations
▐ Upload speed
► GBs can be sent in minutes or hours on modern LAN and Internet
► Services timeouts are a problem
► Data zip-bombs, memory leaks, multithreading... can help
▐ Lack of information about the target
► Brute force is likely to be needed for these attacks
Large memory management vulnerabilities
Protection
Protection
▐ GCC flag -fstack-check
▐ On Linux 2.4: increase heap_stack_gap in /proc
▐ Application code security audit
▐ Memory limits handled by the application (PHP...)
► Memory leaks are to be treated as security bugs ;-)
▐ Resource limitation enforced by the OS
► Two edges sword
▐ Vendor patches?
Large memory management vulnerabilities
“
“Don't panic”
Don't panic”
▐ Very specific conditions may be necessary to
succeed in real life with these techniques
▐ Vulnerabilities introduced by OSes will be patched
▐ Switching to 64 bits will solve some of these issues
... and introduce new ones (ex: the long type is 64 bits on Unixes, but is
32 bits on Windows!)
▐ Yet some applications, on some systems, meet the
conditions and critical exploitable vulnerabilities
exist. (but don't panic and go audit your code)
Large memory management vulnerabilities
Conclusion
Conclusion
▐ Usage of large quantities of memory in modern
computing introduces unexpected vulnerabilities
▐ Security holes may exist in applications even if
their code is valid: the OS and the compiler break
usual safe assumptions
▐ Some of these flaws will be patched...
▐ ... others are classes of vulnerabilities that will last
► More research needs to be done:
 The area is broad, and my time is limited
 What about other OSes, threading libraries, compilers, embedded
systems, emulation layers (Linux...), virtual machines...
 How many applications are exploitable in real life situations?
Large memory management vulnerabilities
Thank you!
Thank you!
Feedback, questions, comments... are welcome
gael.delalleau@beijaflore.com
gael.delalleau+csw@m4x.org
These updated slides will be available on
http://www.cppsecurity.com
Any questions?
Thanks to Solar Designer and H. D. Moore. All errors are mine.

Mais conteúdo relacionado

Semelhante a memory_vulns_delalleau_cansecwest2005_talk.pdf

Performance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonPerformance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonJAXLondon2014
 
Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...
Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...
Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...Виталий Стародубцев
 
Talk 160920 @ Cat System Workshop
Talk 160920 @ Cat System WorkshopTalk 160920 @ Cat System Workshop
Talk 160920 @ Cat System WorkshopQuey-Liang Kao
 
AllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW SecurityAllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW SecurityAllBits BVBA (freelancer)
 
HBase at Xiaomi
HBase at XiaomiHBase at Xiaomi
HBase at XiaomiHBaseCon
 
[h2hc] Generic exploitation of invalid memory writes
[h2hc] Generic exploitation of invalid memory writes[h2hc] Generic exploitation of invalid memory writes
[h2hc] Generic exploitation of invalid memory writesMoabi.com
 
[Pgday.Seoul 2018] PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
[Pgday.Seoul 2018]  PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha[Pgday.Seoul 2018]  PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
[Pgday.Seoul 2018] PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposhaPgDay.Seoul
 
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)Elvin Gentiles
 
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...DefconRussia
 
Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Tim Bunce
 
Linux binary analysis and exploitation
Linux binary analysis and exploitationLinux binary analysis and exploitation
Linux binary analysis and exploitationDharmalingam Ganesan
 
[ENG] Hacktivity 2013 - Alice in eXploitland
[ENG] Hacktivity 2013 - Alice in eXploitland[ENG] Hacktivity 2013 - Alice in eXploitland
[ENG] Hacktivity 2013 - Alice in eXploitlandZoltan Balazs
 
Putting Wings on the Elephant
Putting Wings on the ElephantPutting Wings on the Elephant
Putting Wings on the ElephantDataWorks Summit
 
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Ontico
 
Less is More: 2X Storage Efficiency with HDFS Erasure Coding
Less is More: 2X Storage Efficiency with HDFS Erasure CodingLess is More: 2X Storage Efficiency with HDFS Erasure Coding
Less is More: 2X Storage Efficiency with HDFS Erasure CodingZhe Zhang
 

Semelhante a memory_vulns_delalleau_cansecwest2005_talk.pdf (20)

Performance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonPerformance and Predictability - Richard Warburton
Performance and Predictability - Richard Warburton
 
Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...
Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...
Технологии работы с дисковыми хранилищами и файловыми системами Windows Serve...
 
Talk 160920 @ Cat System Workshop
Talk 160920 @ Cat System WorkshopTalk 160920 @ Cat System Workshop
Talk 160920 @ Cat System Workshop
 
L05 parallel
L05 parallelL05 parallel
L05 parallel
 
AllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW SecurityAllBits presentation - Lower Level SW Security
AllBits presentation - Lower Level SW Security
 
The Cell Processor
The Cell ProcessorThe Cell Processor
The Cell Processor
 
HBase at Xiaomi
HBase at XiaomiHBase at Xiaomi
HBase at Xiaomi
 
[h2hc] Generic exploitation of invalid memory writes
[h2hc] Generic exploitation of invalid memory writes[h2hc] Generic exploitation of invalid memory writes
[h2hc] Generic exploitation of invalid memory writes
 
[Pgday.Seoul 2018] PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
[Pgday.Seoul 2018]  PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha[Pgday.Seoul 2018]  PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
[Pgday.Seoul 2018] PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
 
Advanced Windows Exploitation
Advanced Windows ExploitationAdvanced Windows Exploitation
Advanced Windows Exploitation
 
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
 
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
Nikita Abdullin - Reverse-engineering of embedded MIPS devices. Case Study - ...
 
Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )
 
Linux binary analysis and exploitation
Linux binary analysis and exploitationLinux binary analysis and exploitation
Linux binary analysis and exploitation
 
Basics of JVM Tuning
Basics of JVM TuningBasics of JVM Tuning
Basics of JVM Tuning
 
[ENG] Hacktivity 2013 - Alice in eXploitland
[ENG] Hacktivity 2013 - Alice in eXploitland[ENG] Hacktivity 2013 - Alice in eXploitland
[ENG] Hacktivity 2013 - Alice in eXploitland
 
Putting Wings on the Elephant
Putting Wings on the ElephantPutting Wings on the Elephant
Putting Wings on the Elephant
 
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
 
Local Exploits
Local ExploitsLocal Exploits
Local Exploits
 
Less is More: 2X Storage Efficiency with HDFS Erasure Coding
Less is More: 2X Storage Efficiency with HDFS Erasure CodingLess is More: 2X Storage Efficiency with HDFS Erasure Coding
Less is More: 2X Storage Efficiency with HDFS Erasure Coding
 

Último

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Último (20)

Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

memory_vulns_delalleau_cansecwest2005_talk.pdf

  • 1. Large memory management vulnerabilities Large memory management Large memory management vulnerabilities vulnerabilities System, compiler, and application issues System, compiler, and application issues Gaël Delalleau gael.delalleau@beijaflore.com gael.delalleau+csw@m4x.org Security consultant from http://www.beijaflore.com CancSecWest 2005 Vancouver – May 4-6
  • 2. Large memory management vulnerabilities Agenda Agenda ▐ Dynamic map of a process virtual memory space ►Operating systems & compilers security issues ►Exploiting unexploitable bugs ►Application flaws dealing with large data sizes ▐ Easy to exploit? Easy to protect from? OS, cc issues Exploiting more bugs Application flaws
  • 3. Large memory management vulnerabilities Introduction (1/2) Introduction (1/2) ▐ Large (N GB) memory sizes now common ► Memory size = RAM + swap ► Servers ► Desktop (games, multiple apps) ▐ A process can alloc 1 to 3 GB depending on OS ► An application may need to handle large (N GB) data sizes ► 32 bits CPUs : the whole virtual memory space can be filled ► “Out of memory” situations (OOM) in a process are not fatal ▐ 64 bits CPUs give much more virtual space
  • 4. Large memory management vulnerabilities Introduction (2/2) Introduction (2/2) ▐ Big memory usage situations are badly handled, introducing exploitable holes in applications ► Operating systems : break the usual behavior rules about stack, heap, mappings at page 0... ► Compilers : introduce security flaws in valid application code ► Applications : 32 bits counters overflow and sign problems ▐ “Unexploitable” bugs may be exploited to run arbitrary code in a process ► NULL pointers dereference, common in OOM conditions ► Buffer overflows and underflows may corrupt an adjacent memory area
  • 5. Large memory management vulnerabilities Dynamic map of a process Dynamic map of a process virtual memory space virtual memory space
  • 6. Large memory management vulnerabilities ► Naive view ► Naive view memory protections ► Solaris 10 ► FreeBSD 5.3 ► Linux 2.6 Dynamic map of a process virtual Dynamic map of a process virtual memory space memory space
  • 7. Large memory management vulnerabilities Virtual memory space: naive view Virtual memory space: naive view 00000000 FFFFFFFF (32 bits) Protected memory reserved for the OS (kernel) Executable code Static data Dynamically allocated data: “heap” Memory holding environment, arguments, local variables and execution flow control structures: “stack” brk() Unmapped memory Unmapped memory Mapped from executable file at a static address
  • 8. Large memory management vulnerabilities Memory protections (naive) Memory protections (naive) 00000000 FFFFFFFF (32 bits) Protected memory reserved for the OS (kernel) Executable code Static data Dynamically allocated data: “heap” Memory holding environment, arguments, local variables and execution flow control structures: “stack” brk() Unmapped memory Mapped from executable file at a static address A “guard page” prevents heap and stack from colliding Trying to access unmapped memory throws an exception or a segmentation fault signal => protects against NULL pointer dereference
  • 9. Large memory management vulnerabilities Let's dive into the real world Let's dive into the real world ▐ No standard for memory allocation behavior ▐ Major changes between vendors and versions of: ► OS ► Libc ► Threading library ► Compiler and linker ▐ Additional mappings ► Dynamic libraries: code and data ► Additional heaps and stacks (threads...) ► Anonymous memory (mmap, VirtualAlloc...) ► Shared memory (IPC) ► Files mapped in memory ► System mappings: PEB, TEB (Windows), vsyscall (Linux), ... ▐ Next slides show the behavior of real systems
  • 10. Large memory management vulnerabilities Solaris 10 / x86 Solaris 10 / x86 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08050000 Mapping allowed, but can't be reached with default limits Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [continuum] Gap area, mapping impossible “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Upper and lower limits of the heap
  • 11. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M)
  • 12. Large memory management vulnerabilities FreeBSD 5.3 FreeBSD 5.3 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Stack growing down [continuum] Heap growing up [continuum] Mmap area “from bottom to top” [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Limit between heap and mmap area Status of unallocated memory : Mapping allowed Mapping forbidden
  • 13. Large memory management vulnerabilities Operating systems and compilers Operating systems and compilers security issues security issues
  • 14. Large memory management vulnerabilities ►Heap / stack overlap ►Jumping the stack gap ►Example of a memory management kernel bug Operating systems and compilers Operating systems and compilers security issues security issues
  • 15. Large memory management vulnerabilities Heap / stack overlap Heap / stack overlap ▐ Can heap and stack “collide”? ► Heap grows up... stack grows down... ► Collision or not: depends on process VM map ► Two protections mechanisms at bottom of stack  Gap page(s): mappings forbidden  Guard page(s): PROT_NONE mapping ▐ Linux 2.6 ► No gap, no guard page! ► mmap() allocates close to bottom of stack if low mem (kernel>=2.6.9 ?) ► Heap allocations use mmap if size>128K or if low memory condition ► Thus heap and stack can be contiguous! OS, cc issues
  • 16. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M)
  • 17. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M)
  • 18. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M)
  • 19. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M)
  • 20. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M) Heap and stack are contiguous. There is no unmapped memory at bottom of stack. Stack growth and stack overflow signaling are no longer handled by the kernel since they rely on page faults access at bottom of stack.
  • 21. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M) %esp lies in heap mapping (for purists: the stack and heap VMAs don't overlap) Stack growth needed by application
  • 22. Large memory management vulnerabilities Heap / stack overlap Demo Heap / stack overlap Demo ▐ Exploiting mod_php 4.3.0 on Apache 2.0.53 ► Goal: execute assembly code from a restricted PHP script ► Allows for breaking out of safe_mode ► Needs ability to allocate ~3 GB of memory  Enough RAM + swap  Disabled PHP memory_limit option, or use a memory leak ▐ Exploit scenario ► Allocate large blocks of memory with emalloc() => malloc() ► Call recursive function many times  the stack “goes down” and overlap with one of the allocated block  R/W access to this block == R/W access to stack memory :-)  Modify a saved EIP address in stack to point to shellcode and return OS, cc issues
  • 23. Large memory management vulnerabilities Vulnerability Status Vulnerability Status for heap / stack overlap for heap / stack overlap ► Linux 2.4 ► Linux 2.6 ► FreeBSD 5.3 ► OpenBSD 3.6 ► Linux emulation on FreeBSD 5.3 ► Linux emulation on OpenBSD 3.6 ► Solaris 10 / x86 ► Solaris 9 / Sparc ► Windows XP SP1 ► Any OS with certain uncommon threading libraries SAFE UNSAFE MMAP UNSAFE SAFE (but...) UNSAFE SAFE (but...) SAFE SAFE SAFE UNSAFE OS, cc issues
  • 24. Large memory management vulnerabilities Jumping the stack gap Jumping the stack gap ▐ Protection with gap or guard page: unsafe ► A few KB under the stack are protected by the OS  No other mapping can lie there  OS grows the stack mapping if a GP fault happens below the stack  If the stack can't be grown a SIGSEGV is delivered ► BUT: the application controls the stack pointer, not the OS  Local (“automatic”) variables allocation on function calls  Usage of alloca() ▐ Vulnerability is not in the application C code... ▐ ... but may be introduced by the compiler OS, cc issues
  • 25. Large memory management vulnerabilities Normal behavior Normal behavior Gap or guard page(s) : a few KB Stack (MAP_GROWSDOWN mapping on Linux) Other mappings int f(char *src) { char buffer1[5000]; char buffer2[5000]; memcpy(buffer2, src, 5000); ... return 0; } %esp %esp - 10008 OS, cc issues
  • 26. Large memory management vulnerabilities Normal behavior Normal behavior Gap or guard page(s) : a few KB Stack (MAP_GROWSDOWN mapping on Linux) Other mappings int f(char *src) { char buffer1[5000]; char buffer2[5000]; memcpy(buffer2, src, 5000); ... return 0; } %esp %esp - 10008 %esp OS, cc issues
  • 27. Large memory management vulnerabilities Jumping the gap Jumping the gap Gap or guard page(s) : a few KB Stack Heap or other interesting mappings allocated just under the stack int f(char *src) { char buffer1[5000]; char buffer2[5000]; memcpy(buffer2, src, 5000); ... return 0; } %esp %esp - 10008 OS, cc issues
  • 28. Large memory management vulnerabilities Jumping the gap Jumping the gap Gap or guard page(s) : a few KB Stack Heap or other interesting mappings allocated just under the stack int f(char *src) { char buffer1[5000]; char buffer2[5000]; memcpy(buffer2, src, 5000); ... return 0; } %esp %esp - 10008 %esp Heap or other interesting mappings OVERLAP with stack ! OS, cc issues
  • 29. Large memory management vulnerabilities GCC default behaviour unsafe GCC default behaviour unsafe ▐ Allocation of local variables on stack Prologue: sub $size, %esp ► $size > size of protected gap >= size of a page (4096 B) ► Area not accessed until told by the application ► If a mapping exists at %esp = %esp_old - $size, then access to %esp doesn't create a GP fault: “jumping the gap” ▐ Same problem with alloca(size) ► Inlined as sub $size, %esp ► No sanity check on $size  $size can be larger than gap size  $size can be negative OS, cc issues
  • 30. Large memory management vulnerabilities Example on Solaris 9 / Sparc Example on Solaris 9 / Sparc ▐ Big gap, between 16 KB and 64 KB ► depends on stack size limit ▐ ld.so is mapped below the stack size limit ► If we jump the gap, stack variables will overwrite the .data and .bss sections of ld.so ▐ Most applications will not be vulnerable ► Need for a function with a huge unused local variable or alloca() ► We must access this function when %esp is close from stack limit ▐ We can control %pc on vulnerable applications ► ld.so data section has pointers to function pointers, which are called OS, cc issues
  • 31. Large memory management vulnerabilities Forcing safer stack allocations Forcing safer stack allocations ▐ Use gcc flag -fstack-check ► A NULL byte is written every 4096 bytes in the allocated area ► The gap or guard page will be hit, forcing stack growth ► If stack is unable to grow the kernel delivers a signal to the process ► This is the default on Windows  the kernel uses only guard page accesses to grow the stack  access below the guard page would trigger a 0xC0000005 exception ▐ alloca(size) also checked... ▐ ...yet negative sizes (>2G) are still unsafe OS, cc issues
  • 32. Large memory management vulnerabilities Vulnerability Status Vulnerability Status for “gap jump” for “gap jump” ► GCC on UNIX (default) ► GCC on UNIX (with -fstack-check) ► Other compilers on UNIX ► Any good compiler on Windows UNSAFE SAFE UNTESTED SAFE OS, cc issues
  • 33. Large memory management vulnerabilities Example of a memory Example of a memory management bug in kernel management bug in kernel ▐ Small flaw in mmap() allocations on OpenBSD ► size = 3,9G => error, mmap() returns -1 (OOM) ► 4G-4096 < size <= 4G => success, but allocates nothing ▐ The flaw lies in kernel code #define round_page(x) (((x) + PAGE_MASK) & ~PAGE_MASK) In sys_mmap(p, v, retval): pageoff = (pos & PAGE_MASK); size += pageoff; /* add offset */ size = (vsize_t) round_page(size); /* round up */ if ((ssize_t) size < 0) return (EINVAL); /* don't allow wrap */ ▐ Some applications might be at risk ► If mmap() call with a size parameter we can control (file mapping?) ► Exploitation: access to other mappings instead of the expected one OS, cc issues
  • 34. Large memory management vulnerabilities Exploiting unexploitable bugs Exploiting unexploitable bugs
  • 35. Large memory management vulnerabilities ►Exploiting NULL pointers (OOM crashes) ►Exploiting other bugs using mapping overflows Exploiting unexploitable bugs Exploiting unexploitable bugs
  • 36. Large memory management vulnerabilities Exploiting NULL pointers Exploiting NULL pointers Using OOM “crashes” to run arbitrary code Using OOM “crashes” to run arbitrary code ▐ malloc(size) returns NULL (00000000) if OOM ► Flawed applications fail to check this return value ► Dereferencing the NULL pointer access unallocated memory => OS sends SIGSEGV or exception ► This is the expected behavior (documented)... BUT! ▐ We might be able to exploit this to run ASM code ► On some OSes we can map the first page at address 0 ► On some applications the address really accessed is not 0 Exploiting more bugs
  • 37. Large memory management vulnerabilities Creating a mapping Creating a mapping at address 0 at address 0 ▐ On Linux 2.6.x mmap() can allocate the first page ► So malloc() can too ► We just need to fill the available memory space ▐ On Solaris 10/x86, the stack can “grow” down to 0 ► But only if the default stack size limit has been increased Exploiting more bugs
  • 38. Large memory management vulnerabilities Solaris 10 / x86 Solaris 10 / x86 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08050000 Mapping allowed, but can't be reached with default limits Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [continuum] Gap area, mapping impossible “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Upper and lower limits of the heap
  • 39. Large memory management vulnerabilities Linux 2.6 Linux 2.6 00000000 FFFFFFFF heap ELF mmap stack libs 4 Gb 3 Gb 2 Gb 1 Gb 08048000 Mapping allowed Mapping forbidden Status of unallocated memory : Stack growing down [continuum] Heap growing up [fragmented] “top down” mmap area [fragmented] ELF mapping : code segment [r-x] ELF mapping : data segment [rwx] Lower limit of the stack (default 128 M)
  • 40. Large memory management vulnerabilities Example Example ▐ Sample vulnerable code /* Let's copy 'userdata' into 'buffer' */ size = strlen(userdata) + 1; buffer = (char *) malloc(size); // no check of return value memcpy(buffer, userdata, size); // buffer may be 00000000 ▐ On Linux 2.6 heap overflow situation ▐ On Solaris 10/x86 stack overflow situation Exploiting more bugs
  • 41. Large memory management vulnerabilities Vulnerability Status Vulnerability Status for memory allocation at 0 for memory allocation at 0 ► Linux 2.4 ► Linux 2.6 ► FreeBSD 5.3 ► OpenBSD 3.6 ► Linux emulation on FreeBSD 5.3 ► Linux emulation on OpenBSD 3.6 ► Solaris 10 / x86 ► Solaris 9 / Sparc ► Windows XP SP1 SAFE UNSAFE SAFE SAFE SAFE SAFE UNSAFE SAFE SAFE OS, cc issues
  • 42. Large memory management vulnerabilities Table offsets Table offsets ▐ Access to buffer[i] == *(buffer+i) instead of *buffer ► Means access to *(i) if buffer is NULL ► Can be in a valid mapping!  Depends on how much control we have on the index i  Depends on how close to address 0 we can put a mapping ▐ Vulnerable code sample numberOfMessages += 1; buffer = realloc(buffer, sizeof(imapFlags) * numberOfMessages); (...) // no check to see if buffer resizing has failed buffer[numberOfMessages-1] = messageFlags; // could be a write to *(0+numberOfMessages-1) Exploiting more bugs
  • 43. Large memory management vulnerabilities C++ “NULL” objects C++ “NULL” objects ▐ A high-level allocation function might return a “NULL” instance of a C++ class on failure ► Static object stored in .(ro)data ► Heap corruption may happen if return value is not checked ▐ Vulnerable code real-life example (Mozilla) When it fails to allocate memory, the ReplacePrep function "nullifies" the string : mData = NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer); mLength = 0; But in nsTSubstring_CharT::Replace the return value was not checked: size_type length = tuple.Length(); cutStart = PR_MIN(cutStart, Length()); ReplacePrep(cutStart, cutLength, length); if (length > 0) tuple.WriteTo(mData + cutStart, length); Exploiting more bugs
  • 44. Large memory management vulnerabilities Exploiting other bugs Exploiting other bugs with mapping overflows with mapping overflows ▐ No gap / guard page enforced between mappings ► Not enough protections on most systems ► Contiguous mappings happen if large memory usage ► Allows us to turn an overflow or underflow... ► ... into corruption of another memory area ▐ May help to solve some exploitation problems  Difficult heap buffer overflows (end of heap, new GLibc and XP SP2 protections)  Large size memcpy heap overflows (-1 == 4G) that would trigger a crash  If a mapping is allowed on top of stack (threads, grsec random stack...): • Stack buffer overflow in argv() or env, in main() when main() never returns, big overflows with propolice-like protection...  Buffer underflows (also on stack on Linux 2.6) Exploiting more bugs
  • 45. Large memory management vulnerabilities Example: exploiting Windows XP Example: exploiting Windows XP heap overflows with a mapping overflow heap overflows with a mapping overflow ► By filling the memory we may allocate heap next to Thread Environment Blocks (TEB)... Exploiting more bugs Heap TEB 1 TEB 2 TEB N-1 TEB N TEB 3 Overflow of controlled size Large size overflow (memcpy with size -1...) Top of VM
  • 46. Large memory management vulnerabilities Example: exploiting Windows XP Example: exploiting Windows XP heap overflows with a mapping overflow heap overflows with a mapping overflow ► By filling the memory we may allocate heap next to Thread Environment Blocks (TEB)... Exploiting more bugs Heap TEB 1 TEB 2 TEB N-1 TEB N TEB 3 Overflow of controlled size Large size overflow (memcpy with size -1...) ... other TEB data ... bottom of stack top of stack SEH chain ptr Heap Top of VM FFFFFFFF ptr to code “shellcode” ► ...and overwrite the SEH chain pointer of our thread's TEB to redirect execution flow Sanity checks in ntdll.dll can be bypassed by tuning the “top of stack” and “bottom of stack” pointers
  • 47. Large memory management vulnerabilities Application flaws Application flaws dealing with large data sizes dealing with large data sizes
  • 48. Large memory management vulnerabilities ►Analysis of integer overflows in 32 bits counters ►Handling of library calls in OOM situations ►The MMAP_FIXED aberration Application flaws Application flaws dealing with large data sizes dealing with large data sizes
  • 49. Large memory management vulnerabilities Integer overflows and sign Integer overflows and sign problems in 32 bits counters problems in 32 bits counters ▐ 32 bits integer overflows when handling large data ► Impossible to allocate 4G on 32 bits CPU ► Nevertheless math calculations can make integer wrap  Multiply by 3 (base64 decoding...): if we can allocate 1.3G contiguous int len = strlen(data); bufLen = (len * 3)/4;  Multiply by 2 (string escape, buffer growth...): 2.7 G cont. max on Lin. 2.6 ▐ 32 bits integer sign problems ► Appear when len > 2G int len = strlen(ptr); if (len > buf_length) buf = realloc(buf, len+1); strcpy(buf, ptr); // overflow if reallocation was not done Application flaws
  • 50. Large memory management vulnerabilities Handling of library calls Handling of library calls in OOM situations in OOM situations ▐ Idea: applications do not check return values for library calls that are trivial or that “always” success, but some of them will not do their job in OOM situations ► Applications make wrong assumptions about the actions they took ► It may create application malfunction and errors, some potentially exploitable ▐ Needs more research! Application flaws
  • 51. Large memory management vulnerabilities Usage of MMAP_FIXED Usage of MMAP_FIXED ▐ Calling mmap() with the MMAP_FIXED parameter destroys any previous mapping at the address ▐ It is impossible to safely predict an address where a hole will exist in memory ► Example for Linux programs:  Differences in allocation behavior between Linux 2.4 and 2.6  Linux emulation layers on other OSes has a different behavior  User interaction (big memory allocations) ▐ Thus, its use is unsafe Application flaws
  • 52. Large memory management vulnerabilities Easy to exploit? Easy to exploit? Easy to protect from? Easy to protect from?
  • 53. Large memory management vulnerabilities ►System limitations ►Network limitations ►Protecting ourselves Easy to exploit? Easy to exploit? Easy to protect from? Easy to protect from?
  • 54. Large memory management vulnerabilities System limitations System limitations ▐ Memory size ► But VM can be overcommited if it is not accessed ▐ Resource limitations ► Stack size, data size, total VM size... ▐ Allocation speed ► RAM is quick even for GBs ► Becomes quite slow (minutes) when switching to disk swap
  • 55. Large memory management vulnerabilities Network limitations Network limitations ▐ Upload speed ► GBs can be sent in minutes or hours on modern LAN and Internet ► Services timeouts are a problem ► Data zip-bombs, memory leaks, multithreading... can help ▐ Lack of information about the target ► Brute force is likely to be needed for these attacks
  • 56. Large memory management vulnerabilities Protection Protection ▐ GCC flag -fstack-check ▐ On Linux 2.4: increase heap_stack_gap in /proc ▐ Application code security audit ▐ Memory limits handled by the application (PHP...) ► Memory leaks are to be treated as security bugs ;-) ▐ Resource limitation enforced by the OS ► Two edges sword ▐ Vendor patches?
  • 57. Large memory management vulnerabilities “ “Don't panic” Don't panic” ▐ Very specific conditions may be necessary to succeed in real life with these techniques ▐ Vulnerabilities introduced by OSes will be patched ▐ Switching to 64 bits will solve some of these issues ... and introduce new ones (ex: the long type is 64 bits on Unixes, but is 32 bits on Windows!) ▐ Yet some applications, on some systems, meet the conditions and critical exploitable vulnerabilities exist. (but don't panic and go audit your code)
  • 58. Large memory management vulnerabilities Conclusion Conclusion ▐ Usage of large quantities of memory in modern computing introduces unexpected vulnerabilities ▐ Security holes may exist in applications even if their code is valid: the OS and the compiler break usual safe assumptions ▐ Some of these flaws will be patched... ▐ ... others are classes of vulnerabilities that will last ► More research needs to be done:  The area is broad, and my time is limited  What about other OSes, threading libraries, compilers, embedded systems, emulation layers (Linux...), virtual machines...  How many applications are exploitable in real life situations?
  • 59. Large memory management vulnerabilities Thank you! Thank you! Feedback, questions, comments... are welcome gael.delalleau@beijaflore.com gael.delalleau+csw@m4x.org These updated slides will be available on http://www.cppsecurity.com Any questions? Thanks to Solar Designer and H. D. Moore. All errors are mine.