Blame SOURCES/libvncserver-0.9.11-CVE-2017-18922.patch

9ca2f2
Backport of:
9ca2f2
From aac95a9dcf4bbba87b76c72706c3221a842ca433 Mon Sep 17 00:00:00 2001
9ca2f2
From: Andreas Weigel <andreaswe@securepoint.de>
9ca2f2
Date: Wed, 15 Feb 2017 12:31:05 +0100
9ca2f2
Subject: [PATCH] fix overflow and refactor websockets decode (Hybi)
9ca2f2
9ca2f2
fix critical heap-based buffer overflow which allowed easy modification
9ca2f2
of a return address via an overwritten function pointer
9ca2f2
9ca2f2
fix bug causing connections to fail due a "one websocket frame = one
9ca2f2
ws_read" assumption, which failed with LibVNCServer-0.9.11
9ca2f2
9ca2f2
refactor websocket Hybi decode to use a simple state machine for
9ca2f2
decoding of websocket frames
9ca2f2
9ca2f2
[Ubuntu note: Renamed b64_pton to __b64_pton in patch to ensure patch can be
9ca2f2
applied.
9ca2f2
 -- Avital]
9ca2f2
9ca2f2
---
9ca2f2
 libvncserver/websockets.c | 595 +++++++++++++++++++++++++++++---------
9ca2f2
 1 file changed, 463 insertions(+), 132 deletions(-)
9ca2f2
9ca2f2
--- a/libvncserver/websockets.c
9ca2f2
+++ b/libvncserver/websockets.c
9ca2f2
@@ -62,6 +62,9 @@
9ca2f2
 
9ca2f2
 #define B64LEN(__x) (((__x + 2) / 3) * 12 / 3)
9ca2f2
 #define WSHLENMAX 14  /* 2 + sizeof(uint64_t) + sizeof(uint32_t) */
9ca2f2
+#define WS_HYBI_MASK_LEN 4
9ca2f2
+
9ca2f2
+#define ARRAYSIZE(a) ((sizeof(a) / sizeof((a[0]))) / (size_t)(!(sizeof(a) % sizeof((a[0])))))
9ca2f2
 
9ca2f2
 enum {
9ca2f2
   WEBSOCKETS_VERSION_HIXIE,
9ca2f2
@@ -78,20 +81,20 @@ static int gettid() {
9ca2f2
 typedef int (*wsEncodeFunc)(rfbClientPtr cl, const char *src, int len, char **dst);
9ca2f2
 typedef int (*wsDecodeFunc)(rfbClientPtr cl, char *dst, int len);
9ca2f2
 
9ca2f2
-typedef struct ws_ctx_s {
9ca2f2
-    char codeBufDecode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
9ca2f2
-	char codeBufEncode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
9ca2f2
-	char readbuf[8192];
9ca2f2
-    int readbufstart;
9ca2f2
-    int readbuflen;
9ca2f2
-    int dblen;
9ca2f2
-    char carryBuf[3];                      /* For base64 carry-over */
9ca2f2
-    int carrylen;
9ca2f2
-    int version;
9ca2f2
-    int base64;
9ca2f2
-    wsEncodeFunc encode;
9ca2f2
-    wsDecodeFunc decode;
9ca2f2
-} ws_ctx_t;
9ca2f2
+
9ca2f2
+enum {
9ca2f2
+  /* header not yet received completely */
9ca2f2
+  WS_HYBI_STATE_HEADER_PENDING,
9ca2f2
+  /* data available */
9ca2f2
+  WS_HYBI_STATE_DATA_AVAILABLE,
9ca2f2
+  WS_HYBI_STATE_DATA_NEEDED,
9ca2f2
+  /* received a complete frame */
9ca2f2
+  WS_HYBI_STATE_FRAME_COMPLETE,
9ca2f2
+  /* received part of a 'close' frame */
9ca2f2
+  WS_HYBI_STATE_CLOSE_REASON_PENDING,
9ca2f2
+  /* */
9ca2f2
+  WS_HYBI_STATE_ERR
9ca2f2
+};
9ca2f2
 
9ca2f2
 typedef union ws_mask_s {
9ca2f2
   char c[4];
9ca2f2
@@ -119,6 +122,38 @@ typedef struct __attribute__ ((__packed_
9ca2f2
   } u;
9ca2f2
 } ws_header_t;
9ca2f2
 
9ca2f2
+typedef struct ws_header_data_s {
9ca2f2
+  ws_header_t *data;
9ca2f2
+  /** bytes read */
9ca2f2
+  int nRead;
9ca2f2
+  /** mask value */
9ca2f2
+  ws_mask_t mask;
9ca2f2
+  /** length of frame header including payload len, but without mask */
9ca2f2
+  int headerLen;
9ca2f2
+  /** length of the payload data */
9ca2f2
+  int payloadLen;
9ca2f2
+  /** opcode */
9ca2f2
+  unsigned char opcode;
9ca2f2
+} ws_header_data_t;
9ca2f2
+
9ca2f2
+typedef struct ws_ctx_s {
9ca2f2
+    char codeBufDecode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
9ca2f2
+    char codeBufEncode[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
9ca2f2
+    char *writePos;
9ca2f2
+    unsigned char *readPos;
9ca2f2
+    int readlen;
9ca2f2
+    int hybiDecodeState;
9ca2f2
+    char carryBuf[3];                      /* For base64 carry-over */
9ca2f2
+    int carrylen;
9ca2f2
+    int version;
9ca2f2
+    int base64;
9ca2f2
+    ws_header_data_t header;
9ca2f2
+    int nReadRaw;
9ca2f2
+    int nToRead;
9ca2f2
+    wsEncodeFunc encode;
9ca2f2
+    wsDecodeFunc decode;
9ca2f2
+} ws_ctx_t;
9ca2f2
+
9ca2f2
 enum
9ca2f2
 {
9ca2f2
     WS_OPCODE_CONTINUATION = 0x0,
9ca2f2
@@ -179,6 +214,8 @@ static int webSocketsEncodeHixie(rfbClie
9ca2f2
 static int webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len);
9ca2f2
 static int webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len);
9ca2f2
 
9ca2f2
+static void hybiDecodeCleanup(ws_ctx_t *wsctx);
9ca2f2
+
9ca2f2
 static int
9ca2f2
 min (int a, int b) {
9ca2f2
     return a < b ? a : b;
9ca2f2
@@ -440,10 +477,11 @@ webSocketsHandshake(rfbClientPtr cl, cha
9ca2f2
 	wsctx->decode = webSocketsDecodeHixie;
9ca2f2
     }
9ca2f2
     wsctx->base64 = base64;
9ca2f2
+    hybiDecodeCleanup(wsctx);
9ca2f2
     cl->wsctx = (wsCtx *)wsctx;
9ca2f2
     return TRUE;
9ca2f2
 }
9ca2f2
- 
9ca2f2
+
9ca2f2
 void
9ca2f2
 webSocketsGenMd5(char * target, char *key1, char *key2, char *key3)
9ca2f2
 {
9ca2f2
@@ -635,146 +673,439 @@ webSocketsDecodeHixie(rfbClientPtr cl, c
9ca2f2
 }
9ca2f2
 
9ca2f2
 static int
9ca2f2
-webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len)
9ca2f2
+hybiRemaining(ws_ctx_t *wsctx)
9ca2f2
 {
9ca2f2
-    char *buf, *payload;
9ca2f2
-    uint32_t *payload32;
9ca2f2
-    int ret = -1, result = -1;
9ca2f2
-    int total = 0;
9ca2f2
-    ws_mask_t mask;
9ca2f2
-    ws_header_t *header;
9ca2f2
-    int i;
9ca2f2
-    unsigned char opcode;
9ca2f2
-    ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
9ca2f2
-    int flength, fhlen;
9ca2f2
-    /* int fin; */ /* not used atm */ 
9ca2f2
+  return wsctx->nToRead - wsctx->nReadRaw;
9ca2f2
+}
9ca2f2
 
9ca2f2
-    /* rfbLog(" <== %s[%d]: %d cl: %p, wsctx: %p-%p (%d)\n", __func__, gettid(), len, cl, wsctx, (char *)wsctx + sizeof(ws_ctx_t), sizeof(ws_ctx_t)); */
9ca2f2
+static void
9ca2f2
+hybiDecodeCleanup(ws_ctx_t *wsctx)
9ca2f2
+{
9ca2f2
+  wsctx->header.payloadLen = 0;
9ca2f2
+  wsctx->header.mask.u = 0;
9ca2f2
+  wsctx->nReadRaw = 0;
9ca2f2
+  wsctx->nToRead= 0;
9ca2f2
+  wsctx->carrylen = 0;
9ca2f2
+  wsctx->readPos = (unsigned char *)wsctx->codeBufDecode;
9ca2f2
+  wsctx->readlen = 0;
9ca2f2
+  wsctx->hybiDecodeState = WS_HYBI_STATE_HEADER_PENDING;
9ca2f2
+  wsctx->writePos = NULL;
9ca2f2
+  rfbLog("cleaned up wsctx\n");
9ca2f2
+}
9ca2f2
 
9ca2f2
-    if (wsctx->readbuflen) {
9ca2f2
-      /* simply return what we have */
9ca2f2
-      if (wsctx->readbuflen > len) {
9ca2f2
-	memcpy(dst, wsctx->readbuf +  wsctx->readbufstart, len);
9ca2f2
-	result = len;
9ca2f2
-	wsctx->readbuflen -= len;
9ca2f2
-	wsctx->readbufstart += len;
9ca2f2
+/**
9ca2f2
+ * Return payload data that has been decoded/unmasked from
9ca2f2
+ * a websocket frame.
9ca2f2
+ *
9ca2f2
+ * @param[out]     dst destination buffer
9ca2f2
+ * @param[in]      len bytes to copy to destination buffer
9ca2f2
+ * @param[in,out]  wsctx internal state of decoding procedure
9ca2f2
+ * @param[out]     number of bytes actually written to dst buffer
9ca2f2
+ * @return next hybi decoding state
9ca2f2
+ */
9ca2f2
+static int
9ca2f2
+hybiReturnData(char *dst, int len, ws_ctx_t *wsctx, int *nWritten)
9ca2f2
+{
9ca2f2
+  int nextState = WS_HYBI_STATE_ERR;
9ca2f2
+
9ca2f2
+  /* if we have something already decoded copy and return */
9ca2f2
+  if (wsctx->readlen > 0) {
9ca2f2
+    /* simply return what we have */
9ca2f2
+    if (wsctx->readlen > len) {
9ca2f2
+      rfbLog("copy to %d bytes to dst buffer; readPos=%p, readLen=%d\n", len, wsctx->readPos, wsctx->readlen);
9ca2f2
+      memcpy(dst, wsctx->readPos, len);
9ca2f2
+      *nWritten = len;
9ca2f2
+      wsctx->readlen -= len;
9ca2f2
+      wsctx->readPos += len;
9ca2f2
+      nextState = WS_HYBI_STATE_DATA_AVAILABLE;
9ca2f2
+    } else {
9ca2f2
+      rfbLog("copy to %d bytes to dst buffer; readPos=%p, readLen=%d\n", wsctx->readlen, wsctx->readPos, wsctx->readlen);
9ca2f2
+      memcpy(dst, wsctx->readPos, wsctx->readlen);
9ca2f2
+      *nWritten = wsctx->readlen;
9ca2f2
+      wsctx->readlen = 0;
9ca2f2
+      wsctx->readPos = NULL;
9ca2f2
+      if (hybiRemaining(wsctx) == 0) {
9ca2f2
+        nextState = WS_HYBI_STATE_FRAME_COMPLETE;
9ca2f2
       } else {
9ca2f2
-	memcpy(dst, wsctx->readbuf +  wsctx->readbufstart, wsctx->readbuflen);
9ca2f2
-	result = wsctx->readbuflen;
9ca2f2
-	wsctx->readbuflen = 0;
9ca2f2
-	wsctx->readbufstart = 0;
9ca2f2
+        nextState = WS_HYBI_STATE_DATA_NEEDED;
9ca2f2
       }
9ca2f2
-      goto spor;
9ca2f2
     }
9ca2f2
+    rfbLog("after copy: readPos=%p, readLen=%d\n", wsctx->readPos, wsctx->readlen);
9ca2f2
+  } else if (wsctx->hybiDecodeState == WS_HYBI_STATE_CLOSE_REASON_PENDING) {
9ca2f2
+    nextState = WS_HYBI_STATE_CLOSE_REASON_PENDING;
9ca2f2
+  }
9ca2f2
+  return nextState;
9ca2f2
+}
9ca2f2
 
9ca2f2
-    buf = wsctx->codeBufDecode;
9ca2f2
-    header = (ws_header_t *)wsctx->codeBufDecode;
9ca2f2
+/**
9ca2f2
+ * Read an RFC 6455 websocket frame (IETF hybi working group).
9ca2f2
+ *
9ca2f2
+ * Internal state is updated according to bytes received and the
9ca2f2
+ * decoding of header information.
9ca2f2
+ *
9ca2f2
+ * @param[in]   cl client ptr with ptr to raw socket and ws_ctx_t ptr
9ca2f2
+ * @param[out]  sockRet emulated recv return value
9ca2f2
+ * @return next hybi decoding state; WS_HYBI_STATE_HEADER_PENDING indicates
9ca2f2
+ *         that the header was not received completely.
9ca2f2
+ */
9ca2f2
+static int
9ca2f2
+hybiReadHeader(rfbClientPtr cl, int *sockRet)
9ca2f2
+{
9ca2f2
+  int ret;
9ca2f2
+  ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
9ca2f2
+  char *headerDst = wsctx->codeBufDecode + wsctx->nReadRaw;
9ca2f2
+  int n = WSHLENMAX - wsctx->nReadRaw;
9ca2f2
+
9ca2f2
+  rfbLog("header_read to %p with len=%d\n", headerDst, n);
9ca2f2
+  ret = ws_read(cl, headerDst, n);
9ca2f2
+  rfbLog("read %d bytes from socket\n", ret);
9ca2f2
+  if (ret <= 0) {
9ca2f2
+    if (-1 == ret) {
9ca2f2
+      /* save errno because rfbErr() will tamper it */
9ca2f2
+      int olderrno = errno;
9ca2f2
+      rfbErr("%s: peek; %m\n", __func__);
9ca2f2
+      errno = olderrno;
9ca2f2
+      *sockRet = -1;
9ca2f2
+    } else {
9ca2f2
+      *sockRet = 0;
9ca2f2
+    }
9ca2f2
+    return WS_HYBI_STATE_ERR;
9ca2f2
+  }
9ca2f2
 
9ca2f2
-    ret = ws_peek(cl, buf, B64LEN(len) + WSHLENMAX);
9ca2f2
+  wsctx->nReadRaw += ret;
9ca2f2
+  if (wsctx->nReadRaw < 2) {
9ca2f2
+    /* cannot decode header with less than two bytes */
9ca2f2
+    errno = EAGAIN;
9ca2f2
+    *sockRet = -1;
9ca2f2
+    return WS_HYBI_STATE_HEADER_PENDING;
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  /* first two header bytes received; interpret header data and get rest */
9ca2f2
+  wsctx->header.data = (ws_header_t *)wsctx->codeBufDecode;
9ca2f2
+
9ca2f2
+  wsctx->header.opcode = wsctx->header.data->b0 & 0x0f;
9ca2f2
+
9ca2f2
+  /* fin = (header->b0 & 0x80) >> 7; */ /* not used atm */
9ca2f2
+  wsctx->header.payloadLen = wsctx->header.data->b1 & 0x7f;
9ca2f2
+  rfbLog("first header bytes received; opcode=%d lenbyte=%d\n", wsctx->header.opcode, wsctx->header.payloadLen);
9ca2f2
+
9ca2f2
+  /*
9ca2f2
+   * 4.3. Client-to-Server Masking
9ca2f2
+   *
9ca2f2
+   * The client MUST mask all frames sent to the server.  A server MUST
9ca2f2
+   * close the connection upon receiving a frame with the MASK bit set to 0.
9ca2f2
+  **/
9ca2f2
+  if (!(wsctx->header.data->b1 & 0x80)) {
9ca2f2
+    rfbErr("%s: got frame without mask ret=%d\n", __func__, ret);
9ca2f2
+    errno = EIO;
9ca2f2
+    *sockRet = -1;
9ca2f2
+    return WS_HYBI_STATE_ERR;
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  if (wsctx->header.payloadLen < 126 && wsctx->nReadRaw >= 6) {
9ca2f2
+    wsctx->header.headerLen = 2 + WS_HYBI_MASK_LEN;
9ca2f2
+    wsctx->header.mask = wsctx->header.data->u.m;
9ca2f2
+  } else if (wsctx->header.payloadLen == 126 && 8 <= wsctx->nReadRaw) {
9ca2f2
+    wsctx->header.headerLen = 4 + WS_HYBI_MASK_LEN;
9ca2f2
+    wsctx->header.payloadLen = WS_NTOH16(wsctx->header.data->u.s16.l16);
9ca2f2
+    wsctx->header.mask = wsctx->header.data->u.s16.m16;
9ca2f2
+  } else if (wsctx->header.payloadLen == 127 && 14 <= wsctx->nReadRaw) {
9ca2f2
+    wsctx->header.headerLen = 10 + WS_HYBI_MASK_LEN;
9ca2f2
+    wsctx->header.payloadLen = WS_NTOH64(wsctx->header.data->u.s64.l64);
9ca2f2
+    wsctx->header.mask = wsctx->header.data->u.s64.m64;
9ca2f2
+  } else {
9ca2f2
+    /* Incomplete frame header, try again */
9ca2f2
+    rfbErr("%s: incomplete frame header; ret=%d\n", __func__, ret);
9ca2f2
+    errno = EAGAIN;
9ca2f2
+    *sockRet = -1;
9ca2f2
+    return WS_HYBI_STATE_HEADER_PENDING;
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  /* absolute length of frame */
9ca2f2
+  wsctx->nToRead = wsctx->header.headerLen + wsctx->header.payloadLen;
9ca2f2
 
9ca2f2
-    if (ret < 2) {
9ca2f2
-        /* save errno because rfbErr() will tamper it */
9ca2f2
-        if (-1 == ret) {
9ca2f2
-            int olderrno = errno;
9ca2f2
-            rfbErr("%s: peek; %m\n", __func__);
9ca2f2
-            errno = olderrno;
9ca2f2
-        } else if (0 == ret) {
9ca2f2
-            result = 0;
9ca2f2
-        } else {
9ca2f2
-            errno = EAGAIN;
9ca2f2
-        }
9ca2f2
-        goto spor;
9ca2f2
-    }
9ca2f2
+  /* set payload pointer just after header */
9ca2f2
+  wsctx->writePos = wsctx->codeBufDecode + wsctx->nReadRaw;
9ca2f2
 
9ca2f2
-    opcode = header->b0 & 0x0f;
9ca2f2
-    /* fin = (header->b0 & 0x80) >> 7; */ /* not used atm */
9ca2f2
-    flength = header->b1 & 0x7f;
9ca2f2
+  wsctx->readPos = (unsigned char *)(wsctx->codeBufDecode + wsctx->header.headerLen);
9ca2f2
 
9ca2f2
-    /*
9ca2f2
-     * 4.3. Client-to-Server Masking
9ca2f2
-     *
9ca2f2
-     * The client MUST mask all frames sent to the server.  A server MUST
9ca2f2
-     * close the connection upon receiving a frame with the MASK bit set to 0.
9ca2f2
-    **/
9ca2f2
-    if (!(header->b1 & 0x80)) {
9ca2f2
-	rfbErr("%s: got frame without mask\n", __func__, ret);
9ca2f2
-	errno = EIO;
9ca2f2
-	goto spor;
9ca2f2
-    }
9ca2f2
-
9ca2f2
-    if (flength < 126) {
9ca2f2
-	fhlen = 2;
9ca2f2
-	mask = header->u.m;
9ca2f2
-    } else if (flength == 126 && 4 <= ret) {
9ca2f2
-	flength = WS_NTOH16(header->u.s16.l16);
9ca2f2
-	fhlen = 4;
9ca2f2
-	mask = header->u.s16.m16;
9ca2f2
-    } else if (flength == 127 && 10 <= ret) {
9ca2f2
-	flength = WS_NTOH64(header->u.s64.l64);
9ca2f2
-	fhlen = 10;
9ca2f2
-	mask = header->u.s64.m64;
9ca2f2
-    } else {
9ca2f2
-      /* Incomplete frame header */
9ca2f2
-      rfbErr("%s: incomplete frame header\n", __func__, ret);
9ca2f2
-      errno = EIO;
9ca2f2
-      goto spor;
9ca2f2
-    }
9ca2f2
+  rfbLog("header complete: state=%d flen=%d writeTo=%p\n", wsctx->hybiDecodeState, wsctx->nToRead, wsctx->writePos);
9ca2f2
+
9ca2f2
+  return WS_HYBI_STATE_DATA_NEEDED;
9ca2f2
+}
9ca2f2
 
9ca2f2
-    /* absolute length of frame */
9ca2f2
-    total = fhlen + flength + 4;
9ca2f2
-    payload = buf + fhlen + 4; /* header length + mask */
9ca2f2
+static int
9ca2f2
+hybiWsFrameComplete(ws_ctx_t *wsctx)
9ca2f2
+{
9ca2f2
+  return wsctx != NULL && hybiRemaining(wsctx) == 0;
9ca2f2
+}
9ca2f2
 
9ca2f2
-    if (-1 == (ret = ws_read(cl, buf, total))) {
9ca2f2
+static char *
9ca2f2
+hybiPayloadStart(ws_ctx_t *wsctx)
9ca2f2
+{
9ca2f2
+  return wsctx->codeBufDecode + wsctx->header.headerLen;
9ca2f2
+}
9ca2f2
+
9ca2f2
+
9ca2f2
+/**
9ca2f2
+ * Read the remaining payload bytes from associated raw socket.
9ca2f2
+ *
9ca2f2
+ *  - try to read remaining bytes from socket
9ca2f2
+ *  - unmask all multiples of 4
9ca2f2
+ *  - if frame incomplete but some bytes are left, these are copied to
9ca2f2
+ *      the carry buffer
9ca2f2
+ *  - if opcode is TEXT: Base64-decode all unmasked received bytes
9ca2f2
+ *  - set state for reading decoded data
9ca2f2
+ *  - reset write position to begin of buffer (+ header)
9ca2f2
+ *      --> before we retrieve more data we let the caller clear all bytes
9ca2f2
+ *          from the reception buffer
9ca2f2
+ *  - execute return data routine
9ca2f2
+ *
9ca2f2
+ *  Sets errno corresponding to what it gets from the underlying
9ca2f2
+ *  socket or EIO if some internal sanity check fails.
9ca2f2
+ *
9ca2f2
+ *  @param[in]  cl client ptr with raw socket reference
9ca2f2
+ *  @param[out] dst  destination buffer
9ca2f2
+ *  @param[in]  len  size of destination buffer
9ca2f2
+ *  @param[out] sockRet emulated recv return value
9ca2f2
+ *  @return next hybi decode state
9ca2f2
+ */
9ca2f2
+static int
9ca2f2
+hybiReadAndDecode(rfbClientPtr cl, char *dst, int len, int *sockRet)
9ca2f2
+{
9ca2f2
+  int n;
9ca2f2
+  int i;
9ca2f2
+  int toReturn;
9ca2f2
+  int toDecode;
9ca2f2
+  int bufsize;
9ca2f2
+  int nextRead;
9ca2f2
+  unsigned char *data;
9ca2f2
+  uint32_t *data32;
9ca2f2
+  ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
9ca2f2
+
9ca2f2
+  /* if data was carried over, copy to start of buffer */
9ca2f2
+  memcpy(wsctx->writePos, wsctx->carryBuf, wsctx->carrylen);
9ca2f2
+  wsctx->writePos += wsctx->carrylen;
9ca2f2
+
9ca2f2
+  /* -1 accounts for potential '\0' terminator for base64 decoding */
9ca2f2
+  bufsize = wsctx->codeBufDecode + ARRAYSIZE(wsctx->codeBufDecode) - wsctx->writePos - 1;
9ca2f2
+  if (hybiRemaining(wsctx) > bufsize) {
9ca2f2
+    nextRead = bufsize;
9ca2f2
+  } else {
9ca2f2
+    nextRead = hybiRemaining(wsctx);
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  rfbLog("calling read with buf=%p and len=%d (decodebuf=%p headerLen=%d\n)", wsctx->writePos, nextRead, wsctx->codeBufDecode, wsctx->header.headerLen);
9ca2f2
+
9ca2f2
+  if (wsctx->nReadRaw < wsctx->nToRead) {
9ca2f2
+    /* decode more data */
9ca2f2
+    if (-1 == (n = ws_read(cl, wsctx->writePos, nextRead))) {
9ca2f2
       int olderrno = errno;
9ca2f2
       rfbErr("%s: read; %m", __func__);
9ca2f2
       errno = olderrno;
9ca2f2
-      return ret;
9ca2f2
-    } else if (ret < total) {
9ca2f2
-      /* GT TODO: hmm? */
9ca2f2
-      rfbLog("%s: read; got partial data\n", __func__);
9ca2f2
-    } else {
9ca2f2
-      buf[ret] = '\0';
9ca2f2
-    }
9ca2f2
+      *sockRet = -1;
9ca2f2
+      return WS_HYBI_STATE_ERR;
9ca2f2
+    } else if (n == 0) {
9ca2f2
+      *sockRet = 0;
9ca2f2
+      return WS_HYBI_STATE_ERR;
9ca2f2
+    }
9ca2f2
+    wsctx->nReadRaw += n;
9ca2f2
+    rfbLog("read %d bytes from socket; nRead=%d\n", n, wsctx->nReadRaw);
9ca2f2
+  } else {
9ca2f2
+    n = 0;
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  wsctx->writePos += n;
9ca2f2
+
9ca2f2
+  if (wsctx->nReadRaw >= wsctx->nToRead) {
9ca2f2
+    if (wsctx->nReadRaw > wsctx->nToRead) {
9ca2f2
+      rfbErr("%s: internal error, read past websocket frame", __func__);
9ca2f2
+      errno=EIO;
9ca2f2
+      *sockRet = -1;
9ca2f2
+      return WS_HYBI_STATE_ERR;
9ca2f2
+    }
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  toDecode = wsctx->writePos - hybiPayloadStart(wsctx);
9ca2f2
+  rfbLog("toDecode=%d from n=%d carrylen=%d headerLen=%d\n", toDecode, n, wsctx->carrylen, wsctx->header.headerLen);
9ca2f2
+  if (toDecode < 0) {
9ca2f2
+    rfbErr("%s: internal error; negative number of bytes to decode: %d", __func__, toDecode);
9ca2f2
+    errno=EIO;
9ca2f2
+    *sockRet = -1;
9ca2f2
+    return WS_HYBI_STATE_ERR;
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  /* for a possible base64 decoding, we decode multiples of 4 bytes until
9ca2f2
+   * the whole frame is received and carry over any remaining bytes in the carry buf*/
9ca2f2
+  data = (unsigned char *)hybiPayloadStart(wsctx);
9ca2f2
+  data32= (uint32_t *)data;
9ca2f2
+
9ca2f2
+  for (i = 0; i < (toDecode >> 2); i++) {
9ca2f2
+    data32[i] ^= wsctx->header.mask.u;
9ca2f2
+  }
9ca2f2
+  rfbLog("mask decoding; i=%d toDecode=%d\n", i, toDecode);
9ca2f2
 
9ca2f2
-    /* process 1 frame (32 bit op) */
9ca2f2
-    payload32 = (uint32_t *)payload;
9ca2f2
-    for (i = 0; i < flength / 4; i++) {
9ca2f2
-	payload32[i] ^= mask.u;
9ca2f2
-    }
9ca2f2
+  if (wsctx->hybiDecodeState == WS_HYBI_STATE_FRAME_COMPLETE) {
9ca2f2
     /* process the remaining bytes (if any) */
9ca2f2
-    for (i*=4; i < flength; i++) {
9ca2f2
-	payload[i] ^= mask.c[i % 4];
9ca2f2
+    for (i*=4; i < toDecode; i++) {
9ca2f2
+      data[i] ^= wsctx->header.mask.c[i % 4];
9ca2f2
     }
9ca2f2
 
9ca2f2
-    switch (opcode) {
9ca2f2
-      case WS_OPCODE_CLOSE:
9ca2f2
-	rfbLog("got closure, reason %d\n", WS_NTOH16(((uint16_t *)payload)[0]));
9ca2f2
-	errno = ECONNRESET;
9ca2f2
-	break;
9ca2f2
-      case WS_OPCODE_TEXT_FRAME:
9ca2f2
-	if (-1 == (flength = __b64_pton(payload, (unsigned char *)wsctx->codeBufDecode, sizeof(wsctx->codeBufDecode)))) {
9ca2f2
-	  rfbErr("%s: Base64 decode error; %m\n", __func__);
9ca2f2
-	  break;
9ca2f2
-	}
9ca2f2
-	payload = wsctx->codeBufDecode;
9ca2f2
-	/* fall through */
9ca2f2
-      case WS_OPCODE_BINARY_FRAME:
9ca2f2
-	if (flength > len) {
9ca2f2
-	  memcpy(wsctx->readbuf, payload + len, flength - len);
9ca2f2
-	  wsctx->readbufstart = 0;
9ca2f2
-	  wsctx->readbuflen = flength - len;
9ca2f2
-	  flength = len;
9ca2f2
-	}
9ca2f2
-	memcpy(dst, payload, flength);
9ca2f2
-	result = flength;
9ca2f2
-	break;
9ca2f2
+    /* all data is here, no carrying */
9ca2f2
+    wsctx->carrylen = 0;
9ca2f2
+  } else {
9ca2f2
+    /* carry over remaining, non-multiple-of-four bytes */
9ca2f2
+    wsctx->carrylen = toDecode - (i * 4);
9ca2f2
+    if (wsctx->carrylen < 0 || wsctx->carrylen > ARRAYSIZE(wsctx->carryBuf)) {
9ca2f2
+      rfbErr("%s: internal error, invalid carry over size: carrylen=%d, toDecode=%d, i=%d", __func__, wsctx->carrylen, toDecode, i);
9ca2f2
+      *sockRet = -1;
9ca2f2
+      errno = EIO;
9ca2f2
+      return WS_HYBI_STATE_ERR;
9ca2f2
+    }
9ca2f2
+    rfbLog("carrying over %d bytes from %p to %p\n", wsctx->carrylen, wsctx->writePos + (i * 4), wsctx->carryBuf);
9ca2f2
+    memcpy(wsctx->carryBuf, data + (i * 4), wsctx->carrylen);
9ca2f2
+  }
9ca2f2
+
9ca2f2
+  toReturn = toDecode - wsctx->carrylen;
9ca2f2
+
9ca2f2
+  switch (wsctx->header.opcode) {
9ca2f2
+    case WS_OPCODE_CLOSE:
9ca2f2
+
9ca2f2
+      /* this data is not returned as payload data */
9ca2f2
+      if (hybiWsFrameComplete(wsctx)) {
9ca2f2
+        rfbLog("got closure, reason %d\n", WS_NTOH16(((uint16_t *)data)[0]));
9ca2f2
+        errno = ECONNRESET;
9ca2f2
+        *sockRet = -1;
9ca2f2
+        return WS_HYBI_STATE_FRAME_COMPLETE;
9ca2f2
+      } else {
9ca2f2
+        rfbErr("%s: close reason with long frame not supported", __func__);
9ca2f2
+        errno = EIO;
9ca2f2
+        *sockRet = -1;
9ca2f2
+        return WS_HYBI_STATE_ERR;
9ca2f2
+      }
9ca2f2
+      break;
9ca2f2
+    case WS_OPCODE_TEXT_FRAME:
9ca2f2
+      data[toReturn] = '\0';
9ca2f2
+      rfbLog("Initiate Base64 decoding in %p with max size %d and '\\0' at %p\n", data, bufsize, data + toReturn);
9ca2f2
+      if (-1 == (wsctx->readlen = __b64_pton((char *)data, data, bufsize))) {
9ca2f2
+        rfbErr("Base64 decode error in %s; data=%p bufsize=%d", __func__, data, bufsize);
9ca2f2
+        rfbErr("%s: Base64 decode error; %m\n", __func__);
9ca2f2
+      }
9ca2f2
+      wsctx->writePos = hybiPayloadStart(wsctx);
9ca2f2
+      break;
9ca2f2
+    case WS_OPCODE_BINARY_FRAME:
9ca2f2
+      wsctx->readlen = toReturn;
9ca2f2
+      wsctx->writePos = hybiPayloadStart(wsctx);
9ca2f2
+      break;
9ca2f2
+    default:
9ca2f2
+      rfbErr("%s: unhandled opcode %d, b0: %02x, b1: %02x\n", __func__, (int)wsctx->header.opcode, wsctx->header.data->b0, wsctx->header.data->b1);
9ca2f2
+  }
9ca2f2
+  wsctx->readPos = data;
9ca2f2
+
9ca2f2
+  return hybiReturnData(dst, len, wsctx, sockRet);
9ca2f2
+}
9ca2f2
+
9ca2f2
+/**
9ca2f2
+ * Read function for websocket-socket emulation.
9ca2f2
+ *
9ca2f2
+ *    0                   1                   2                   3
9ca2f2
+ *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
9ca2f2
+ *   +-+-+-+-+-------+-+-------------+-------------------------------+
9ca2f2
+ *   |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
9ca2f2
+ *   |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
9ca2f2
+ *   |N|V|V|V|       |S|             |   (if payload len==126/127)   |
9ca2f2
+ *   | |1|2|3|       |K|             |                               |
9ca2f2
+ *   +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
9ca2f2
+ *   |     Extended payload length continued, if payload len == 127  |
9ca2f2
+ *   + - - - - - - - - - - - - - - - +-------------------------------+
9ca2f2
+ *   |                               |Masking-key, if MASK set to 1  |
9ca2f2
+ *   +-------------------------------+-------------------------------+
9ca2f2
+ *   | Masking-key (continued)       |          Payload Data         |
9ca2f2
+ *   +-------------------------------- - - - - - - - - - - - - - - - +
9ca2f2
+ *   :                     Payload Data continued ...                :
9ca2f2
+ *   + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
9ca2f2
+ *   |                     Payload Data continued ...                |
9ca2f2
+ *   +---------------------------------------------------------------+
9ca2f2
+ *
9ca2f2
+ * Using the decode buffer, this function:
9ca2f2
+ *  - reads the complete header from the underlying socket
9ca2f2
+ *  - reads any remaining data bytes
9ca2f2
+ *  - unmasks the payload data using the provided mask
9ca2f2
+ *  - decodes Base64 encoded text data
9ca2f2
+ *  - copies len bytes of decoded payload data into dst
9ca2f2
+ *
9ca2f2
+ * Emulates a read call on a socket.
9ca2f2
+ */
9ca2f2
+static int
9ca2f2
+webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len)
9ca2f2
+{
9ca2f2
+    int result = -1;
9ca2f2
+    ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
9ca2f2
+    /* int fin; */ /* not used atm */
9ca2f2
+
9ca2f2
+    /* rfbLog(" <== %s[%d]: %d cl: %p, wsctx: %p-%p (%d)\n", __func__, gettid(), len, cl, wsctx, (char *)wsctx + sizeof(ws_ctx_t), sizeof(ws_ctx_t)); */
9ca2f2
+    rfbLog("%s_enter: len=%d; "
9ca2f2
+                      "CTX: readlen=%d readPos=%p "
9ca2f2
+                      "writeTo=%p "
9ca2f2
+                      "state=%d toRead=%d remaining=%d "
9ca2f2
+                      " nReadRaw=%d carrylen=%d carryBuf=%p\n",
9ca2f2
+                      __func__, len,
9ca2f2
+                      wsctx->readlen, wsctx->readPos,
9ca2f2
+                      wsctx->writePos,
9ca2f2
+                      wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
9ca2f2
+                      wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf);
9ca2f2
+
9ca2f2
+    switch (wsctx->hybiDecodeState){
9ca2f2
+      case WS_HYBI_STATE_HEADER_PENDING:
9ca2f2
+        wsctx->hybiDecodeState = hybiReadHeader(cl, &result);
9ca2f2
+        if (wsctx->hybiDecodeState == WS_HYBI_STATE_ERR) {
9ca2f2
+          goto spor;
9ca2f2
+        }
9ca2f2
+        if (wsctx->hybiDecodeState != WS_HYBI_STATE_HEADER_PENDING) {
9ca2f2
+
9ca2f2
+          /* when header is complete, try to read some more data */
9ca2f2
+          wsctx->hybiDecodeState = hybiReadAndDecode(cl, dst, len, &result);
9ca2f2
+        }
9ca2f2
+        break;
9ca2f2
+      case WS_HYBI_STATE_DATA_AVAILABLE:
9ca2f2
+        wsctx->hybiDecodeState = hybiReturnData(dst, len, wsctx, &result);
9ca2f2
+        break;
9ca2f2
+      case WS_HYBI_STATE_DATA_NEEDED:
9ca2f2
+        wsctx->hybiDecodeState = hybiReadAndDecode(cl, dst, len, &result);
9ca2f2
+        break;
9ca2f2
+      case WS_HYBI_STATE_CLOSE_REASON_PENDING:
9ca2f2
+        wsctx->hybiDecodeState = hybiReadAndDecode(cl, dst, len, &result);
9ca2f2
+        break;
9ca2f2
       default:
9ca2f2
-	rfbErr("%s: unhandled opcode %d, b0: %02x, b1: %02x\n", __func__, (int)opcode, header->b0, header->b1);
9ca2f2
+        /* invalid state */
9ca2f2
+        rfbErr("%s: called with invalid state %d\n", wsctx->hybiDecodeState);
9ca2f2
+        result = -1;
9ca2f2
+        errno = EIO;
9ca2f2
+        wsctx->hybiDecodeState = WS_HYBI_STATE_ERR;
9ca2f2
     }
9ca2f2
 
9ca2f2
     /* single point of return, if someone has questions :-) */
9ca2f2
 spor:
9ca2f2
     /* rfbLog("%s: ret: %d/%d\n", __func__, result, len); */
9ca2f2
+    if (wsctx->hybiDecodeState == WS_HYBI_STATE_FRAME_COMPLETE) {
9ca2f2
+      rfbLog("frame received successfully, cleaning up: read=%d hlen=%d plen=%d\n", wsctx->header.nRead, wsctx->header.headerLen, wsctx->header.payloadLen);
9ca2f2
+      /* frame finished, cleanup state */
9ca2f2
+      hybiDecodeCleanup(wsctx);
9ca2f2
+    } else if (wsctx->hybiDecodeState == WS_HYBI_STATE_ERR) {
9ca2f2
+      hybiDecodeCleanup(wsctx);
9ca2f2
+    }
9ca2f2
+    rfbLog("%s_exit: len=%d; "
9ca2f2
+                      "CTX: readlen=%d readPos=%p "
9ca2f2
+                      "writePos=%p "
9ca2f2
+                      "state=%d toRead=%d remaining=%d "
9ca2f2
+                      "nRead=%d carrylen=%d carryBuf=%p "
9ca2f2
+                      "result=%d\n",
9ca2f2
+                      __func__, len,
9ca2f2
+                      wsctx->readlen, wsctx->readPos,
9ca2f2
+                      wsctx->writePos,
9ca2f2
+                      wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
9ca2f2
+                      wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf,
9ca2f2
+                      result);
9ca2f2
     return result;
9ca2f2
 }
9ca2f2
 
9ca2f2
@@ -924,7 +1255,7 @@ webSocketsHasDataInBuffer(rfbClientPtr c
9ca2f2
 {
9ca2f2
     ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
9ca2f2
 
9ca2f2
-    if (wsctx && wsctx->readbuflen)
9ca2f2
+    if (wsctx && wsctx->readlen)
9ca2f2
       return TRUE;
9ca2f2
 
9ca2f2
     return (cl->sslctx && rfbssl_pending(cl) > 0);