Section: Linux Programmer's Manual (3)
bzero, explicit_bzero - zero a byte string
void bzero(void *s, size_t n);
void explicit_bzero(void *s, size_t n);
function erases the data in the
bytes of the memory starting at the location pointed to by
by writing zeros (bytes containing '\0') to that area.
function performs the same task as
It differs from
in that it guarantees that compiler optimizations will not remove the
erase operation if the compiler deduces that the operation is "unnecessary".
first appeared in glibc 2.25.
For an explanation of the terms used in this section, see
function is deprecated (marked as LEGACY in POSIX.1-2001); use
in new programs.
POSIX.1-2008 removes the specification of
function first appeared in 4.3BSD.
function is a nonstandard extension that is also present on some of the BSDs.
Some other implementations have a similar function, such as
function addresses a problem that security-conscious applications
may run into when using
if the compiler can deduce that the location to zeroed will
never again be touched by a
program, then it may remove the
This is a problem if the intent of the
call was to erase sensitive data (e.g., passwords)
to prevent the possibility that the data was leaked
by an incorrect or compromised program.
are never optimized away by the compiler.
function does not solve all problems associated with erasing sensitive data:
guarantee that sensitive data is completely erased from memory.
(The same is true of
For example, there may be copies of the sensitive data in
a register and in "scratch" stack areas.
function is not aware of these copies, and can't erase them.
In some circumstances,
If the compiler determined that the variable containing the
sensitive data could be optimized to be stored in a register
(because it is small enough to fit in a register,
and no operation other than the
call would need to take the address of the variable), then the
call will force the data to be copied from the register
to a location in RAM that is then immediately erased
(while the copy in the register remains unaffected).
The problem here is that data in RAM is more likely to be exposed
by a bug than data in a register, and thus the
call creates a brief time window where the sensitive data is more
vulnerable than it would otherwise have been
if no attempt had been made to erase the data.
Note that declaring the sensitive variable with the
eliminate the above problems.
Indeed, it will make them worse, since, for example,
it may force a variable that would otherwise have been optimized
into a register to instead be maintained in (more vulnerable)
RAM for its entire lifetime.
Notwithstanding the above details, for security-conscious applications, using
is generally preferable to not using it.
The developers of
anticipate that future compilers will recognize calls to
and take steps to ensure that all copies of the sensitive data are erased,
including copies in registers or in "scratch" stack areas.
This page is part of release 5.10 of the Linux
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at