aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-05-09 19:21:45 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2023-05-09 19:21:45 +0200
commit62775ec4b34bd2a1b5e4c60aa40ba67bd979069a (patch)
tree8ec707dbb2f3e271d30160be10e5f221352112ad
parent3a7f00eadcf400df5f9381b49f3ff5e882af0261 (diff)
downloadbusybox-w32-62775ec4b34bd2a1b5e4c60aa40ba67bd979069a.tar.gz
busybox-w32-62775ec4b34bd2a1b5e4c60aa40ba67bd979069a.tar.bz2
busybox-w32-62775ec4b34bd2a1b5e4c60aa40ba67bd979069a.zip
nslookup: ensure unique transaction IDs for the DNS queries
Based on patch by Uwe Kleine-König. It makes sense to actually see the nitty-gritty details of DNS querying, so bringing in (commented-out) musl's DNS request code. function old new delta nslookup_main 760 822 +62 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/nslookup.c394
1 files changed, 390 insertions, 4 deletions
diff --git a/networking/nslookup.c b/networking/nslookup.c
index 6da97baf4..24eae4010 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -34,16 +34,14 @@
34//usage: "Name: debian\n" 34//usage: "Name: debian\n"
35//usage: "Address: 127.0.0.1\n" 35//usage: "Address: 127.0.0.1\n"
36 36
37#if !ENABLE_FEATURE_NSLOOKUP_BIG
38
37#include <resolv.h> 39#include <resolv.h>
38#include <net/if.h> /* for IFNAMSIZ */
39//#include <arpa/inet.h> 40//#include <arpa/inet.h>
40//#include <netdb.h> 41//#include <netdb.h>
41#include "libbb.h" 42#include "libbb.h"
42#include "common_bufsiz.h" 43#include "common_bufsiz.h"
43 44
44
45#if !ENABLE_FEATURE_NSLOOKUP_BIG
46
47/* 45/*
48 * Mini nslookup implementation for busybox 46 * Mini nslookup implementation for busybox
49 * 47 *
@@ -248,12 +246,385 @@ int nslookup_main(int argc, char **argv)
248 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 246 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
249 */ 247 */
250 248
249#include "libbb.h"
250#include "common_bufsiz.h"
251
251#if 0 252#if 0
252# define dbg(...) fprintf(stderr, __VA_ARGS__) 253# define dbg(...) fprintf(stderr, __VA_ARGS__)
253#else 254#else
254# define dbg(...) ((void)0) 255# define dbg(...) ((void)0)
255#endif 256#endif
256 257
258/* Instead of using ancient libc DNS query support,
259 * we can carry our own, independent code.
260 * E.g. res_mkquery() loses
261 * three of its paramemters (they are unused!).
262 * Unfortunately, while it does eliminate
263 * ns_get16
264 * ns_get32
265 * ns_name_uncompress
266 * dn_skipname
267 * ns_skiprr
268 * ns_initparse
269 * ns_parserr
270 * libc functions from a static binary, libc versions of
271 * dn_expand and res_mkquery are still linked in
272 * - they are used by getnameinfo(). Each is ~230 bytes of code.
273 * This makes USE_LIBC_RESOLV = 0 code _bigger_ (by about 27 bytes),
274 * despite inlining and constant propagation.
275 */
276#define USE_LIBC_RESOLV 1
277
278#if USE_LIBC_RESOLV
279
280#include <resolv.h>
281
282#else
283
284#define RESOLVFUNC /*nothing*/
285#define BIGRESOLVFUNC /*nothing*/
286#define TINYRESOLVFUNC ALWAYS_INLINE
287
288/* This one is taken from musl 1.2.4 */
289
290#define NS_MAXDNAME 1025
291#define NS_INT32SZ 4
292#define NS_INT16SZ 2
293
294#define MAXDNAME NS_MAXDNAME
295
296typedef enum __ns_opcode {
297 ns_o_query = 0,
298} ns_opcode;
299typedef enum __ns_class {
300 ns_c_in = 1,
301} ns_class;
302typedef enum __ns_sect {
303 ns_s_qd = 0,
304 ns_s_zn = 0,
305 ns_s_an = 1,
306 ns_s_pr = 1,
307 ns_s_ns = 2,
308 ns_s_ud = 2,
309 ns_s_ar = 3,
310 ns_s_max = 4
311} ns_sect;
312typedef enum __ns_type {
313 ns_t_a = 1,
314 ns_t_ns = 2,
315 ns_t_cname = 5,
316 ns_t_soa = 6,
317 ns_t_ptr = 12,
318 ns_t_mx = 15,
319 ns_t_txt = 16,
320 ns_t_aaaa = 28,
321 ns_t_srv = 33,
322 ns_t_any = 255,
323} ns_type;
324#define QUERY ns_o_query
325#define T_A ns_t_a
326#define T_PTR ns_t_ptr
327#define T_AAAA ns_t_aaaa
328#define C_IN ns_c_in
329
330typedef struct __ns_msg {
331 const unsigned char *_msg, *_eom;
332 uint16_t _id, _flags, _counts[ns_s_max];
333 const unsigned char *_sections[ns_s_max];
334 ns_sect _sect;
335 int _rrnum;
336 const unsigned char *_msg_ptr;
337} ns_msg;
338#define ns_msg_id(handle) ((handle)._id + 0)
339#define ns_msg_base(handle) ((handle)._msg + 0)
340#define ns_msg_end(handle) ((handle)._eom + 0)
341#define ns_msg_size(handle) ((handle)._eom - (handle)._msg)
342#define ns_msg_count(handle, section) ((handle)._counts[section] + 0)
343#define ns_msg_getflag(handle, flag) \
344 (((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift)
345
346typedef struct __ns_rr {
347 char name[NS_MAXDNAME];
348 uint16_t type;
349 uint16_t rr_class;
350 uint32_t ttl;
351 uint16_t rdlength;
352 const unsigned char *rdata;
353} ns_rr;
354#define ns_rr_name(rr) (((rr).name[0] != '\0') ? (rr).name : ".")
355#define ns_rr_type(rr) ((ns_type)((rr).type + 0))
356#define ns_rr_class(rr) ((ns_class)((rr).rr_class + 0))
357#define ns_rr_ttl(rr) ((rr).ttl + 0)
358#define ns_rr_rdlen(rr) ((rr).rdlength + 0)
359#define ns_rr_rdata(rr) ((rr).rdata + 0)
360
361typedef struct {
362 unsigned id :16;
363#if __BYTE_ORDER == __BIG_ENDIAN
364 unsigned qr: 1;
365 unsigned opcode: 4;
366 unsigned aa: 1;
367 unsigned tc: 1;
368 unsigned rd: 1;
369 unsigned ra: 1;
370 unsigned unused :1;
371 unsigned ad: 1;
372 unsigned cd: 1;
373 unsigned rcode :4;
374#else
375 unsigned rd :1;
376 unsigned tc :1;
377 unsigned aa :1;
378 unsigned opcode :4;
379 unsigned qr :1;
380 unsigned rcode :4;
381 unsigned cd: 1;
382 unsigned ad: 1;
383 unsigned unused :1;
384 unsigned ra :1;
385#endif
386 unsigned qdcount :16;
387 unsigned ancount :16;
388 unsigned nscount :16;
389 unsigned arcount :16;
390} HEADER;
391
392#define dn_ns_get16 bb_ns_get16
393static unsigned TINYRESOLVFUNC ns_get16(const unsigned char *cp)
394{
395 return cp[0]<<8 | cp[1];
396}
397#define ns_get32 bb_ns_get32
398static unsigned long TINYRESOLVFUNC ns_get32(const unsigned char *cp)
399{
400 return (unsigned)cp[0]<<24 | cp[1]<<16 | cp[2]<<8 | cp[3];
401}
402#define NS_GET16(s, cp) (void)((s) = ns_get16(((cp)+=2)-2))
403#define NS_GET32(l, cp) (void)((l) = ns_get32(((cp)+=4)-4))
404
405#define dn_expand bb_dn_expand
406static int BIGRESOLVFUNC dn_expand(const unsigned char *base, const unsigned char *end, const unsigned char *src, char *dest, int space)
407{
408 const unsigned char *p = src;
409 char *dend, *dbegin = dest;
410 int len = -1, i, j;
411 if (p==end || space <= 0) return -1;
412 dend = dest + (space > 254 ? 254 : space);
413 /* detect reference loop using an iteration counter */
414 for (i=0; i < end-base; i+=2) {
415 /* loop invariants: p<end, dest<dend */
416 if (*p & 0xc0) {
417 if (p+1==end) return -1;
418 j = ((p[0] & 0x3f) << 8) | p[1];
419 if (len < 0) len = p+2-src;
420 if (j >= end-base) return -1;
421 p = base+j;
422 } else if (*p) {
423 if (dest != dbegin) *dest++ = '.';
424 j = *p++;
425 if (j >= end-p || j >= dend-dest) return -1;
426 while (j--) *dest++ = *p++;
427 } else {
428 *dest = 0;
429 if (len < 0) len = p+1-src;
430 return len;
431 }
432 }
433 return -1;
434}
435
436#define ns_name_uncompress bb_ns_name_uncompress
437static int RESOLVFUNC ns_name_uncompress(const unsigned char *msg, const unsigned char *eom,
438 const unsigned char *src, char *dst, size_t dstsiz)
439{
440 int r;
441 r = dn_expand(msg, eom, src, dst, dstsiz);
442 if (r < 0) errno = EMSGSIZE;
443 return r;
444}
445
446#define dn_skipname bb_dn_skipname
447static int RESOLVFUNC dn_skipname(const unsigned char *s, const unsigned char *end)
448{
449 const unsigned char *p = s;
450 while (p < end)
451 if (!*p) return p-s+1;
452 else if (*p>=192)
453 if (p+1<end) return p-s+2;
454 else break;
455 else
456 if (end-p<*p+1) break;
457 else p += *p + 1;
458 return -1;
459}
460#define ns_skiprr bb_ns_skiprr
461static int BIGRESOLVFUNC ns_skiprr(const unsigned char *ptr, const unsigned char *eom, ns_sect section, int count)
462{
463 const unsigned char *p = ptr;
464 int r;
465
466 while (count--) {
467 r = dn_skipname(p, eom);
468 if (r < 0) goto bad;
469 if (r + 2 * NS_INT16SZ > eom - p) goto bad;
470 p += r + 2 * NS_INT16SZ;
471 if (section != ns_s_qd) {
472 if (NS_INT32SZ + NS_INT16SZ > eom - p) goto bad;
473 p += NS_INT32SZ;
474 NS_GET16(r, p);
475 if (r > eom - p) goto bad;
476 p += r;
477 }
478 }
479 return p - ptr;
480bad:
481 errno = EMSGSIZE;
482 return -1;
483}
484
485#define ns_parserr bb_ns_parserr
486static int BIGRESOLVFUNC ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr)
487{
488 int r;
489
490 if (section < 0 || section >= ns_s_max) goto bad;
491 if (section != handle->_sect) {
492 handle->_sect = section;
493 handle->_rrnum = 0;
494 handle->_msg_ptr = handle->_sections[section];
495 }
496 if (rrnum == -1) rrnum = handle->_rrnum;
497 if (rrnum < 0 || rrnum >= handle->_counts[section]) goto bad;
498 if (rrnum < handle->_rrnum) {
499 handle->_rrnum = 0;
500 handle->_msg_ptr = handle->_sections[section];
501 }
502 if (rrnum > handle->_rrnum) {
503 r = ns_skiprr(handle->_msg_ptr, handle->_eom, section, rrnum - handle->_rrnum);
504 if (r < 0) return -1;
505 handle->_msg_ptr += r;
506 handle->_rrnum = rrnum;
507 }
508 r = ns_name_uncompress(handle->_msg, handle->_eom, handle->_msg_ptr, rr->name, NS_MAXDNAME);
509 if (r < 0) return -1;
510 handle->_msg_ptr += r;
511 if (2 * NS_INT16SZ > handle->_eom - handle->_msg_ptr) goto size;
512 NS_GET16(rr->type, handle->_msg_ptr);
513 NS_GET16(rr->rr_class, handle->_msg_ptr);
514 if (section != ns_s_qd) {
515 if (NS_INT32SZ + NS_INT16SZ > handle->_eom - handle->_msg_ptr) goto size;
516 NS_GET32(rr->ttl, handle->_msg_ptr);
517 NS_GET16(rr->rdlength, handle->_msg_ptr);
518 if (rr->rdlength > handle->_eom - handle->_msg_ptr) goto size;
519 rr->rdata = handle->_msg_ptr;
520 handle->_msg_ptr += rr->rdlength;
521 } else {
522 rr->ttl = 0;
523 rr->rdlength = 0;
524 rr->rdata = NULL;
525 }
526 handle->_rrnum++;
527 if (handle->_rrnum > handle->_counts[section]) {
528 handle->_sect = section + 1;
529 if (handle->_sect == ns_s_max) {
530 handle->_rrnum = -1;
531 handle->_msg_ptr = NULL;
532 } else {
533 handle->_rrnum = 0;
534 }
535 }
536 return 0;
537bad:
538 errno = ENODEV;
539 return -1;
540size:
541 errno = EMSGSIZE;
542 return -1;
543}
544
545#define ns_initparse bb_ns_initparse
546static int BIGRESOLVFUNC ns_initparse(const unsigned char *msg, int msglen, ns_msg *handle)
547{
548 int i, r;
549
550 handle->_msg = msg;
551 handle->_eom = msg + msglen;
552 if (msglen < (2 + ns_s_max) * NS_INT16SZ) goto bad;
553 NS_GET16(handle->_id, msg);
554 NS_GET16(handle->_flags, msg);
555 for (i = 0; i < ns_s_max; i++) NS_GET16(handle->_counts[i], msg);
556 for (i = 0; i < ns_s_max; i++) {
557 if (handle->_counts[i]) {
558 handle->_sections[i] = msg;
559 r = ns_skiprr(msg, handle->_eom, i, handle->_counts[i]);
560 if (r < 0) return -1;
561 msg += r;
562 } else {
563 handle->_sections[i] = NULL;
564 }
565 }
566 if (msg != handle->_eom) goto bad;
567 handle->_sect = ns_s_max;
568 handle->_rrnum = -1;
569 handle->_msg_ptr = NULL;
570 return 0;
571bad:
572 errno = EMSGSIZE;
573 return -1;
574}
575
576#define res_mkquery bb_res_mkquery
577static int RESOLVFUNC res_mkquery(int op, const char *dname, int class, int type,
578 const unsigned char *data UNUSED_PARAM, int datalen UNUSED_PARAM,
579 const unsigned char *newrr UNUSED_PARAM, unsigned char *buf, int buflen)
580{
581 int i, j;
582 unsigned char q[280];
583 size_t l = strnlen(dname, 255);
584 int n;
585
586 if (l && dname[l-1]=='.') l--;
587 if (l && dname[l-1]=='.') return -1;
588 n = 17+l+!!l;
589 if (l>253 || buflen<n || op>15u || class>255u || type>255u)
590 return -1;
591
592 /* Construct query template - ID will be filled later */
593 memset(q, 0, n);
594 q[2] = op*8 + 1;
595 q[3] = 32; /* AD */
596 q[5] = 1;
597 memcpy((char *)q+13, dname, l);
598 for (i=13; q[i]; i=j+1) {
599 for (j=i; q[j] && q[j] != '.'; j++);
600 if (j-i-1u > 62u) return -1;
601 q[i-1] = j-i;
602 }
603 q[i+1] = type;
604 q[i+3] = class;
605#if 0
606//For some machines (here: a TP-Link RE200 powered by a MediaTek MT7620A)
607//the monotonic clock has a coarse resolution (here: 20us) and it can happen
608//that the requests for A and AAAA share the same transaction ID.
609
610//In that case the mapping from received responses to the sent queries
611//doesn't work and name resolution fails because the AAAA reply
612//is dropped as a duplicate reply to the A query.
613 /* Make a reasonably unpredictable id */
614 unsigned id;
615 struct timespec ts;
616 clock_gettime(CLOCK_REALTIME, &ts);
617 id = ts.tv_nsec + ((uint32_t)(ts.tv_nsec) >> 16);
618 q[0] = id/256;
619 q[1] = id;
620#endif
621 memcpy(buf, q, n);
622 return n;
623}
624
625#endif /* !USE_LIBC_RESOLV */
626
627
257struct ns { 628struct ns {
258 const char *name; 629 const char *name;
259 len_and_sockaddr *lsa; 630 len_and_sockaddr *lsa;
@@ -978,6 +1349,21 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
978 } 1349 }
979 } 1350 }
980 1351
1352 /* Ensure the Transaction IDs are unique.
1353 * See, for example, musl source of res_mkquery() where
1354 * it risks using current time (same value!) for ALL queries.
1355 */
1356 {
1357 struct timeval tv;
1358 unsigned id;
1359 xgettimeofday(&tv);
1360 id = tv.tv_sec + tv.tv_usec;
1361 for (rc = 0; rc < G.query_count; rc++) {
1362 G.query[rc].query[0] = id >> 8;
1363 G.query[rc].query[1] = id++;
1364 }
1365 }
1366
981 for (rc = 0; rc < G.serv_count;) { 1367 for (rc = 0; rc < G.serv_count;) {
982 int c; 1368 int c;
983 1369