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