--- sys/src/libsec/port/tlshand.c
+++ sys/src/libsec/port/tlshand.c
@@ -1412,6 +1412,10 @@ msgSend(TlsConnection *c, Msg *m, int act)
if(sendp == nil)
sendp = sendbuf;
p = sendp;
+ if(p + 4 - sendbuf > sizeof(sendbuf)) {
+ tlsError(c, EInternalError, "output buffer too small for handshake header");
+ goto Err;
+ }
if(c->trace)
c->trace("send %s", msgPrint((char*)p, (sizeof sendbuf) - (p-sendbuf), m));
@@ -1423,6 +1427,15 @@ msgSend(TlsConnection *c, Msg *m, int act)
tlsError(c, EInternalError, "can't encode a %d", m->tag);
goto Err;
case HClientHello:
+ nn = 2 + RandomSize + 1 + m->u.clientHello.sid->len;
+ nn += 2 + 2*m->u.clientHello.ciphers->len;
+ nn += 1 + m->u.clientHello.compressors->len;
+ if(m->u.clientHello.extensions != nil)
+ nn += 2 + m->u.clientHello.extensions->len;
+ if(p + nn - sendbuf > sizeof(sendbuf)) {
+ tlsError(c, EInternalError, "output buffer too small for client hello");
+ goto Err;
+ }
// version
put16(p, m->u.clientHello.version);
p += 2;
@@ -1463,6 +1476,13 @@ msgSend(TlsConnection *c, Msg *m, int act)
}
break;
case HServerHello:
+ nn = 2 + RandomSize + 1 + m->u.serverHello.sid->len + 2 + 1;
+ if(m->u.serverHello.extensions != nil)
+ nn += 2 + m->u.serverHello.extensions->len;
+ if(p + nn - sendbuf > sizeof(sendbuf)) {
+ tlsError(c, EInternalError, "output buffer too small for server hello");
+ goto Err;
+ }
put16(p, m->u.serverHello.version);
p += 2;
@@ -1562,6 +1582,10 @@ msgSend(TlsConnection *c, Msg *m, int act)
p += n;
break;
case HFinished:
+ if(p + m->u.finished.n - sendbuf > sizeof(sendbuf)) {
+ tlsError(c, EInternalError, "output buffer too small for finished");
+ goto Err;
+ }
memmove(p, m->u.finished.verify, m->u.finished.n);
p += m->u.finished.n;
break;
@@ -1598,6 +1622,12 @@ tlsReadN(TlsConnection *c, int n)
uchar *p;
int nn, nr;
+ /* sizeof is signed, so test n < 0 separately */
+ if(n < 0 || n > sizeof(c->buf)) {
+ tlsError(c, EDecodeError, "handshake message too long %d %d", n, sizeof(c->buf));
+ return nil;
+ }
+
nn = c->ep - c->rp;
if(nn < n){
if(c->rp != c->buf){
@@ -1636,11 +1666,6 @@ msgRecv(TlsConnection *c, Msg *m)
tlsError(c, EDecodeError, "invalid hello request during handshake");
return 0;
}
- }
-
- if(n > sizeof(c->buf)) {
- tlsError(c, EDecodeError, "handshake message too long %d %d", n, sizeof(c->buf));
- return 0;
}
if(type == HSSL2ClientHello){
|