From 68e44be3ce3d8777760c2e828e8c094fcf13dce6 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 28 Apr 2015 08:52:16 +1000 Subject: [PATCH] Check for oversized client message before doing any reads to avoid possibility of exactly the wrong size buffer to ever invalidate the client, adding more info to a downgraded message --- src/connector.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/connector.c b/src/connector.c index ae768f8b..224fc5b8 100644 --- a/src/connector.c +++ b/src/connector.c @@ -354,6 +354,12 @@ static void parse_client_msg(cdata_t *cdata, client_instance_t *client) json_t *val; retry: + if (unlikely(client->bufofs > MAX_MSGSIZE)) { + LOGNOTICE("Client id %"PRId64" fd %d overloaded buffer without EOL, disconnecting", + client->id, client->fd); + invalidate_client(ckp, cdata, client); + return; + } buflen = PAGESIZE - client->bufofs; /* This read call is non-blocking since the socket is set to O_NOBLOCK */ ret = read(client->fd, client->buf + client->bufofs, buflen); @@ -368,14 +374,8 @@ retry: client->bufofs += ret; reparse: eol = memchr(client->buf, '\n', client->bufofs); - if (!eol) { - if (unlikely(client->bufofs > MAX_MSGSIZE)) { - LOGWARNING("Client fd %d overloaded buffer without EOL, disconnecting", client->fd); - invalidate_client(ckp, cdata, client); - return; - } + if (!eol) goto retry; - } /* Do something useful with this message now */ buflen = eol - client->buf + 1;