Skip to content

Commit

Permalink
upstream: change explicit_bzero();free() to freezero()
Browse files Browse the repository at this point in the history
While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@

OpenBSD-Commit-ID: 2660fa334fcc7cd05ec74dd99cb036f9ade6384a
  • Loading branch information
jonathangray authored and djmdjm committed Feb 28, 2020
1 parent 9e3220b commit d5ba1c0
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 149 deletions.
5 changes: 2 additions & 3 deletions auth-options.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: auth-options.c,v 1.90 2019/11/25 00:54:23 djm Exp $ */
/* $OpenBSD: auth-options.c,v 1.91 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2018 Damien Miller <[email protected]>
*
Expand Down Expand Up @@ -222,8 +222,7 @@ sshauthopt_free(struct sshauthopt *opts)
free(opts->permitlisten[i]);
free(opts->permitlisten);

explicit_bzero(opts, sizeof(*opts));
free(opts);
freezero(opts, sizeof(*opts));
}

struct sshauthopt *
Expand Down
5 changes: 2 additions & 3 deletions auth2-chall.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: auth2-chall.c,v 1.52 2019/11/13 04:47:52 deraadt Exp $ */
/* $OpenBSD: auth2-chall.c,v 1.53 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
* Copyright (c) 2001 Per Allansson. All rights reserved.
Expand Down Expand Up @@ -147,8 +147,7 @@ kbdint_free(KbdintAuthctxt *kbdintctxt)
if (kbdintctxt->device)
kbdint_reset_device(kbdintctxt);
free(kbdintctxt->devices);
explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
free(kbdintctxt);
freezero(kbdintctxt, sizeof(*kbdintctxt));
}
/* get next device */
static int
Expand Down
5 changes: 2 additions & 3 deletions auth2-passwd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: auth2-passwd.c,v 1.17 2019/09/06 04:53:27 djm Exp $ */
/* $OpenBSD: auth2-passwd.c,v 1.18 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
*
Expand Down Expand Up @@ -66,8 +66,7 @@ userauth_passwd(struct ssh *ssh)
logit("password change not supported");
else if (PRIVSEP(auth_password(ssh, password)) == 1)
authenticated = 1;
explicit_bzero(password, len);
free(password);
freezero(password, len);
return authenticated;
}

Expand Down
8 changes: 3 additions & 5 deletions authfd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: authfd.c,v 1.121 2019/12/21 02:19:13 djm Exp $ */
/* $OpenBSD: authfd.c,v 1.122 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -561,10 +561,8 @@ ssh_remove_identity(int sock, struct sshkey *key)
goto out;
r = decode_reply(type);
out:
if (blob != NULL) {
explicit_bzero(blob, blen);
free(blob);
}
if (blob != NULL)
freezero(blob, blen);
sshbuf_free(msg);
return r;
}
Expand Down
11 changes: 4 additions & 7 deletions channels.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: channels.c,v 1.395 2020/01/25 06:40:20 djm Exp $ */
/* $OpenBSD: channels.c,v 1.396 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -625,14 +625,12 @@ channel_free(struct ssh *ssh, Channel *c)
if (cc->abandon_cb != NULL)
cc->abandon_cb(ssh, c, cc->ctx);
TAILQ_REMOVE(&c->status_confirms, cc, entry);
explicit_bzero(cc, sizeof(*cc));
free(cc);
freezero(cc, sizeof(*cc));
}
if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
c->filter_cleanup(ssh, c->self, c->filter_ctx);
sc->channels[c->self] = NULL;
explicit_bzero(c, sizeof(*c));
free(c);
freezero(c, sizeof(*c));
}

void
Expand Down Expand Up @@ -3295,8 +3293,7 @@ channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
return 0;
cc->cb(ssh, type, c, cc->ctx);
TAILQ_REMOVE(&c->status_confirms, cc, entry);
explicit_bzero(cc, sizeof(*cc));
free(cc);
freezero(cc, sizeof(*cc));
return 0;
}

Expand Down
8 changes: 3 additions & 5 deletions cipher.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: cipher.c,v 1.114 2020/01/23 10:24:29 dtucker Exp $ */
/* $OpenBSD: cipher.c,v 1.115 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -328,8 +328,7 @@ cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
#ifdef WITH_OPENSSL
EVP_CIPHER_CTX_free(cc->evp);
#endif /* WITH_OPENSSL */
explicit_bzero(cc, sizeof(*cc));
free(cc);
freezero(cc, sizeof(*cc));
}
}
return ret;
Expand Down Expand Up @@ -434,8 +433,7 @@ cipher_free(struct sshcipher_ctx *cc)
EVP_CIPHER_CTX_free(cc->evp);
cc->evp = NULL;
#endif
explicit_bzero(cc, sizeof(*cc));
free(cc);
freezero(cc, sizeof(*cc));
}

/*
Expand Down
5 changes: 2 additions & 3 deletions clientloop.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: clientloop.c,v 1.341 2020/02/26 01:31:47 dtucker Exp $ */
/* $OpenBSD: clientloop.c,v 1.342 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -473,8 +473,7 @@ client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
gc->cb(ssh, type, seq, gc->ctx);
if (--gc->ref_count <= 0) {
TAILQ_REMOVE(&global_confirms, gc, entry);
explicit_bzero(gc, sizeof(*gc));
free(gc);
freezero(gc, sizeof(*gc));
}

ssh_packet_set_alive_timeouts(ssh, 0);
Expand Down
5 changes: 2 additions & 3 deletions digest-libc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: digest-libc.c,v 1.6 2017/05/08 22:57:38 djm Exp $ */
/* $OpenBSD: digest-libc.c,v 1.7 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2013 Damien Miller <[email protected]>
* Copyright (c) 2014 Markus Friedl. All rights reserved.
Expand Down Expand Up @@ -230,8 +230,7 @@ ssh_digest_free(struct ssh_digest_ctx *ctx)
if (digest) {
explicit_bzero(ctx->mdctx, digest->ctx_len);
free(ctx->mdctx);
explicit_bzero(ctx, sizeof(*ctx));
free(ctx);
freezero(ctx, sizeof(*ctx));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions hmac.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: hmac.c,v 1.13 2019/09/06 04:53:27 djm Exp $ */
/* $OpenBSD: hmac.c,v 1.14 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2014 Markus Friedl. All rights reserved.
*
Expand Down Expand Up @@ -131,8 +131,7 @@ ssh_hmac_free(struct ssh_hmac_ctx *ctx)
explicit_bzero(ctx->buf, ctx->buf_len);
free(ctx->buf);
}
explicit_bzero(ctx, sizeof(*ctx));
free(ctx);
freezero(ctx, sizeof(*ctx));
}
}

Expand Down
5 changes: 2 additions & 3 deletions hostfile.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: hostfile.c,v 1.77 2020/01/25 00:21:08 djm Exp $ */
/* $OpenBSD: hostfile.c,v 1.78 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -298,8 +298,7 @@ free_hostkeys(struct hostkeys *hostkeys)
explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
}
free(hostkeys->entries);
explicit_bzero(hostkeys, sizeof(*hostkeys));
free(hostkeys);
freezero(hostkeys, sizeof(*hostkeys));
}

static int
Expand Down
5 changes: 2 additions & 3 deletions kex.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: kex.c,v 1.156 2020/01/23 10:24:29 dtucker Exp $ */
/* $OpenBSD: kex.c,v 1.157 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
*
Expand Down Expand Up @@ -670,8 +670,7 @@ kex_free_newkeys(struct newkeys *newkeys)
}
free(newkeys->mac.name);
explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
explicit_bzero(newkeys, sizeof(*newkeys));
free(newkeys);
freezero(newkeys, sizeof(*newkeys));
}

void
Expand Down
5 changes: 2 additions & 3 deletions monitor.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: monitor.c,v 1.208 2020/02/06 22:30:54 naddy Exp $ */
/* $OpenBSD: monitor.c,v 1.209 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright 2002 Niels Provos <[email protected]>
* Copyright 2002 Markus Friedl <[email protected]>
Expand Down Expand Up @@ -893,8 +893,7 @@ mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
/* Only authenticate if the context is valid */
authenticated = options.password_authentication &&
auth_password(ssh, passwd);
explicit_bzero(passwd, plen);
free(passwd);
freezero(passwd, plen);

sshbuf_reset(m);
if ((r = sshbuf_put_u32(m, authenticated)) != 0)
Expand Down
11 changes: 4 additions & 7 deletions ssh-add.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh-add.c,v 1.153 2020/02/18 08:58:33 dtucker Exp $ */
/* $OpenBSD: ssh-add.c,v 1.154 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -105,8 +105,7 @@ static void
clear_pass(void)
{
if (pass) {
explicit_bzero(pass, strlen(pass));
free(pass);
freezero(pass, strlen(pass));
pass = NULL;
}
}
Expand Down Expand Up @@ -521,8 +520,7 @@ lock_agent(int agent_fd, int lock)
fprintf(stderr, "Passwords do not match.\n");
passok = 0;
}
explicit_bzero(p2, strlen(p2));
free(p2);
freezero(p2, strlen(p2));
}
if (passok) {
if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
Expand All @@ -533,8 +531,7 @@ lock_agent(int agent_fd, int lock)
lock ? "" : "un", ssh_err(r));
}
}
explicit_bzero(p1, strlen(p1));
free(p1);
freezero(p1, strlen(p1));
return (ret);
}

Expand Down
5 changes: 2 additions & 3 deletions ssh-agent.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh-agent.c,v 1.255 2020/02/06 22:30:54 naddy Exp $ */
/* $OpenBSD: ssh-agent.c,v 1.256 2020/02/26 13:40:09 jsg Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -620,8 +620,7 @@ process_lock_agent(SocketEntry *e, int lock)
fatal("bcrypt_pbkdf");
success = 1;
}
explicit_bzero(passwd, pwlen);
free(passwd);
freezero(passwd, pwlen);
send_status(e, success);
}

Expand Down
8 changes: 3 additions & 5 deletions ssh-dss.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh-dss.c,v 1.37 2018/02/07 02:06:51 jsing Exp $ */
/* $OpenBSD: ssh-dss.c,v 1.39 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
*
Expand Down Expand Up @@ -200,10 +200,8 @@ ssh_dss_verify(const struct sshkey *key,
BN_clear_free(sig_s);
sshbuf_free(b);
free(ktype);
if (sigblob != NULL) {
explicit_bzero(sigblob, len);
free(sigblob);
}
if (sigblob != NULL)
freezero(sigblob, len);
return ret;
}
#endif /* WITH_OPENSSL */
8 changes: 3 additions & 5 deletions ssh-ed25519-sk.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh-ed25519-sk.c,v 1.4 2019/11/26 03:04:27 djm Exp $ */
/* $OpenBSD: ssh-ed25519-sk.c,v 1.5 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2019 Markus Friedl. All rights reserved.
*
Expand Down Expand Up @@ -154,10 +154,8 @@ ssh_ed25519_sk_verify(const struct sshkey *key,
details = NULL;
}
out:
if (m != NULL) {
explicit_bzero(m, smlen); /* NB mlen may be invalid if r != 0 */
free(m);
}
if (m != NULL)
freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
sshkey_sig_details_free(details);
sshbuf_free(b);
sshbuf_free(encoded);
Expand Down
20 changes: 7 additions & 13 deletions ssh-ed25519.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh-ed25519.c,v 1.7 2016/04/21 06:08:02 djm Exp $ */
/* $OpenBSD: ssh-ed25519.c,v 1.8 2020/02/26 13:40:09 jsg Exp $ */
/*
* Copyright (c) 2013 Markus Friedl <[email protected]>
*
Expand Down Expand Up @@ -83,10 +83,8 @@ ssh_ed25519_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
r = 0;
out:
sshbuf_free(b);
if (sig != NULL) {
explicit_bzero(sig, slen);
free(sig);
}
if (sig != NULL)
freezero(sig, slen);

return r;
}
Expand Down Expand Up @@ -153,14 +151,10 @@ ssh_ed25519_verify(const struct sshkey *key,
/* success */
r = 0;
out:
if (sm != NULL) {
explicit_bzero(sm, smlen);
free(sm);
}
if (m != NULL) {
explicit_bzero(m, smlen); /* NB mlen may be invalid if r != 0 */
free(m);
}
if (sm != NULL)
freezero(sm, smlen);
if (m != NULL)
freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
sshbuf_free(b);
free(ktype);
return r;
Expand Down
Loading

0 comments on commit d5ba1c0

Please sign in to comment.