netfilter: xt_hashlimit: validate hashtable supports XT_HASHLIMIT_RATE_MATCH
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v3 1/1 netfilter: xt_hashlimit: validate hashtable supports XT_HASHLIMIT_RATE_MATCH
The XT_HASHLIMIT_RATE_MATCH flag mode changes the semantics of the dsthash_ent structure which represents an entry in the hashtable. There is a union area which uses a different layout to express the rate match mode.
Update .checkentry path to validate the XT_HASHLIMIT_RATE_MATCH mode flag is requested by two or more different rules that refer to the same hashtable. Otherwise, uninitialized access to the burst field in the union is possible.
Reject the use of the XT_HASHLIMIT_RATE_MATCH mode flag if set on by revision less than 3 too.
---
v3: reject XT_HASHLIMIT_RATE_MATCH for revision less than 3 too.
net/netfilter/xt_hashlimit.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
net/netfilter/xt_hashlimit.c +15 -1
@@ -117,6 +117,7 @@ struct xt_hashlimit_htable {refcount_t use;u_int8_t family;bool rnd_initialized; +bool ratematch;struct hashlimit_cfg3 cfg;/* config */
@@ -323,6 +324,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg,kvfree (hinfo);return -ENOMEM ; } + hinfo->ratematch = !!(cfg->mode &XT_HASHLIMIT_RATE_MATCH );spin_lock_init (&hinfo->lock);switch (revision) {
@@ -872,7 +874,10 @@ static int hashlimit_mt_check_common(const struct xt_mtchk_param *par, }/* Check for overflow. */ -if (revision >=3 && cfg->mode &XT_HASHLIMIT_RATE_MATCH ) { +if (cfg->mode &XT_HASHLIMIT_RATE_MATCH ) { +if (revision <3 ) +return -EINVAL ; +if (cfg->avg ==0 || cfg->avg >U32_MAX ) {pr_info_ratelimited ("invalid rate\n" );return -ERANGE ;
@@ -905,6 +910,15 @@ static int hashlimit_mt_check_common(const struct xt_mtchk_param *par,mutex_unlock (&hashlimit_mutex);return ret; } + }else { +if ((cfg->mode &XT_HASHLIMIT_RATE_MATCH && + !(*hinfo)->ratematch) || + (!(cfg->mode &XT_HASHLIMIT_RATE_MATCH ) && + (*hinfo)->ratematch)) { +mutex_unlock (&hashlimit_mutex); +htable_put (*hinfo); +return -EINVAL ; + } }mutex_unlock (&hashlimit_mutex);
Signature · 1 line
2.47.3
Quoted 1 line · Pablo Neira Ayuso
[PATCH nf,v3] netfilter:
xt_hashlimit: validate hashtable supportsXT_HASHLIMIT_RATE_MATCH
Hi Pablo,
Sorry for the delayed reply, I am based in Turkey (UTC+3) and saw your v3 this morning.
I tested v3 on Debian 12 (kernel 6.1.0-50-amd64) by building the patched module against the 6.1.176 headers.
Before patch: both a rule without XT_HASHLIMIT_RATE_MATCH and a second rule with XT_HASHLIMIT_RATE_MATCH sharing the same table name were accepted silently (exit 0), leaving rateinfo.burst uninitialized.
After patch: the conflicting rule is correctly rejected at .checkentry:
iptables v1.8.9 (nf_tables): RULE_APPEND failed (Invalid argument): rule in chain INPUT
Thanks,
Talha Berk Arslan
Artfical Security Intelligence