source: npl/internetserver/djbdns/CVE2008-4392_0001-dnscache-merge-similar-outgoing-queries.patch @ 21d4867

perl-5.22
Last change on this file since 21d4867 was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 9.7 KB
RevLine 
[c5c522c]1diff --git a/Makefile b/Makefile
2index 1429643..bc047c0 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -318,11 +318,11 @@ stralloc.h iopause.h taia.h tai.h uint64.h taia.h
6        ./compile dns_txt.c
7 
8 dnscache: \
9-load dnscache.o droproot.o okclient.o log.o cache.o query.o \
10+load dnscache.o droproot.o okclient.o log.o cache.o query.o qmerge.o \
11 response.o dd.o roots.o iopause.o prot.o dns.a env.a alloc.a buffer.a \
12 libtai.a unix.a byte.a socket.lib
13        ./load dnscache droproot.o okclient.o log.o cache.o \
14-       query.o response.o dd.o roots.o iopause.o prot.o dns.a \
15+       query.o qmerge.o response.o dd.o roots.o iopause.o prot.o dns.a \
16        env.a alloc.a buffer.a libtai.a unix.a byte.a  `cat \
17        socket.lib`
18 
19@@ -343,7 +343,7 @@ compile dnscache.c env.h exit.h scan.h strerr.h error.h ip4.h \
20 uint16.h uint64.h socket.h uint16.h dns.h stralloc.h gen_alloc.h \
21 iopause.h taia.h tai.h uint64.h taia.h taia.h byte.h roots.h fmt.h \
22 iopause.h query.h dns.h uint32.h alloc.h response.h uint32.h cache.h \
23-uint32.h uint64.h ndelay.h log.h uint64.h okclient.h droproot.h
24+uint32.h uint64.h ndelay.h log.h uint64.h okclient.h droproot.h maxclient.h
25        ./compile dnscache.c
26 
27 dnsfilter: \
28@@ -687,11 +687,16 @@ qlog.o: \
29 compile qlog.c buffer.h qlog.h uint16.h
30        ./compile qlog.c
31 
32+qmerge.o: \
33+compile qmerge.c qmerge.h dns.h stralloc.h gen_alloc.h iopause.h \
34+taia.h tai.h uint64.h log.h maxclient.h
35+       ./compile qmerge.c
36+
37 query.o: \
38 compile query.c error.h roots.h log.h uint64.h case.h cache.h \
39 uint32.h uint64.h byte.h dns.h stralloc.h gen_alloc.h iopause.h \
40 taia.h tai.h uint64.h taia.h uint64.h uint32.h uint16.h dd.h alloc.h \
41-response.h uint32.h query.h dns.h uint32.h
42+response.h uint32.h query.h dns.h uint32.h qmerge.h
43        ./compile query.c
44 
45 random-ip: \
46diff --git a/dnscache.c b/dnscache.c
47index 8c899a3..5ccb16a 100644
48--- a/dnscache.c
49+++ b/dnscache.c
50@@ -22,6 +22,7 @@
51 #include "log.h"
52 #include "okclient.h"
53 #include "droproot.h"
54+#include "maxclient.h"
55 
56 static int packetquery(char *buf,unsigned int len,char **q,char qtype[2],char qclass[2],char id[2])
57 {
58@@ -54,7 +55,6 @@ uint64 numqueries = 0;
59 
60 static int udp53;
61 
62-#define MAXUDP 200
63 static struct udpclient {
64   struct query q;
65   struct taia start;
66@@ -131,7 +131,6 @@ void u_new(void)
67 
68 static int tcp53;
69 
70-#define MAXTCP 20
71 struct tcpclient {
72   struct query q;
73   struct taia start;
74diff --git a/log.c b/log.c
75index c43e8b0..b8cd7ce 100644
76--- a/log.c
77+++ b/log.c
78@@ -150,6 +150,13 @@ void log_tx(const char *q,const char qtype[2],const char *control,const char ser
79   line();
80 }
81 
82+void log_tx_piggyback(const char *q, const char qtype[2], const char *control)
83+{
84+  string("txpb ");
85+  logtype(qtype); space(); name(q); space(); name(control);
86+  line();
87+}
88+
89 void log_cachedanswer(const char *q,const char type[2])
90 {
91   string("cached "); logtype(type); space();
92diff --git a/log.h b/log.h
93index fe62fa3..d9a829b 100644
94--- a/log.h
95+++ b/log.h
96@@ -18,6 +18,7 @@ extern void log_cachednxdomain(const char *);
97 extern void log_cachedns(const char *,const char *);
98 
99 extern void log_tx(const char *,const char *,const char *,const char *,unsigned int);
100+extern void log_tx_piggyback(const char *,const char *,const char *);
101 
102 extern void log_nxdomain(const char *,const char *,unsigned int);
103 extern void log_nodata(const char *,const char *,const char *,unsigned int);
104diff --git a/maxclient.h b/maxclient.h
105new file mode 100644
106index 0000000..e52fcd1
107--- /dev/null
108+++ b/maxclient.h
109@@ -0,0 +1,7 @@
110+#ifndef MAXCLIENT_H
111+#define MAXCLIENT_H
112+
113+#define MAXUDP 200
114+#define MAXTCP 20
115+
116+#endif /* MAXCLIENT_H */
117diff --git a/qmerge.c b/qmerge.c
118new file mode 100644
119index 0000000..7c92299
120--- /dev/null
121+++ b/qmerge.c
122@@ -0,0 +1,115 @@
123+#include "qmerge.h"
124+#include "byte.h"
125+#include "log.h"
126+#include "maxclient.h"
127+
128+#define QMERGE_MAX (MAXUDP+MAXTCP)
129+struct qmerge inprogress[QMERGE_MAX];
130+
131+static
132+int qmerge_key_init(struct qmerge_key *qmk, const char *q, const char qtype[2],
133+    const char *control)
134+{
135+  if (!dns_domain_copy(&qmk->q, q)) return 0;
136+  byte_copy(qmk->qtype, 2, qtype);
137+  if (!dns_domain_copy(&qmk->control, control)) return 0;
138+  return 1;
139+}
140+
141+static
142+int qmerge_key_equal(struct qmerge_key *a, struct qmerge_key *b)
143+{
144+  return
145+    byte_equal(a->qtype, 2, b->qtype) &&
146+    dns_domain_equal(a->q, b->q) &&
147+    dns_domain_equal(a->control, b->control);
148+}
149+
150+static
151+void qmerge_key_free(struct qmerge_key *qmk)
152+{
153+  dns_domain_free(&qmk->q);
154+  dns_domain_free(&qmk->control);
155+}
156+
157+void qmerge_free(struct qmerge **x)
158+{
159+  struct qmerge *qm;
160+
161+  qm = *x;
162+  *x = 0;
163+  if (!qm || !qm->active) return;
164+
165+  qm->active--;
166+  if (!qm->active) {
167+    qmerge_key_free(&qm->key);
168+    dns_transmit_free(&qm->dt);
169+  }
170+}
171+
172+int qmerge_start(struct qmerge **qm, const char servers[64], int flagrecursive,
173+    const char *q, const char qtype[2], const char localip[4],
174+    const char *control)
175+{
176+  struct qmerge_key k;
177+  int i;
178+  int r;
179+
180+  qmerge_free(qm);
181+
182+  byte_zero(&k, sizeof k);
183+  if (!qmerge_key_init(&k, q, qtype, control)) return -1;
184+  for (i = 0; i < QMERGE_MAX; i++) {
185+    if (!inprogress[i].active) continue;
186+    if (!qmerge_key_equal(&k, &inprogress[i].key)) continue;
187+    log_tx_piggyback(q, qtype, control);
188+    inprogress[i].active++;
189+    *qm = &inprogress[i];
190+    qmerge_key_free(&k);
191+    return 0;
192+  }
193+
194+  for (i = 0; i < QMERGE_MAX; i++)
195+    if (!inprogress[i].active)
196+      break;
197+  if (i == QMERGE_MAX) return -1;
198+
199+  log_tx(q, qtype, control, servers, 0);
200+  r = dns_transmit_start(&inprogress[i].dt, servers, flagrecursive, q, qtype, localip);
201+  if (r == -1) { qmerge_key_free(&k); return -1; }
202+  inprogress[i].active++;
203+  inprogress[i].state = 0;
204+  qmerge_key_free(&inprogress[i].key);
205+  byte_copy(&inprogress[i].key, sizeof k, &k);
206+  *qm = &inprogress[i];
207+  return 0;
208+}
209+
210+void qmerge_io(struct qmerge *qm, iopause_fd *io, struct taia *deadline)
211+{
212+  if (qm->state == 0) {
213+    dns_transmit_io(&qm->dt, io, deadline);
214+    qm->state = 1;
215+  }
216+  else {
217+    io->fd = -1;
218+    io->events = 0;
219+  }
220+}
221+
222+int qmerge_get(struct qmerge **x, const iopause_fd *io, const struct taia *when)
223+{
224+  int r;
225+  struct qmerge *qm;
226+
227+  qm = *x;
228+  if (qm->state == -1) return -1; /* previous error */
229+  if (qm->state == 0) return 0; /* no packet */
230+  if (qm->state == 2) return 1; /* already got packet */
231+
232+  r = dns_transmit_get(&qm->dt, io, when);
233+  if (r == -1) { qm->state = -1; return -1; } /* error */
234+  if (r == 0) { qm->state = 0; return 0; } /* must wait for i/o */
235+  if (r == 1) { qm->state = 2; return 1; } /* got packet */
236+  return -1; /* bug */
237+}
238diff --git a/qmerge.h b/qmerge.h
239new file mode 100644
240index 0000000..9a58157
241--- /dev/null
242+++ b/qmerge.h
243@@ -0,0 +1,24 @@
244+#ifndef QMERGE_H
245+#define QMERGE_H
246+
247+#include "dns.h"
248+
249+struct qmerge_key {
250+  char *q;
251+  char qtype[2];
252+  char *control;
253+};
254+
255+struct qmerge {
256+  int active;
257+  struct qmerge_key key;
258+  struct dns_transmit dt;
259+  int state; /* -1 = error, 0 = need io, 1 = need get, 2 = got packet */
260+};
261+
262+extern int qmerge_start(struct qmerge **,const char *,int,const char *,const char *,const char *,const char *);
263+extern void qmerge_io(struct qmerge *,iopause_fd *,struct taia *);
264+extern int qmerge_get(struct qmerge **,const iopause_fd *,const struct taia *);
265+extern void qmerge_free(struct qmerge **);
266+
267+#endif /* QMERGE_H */
268diff --git a/query.c b/query.c
269index 46cdc00..f091fdd 100644
270--- a/query.c
271+++ b/query.c
272@@ -81,7 +81,7 @@ static void cleanup(struct query *z)
273   int j;
274   int k;
275 
276-  dns_transmit_free(&z->dt);
277+  qmerge_free(&z->qm);
278   for (j = 0;j < QUERY_MAXALIAS;++j)
279     dns_domain_free(&z->alias[j]);
280   for (j = 0;j < QUERY_MAXLEVEL;++j) {
281@@ -429,14 +429,8 @@ static int doit(struct query *z,int state)
282   if (j == 64) goto SERVFAIL;
283 
284   dns_sortip(z->servers[z->level],64);
285-  if (z->level) {
286-    log_tx(z->name[z->level],DNS_T_A,z->control[z->level],z->servers[z->level],z->level);
287-    if (dns_transmit_start(&z->dt,z->servers[z->level],flagforwardonly,z->name[z->level],DNS_T_A,z->localip) == -1) goto DIE;
288-  }
289-  else {
290-    log_tx(z->name[0],z->type,z->control[0],z->servers[0],0);
291-    if (dns_transmit_start(&z->dt,z->servers[0],flagforwardonly,z->name[0],z->type,z->localip) == -1) goto DIE;
292-  }
293+  dtype = z->level ? DNS_T_A : z->type;
294+  if (qmerge_start(&z->qm,z->servers[z->level],flagforwardonly,z->name[z->level],dtype,z->localip,z->control[z->level]) == -1) goto DIE;
295   return 0;
296 
297 
298@@ -450,10 +444,10 @@ static int doit(struct query *z,int state)
299 
300   HAVEPACKET:
301   if (++z->loop == 100) goto DIE;
302-  buf = z->dt.packet;
303-  len = z->dt.packetlen;
304+  buf = z->qm->dt.packet;
305+  len = z->qm->dt.packetlen;
306 
307-  whichserver = z->dt.servers + 4 * z->dt.curserver;
308+  whichserver = z->qm->dt.servers + 4 * z->qm->dt.curserver;
309   control = z->control[z->level];
310   d = z->name[z->level];
311   dtype = z->level ? DNS_T_A : z->type;
312@@ -836,7 +830,7 @@ int query_start(struct query *z,char *dn,char type[2],char class[2],char localip
313 
314 int query_get(struct query *z,iopause_fd *x,struct taia *stamp)
315 {
316-  switch(dns_transmit_get(&z->dt,x,stamp)) {
317+  switch(qmerge_get(&z->qm,x,stamp)) {
318     case 1:
319       return doit(z,1);
320     case -1:
321@@ -847,5 +841,5 @@ int query_get(struct query *z,iopause_fd *x,struct taia *stamp)
322 
323 void query_io(struct query *z,iopause_fd *x,struct taia *deadline)
324 {
325-  dns_transmit_io(&z->dt,x,deadline);
326+  qmerge_io(z->qm,x,deadline);
327 }
328diff --git a/query.h b/query.h
329index eff68b2..06feab4 100644
330--- a/query.h
331+++ b/query.h
332@@ -1,7 +1,7 @@
333 #ifndef QUERY_H
334 #define QUERY_H
335 
336-#include "dns.h"
337+#include "qmerge.h"
338 #include "uint32.h"
339 
340 #define QUERY_MAXLEVEL 5
341@@ -20,7 +20,7 @@ struct query {
342   char localip[4];
343   char type[2];
344   char class[2];
345-  struct dns_transmit dt;
346+  struct qmerge *qm;
347 } ;
348 
349 extern int query_start(struct query *,char *,char *,char *,char *);
Note: See TracBrowser for help on using the repository browser.