Skip to content

Commit 5619155

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 8513091 commit 5619155

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;
@@ -7964,6 +7967,7 @@ static void nft_set_commit_update(struct list_head *set_update_list)
79647967

79657968
static int nf_tables_commit(struct net *net, struct sk_buff *skb)
79667969
{
7970+
struct nftables_pernet *nft_net = nft_pernet(net);
79677971
struct nft_trans *trans, *next;
79687972
LIST_HEAD(set_update_list);
79697973
struct nft_trans_elem *te;
@@ -8398,7 +8402,7 @@ static void nf_tables_cleanup(struct net *net)
83988402
static int nf_tables_abort(struct net *net, struct sk_buff *skb,
83998403
enum nfnl_abort_action action)
84008404
{
8401-
int ret = __nf_tables_abort(net, action);
8405+
struct nftables_pernet *nft_net = nft_pernet(net);
84028406

84038407
mutex_unlock(&net->nft_commit_mutex);
84048408

@@ -8407,6 +8411,7 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb,
84078411

84088412
static bool nf_tables_valid_genid(struct net *net, u32 genid)
84098413
{
8414+
struct nftables_pernet *nft_net = nft_pernet(net);
84108415
bool genid_ok;
84118416

84128417
mutex_lock(&net->nft_commit_mutex);
@@ -9019,6 +9024,8 @@ static void __nft_release_tables(struct net *net)
90199024

90209025
static int __net_init nf_tables_init_net(struct net *net)
90219026
{
9027+
struct nftables_pernet *nft_net = nft_pernet(net);
9028+
90229029
INIT_LIST_HEAD(&net->nft.tables);
90239030
INIT_LIST_HEAD(&net->nft.commit_list);
90249031
INIT_LIST_HEAD(&net->nft_module_list);
@@ -9039,11 +9046,16 @@ static void __net_exit nf_tables_pre_exit_net(struct net *net)
90399046

90409047
static void __net_exit nf_tables_exit_net(struct net *net)
90419048
{
9049+
struct nftables_pernet *nft_net = nft_pernet(net);
9050+
90429051
mutex_lock(&net->nft_commit_mutex);
9052+
90439053
if (!list_empty(&net->nft.commit_list) ||
90449054
!list_empty(&net->nft_module_list))
90459055
__nf_tables_abort(net, NFNL_ABORT_NONE);
9056+
90469057
__nft_release_tables(net);
9058+
90479059
mutex_unlock(&net->nft_commit_mutex);
90489060
WARN_ON_ONCE(!list_empty(&net->nft.tables));
90499061
WARN_ON_ONCE(!list_empty(&net->nft_module_list));

0 commit comments

Comments
 (0)