[Dnsmasq-discuss] [PATCH] Fix DHCPv6 options memory leaks

renmingshuai renmingshuai at huawei.com
Thu Sep 28 08:28:35 UTC 2023


When I repeatedly reloaded the dnsmasq process, I found a memory leak. To reproduce the problem, perform the following steps:

(1)   Run dnsmasq as follows:
# dnsmasq --no-hosts --no-resolv --dhcp-optsfile=/tmp/opts --dhcp-range=set:subnet-368e344f-8862-46f5-897e-02602d2e0eaa,2003:0:0:1499::,static,64,2h
# cat /tmp/opts
tag:subnet-368e344f-8862-46f5-897e-02602d2e0eaa,option6:domain-search,openstacklocal
tag:subnet-368e344f-8862-46f5-897e-02602d2e0eaa,option6:dns-server,[2003:0:0:1499::2],[2003:0:0:1499::3]

(2)    Send SIGHUP to dnsmasq repeatedly
# cat send.sh
#!/bin/bash
while true
do
       kill -HUP `pidof dnsmasq`
       sleep
done
# sh send.sh

After a period of time, the vmRss value of the dnsmasq process keeps increasing.

# date
Fri Sep 29 12:43:27 AM CST 2023
[root at localhost home]# ps -aux|grep dnsmasq
dnsmasq  3322571  0.0  0.0   9044  2456 ?        S    00:43   0:00 dnsmasq --no-hosts --no-resolv --dhcp-optsfile=/tmp/opts --dhcp-range=set:subnet-368e344f-8862-46f5-897e-02602d2e0eaa,2003:0:0:1499::,static,64,infinite
# date
Fri Sep 29 01:09:28 AM CST 2023
# ps -aux|grep dnsmasq
dnsmasq  3322571  0.1  0.0   9772  3280 ?        S    00:43   0:02 dnsmasq --no-hosts --no-resolv --dhcp-optsfile=/tmp/opts --dhcp-range=set:subnet-368e344f-8862-46f5-897e-02602d2e0eaa,2003:0:0:1499::,static,64,infinite

When we configure DHCPv6 option information by using -dhcp-optsfile, dnsmasq process allocates memory to store those information. However, when dnsmasq receives SIGHUP and reread the -dhcp-optsfile, it does not release the memory that store DHCPv6 options information as it does for DHCPv4 options.

The following patch could fix this problem.
---
src/option.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/option.c b/src/option.c
index 8322725..1cf2796 100644
--- a/src/option.c
+++ b/src/option.c
@@ -5734,11 +5734,11 @@ static void clear_dynamic_conf(void)
     }
}
-static void clear_dynamic_opt(void)
+static void clear_dhcp_opt(struct dhcp_opt **dhcp_opts)
{
   struct dhcp_opt *opts, *cp, **up;
-  for (up = &daemon->dhcp_opts, opts = daemon->dhcp_opts; opts; opts = cp)
+  for (up = dhcp_opts, opts = *dhcp_opts; opts; opts = cp)
     {
       cp = opts->next;

@@ -5752,6 +5752,14 @@ static void clear_dynamic_opt(void)
     }
}
+static void clear_dynamic_opt(void)
+{
+  clear_dhcp_opt(&daemon->dhcp_opts);
+#ifdef HAVE_DHCP6
+  clear_dhcp_opt(&daemon->dhcp_opts6);
+#endif
+}
+
void reread_dhcp(void)
{
    struct hostsfile *hf;
--
2.33.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.thekelleys.org.uk/pipermail/dnsmasq-discuss/attachments/20230928/ee5894f4/attachment.htm>


More information about the Dnsmasq-discuss mailing list