kernel - CAM cleanup 3/N - Remove unnecessary mplocks
[dragonfly.git] / crypto / openssh / sshbuf.c
blob4d6e0ea0acb0242cf83968ccf1d921f9b1c43c65
1 /* $OpenBSD: sshbuf.c,v 1.6 2016/01/12 23:42:54 djm Exp $ */
2 /*
3 * Copyright (c) 2011 Damien Miller
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #define SSHBUF_INTERNAL
19 #include "includes.h"
21 #include <sys/param.h> /* roundup */
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "ssherr.h"
29 #include "sshbuf.h"
31 static inline int
32 sshbuf_check_sanity(const struct sshbuf *buf)
34 SSHBUF_TELL("sanity");
35 if (__predict_false(buf == NULL ||
36 (!buf->readonly && buf->d != buf->cd) ||
37 buf->refcount < 1 || buf->refcount > SSHBUF_REFS_MAX ||
38 buf->cd == NULL ||
39 (buf->dont_free && (buf->readonly || buf->parent != NULL)) ||
40 buf->max_size > SSHBUF_SIZE_MAX ||
41 buf->alloc > buf->max_size ||
42 buf->size > buf->alloc ||
43 buf->off > buf->size)) {
44 /* Do not try to recover from corrupted buffer internals */
45 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
46 signal(SIGSEGV, SIG_DFL);
47 raise(SIGSEGV);
48 return SSH_ERR_INTERNAL_ERROR;
50 return 0;
53 static void
54 sshbuf_maybe_pack(struct sshbuf *buf, int force)
56 SSHBUF_DBG(("force %d", force));
57 SSHBUF_TELL("pre-pack");
58 if (buf->off == 0 || buf->readonly || buf->refcount > 1)
59 return;
60 if (force ||
61 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) {
62 memmove(buf->d, buf->d + buf->off, buf->size - buf->off);
63 buf->size -= buf->off;
64 buf->off = 0;
65 SSHBUF_TELL("packed");
69 struct sshbuf *
70 sshbuf_new(void)
72 struct sshbuf *ret;
74 if ((ret = calloc(sizeof(*ret), 1)) == NULL)
75 return NULL;
76 ret->alloc = SSHBUF_SIZE_INIT;
77 ret->max_size = SSHBUF_SIZE_MAX;
78 ret->readonly = 0;
79 ret->refcount = 1;
80 ret->parent = NULL;
81 if ((ret->cd = ret->d = calloc(1, ret->alloc)) == NULL) {
82 free(ret);
83 return NULL;
85 return ret;
88 struct sshbuf *
89 sshbuf_from(const void *blob, size_t len)
91 struct sshbuf *ret;
93 if (blob == NULL || len > SSHBUF_SIZE_MAX ||
94 (ret = calloc(sizeof(*ret), 1)) == NULL)
95 return NULL;
96 ret->alloc = ret->size = ret->max_size = len;
97 ret->readonly = 1;
98 ret->refcount = 1;
99 ret->parent = NULL;
100 ret->cd = blob;
101 ret->d = NULL;
102 return ret;
106 sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent)
108 int r;
110 if ((r = sshbuf_check_sanity(child)) != 0 ||
111 (r = sshbuf_check_sanity(parent)) != 0)
112 return r;
113 child->parent = parent;
114 child->parent->refcount++;
115 return 0;
118 struct sshbuf *
119 sshbuf_fromb(struct sshbuf *buf)
121 struct sshbuf *ret;
123 if (sshbuf_check_sanity(buf) != 0)
124 return NULL;
125 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL)
126 return NULL;
127 if (sshbuf_set_parent(ret, buf) != 0) {
128 sshbuf_free(ret);
129 return NULL;
131 return ret;
134 void
135 sshbuf_init(struct sshbuf *ret)
137 explicit_bzero(ret, sizeof(*ret));
138 ret->alloc = SSHBUF_SIZE_INIT;
139 ret->max_size = SSHBUF_SIZE_MAX;
140 ret->readonly = 0;
141 ret->dont_free = 1;
142 ret->refcount = 1;
143 if ((ret->cd = ret->d = calloc(1, ret->alloc)) == NULL)
144 ret->alloc = 0;
147 void
148 sshbuf_free(struct sshbuf *buf)
150 int dont_free = 0;
152 if (buf == NULL)
153 return;
155 * The following will leak on insane buffers, but this is the safest
156 * course of action - an invalid pointer or already-freed pointer may
157 * have been passed to us and continuing to scribble over memory would
158 * be bad.
160 if (sshbuf_check_sanity(buf) != 0)
161 return;
163 * If we are a child, the free our parent to decrement its reference
164 * count and possibly free it.
166 sshbuf_free(buf->parent);
167 buf->parent = NULL;
169 * If we are a parent with still-extant children, then don't free just
170 * yet. The last child's call to sshbuf_free should decrement our
171 * refcount to 0 and trigger the actual free.
173 buf->refcount--;
174 if (buf->refcount > 0)
175 return;
176 dont_free = buf->dont_free;
177 if (!buf->readonly) {
178 explicit_bzero(buf->d, buf->alloc);
179 free(buf->d);
181 explicit_bzero(buf, sizeof(*buf));
182 if (!dont_free)
183 free(buf);
186 void
187 sshbuf_reset(struct sshbuf *buf)
189 u_char *d;
191 if (buf->readonly || buf->refcount > 1) {
192 /* Nonsensical. Just make buffer appear empty */
193 buf->off = buf->size;
194 return;
196 if (sshbuf_check_sanity(buf) == 0)
197 explicit_bzero(buf->d, buf->alloc);
198 buf->off = buf->size = 0;
199 if (buf->alloc != SSHBUF_SIZE_INIT) {
200 if ((d = realloc(buf->d, SSHBUF_SIZE_INIT)) != NULL) {
201 buf->cd = buf->d = d;
202 buf->alloc = SSHBUF_SIZE_INIT;
207 size_t
208 sshbuf_max_size(const struct sshbuf *buf)
210 return buf->max_size;
213 size_t
214 sshbuf_alloc(const struct sshbuf *buf)
216 return buf->alloc;
219 const struct sshbuf *
220 sshbuf_parent(const struct sshbuf *buf)
222 return buf->parent;
225 u_int
226 sshbuf_refcount(const struct sshbuf *buf)
228 return buf->refcount;
232 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size)
234 size_t rlen;
235 u_char *dp;
236 int r;
238 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size));
239 if ((r = sshbuf_check_sanity(buf)) != 0)
240 return r;
241 if (max_size == buf->max_size)
242 return 0;
243 if (buf->readonly || buf->refcount > 1)
244 return SSH_ERR_BUFFER_READ_ONLY;
245 if (max_size > SSHBUF_SIZE_MAX)
246 return SSH_ERR_NO_BUFFER_SPACE;
247 /* pack and realloc if necessary */
248 sshbuf_maybe_pack(buf, max_size < buf->size);
249 if (max_size < buf->alloc && max_size > buf->size) {
250 if (buf->size < SSHBUF_SIZE_INIT)
251 rlen = SSHBUF_SIZE_INIT;
252 else
253 rlen = roundup(buf->size, SSHBUF_SIZE_INC);
254 if (rlen > max_size)
255 rlen = max_size;
256 explicit_bzero(buf->d + buf->size, buf->alloc - buf->size);
257 SSHBUF_DBG(("new alloc = %zu", rlen));
258 if ((dp = realloc(buf->d, rlen)) == NULL)
259 return SSH_ERR_ALLOC_FAIL;
260 buf->cd = buf->d = dp;
261 buf->alloc = rlen;
263 SSHBUF_TELL("new-max");
264 if (max_size < buf->alloc)
265 return SSH_ERR_NO_BUFFER_SPACE;
266 buf->max_size = max_size;
267 return 0;
270 size_t
271 sshbuf_len(const struct sshbuf *buf)
273 if (sshbuf_check_sanity(buf) != 0)
274 return 0;
275 return buf->size - buf->off;
278 size_t
279 sshbuf_avail(const struct sshbuf *buf)
281 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
282 return 0;
283 return buf->max_size - (buf->size - buf->off);
286 const u_char *
287 sshbuf_ptr(const struct sshbuf *buf)
289 if (sshbuf_check_sanity(buf) != 0)
290 return NULL;
291 return buf->cd + buf->off;
294 u_char *
295 sshbuf_mutable_ptr(const struct sshbuf *buf)
297 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
298 return NULL;
299 return buf->d + buf->off;
303 sshbuf_check_reserve(const struct sshbuf *buf, size_t len)
305 int r;
307 if ((r = sshbuf_check_sanity(buf)) != 0)
308 return r;
309 if (buf->readonly || buf->refcount > 1)
310 return SSH_ERR_BUFFER_READ_ONLY;
311 SSHBUF_TELL("check");
312 /* Check that len is reasonable and that max_size + available < len */
313 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off)
314 return SSH_ERR_NO_BUFFER_SPACE;
315 return 0;
319 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp)
321 size_t rlen, need;
322 u_char *dp;
323 int r;
325 if (dpp != NULL)
326 *dpp = NULL;
328 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len));
329 if ((r = sshbuf_check_reserve(buf, len)) != 0)
330 return r;
332 * If the requested allocation appended would push us past max_size
333 * then pack the buffer, zeroing buf->off.
335 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size);
336 SSHBUF_TELL("reserve");
337 if (len + buf->size > buf->alloc) {
339 * Prefer to alloc in SSHBUF_SIZE_INC units, but
340 * allocate less if doing so would overflow max_size.
342 need = len + buf->size - buf->alloc;
343 rlen = roundup(buf->alloc + need, SSHBUF_SIZE_INC);
344 SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen));
345 if (rlen > buf->max_size)
346 rlen = buf->alloc + need;
347 SSHBUF_DBG(("adjusted rlen %zu", rlen));
348 if ((dp = realloc(buf->d, rlen)) == NULL) {
349 SSHBUF_DBG(("realloc fail"));
350 if (dpp != NULL)
351 *dpp = NULL;
352 return SSH_ERR_ALLOC_FAIL;
354 buf->alloc = rlen;
355 buf->cd = buf->d = dp;
356 if ((r = sshbuf_check_reserve(buf, len)) < 0) {
357 /* shouldn't fail */
358 if (dpp != NULL)
359 *dpp = NULL;
360 return r;
363 dp = buf->d + buf->size;
364 buf->size += len;
365 SSHBUF_TELL("done");
366 if (dpp != NULL)
367 *dpp = dp;
368 return 0;
372 sshbuf_consume(struct sshbuf *buf, size_t len)
374 int r;
376 SSHBUF_DBG(("len = %zu", len));
377 if ((r = sshbuf_check_sanity(buf)) != 0)
378 return r;
379 if (len == 0)
380 return 0;
381 if (len > sshbuf_len(buf))
382 return SSH_ERR_MESSAGE_INCOMPLETE;
383 buf->off += len;
384 SSHBUF_TELL("done");
385 return 0;
389 sshbuf_consume_end(struct sshbuf *buf, size_t len)
391 int r;
393 SSHBUF_DBG(("len = %zu", len));
394 if ((r = sshbuf_check_sanity(buf)) != 0)
395 return r;
396 if (len == 0)
397 return 0;
398 if (len > sshbuf_len(buf))
399 return SSH_ERR_MESSAGE_INCOMPLETE;
400 buf->size -= len;
401 SSHBUF_TELL("done");
402 return 0;