]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/bzero.3
man*/: ffix (un-bracket tables)
[thirdparty/man-pages.git] / man3 / bzero.3
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>
15 .PP
16 .BI "void bzero(void " s [. n "], size_t " n );
17 .PP
18 .B #include <string.h>
19 .PP
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.
30 .PP
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
49 T{
50 .na
51 .nh
52 .BR bzero (),
53 .BR explicit_bzero ()
54 T} Thread safety MT-Safe
55 .TE
56 .sp 1
57 .SH STANDARDS
58 None.
59 .SH HISTORY
60 .TP
61 .BR explicit_bzero ()
62 glibc 2.25.
63 .IP
64 The
65 .BR explicit_bzero ()
66 function is a nonstandard extension that is also present on some of the BSDs.
67 Some other implementations have a similar function, such as
68 .BR memset_explicit ()
69 or
70 .BR memset_s ().
71 .TP
72 .BR bzero ()
73 4.3BSD.
74 .IP
75 Marked as LEGACY in POSIX.1-2001.
76 Removed in POSIX.1-2008.
77 .SH NOTES
78 The
79 .BR explicit_bzero ()
80 function addresses a problem that security-conscious applications
81 may run into when using
82 .BR bzero ():
83 if the compiler can deduce that the location to be zeroed will
84 never again be touched by a
85 .I correct
86 program, then it may remove the
87 .BR bzero ()
88 call altogether.
89 This is a problem if the intent of the
90 .BR bzero ()
91 call was to erase sensitive data (e.g., passwords)
92 to prevent the possibility that the data was leaked
93 by an incorrect or compromised program.
94 Calls to
95 .BR explicit_bzero ()
96 are never optimized away by the compiler.
97 .PP
98 The
99 .BR explicit_bzero ()
100 function does not solve all problems associated with erasing sensitive data:
101 .IP \[bu] 3
102 The
103 .BR explicit_bzero ()
104 function does
105 .I not
106 guarantee that sensitive data is completely erased from memory.
107 (The same is true of
108 .BR bzero ().)
109 For example, there may be copies of the sensitive data in
110 a register and in "scratch" stack areas.
111 The
112 .BR explicit_bzero ()
113 function is not aware of these copies, and can't erase them.
114 .IP \[bu]
115 In some circumstances,
116 .BR explicit_bzero ()
117 can
118 .I decrease
119 security.
120 If the compiler determined that the variable containing the
121 sensitive data could be optimized to be stored in a register
122 (because it is small enough to fit in a register,
123 and no operation other than the
124 .BR explicit_bzero ()
125 call would need to take the address of the variable), then the
126 .BR explicit_bzero ()
127 call will force the data to be copied from the register
128 to a location in RAM that is then immediately erased
129 (while the copy in the register remains unaffected).
130 The problem here is that data in RAM is more likely to be exposed
131 by a bug than data in a register, and thus the
132 .BR explicit_bzero ()
133 call creates a brief time window where the sensitive data is more
134 vulnerable than it would otherwise have been
135 if no attempt had been made to erase the data.
136 .PP
137 Note that declaring the sensitive variable with the
138 .B volatile
139 qualifier does
140 .I not
141 eliminate the above problems.
142 Indeed, it will make them worse, since, for example,
143 it may force a variable that would otherwise have been optimized
144 into a register to instead be maintained in (more vulnerable)
145 RAM for its entire lifetime.
146 .PP
147 Notwithstanding the above details, for security-conscious applications, using
148 .BR explicit_bzero ()
149 is generally preferable to not using it.
150 The developers of
151 .BR explicit_bzero ()
152 anticipate that future compilers will recognize calls to
153 .BR explicit_bzero ()
154 and take steps to ensure that all copies of the sensitive data are erased,
155 including copies in registers or in "scratch" stack areas.
156 .SH SEE ALSO
157 .BR bstring (3),
158 .BR memset (3),
159 .BR swab (3)