public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 1/6] Correct some missing endian conversions of IPv4 addresses
Date: Fri,  4 Nov 2022 14:10:32 +1100	[thread overview]
Message-ID: <20221104031037.3866034-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20221104031037.3866034-1-david@gibson.dropbear.id.au>

The INADDR_LOOPBACK constant is in host endianness, and similarly the
IN_MULTICAST macro expects a host endian address.  However, there are some
places in passt where we use those with network endian values.  This means
that passt will incorrectly allow you to set 127.0.0.1 or a multicast
address as the guest address or DNS forwarding address.  Add the necessary
conversions to correct this.

INADDR_ANY and INADDR_BROADCAST logically behave the same way, although
because they're palindromes it doesn't have an effect in practice.  Change
them to be logically correct while we're there, though.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 conf.c | 26 +++++++++++++-------------
 icmp.c |  2 +-
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/conf.c b/conf.c
index 598c711..769df97 100644
--- a/conf.c
+++ b/conf.c
@@ -1164,11 +1164,11 @@ void conf(struct ctx *c, int argc, char **argv)
 			    !IN6_IS_ADDR_LOOPBACK(&c->ip6.dns_fwd))
 				break;
 
-			if (c->ip4.dns_fwd == INADDR_ANY		&&
+			if (c->ip4.dns_fwd == htonl(INADDR_ANY)		&&
 			    inet_pton(AF_INET, optarg, &c->ip4.dns_fwd)	&&
-			    c->ip4.dns_fwd != INADDR_ANY		&&
-			    c->ip4.dns_fwd != INADDR_BROADCAST		&&
-			    c->ip4.dns_fwd != INADDR_LOOPBACK)
+			    c->ip4.dns_fwd != htonl(INADDR_ANY)		&&
+			    c->ip4.dns_fwd != htonl(INADDR_BROADCAST)	&&
+			    c->ip4.dns_fwd != htonl(INADDR_LOOPBACK))
 				break;
 
 			err("Invalid DNS forwarding address: %s", optarg);
@@ -1363,12 +1363,12 @@ void conf(struct ctx *c, int argc, char **argv)
 			    !IN6_IS_ADDR_MULTICAST(&c->ip6.addr))
 				break;
 
-			if (c->ip4.addr == INADDR_ANY			&&
+			if (c->ip4.addr == htonl(INADDR_ANY)		&&
 			    inet_pton(AF_INET, optarg, &c->ip4.addr)	&&
-			    c->ip4.addr != INADDR_ANY			&&
-			    c->ip4.addr != INADDR_BROADCAST		&&
-			    c->ip4.addr != INADDR_LOOPBACK		&&
-			    !IN_MULTICAST(c->ip4.addr))
+			    c->ip4.addr != htonl(INADDR_ANY)		&&
+			    c->ip4.addr != htonl(INADDR_BROADCAST)	&&
+			    c->ip4.addr != htonl(INADDR_LOOPBACK)	&&
+			    !IN_MULTICAST(ntohl(c->ip4.addr)))
 				break;
 
 			err("Invalid address: %s", optarg);
@@ -1406,11 +1406,11 @@ void conf(struct ctx *c, int argc, char **argv)
 			    !IN6_IS_ADDR_LOOPBACK(&c->ip6.gw))
 				break;
 
-			if (c->ip4.gw == INADDR_ANY			&&
+			if (c->ip4.gw == htonl(INADDR_ANY)		&&
 			    inet_pton(AF_INET, optarg, &c->ip4.gw)	&&
-			    c->ip4.gw != INADDR_ANY			&&
-			    c->ip4.gw != INADDR_BROADCAST		&&
-			    c->ip4.gw != INADDR_LOOPBACK)
+			    c->ip4.gw != htonl(INADDR_ANY)		&&
+			    c->ip4.gw != htonl(INADDR_BROADCAST)	&&
+			    c->ip4.gw != htonl(INADDR_LOOPBACK))
 				break;
 
 			err("Invalid gateway address: %s", optarg);
diff --git a/icmp.c b/icmp.c
index 233acf9..125f314 100644
--- a/icmp.c
+++ b/icmp.c
@@ -148,7 +148,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
 		union icmp_epoll_ref iref = { .icmp.v6 = 0 };
 		struct sockaddr_in sa = {
 			.sin_family = AF_INET,
-			.sin_addr = { .s_addr = INADDR_ANY },
+			.sin_addr = { .s_addr = htonl(INADDR_ANY) },
 		};
 		struct icmphdr *ih;
 		int id, s;
-- 
@@ -148,7 +148,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
 		union icmp_epoll_ref iref = { .icmp.v6 = 0 };
 		struct sockaddr_in sa = {
 			.sin_family = AF_INET,
-			.sin_addr = { .s_addr = INADDR_ANY },
+			.sin_addr = { .s_addr = htonl(INADDR_ANY) },
 		};
 		struct icmphdr *ih;
 		int id, s;
-- 
2.38.1


  reply	other threads:[~2022-11-04  3:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04  3:10 [PATCH v2 0/6] Improve IPv4 address endian handling and related bug fixes David Gibson
2022-11-04  3:10 ` David Gibson [this message]
2022-11-04  3:10 ` [PATCH v2 2/6] Minor improvements to IPv4 netmask handling David Gibson
2022-11-04  3:10 ` [PATCH v2 3/6] Use IPV4_IS_LOOPBACK more widely David Gibson
2022-11-04  3:10 ` [PATCH v2 4/6] Use typing to reduce chances of IPv4 endianness errors David Gibson
2022-11-04  3:10 ` [PATCH v2 5/6] Use endian-safer typing in struct tap4_l4_t David Gibson
2022-11-04  3:10 ` [PATCH v2 6/6] tcp: Correct function comments for address types David Gibson

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=20221104031037.3866034-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).