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