Skip to contentSkip to conversation

netfilter: nf_tables: support updating userdata for nft_table

PATCH In review Quan Tian 2 participants 2 messages Mar 10, 2024

Subsystems (2)
lore
Share & tools
lore

Quan Tian

author
#0 lore raw

PATCH 1/1 netfilter: nf_tables: support updating userdata for nft_table

The NFTA_TABLE_USERDATA attribute was ignored on updates. The patch adds handling for it to support table comment updates.

---
 include/net/netfilter/nf_tables.h |  6 ++++
 net/netfilter/nf_tables_api.c     | 53 +++++++++++++++++++++++--------
 2 files changed, 45 insertions(+), 14 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
index 510244cc0f8f..4406c056f52f 100644
@@ -1680,10 +1680,16 @@ struct nft_trans_chain {
 
 struct nft_trans_table {
 	bool				update;
+	u16				udlen;
+	u8				*udata;
 };
 
 #define nft_trans_table_update(trans)	\
 	(((struct nft_trans_table *)trans->data)->update)
+#define nft_trans_table_udlen(trans)	\
+	(((struct nft_trans_table *)trans->data)->udlen)
+#define nft_trans_table_udata(trans)	\
+	(((struct nft_trans_table *)trans->data)->udata)
 
 struct nft_trans_elem {
 	struct nft_set			*set;
net/netfilter/nf_tables_api.c +39 -14

91 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-145)
maintainer
#1 lore raw

net/netfilter/nf_tables_api.c static int nf_tables_commit(struct net *net, struct sk_buff *skb) · line 10160

Quoted 13 lines · Quan Tian
net/netfilter/nf_tables_api.c +5 -7
-				if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
-					nft_trans_destroy(trans);
-					break;
+				if (trans->ctx.table->flags & __NFT_TABLE_F_UPDATE) {
+					if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
+						nf_tables_table_disable(net, trans->ctx.table);
+					trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
 				}
-				if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
-					nf_tables_table_disable(net, trans->ctx.table);
-
-				trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
+				kfree(trans->ctx.table->udata);

This kfree() needs to happen in nft_commit_release().

Otherwise netlink dumps (which run without a lock) results in use-after-free.

Second issue is that ->udata and ->udlen would have to be updated atomically so that async dump doesn't observe old/new pointer with a larger udlen.

I suggest a preparation patch that replaces

u16 udlen;
u8 *udata;

in struct nft_table with

	struct nlattr *udata;

so its enough to swap() ctx.table->udata with nft_trans_table_udata(), then kfree() the old udata from nft_commit_release().

At _release time we can be sure no other cpu is referencing the old udata.