summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog16
-rw-r--r--auth-bsdauth.c5
-rw-r--r--auth-skey.c11
-rw-r--r--auth.c12
-rw-r--r--auth2-chall.c2
-rw-r--r--channels.c7
-rw-r--r--clientloop.c5
-rw-r--r--deattack.c2
-rw-r--r--gss-genr.c6
-rw-r--r--kex.c8
-rw-r--r--key.c10
-rw-r--r--misc.c6
-rw-r--r--moduli.c17
-rw-r--r--monitor.c6
-rw-r--r--monitor_wrap.c10
-rw-r--r--packet.c4
-rw-r--r--scard.c2
-rw-r--r--sftp-server.c2
-rw-r--r--ssh-agent.c9
-rw-r--r--ssh-keyscan.c18
-rw-r--r--ssh.c5
-rw-r--r--sshconnect.c17
-rw-r--r--sshconnect2.c10
-rw-r--r--sshd.c16
-rw-r--r--uuencode.c7
-rw-r--r--xmalloc.c32
-rw-r--r--xmalloc.h6
27 files changed, 127 insertions, 124 deletions
diff --git a/ChangeLog b/ChangeLog
index 04275b22c..20d034a6e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -104,6 +104,20 @@
104 - deraadt@cvs.openbsd.org 2006/03/20 21:11:53 104 - deraadt@cvs.openbsd.org 2006/03/20 21:11:53
105 [ttymodes.c] 105 [ttymodes.c]
106 spacing 106 spacing
107 - djm@cvs.openbsd.org 2006/03/25 00:05:41
108 [auth-bsdauth.c auth-skey.c auth.c auth2-chall.c channels.c]
109 [clientloop.c deattack.c gss-genr.c kex.c key.c misc.c moduli.c]
110 [monitor.c monitor_wrap.c packet.c scard.c sftp-server.c ssh-agent.c]
111 [ssh-keyscan.c ssh.c sshconnect.c sshconnect2.c sshd.c uuencode.c]
112 [xmalloc.c xmalloc.h]
113 introduce xcalloc() and xasprintf() failure-checked allocations
114 functions and use them throughout openssh
115
116 xcalloc is particularly important because malloc(nmemb * size) is a
117 dangerous idiom (subject to integer overflow) and it is time for it
118 to die
119
120 feedback and ok deraadt@
107 121
10820060325 12220060325
109 - OpenBSD CVS Sync 123 - OpenBSD CVS Sync
@@ -4361,4 +4375,4 @@
4361 - (djm) Trim deprecated options from INSTALL. Mention UsePAM 4375 - (djm) Trim deprecated options from INSTALL. Mention UsePAM
4362 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu 4376 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
4363 4377
4364$Id: ChangeLog,v 1.4272 2006/03/26 03:10:34 djm Exp $ 4378$Id: ChangeLog,v 1.4273 2006/03/26 03:19:21 djm Exp $
diff --git a/auth-bsdauth.c b/auth-bsdauth.c
index f48b43174..2ccbc9d43 100644
--- a/auth-bsdauth.c
+++ b/auth-bsdauth.c
@@ -68,9 +68,8 @@ bsdauth_query(void *ctx, char **name, char **infotxt,
68 *name = xstrdup(""); 68 *name = xstrdup("");
69 *infotxt = xstrdup(""); 69 *infotxt = xstrdup("");
70 *numprompts = 1; 70 *numprompts = 1;
71 *prompts = xmalloc(*numprompts * sizeof(char *)); 71 *prompts = xcalloc(*numprompts, sizeof(char *));
72 *echo_on = xmalloc(*numprompts * sizeof(u_int)); 72 *echo_on = xcalloc(*numprompts, sizeof(u_int));
73 (*echo_on)[0] = 0;
74 (*prompts)[0] = xstrdup(challenge); 73 (*prompts)[0] = xstrdup(challenge);
75 74
76 return 0; 75 return 0;
diff --git a/auth-skey.c b/auth-skey.c
index ce8c1a809..3e6a06db7 100644
--- a/auth-skey.c
+++ b/auth-skey.c
@@ -53,15 +53,10 @@ skey_query(void *ctx, char **name, char **infotxt,
53 *name = xstrdup(""); 53 *name = xstrdup("");
54 *infotxt = xstrdup(""); 54 *infotxt = xstrdup("");
55 *numprompts = 1; 55 *numprompts = 1;
56 *prompts = xmalloc(*numprompts * sizeof(char *)); 56 *prompts = xcalloc(*numprompts, sizeof(char *));
57 *echo_on = xmalloc(*numprompts * sizeof(u_int)); 57 *echo_on = xcalloc(*numprompts, sizeof(u_int));
58 (*echo_on)[0] = 0;
59 58
60 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1; 59 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
61 p = xmalloc(len);
62 strlcpy(p, challenge, len);
63 strlcat(p, SKEY_PROMPT, len);
64 (*prompts)[0] = p;
65 60
66 return 0; 61 return 0;
67} 62}
diff --git a/auth.c b/auth.c
index 85c6f8d1d..aa6d66075 100644
--- a/auth.c
+++ b/auth.c
@@ -340,7 +340,8 @@ auth_root_allowed(char *method)
340static char * 340static char *
341expand_authorized_keys(const char *filename, struct passwd *pw) 341expand_authorized_keys(const char *filename, struct passwd *pw)
342{ 342{
343 char *file, *ret; 343 char *file, ret[MAXPATHLEN];
344 int i;
344 345
345 file = percent_expand(filename, "h", pw->pw_dir, 346 file = percent_expand(filename, "h", pw->pw_dir,
346 "u", pw->pw_name, (char *)NULL); 347 "u", pw->pw_name, (char *)NULL);
@@ -352,14 +353,11 @@ expand_authorized_keys(const char *filename, struct passwd *pw)
352 if (*file == '/') 353 if (*file == '/')
353 return (file); 354 return (file);
354 355
355 ret = xmalloc(MAXPATHLEN); 356 i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
356 if (strlcpy(ret, pw->pw_dir, MAXPATHLEN) >= MAXPATHLEN || 357 if (i < 0 || (size_t)i >= sizeof(ret))
357 strlcat(ret, "/", MAXPATHLEN) >= MAXPATHLEN ||
358 strlcat(ret, file, MAXPATHLEN) >= MAXPATHLEN)
359 fatal("expand_authorized_keys: path too long"); 358 fatal("expand_authorized_keys: path too long");
360
361 xfree(file); 359 xfree(file);
362 return (ret); 360 return (xstrdup(ret));
363} 361}
364 362
365char * 363char *
diff --git a/auth2-chall.c b/auth2-chall.c
index 8860a94c5..d54ee2856 100644
--- a/auth2-chall.c
+++ b/auth2-chall.c
@@ -290,7 +290,7 @@ input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
290 if (nresp > 100) 290 if (nresp > 100)
291 fatal("input_userauth_info_response: too many replies"); 291 fatal("input_userauth_info_response: too many replies");
292 if (nresp > 0) { 292 if (nresp > 0) {
293 response = xmalloc(nresp * sizeof(char *)); 293 response = xcalloc(nresp, sizeof(char *));
294 for (i = 0; i < nresp; i++) 294 for (i = 0; i < nresp; i++)
295 response[i] = packet_get_string(NULL); 295 response[i] = packet_get_string(NULL);
296 } 296 }
diff --git a/channels.c b/channels.c
index 1ff7152a8..0e7d5cf58 100644
--- a/channels.c
+++ b/channels.c
@@ -249,7 +249,7 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
249 /* Do initial allocation if this is the first call. */ 249 /* Do initial allocation if this is the first call. */
250 if (channels_alloc == 0) { 250 if (channels_alloc == 0) {
251 channels_alloc = 10; 251 channels_alloc = 10;
252 channels = xmalloc(channels_alloc * sizeof(Channel *)); 252 channels = xcalloc(channels_alloc, sizeof(Channel *));
253 for (i = 0; i < channels_alloc; i++) 253 for (i = 0; i < channels_alloc; i++)
254 channels[i] = NULL; 254 channels[i] = NULL;
255 } 255 }
@@ -274,8 +274,7 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
274 channels[i] = NULL; 274 channels[i] = NULL;
275 } 275 }
276 /* Initialize and return new channel. */ 276 /* Initialize and return new channel. */
277 c = channels[found] = xmalloc(sizeof(Channel)); 277 c = channels[found] = xcalloc(1, sizeof(Channel));
278 memset(c, 0, sizeof(Channel));
279 buffer_init(&c->input); 278 buffer_init(&c->input);
280 buffer_init(&c->output); 279 buffer_init(&c->output);
281 buffer_init(&c->extended); 280 buffer_init(&c->extended);
@@ -2842,7 +2841,7 @@ x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
2842 } 2841 }
2843 2842
2844 /* Allocate a channel for each socket. */ 2843 /* Allocate a channel for each socket. */
2845 *chanids = xmalloc(sizeof(**chanids) * (num_socks + 1)); 2844 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
2846 for (n = 0; n < num_socks; n++) { 2845 for (n = 0; n < num_socks; n++) {
2847 sock = socks[n]; 2846 sock = socks[n];
2848 nc = channel_new("x11 listener", 2847 nc = channel_new("x11 listener",
diff --git a/clientloop.c b/clientloop.c
index 36a4a64ae..aa4ebb3aa 100644
--- a/clientloop.c
+++ b/clientloop.c
@@ -820,8 +820,7 @@ client_process_control(fd_set * readset)
820 return; 820 return;
821 } 821 }
822 822
823 cctx = xmalloc(sizeof(*cctx)); 823 cctx = xcalloc(1, sizeof(*cctx));
824 memset(cctx, 0, sizeof(*cctx));
825 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0; 824 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
826 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0; 825 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
827 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0; 826 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
@@ -836,7 +835,7 @@ client_process_control(fd_set * readset)
836 env_len = MIN(env_len, 4096); 835 env_len = MIN(env_len, 4096);
837 debug3("%s: receiving %d env vars", __func__, env_len); 836 debug3("%s: receiving %d env vars", __func__, env_len);
838 if (env_len != 0) { 837 if (env_len != 0) {
839 cctx->env = xmalloc(sizeof(*cctx->env) * (env_len + 1)); 838 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
840 for (i = 0; i < env_len; i++) 839 for (i = 0; i < env_len; i++)
841 cctx->env[i] = buffer_get_string(&m, &len); 840 cctx->env[i] = buffer_get_string(&m, &len);
842 cctx->env[i] = NULL; 841 cctx->env[i] = NULL;
diff --git a/deattack.c b/deattack.c
index bf4451b88..746ff5d43 100644
--- a/deattack.c
+++ b/deattack.c
@@ -93,7 +93,7 @@ detect_attack(u_char *buf, u_int32_t len)
93 93
94 if (h == NULL) { 94 if (h == NULL) {
95 debug("Installing crc compensation attack detector."); 95 debug("Installing crc compensation attack detector.");
96 h = (u_int16_t *) xmalloc(l * HASH_ENTRYSIZE); 96 h = (u_int16_t *) xcalloc(l, HASH_ENTRYSIZE);
97 n = l; 97 n = l;
98 } else { 98 } else {
99 if (l > n) { 99 if (l > n) {
diff --git a/gss-genr.c b/gss-genr.c
index 8d75ee5c7..9cedfcdc3 100644
--- a/gss-genr.c
+++ b/gss-genr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gss-genr.c,v 1.7 2006/03/20 04:07:49 djm Exp $ */ 1/* $OpenBSD: gss-genr.c,v 1.8 2006/03/25 00:05:41 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. 4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
@@ -135,9 +135,7 @@ ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
135void 135void
136ssh_gssapi_build_ctx(Gssctxt **ctx) 136ssh_gssapi_build_ctx(Gssctxt **ctx)
137{ 137{
138 *ctx = xmalloc(sizeof (Gssctxt)); 138 *ctx = xcalloc(1, sizeof (Gssctxt));
139 (*ctx)->major = 0;
140 (*ctx)->minor = 0;
141 (*ctx)->context = GSS_C_NO_CONTEXT; 139 (*ctx)->context = GSS_C_NO_CONTEXT;
142 (*ctx)->name = GSS_C_NO_NAME; 140 (*ctx)->name = GSS_C_NO_NAME;
143 (*ctx)->oid = GSS_C_NO_OID; 141 (*ctx)->oid = GSS_C_NO_OID;
diff --git a/kex.c b/kex.c
index 91081b18e..030df6be0 100644
--- a/kex.c
+++ b/kex.c
@@ -82,7 +82,7 @@ kex_buf2prop(Buffer *raw, int *first_kex_follows)
82 int i; 82 int i;
83 char **proposal; 83 char **proposal;
84 84
85 proposal = xmalloc(PROPOSAL_MAX * sizeof(char *)); 85 proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
86 86
87 buffer_init(&b); 87 buffer_init(&b);
88 buffer_append(&b, buffer_ptr(raw), buffer_len(raw)); 88 buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
@@ -217,8 +217,7 @@ kex_setup(char *proposal[PROPOSAL_MAX])
217{ 217{
218 Kex *kex; 218 Kex *kex;
219 219
220 kex = xmalloc(sizeof(*kex)); 220 kex = xcalloc(1, sizeof(*kex));
221 memset(kex, 0, sizeof(*kex));
222 buffer_init(&kex->peer); 221 buffer_init(&kex->peer);
223 buffer_init(&kex->my); 222 buffer_init(&kex->my);
224 kex_prop2buf(&kex->my, proposal); 223 kex_prop2buf(&kex->my, proposal);
@@ -379,8 +378,7 @@ kex_choose_conf(Kex *kex)
379 378
380 /* Algorithm Negotiation */ 379 /* Algorithm Negotiation */
381 for (mode = 0; mode < MODE_MAX; mode++) { 380 for (mode = 0; mode < MODE_MAX; mode++) {
382 newkeys = xmalloc(sizeof(*newkeys)); 381 newkeys = xcalloc(1, sizeof(*newkeys));
383 memset(newkeys, 0, sizeof(*newkeys));
384 kex->newkeys[mode] = newkeys; 382 kex->newkeys[mode] = newkeys;
385 ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN); 383 ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
386 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; 384 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
diff --git a/key.c b/key.c
index d6dd3abea..0d29593b0 100644
--- a/key.c
+++ b/key.c
@@ -49,9 +49,8 @@ key_new(int type)
49 Key *k; 49 Key *k;
50 RSA *rsa; 50 RSA *rsa;
51 DSA *dsa; 51 DSA *dsa;
52 k = xmalloc(sizeof(*k)); 52 k = xcalloc(1, sizeof(*k));
53 k->type = type; 53 k->type = type;
54 k->flags = 0;
55 k->dsa = NULL; 54 k->dsa = NULL;
56 k->rsa = NULL; 55 k->rsa = NULL;
57 switch (k->type) { 56 switch (k->type) {
@@ -231,8 +230,7 @@ key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
231 char *retval; 230 char *retval;
232 u_int i; 231 u_int i;
233 232
234 retval = xmalloc(dgst_raw_len * 3 + 1); 233 retval = xcalloc(1, dgst_raw_len * 3 + 1);
235 retval[0] = '\0';
236 for (i = 0; i < dgst_raw_len; i++) { 234 for (i = 0; i < dgst_raw_len; i++) {
237 char hex[4]; 235 char hex[4];
238 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); 236 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
@@ -254,7 +252,7 @@ key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
254 char *retval; 252 char *retval;
255 253
256 rounds = (dgst_raw_len / 2) + 1; 254 rounds = (dgst_raw_len / 2) + 1;
257 retval = xmalloc(sizeof(char) * (rounds*6)); 255 retval = xcalloc((rounds * 6), sizeof(char));
258 retval[j++] = 'x'; 256 retval[j++] = 'x';
259 for (i = 0; i < rounds; i++) { 257 for (i = 0; i < rounds; i++) {
260 u_int idx0, idx1, idx2, idx3, idx4; 258 u_int idx0, idx1, idx2, idx3, idx4;
@@ -824,7 +822,7 @@ key_demote(const Key *k)
824{ 822{
825 Key *pk; 823 Key *pk;
826 824
827 pk = xmalloc(sizeof(*pk)); 825 pk = xcalloc(1, sizeof(*pk));
828 pk->type = k->type; 826 pk->type = k->type;
829 pk->flags = k->flags; 827 pk->flags = k->flags;
830 pk->dsa = NULL; 828 pk->dsa = NULL;
diff --git a/misc.c b/misc.c
index 1949dd4b3..bf7b1ed66 100644
--- a/misc.c
+++ b/misc.c
@@ -172,9 +172,8 @@ strdelim(char **s)
172struct passwd * 172struct passwd *
173pwcopy(struct passwd *pw) 173pwcopy(struct passwd *pw)
174{ 174{
175 struct passwd *copy = xmalloc(sizeof(*copy)); 175 struct passwd *copy = xcalloc(1, sizeof(*copy));
176 176
177 memset(copy, 0, sizeof(*copy));
178 copy->pw_name = xstrdup(pw->pw_name); 177 copy->pw_name = xstrdup(pw->pw_name);
179 copy->pw_passwd = xstrdup(pw->pw_passwd); 178 copy->pw_passwd = xstrdup(pw->pw_passwd);
180 copy->pw_gecos = xstrdup(pw->pw_gecos); 179 copy->pw_gecos = xstrdup(pw->pw_gecos);
@@ -697,8 +696,7 @@ tohex(const u_char *d, u_int l)
697 u_int i, hl; 696 u_int i, hl;
698 697
699 hl = l * 2 + 1; 698 hl = l * 2 + 1;
700 r = xmalloc(hl); 699 r = xcalloc(1, hl);
701 *r = '\0';
702 for (i = 0; i < l; i++) { 700 for (i = 0; i < l; i++) {
703 snprintf(b, sizeof(b), "%02x", d[i]); 701 snprintf(b, sizeof(b), "%02x", d[i]);
704 strlcat(r, b, hl); 702 strlcat(r, b, hl);
diff --git a/moduli.c b/moduli.c
index d53806ea6..f6f15a2a4 100644
--- a/moduli.c
+++ b/moduli.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: moduli.c,v 1.12 2005/07/17 07:17:55 djm Exp $ */ 1/* $OpenBSD: moduli.c,v 1.13 2006/03/25 00:05:41 djm Exp $ */
2/* 2/*
3 * Copyright 1994 Phil Karn <karn@qualcomm.com> 3 * Copyright 1994 Phil Karn <karn@qualcomm.com>
4 * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com> 4 * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com>
@@ -301,21 +301,10 @@ gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start)
301 largewords = (largememory << SHIFT_MEGAWORD); 301 largewords = (largememory << SHIFT_MEGAWORD);
302 } 302 }
303 303
304 TinySieve = calloc(tinywords, sizeof(u_int32_t)); 304 TinySieve = xcalloc(tinywords, sizeof(u_int32_t));
305 if (TinySieve == NULL) {
306 error("Insufficient memory for tiny sieve: need %u bytes",
307 tinywords << SHIFT_BYTE);
308 exit(1);
309 }
310 tinybits = tinywords << SHIFT_WORD; 305 tinybits = tinywords << SHIFT_WORD;
311 306
312 SmallSieve = calloc(smallwords, sizeof(u_int32_t)); 307 SmallSieve = xcalloc(smallwords, sizeof(u_int32_t));
313 if (SmallSieve == NULL) {
314 error("Insufficient memory for small sieve: need %u bytes",
315 smallwords << SHIFT_BYTE);
316 xfree(TinySieve);
317 exit(1);
318 }
319 smallbits = smallwords << SHIFT_WORD; 308 smallbits = smallwords << SHIFT_WORD;
320 309
321 /* 310 /*
diff --git a/monitor.c b/monitor.c
index 97b420fc3..7409be32b 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1625,8 +1625,7 @@ mm_get_kex(Buffer *m)
1625 void *blob; 1625 void *blob;
1626 u_int bloblen; 1626 u_int bloblen;
1627 1627
1628 kex = xmalloc(sizeof(*kex)); 1628 kex = xcalloc(1, sizeof(*kex));
1629 memset(kex, 0, sizeof(*kex));
1630 kex->session_id = buffer_get_string(m, &kex->session_id_len); 1629 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1631 if ((session_id2 == NULL) || 1630 if ((session_id2 == NULL) ||
1632 (kex->session_id_len != session_id2_len) || 1631 (kex->session_id_len != session_id2_len) ||
@@ -1796,9 +1795,8 @@ monitor_init(void)
1796 struct monitor *mon; 1795 struct monitor *mon;
1797 int pair[2]; 1796 int pair[2];
1798 1797
1799 mon = xmalloc(sizeof(*mon)); 1798 mon = xcalloc(1, sizeof(*mon));
1800 1799
1801 mon->m_pid = 0;
1802 monitor_socketpair(pair); 1800 monitor_socketpair(pair);
1803 1801
1804 mon->m_recvfd = pair[0]; 1802 mon->m_recvfd = pair[0];
diff --git a/monitor_wrap.c b/monitor_wrap.c
index e5a65491d..cd340360a 100644
--- a/monitor_wrap.c
+++ b/monitor_wrap.c
@@ -859,8 +859,8 @@ mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
859 *name = xstrdup(""); 859 *name = xstrdup("");
860 *infotxt = xstrdup(""); 860 *infotxt = xstrdup("");
861 *numprompts = 1; 861 *numprompts = 1;
862 *prompts = xmalloc(*numprompts * sizeof(char *)); 862 *prompts = xcalloc(*numprompts, sizeof(char *));
863 *echo_on = xmalloc(*numprompts * sizeof(u_int)); 863 *echo_on = xcalloc(*numprompts, sizeof(u_int));
864 (*echo_on)[0] = 0; 864 (*echo_on)[0] = 0;
865} 865}
866 866
@@ -953,11 +953,7 @@ mm_skey_query(void *ctx, char **name, char **infotxt,
953 953
954 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 954 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
955 955
956 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1; 956 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
957 p = xmalloc(len);
958 strlcpy(p, challenge, len);
959 strlcat(p, SKEY_PROMPT, len);
960 (*prompts)[0] = p;
961 xfree(challenge); 957 xfree(challenge);
962 958
963 return (0); 959 return (0);
diff --git a/packet.c b/packet.c
index 0121f8aee..40c6b1d2b 100644
--- a/packet.c
+++ b/packet.c
@@ -877,7 +877,7 @@ packet_read_seqnr(u_int32_t *seqnr_p)
877 char buf[8192]; 877 char buf[8192];
878 DBG(debug("packet_read()")); 878 DBG(debug("packet_read()"));
879 879
880 setp = (fd_set *)xmalloc(howmany(connection_in+1, NFDBITS) * 880 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS),
881 sizeof(fd_mask)); 881 sizeof(fd_mask));
882 882
883 /* Since we are blocking, ensure that all written packets have been sent. */ 883 /* Since we are blocking, ensure that all written packets have been sent. */
@@ -1419,7 +1419,7 @@ packet_write_wait(void)
1419{ 1419{
1420 fd_set *setp; 1420 fd_set *setp;
1421 1421
1422 setp = (fd_set *)xmalloc(howmany(connection_out + 1, NFDBITS) * 1422 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS),
1423 sizeof(fd_mask)); 1423 sizeof(fd_mask));
1424 packet_write_poll(); 1424 packet_write_poll();
1425 while (packet_have_data_to_write()) { 1425 while (packet_have_data_to_write()) {
diff --git a/scard.c b/scard.c
index 7cffc2d4e..c0c22aa73 100644
--- a/scard.c
+++ b/scard.c
@@ -382,7 +382,7 @@ sc_get_keys(const char *id, const char *pin)
382 key_free(k); 382 key_free(k);
383 return NULL; 383 return NULL;
384 } 384 }
385 keys = xmalloc((nkeys+1) * sizeof(Key *)); 385 keys = xcalloc((nkeys+1), sizeof(Key *));
386 386
387 n = key_new(KEY_RSA1); 387 n = key_new(KEY_RSA1);
388 BN_copy(n->rsa->n, k->rsa->n); 388 BN_copy(n->rsa->n, k->rsa->n);
diff --git a/sftp-server.c b/sftp-server.c
index cf3458120..a6add52aa 100644
--- a/sftp-server.c
+++ b/sftp-server.c
@@ -712,7 +712,7 @@ process_readdir(void)
712 Stat *stats; 712 Stat *stats;
713 int nstats = 10, count = 0, i; 713 int nstats = 10, count = 0, i;
714 714
715 stats = xmalloc(nstats * sizeof(Stat)); 715 stats = xcalloc(nstats, sizeof(Stat));
716 while ((dp = readdir(dirp)) != NULL) { 716 while ((dp = readdir(dirp)) != NULL) {
717 if (count >= nstats) { 717 if (count >= nstats) {
718 nstats *= 2; 718 nstats *= 2;
diff --git a/ssh-agent.c b/ssh-agent.c
index 7feb898dd..67bde5560 100644
--- a/ssh-agent.c
+++ b/ssh-agent.c
@@ -109,8 +109,8 @@ int max_fd = 0;
109pid_t parent_pid = -1; 109pid_t parent_pid = -1;
110 110
111/* pathname and directory for AUTH_SOCKET */ 111/* pathname and directory for AUTH_SOCKET */
112char socket_name[1024]; 112char socket_name[MAXPATHLEN];
113char socket_dir[1024]; 113char socket_dir[MAXPATHLEN];
114 114
115/* locking */ 115/* locking */
116int locked = 0; 116int locked = 0;
@@ -803,10 +803,7 @@ new_socket(sock_type type, int fd)
803 } 803 }
804 old_alloc = sockets_alloc; 804 old_alloc = sockets_alloc;
805 new_alloc = sockets_alloc + 10; 805 new_alloc = sockets_alloc + 10;
806 if (sockets) 806 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
807 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
808 else
809 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
810 for (i = old_alloc; i < new_alloc; i++) 807 for (i = old_alloc; i < new_alloc; i++)
811 sockets[i].type = AUTH_UNUSED; 808 sockets[i].type = AUTH_UNUSED;
812 sockets_alloc = new_alloc; 809 sockets_alloc = new_alloc;
diff --git a/ssh-keyscan.c b/ssh-keyscan.c
index c7296938b..07b679442 100644
--- a/ssh-keyscan.c
+++ b/ssh-keyscan.c
@@ -54,7 +54,7 @@ int maxfd;
54 54
55extern char *__progname; 55extern char *__progname;
56fd_set *read_wait; 56fd_set *read_wait;
57size_t read_wait_size; 57size_t read_wait_nfdset;
58int ncon; 58int ncon;
59int nonfatal_fatal = 0; 59int nonfatal_fatal = 0;
60jmp_buf kexjmp; 60jmp_buf kexjmp;
@@ -634,10 +634,10 @@ conloop(void)
634 } else 634 } else
635 seltime.tv_sec = seltime.tv_usec = 0; 635 seltime.tv_sec = seltime.tv_usec = 0;
636 636
637 r = xmalloc(read_wait_size); 637 r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
638 memcpy(r, read_wait, read_wait_size); 638 e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
639 e = xmalloc(read_wait_size); 639 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
640 memcpy(e, read_wait, read_wait_size); 640 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
641 641
642 while (select(maxfd, r, NULL, e, &seltime) == -1 && 642 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
643 (errno == EAGAIN || errno == EINTR)) 643 (errno == EAGAIN || errno == EINTR))
@@ -804,12 +804,10 @@ main(int argc, char **argv)
804 fatal("%s: not enough file descriptors", __progname); 804 fatal("%s: not enough file descriptors", __progname);
805 if (maxfd > fdlim_get(0)) 805 if (maxfd > fdlim_get(0))
806 fdlim_set(maxfd); 806 fdlim_set(maxfd);
807 fdcon = xmalloc(maxfd * sizeof(con)); 807 fdcon = xcalloc(maxfd, sizeof(con));
808 memset(fdcon, 0, maxfd * sizeof(con));
809 808
810 read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask); 809 read_wait_nfdset = howmany(maxfd, NFDBITS);
811 read_wait = xmalloc(read_wait_size); 810 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
812 memset(read_wait, 0, read_wait_size);
813 811
814 if (fopt_count) { 812 if (fopt_count) {
815 Linebuf *lb; 813 Linebuf *lb;
diff --git a/ssh.c b/ssh.c
index 0c950745b..f34be679c 100644
--- a/ssh.c
+++ b/ssh.c
@@ -687,7 +687,7 @@ main(int ac, char **av)
687 if (options.rhosts_rsa_authentication || 687 if (options.rhosts_rsa_authentication ||
688 options.hostbased_authentication) { 688 options.hostbased_authentication) {
689 sensitive_data.nkeys = 3; 689 sensitive_data.nkeys = 3;
690 sensitive_data.keys = xmalloc(sensitive_data.nkeys * 690 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
691 sizeof(Key)); 691 sizeof(Key));
692 692
693 PRIV_START; 693 PRIV_START;
@@ -1250,7 +1250,8 @@ env_permitted(char *env)
1250 int i; 1250 int i;
1251 char name[1024], *cp; 1251 char name[1024], *cp;
1252 1252
1253 strlcpy(name, env, sizeof(name)); 1253 if (strlcpy(name, env, sizeof(name)) >= sizeof(name))
1254 fatal("env_permitted: name too long");
1254 if ((cp = strchr(name, '=')) == NULL) 1255 if ((cp = strchr(name, '=')) == NULL)
1255 return (0); 1256 return (0);
1256 1257
diff --git a/sshconnect.c b/sshconnect.c
index 33961e4dc..8d4928a82 100644
--- a/sshconnect.c
+++ b/sshconnect.c
@@ -68,7 +68,6 @@ ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
68 int pin[2], pout[2]; 68 int pin[2], pout[2];
69 pid_t pid; 69 pid_t pid;
70 char strport[NI_MAXSERV]; 70 char strport[NI_MAXSERV];
71 size_t len;
72 71
73 /* Convert the port number into a string. */ 72 /* Convert the port number into a string. */
74 snprintf(strport, sizeof strport, "%hu", port); 73 snprintf(strport, sizeof strport, "%hu", port);
@@ -80,10 +79,7 @@ ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
80 * Use "exec" to avoid "sh -c" processes on some platforms 79 * Use "exec" to avoid "sh -c" processes on some platforms
81 * (e.g. Solaris) 80 * (e.g. Solaris)
82 */ 81 */
83 len = strlen(proxy_command) + 6; 82 xasprintf(&tmp, "exec %s", proxy_command);
84 tmp = xmalloc(len);
85 strlcpy(tmp, "exec ", len);
86 strlcat(tmp, proxy_command, len);
87 command_string = percent_expand(tmp, "h", host, 83 command_string = percent_expand(tmp, "h", host,
88 "p", strport, (char *)NULL); 84 "p", strport, (char *)NULL);
89 xfree(tmp); 85 xfree(tmp);
@@ -211,7 +207,7 @@ timeout_connect(int sockfd, const struct sockaddr *serv_addr,
211 fd_set *fdset; 207 fd_set *fdset;
212 struct timeval tv; 208 struct timeval tv;
213 socklen_t optlen; 209 socklen_t optlen;
214 int fdsetsz, optval, rc, result = -1; 210 int optval, rc, result = -1;
215 211
216 if (timeout <= 0) 212 if (timeout <= 0)
217 return (connect(sockfd, serv_addr, addrlen)); 213 return (connect(sockfd, serv_addr, addrlen));
@@ -225,10 +221,8 @@ timeout_connect(int sockfd, const struct sockaddr *serv_addr,
225 if (errno != EINPROGRESS) 221 if (errno != EINPROGRESS)
226 return (-1); 222 return (-1);
227 223
228 fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask); 224 fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
229 fdset = (fd_set *)xmalloc(fdsetsz); 225 sizeof(fd_mask));
230
231 memset(fdset, 0, fdsetsz);
232 FD_SET(sockfd, fdset); 226 FD_SET(sockfd, fdset);
233 tv.tv_sec = timeout; 227 tv.tv_sec = timeout;
234 tv.tv_usec = 0; 228 tv.tv_usec = 0;
@@ -957,8 +951,7 @@ ssh_put_password(char *password)
957 return; 951 return;
958 } 952 }
959 size = roundup(strlen(password) + 1, 32); 953 size = roundup(strlen(password) + 1, 32);
960 padded = xmalloc(size); 954 padded = xcalloc(1, size);
961 memset(padded, 0, size);
962 strlcpy(padded, password, size); 955 strlcpy(padded, password, size);
963 packet_put_string(padded, size); 956 packet_put_string(padded, size);
964 memset(padded, 0, size); 957 memset(padded, 0, size);
diff --git a/sshconnect2.c b/sshconnect2.c
index f8d21489e..c3501c2a5 100644
--- a/sshconnect2.c
+++ b/sshconnect2.c
@@ -1029,8 +1029,7 @@ pubkey_prepare(Authctxt *authctxt)
1029 if (key && key->type == KEY_RSA1) 1029 if (key && key->type == KEY_RSA1)
1030 continue; 1030 continue;
1031 options.identity_keys[i] = NULL; 1031 options.identity_keys[i] = NULL;
1032 id = xmalloc(sizeof(*id)); 1032 id = xcalloc(1, sizeof(*id));
1033 memset(id, 0, sizeof(*id));
1034 id->key = key; 1033 id->key = key;
1035 id->filename = xstrdup(options.identity_files[i]); 1034 id->filename = xstrdup(options.identity_files[i]);
1036 TAILQ_INSERT_TAIL(&files, id, next); 1035 TAILQ_INSERT_TAIL(&files, id, next);
@@ -1054,8 +1053,7 @@ pubkey_prepare(Authctxt *authctxt)
1054 } 1053 }
1055 } 1054 }
1056 if (!found && !options.identities_only) { 1055 if (!found && !options.identities_only) {
1057 id = xmalloc(sizeof(*id)); 1056 id = xcalloc(1, sizeof(*id));
1058 memset(id, 0, sizeof(*id));
1059 id->key = key; 1057 id->key = key;
1060 id->filename = comment; 1058 id->filename = comment;
1061 id->ac = ac; 1059 id->ac = ac;
@@ -1336,9 +1334,7 @@ userauth_hostbased(Authctxt *authctxt)
1336 return 0; 1334 return 0;
1337 } 1335 }
1338 len = strlen(p) + 2; 1336 len = strlen(p) + 2;
1339 chost = xmalloc(len); 1337 xasprintf(&chost, "%s.", p);
1340 strlcpy(chost, p, len);
1341 strlcat(chost, ".", len);
1342 debug2("userauth_hostbased: chost %s", chost); 1338 debug2("userauth_hostbased: chost %s", chost);
1343 xfree(p); 1339 xfree(p);
1344 1340
diff --git a/sshd.c b/sshd.c
index bb830161e..28e8c1aa3 100644
--- a/sshd.c
+++ b/sshd.c
@@ -891,7 +891,7 @@ main(int ac, char **av)
891{ 891{
892 extern char *optarg; 892 extern char *optarg;
893 extern int optind; 893 extern int optind;
894 int opt, j, i, fdsetsz, on = 1; 894 int opt, j, i, on = 1;
895 int sock_in = -1, sock_out = -1, newsock = -1; 895 int sock_in = -1, sock_out = -1, newsock = -1;
896 pid_t pid; 896 pid_t pid;
897 socklen_t fromlen; 897 socklen_t fromlen;
@@ -1110,7 +1110,7 @@ main(int ac, char **av)
1110 debug("sshd version %.100s", SSH_RELEASE); 1110 debug("sshd version %.100s", SSH_RELEASE);
1111 1111
1112 /* load private host keys */ 1112 /* load private host keys */
1113 sensitive_data.host_keys = xmalloc(options.num_host_key_files * 1113 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
1114 sizeof(Key *)); 1114 sizeof(Key *));
1115 for (i = 0; i < options.num_host_key_files; i++) 1115 for (i = 0; i < options.num_host_key_files; i++)
1116 sensitive_data.host_keys[i] = NULL; 1116 sensitive_data.host_keys[i] = NULL;
@@ -1212,7 +1212,7 @@ main(int ac, char **av)
1212 debug("setgroups() failed: %.200s", strerror(errno)); 1212 debug("setgroups() failed: %.200s", strerror(errno));
1213 1213
1214 if (rexec_flag) { 1214 if (rexec_flag) {
1215 rexec_argv = xmalloc(sizeof(char *) * (rexec_argc + 2)); 1215 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
1216 for (i = 0; i < rexec_argc; i++) { 1216 for (i = 0; i < rexec_argc; i++) {
1217 debug("rexec_argv[%d]='%s'", i, saved_argv[i]); 1217 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1218 rexec_argv[i] = saved_argv[i]; 1218 rexec_argv[i] = saved_argv[i];
@@ -1391,7 +1391,7 @@ main(int ac, char **av)
1391 if (listen_socks[i] > maxfd) 1391 if (listen_socks[i] > maxfd)
1392 maxfd = listen_socks[i]; 1392 maxfd = listen_socks[i];
1393 /* pipes connected to unauthenticated childs */ 1393 /* pipes connected to unauthenticated childs */
1394 startup_pipes = xmalloc(options.max_startups * sizeof(int)); 1394 startup_pipes = xcalloc(options.max_startups, sizeof(int));
1395 for (i = 0; i < options.max_startups; i++) 1395 for (i = 0; i < options.max_startups; i++)
1396 startup_pipes[i] = -1; 1396 startup_pipes[i] = -1;
1397 1397
@@ -1404,9 +1404,8 @@ main(int ac, char **av)
1404 sighup_restart(); 1404 sighup_restart();
1405 if (fdset != NULL) 1405 if (fdset != NULL)
1406 xfree(fdset); 1406 xfree(fdset);
1407 fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask); 1407 fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS),
1408 fdset = (fd_set *)xmalloc(fdsetsz); 1408 sizeof(fd_mask));
1409 memset(fdset, 0, fdsetsz);
1410 1409
1411 for (i = 0; i < num_listen_socks; i++) 1410 for (i = 0; i < num_listen_socks; i++)
1412 FD_SET(listen_socks[i], fdset); 1411 FD_SET(listen_socks[i], fdset);
@@ -1713,8 +1712,7 @@ main(int ac, char **av)
1713 packet_set_nonblocking(); 1712 packet_set_nonblocking();
1714 1713
1715 /* allocate authentication context */ 1714 /* allocate authentication context */
1716 authctxt = xmalloc(sizeof(*authctxt)); 1715 authctxt = xcalloc(1, sizeof(*authctxt));
1717 memset(authctxt, 0, sizeof(*authctxt));
1718 1716
1719 authctxt->loginmsg = &loginmsg; 1717 authctxt->loginmsg = &loginmsg;
1720 1718
diff --git a/uuencode.c b/uuencode.c
index 314eb92f3..feda6a016 100644
--- a/uuencode.c
+++ b/uuencode.c
@@ -57,9 +57,14 @@ uudecode(const char *src, u_char *target, size_t targsize)
57void 57void
58dump_base64(FILE *fp, u_char *data, u_int len) 58dump_base64(FILE *fp, u_char *data, u_int len)
59{ 59{
60 char *buf = xmalloc(2*len); 60 char *buf;;
61 int i, n; 61 int i, n;
62 62
63 if (len > 65536) {
64 fprintf(fp, "dump_base64: len > 65536\n");
65 return;
66 }
67 buf = xmalloc(2*len);
63 n = uuencode(data, len, buf, 2*len); 68 n = uuencode(data, len, buf, 2*len);
64 for (i = 0; i < n; i++) { 69 for (i = 0; i < n; i++) {
65 fprintf(fp, "%c", buf[i]); 70 fprintf(fp, "%c", buf[i]);
diff --git a/xmalloc.c b/xmalloc.c
index 64e439853..6d56781d9 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -31,6 +31,22 @@ xmalloc(size_t size)
31} 31}
32 32
33void * 33void *
34xcalloc(size_t nmemb, size_t size)
35{
36 void *ptr;
37
38 if (nmemb && size && SIZE_T_MAX / nmemb < size)
39 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
40 if (size == 0 || nmemb == 0)
41 fatal("xcalloc: zero size");
42 ptr = calloc(nmemb, size);
43 if (ptr == NULL)
44 fatal("xcalloc: out of memory (allocating %lu bytes)",
45 (u_long)(size * nmemb));
46 return ptr;
47}
48
49void *
34xrealloc(void *ptr, size_t new_size) 50xrealloc(void *ptr, size_t new_size)
35{ 51{
36 void *new_ptr; 52 void *new_ptr;
@@ -65,3 +81,19 @@ xstrdup(const char *str)
65 strlcpy(cp, str, len); 81 strlcpy(cp, str, len);
66 return cp; 82 return cp;
67} 83}
84
85int
86xasprintf(char **ret, const char *fmt, ...)
87{
88 va_list ap;
89 int i;
90
91 va_start(ap, fmt);
92 i = vasprintf(ret, fmt, ap);
93 va_end(ap);
94
95 if (i < 0 || *ret == NULL)
96 fatal("xasprintf: could not allocate memory");
97
98 return (i);
99}
diff --git a/xmalloc.h b/xmalloc.h
index 7ac4b13d6..b6d521a66 100644
--- a/xmalloc.h
+++ b/xmalloc.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: xmalloc.h,v 1.9 2002/06/19 00:27:55 deraadt Exp $ */ 1/* $OpenBSD: xmalloc.h,v 1.10 2006/03/25 00:05:41 djm Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -20,8 +20,12 @@
20#define XMALLOC_H 20#define XMALLOC_H
21 21
22void *xmalloc(size_t); 22void *xmalloc(size_t);
23void *xcalloc(size_t, size_t);
23void *xrealloc(void *, size_t); 24void *xrealloc(void *, size_t);
24void xfree(void *); 25void xfree(void *);
25char *xstrdup(const char *); 26char *xstrdup(const char *);
27int xasprintf(char **, const char *, ...)
28 __attribute__((__format__ (printf, 2, 3)))
29 __attribute__((__nonnull__ (2)));
26 30
27#endif /* XMALLOC_H */ 31#endif /* XMALLOC_H */