Skip to content

Commit 1689f25

Browse files
committed
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>
1 parent c451410 commit 1689f25

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
@@ -1211,6 +1211,29 @@ int __nft_release_basechain(struct nft_ctx *ctx);
12111211

12121212
unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
12131213

1214+
static inline bool nft_use_inc(u32 *use)
1215+
{
1216+
if (*use == UINT_MAX)
1217+
return false;
1218+
1219+
(*use)++;
1220+
1221+
return true;
1222+
}
1223+
1224+
static inline void nft_use_dec(u32 *use)
1225+
{
1226+
WARN_ON_ONCE((*use)-- == 0);
1227+
}
1228+
1229+
/* For error and abort path: restore use counter to previous state. */
1230+
static inline void nft_use_inc_restore(u32 *use)
1231+
{
1232+
WARN_ON_ONCE(!nft_use_inc(use));
1233+
}
1234+
1235+
#define nft_use_dec_restore nft_use_dec
1236+
12141237
/**
12151238
* struct nft_table - nf_tables table
12161239
*
@@ -1296,8 +1319,8 @@ struct nft_object {
12961319
struct list_head list;
12971320
struct rhlist_head rhlhead;
12981321
struct nft_object_hash_key key;
1299-
u32 genmask:2,
1300-
use:30;
1322+
u32 genmask:2;
1323+
u32 use;
13011324
u64 handle;
13021325
u16 udlen;
13031326
u8 *udata;
@@ -1399,8 +1422,8 @@ struct nft_flowtable {
13991422
char *name;
14001423
int hooknum;
14011424
int ops_len;
1402-
u32 genmask:2,
1403-
use:30;
1425+
u32 genmask:2;
1426+
u32 use;
14041427
u64 handle;
14051428
/* runtime data below here */
14061429
struct list_head hook_list ____cacheline_aligned;

0 commit comments

Comments
 (0)