]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/bzero.3
bzero.3: wfix
[thirdparty/man-pages.git] / man3 / bzero.3
1 .\" Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH BZERO 3 2017-03-13 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 bzero, explicit_bzero \- zero a byte string
28 .SH SYNOPSIS
29 .nf
30 .B #include <strings.h>
31 .sp
32 .BI "void bzero(void *" s ", size_t " n );
33
34 .BI "void explicit_bzero(void *" s ", size_t " n );
35 .fi
36 .SH DESCRIPTION
37 The
38 .BR bzero ()
39 function erases the data in the
40 .I n
41 bytes of the memory starting at the location pointed to by
42 .IR s ,
43 by writing zeroes (bytes containing \(aq\\0\(aq) to that area.
44
45 The
46 .BR explicit_bzero ()
47 function performs the same task as
48 .BR bzero ().
49 It differs from
50 .BR bzero ()
51 in that it guarantees that compiler optimizations will not remove the
52 erase operation if the compiler deduces that the operation is "unnecessary".
53 .SH RETURN VALUE
54 None.
55 .SH VERSIONS
56 .BR explicit_bzero ()
57 first appeared in glibc 2.25.
58 .SH ATTRIBUTES
59 For an explanation of the terms used in this section, see
60 .BR attributes (7).
61 .TS
62 allbox;
63 lb lb lb
64 l l l.
65 Interface Attribute Value
66 T{
67 .BR bzero (),
68 .br
69 .BR explicit_bzero ()
70 T} Thread safety MT-Safe
71 .TE
72 .SH CONFORMING TO
73 The
74 .BR bzero ()
75 function is deprecated (marked as LEGACY in POSIX.1-2001); use
76 .BR memset (3)
77 in new programs.
78 POSIX.1-2008 removes the specification of
79 .BR bzero ().
80 The
81 .BR bzero ()
82 function first appeared in 4.3BSD.
83
84 The
85 .BR explicit_bzero ()
86 function is a nonstandard extension that is also present on some of the BSDs.
87 Some other implementations have a similar function, such as
88 .BR memset_explicit ()
89 or
90 .BR memset_s ().
91 .SH NOTES
92 The
93 .BR explicit_bzero ()
94 function addresses a problem that security-conscious applications
95 may run into when using
96 .BR bzero ():
97 if the compiler can deduce that the location to zeroed will
98 never again be touched by a
99 .I correct
100 program, then it may remove the
101 .BR bzero ()
102 call altogether.
103 This is a problem if the intent of the
104 .BR bzero ()
105 call was to erase sensitive data (e.g., passwords)
106 to prevent the possibility that the data was leaked
107 by an incorrect or compromised program.
108 Calls to
109 .BR explicit_bzero ()
110 are never optimized away by the compiler.
111
112 The
113 .BR explicit_bzero ()
114 function does not solve all problems associated with erasing sensitive data:
115 .IP 1. 3
116 The
117 .BR explicit_bzero ()
118 function does
119 .I not
120 guarantee that sensitive data is completely erased from memory.
121 (The same is true of
122 .BR bzero ().)
123 For example, there may be copies of the sensitive data in
124 a register and in "scratch" stack areas.
125 The
126 .BR explicit_bzero ()
127 function is not aware of these copies, and can't erase them.
128 .IP 2.
129 In some circumstances,
130 .BR explicit_bzero ()
131 can
132 .I decrease
133 security.
134 If the compiler determined that the variable containing the
135 sensitive data could be optimized to be stored in a register
136 (because it is small enough to fit in a register,
137 and no operation other than the
138 .BR explicit_bzero ()
139 call would need to take the address of the variable), then the
140 .BR explicit_bzero ()
141 call will force the data to be copied from the register
142 to a location in RAM that is then immediately erased
143 (while the copy in the register remains unaffected).
144 The problem here is that data in RAM is more likely to be exposed
145 by a bug than data in a register, and thus the
146 .BR explicit_bzero ()
147 call creates a brief time window where the sensitive data is more
148 vulnerable than it would otherwise have been
149 if no attempt had been made to erase the data.
150 .PP
151 Note that declaring the sensitive variable with the
152 .B volatile
153 qualifier does
154 .I not
155 eliminate the above problems.
156 Indeed, it will make them worse, since, for example,
157 it may force a variable that would otherwise have been optimized
158 into a register to instead be maintained in (more vulnerable)
159 RAM for its entire lifetime.
160
161 Notwithstanding the above details, for security-conscious applications, using
162 .BR explicit_bzero ()
163 is generally preferable to not using it.
164 The developers of
165 .BR explicit_bzero ()
166 anticipate that future compilers will recognize calls to
167 .BR explicit_bzero ()
168 and take steps to ensure that all copies of the sensitive data are erased,
169 including copies in registers or in "scratch" stack areas.
170 .SH SEE ALSO
171 .BR bstring (3),
172 .BR memset (3),
173 .BR swab (3)