Skip to content

Commit 854ec83

Browse files
committed
netfilter: nf_tables: report use refcount overflow
JIRA: https://issues.redhat.com/browse/RHEL-1720 JIRA: https://issues.redhat.com/browse/RHEL-1721 Upstream Status: commit 1689f25 Conflicts: net/netfilter/nf_tables_api.c Upstream switched tp KERNEL_ACCOUNT in 33758c8 ("memcg: enable accounting for nft objects"). Also, we lack b9703ed ("netfilter: nf_tables: support for adding new devices to an existing netdev chain") so we can drop the if/else part. commit 1689f25 Author: Pablo Neira Ayuso <pablo@netfilter.org> Date: Wed Jun 28 16:24:27 2023 +0200 netfilter: nf_tables: report use refcount overflow Overflow use refcount checks are not complete. Add helper function to deal with object reference counter tracking. Report -EMFILE in case UINT_MAX is reached. nft_use_dec() splats in case that reference counter underflows, which should not ever happen. Add nft_use_inc_restore() and nft_use_dec_restore() which are used to restore reference counter from error and abort paths. Use u32 in nft_flowtable and nft_object since helper functions cannot work on bitfields. Remove the few early incomplete checks now that the helper functions are in place and used to check for refcount overflow. Fixes: 9651851 ("netfilter: add nftables") Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Florian Westphal <fwestpha@redhat.com>
1 parent 12f4f55 commit 854ec83

File tree

5 files changed

+141
-75
lines changed

5 files changed

+141
-75
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,29 @@ int __nft_release_basechain(struct nft_ctx *ctx);
11451145

11461146
unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
11471147

1148+
static inline bool nft_use_inc(u32 *use)
1149+
{
1150+
if (*use == UINT_MAX)
1151+
return false;
1152+
1153+
(*use)++;
1154+
1155+
return true;
1156+
}
1157+
1158+
static inline void nft_use_dec(u32 *use)
1159+
{
1160+
WARN_ON_ONCE((*use)-- == 0);
1161+
}
1162+
1163+
/* For error and abort path: restore use counter to previous state. */
1164+
static inline void nft_use_inc_restore(u32 *use)
1165+
{
1166+
WARN_ON_ONCE(!nft_use_inc(use));
1167+
}
1168+
1169+
#define nft_use_dec_restore nft_use_dec
1170+
11481171
/**
11491172
* struct nft_table - nf_tables table
11501173
*
@@ -1228,8 +1251,8 @@ struct nft_object {
12281251
struct list_head list;
12291252
struct rhlist_head rhlhead;
12301253
struct nft_object_hash_key key;
1231-
u32 genmask:2,
1232-
use:30;
1254+
u32 genmask:2;
1255+
u32 use;
12331256
u64 handle;
12341257
u16 udlen;
12351258
u8 *udata;
@@ -1331,8 +1354,8 @@ struct nft_flowtable {
13311354
char *name;
13321355
int hooknum;
13331356
int ops_len;
1334-
u32 genmask:2,
1335-
use:30;
1357+
u32 genmask:2;
1358+
u32 use;
13361359
u64 handle;
13371360
/* runtime data below here */
13381361
struct list_head hook_list ____cacheline_aligned;

0 commit comments

Comments
 (0)