Skip to content

Commit c56716c

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: extensions: introduce extension genid count
Multiple netfilter extensions store pointers to external data in their extension area struct. Examples: 1. Timeout policies 2. Connection tracking helpers. No references are taken for these. When a helper or timeout policy is removed, the conntrack table gets traversed and affected extensions are cleared. Conntrack entries not yet in the hashtable are referenced via a special list, the unconfirmed list. On removal of a policy or connection tracking helper, the unconfirmed list gets traversed an all entries are marked as dying, this prevents them from getting committed to the table at insertion time: core checks for dying bit, if set, the conntrack entry gets destroyed at confirm time. The disadvantage is that each new conntrack has to be added to the percpu unconfirmed list, and each insertion needs to remove it from this list. The list is only ever needed when a policy or helper is removed -- a rare occurrence. Add a generation ID count: Instead of adding to the list and then traversing that list on policy/helper removal, increment a counter that is stored in the extension area. For unconfirmed conntracks, the extension has the genid valid at ct allocation time. Removal of a helper/policy etc. increments the counter. At confirmation time, validate that ext->genid == global_id. If the stored number is not the same, do not allow the conntrack insertion, just like as if a confirmed-list traversal would have flagged the entry as dying. After insertion, the genid is no longer relevant (conntrack entries are now reachable via the conntrack table iterators and is set to 0. This allows removal of the percpu unconfirmed list. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 17438b4 commit c56716c

File tree

4 files changed

+111
-17
lines changed

4 files changed

+111
-17
lines changed

include/net/netfilter/nf_conntrack_extend.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,11 @@ enum nf_ct_ext_id {
3434
NF_CT_EXT_NUM,
3535
};
3636

37-
#define NF_CT_EXT_HELPER_TYPE struct nf_conn_help
38-
#define NF_CT_EXT_NAT_TYPE struct nf_conn_nat
39-
#define NF_CT_EXT_SEQADJ_TYPE struct nf_conn_seqadj
40-
#define NF_CT_EXT_ACCT_TYPE struct nf_conn_acct
41-
#define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache
42-
#define NF_CT_EXT_TSTAMP_TYPE struct nf_conn_tstamp
43-
#define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout
44-
#define NF_CT_EXT_LABELS_TYPE struct nf_conn_labels
45-
#define NF_CT_EXT_SYNPROXY_TYPE struct nf_conn_synproxy
46-
#define NF_CT_EXT_ACT_CT_TYPE struct nf_conn_act_ct_ext
47-
4837
/* Extensions: optional stuff which isn't permanently in struct. */
4938
struct nf_ct_ext {
5039
u8 offset[NF_CT_EXT_NUM];
5140
u8 len;
41+
unsigned int gen_id;
5242
char data[] __aligned(8);
5343
};
5444

@@ -62,17 +52,28 @@ static inline bool nf_ct_ext_exist(const struct nf_conn *ct, u8 id)
6252
return (ct->ext && __nf_ct_ext_exist(ct->ext, id));
6353
}
6454

65-
static inline void *__nf_ct_ext_find(const struct nf_conn *ct, u8 id)
55+
void *__nf_ct_ext_find(const struct nf_ct_ext *ext, u8 id);
56+
57+
static inline void *nf_ct_ext_find(const struct nf_conn *ct, u8 id)
6658
{
67-
if (!nf_ct_ext_exist(ct, id))
59+
struct nf_ct_ext *ext = ct->ext;
60+
61+
if (!ext || !__nf_ct_ext_exist(ext, id))
6862
return NULL;
6963

64+
if (unlikely(ext->gen_id))
65+
return __nf_ct_ext_find(ext, id);
66+
7067
return (void *)ct->ext + ct->ext->offset[id];
7168
}
72-
#define nf_ct_ext_find(ext, id) \
73-
((id##_TYPE *)__nf_ct_ext_find((ext), (id)))
7469

7570
/* Add this type, returns pointer to data or NULL. */
7671
void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp);
7772

73+
/* ext genid. if ext->id != ext_genid, extensions cannot be used
74+
* anymore unless conntrack has CONFIRMED bit set.
75+
*/
76+
extern atomic_t nf_conntrack_ext_genid;
77+
void nf_ct_ext_bump_genid(void);
78+
7879
#endif /* _NF_CONNTRACK_EXTEND_H */

include/net/netfilter/nf_conntrack_labels.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ struct nf_conn_labels {
1717
unsigned long bits[NF_CT_LABELS_MAX_SIZE / sizeof(long)];
1818
};
1919

20+
/* Can't use nf_ct_ext_find(), flow dissector cannot use symbols
21+
* exported by nf_conntrack module.
22+
*/
2023
static inline struct nf_conn_labels *nf_ct_labels_find(const struct nf_conn *ct)
2124
{
2225
#ifdef CONFIG_NF_CONNTRACK_LABELS
23-
return nf_ct_ext_find(ct, NF_CT_EXT_LABELS);
26+
struct nf_ct_ext *ext = ct->ext;
27+
28+
if (!ext || !__nf_ct_ext_exist(ext, NF_CT_EXT_LABELS))
29+
return NULL;
30+
31+
return (void *)ct->ext + ct->ext->offset[NF_CT_EXT_LABELS];
2432
#else
2533
return NULL;
2634
#endif

net/netfilter/nf_conntrack_core.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,33 @@ static void __nf_conntrack_hash_insert(struct nf_conn *ct,
876876
&nf_conntrack_hash[reply_hash]);
877877
}
878878

879+
static bool nf_ct_ext_valid_pre(const struct nf_ct_ext *ext)
880+
{
881+
/* if ext->gen_id is not equal to nf_conntrack_ext_genid, some extensions
882+
* may contain stale pointers to e.g. helper that has been removed.
883+
*
884+
* The helper can't clear this because the nf_conn object isn't in
885+
* any hash and synchronize_rcu() isn't enough because associated skb
886+
* might sit in a queue.
887+
*/
888+
return !ext || ext->gen_id == atomic_read(&nf_conntrack_ext_genid);
889+
}
890+
891+
static bool nf_ct_ext_valid_post(struct nf_ct_ext *ext)
892+
{
893+
if (!ext)
894+
return true;
895+
896+
if (ext->gen_id != atomic_read(&nf_conntrack_ext_genid))
897+
return false;
898+
899+
/* inserted into conntrack table, nf_ct_iterate_cleanup()
900+
* will find it. Disable nf_ct_ext_find() id check.
901+
*/
902+
WRITE_ONCE(ext->gen_id, 0);
903+
return true;
904+
}
905+
879906
int
880907
nf_conntrack_hash_check_insert(struct nf_conn *ct)
881908
{
@@ -891,6 +918,11 @@ nf_conntrack_hash_check_insert(struct nf_conn *ct)
891918

892919
zone = nf_ct_zone(ct);
893920

921+
if (!nf_ct_ext_valid_pre(ct->ext)) {
922+
NF_CT_STAT_INC(net, insert_failed);
923+
return -ETIMEDOUT;
924+
}
925+
894926
local_bh_disable();
895927
do {
896928
sequence = read_seqcount_begin(&nf_conntrack_generation);
@@ -931,6 +963,13 @@ nf_conntrack_hash_check_insert(struct nf_conn *ct)
931963
nf_conntrack_double_unlock(hash, reply_hash);
932964
NF_CT_STAT_INC(net, insert);
933965
local_bh_enable();
966+
967+
if (!nf_ct_ext_valid_post(ct->ext)) {
968+
nf_ct_kill(ct);
969+
NF_CT_STAT_INC(net, drop);
970+
return -ETIMEDOUT;
971+
}
972+
934973
return 0;
935974
chaintoolong:
936975
NF_CT_STAT_INC(net, chaintoolong);
@@ -1198,6 +1237,11 @@ __nf_conntrack_confirm(struct sk_buff *skb)
11981237
return NF_DROP;
11991238
}
12001239

1240+
if (!nf_ct_ext_valid_pre(ct->ext)) {
1241+
NF_CT_STAT_INC(net, insert_failed);
1242+
goto dying;
1243+
}
1244+
12011245
pr_debug("Confirming conntrack %p\n", ct);
12021246
/* We have to check the DYING flag after unlink to prevent
12031247
* a race against nf_ct_get_next_corpse() possibly called from
@@ -1254,6 +1298,16 @@ __nf_conntrack_confirm(struct sk_buff *skb)
12541298
nf_conntrack_double_unlock(hash, reply_hash);
12551299
local_bh_enable();
12561300

1301+
/* ext area is still valid (rcu read lock is held,
1302+
* but will go out of scope soon, we need to remove
1303+
* this conntrack again.
1304+
*/
1305+
if (!nf_ct_ext_valid_post(ct->ext)) {
1306+
nf_ct_kill(ct);
1307+
NF_CT_STAT_INC(net, drop);
1308+
return NF_DROP;
1309+
}
1310+
12571311
help = nfct_help(ct);
12581312
if (help && help->helper)
12591313
nf_conntrack_event_cache(IPCT_HELPER, ct);
@@ -2491,6 +2545,7 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
24912545
*/
24922546
synchronize_net();
24932547

2548+
nf_ct_ext_bump_genid();
24942549
nf_ct_iterate_cleanup(iter, data, 0, 0);
24952550
}
24962551
EXPORT_SYMBOL_GPL(nf_ct_iterate_destroy);

net/netfilter/nf_conntrack_extend.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
2929

30+
atomic_t nf_conntrack_ext_genid __read_mostly = ATOMIC_INIT(1);
31+
3032
static const u8 nf_ct_ext_type_len[NF_CT_EXT_NUM] = {
3133
[NF_CT_EXT_HELPER] = sizeof(struct nf_conn_help),
3234
#if IS_ENABLED(CONFIG_NF_NAT)
@@ -116,8 +118,10 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
116118
if (!new)
117119
return NULL;
118120

119-
if (!ct->ext)
121+
if (!ct->ext) {
120122
memset(new->offset, 0, sizeof(new->offset));
123+
new->gen_id = atomic_read(&nf_conntrack_ext_genid);
124+
}
121125

122126
new->offset[id] = newoff;
123127
new->len = newlen;
@@ -127,3 +131,29 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
127131
return (void *)new + newoff;
128132
}
129133
EXPORT_SYMBOL(nf_ct_ext_add);
134+
135+
/* Use nf_ct_ext_find wrapper. This is only useful for unconfirmed entries. */
136+
void *__nf_ct_ext_find(const struct nf_ct_ext *ext, u8 id)
137+
{
138+
unsigned int gen_id = atomic_read(&nf_conntrack_ext_genid);
139+
unsigned int this_id = READ_ONCE(ext->gen_id);
140+
141+
if (!__nf_ct_ext_exist(ext, id))
142+
return NULL;
143+
144+
if (this_id == 0 || ext->gen_id == gen_id)
145+
return (void *)ext + ext->offset[id];
146+
147+
return NULL;
148+
}
149+
EXPORT_SYMBOL(__nf_ct_ext_find);
150+
151+
void nf_ct_ext_bump_genid(void)
152+
{
153+
unsigned int value = atomic_inc_return(&nf_conntrack_ext_genid);
154+
155+
if (value == UINT_MAX)
156+
atomic_set(&nf_conntrack_ext_genid, 1);
157+
158+
msleep(HZ);
159+
}

0 commit comments

Comments
 (0)