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