<s>
In	O
software	O
development	O
,	O
time-of-check	B-Error_Name
to	I-Error_Name
time-of-use	I-Error_Name
(	O
TOCTOU	B-Error_Name
,	O
TOCTTOU	B-Error_Name
or	O
TOC/TOU	O
)	O
is	O
a	O
class	O
of	O
software	B-Error_Name
bugs	I-Error_Name
caused	O
by	O
a	O
race	B-Operating_System
condition	I-Operating_System
involving	O
the	O
checking	O
of	O
the	O
state	O
of	O
a	O
part	O
of	O
a	O
system	O
(	O
such	O
as	O
a	O
security	O
credential	O
)	O
and	O
the	O
use	O
of	O
the	O
results	O
of	O
that	O
check	O
.	O
</s>
<s>
TOCTOU	B-Error_Name
race	B-Operating_System
conditions	I-Operating_System
are	O
common	O
in	O
Unix	B-Application
between	O
operations	O
on	O
the	O
file	O
system	O
,	O
but	O
can	O
occur	O
in	O
other	O
contexts	O
,	O
including	O
local	O
sockets	B-Protocol
and	O
improper	O
use	O
of	O
database	B-General_Concept
transactions	I-General_Concept
.	O
</s>
<s>
In	O
the	O
early	O
1990s	O
,	O
the	O
mail	O
utility	O
of	O
BSD	O
4.3	O
UNIX	B-Application
had	O
an	O
exploitable	O
race	B-Operating_System
condition	I-Operating_System
for	O
temporary	O
files	O
because	O
it	O
used	O
the	O
mktemp( )	O
function	O
.	O
</s>
<s>
Early	O
versions	O
of	O
OpenSSH	B-Language
had	O
an	O
exploitable	O
race	B-Operating_System
condition	I-Operating_System
for	O
Unix	B-Protocol
domain	I-Protocol
sockets	I-Protocol
.	O
</s>
<s>
They	O
remain	O
a	O
problem	O
in	O
modern	O
systems	O
;	O
as	O
of	O
2019	O
,	O
a	O
TOCTOU	B-Error_Name
race	B-Operating_System
condition	I-Operating_System
in	O
Docker	B-Language
allows	O
root	O
access	O
to	O
the	O
filesystem	O
of	O
the	O
host	O
platform	O
.	O
</s>
<s>
In	O
the	O
2023	O
Pwn²Own	O
competition	O
in	O
Vancouver	O
,	O
a	O
team	O
of	O
hackers	O
was	O
able	O
to	O
compromise	O
the	O
gateway	O
in	O
updated	O
Tesla	O
model	O
3	O
using	O
this	O
bug	B-Error_Name
.	O
</s>
<s>
In	O
Unix	B-Application
,	O
the	O
following	O
C	B-Language
code	O
,	O
when	O
used	O
in	O
a	O
setuid	O
program	O
,	O
has	O
a	O
TOCTOU	B-Error_Name
bug	B-Error_Name
:	O
</s>
<s>
This	O
race	B-Operating_System
condition	I-Operating_System
is	O
vulnerable	O
to	O
an	O
attack	O
:	O
</s>
<s>
In	O
this	O
example	O
,	O
an	O
attacker	O
can	O
exploit	O
the	O
race	B-Operating_System
condition	I-Operating_System
between	O
the	O
access	O
and	O
open	O
to	O
trick	O
the	O
setuid	O
victim	O
into	O
overwriting	O
an	O
entry	O
in	O
the	O
system	O
password	O
database	O
.	O
</s>
<s>
TOCTOU	B-Error_Name
races	O
can	O
be	O
used	O
for	O
privilege	O
escalation	O
to	O
get	O
administrative	O
access	O
to	O
a	O
machine	O
.	O
</s>
<s>
Exploiting	O
a	O
TOCTOU	B-Error_Name
race	B-Operating_System
condition	I-Operating_System
requires	O
precise	O
timing	O
to	O
ensure	O
that	O
the	O
attacker	O
's	O
operations	O
interleave	O
properly	O
with	O
the	O
victim	O
's	O
.	O
</s>
<s>
Despite	O
conceptual	O
simplicity	O
,	O
TOCTOU	B-Error_Name
race	B-Operating_System
conditions	I-Operating_System
are	O
difficult	O
to	O
avoid	O
and	O
eliminate	O
.	O
</s>
<s>
In	O
the	O
context	O
of	O
file	O
system	O
TOCTOU	B-Error_Name
race	B-Operating_System
conditions	I-Operating_System
,	O
the	O
fundamental	O
challenge	O
is	O
ensuring	O
that	O
the	O
file	O
system	O
cannot	O
be	O
changed	O
between	O
two	O
system	O
calls	O
.	O
</s>
<s>
In	O
2004	O
,	O
an	O
impossibility	O
result	O
was	O
published	O
,	O
showing	O
that	O
there	O
was	O
no	O
portable	O
,	O
deterministic	O
technique	O
for	O
avoiding	O
TOCTOU	B-Error_Name
race	B-Operating_System
conditions	I-Operating_System
when	O
using	O
the	O
UNIX	B-Application
access	O
and	O
open	O
filesystem	O
calls	O
.	O
</s>
<s>
Since	O
this	O
impossibility	O
result	O
,	O
libraries	O
for	O
tracking	O
file	B-Application
descriptors	I-Application
and	O
ensuring	O
correctness	O
have	O
been	O
proposed	O
by	O
researchers	O
.	O
</s>
<s>
An	O
alternative	O
solution	O
proposed	O
in	O
the	O
research	O
community	O
is	O
for	O
UNIX	B-Application
systems	I-Application
to	O
adopt	O
transactions	O
in	O
the	O
file	O
system	O
or	O
the	O
OS	O
kernel	O
.	O
</s>
<s>
Transactions	O
provide	O
a	O
concurrency	B-Operating_System
control	I-Operating_System
abstraction	O
for	O
the	O
OS	O
,	O
and	O
can	O
be	O
used	O
to	O
prevent	O
TOCTOU	B-Error_Name
races	O
.	O
</s>
<s>
While	O
no	O
production	O
UNIX	B-Application
kernel	O
has	O
yet	O
adopted	O
transactions	O
,	O
proof-of-concept	O
research	O
prototypes	O
have	O
been	O
developed	O
for	O
Linux	O
,	O
including	O
the	O
Valor	O
file	O
system	O
and	O
the	O
TxOS	O
kernel	O
.	O
</s>
<s>
Microsoft	B-Application
Windows	I-Application
has	O
added	O
transactions	O
to	O
its	O
NTFS	B-Application
file	I-Application
system	I-Application
,	O
but	O
Microsoft	O
discourages	O
their	O
use	O
,	O
and	O
has	O
indicated	O
that	O
they	O
may	O
be	O
removed	O
in	O
a	O
future	O
version	O
of	O
Windows	O
.	O
</s>
<s>
File	B-Application
locking	I-Application
is	O
a	O
common	O
technique	O
for	O
preventing	O
race	B-Operating_System
conditions	I-Operating_System
for	O
a	O
single	O
file	O
,	O
but	O
it	O
does	O
not	O
extend	O
to	O
the	O
file	O
system	O
namespace	O
and	O
other	O
metadata	O
,	O
nor	O
does	O
locking	O
work	O
well	O
with	O
networked	O
filesystems	O
,	O
and	O
cannot	O
prevent	O
TOCTOU	B-Error_Name
race	B-Operating_System
conditions	I-Operating_System
.	O
</s>
