Skip to contentSkip to conversation

netfilter: xt_recent: Allow for much larger hitcount values

PATCH Superseded Phil Sutter 3 participants 6 messages Jun 13, 2024 – Jun 17, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover netfilter: xt_recent: Allow for much larger hitcount values

Patch 2 lifts the restriction of 255 as max hitcount value by adjusting XT_RECENT_MAX_NSTAMPS value and increasing required struct field sizes accordingly.

In struct recent_entry, field 'nstamps' was 16bit in size of unclear reasons. Patch 1 changes that to match field 'index' providing rationale why it is sufficient, thus pavin the way for keeping both at 32bit (and avoiding a larger size for 'nstamps').

Phil Sutter (2):
netfilter: xt_reent: Reduce size of struct recent_entry::nstamps
netfilter: xt_recent: Largely lift restrictions on max hitcount value

 net/netfilter/xt_recent.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 1/2 netfilter: xt_reent: Reduce size of struct recent_entry::nstamps

There is no point in this change besides presenting its possibility separate from a follow-up patch extending the size of both 'index' and 'nstamps' fields.

The value of 'nstamps' is initialized to 1 in recent_entry_init() and adjusted in recent_entry_update() to match that of 'index' if it becomes larger after being incremented. Since 'index' is of type u8, it will at max become 255 (and wrap to 0 afterwards). Therefore, 'nstamps' will also never exceed the value 255.

---
 net/netfilter/xt_recent.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/xt_recent.c +1 -1
index ef93e0d3bee0..60259280b2d5 100644
@@ -70,7 +70,7 @@ struct recent_entry {
 	u_int16_t		family;
 	u_int8_t		ttl;
 	u_int8_t		index;
-	u_int16_t		nstamps;
+	u_int8_t		nstamps;
 	unsigned long		stamps[];
 };
 
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH 2/2 netfilter: xt_recent: Largely lift restrictions on max hitcount value

In reply to Phil Sutter

Support tracking of up to 2^32-1 packets per table. Since users provide the hitcount value in a __u32 variable, they can't exceed the max value anymore.

---
 net/netfilter/xt_recent.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
net/netfilter/xt_recent.c +5 -10
index 60259280b2d5..77ac4964e2dc 100644
@@ -59,9 +59,9 @@ MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* fil
 /* retained for backwards compatibility */
 static unsigned int ip_pkt_list_tot __read_mostly;
 module_param(ip_pkt_list_tot, uint, 0400);
-MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
+MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 2^32 - 1)");
 
-#define XT_RECENT_MAX_NSTAMPS	256
+#define XT_RECENT_MAX_NSTAMPS	(1ULL << 32)
 
 struct recent_entry {
 	struct list_head	list;
@@ -69,8 +69,8 @@ struct recent_entry {
 	union nf_inet_addr	addr;
 	u_int16_t		family;
 	u_int8_t		ttl;
-	u_int8_t		index;
-	u_int8_t		nstamps;
+	u_int32_t		index;
+	u_int32_t		nstamps;
 	unsigned long		stamps[];
 };
 
@@ -80,7 +80,7 @@ struct recent_table {
 	union nf_inet_addr	mask;
 	unsigned int		refcnt;
 	unsigned int		entries;
-	u8			nstamps_max_mask;
+	uint32_t		nstamps_max_mask;
 	struct list_head	lru_list;
 	struct list_head	iphash[];
 };
@@ -360,11 +360,6 @@ static int recent_mt_check(const struct xt_mtchk_param *par,
 		return -EINVAL;
 	if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
 		return -EINVAL;
-	if (info->hit_count >= XT_RECENT_MAX_NSTAMPS) {
-		pr_info_ratelimited("hitcount (%u) is larger than allowed maximum (%u)\n",
-				    info->hit_count, XT_RECENT_MAX_NSTAMPS - 1);
-		return -EINVAL;
-	}
 	ret = xt_check_proc_name(info->name, sizeof(info->name));
 	if (ret)
 		return ret;
Signature · 1 line
2.43.0
maintainer
#3 lore raw

Re: [nf-next PATCH 2/2] netfilter: xt_recent: Largely lift restrictions on max hitcount value

Quoted 16 lines · Phil Sutter

Support tracking of up to 2^32-1 packets per table. Since users provide the hitcount value in a __u32 variable, they can't exceed the max value anymore.

---
 net/netfilter/xt_recent.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
net/netfilter/xt_recent.c +0 -0
index 60259280b2d5..77ac4964e2dc 100644

net/netfilter/xt_recent.c MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* fil · line 64

Quoted 8 lines
net/netfilter/xt_recent.c +2 -2
@@ -59,9 +59,9 @@ MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* fil
 /* retained for backwards compatibility */
 static unsigned int ip_pkt_list_tot __read_mostly;
 module_param(ip_pkt_list_tot, uint, 0400);
-MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
+MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 2^32 - 1)");
 
-#define XT_RECENT_MAX_NSTAMPS	256
+#define XT_RECENT_MAX_NSTAMPS	(1ULL << 32)

Won't that allow massive mem hog?

Actually I think this is already a mem hog, unbounded allocations from time where we had no untrusted netns :-(

author reviewer
#4 lore raw

Re: [nf-next PATCH 2/2] netfilter: xt_recent: Largely lift restrictions on max hitcount value

Quoted 23 lines · Florian Westphal
Quoted 22 lines · Phil Sutter

Support tracking of up to 2^32-1 packets per table. Since users provide the hitcount value in a __u32 variable, they can't exceed the max value anymore.

---
 net/netfilter/xt_recent.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
net/netfilter/xt_recent.c +1 -1
index 60259280b2d5..77ac4964e2dc 100644
@@ -59,9 +59,9 @@ MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* fil
 /* retained for backwards compatibility */
 static unsigned int ip_pkt_list_tot __read_mostly;
 module_param(ip_pkt_list_tot, uint, 0400);
-MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
+MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 2^32 - 1)");
 
Quoted 4 lines

net/netfilter/xt_recent.c MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* fil · line 64

Quoted 2 lines
net/netfilter/xt_recent.c +1 -1
-#define XT_RECENT_MAX_NSTAMPS	256
+#define XT_RECENT_MAX_NSTAMPS	(1ULL << 32)

Won't that allow massive mem hog?

You're right, struct recent_entry may become ~32GB in size.

Quoted 2 lines

Actually I think this is already a mem hog, unbounded allocations from time where we had no untrusted netns :-(

With the current max of 255 stamps, entries are at max 1KB in size. Is this bad already? Given unrestricted rule counts, there are various ways to cause large memory allocation, no?

How about restricting MAX_NSTAMPS to 1<<16? Max entry size is 568B, a little less insane than th 32GB I thoughtlessly proposed above. :)

Cheers, Phil

#5 lore raw

Re: [nf-next PATCH 2/2] netfilter: xt_recent: Largely lift restrictions on max hitcount value

In reply to Phil Sutter
kernel test robot noticed the following build warnings:

Hi Phil,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Phil-Sutter/netfilter-xt_reent-Reduce-size-of-struct-recent_entry-nstamps/20240613-223955
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git master
patch link:    https://lore.kernel.org/r/20240613143254.26622-3-phil%40nwl.cc
patch subject: [nf-next PATCH 2/2] netfilter: xt_recent: Largely lift restrictions on max hitcount value
config: s390-defconfig (https://download.01.org/0day-ci/archive/20240618/[email protected]/config)
compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project 78ee473784e5ef6f0b19ce4cb111fb6e4d23c6b2)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240618/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from net/netfilter/xt_recent.c:14:
In file included from include/linux/ip.h:16:
In file included from include/linux/skbuff.h:17:
In file included from include/linux/bvec.h:10:
In file included from include/linux/highmem.h:9:
In file included from include/linux/mm.h:1719:
include/linux/vmstat.h:508:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     508 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     509 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:515:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     515 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     516 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:522:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     522 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:527:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     527 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     528 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:536:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     536 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     537 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   In file included from net/netfilter/xt_recent.c:14:
   In file included from include/linux/ip.h:16:
   In file included from include/linux/skbuff.h:31:
   In file included from include/linux/dma-mapping.h:10:
   In file included from include/linux/scatterlist.h:9:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:464:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     464 |         val = __raw_readb(PCI_IOBASE + addr);
         |                           ~~~~~~~~~~ ^
   include/asm-generic/io.h:477:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     477 |         val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
      37 | #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
         |                                                           ^
   include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
     102 | #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
         |                                                      ^
   In file included from net/netfilter/xt_recent.c:14:
   In file included from include/linux/ip.h:16:
   In file included from include/linux/skbuff.h:31:
   In file included from include/linux/dma-mapping.h:10:
   In file included from include/linux/scatterlist.h:9:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:490:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     490 |         val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
      35 | #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
         |                                                           ^
   include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
     115 | #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
         |                                                      ^
   In file included from net/netfilter/xt_recent.c:14:
   In file included from include/linux/ip.h:16:
   In file included from include/linux/skbuff.h:31:
   In file included from include/linux/dma-mapping.h:10:
   In file included from include/linux/scatterlist.h:9:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:501:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     501 |         __raw_writeb(value, PCI_IOBASE + addr);
         |                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:511:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     511 |         __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:521:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     521 |         __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:609:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     609 |         readsb(PCI_IOBASE + addr, buffer, count);
         |                ~~~~~~~~~~ ^
   include/asm-generic/io.h:617:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     617 |         readsw(PCI_IOBASE + addr, buffer, count);
         |                ~~~~~~~~~~ ^
   include/asm-generic/io.h:625:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     625 |         readsl(PCI_IOBASE + addr, buffer, count);
         |                ~~~~~~~~~~ ^
   include/asm-generic/io.h:634:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     634 |         writesb(PCI_IOBASE + addr, buffer, count);
         |                 ~~~~~~~~~~ ^
   include/asm-generic/io.h:643:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     643 |         writesw(PCI_IOBASE + addr, buffer, count);
         |                 ~~~~~~~~~~ ^
   include/asm-generic/io.h:652:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     652 |         writesl(PCI_IOBASE + addr, buffer, count);
         |                 ~~~~~~~~~~ ^
>> net/netfilter/xt_recent.c:738:38: warning: result of comparison of constant 4294967296 with expression of type 'unsigned int' is always false [-Wtautological-constant-out-of-range-compare]
     738 |         if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS)
         |                             ~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~

18 warnings generated.

vim +738 net/netfilter/xt_recent.c

^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  731  
d3c5ee6d545b53 net/ipv4/netfilter/ipt_recent.c Jan Engelhardt   2007-12-04  732  static int __init recent_mt_init(void)
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  733  {
404bdbfd242cb9 net/ipv4/netfilter/ipt_recent.c Patrick McHardy  2006-05-29  734  	int err;
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  735  
abc86d0f99242b net/netfilter/xt_recent.c       Florian Westphal 2014-11-24  736  	BUILD_BUG_ON_NOT_POWER_OF_2(XT_RECENT_MAX_NSTAMPS);
abc86d0f99242b net/netfilter/xt_recent.c       Florian Westphal 2014-11-24  737  
abc86d0f99242b net/netfilter/xt_recent.c       Florian Westphal 2014-11-24 @738  	if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS)
404bdbfd242cb9 net/ipv4/netfilter/ipt_recent.c Patrick McHardy  2006-05-29  739  		return -EINVAL;
404bdbfd242cb9 net/ipv4/netfilter/ipt_recent.c Patrick McHardy  2006-05-29  740  	ip_list_hash_size = 1 << fls(ip_list_tot);
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  741  
7d07d5632b672c net/netfilter/xt_recent.c       Alexey Dobriyan  2010-01-18  742  	err = register_pernet_subsys(&recent_net_ops);
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  743  	if (err)
404bdbfd242cb9 net/ipv4/netfilter/ipt_recent.c Patrick McHardy  2006-05-29  744  		return err;
7d07d5632b672c net/netfilter/xt_recent.c       Alexey Dobriyan  2010-01-18  745  	err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
7d07d5632b672c net/netfilter/xt_recent.c       Alexey Dobriyan  2010-01-18  746  	if (err)
7d07d5632b672c net/netfilter/xt_recent.c       Alexey Dobriyan  2010-01-18  747  		unregister_pernet_subsys(&recent_net_ops);
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  748  	return err;
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  749  }
^1da177e4c3f41 net/ipv4/netfilter/ipt_recent.c Linus Torvalds   2005-04-16  750  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki