Skip to contentSkip to conversation

netfilter: nf_tables: disable delset and delsetelem on anonymous sets

PATCH In review Florian Westphal 2 participants 2 messages May 16, 2023 – May 17, 2023

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: disable delset and delsetelem on anonymous sets

nftables doesn't need this because anonymous sets are always released implicitly when the owning rule is deleted.

nftables will also never remove elements from anonymous sets.

We've had bad interaction of anon sets with the rule deletion, e.g. ability to schedule element removal from an anon set after rule deletion already deleted that set.

Disable support for this until a use-case shows up, this should not result in any user-visible breakage even though this removes an existing feature.

---
iptables-nft and nftables test cases pass with this applied.

 net/netfilter/nf_tables_api.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/netfilter/nf_tables_api.c +11 -0
index 0396fd8f4e71..02c3f912db88 100644
@@ -4996,6 +4996,11 @@ static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,
 		set = nft_set_lookup(table, attr, genmask);
 	}
 
+	if (set->flags & NFT_SET_ANONYMOUS) {
+		NL_SET_BAD_ATTR(extack, attr);
+		return -EBUSY;
+	}
+
 	if (IS_ERR(set)) {
 		if (PTR_ERR(set) == -ENOENT &&
 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET)
@@ -6888,6 +6893,12 @@ static int nf_tables_delsetelem(struct sk_buff *skb,
 	set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
 	if (IS_ERR(set))
 		return PTR_ERR(set);
+
+	if (set->flags & NFT_SET_ANONYMOUS) {
+		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_SET]);
+		return -EROFS;
+	}
+
 	if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
 		return -EBUSY;
 
Signature · 1 line
2.39.3
#1 lore raw

Hi Florian,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Florian-Westphal/netfilter-nf_tables-disable-delset-and-delsetelem-on-anonymous-sets/20230516-224334
base:   linus/master
patch link:    https://lore.kernel.org/r/20230516132414.16245-1-fw%40strlen.de
patch subject: [PATCH nf-next] netfilter: nf_tables: disable delset and delsetelem on anonymous sets
config: i386-randconfig-m021-20230515
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0

If you fix the issue, kindly add following tag where applicable

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
net/netfilter/nf_tables_api.c:5001 nf_tables_delset() error: 'set' dereferencing possible ERR_PTR()

vim +/set +5001 net/netfilter/nf_tables_api.c

7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  4971  static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,
7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  4972  			    const struct nlattr * const nla[])
20a69341f2d00c Patrick McHardy            2013-10-11  4973  {
7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  4974  	struct netlink_ext_ack *extack = info->extack;
7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  4975  	u8 genmask = nft_genmask_next(info->net);
670866512f971d Pablo Neira Ayuso          2021-05-31  4976  	u8 family = info->nfmsg->nfgen_family;
7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  4977  	struct net *net = info->net;
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  4978  	const struct nlattr *attr;
670866512f971d Pablo Neira Ayuso          2021-05-31  4979  	struct nft_table *table;
20a69341f2d00c Patrick McHardy            2013-10-11  4980  	struct nft_set *set;
20a69341f2d00c Patrick McHardy            2013-10-11  4981  	struct nft_ctx ctx;
20a69341f2d00c Patrick McHardy            2013-10-11  4982  
ef4b65e53cc77e Pablo Neira Ayuso          2021-05-31  4983  	if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
ec2c9935688fbd Patrick McHardy            2014-02-05  4984  		return -EAFNOSUPPORT;
20a69341f2d00c Patrick McHardy            2013-10-11  4985  
670866512f971d Pablo Neira Ayuso          2021-05-31  4986  	table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
6001a930ce0378 Pablo Neira Ayuso          2021-02-15  4987  				 genmask, NETLINK_CB(skb).portid);
670866512f971d Pablo Neira Ayuso          2021-05-31  4988  	if (IS_ERR(table)) {
670866512f971d Pablo Neira Ayuso          2021-05-31  4989  		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
670866512f971d Pablo Neira Ayuso          2021-05-31  4990  		return PTR_ERR(table);
670866512f971d Pablo Neira Ayuso          2021-05-31  4991  	}
20a69341f2d00c Patrick McHardy            2013-10-11  4992  
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  4993  	if (nla[NFTA_SET_HANDLE]) {
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  4994  		attr = nla[NFTA_SET_HANDLE];
670866512f971d Pablo Neira Ayuso          2021-05-31  4995  		set = nft_set_lookup_byhandle(table, attr, genmask);
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  4996  	} else {
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  4997  		attr = nla[NFTA_SET_NAME];
670866512f971d Pablo Neira Ayuso          2021-05-31  4998  		set = nft_set_lookup(table, attr, genmask);
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  4999  	}
a8278400809dd9 Pablo Neira Ayuso          2017-09-03  5000  
3743fd7cff321a Florian Westphal           2023-05-16 @5001  	if (set->flags & NFT_SET_ANONYMOUS) {
                                                                    ^^^^^^^^^^

Dereferenced.

3743fd7cff321a Florian Westphal           2023-05-16  5002  		NL_SET_BAD_ATTR(extack, attr);
3743fd7cff321a Florian Westphal           2023-05-16  5003  		return -EBUSY;
3743fd7cff321a Florian Westphal           2023-05-16  5004  	}
3743fd7cff321a Florian Westphal           2023-05-16  5005  
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  5006  	if (IS_ERR(set)) {
                                                                    ^^^^^^^^^^^

Checked too late.

f80a612dd77c45 Fernando Fernandez Mancera 2023-01-02  5007  		if (PTR_ERR(set) == -ENOENT &&
f80a612dd77c45 Fernando Fernandez Mancera 2023-01-02  5008  		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET)
f80a612dd77c45 Fernando Fernandez Mancera 2023-01-02  5009  			return 0;
f80a612dd77c45 Fernando Fernandez Mancera 2023-01-02  5010  
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  5011  		NL_SET_BAD_ATTR(extack, attr);
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  5012  		return PTR_ERR(set);
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  5013  	}
273fe3f1006ea5 Pablo Neira Ayuso          2019-03-08  5014  	if (set->use ||
7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  5015  	    (info->nlh->nlmsg_flags & NLM_F_NONREC &&
7dab8ee3b6e7ec Pablo Neira Ayuso          2021-04-23  5016  	     atomic_read(&set->nelems) > 0)) {
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  5017  		NL_SET_BAD_ATTR(extack, attr);
20a69341f2d00c Patrick McHardy            2013-10-11  5018  		return -EBUSY;
36dd1bcc07e529 Pablo Neira Ayuso          2018-03-28  5019  	}
20a69341f2d00c Patrick McHardy            2013-10-11  5020  
670866512f971d Pablo Neira Ayuso          2021-05-31  5021  	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
670866512f971d Pablo Neira Ayuso          2021-05-31  5022  
ee01d542563426 Arturo Borrero             2014-09-02  5023  	return nft_delset(&ctx, set);
20a69341f2d00c Patrick McHardy            2013-10-11  5024  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki