check_send_expect() does some nasty ibuf magic to allow fn_match()
authorclaudio <claudio@openbsd.org>
Sat, 18 Sep 2021 19:44:46 +0000 (19:44 +0000)
committerclaudio <claudio@openbsd.org>
Sat, 18 Sep 2021 19:44:46 +0000 (19:44 +0000)
to work with a buffer that is not a real string.
The wpos is decremented in the wrong spot and would affect both
binary and non binary checks.
Simplify this code by using strndup.
OK rob@ benno@

usr.sbin/relayd/check_tcp.c

index 0a92712..6ab6952 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: check_tcp.c,v 1.57 2019/09/15 19:23:29 rob Exp $      */
+/*     $OpenBSD: check_tcp.c,v 1.58 2021/09/18 19:44:46 claudio Exp $  */
 
 /*
  * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -321,23 +321,20 @@ check_send_expect(struct ctl_tcp_event *cte)
                /*
                 * ensure string is nul-terminated.
                 */
-               b = ibuf_reserve(cte->buf, 1);
+               b = strndup(cte->buf->buf, ibuf_size(cte->buf));
                if (b == NULL)
                        fatal("out of memory");
-               *b = '\0';
-               if (fnmatch(cte->table->conf.exbuf, cte->buf->buf, 0) == 0) {
+               if (fnmatch(cte->table->conf.exbuf, b, 0) == 0) {
                        cte->host->he = HCE_SEND_EXPECT_OK;
                        cte->host->up = HOST_UP;
+                       free(b);
                        return (0);
                }
+               free(b);
        }
+
        cte->host->he = HCE_SEND_EXPECT_FAIL;
        cte->host->up = HOST_UNKNOWN;
-
-       /*
-        * go back to original position.
-        */
-       cte->buf->wpos--;
        return (1);
 }