<s>
In	O
software	O
,	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
or	O
stack	B-General_Concept
buffer	I-General_Concept
overrun	I-General_Concept
occurs	O
when	O
a	O
program	O
writes	O
to	O
a	O
memory	B-General_Concept
address	O
on	O
the	O
program	O
's	O
call	B-General_Concept
stack	I-General_Concept
outside	O
of	O
the	O
intended	O
data	O
structure	O
,	O
which	O
is	O
usually	O
a	O
fixed-length	O
buffer	B-General_Concept
.	O
</s>
<s>
Stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
bugs	O
are	O
caused	O
when	O
a	O
program	O
writes	O
more	O
data	O
to	O
a	O
buffer	B-General_Concept
located	O
on	O
the	O
stack	O
than	O
what	O
is	O
actually	O
allocated	O
for	O
that	O
buffer	B-General_Concept
.	O
</s>
<s>
Stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
is	O
a	O
type	O
of	O
the	O
more	O
general	O
programming	O
malfunction	O
known	O
as	O
buffer	B-General_Concept
overflow	I-General_Concept
(	O
or	O
buffer	B-General_Concept
overrun	I-General_Concept
)	O
.	O
</s>
<s>
Overfilling	O
a	O
buffer	B-General_Concept
on	O
the	O
stack	O
is	O
more	O
likely	O
to	O
derail	O
program	O
execution	O
than	O
overfilling	O
a	O
buffer	B-General_Concept
on	O
the	O
heap	O
because	O
the	O
stack	O
contains	O
the	O
return	O
addresses	O
for	O
all	O
active	O
function	O
calls	O
.	O
</s>
<s>
A	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
can	O
be	O
caused	O
deliberately	O
as	O
part	O
of	O
an	O
attack	O
known	O
as	O
stack	B-General_Concept
smashing	I-General_Concept
.	O
</s>
<s>
a	O
webserver	B-Application
)	O
then	O
the	O
bug	O
is	O
a	O
potential	O
security	O
vulnerability	O
.	O
</s>
<s>
If	O
the	O
stack	O
buffer	B-General_Concept
is	O
filled	O
with	O
data	O
supplied	O
from	O
an	O
untrusted	O
user	O
then	O
that	O
user	O
can	O
corrupt	O
the	O
stack	O
in	O
such	O
a	O
way	O
as	O
to	O
inject	O
executable	O
code	O
into	O
the	O
running	O
program	O
and	O
take	O
control	O
of	O
the	O
process	O
.	O
</s>
<s>
The	O
canonical	O
method	O
for	O
exploiting	O
a	O
stack-based	O
buffer	B-General_Concept
overflow	I-General_Concept
is	O
to	O
overwrite	O
the	O
function	O
return	O
address	O
with	O
a	O
pointer	O
to	O
attacker-controlled	O
data	O
(	O
usually	O
on	O
the	O
stack	O
itself	O
)	O
.	O
</s>
<s>
(	O
The	O
maximum	O
number	O
of	O
characters	O
that	O
is	O
safe	O
is	O
one	O
less	O
than	O
the	O
size	O
of	O
the	O
buffer	B-General_Concept
here	O
because	O
in	O
the	O
C	O
programming	O
language	O
,	O
strings	O
are	O
terminated	O
by	O
a	O
null	O
byte	O
character	O
.	O
</s>
<s>
The	O
zero	O
byte	O
then	O
ends	O
up	O
overwriting	O
a	O
memory	B-General_Concept
location	O
that	O
's	O
one	O
byte	O
beyond	O
the	O
end	O
of	O
the	O
buffer	B-General_Concept
.	O
)	O
</s>
<s>
Thus	O
,	O
the	O
attacker	O
has	O
overwritten	O
the	O
return	O
address	O
with	O
a	O
pointer	O
to	O
the	O
stack	O
buffer	B-General_Concept
char	O
c[12],	O
which	O
now	O
contains	O
attacker-supplied	O
data	O
.	O
</s>
<s>
In	O
an	O
actual	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
exploit	O
the	O
string	O
of	O
"	O
A	O
"	O
 '	O
s	O
would	O
instead	O
be	O
shellcode	O
suitable	O
to	O
the	O
platform	O
and	O
desired	O
function	O
.	O
</s>
<s>
the	O
SUID	O
bit	O
set	O
to	O
run	O
as	O
the	O
superuser	B-Application
)	O
,	O
then	O
the	O
attacker	O
could	O
use	O
this	O
vulnerability	O
to	O
gain	O
superuser	B-Application
privileges	O
on	O
the	O
affected	O
machine	O
.	O
</s>
<s>
A	O
number	O
of	O
platforms	O
have	O
subtle	O
differences	O
in	O
their	O
implementation	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
that	O
can	O
affect	O
the	O
way	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
exploit	O
will	O
work	O
.	O
</s>
<s>
Some	O
machine	O
architectures	O
store	O
the	O
top-level	O
return	O
address	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
in	O
a	O
register	O
.	O
</s>
<s>
This	O
means	O
that	O
any	O
overwritten	O
return	O
address	O
will	O
not	O
be	O
used	O
until	O
a	O
later	O
unwinding	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Another	O
example	O
of	O
a	O
machine-specific	O
detail	O
that	O
can	O
affect	O
the	O
choice	O
of	O
exploitation	O
techniques	O
is	O
the	O
fact	O
that	O
most	O
RISC-style	O
machine	O
architectures	O
will	O
not	O
allow	O
unaligned	O
access	O
to	O
memory	B-General_Concept
.	O
</s>
<s>
Within	O
the	O
topic	O
of	O
stack	B-General_Concept
buffer	I-General_Concept
overflows	I-General_Concept
,	O
an	O
often-discussed-but-rarely-seen	O
architecture	O
is	O
one	O
in	O
which	O
the	O
stack	O
grows	O
in	O
the	O
opposite	O
direction	O
.	O
</s>
<s>
This	O
change	O
in	O
architecture	O
is	O
frequently	O
suggested	O
as	O
a	O
solution	O
to	O
the	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
problem	O
because	O
any	O
overflow	O
of	O
a	O
stack	O
buffer	B-General_Concept
that	O
occurs	O
within	O
the	O
same	O
stack	O
frame	O
cannot	O
overwrite	O
the	O
return	B-General_Concept
pointer	I-General_Concept
.	O
</s>
<s>
However	O
,	O
any	O
overflow	O
that	O
occurs	O
in	O
a	O
buffer	B-General_Concept
from	O
a	O
previous	O
stack	O
frame	O
will	O
still	O
overwrite	O
a	O
return	B-General_Concept
pointer	I-General_Concept
and	O
allow	O
for	O
malicious	O
exploitation	O
of	O
the	O
bug	O
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
the	O
example	O
above	O
,	O
the	O
return	B-General_Concept
pointer	I-General_Concept
for	O
foo	O
will	O
not	O
be	O
overwritten	O
because	O
the	O
overflow	O
actually	O
occurs	O
within	O
the	O
stack	O
frame	O
for	O
memcpy	O
.	O
</s>
<s>
However	O
,	O
because	O
the	O
buffer	B-General_Concept
that	O
overflows	O
during	O
the	O
call	O
to	O
memcpy	O
resides	O
in	O
a	O
previous	O
stack	O
frame	O
,	O
the	O
return	B-General_Concept
pointer	I-General_Concept
for	O
memcpy	O
will	O
have	O
a	O
numerically	O
higher	O
memory	B-General_Concept
address	O
than	O
the	O
buffer	B-General_Concept
.	O
</s>
<s>
This	O
means	O
that	O
instead	O
of	O
the	O
return	B-General_Concept
pointer	I-General_Concept
for	O
foo	O
being	O
overwritten	O
,	O
the	O
return	B-General_Concept
pointer	I-General_Concept
for	O
memcpy	O
will	O
be	O
overwritten	O
.	O
</s>
<s>
At	O
most	O
,	O
this	O
means	O
that	O
growing	O
the	O
stack	O
in	O
the	O
opposite	O
direction	O
will	O
change	O
some	O
details	O
of	O
how	O
stack	B-General_Concept
buffer	I-General_Concept
overflows	I-General_Concept
are	O
exploitable	O
,	O
but	O
it	O
will	O
not	O
reduce	O
significantly	O
the	O
number	O
of	O
exploitable	O
bugs	O
.	O
</s>
<s>
Over	O
the	O
years	O
,	O
a	O
number	O
of	O
control-flow	O
integrity	O
schemes	O
have	O
been	O
developed	O
to	O
inhibit	O
malicious	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
exploitation	O
.	O
</s>
<s>
Detect	O
that	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
has	O
occurred	O
and	O
thus	O
prevent	O
redirection	O
of	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
to	O
malicious	O
code	O
.	O
</s>
<s>
Prevent	O
the	O
execution	O
of	O
malicious	O
code	O
from	O
the	O
stack	O
without	O
directly	O
detecting	O
the	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
.	O
</s>
<s>
Randomize	O
the	O
memory	B-General_Concept
space	O
such	O
that	O
finding	O
executable	O
code	O
becomes	O
unreliable	O
.	O
</s>
<s>
Stack	O
canaries	O
,	O
named	O
for	O
their	O
analogy	O
to	O
a	O
canary	O
in	O
a	O
coal	O
mine	O
,	O
are	O
used	O
to	O
detect	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
before	O
execution	O
of	O
malicious	O
code	O
can	O
occur	O
.	O
</s>
<s>
This	O
method	O
works	O
by	O
placing	O
a	O
small	O
integer	O
,	O
the	O
value	O
of	O
which	O
is	O
randomly	O
chosen	O
at	O
program	O
start	O
,	O
in	O
memory	B-General_Concept
just	O
before	O
the	O
stack	O
return	B-General_Concept
pointer	I-General_Concept
.	O
</s>
<s>
Most	O
buffer	B-General_Concept
overflows	I-General_Concept
overwrite	O
memory	B-General_Concept
from	O
lower	O
to	O
higher	O
memory	B-General_Concept
addresses	O
,	O
so	O
in	O
order	O
to	O
overwrite	O
the	O
return	B-General_Concept
pointer	I-General_Concept
(	O
and	O
thus	O
take	O
control	O
of	O
the	O
process	O
)	O
the	O
canary	O
value	O
must	O
also	O
be	O
overwritten	O
.	O
</s>
<s>
This	O
value	O
is	O
checked	O
to	O
make	O
sure	O
it	O
has	O
not	O
changed	O
before	O
a	O
routine	O
uses	O
the	O
return	B-General_Concept
pointer	I-General_Concept
on	O
the	O
stack	O
.	O
</s>
<s>
This	O
technique	O
can	O
greatly	O
increase	O
the	O
difficulty	O
of	O
exploiting	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
because	O
it	O
forces	O
the	O
attacker	O
to	O
gain	O
control	O
of	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
by	O
some	O
non-traditional	O
means	O
such	O
as	O
corrupting	O
other	O
important	O
variables	O
on	O
the	O
stack	O
.	O
</s>
<s>
Another	O
approach	O
to	O
preventing	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
exploitation	O
is	O
to	O
enforce	O
a	O
memory	B-General_Concept
policy	O
on	O
the	O
stack	O
memory	B-General_Concept
region	O
that	O
disallows	O
execution	O
from	O
the	O
stack	O
(	O
W^X	O
,	O
"	O
Write	O
XOR	O
Execute	O
"	O
)	O
.	O
</s>
<s>
This	O
means	O
that	O
in	O
order	O
to	O
execute	O
shellcode	O
from	O
the	O
stack	O
an	O
attacker	O
must	O
either	O
find	O
a	O
way	O
to	O
disable	O
the	O
execution	O
protection	O
from	O
memory	B-General_Concept
,	O
or	O
find	O
a	O
way	O
to	O
put	O
their	O
shellcode	O
payload	O
in	O
a	O
non-protected	O
region	O
of	O
memory	B-General_Concept
.	O
</s>
<s>
This	O
method	O
is	O
becoming	O
more	O
popular	O
now	O
that	O
hardware	O
support	O
for	O
the	O
no-execute	B-General_Concept
flag	I-General_Concept
is	O
available	O
in	O
most	O
desktop	O
processors	O
.	O
</s>
<s>
While	O
this	O
method	O
prevents	O
the	O
canonical	O
stack	B-General_Concept
smashing	I-General_Concept
exploit	O
,	O
stack	B-Error_Name
overflows	I-Error_Name
can	O
be	O
exploited	O
in	O
other	O
ways	O
.	O
</s>
<s>
First	O
,	O
it	O
is	O
common	O
to	O
find	O
ways	O
to	O
store	O
shellcode	O
in	O
unprotected	O
memory	B-General_Concept
regions	O
like	O
the	O
heap	O
,	O
and	O
so	O
very	O
little	O
need	O
change	O
in	O
the	O
way	O
of	O
exploitation	O
.	O
</s>
<s>
Another	O
attack	O
is	O
the	O
so-called	O
return	B-Language
to	I-Language
libc	I-Language
method	O
for	O
shellcode	O
creation	O
.	O
</s>
<s>
In	O
this	O
attack	O
the	O
malicious	O
payload	O
will	O
load	O
the	O
stack	O
not	O
with	O
shellcode	O
,	O
but	O
with	O
a	O
proper	O
call	B-General_Concept
stack	I-General_Concept
so	O
that	O
execution	O
is	O
vectored	O
to	O
a	O
chain	O
of	O
standard	O
library	O
calls	O
,	O
usually	O
with	O
the	O
effect	O
of	O
disabling	O
memory	B-General_Concept
execute	O
protections	O
and	O
allowing	O
shellcode	O
to	O
run	O
as	O
normal	O
.	O
</s>
<s>
A	O
variant	O
of	O
return-to-libc	B-Language
is	O
return-oriented	O
programming	O
(	O
ROP	O
)	O
,	O
which	O
sets	O
up	O
a	O
series	O
of	O
return	O
addresses	O
,	O
each	O
of	O
which	O
executes	O
a	O
small	O
sequence	O
of	O
cherry-picked	O
machine	O
instructions	O
within	O
the	O
existing	O
program	O
code	O
or	O
system	O
libraries	O
,	O
sequence	O
which	O
ends	O
with	O
a	O
return	O
.	O
</s>
<s>
Instead	O
of	O
separating	O
the	O
code	O
from	O
the	O
data	O
,	O
another	O
mitigation	O
technique	O
is	O
to	O
introduce	O
randomization	O
to	O
the	O
memory	B-General_Concept
space	O
of	O
the	O
executing	O
program	O
.	O
</s>
<s>
Since	O
the	O
attacker	O
needs	O
to	O
determine	O
where	O
executable	O
code	O
that	O
can	O
be	O
used	O
resides	O
,	O
either	O
an	O
executable	O
payload	O
is	O
provided	O
(	O
with	O
an	O
executable	O
stack	O
)	O
or	O
one	O
is	O
constructed	O
using	O
code	O
reuse	O
such	O
as	O
in	O
ret2libc	B-Language
or	O
return-oriented	O
programming	O
(	O
ROP	O
)	O
.	O
</s>
<s>
Randomizing	O
the	O
memory	B-General_Concept
layout	O
will	O
,	O
as	O
a	O
concept	O
,	O
prevent	O
the	O
attacker	O
from	O
knowing	O
where	O
any	O
code	O
is	O
.	O
</s>
<s>
However	O
,	O
implementations	O
typically	O
will	O
not	O
randomize	O
everything	O
;	O
usually	O
the	O
executable	O
itself	O
is	O
loaded	O
at	O
a	O
fixed	O
address	O
and	O
hence	O
even	O
when	O
ASLR	O
(	O
address	O
space	O
layout	O
randomization	O
)	O
is	O
combined	O
with	O
a	O
nonexecutable	O
stack	O
the	O
attacker	O
can	O
use	O
this	O
fixed	O
region	O
of	O
memory	B-General_Concept
.	O
</s>
<s>
Therefore	O
,	O
all	O
programs	O
should	O
be	O
compiled	O
with	O
PIE	O
(	O
position-independent	O
executables	O
)	O
such	O
that	O
even	O
this	O
region	O
of	O
memory	B-General_Concept
is	O
randomized	O
.	O
</s>
<s>
The	O
entropy	O
of	O
the	O
randomization	O
is	O
different	O
from	O
implementation	O
to	O
implementation	O
and	O
a	O
low	O
enough	O
entropy	O
can	O
in	O
itself	O
be	O
a	O
problem	O
in	O
terms	O
of	O
brute	O
forcing	O
the	O
memory	B-General_Concept
space	O
that	O
is	O
randomized	O
.	O
</s>
<s>
But	O
it	O
is	O
still	O
possible	O
to	O
exploit	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
if	O
some	O
vulnerabilities	O
are	O
presents	O
or	O
if	O
some	O
conditions	O
are	O
met	O
.	O
</s>
<s>
An	O
attacker	O
is	O
able	O
to	O
exploit	O
the	O
format	O
string	O
vulnerability	O
for	O
revealing	O
the	O
memory	B-General_Concept
locations	O
in	O
the	O
vulnerable	O
program	O
.	O
</s>
<s>
When	O
Data	O
Execution	O
Prevention	O
is	O
enabled	O
to	O
forbid	O
any	O
execute	O
access	O
to	O
the	O
stack	O
,	O
the	O
attacker	O
can	O
still	O
use	O
the	O
overwritten	O
return	O
address	O
(	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
)	O
to	O
point	O
to	O
data	O
in	O
a	O
code	B-General_Concept
segment	I-General_Concept
(	O
on	O
Linux	O
)	O
or	O
every	O
other	O
executable	O
section	O
of	O
the	O
program	O
.	O
</s>
<s>
Consists	O
to	O
overwrite	O
the	O
return	B-General_Concept
pointer	I-General_Concept
a	O
bit	O
before	O
a	O
return	O
instruction	O
(	O
ret	O
in	O
x86	O
)	O
of	O
the	O
program	O
.	O
</s>
<s>
The	O
instructions	O
between	O
the	O
new	O
return	B-General_Concept
pointer	I-General_Concept
and	O
the	O
return	O
instruction	O
will	O
be	O
executed	O
and	O
the	O
return	O
instruction	O
will	O
return	O
to	O
the	O
payload	O
controlled	O
by	O
the	O
exploiter	O
.	O
</s>
<s>
A	O
limitation	O
of	O
ASLR	O
realization	O
on	O
64-bit	O
systems	O
is	O
that	O
it	O
is	O
vulnerable	O
to	O
memory	B-General_Concept
disclosure	O
and	O
information	O
leakage	O
attacks	O
.	O
</s>
<s>
The	O
Morris	O
worm	O
in	O
1988	O
spread	O
in	O
part	O
by	O
exploiting	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
in	O
the	O
Unix	B-Application
finger	B-Application
server	O
.	O
</s>
<s>
The	O
Slammer	O
worm	O
in	O
2003	O
spread	O
by	O
exploiting	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
in	O
Microsoft	O
's	O
SQL	O
server	O
.	O
</s>
<s>
The	O
Blaster	O
worm	O
in	O
2003	O
spread	O
by	O
exploiting	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
in	O
Microsoft	O
DCOM	B-Operating_System
service	O
.	O
</s>
<s>
The	O
Witty	O
worm	O
in	O
2004	O
spread	O
by	O
exploiting	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
in	O
the	O
Internet	O
Security	O
Systems	O
BlackICE	O
Desktop	O
Agent	O
.	O
</s>
<s>
There	O
are	O
a	O
couple	O
of	O
examples	O
of	O
the	B-Operating_System
Wii	I-Operating_System
allowing	O
arbitrary	O
code	O
to	O
be	O
run	O
on	O
an	O
unmodified	O
system	O
.	O
</s>
<s>
The	O
"	O
Twilight	O
hack	O
"	O
which	O
involves	O
giving	O
a	O
lengthy	O
name	O
to	O
the	O
main	O
character	O
's	O
horse	O
in	O
The	B-Application
Legend	I-Application
of	I-Application
Zelda	I-Application
:	I-Application
Twilight	I-Application
Princess	I-Application
,	O
and	O
"	O
Smash	O
Stack	O
"	O
for	O
Super	B-Application
Smash	I-Application
Bros	I-Application
.	I-Application
Brawl	I-Application
which	O
involves	O
using	O
an	O
SD	O
card	O
to	O
load	O
a	O
specially	O
prepared	O
file	O
into	O
the	O
in-game	O
level	O
editor	O
.	O
</s>
