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>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH gwproxy v1 2/2] dns: validate restyp on initialization
Date: Mon, 28 Jul 2025 23:07:58 +0700	[thread overview]
Message-ID: <20250728160800.95383-3-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250728160800.95383-1-reyuki@gnuweeb.org>

Return EINVAL on initialization if restyp is invalid.
And then prefer ipv4 if GWP_DNS_RESTYP_DEFAULT is used.

Fixes: 82c86a7256d6 ("gwproxy/dns: Replace the old DNS cache system with a new one.")
Signed-off-by: Ahmad Gani <reyuki@gnuweeb.org>
---
 src/gwproxy/dns.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/gwproxy/dns.c b/src/gwproxy/dns.c
index f7cbcb2d6e89..b189b33127af 100644
--- a/src/gwproxy/dns.c
+++ b/src/gwproxy/dns.c
@@ -619,7 +619,8 @@ static int fetch_addr(struct gwp_dns_cache_entry *e, struct gwp_sockaddr *addr,
 			if (!fetch_i4(e, addr, port))
 				return -EHOSTUNREACH;
 		}
-	} else if (restyp == GWP_DNS_RESTYP_PREFER_IPV4) {
+	} else if (restyp == GWP_DNS_RESTYP_PREFER_IPV4 ||
+		   restyp == GWP_DNS_RESTYP_DEFAULT) {
 		if (!fetch_i4(e, addr, port)) {
 			if (!fetch_i6(e, addr, port))
 				return -EHOSTUNREACH;
@@ -679,11 +680,28 @@ static void free_cache(struct gwp_dns_cache *cache)
 	cache = NULL;
 }
 
+static inline bool validate_restyp(int restyp)
+{
+	switch (restyp) {
+	case GWP_DNS_RESTYP_DEFAULT:
+	case GWP_DNS_RESTYP_IPV4_ONLY:
+	case GWP_DNS_RESTYP_IPV6_ONLY:
+	case GWP_DNS_RESTYP_PREFER_IPV4:
+	case GWP_DNS_RESTYP_PREFER_IPV6:
+		return true;
+	default:
+		return false;
+	}
+}
+
 int gwp_dns_ctx_init(struct gwp_dns_ctx **ctx_p, const struct gwp_dns_cfg *cfg)
 {
 	struct gwp_dns_ctx *ctx;
 	int r;
 
+	if (!validate_restyp(cfg->restyp))
+		return -EINVAL;
+
 	ctx = calloc(1, sizeof(*ctx));
 	if (!ctx)
 		return -ENOMEM;
-- 
Ahmad Gani


  parent reply	other threads:[~2025-07-28 16:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-28 16:07 [PATCH gwproxy v1 0/2] Fix missing validation of restyp in dns Ahmad Gani
2025-07-28 16:07 ` [PATCH gwproxy v1 1/2] dns: add a new restyp GWP_DNS_RESTYP_DEFAULT Ahmad Gani
2025-07-28 16:07 ` Ahmad Gani [this message]
2025-07-28 16:14 ` [PATCH gwproxy v1 0/2] Fix missing validation of restyp in dns 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=20250728160800.95383-3-reyuki@gnuweeb.org \
    --to=reyuki@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