public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Ahmad Gani <reyuki@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Ahmad Gani <reyuki@gnuweeb.org>,
	Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH gwproxy v6 05/11] dnsparser: Ignore CNAME if any
Date: Thu, 28 Aug 2025 21:34:27 +0700	[thread overview]
Message-ID: <20250828143444.540247-6-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250828143444.540247-1-reyuki@gnuweeb.org>

Previously the parser will abort the program if it detects a type other
than A or AAAA record.

and other minor changes

Signed-off-by: Ahmad Gani <reyuki@gnuweeb.org>
---
 src/gwproxy/dnsparser.c | 63 +++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/src/gwproxy/dnsparser.c b/src/gwproxy/dnsparser.c
index 7b3a3c981ea7..cb2a090dfbb0 100644
--- a/src/gwproxy/dnsparser.c
+++ b/src/gwproxy/dnsparser.c
@@ -20,7 +20,7 @@ static ssize_t construct_qname(uint8_t *dst, size_t dst_len, const char *qname)
 			return -ENAMETOOLONG;
 
 		if (c == '.' || c == '\0') {
-			if (l < 1 || l > 255)
+			if (l < 1 || l > DOMAIN_LABEL_LIMIT)
 				return -EINVAL;
 
 			*lp = (uint8_t)l;
@@ -51,14 +51,19 @@ static int calculate_question_len(uint8_t *in, size_t in_len)
 		}
 
 		if (tot_len >= (int)in_len)
-			return -ENAMETOOLONG;
+			return -ENOBUFS;
 
 		advance_len = *p + 1;
 		tot_len += advance_len;
 		p += advance_len;
 	}
 
+	if (tot_len > DOMAIN_NAME_LIMIT)
+		return -ENAMETOOLONG;
+
 	tot_len += 4;
+	if (tot_len >= (int)in_len)
+		return -ENOBUFS;
 
 	return  tot_len;
 }
@@ -133,7 +138,8 @@ static int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_
 		}
 
 		memcpy(&is_compressed, &in[idx], sizeof(is_compressed));
-		is_compressed = DNS_IS_COMPRESSED(ntohs(is_compressed));
+		is_compressed = ntohs(is_compressed);
+		is_compressed = DNS_IS_COMPRESSED(is_compressed);
 		assert(is_compressed);
 		idx += 2; // NAME
 		if (idx >= in_len) {
@@ -169,21 +175,33 @@ static int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_
 
 		memcpy(&rdlength, &in[idx], sizeof(rdlength));
 		rdlength = ntohs(rdlength);
-		if (item->rr_type != TYPE_AAAA && item->rr_type != TYPE_A) {
-			ret = -EINVAL;
-			free(item);
-			goto exit_free;
-		}
-		if (item->rr_type == TYPE_AAAA && rdlength != sizeof(struct in6_addr)) {
-			ret = -EINVAL;
+		switch (item->rr_type) {
+		case TYPE_AAAA:
+			if (rdlength != sizeof(struct in6_addr)) {
+				ret = -EINVAL;
+				free(item);
+				goto exit_free;
+			}
+			break;
+		case TYPE_A:
+			if (rdlength != sizeof(struct in_addr)) {
+				ret = -EINVAL;
+				free(item);
+				goto exit_free;
+			}
+			break;
+		case TYPE_CNAME:
+			idx += 2 + rdlength;
 			free(item);
-			goto exit_free;
-		}
-		if (item->rr_type == TYPE_A && rdlength != sizeof(struct in_addr)) {
+			continue;
+
+		default:
 			ret = -EINVAL;
 			free(item);
 			goto exit_free;
+			break;
 		}
+
 		item->rdlength = rdlength;
 		idx += 2;
 		if (idx >= in_len) {
@@ -192,13 +210,6 @@ static int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_
 			goto exit_free;
 		}
 
-		/*
-		 * considering if condition above,
-		 * maybe we don't need a malloc and just allocate fixed size
-		 * for rdata? however if this parser want to be expanded for
-		 * other dns operation (e.g OPCODE_IQUERY, etc), rdata maybe
-		 * contain more than sizeof in6_addr.
-		 */
 		ptr = malloc(rdlength);
 		if (!ptr) {
 			ret = -ENOMEM;
@@ -216,10 +227,15 @@ static int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_
 		}
 
 		item->rdata = ptr;
-		out->rr_answ[i] = item;
+		out->rr_answ[out->hdr.ancount] = item;
 		out->hdr.ancount++;
 	}
 
+	if (!out->hdr.ancount) {
+		free(out->rr_answ);
+		return -ENODATA;
+	}
+
 	return 0;
 exit_free:
 	for (i = 0; i < out->hdr.ancount; i++) {
@@ -258,10 +274,7 @@ int gwdns_parse_query(uint16_t txid, const char *service,
 	if (r)
 		return r;
 
-	if (!raw_answ.hdr.ancount)
-		goto exit_free;
-
-	tail = NULL;
+	results = tail = NULL;
 	for (i = 0; i < raw_answ.hdr.ancount; i++) {
 		struct gwdns_addrinfo_node *new_node;
 		gwdns_serialized_answ *answ;
-- 
Ahmad Gani


  parent reply	other threads:[~2025-08-28 14:36 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-28 14:34 [PATCH gwproxy v6 00/11] Initial work on integration of DNS parser lib in gwproxy Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 01/11] gwproxy: Fix syntax error inside assertion Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 02/11] gwproxy: Fix socks5 failure on debug mode Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 03/11] dnsparser: Add dns parser code Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 04/11] dnsparser: remove unused constant Ahmad Gani
2025-08-28 14:34 ` Ahmad Gani [this message]
2025-08-28 14:34 ` [PATCH gwproxy v6 06/11] dns: Remove code block related to the usage of glibc's getaddrinfo_a function Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 07/11] dns: refactor dns.c to integrate the dns parser Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 08/11] dns: revert removed DNS code and disable raw DNS by default Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 09/11] test: revert DNS test-case Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 10/11] gwproxy: Add DNS server option Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 11/11] dns: Add fallback mechanism for raw DNS Ahmad Gani
2025-08-28 21:52 ` [PATCH gwproxy v6 00/11] Initial work on integration of DNS parser lib in gwproxy Ammar Faizi
2025-08-28 22:23 ` Ammar Faizi
2025-08-29  0:54   ` Ahmad Gani
2025-08-29  1:59     ` Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250828143444.540247-6-reyuki@gnuweeb.org \
    --to=reyuki@gnuweeb.org \
    --cc=alviro.iskandar@gnuweeb.org \
    --cc=ammarfaizi2@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox