[Dnsmasq-discuss] [PATCH] Fix hosts file index collision between --addn-hosts dirs and --hostsdir

David Härdeman david at hardeman.nu
Tue Nov 17 02:00:55 UTC 2026


Files found in an --addn-hosts directory get their index from
expand_filelist(), which counts on from the largest index in the
--addn-hosts list and doesn't advance daemon->host_index. Files in a
--hostsdir directory get theirs from daemon->host_index
(dyndir_addhosts()), which doesn't know about the former, so files of
both can get the same index.

When such a --hostsdir file changes, cache_remove_uid() then also
removes the names read from the --addn-hosts file with the same index,
and they stay gone until the next SIGHUP. E.g. with
--addn-hosts=/dir1 (containing a.hosts) and --hostsdir=/dir2, creating
/dir2/x.hosts flushes the names from /dir1/a.hosts.
record_source() also logs the wrong file for them. The names are lost
since 2.88, where a changed --hostsdir file has its old names removed
(commits 0017dd7 and 0ba25a0); before, the collision only affected the
logged source.

Allocate the indices in expand_filelist() from daemon->host_index as
well, and advance it, so that all hosts files get unique indices.

Signed-off-by: David Härdeman <david at hardeman.nu>
---
Found while adding a --hostsdir option to OpenWrt's dnsmasq init script, which already passes
--addn-hosts=/tmp/hosts (a directory). Reproducer, unprivileged:

  d=$(mktemp -d); mkdir $d/dir1 $d/dir2
  echo "192.0.2.1 router" > $d/dir1/a.hosts
  dnsmasq -k -C /dev/null --port=15353 --listen-address=127.0.0.1 --bind-interfaces \
      --no-resolv --no-hosts --addn-hosts=$d/dir1 --hostsdir=$d/dir2 &
  dig +short -p 15353 @127.0.0.1 router        # 192.0.2.1
  echo "192.0.2.9 x" > $d/dir2/x.hosts
  dig +short -p 15353 @127.0.0.1 router        # nothing until SIGHUP

It's a regression since 2.88 (v2.87 keeps the names; with v2.88, v2.93 and master 10fbc39 they
are flushed). I didn't find an earlier report on the list. Tested on v2.93 and master.

 src/option.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/option.c b/src/option.c
index 012b14a..6595368 100644
--- a/src/option.c
+++ b/src/option.c
@@ -5774,8 +5774,10 @@ struct hostsfile *expand_filelist(struct hostsfile *list)
   struct hostsfile *ah, *last, *next, **up;
   struct dirent **namelist;
 
-  /* find largest used index */
-  for (last = NULL, i = SRC_AH, ah = list; ah; ah = ah->next)
+  /* find largest used index. Files found in --hostsdir directories
+     take theirs from daemon->host_index too (dyndir_addhosts()), so
+     start there and advance it below, to keep all indices unique. */
+  for (last = NULL, i = daemon->host_index, ah = list; ah; ah = ah->next)
     {
       last = ah;
       
@@ -5873,6 +5875,9 @@ struct hostsfile *expand_filelist(struct hostsfile *list)
 	    free(namelist);
 	  }
       }
+
+  if (daemon->host_index < (int)i)
+    daemon->host_index = i;
   
   return list;
 }
-- 
2.47.3




More information about the Dnsmasq-discuss mailing list