Skip to content

Commit f6f0cf3

Browse files
committed
netfilter: nftables: add nft_pernet() helper function
jira VULN-429 pre-cve CVE-2023-4244 commit-author Pablo Neira Ayuso <pablo@netfilter.org> commit d59d2f8 upstream-diff So many conflicts when trying to cherry pick this but they're all very similar and didn't have much trouble picking them out. As per previous commits in this series I've used 4.18.0-534 as the source of truth when resolving conflicts. Consolidate call to net_generic(net, nf_tables_net_id) in this wrapper function. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> (cherry picked from commit d59d2f8) Signed-off-by: Greg Rose <g.v.rose@ciq.com>
1 parent a1110cf commit f6f0cf3

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <net/netfilter/nf_flow_table.h>
1414
#include <net/netlink.h>
1515
#include <net/flow_offload.h>
16+
#include <net/netns/generic.h>
1617

1718
struct module;
1819

@@ -1561,4 +1562,16 @@ __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
15611562
#else
15621563
static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
15631564
#endif
1565+
1566+
struct nftables_pernet {
1567+
unsigned int gc_seq;
1568+
};
1569+
1570+
extern unsigned int nf_tables_net_id;
1571+
1572+
static inline struct nftables_pernet *nft_pernet(const struct net *net)
1573+
{
1574+
return net_generic(net, nf_tables_net_id);
1575+
}
1576+
15641577
#endif /* _NET_NF_TABLES_H */

net/netfilter/nf_tables_api.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
#define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
3030

31+
unsigned int nf_tables_net_id __read_mostly;
32+
EXPORT_SYMBOL_GPL(nf_tables_net_id);
33+
3134
static LIST_HEAD(nf_tables_expressions);
3235
static LIST_HEAD(nf_tables_objects);
3336
static LIST_HEAD(nf_tables_flowtables);
@@ -3593,8 +3596,8 @@ static struct nft_set *nft_set_lookup_byid(const struct net *net,
35933596
const struct nft_table *table,
35943597
const struct nlattr *nla, u8 genmask)
35953598
{
3596-
struct nft_trans *trans;
35973599
u32 id = ntohl(nla_get_be32(nla));
3600+
struct nft_trans *trans;
35983601

35993602
list_for_each_entry(trans, &net->nft.commit_list, list) {
36003603
if (trans->msg_type == NFT_MSG_NEWSET) {
@@ -3837,8 +3840,8 @@ static void nf_tables_set_notify(const struct nft_ctx *ctx,
38373840
const struct nft_set *set, int event,
38383841
gfp_t gfp_flags)
38393842
{
3840-
u32 portid = ctx->portid;
38413843
struct sk_buff *skb;
3844+
u32 portid = ctx->portid;
38423845
u16 flags = 0;
38433846
int err;
38443847

@@ -4786,7 +4789,7 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
47864789
rcu_read_lock();
47874790
cb->seq = READ_ONCE(net->nft.base_seq);
47884791

4789-
list_for_each_entry_rcu(table, &net->nft.tables, list) {
4792+
list_for_each_entry_rcu(table, &net->nft.tables, list) {
47904793
if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
47914794
dump_ctx->ctx.family != table->family)
47924795
continue;
@@ -7957,6 +7960,7 @@ static void nft_set_commit_update(struct list_head *set_update_list)
79577960

79587961
static int nf_tables_commit(struct net *net, struct sk_buff *skb)
79597962
{
7963+
struct nftables_pernet *nft_net = nft_pernet(net);
79607964
struct nft_trans *trans, *next;
79617965
LIST_HEAD(set_update_list);
79627966
struct nft_trans_elem *te;
@@ -8391,7 +8395,7 @@ static void nf_tables_cleanup(struct net *net)
83918395
static int nf_tables_abort(struct net *net, struct sk_buff *skb,
83928396
enum nfnl_abort_action action)
83938397
{
8394-
int ret = __nf_tables_abort(net, action);
8398+
struct nftables_pernet *nft_net = nft_pernet(net);
83958399

83968400
mutex_unlock(&net->nft_commit_mutex);
83978401

@@ -8400,6 +8404,7 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb,
84008404

84018405
static bool nf_tables_valid_genid(struct net *net, u32 genid)
84028406
{
8407+
struct nftables_pernet *nft_net = nft_pernet(net);
84038408
bool genid_ok;
84048409

84058410
mutex_lock(&net->nft_commit_mutex);
@@ -9012,6 +9017,8 @@ static void __nft_release_tables(struct net *net)
90129017

90139018
static int __net_init nf_tables_init_net(struct net *net)
90149019
{
9020+
struct nftables_pernet *nft_net = nft_pernet(net);
9021+
90159022
INIT_LIST_HEAD(&net->nft.tables);
90169023
INIT_LIST_HEAD(&net->nft.commit_list);
90179024
INIT_LIST_HEAD(&net->nft_module_list);
@@ -9032,11 +9039,16 @@ static void __net_exit nf_tables_pre_exit_net(struct net *net)
90329039

90339040
static void __net_exit nf_tables_exit_net(struct net *net)
90349041
{
9042+
struct nftables_pernet *nft_net = nft_pernet(net);
9043+
90359044
mutex_lock(&net->nft_commit_mutex);
9045+
90369046
if (!list_empty(&net->nft.commit_list) ||
90379047
!list_empty(&net->nft_module_list))
90389048
__nf_tables_abort(net, NFNL_ABORT_NONE);
9049+
90399050
__nft_release_tables(net);
9051+
90409052
mutex_unlock(&net->nft_commit_mutex);
90419053
WARN_ON_ONCE(!list_empty(&net->nft.tables));
90429054
WARN_ON_ONCE(!list_empty(&net->nft_module_list));

0 commit comments

Comments
 (0)