1 /* $OpenBSD: sshbuf.c,v 1.12 2018/07/09 21:56:06 markus Exp $ */
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
21 #include <sys/types.h>
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 ||
39 buf->max_size > SSHBUF_SIZE_MAX ||
40 buf->alloc > buf->max_size ||
41 buf->size > buf->alloc ||
42 buf->off > buf->size)) {
43 /* Do not try to recover from corrupted buffer internals */
44 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));
45 signal(SIGSEGV, SIG_DFL);
47 return SSH_ERR_INTERNAL_ERROR;
53 sshbuf_maybe_pack(struct sshbuf *buf, int force)
55 SSHBUF_DBG(("force %d", force));
56 SSHBUF_TELL("pre-pack");
57 if (buf->off == 0 || buf->readonly || buf->refcount > 1)
60 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) {
61 memmove(buf->d, buf->d + buf->off, buf->size - buf->off);
62 buf->size -= buf->off;
64 SSHBUF_TELL("packed");
73 if ((ret = calloc(sizeof(*ret), 1)) == NULL)
75 ret->alloc = SSHBUF_SIZE_INIT;
76 ret->max_size = SSHBUF_SIZE_MAX;
80 if ((ret->cd = ret->d = calloc(1, ret->alloc)) == NULL) {
88 sshbuf_from(const void *blob, size_t len)
92 if (blob == NULL || len > SSHBUF_SIZE_MAX ||
93 (ret = calloc(sizeof(*ret), 1)) == NULL)
95 ret->alloc = ret->size = ret->max_size = len;
105 sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent)
109 if ((r = sshbuf_check_sanity(child)) != 0 ||
110 (r = sshbuf_check_sanity(parent)) != 0)
112 child->parent = parent;
113 child->parent->refcount++;
118 sshbuf_fromb(struct sshbuf *buf)
122 if (sshbuf_check_sanity(buf) != 0)
124 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL)
126 if (sshbuf_set_parent(ret, buf) != 0) {
134 sshbuf_free(struct sshbuf *buf)
139 * The following will leak on insane buffers, but this is the safest
140 * course of action - an invalid pointer or already-freed pointer may
141 * have been passed to us and continuing to scribble over memory would
144 if (sshbuf_check_sanity(buf) != 0)
147 * If we are a child, the free our parent to decrement its reference
148 * count and possibly free it.
150 sshbuf_free(buf->parent);
153 * If we are a parent with still-extant children, then don't free just
154 * yet. The last child's call to sshbuf_free should decrement our
155 * refcount to 0 and trigger the actual free.
158 if (buf->refcount > 0)
160 if (!buf->readonly) {
161 explicit_bzero(buf->d, buf->alloc);
164 explicit_bzero(buf, sizeof(*buf));
169 sshbuf_reset(struct sshbuf *buf)
173 if (buf->readonly || buf->refcount > 1) {
174 /* Nonsensical. Just make buffer appear empty */
175 buf->off = buf->size;
178 (void) sshbuf_check_sanity(buf);
179 buf->off = buf->size = 0;
180 if (buf->alloc != SSHBUF_SIZE_INIT) {
181 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT,
183 buf->cd = buf->d = d;
184 buf->alloc = SSHBUF_SIZE_INIT;
187 explicit_bzero(buf->d, SSHBUF_SIZE_INIT);
191 sshbuf_max_size(const struct sshbuf *buf)
193 return buf->max_size;
197 sshbuf_alloc(const struct sshbuf *buf)
202 const struct sshbuf *
203 sshbuf_parent(const struct sshbuf *buf)
209 sshbuf_refcount(const struct sshbuf *buf)
211 return buf->refcount;
215 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size)
221 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size));
222 if ((r = sshbuf_check_sanity(buf)) != 0)
224 if (max_size == buf->max_size)
226 if (buf->readonly || buf->refcount > 1)
227 return SSH_ERR_BUFFER_READ_ONLY;
228 if (max_size > SSHBUF_SIZE_MAX)
229 return SSH_ERR_NO_BUFFER_SPACE;
230 /* pack and realloc if necessary */
231 sshbuf_maybe_pack(buf, max_size < buf->size);
232 if (max_size < buf->alloc && max_size > buf->size) {
233 if (buf->size < SSHBUF_SIZE_INIT)
234 rlen = SSHBUF_SIZE_INIT;
236 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC);
239 SSHBUF_DBG(("new alloc = %zu", rlen));
240 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL)
241 return SSH_ERR_ALLOC_FAIL;
242 buf->cd = buf->d = dp;
245 SSHBUF_TELL("new-max");
246 if (max_size < buf->alloc)
247 return SSH_ERR_NO_BUFFER_SPACE;
248 buf->max_size = max_size;
253 sshbuf_len(const struct sshbuf *buf)
255 if (sshbuf_check_sanity(buf) != 0)
257 return buf->size - buf->off;
261 sshbuf_avail(const struct sshbuf *buf)
263 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
265 return buf->max_size - (buf->size - buf->off);
269 sshbuf_ptr(const struct sshbuf *buf)
271 if (sshbuf_check_sanity(buf) != 0)
273 return buf->cd + buf->off;
277 sshbuf_mutable_ptr(const struct sshbuf *buf)
279 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
281 return buf->d + buf->off;
285 sshbuf_check_reserve(const struct sshbuf *buf, size_t len)
289 if ((r = sshbuf_check_sanity(buf)) != 0)
291 if (buf->readonly || buf->refcount > 1)
292 return SSH_ERR_BUFFER_READ_ONLY;
293 SSHBUF_TELL("check");
294 /* Check that len is reasonable and that max_size + available < len */
295 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off)
296 return SSH_ERR_NO_BUFFER_SPACE;
301 sshbuf_allocate(struct sshbuf *buf, size_t len)
307 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len));
308 if ((r = sshbuf_check_reserve(buf, len)) != 0)
311 * If the requested allocation appended would push us past max_size
312 * then pack the buffer, zeroing buf->off.
314 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size);
315 SSHBUF_TELL("allocate");
316 if (len + buf->size <= buf->alloc)
317 return 0; /* already have it. */
320 * Prefer to alloc in SSHBUF_SIZE_INC units, but
321 * allocate less if doing so would overflow max_size.
323 need = len + buf->size - buf->alloc;
324 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC);
325 SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen));
326 if (rlen > buf->max_size)
327 rlen = buf->alloc + need;
328 SSHBUF_DBG(("adjusted rlen %zu", rlen));
329 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) {
330 SSHBUF_DBG(("realloc fail"));
331 return SSH_ERR_ALLOC_FAIL;
334 buf->cd = buf->d = dp;
335 if ((r = sshbuf_check_reserve(buf, len)) < 0) {
344 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp)
352 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len));
353 if ((r = sshbuf_allocate(buf, len)) != 0)
356 dp = buf->d + buf->size;
364 sshbuf_consume(struct sshbuf *buf, size_t len)
368 SSHBUF_DBG(("len = %zu", len));
369 if ((r = sshbuf_check_sanity(buf)) != 0)
373 if (len > sshbuf_len(buf))
374 return SSH_ERR_MESSAGE_INCOMPLETE;
376 /* deal with empty buffer */
377 if (buf->off == buf->size)
378 buf->off = buf->size = 0;
384 sshbuf_consume_end(struct sshbuf *buf, size_t len)
388 SSHBUF_DBG(("len = %zu", len));
389 if ((r = sshbuf_check_sanity(buf)) != 0)
393 if (len > sshbuf_len(buf))
394 return SSH_ERR_MESSAGE_INCOMPLETE;