Skip to contentSkip to conversation

netfilter stable fix for 4.14

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Aug 12, 2023 – Aug 13, 2023

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover netfilter stable fix for 4.14

Hi Greg, Sasha,

This is a backport of:

1689f25924ad ("netfilter: nf_tables: report use refcount overflow")

for -stable 4.14.

Please, apply.

Thanks.

Pablo Neira Ayuso (1):
netfilter: nf_tables: report use refcount overflow

 include/net/netfilter/nf_tables.h |  27 +++++-
 net/netfilter/nf_tables_api.c     | 143 +++++++++++++++++++-----------
 net/netfilter/nft_objref.c        |   8 +-
 3 files changed, 119 insertions(+), 59 deletions(-)

--
2.30.2

author maintainer
#1 lore raw

PATCH 1/1 netfilter: nf_tables: report use refcount overflow

commit 1689f25924ada8fe14a4a82c38925d04994c7142 upstream.

Overflow use refcount checks are not complete.

Add helper function to deal with object reference counter tracking. Report -EMFILE in case UINT_MAX is reached.

nft_use_dec() splats in case that reference counter underflows, which should not ever happen.

Add nft_use_inc_restore() and nft_use_dec_restore() which are used to restore reference counter from error and abort paths.

Use u32 in nft_flowtable and nft_object since helper functions cannot work on bitfields.

Remove the few early incomplete checks now that the helper functions are in place and used to check for refcount overflow.

Fixes 96518518cc41 ("netfilter: add nftables")
---
 include/net/netfilter/nf_tables.h |  27 +++++-
 net/netfilter/nf_tables_api.c     | 143 +++++++++++++++++++-----------
 net/netfilter/nft_objref.c        |   8 +-
 3 files changed, 119 insertions(+), 59 deletions(-)
include/net/netfilter/nf_tables.h +25 -2
index d5e933e6a611..e71e155f3b88 100644
@@ -968,6 +968,29 @@ int __nft_release_basechain(struct nft_ctx *ctx);
 
 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
 
+static inline bool nft_use_inc(u32 *use)
+{
+	if (*use == UINT_MAX)
+		return false;
+
+	(*use)++;
+
+	return true;
+}
+
+static inline void nft_use_dec(u32 *use)
+{
+	WARN_ON_ONCE((*use)-- == 0);
+}
+
+/* For error and abort path: restore use counter to previous state. */
+static inline void nft_use_inc_restore(u32 *use)
+{
+	WARN_ON_ONCE(!nft_use_inc(use));
+}
+
+#define nft_use_dec_restore	nft_use_dec
+
 /**
  *	struct nft_table - nf_tables table
  *
@@ -1051,8 +1074,8 @@ struct nft_object {
 	struct list_head		list;
 	char				*name;
 	struct nft_table		*table;
-	u32				genmask:2,
-					use:30;
+	u32				genmask:2;
+	u32				use;
 	/* runtime data below here */
 	const struct nft_object_ops	*ops ____cacheline_aligned;
 	unsigned char			data[]
net/netfilter/nf_tables_api.c +89 -54

443 lines not on this page · raw message

net/netfilter/nft_objref.c +5 -3
index 8ad0be6c53fd..f3c5d4b14657 100644
@@ -43,8 +43,10 @@ static int nft_objref_init(const struct nft_ctx *ctx,
 	if (IS_ERR(obj))
 		return -ENOENT;
 
+	if (!nft_use_inc(&obj->use))
+		return -EMFILE;
+
 	nft_objref_priv(expr) = obj;
-	obj->use++;
 
 	return 0;
 }
@@ -73,7 +75,7 @@ static void nft_objref_deactivate(const struct nft_ctx *ctx,
 	if (phase == NFT_TRANS_COMMIT)
 		return;
 
-	obj->use--;
+	nft_use_dec(&obj->use);
 }
 
 static void nft_objref_activate(const struct nft_ctx *ctx,
@@ -81,7 +83,7 @@ static void nft_objref_activate(const struct nft_ctx *ctx,
 {
 	struct nft_object *obj = nft_objref_priv(expr);
 
-	obj->use++;
+	nft_use_inc_restore(&obj->use);
 }
 
 static struct nft_expr_type nft_objref_type;
Signature · 1 line
2.30.2
maintainer
#2 lore raw
In reply to Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso

Hi Greg, Sasha,

This is a backport of:

1689f25924ad ("netfilter: nf_tables: report use refcount overflow")

for -stable 4.14.

Please, apply.

Thanks.

Pablo Neira Ayuso (1):
netfilter: nf_tables: report use refcount overflow

Quoted 7 lines
 include/net/netfilter/nf_tables.h |  27 +++++-
 net/netfilter/nf_tables_api.c     | 143 +++++++++++++++++++-----------
 net/netfilter/nft_objref.c        |   8 +-
 3 files changed, 119 insertions(+), 59 deletions(-)

--
2.30.2

All now queued up, thanks!

greg k-h