Fix race in TCP-process to parent pipe.

The pipe from a TCP handler process to the main dnsmasq process
is used to return information back to the main process, normally
RRsets for caching, but other messages too. This all normally happens
synchronously, but there's one situation where this is not true.

If the TCP handler process last for too long, it is reaped by ALARM
signal, and if the TCP handler process is working on DNSSEC
validation which started in UDP mode, but got promoted to TCP,
then a report of that fact gets sent to the main process so that
the blocked UDP query can be released to fail. By it's nature this
happens asynchronously and if the alarm signal interrupts whilst
another message is being send down the pipe, the two messages get
interleaved and garbage results. If this happens by accident
the usual result to a crash, but it could be manipulated by an
attacker to poison the cache or overwrite random memory.

The fix is to block the alarm signals whilst other processes
are sending messages down the pipe.

Thanks to Daniel Wade for finding and analysing this problem.
This commit is contained in:
Simon Kelley committed 2026-11-25 20:00:53 +00:00
1 parent 91a02a187c
commit 295d546705
4 files changed
+34 -4

No files matched your search

+2
View File
@@ -872,6 +872,7 @@ void cache_end_insert(void)
if (daemon->pipe_to_parent != -1)
{
unsigned char op = PIPE_OP_INSERT;
pipe_hold_alarm(1);
read_write(daemon->pipe_to_parent, &op, sizeof(op), RW_WRITE);
}
@@ -935,6 +936,7 @@ void cache_end_insert(void)
{
ssize_t m = -1;
read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), RW_WRITE);
pipe_hold_alarm(0);
}
}
+21 -2
View File
@@ -1368,7 +1368,7 @@ static void sig_handler(int sig)
else
{
/* udp_to_tcp transfer.
If daemon->header_to_tcp is NULL the waiting is over and
If daemon->header_to_tcp is NULL we're in debug mode and
we can let things take their course, otherwise, send a failure
return down the pipe to unblock the UDP transaction and kill
the process. */
@@ -1437,6 +1437,17 @@ void send_alarm(time_t event, time_t now)
}
}
/* Don't let the alarm handler interleave messages on the parent pipe. */
void pipe_hold_alarm(int hold)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGALRM);
sigprocmask(hold ? SIG_BLOCK : SIG_UNBLOCK, &set, NULL);
}
void queue_event(int event)
{
send_event(pipewrite, event, 0, NULL);
@@ -2252,7 +2263,9 @@ static void do_tcp_connection(struct listener *listener, time_t now, int slot)
if (!option_bool(OPT_DEBUG))
{
#ifdef HAVE_DNSSEC
pipe_hold_alarm(1);
cache_update_hwm(); /* Sneak out possibly updated crypto HWM values. */
pipe_hold_alarm(0);
#endif
close(daemon->pipe_to_parent);
@@ -2387,6 +2400,8 @@ int swap_to_tcp(struct frec *forward, time_t now, int status, struct dns_header
{
unsigned char op = PIPE_OP_RESULT;
pipe_hold_alarm(1); /* Released below, once the pipe is closed. */
/* tell our parent we're done, and what the result was then exit. */
read_write(daemon->pipe_to_parent, &op, sizeof(op), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)&status, sizeof(status), RW_WRITE);
@@ -2402,7 +2417,11 @@ int swap_to_tcp(struct frec *forward, time_t now, int status, struct dns_header
cache_update_hwm(); /* Sneak out possibly updated crypto HWM values. */
close(daemon->pipe_to_parent);
/* Nothing more goes up the pipe. Let the alarm in again, so a log
socket that never drains can't hold us in flush_log() for ever.
With forward_to_tcp clear, the handler just exits. */
daemon->forward_to_tcp = NULL;
pipe_hold_alarm(0);
flush_log();
_exit(0);
}
+1
View File
@@ -1384,6 +1384,7 @@ void cache_end_insert(void);
void cache_start_insert(void);
unsigned int cache_remove_uid(const unsigned int uid);
int cache_recv_insert(time_t now, int fd);
void pipe_hold_alarm(int hold);
#ifdef HAVE_DNSSEC
void cache_update_hwm(void);
#endif
+10 -2
View File
@@ -1007,7 +1007,11 @@ int extract_addresses(struct dns_header *header, size_t qlen, char *name, time_t
if (ipsets)
{
if (daemon->pipe_to_parent != -1)
cache_send_ipset(PIPE_OP_IPSET, ipsets, flags, &addr);
{
pipe_hold_alarm(1);
cache_send_ipset(PIPE_OP_IPSET, ipsets, flags, &addr);
pipe_hold_alarm(0);
}
else
for (ipsets_cur = ipsets->sets; *ipsets_cur; ipsets_cur++)
if (add_to_ipset(*ipsets_cur, &addr, flags, 0) == 0)
@@ -1018,7 +1022,11 @@ int extract_addresses(struct dns_header *header, size_t qlen, char *name, time_t
if (nftsets)
{
if (daemon->pipe_to_parent != -1)
cache_send_ipset(PIPE_OP_NFTSET, nftsets, flags, &addr);
{
pipe_hold_alarm(1);
cache_send_ipset(PIPE_OP_NFTSET, nftsets, flags, &addr);
pipe_hold_alarm(0);
}
else
for (nftsets_cur = nftsets->sets; *nftsets_cur; nftsets_cur++)
if (add_to_nftset(*nftsets_cur, &addr, flags, 0) == 0)