From: djm Date: Fri, 8 Apr 2022 04:40:40 +0000 (+0000) Subject: two defensive changes from Tobias Stoeckmann via GHPR287 X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=fe1380ca86fc055220500fbc416b0d525de05e69;p=openbsd two defensive changes from Tobias Stoeckmann via GHPR287 enforce stricter invarient for sshbuf_set_parent() - never allow a buffer to have a previously-set parent changed. In sshbuf_reset(), if the reallocation fails, then zero the entire buffer and not the (potentially smaller) default initial alloc size. --- diff --git a/usr.bin/ssh/sshbuf.c b/usr.bin/ssh/sshbuf.c index e8c4781cbdd..8db22a0a827 100644 --- a/usr.bin/ssh/sshbuf.c +++ b/usr.bin/ssh/sshbuf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sshbuf.c,v 1.15 2020/02/26 13:40:09 jsg Exp $ */ +/* $OpenBSD: sshbuf.c,v 1.16 2022/04/08 04:40:40 djm Exp $ */ /* * Copyright (c) 2011 Damien Miller * @@ -107,6 +107,8 @@ sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent) if ((r = sshbuf_check_sanity(child)) != 0 || (r = sshbuf_check_sanity(parent)) != 0) return r; + if (child->parent != NULL && child->parent != parent) + return SSH_ERR_INTERNAL_ERROR; child->parent = parent; child->parent->refcount++; return 0; @@ -175,7 +177,8 @@ sshbuf_reset(struct sshbuf *buf) buf->off = buf->size; return; } - (void) sshbuf_check_sanity(buf); + if (sshbuf_check_sanity(buf) != 0) + return; buf->off = buf->size = 0; if (buf->alloc != SSHBUF_SIZE_INIT) { if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT, @@ -184,7 +187,7 @@ sshbuf_reset(struct sshbuf *buf) buf->alloc = SSHBUF_SIZE_INIT; } } - explicit_bzero(buf->d, SSHBUF_SIZE_INIT); + explicit_bzero(buf->d, buf->alloc); } size_t