Index: bitmap.c
===================================================================
RCS file: /cvs/src/usr.bin/ssh/bitmap.c,v
retrieving revision 1.8
diff -u -p -u -r1.8 bitmap.c
--- bitmap.c    1 Jun 2017 06:59:21 -0000       1.8
+++ bitmap.c    3 Oct 2017 19:45:39 -0000
@@ -19,6 +19,7 @@
#include <stdlib.h>

#include "bitmap.h"
+#include "xmalloc.h"

#define BITMAP_WTYPE   u_int
#define BITMAP_MAX     (1<<24)
Index: krl.c
===================================================================
RCS file: /cvs/src/usr.bin/ssh/krl.c,v
retrieving revision 1.40
diff -u -p -u -r1.40 krl.c
--- krl.c       31 May 2017 09:15:42 -0000      1.40
+++ krl.c       3 Oct 2017 19:45:44 -0000
@@ -35,6 +35,7 @@
#include "log.h"
#include "digest.h"
#include "bitmap.h"
+#include "xmalloc.h"

#include "krl.h"

Index: recallocarray.c
===================================================================
RCS file: recallocarray.c
diff -N recallocarray.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ recallocarray.c     3 Oct 2017 19:45:44 -0000
@@ -0,0 +1,83 @@
+/*     $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $  */
+/*
+ * Copyright (c) 2008, 2017 Otto Moerbeek <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "xmalloc.h"
+
+/*
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
+ */
+#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
+
+void *
+recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
+{
+       size_t oldsize, newsize;
+       void *newptr;
+
+       if (ptr == NULL)
+               return calloc(newnmemb, size);
+
+       if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+           newnmemb > 0 && SIZE_MAX / newnmemb < size) {
+               errno = ENOMEM;
+               return NULL;
+       }
+       newsize = newnmemb * size;
+
+       if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+           oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
+               errno = EINVAL;
+               return NULL;
+       }
+       oldsize = oldnmemb * size;
+
+       /*
+        * Don't bother too much if we're shrinking just a bit,
+        * we do not shrink for series of small steps, oh well.
+        */
+       if (newsize <= oldsize) {
+               size_t d = oldsize - newsize;
+
+               if (d < oldsize / 2 && d < getpagesize()) {
+                       memset((char *)ptr + newsize, 0, d);
+                       return ptr;
+               }
+       }
+
+       newptr = malloc(newsize);
+       if (newptr == NULL)
+               return NULL;
+
+       if (newsize > oldsize) {
+               memcpy(newptr, ptr, oldsize);
+               memset((char *)newptr + oldsize, 0, newsize - oldsize);
+       } else
+               memcpy(newptr, ptr, newsize);
+
+       explicit_bzero(ptr, oldsize);
+       free(ptr);
+
+       return newptr;
+}
+DEF_WEAK(recallocarray);
Index: sshbuf.c
===================================================================
RCS file: /cvs/src/usr.bin/ssh/sshbuf.c,v
retrieving revision 1.11
diff -u -p -u -r1.11 sshbuf.c
--- sshbuf.c    1 Jun 2017 06:58:25 -0000       1.11
+++ sshbuf.c    3 Oct 2017 19:45:44 -0000
@@ -21,6 +21,7 @@
#include <stdio.h>
#include <string.h>

+#include "xmalloc.h"
#include "ssherr.h"
#define SSHBUF_INTERNAL
#include "sshbuf.h"
Index: sshkey.c
===================================================================
RCS file: /cvs/src/usr.bin/ssh/sshkey.c,v
retrieving revision 1.56
diff -u -p -u -r1.56 sshkey.c
--- sshkey.c    12 Aug 2017 06:42:52 -0000      1.56
+++ sshkey.c    3 Oct 2017 19:45:49 -0000
@@ -43,6 +43,7 @@
#include <limits.h>
#include <resolv.h>

+#include "xmalloc.h"
#include "ssh2.h"
#include "ssherr.h"
#include "misc.h"
Index: utf8.c
===================================================================
RCS file: /cvs/src/usr.bin/ssh/utf8.c,v
retrieving revision 1.7
diff -u -p -u -r1.7 utf8.c
--- utf8.c      31 May 2017 09:15:42 -0000      1.7
+++ utf8.c      3 Oct 2017 19:45:49 -0000
@@ -31,6 +31,7 @@
#include <wchar.h>

#include "utf8.h"
+#include "xmalloc.h"

static int      dangerous_locale(void);
static int      grow_dst(char **, size_t *, size_t, char **, size_t);
Index: xmalloc.h
===================================================================
RCS file: /cvs/src/usr.bin/ssh/xmalloc.h,v
retrieving revision 1.17
diff -u -p -u -r1.17 xmalloc.h
--- xmalloc.h   31 May 2017 09:15:42 -0000      1.17
+++ xmalloc.h   3 Oct 2017 19:45:49 -0000
@@ -25,3 +25,5 @@ char  *xstrdup(const char *);
int     xasprintf(char **, const char *, ...)
                __attribute__((__format__ (printf, 2, 3)))
                __attribute__((__nonnull__ (2)));
+
+void *recallocarray(void *, size_t, size_t, size_t);
Index: lib/Makefile
===================================================================
RCS file: /cvs/src/usr.bin/ssh/lib/Makefile,v
retrieving revision 1.89
diff -u -p -u -r1.89 Makefile
--- lib/Makefile        1 Jul 2017 13:50:45 -0000       1.89
+++ lib/Makefile        3 Oct 2017 19:45:49 -0000
@@ -33,7 +33,8 @@ SRCS= ${LIB_SRCS} \
       monitor_fdpass.c addrmatch.c \
       smult_curve25519_ref.c \
       kexc25519.c kexc25519c.c kexc25519s.c \
-       chacha.c poly1305.c cipher-chachapoly.c ssh-ed25519.c hmac.c umac.c
+       chacha.c poly1305.c cipher-chachapoly.c ssh-ed25519.c hmac.c umac.c \
+       recallocarray.c

.if (${OPENSSL:L} == "yes")
SRCS+= bufec.c bufbn.c \