<s>
Defensive	B-Application
programming	I-Application
is	O
a	O
form	O
of	O
defensive	O
design	O
intended	O
to	O
develop	O
programs	O
that	O
are	O
capable	O
of	O
detecting	O
potential	O
security	O
abnormalities	O
and	O
make	O
predetermined	O
responses	O
.	O
</s>
<s>
Defensive	B-Application
programming	I-Application
practices	O
are	O
often	O
used	O
where	O
high	O
availability	O
,	O
safety	O
,	O
or	O
security	O
is	O
needed	O
.	O
</s>
<s>
Defensive	B-Application
programming	I-Application
is	O
an	O
approach	O
to	O
improve	O
software	O
and	O
source	O
code	O
,	O
in	O
terms	O
of	O
:	O
</s>
<s>
General	O
quality	O
–	O
reducing	O
the	O
number	O
of	O
software	B-Error_Name
bugs	I-Error_Name
and	O
problems	O
.	O
</s>
<s>
Overly	O
defensive	B-Application
programming	I-Application
,	O
however	O
,	O
may	O
safeguard	O
against	O
errors	O
that	O
will	O
never	O
be	O
encountered	O
,	O
thus	O
incurring	O
run-time	O
and	O
maintenance	O
costs	O
.	O
</s>
<s>
There	O
is	O
also	O
a	O
risk	O
that	O
code	O
traps	O
prevent	O
too	O
many	O
exceptions	B-General_Concept
,	O
potentially	O
resulting	O
in	O
unnoticed	O
,	O
incorrect	O
results	O
.	O
</s>
<s>
Secure	O
programming	O
is	O
the	O
subset	O
of	O
defensive	B-Application
programming	I-Application
concerned	O
with	O
computer	O
security	O
.	O
</s>
<s>
As	O
with	O
all	O
kinds	O
of	O
defensive	B-Application
programming	I-Application
,	O
avoiding	O
bugs	B-Error_Name
is	O
a	O
primary	O
objective	O
;	O
however	O
,	O
the	O
motivation	O
is	O
not	O
as	O
much	O
to	O
reduce	O
the	O
likelihood	O
of	O
failure	O
in	O
normal	O
operation	O
(	O
as	O
if	O
safety	O
were	O
the	O
concern	O
)	O
,	O
but	O
to	O
reduce	O
the	O
attack	O
surface	O
–	O
the	O
programmer	O
must	O
assume	O
that	O
the	O
software	O
might	O
be	O
misused	O
actively	O
to	O
reveal	O
bugs	B-Error_Name
,	O
and	O
that	O
bugs	B-Error_Name
could	O
be	O
exploited	O
maliciously	O
.	O
</s>
<s>
The	O
function	O
will	O
result	O
in	O
undefined	B-Language
behavior	I-Language
when	O
the	O
input	O
is	O
over	O
1000	O
characters	O
.	O
</s>
<s>
This	O
particular	O
bug	B-Error_Name
demonstrates	O
a	O
vulnerability	O
which	O
enables	O
buffer	B-General_Concept
overflow	I-General_Concept
exploits	O
.	O
</s>
<s>
Offensive	O
programming	O
is	O
a	O
category	O
of	O
defensive	B-Application
programming	I-Application
,	O
with	O
the	O
added	O
emphasis	O
that	O
certain	O
errors	O
should	O
not	O
be	O
handled	B-General_Concept
defensively	I-General_Concept
.	O
</s>
<s>
Here	O
are	O
some	O
defensive	B-Application
programming	I-Application
techniques	O
:	O
</s>
<s>
If	O
existing	O
code	O
is	O
tested	O
and	O
known	O
to	O
work	O
,	O
reusing	O
it	O
may	O
reduce	O
the	O
chance	O
of	O
bugs	B-Error_Name
being	O
introduced	O
.	O
</s>
<s>
Before	O
reusing	O
old	O
source	O
code	O
,	O
libraries	O
,	O
APIs	B-Application
,	O
configurations	O
and	O
so	O
forth	O
,	O
it	O
must	O
be	O
considered	O
if	O
the	O
old	O
work	O
is	O
valid	O
for	O
reuse	O
,	O
or	O
if	O
it	O
is	O
likely	O
to	O
be	O
prone	O
to	O
legacy	B-Device
problems	O
.	O
</s>
<s>
Legacy	B-Device
problems	O
are	O
problems	O
inherent	O
when	O
old	O
designs	O
are	O
expected	O
to	O
work	O
with	O
today	O
's	O
requirements	O
,	O
especially	O
when	O
the	O
old	O
designs	O
were	O
not	O
developed	O
or	O
tested	O
with	O
those	O
requirements	O
in	O
mind	O
.	O
</s>
<s>
Many	O
software	O
products	O
have	O
experienced	O
problems	O
with	O
old	O
legacy	B-Device
source	O
code	O
;	O
for	O
example	O
:	O
</s>
<s>
Legacy	B-Device
code	I-Device
may	O
not	O
have	O
been	O
designed	O
under	O
a	O
defensive	B-Application
programming	I-Application
initiative	O
,	O
and	O
might	O
therefore	O
be	O
of	O
much	O
lower	O
quality	O
than	O
newly	O
designed	O
source	O
code	O
.	O
</s>
<s>
Legacy	B-Device
code	I-Device
may	O
have	O
been	O
written	O
and	O
tested	O
under	O
conditions	O
which	O
no	O
longer	O
apply	O
.	O
</s>
<s>
Example	O
1	O
:	O
legacy	B-Device
code	I-Device
may	O
have	O
been	O
designed	O
for	O
ASCII	O
input	O
but	O
now	O
the	O
input	O
is	O
UTF-8	O
.	O
</s>
<s>
Example	O
2	O
:	O
legacy	B-Device
code	I-Device
may	O
have	O
been	O
compiled	O
and	O
tested	O
on	O
32-bit	O
architectures	O
,	O
but	O
when	O
compiled	O
on	O
64-bit	O
architectures	O
,	O
new	O
arithmetic	O
problems	O
may	O
occur	O
(	O
e.g.	O
,	O
invalid	O
signedness	O
tests	O
,	O
invalid	O
type	O
casts	O
,	O
etc	O
.	O
</s>
<s>
Example	O
3	O
:	O
legacy	B-Device
code	I-Device
may	O
have	O
been	O
targeted	O
for	O
offline	O
machines	O
,	O
but	O
becomes	O
vulnerable	O
once	O
network	O
connectivity	O
is	O
added	O
.	O
</s>
<s>
Legacy	B-Device
code	I-Device
is	O
not	O
written	O
with	O
new	O
problems	O
in	O
mind	O
.	O
</s>
<s>
Notable	O
examples	O
of	O
the	O
legacy	B-Device
problem	O
:	O
</s>
<s>
BIND	B-Protocol
9	I-Protocol
,	O
presented	O
by	O
Paul	O
Vixie	O
and	O
David	O
Conrad	O
as	O
"	O
BINDv9	O
is	O
a	O
complete	O
rewrite	O
"	O
,	O
"	O
Security	O
was	O
a	O
key	O
consideration	O
in	O
design	O
"	O
,	O
naming	O
security	O
,	O
robustness	O
,	O
scalability	O
and	O
new	O
protocols	O
as	O
key	O
concerns	O
for	O
rewriting	O
old	O
legacy	B-Device
code	I-Device
.	O
</s>
<s>
Microsoft	B-Application
Windows	I-Application
suffered	O
from	O
"	O
the	O
"	O
Windows	O
Metafile	O
vulnerability	O
and	O
other	O
exploits	O
related	O
to	O
the	O
WMF	O
format	O
.	O
</s>
<s>
Oracle	B-Application
is	O
combating	O
legacy	B-Device
problems	O
,	O
such	O
as	O
old	O
source	O
code	O
written	O
without	O
addressing	O
concerns	O
of	O
SQL	B-Language
injection	I-Language
and	O
privilege	O
escalation	O
,	O
resulting	O
in	O
many	O
security	O
vulnerabilities	O
which	O
have	O
taken	O
time	O
to	O
fix	O
and	O
also	O
generated	O
incomplete	O
fixes	O
.	O
</s>
<s>
An	O
additional	O
criticism	O
is	O
that	O
default	O
installations	O
(	O
largely	O
a	O
legacy	B-Device
from	O
old	O
versions	O
)	O
are	O
not	O
aligned	O
with	O
their	O
own	O
security	O
recommendations	O
,	O
such	O
as	O
,	O
which	O
is	O
hard	O
to	O
amend	O
as	O
many	O
applications	O
require	O
the	O
less	O
secure	O
legacy	B-Device
settings	O
to	O
function	O
correctly	O
.	O
</s>
<s>
Canonicalization	B-General_Concept
libraries	O
can	O
be	O
employed	O
to	O
avoid	O
bugs	B-Error_Name
due	O
to	O
non-canonical	O
input	O
.	O
</s>
<s>
are	O
bugs	B-Error_Name
and	O
potential	O
security	O
flaws	O
.	O
</s>
<s>
One	O
of	O
the	O
most	O
common	O
problems	O
is	O
unchecked	O
use	O
of	O
constant-size	O
or	O
pre-allocated	O
structures	O
for	O
dynamic-size	O
data	O
such	O
as	O
inputs	O
to	O
the	O
program	O
(	O
the	O
buffer	B-General_Concept
overflow	I-General_Concept
problem	O
)	O
.	O
</s>
<s>
This	O
is	O
especially	O
common	O
for	O
string	O
data	O
in	O
C	B-Language
.	O
C	B-Language
library	O
functions	O
like	O
gets	O
should	O
never	O
be	O
used	O
since	O
the	O
maximum	O
size	O
of	O
the	O
input	O
buffer	O
is	O
not	O
passed	O
as	O
an	O
argument	O
.	O
</s>
<s>
C	B-Language
library	O
functions	O
like	O
scanf	O
can	O
be	O
used	O
safely	O
,	O
but	O
require	O
the	O
programmer	O
to	O
take	O
care	O
with	O
the	O
selection	O
of	O
safe	O
format	O
strings	O
,	O
by	O
sanitizing	O
it	O
before	O
using	O
it	O
.	O
</s>
<s>
All	O
code	O
is	O
insecure	O
until	O
proven	O
otherwise	O
-	O
while	O
a	O
slight	O
misnomer	O
,	O
does	O
a	O
good	O
job	O
reminding	O
us	O
to	O
never	O
assume	O
our	O
code	O
is	O
secure	O
as	O
bugs	B-Error_Name
or	O
undefined	B-Language
behavior	I-Language
may	O
expose	O
the	O
project	O
or	O
system	O
to	O
attacks	O
such	O
as	O
common	O
SQL	B-Language
injection	I-Language
attacks	I-Language
.	O
</s>
<s>
Generally	O
speaking	O
,	O
it	O
is	O
preferable	O
to	O
throw	O
exception	O
messages	O
that	O
enforce	O
part	O
of	O
your	O
API	B-Application
contract	B-Application
and	O
guide	O
the	O
developer	O
instead	O
of	O
returning	O
error	O
code	O
values	O
that	O
do	O
not	O
point	O
to	O
where	O
the	O
exception	O
occurred	O
or	O
what	O
the	O
program	O
stack	O
looked	O
liked	O
,	O
Better	O
logging	O
and	O
exception	B-General_Concept
handling	I-General_Concept
will	O
increase	O
robustness	O
and	O
security	O
of	O
your	O
software	O
,	O
while	O
minimizing	O
developer	O
stress	O
.	O
</s>
