diff options
author | otto <> | 2017-03-06 18:44:21 +0000 |
---|---|---|
committer | otto <> | 2017-03-06 18:44:21 +0000 |
commit | 5c5a16a9022c21d04ee4f0cfd724d03b05747ae8 (patch) | |
tree | 570e08cfdf1bfdb9578fff3188f3567b4b7376ee /src/lib/libc/stdlib/malloc.3 | |
parent | 6e1b969bcaca824c560d5b4adb5de8d84e439819 (diff) | |
download | openbsd-5c5a16a9022c21d04ee4f0cfd724d03b05747ae8.tar.gz openbsd-5c5a16a9022c21d04ee4f0cfd724d03b05747ae8.tar.bz2 openbsd-5c5a16a9022c21d04ee4f0cfd724d03b05747ae8.zip |
Introducing recallocarray(3), a blend of calloc(3) and reallocarray(3)
with the added feature that released memory is cleared. Much input from various
developers. ok deraadt@ tom@
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 70 |
1 files changed, 64 insertions, 6 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index 1f80c3529e..66de428cb0 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -30,9 +30,9 @@ | |||
30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | .\" SUCH DAMAGE. | 31 | .\" SUCH DAMAGE. |
32 | .\" | 32 | .\" |
33 | .\" $OpenBSD: malloc.3,v 1.101 2017/02/12 10:46:09 otto Exp $ | 33 | .\" $OpenBSD: malloc.3,v 1.102 2017/03/06 18:44:21 otto Exp $ |
34 | .\" | 34 | .\" |
35 | .Dd $Mdocdate: February 12 2017 $ | 35 | .Dd $Mdocdate: March 6 2017 $ |
36 | .Dt MALLOC 3 | 36 | .Dt MALLOC 3 |
37 | .Os | 37 | .Os |
38 | .Sh NAME | 38 | .Sh NAME |
@@ -51,6 +51,8 @@ | |||
51 | .Ft void * | 51 | .Ft void * |
52 | .Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" | 52 | .Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" |
53 | .Ft void * | 53 | .Ft void * |
54 | .Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size" | ||
55 | .Ft void * | ||
54 | .Fn realloc "void *ptr" "size_t size" | 56 | .Fn realloc "void *ptr" "size_t size" |
55 | .Ft void | 57 | .Ft void |
56 | .Fn free "void *ptr" | 58 | .Fn free "void *ptr" |
@@ -113,6 +115,33 @@ and checks for integer overflow in the calculation | |||
113 | .Fa size . | 115 | .Fa size . |
114 | .Pp | 116 | .Pp |
115 | The | 117 | The |
118 | .Fn recallocarray | ||
119 | function is similar to | ||
120 | .Fn reallocarray | ||
121 | except that it takes care of clearing newly allocated and freed memory. | ||
122 | If | ||
123 | .Fa ptr | ||
124 | is a | ||
125 | .Dv NULL | ||
126 | pointer, | ||
127 | .Fa oldnmemb | ||
128 | is ignored and the call is equivalent to | ||
129 | .Fn calloc . | ||
130 | If | ||
131 | .Fa ptr | ||
132 | is not a | ||
133 | .Dv NULL | ||
134 | pointer, | ||
135 | .Fa oldnmemb | ||
136 | must be a value such that | ||
137 | .Fa oldnmemb | ||
138 | * | ||
139 | .Fa size | ||
140 | is the size of an earlier allocation that returned | ||
141 | .Fa ptr , | ||
142 | otherwise the behaviour is undefined. | ||
143 | .Pp | ||
144 | The | ||
116 | .Fn free | 145 | .Fn free |
117 | function causes the space pointed to by | 146 | function causes the space pointed to by |
118 | .Fa ptr | 147 | .Fa ptr |
@@ -129,16 +158,18 @@ If | |||
129 | was previously freed by | 158 | was previously freed by |
130 | .Fn free , | 159 | .Fn free , |
131 | .Fn realloc , | 160 | .Fn realloc , |
161 | .Fn reallocarray | ||
132 | or | 162 | or |
133 | .Fn reallocarray , | 163 | .Fn recallocarray , |
134 | the behavior is undefined and the double free is a security concern. | 164 | the behavior is undefined and the double free is a security concern. |
135 | .Sh RETURN VALUES | 165 | .Sh RETURN VALUES |
136 | Upon successful completion, the functions | 166 | Upon successful completion, the functions |
137 | .Fn malloc , | 167 | .Fn malloc , |
138 | .Fn calloc , | 168 | .Fn calloc , |
139 | .Fn realloc , | 169 | .Fn realloc , |
140 | and | ||
141 | .Fn reallocarray | 170 | .Fn reallocarray |
171 | and | ||
172 | .Fn recallocarray | ||
142 | return a pointer to the allocated space; otherwise, a | 173 | return a pointer to the allocated space; otherwise, a |
143 | .Dv NULL | 174 | .Dv NULL |
144 | pointer is returned and | 175 | pointer is returned and |
@@ -161,15 +192,31 @@ If multiplying | |||
161 | and | 192 | and |
162 | .Fa size | 193 | .Fa size |
163 | results in integer overflow, | 194 | results in integer overflow, |
164 | .Fn calloc | 195 | .Fn calloc , |
165 | and | ||
166 | .Fn reallocarray | 196 | .Fn reallocarray |
197 | and | ||
198 | .Fn recallocarray | ||
167 | return | 199 | return |
168 | .Dv NULL | 200 | .Dv NULL |
169 | and set | 201 | and set |
170 | .Va errno | 202 | .Va errno |
171 | to | 203 | to |
172 | .Er ENOMEM . | 204 | .Er ENOMEM . |
205 | .Pp | ||
206 | If | ||
207 | .Fa ptr | ||
208 | is not NULL and multiplying | ||
209 | .Fa oldnmemb | ||
210 | and | ||
211 | .Fa size | ||
212 | results in integer overflow | ||
213 | .Fn recallocarray | ||
214 | returns | ||
215 | .Dv NULL | ||
216 | and sets | ||
217 | .Va errno | ||
218 | to | ||
219 | .Er EINVAL . | ||
173 | .Sh IDIOMS | 220 | .Sh IDIOMS |
174 | Consider | 221 | Consider |
175 | .Fn calloc | 222 | .Fn calloc |
@@ -264,6 +311,17 @@ Use the following: | |||
264 | .Bd -literal -offset indent | 311 | .Bd -literal -offset indent |
265 | newp = realloc(p, newsize); | 312 | newp = realloc(p, newsize); |
266 | .Ed | 313 | .Ed |
314 | .Pp | ||
315 | The | ||
316 | .Fn recallocarray | ||
317 | function should be used for resizing objects containing sensitive data like | ||
318 | keys. | ||
319 | To avoid leaking information, | ||
320 | it guarantees memory is cleared before placing it on the internal free list. | ||
321 | A | ||
322 | .Fn free | ||
323 | call for such an object should still be preceded by a call to | ||
324 | .Xr explicit_bzero 3 . | ||
267 | .Sh ENVIRONMENT | 325 | .Sh ENVIRONMENT |
268 | .Bl -tag -width "/etc/malloc.conf" | 326 | .Bl -tag -width "/etc/malloc.conf" |
269 | .It Ev MALLOC_OPTIONS | 327 | .It Ev MALLOC_OPTIONS |