BUG/MEDIUM: mcli: always realign wrapping buffers before parsing them
authorWilly Tarreau <w@1wt.eu>
Thu, 20 Jan 2022 07:47:35 +0000 (08:47 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 26 Jan 2022 07:52:35 +0000 (08:52 +0100)
Pipelined commands easily result in request buffers to wrap, and the
master-cli parser only deals with linear buffers since it needs contiguous
keywords to look for in a list. As soon as a buffer wraps, some commands
are ignored and the parser is called in loops because the wrapped data
do not leave the buffer.

Let's take the easiest path that's already used at the HTTP layer, we
simply realign the buffer if its input wraps. This rarely happens anyway
(typically once per buffer), remains reasonably cheap and guarantees this
cannot happen anymore.

This needs to be backported as far as 2.0.

(cherry picked from commit a4e4d66f701b58da87cad0a2a90dea7e7553bc71)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 31a5b0bb4932fafb273aba02c5529194de483bee)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit ad5bf14af84c59e6f3e98506d7ce394688f70d1d)
[wt: s/MAX_CLI_ARGS/MAX_STATS_ARGS in context]
Signed-off-by: Willy Tarreau <w@1wt.eu>

src/cli.c

index 5a4ca1f..9cb654d 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -2114,8 +2114,8 @@ int pcli_find_and_exec_kw(struct stream *s, char **args, int argl, char **errmsg
  */
 int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int *next_pid)
 {
-       char *str = (char *)ci_head(req);
-       char *end = (char *)ci_stop(req);
+       char *str;
+       char *end;
        char *args[MAX_STATS_ARGS + 1]; /* +1 for storing a NULL */
        int argl; /* number of args */
        char *p;
@@ -2126,6 +2126,15 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int
        int ret;
        int i = 0;
 
+       /* we cannot deal with a wrapping buffer, so let's take care of this
+        * first.
+        */
+       if (b_head(&req->buf) + b_data(&req->buf) > b_wrap(&req->buf))
+               b_slow_realign(&req->buf, trash.area, co_data(req));
+
+       str = (char *)ci_head(req);
+       end = (char *)ci_stop(req);
+
        p = str;
 
        if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {