merge fixes from PuTTY import.c

toint() from misc.c

(revids are from hggit conversion)

changeset:   4620:60a336a6c85c
user:        Simon Tatham <anakin@pobox.com>
date:        Thu Feb 25 20:26:33 2016 +0000
files:       import.c
description:
Fix potential segfaults in reading OpenSSH's ASN.1 key format.

The length coming back from ber_read_id_len might have overflowed, so
treat it as potentially negative. Also, while I'm here, accumulate it
inside ber_read_id_len as an unsigned, so as to avoid undefined
behaviour on integer overflow, and toint() it before return.

Thanks to Hanno Böck for spotting this, with the aid of AFL.

(cherry picked from commit 5b7833cd474a24ec098654dcba8cb9509f3bf2c1)

Conflicts:
	import.c

(cherry-picker's note: resolving the conflict involved removing an
entire section of the original commit which fixed ECDSA code not
present on this branch)


changeset:   4619:9c6c638d98d8
user:        Simon Tatham <anakin@pobox.com>
date:        Sun Jul 14 10:45:54 2013 +0000
files:       import.c ssh.c sshdss.c sshpubk.c sshrsa.c
description:
Tighten up a lot of casts from unsigned to int which are read by one
of the GET_32BIT macros and then used as length fields. Missing bounds
checks against zero have been added, and also I've introduced a helper
function toint() which casts from unsigned to int in such a way as to
avoid C undefined behaviour, since I'm not sure I trust compilers any
more to do the obviously sensible thing.

[originally from svn r9918]


changeset:   4618:3957829f24d3
user:        Simon Tatham <anakin@pobox.com>
date:        Mon Jul 08 22:36:04 2013 +0000
files:       import.c sshdss.c sshrsa.c
description:
Add an assortment of extra safety checks.

[originally from svn r9896]


changeset:   4617:2cddee0bce12
user:        Jacob Nevins <jacobn@chiark.greenend.org.uk>
date:        Wed Dec 07 00:24:45 2005 +0000
files:       import.c
description:
Institutional failure to memset() things pointed at rather than pointers.
Things should now be zeroed and memory not leaked. Spotted by Brant Thomsen.

[originally from svn r6476]


changeset:   4616:24ac78a9c71d
user:        Simon Tatham <anakin@pobox.com>
date:        Wed Feb 11 13:58:27 2004 +0000
files:       import.c
description:
Jacob's last-minute testing found a couple of trivial bugs in
import.c, and my attempts to reproduce them in cmdgen found another
one there :-)

[originally from svn r3847]


changeset:   4615:088d39a73db0
user:        Simon Tatham <anakin@pobox.com>
date:        Thu Jan 22 18:52:49 2004 +0000
files:       import.c
description:
Placate some gcc warnings.

[originally from svn r3761]


changeset:   4614:e4288bad4d93
parent:      1758:108b8924593d
user:        Simon Tatham <anakin@pobox.com>
date:        Fri Oct 03 21:21:23 2003 +0000
files:       import.c
description:
My ASN.1 decoder returned wrong IDs for anything above 0x1E! Good
job it's never had to yet. Ahem.

[originally from svn r3479]
This commit is contained in:
Matt Johnston 2016-07-12 23:00:01 +08:00
parent c0f63ee100
commit 3ee9656250

View File

@ -60,6 +60,8 @@ static int openssh_write(const char *filename, sign_key *key,
static int dropbear_write(const char*filename, sign_key * key); static int dropbear_write(const char*filename, sign_key * key);
static sign_key *dropbear_read(const char* filename); static sign_key *dropbear_read(const char* filename);
static int toint(unsigned u);
#if 0 #if 0
static int sshcom_encrypted(const char *filename, char **comment); static int sshcom_encrypted(const char *filename, char **comment);
static struct ssh2_userkey *sshcom_read(const char *filename, char *passphrase); static struct ssh2_userkey *sshcom_read(const char *filename, char *passphrase);
@ -241,12 +243,11 @@ static int ber_read_id_len(void *source, int sourcelen,
if ((*p & 0x1F) == 0x1F) { if ((*p & 0x1F) == 0x1F) {
*id = 0; *id = 0;
while (*p & 0x80) { while (*p & 0x80) {
*id = (*id << 7) | (*p & 0x7F);
p++, sourcelen--; p++, sourcelen--;
if (sourcelen == 0) if (sourcelen == 0)
return -1; return -1;
}
*id = (*id << 7) | (*p & 0x7F); *id = (*id << 7) | (*p & 0x7F);
}
p++, sourcelen--; p++, sourcelen--;
} else { } else {
*id = *p & 0x1F; *id = *p & 0x1F;
@ -257,14 +258,16 @@ static int ber_read_id_len(void *source, int sourcelen,
return -1; return -1;
if (*p & 0x80) { if (*p & 0x80) {
unsigned len;
int n = *p & 0x7F; int n = *p & 0x7F;
p++, sourcelen--; p++, sourcelen--;
if (sourcelen < n) if (sourcelen < n)
return -1; return -1;
*length = 0; len = 0;
while (n--) while (n--)
*length = (*length << 8) | (*p++); len = (len << 8) | (*p++);
sourcelen -= n; sourcelen -= n;
*length = toint(len);
} else { } else {
*length = *p; *length = *p;
p++, sourcelen--; p++, sourcelen--;
@ -582,7 +585,8 @@ static sign_key *openssh_read(const char *filename, char * UNUSED(passphrase))
/* Expect the SEQUENCE header. Take its absence as a failure to decrypt. */ /* Expect the SEQUENCE header. Take its absence as a failure to decrypt. */
ret = ber_read_id_len(p, key->keyblob_len, &id, &len, &flags); ret = ber_read_id_len(p, key->keyblob_len, &id, &len, &flags);
p += ret; p += ret;
if (ret < 0 || id != 16) { if (ret < 0 || id != 16 || len < 0 ||
key->keyblob+key->keyblob_len-p < len) {
errmsg = "ASN.1 decoding failure - wrong password?"; errmsg = "ASN.1 decoding failure - wrong password?";
goto error; goto error;
} }
@ -617,7 +621,7 @@ static sign_key *openssh_read(const char *filename, char * UNUSED(passphrase))
ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p, ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
&id, &len, &flags); &id, &len, &flags);
p += ret; p += ret;
if (ret < 0 || id != 2 || if (ret < 0 || id != 2 || len < 0 ||
key->keyblob+key->keyblob_len-p < len) { key->keyblob+key->keyblob_len-p < len) {
errmsg = "ASN.1 decoding failure"; errmsg = "ASN.1 decoding failure";
goto error; goto error;
@ -1379,7 +1383,7 @@ static struct sshcom_key *load_sshcom_key(const char *filename)
memset(ret->keyblob, 0, ret->keyblob_size); memset(ret->keyblob, 0, ret->keyblob_size);
m_free(ret->keyblob); m_free(ret->keyblob);
} }
memset(&ret, 0, sizeof(ret)); memset(ret, 0, sizeof(*ret));
m_free(ret); m_free(ret);
} }
return NULL; return NULL;
@ -1407,11 +1411,12 @@ int sshcom_encrypted(const char *filename, char **comment)
pos = 8; pos = 8;
if (key->keyblob_len < pos+4) if (key->keyblob_len < pos+4)
goto done; /* key is far too short */ goto done; /* key is far too short */
pos += 4 + GET_32BIT(key->keyblob + pos); /* skip key type */ len = toint(GET_32BIT(key->keyblob + pos));
if (key->keyblob_len < pos+4) if (len < 0 || len > key->keyblob_len - pos - 4)
goto done; /* key is far too short */ goto done; /* key is far too short */
len = GET_32BIT(key->keyblob + pos); /* find cipher-type length */ pos += 4 + len; /* skip key type */
if (key->keyblob_len < pos+4+len) len = toint(GET_32BIT(key->keyblob + pos)); /* find cipher-type length */
if (len < 0 || len > key->keyblob_len - pos - 4)
goto done; /* cipher type string is incomplete */ goto done; /* cipher type string is incomplete */
if (len != 4 || 0 != memcmp(key->keyblob + pos + 4, "none", 4)) if (len != 4 || 0 != memcmp(key->keyblob + pos + 4, "none", 4))
answer = 1; answer = 1;
@ -1420,15 +1425,14 @@ int sshcom_encrypted(const char *filename, char **comment)
*comment = dupstr(key->comment); *comment = dupstr(key->comment);
memset(key->keyblob, 0, key->keyblob_size); memset(key->keyblob, 0, key->keyblob_size);
m_free(key->keyblob); m_free(key->keyblob);
memset(&key, 0, sizeof(key)); memset(key, 0, sizeof(*key));
m_free(key); m_free(key);
return answer; return answer;
} }
static int sshcom_read_mpint(void *data, int len, struct mpint_pos *ret) static int sshcom_read_mpint(void *data, int len, struct mpint_pos *ret)
{ {
int bits; unsigned bits, bytes;
int bytes;
unsigned char *d = (unsigned char *) data; unsigned char *d = (unsigned char *) data;
if (len < 4) if (len < 4)
@ -1481,7 +1485,7 @@ sign_key *sshcom_read(const char *filename, char *passphrase)
struct ssh2_userkey *ret = NULL, *retkey; struct ssh2_userkey *ret = NULL, *retkey;
const struct ssh_signkey *alg; const struct ssh_signkey *alg;
unsigned char *blob = NULL; unsigned char *blob = NULL;
int blobsize, publen, privlen; int blobsize = 0, publen, privlen;
if (!key) if (!key)
return NULL; return NULL;
@ -1601,8 +1605,8 @@ sign_key *sshcom_read(const char *filename, char *passphrase)
/* /*
* Strip away the containing string to get to the real meat. * Strip away the containing string to get to the real meat.
*/ */
len = GET_32BIT(ciphertext); len = toint(GET_32BIT(ciphertext));
if (len > cipherlen-4) { if (len < 0 || len > cipherlen-4) {
errmsg = "containing string was ill-formed"; errmsg = "containing string was ill-formed";
goto error; goto error;
} }
@ -1669,7 +1673,8 @@ sign_key *sshcom_read(const char *filename, char *passphrase)
publen = pos; publen = pos;
pos += put_mp(blob+pos, x.start, x.bytes); pos += put_mp(blob+pos, x.start, x.bytes);
privlen = pos - publen; privlen = pos - publen;
} } else
return NULL;
dropbear_assert(privlen > 0); /* should have bombed by now if not */ dropbear_assert(privlen > 0); /* should have bombed by now if not */
@ -1693,7 +1698,7 @@ sign_key *sshcom_read(const char *filename, char *passphrase)
} }
memset(key->keyblob, 0, key->keyblob_size); memset(key->keyblob, 0, key->keyblob_size);
m_free(key->keyblob); m_free(key->keyblob);
memset(&key, 0, sizeof(key)); memset(key, 0, sizeof(*key));
m_free(key); m_free(key);
return ret; return ret;
} }
@ -1906,3 +1911,27 @@ int sshcom_write(const char *filename, sign_key *key,
return ret; return ret;
} }
#endif /* ssh.com stuff disabled */ #endif /* ssh.com stuff disabled */
/* From PuTTY misc.c */
static int toint(unsigned u)
{
/*
* Convert an unsigned to an int, without running into the
* undefined behaviour which happens by the strict C standard if
* the value overflows. You'd hope that sensible compilers would
* do the sensible thing in response to a cast, but actually I
* don't trust modern compilers not to do silly things like
* assuming that _obviously_ you wouldn't have caused an overflow
* and so they can elide an 'if (i < 0)' test immediately after
* the cast.
*
* Sensible compilers ought of course to optimise this entire
* function into 'just return the input value'!
*/
if (u <= (unsigned)INT_MAX)
return (int)u;
else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
return INT_MIN + (int)(u - (unsigned)INT_MIN);
else
return INT_MIN; /* fallback; should never occur on binary machines */
}