All of lore.kernel.org
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: netfilter-devel@vger.kernel.org
Subject: [iptables PATCH] libxtables: Promote xtopt_esize_by_type() as xtopt_psize getter
Date: Fri, 18 Jul 2025 18:00:32 +0200	[thread overview]
Message-ID: <20250718160032.30444-1-phil@nwl.cc> (raw)

Apart from supporting range-types, this getter is convenient to sanitize
array out of bounds access. Use it in xtables_option_metavalidate() to
simplify the code a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 libxtables/xtoptions.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/libxtables/xtoptions.c b/libxtables/xtoptions.c
index 64d6599af904b..ecaea4ec16cc9 100644
--- a/libxtables/xtoptions.c
+++ b/libxtables/xtoptions.c
@@ -145,8 +145,11 @@ static size_t xtopt_esize_by_type(enum xt_option_type type)
 	case XTTYPE_UINT64RC:
 		return xtopt_psize[XTTYPE_UINT64];
 	default:
-		return xtopt_psize[type];
+		break;
 	}
+	if (type < ARRAY_SIZE(xtopt_psize))
+		return xtopt_psize[type];
+	return 0;
 }
 
 static uint64_t htonll(uint64_t val)
@@ -886,6 +889,8 @@ void xtables_option_parse(struct xt_option_call *cb)
 void xtables_option_metavalidate(const char *name,
 				 const struct xt_option_entry *entry)
 {
+	size_t psize;
+
 	for (; entry->name != NULL; ++entry) {
 		if (entry->id >= CHAR_BIT * sizeof(unsigned int) ||
 		    entry->id >= XT_OPTION_OFFSET_SCALE)
@@ -900,19 +905,18 @@ void xtables_option_metavalidate(const char *name,
 					"Oversight?", name, entry->name);
 			continue;
 		}
-		if (entry->type >= ARRAY_SIZE(xtopt_psize) ||
-		    xtopt_psize[entry->type] == 0)
+
+		psize = xtopt_esize_by_type(entry->type);
+		if (!psize)
 			xt_params->exit_err(OTHER_PROBLEM,
 				"%s: entry type of option \"--%s\" cannot be "
 				"combined with XTOPT_PUT\n",
 				name, entry->name);
-		if (xtopt_psize[entry->type] != -1 &&
-		    xtopt_psize[entry->type] != entry->size)
+		else if (psize != -1 && psize != entry->size)
 			xt_params->exit_err(OTHER_PROBLEM,
 				"%s: option \"--%s\" points to a memory block "
 				"of wrong size (expected %zu, got %zu)\n",
-				name, entry->name,
-				xtopt_psize[entry->type], entry->size);
+				name, entry->name, psize, entry->size);
 	}
 }
 
-- 
2.49.0


             reply	other threads:[~2025-07-18 16:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-18 16:00 Phil Sutter [this message]
2025-07-21 17:57 ` [iptables PATCH] libxtables: Promote xtopt_esize_by_type() as xtopt_psize getter Florian Westphal
2025-07-22  8:34   ` Phil Sutter

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=20250718160032.30444-1-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.