int sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) { size_t rlen, need; u_char *dp; int r; if (dpp != NULL) *dpp = NULL; SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); if ((r = sshbuf_check_reserve(buf, len)) != 0) return r; /* * If the requested allocation appended would push us past max_size * then pack the buffer, zeroing buf->off. */ sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); SSHBUF_TELL("reserve"); if (len + buf->size > buf->alloc) { /* * Prefer to alloc in SSHBUF_SIZE_INC units, but * allocate less if doing so would overflow max_size. */ need = len + buf->size - buf->alloc; rlen = roundup(buf->alloc + need, SSHBUF_SIZE_INC); SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen)); if (rlen > buf->max_size) rlen = buf->alloc + need; SSHBUF_DBG(("adjusted rlen %zu", rlen)); if ((dp = realloc(buf->d, rlen)) == NULL) { SSHBUF_DBG(("realloc fail")); if (dpp != NULL) *dpp = NULL; return SSH_ERR_ALLOC_FAIL; } buf->alloc = rlen; buf->cd = buf->d = dp; if ((r = sshbuf_check_reserve(buf, len)) < 0) { /* shouldn't fail */ if (dpp != NULL) *dpp = NULL; return r; } } dp = buf->d + buf->size; buf->size += len; SSHBUF_TELL("done"); if (dpp != NULL) *dpp = dp; return 0; }
static void sshbuf_maybe_pack(struct sshbuf *buf, int force) { SSHBUF_DBG(("force %d", force)); SSHBUF_TELL("pre-pack"); if (buf->off == 0 || buf->readonly || buf->refcount > 1) return; if (force || (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) { memmove(buf->d, buf->d + buf->off, buf->size - buf->off); buf->size -= buf->off; buf->off = 0; SSHBUF_TELL("packed"); } }
int sshbuf_check_reserve(const struct sshbuf *buf, size_t len) { int r; if ((r = sshbuf_check_sanity(buf)) != 0) return r; if (buf->readonly || buf->refcount > 1) return SSH_ERR_BUFFER_READ_ONLY; SSHBUF_TELL("check"); /* Check that len is reasonable and that max_size + available < len */ if (len > buf->max_size || buf->max_size - len < buf->size - buf->off) return SSH_ERR_NO_BUFFER_SPACE; return 0; }
int sshbuf_consume_end(struct sshbuf *buf, size_t len) { int r; SSHBUF_DBG(("len = %zu", len)); if ((r = sshbuf_check_sanity(buf)) != 0) return r; if (len == 0) return 0; if (len > sshbuf_len(buf)) return SSH_ERR_MESSAGE_INCOMPLETE; buf->size -= len; SSHBUF_TELL("done"); return 0; }
static inline int sshbuf_check_sanity(const struct sshbuf *buf) { SSHBUF_TELL("sanity"); if (__predict_false(buf == NULL || (!buf->readonly && buf->d != buf->cd) || buf->refcount < 1 || buf->refcount > SSHBUF_REFS_MAX || buf->cd == NULL || buf->max_size > SSHBUF_SIZE_MAX || buf->alloc > buf->max_size || buf->size > buf->alloc || buf->off > buf->size)) { /* Do not try to recover from corrupted buffer internals */ SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR")); signal(SIGSEGV, SIG_DFL); raise(SIGSEGV); return SSH_ERR_INTERNAL_ERROR; } return 0; }
int sshbuf_set_max_size(struct sshbuf *buf, size_t max_size) { size_t rlen; u_char *dp; int r; SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size)); if ((r = sshbuf_check_sanity(buf)) != 0) return r; if (max_size == buf->max_size) return 0; if (buf->readonly || buf->refcount > 1) return SSH_ERR_BUFFER_READ_ONLY; if (max_size > SSHBUF_SIZE_MAX) return SSH_ERR_NO_BUFFER_SPACE; /* pack and realloc if necessary */ sshbuf_maybe_pack(buf, max_size < buf->size); if (max_size < buf->alloc && max_size > buf->size) { if (buf->size < SSHBUF_SIZE_INIT) rlen = SSHBUF_SIZE_INIT; else rlen = roundup(buf->size, SSHBUF_SIZE_INC); if (rlen > max_size) rlen = max_size; bzero(buf->d + buf->size, buf->alloc - buf->size); SSHBUF_DBG(("new alloc = %zu", rlen)); if ((dp = realloc(buf->d, rlen)) == NULL) return SSH_ERR_ALLOC_FAIL; buf->cd = buf->d = dp; buf->alloc = rlen; } SSHBUF_TELL("new-max"); if (max_size < buf->alloc) return SSH_ERR_NO_BUFFER_SPACE; buf->max_size = max_size; return 0; }