malloc_get_state.3: tfix
[man-pages.git] / man3 / bzero.3
blobf6c0ca2f318662a770acd13b443f6ea8b32ac195
1 '\" t
2 .\" Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH bzero 3 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 bzero, explicit_bzero \- zero a byte string
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <strings.h>
16 .BI "void bzero(void " s [. n "], size_t " n );
18 .B #include <string.h>
20 .BI "void explicit_bzero(void " s [. n "], size_t " n );
21 .fi
22 .SH DESCRIPTION
23 The
24 .BR bzero ()
25 function erases the data in the
26 .I n
27 bytes of the memory starting at the location pointed to by
28 .IR s ,
29 by writing zeros (bytes containing \[aq]\e0\[aq]) to that area.
31 The
32 .BR explicit_bzero ()
33 function performs the same task as
34 .BR bzero ().
35 It differs from
36 .BR bzero ()
37 in that it guarantees that compiler optimizations will not remove the
38 erase operation if the compiler deduces that the operation is "unnecessary".
39 .SH RETURN VALUE
40 None.
41 .SH ATTRIBUTES
42 For an explanation of the terms used in this section, see
43 .BR attributes (7).
44 .TS
45 allbox;
46 lbx lb lb
47 l l l.
48 Interface       Attribute       Value
50 .na
51 .nh
52 .BR bzero (),
53 .BR explicit_bzero ()
54 T}      Thread safety   MT-Safe
55 .TE
56 .SH STANDARDS
57 None.
58 .SH HISTORY
59 .TP
60 .BR explicit_bzero ()
61 glibc 2.25.
62 .IP
63 The
64 .BR explicit_bzero ()
65 function is a nonstandard extension that is also present on some of the BSDs.
66 Some other implementations have a similar function, such as
67 .BR memset_explicit ()
69 .BR memset_s ().
70 .TP
71 .BR bzero ()
72 4.3BSD.
73 .IP
74 Marked as LEGACY in POSIX.1-2001.
75 Removed in POSIX.1-2008.
76 .SH NOTES
77 The
78 .BR explicit_bzero ()
79 function addresses a problem that security-conscious applications
80 may run into when using
81 .BR bzero ():
82 if the compiler can deduce that the location to be zeroed will
83 never again be touched by a
84 .I correct
85 program, then it may remove the
86 .BR bzero ()
87 call altogether.
88 This is a problem if the intent of the
89 .BR bzero ()
90 call was to erase sensitive data (e.g., passwords)
91 to prevent the possibility that the data was leaked
92 by an incorrect or compromised program.
93 Calls to
94 .BR explicit_bzero ()
95 are never optimized away by the compiler.
97 The
98 .BR explicit_bzero ()
99 function does not solve all problems associated with erasing sensitive data:
100 .IP \[bu] 3
102 .BR explicit_bzero ()
103 function does
104 .I not
105 guarantee that sensitive data is completely erased from memory.
106 (The same is true of
107 .BR bzero ().)
108 For example, there may be copies of the sensitive data in
109 a register and in "scratch" stack areas.
111 .BR explicit_bzero ()
112 function is not aware of these copies, and can't erase them.
113 .IP \[bu]
114 In some circumstances,
115 .BR explicit_bzero ()
117 .I decrease
118 security.
119 If the compiler determined that the variable containing the
120 sensitive data could be optimized to be stored in a register
121 (because it is small enough to fit in a register,
122 and no operation other than the
123 .BR explicit_bzero ()
124 call would need to take the address of the variable), then the
125 .BR explicit_bzero ()
126 call will force the data to be copied from the register
127 to a location in RAM that is then immediately erased
128 (while the copy in the register remains unaffected).
129 The problem here is that data in RAM is more likely to be exposed
130 by a bug than data in a register, and thus the
131 .BR explicit_bzero ()
132 call creates a brief time window where the sensitive data is more
133 vulnerable than it would otherwise have been
134 if no attempt had been made to erase the data.
136 Note that declaring the sensitive variable with the
137 .B volatile
138 qualifier does
139 .I not
140 eliminate the above problems.
141 Indeed, it will make them worse, since, for example,
142 it may force a variable that would otherwise have been optimized
143 into a register to instead be maintained in (more vulnerable)
144 RAM for its entire lifetime.
146 Notwithstanding the above details, for security-conscious applications, using
147 .BR explicit_bzero ()
148 is generally preferable to not using it.
149 The developers of
150 .BR explicit_bzero ()
151 anticipate that future compilers will recognize calls to
152 .BR explicit_bzero ()
153 and take steps to ensure that all copies of the sensitive data are erased,
154 including copies in registers or in "scratch" stack areas.
155 .SH SEE ALSO
156 .BR bstring (3),
157 .BR memset (3),
158 .BR swab (3)