1 /* $OpenBSD: sshbuf.c,v 1.15 2020/02/26 13:40:09 jsg 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 ssh_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)
148 * If we are a parent with still-extant children, then don't free just
149 * yet. The last child's call to sshbuf_free should decrement our
150 * refcount to 0 and trigger the actual free.
153 if (buf->refcount > 0)
157 * If we are a child, the free our parent to decrement its reference
158 * count and possibly free it.
160 sshbuf_free(buf->parent);
163 if (!buf->readonly) {
164 explicit_bzero(buf->d, buf->alloc);
167 freezero(buf, sizeof(*buf));
171 sshbuf_reset(struct sshbuf *buf)
175 if (buf->readonly || buf->refcount > 1) {
176 /* Nonsensical. Just make buffer appear empty */
177 buf->off = buf->size;
180 (void) sshbuf_check_sanity(buf);
181 buf->off = buf->size = 0;
182 if (buf->alloc != SSHBUF_SIZE_INIT) {
183 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT,
185 buf->cd = buf->d = d;
186 buf->alloc = SSHBUF_SIZE_INIT;
189 explicit_bzero(buf->d, SSHBUF_SIZE_INIT);
193 sshbuf_max_size(const struct sshbuf *buf)
195 return buf->max_size;
199 sshbuf_alloc(const struct sshbuf *buf)
204 const struct sshbuf *
205 sshbuf_parent(const struct sshbuf *buf)
211 sshbuf_refcount(const struct sshbuf *buf)
213 return buf->refcount;
217 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size)
223 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size));
224 if ((r = sshbuf_check_sanity(buf)) != 0)
226 if (max_size == buf->max_size)
228 if (buf->readonly || buf->refcount > 1)
229 return SSH_ERR_BUFFER_READ_ONLY;
230 if (max_size > SSHBUF_SIZE_MAX)
231 return SSH_ERR_NO_BUFFER_SPACE;
232 /* pack and realloc if necessary */
233 sshbuf_maybe_pack(buf, max_size < buf->size);
234 if (max_size < buf->alloc && max_size > buf->size) {
235 if (buf->size < SSHBUF_SIZE_INIT)
236 rlen = SSHBUF_SIZE_INIT;
238 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC);
241 SSHBUF_DBG(("new alloc = %zu", rlen));
242 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL)
243 return SSH_ERR_ALLOC_FAIL;
244 buf->cd = buf->d = dp;
247 SSHBUF_TELL("new-max");
248 if (max_size < buf->alloc)
249 return SSH_ERR_NO_BUFFER_SPACE;
250 buf->max_size = max_size;
255 sshbuf_len(const struct sshbuf *buf)
257 if (sshbuf_check_sanity(buf) != 0)
259 return buf->size - buf->off;
263 sshbuf_avail(const struct sshbuf *buf)
265 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
267 return buf->max_size - (buf->size - buf->off);
271 sshbuf_ptr(const struct sshbuf *buf)
273 if (sshbuf_check_sanity(buf) != 0)
275 return buf->cd + buf->off;
279 sshbuf_mutable_ptr(const struct sshbuf *buf)
281 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
283 return buf->d + buf->off;
287 sshbuf_check_reserve(const struct sshbuf *buf, size_t len)
291 if ((r = sshbuf_check_sanity(buf)) != 0)
293 if (buf->readonly || buf->refcount > 1)
294 return SSH_ERR_BUFFER_READ_ONLY;
295 SSHBUF_TELL("check");
296 /* Check that len is reasonable and that max_size + available < len */
297 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off)
298 return SSH_ERR_NO_BUFFER_SPACE;
303 sshbuf_allocate(struct sshbuf *buf, size_t len)
309 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len));
310 if ((r = sshbuf_check_reserve(buf, len)) != 0)
313 * If the requested allocation appended would push us past max_size
314 * then pack the buffer, zeroing buf->off.
316 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size);
317 SSHBUF_TELL("allocate");
318 if (len + buf->size <= buf->alloc)
319 return 0; /* already have it. */
322 * Prefer to alloc in SSHBUF_SIZE_INC units, but
323 * allocate less if doing so would overflow max_size.
325 need = len + buf->size - buf->alloc;
326 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC);
327 SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen));
328 if (rlen > buf->max_size)
329 rlen = buf->alloc + need;
330 SSHBUF_DBG(("adjusted rlen %zu", rlen));
331 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) {
332 SSHBUF_DBG(("realloc fail"));
333 return SSH_ERR_ALLOC_FAIL;
336 buf->cd = buf->d = dp;
337 if ((r = sshbuf_check_reserve(buf, len)) < 0) {
346 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp)
354 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len));
355 if ((r = sshbuf_allocate(buf, len)) != 0)
358 dp = buf->d + buf->size;
366 sshbuf_consume(struct sshbuf *buf, size_t len)
370 SSHBUF_DBG(("len = %zu", len));
371 if ((r = sshbuf_check_sanity(buf)) != 0)
375 if (len > sshbuf_len(buf))
376 return SSH_ERR_MESSAGE_INCOMPLETE;
378 /* deal with empty buffer */
379 if (buf->off == buf->size)
380 buf->off = buf->size = 0;
386 sshbuf_consume_end(struct sshbuf *buf, size_t len)
390 SSHBUF_DBG(("len = %zu", len));
391 if ((r = sshbuf_check_sanity(buf)) != 0)
395 if (len > sshbuf_len(buf))
396 return SSH_ERR_MESSAGE_INCOMPLETE;