Skip to contentSkip to conversation

netfilter: nf_tables: use struct nlattr * to store userdata for nft_table

PATCH Superseded Quan Tian 2 participants 6 messages Mar 10, 2024 – Mar 11, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Quan Tian

author
#0 lore raw

PATCH v2 1/2 netfilter: nf_tables: use struct nlattr * to store userdata for nft_table

To prepare for the support for table comment updates, the patch changes to store userdata in struct nlattr *, which can be updated atomically on updates.

---
v2: Change to store userdata in struct nlattr * to ensure atomical update

---
 include/net/netfilter/nf_tables.h | 4 +---
 net/netfilter/nf_tables_api.c     | 9 +++++----
 2 files changed, 6 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +1 -3
index 510244cc0f8f..3944eb969172 100644
@@ -1248,7 +1248,6 @@ static inline void nft_use_inc_restore(u32 *use)
  *	@genmask: generation mask
  *	@nlpid: netlink port ID
  *	@name: name of the table
- *	@udlen: length of the user data
  *	@udata: user data
  *	@validate_state: internal, set when transaction adds jumps
  */
@@ -1267,8 +1266,7 @@ struct nft_table {
 					genmask:2;
 	u32				nlpid;
 	char				*name;
-	u16				udlen;
-	u8				*udata;
+	struct nlattr			*udata;
 	u8				validate_state;
 };
 
net/netfilter/nf_tables_api.c +5 -4
index 7e938c7397dd..de7efb8c8089 100644
@@ -983,7 +983,8 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
 		goto nla_put_failure;
 
 	if (table->udata) {
-		if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
+		if (nla_put(skb, NFTA_TABLE_USERDATA, nla_len(table->udata),
+			    nla_data(table->udata)))
 			goto nla_put_failure;
 	}
 
@@ -1386,11 +1387,11 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
 		goto err_strdup;
 
 	if (nla[NFTA_TABLE_USERDATA]) {
-		table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
+		table->udata = kmemdup(nla[NFTA_TABLE_USERDATA],
+				       nla_total_size(nla_len(nla[NFTA_TABLE_USERDATA])),
+				       GFP_KERNEL_ACCOUNT);
 		if (table->udata == NULL)
 			goto err_table_udata;
-
-		table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
 	}
 
 	err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
Signature · 1 line
2.39.3 (Apple Git-145)
author
#1 lore raw

PATCH v2 2/2 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.

---
v2: Change to store userdata in struct nlattr * to ensure atomical update

---
 include/net/netfilter/nf_tables.h |  3 ++
 net/netfilter/nf_tables_api.c     | 52 ++++++++++++++++++++++---------
 2 files changed, 41 insertions(+), 14 deletions(-)
include/net/netfilter/nf_tables.h +3 -0
index 3944eb969172..db324b4d4651 100644
@@ -1678,10 +1678,13 @@ struct nft_trans_chain {
 
 struct nft_trans_table {
 	bool				update;
+	struct nlattr			*udata;
 };
 
 #define nft_trans_table_update(trans)	\
 	(((struct nft_trans_table *)trans->data)->update)
+#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 +38 -14

96 lines not on this page · raw message

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

Re: [PATCH v2 nf-next 2/2] netfilter: nf_tables: support updating userdata for nft_table

Quoted 10 lines · Quan Tian
net/netfilter/nf_tables_api.c +3 -3
@@ -10129,14 +10154,12 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)
 		switch (trans->msg_type) {
 		case NFT_MSG_NEWTABLE:
 			if (nft_trans_table_update(trans)) {
-				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);

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

Quoted 10 lines
net/netfilter/nf_tables_api.c +2 -4
+					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;
+				swap(trans->ctx.table->udata, nft_trans_table_udata(trans));
 			} else {
 				nft_clear(net, trans->ctx.table);
 			}

There is a call to nft_trans_destroy() below here. You could add a "break" after the swap() to avoid it.

Otherwise kmemleak should report old udata being lost on update.

author
#3 lore raw

Re: [PATCH v2 nf-next 2/2] netfilter: nf_tables: support updating userdata for nft_table

Hi Florian,

Thanks a lot for your reviews.

Quoted 25 lines · Florian Westphal
Quoted 10 lines · Quan Tian
net/netfilter/nf_tables_api.c +3 -3
@@ -10129,14 +10154,12 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)
 		switch (trans->msg_type) {
 		case NFT_MSG_NEWTABLE:
 			if (nft_trans_table_update(trans)) {
-				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);

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

Quoted 10 lines
net/netfilter/nf_tables_api.c +2 -4
+					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;
+				swap(trans->ctx.table->udata, nft_trans_table_udata(trans));
 			} else {
 				nft_clear(net, trans->ctx.table);
 			}

There is a call to nft_trans_destroy() below here. You could add a "break" after the swap() to avoid it.

Quoted 2 lines

Otherwise kmemleak should report old udata being lost on update.

Thanks for pointing it out. kmemleak indeed reported the leak. I found "break" after swap() would skip sending the table update event, so I changed to execute different code paths for the two branches in v3. Please let me know if it makes sense to you.

Thanks,
Quan

maintainer
#4 lore raw
In reply to Quan Tian
Quoted 7 lines · Quan Tian

To prepare for the support for table comment updates, the patch changes to store userdata in struct nlattr *, which can be updated atomically on updates.

---
v2: Change to store userdata in struct nlattr * to ensure atomical update

Looks good, one minor nit below.

net/netfilter/nf_tables_api.c static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, · line 1392

Quoted 5 lines
net/netfilter/nf_tables_api.c +3 -1
 	if (nla[NFTA_TABLE_USERDATA]) {
-		table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
+		table->udata = kmemdup(nla[NFTA_TABLE_USERDATA],
+				       nla_total_size(nla_len(nla[NFTA_TABLE_USERDATA])),
+				       GFP_KERNEL_ACCOUNT);

I think its correct but it might make sense to add a small helper for this kmemdup so we don't need to copypaste in case this should get extended to e.g. chain udata update support.

author
#5 lore raw
Quoted 11 lines · Florian Westphal
Quoted 7 lines · Quan Tian

To prepare for the support for table comment updates, the patch changes to store userdata in struct nlattr *, which can be updated atomically on updates.

---
v2: Change to store userdata in struct nlattr * to ensure atomical update

Looks good, one minor nit below.

Quoted 9 lines

net/netfilter/nf_tables_api.c static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, · line 1392

Quoted 5 lines
net/netfilter/nf_tables_api.c +3 -1
 	if (nla[NFTA_TABLE_USERDATA]) {
-		table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
+		table->udata = kmemdup(nla[NFTA_TABLE_USERDATA],
+				       nla_total_size(nla_len(nla[NFTA_TABLE_USERDATA])),
+				       GFP_KERNEL_ACCOUNT);

I think its correct but it might make sense to add a small helper for this kmemdup so we don't need to copypaste in case this should get extended to e.g. chain udata update support.

Extracted a function as suggested in v3, and also used it when duplicating the userdata when preparing trans for table updates. After this is merged, I could extend comment update support for other objects in the same fashion.

Thanks,
Quan