96e6b5
From 2c06dc63aa864be8648758e71fa70e3d3f47e06f Mon Sep 17 00:00:00 2001
96e6b5
From: hopper-vul <118949689+hopper-vul@users.noreply.github.com>
96e6b5
Date: Wed, 18 Jan 2023 22:14:26 +0800
96e6b5
Subject: [PATCH] deps(cares): Add str len check in config_sortlist to avoid
96e6b5
 stack overflow (#497)
96e6b5
96e6b5
In ares_set_sortlist, it calls config_sortlist(..., sortstr) to parse
96e6b5
the input str and initialize a sortlist configuration.
96e6b5
96e6b5
However, ares_set_sortlist has not any checks about the validity of the input str.
96e6b5
It is very easy to create an arbitrary length stack overflow with the unchecked
96e6b5
`memcpy(ipbuf, str, q-str);` and `memcpy(ipbufpfx, str, q-str);`
96e6b5
statements in the config_sortlist call, which could potentially cause severe
96e6b5
security impact in practical programs.
96e6b5
96e6b5
This commit add necessary check for `ipbuf` and `ipbufpfx` which avoid the
96e6b5
potential stack overflows.
96e6b5
96e6b5
fixes #496
96e6b5
96e6b5
Fix By: @hopper-vul
96e6b5
Resolves: CVE-2022-4904
96e6b5
96e6b5
Signed-off-by: rpm-build <rpm-build>
96e6b5
---
96e6b5
 deps/cares/src/lib/ares_init.c | 4 ++++
96e6b5
 1 file changed, 4 insertions(+)
96e6b5
96e6b5
diff --git a/deps/cares/src/lib/ares_init.c b/deps/cares/src/lib/ares_init.c
96e6b5
index de5d86c..d5858f6 100644
96e6b5
--- a/deps/cares/src/lib/ares_init.c
96e6b5
+++ b/deps/cares/src/lib/ares_init.c
96e6b5
@@ -2243,6 +2243,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
96e6b5
       q = str;
96e6b5
       while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
96e6b5
         q++;
96e6b5
+      if (q-str >= 16)
96e6b5
+        return ARES_EBADSTR;
96e6b5
       memcpy(ipbuf, str, q-str);
96e6b5
       ipbuf[q-str] = '\0';
96e6b5
       /* Find the prefix */
96e6b5
@@ -2251,6 +2253,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
96e6b5
           const char *str2 = q+1;
96e6b5
           while (*q && *q != ';' && !ISSPACE(*q))
96e6b5
             q++;
96e6b5
+          if (q-str >= 32)
96e6b5
+            return ARES_EBADSTR;
96e6b5
           memcpy(ipbufpfx, str, q-str);
96e6b5
           ipbufpfx[q-str] = '\0';
96e6b5
           str = str2;
96e6b5
-- 
96e6b5
2.39.2
96e6b5