Skip to contentSkip to conversation

netfilter: conntrack: avoid integer overflow when resizing

PATCH In review Florian Westphal 2 participants 4 messages Apr 23, 2016 – May 3, 2016

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: conntrack: avoid integer overflow when resizing

Can overflow so we might allocate very small table when bucket count is high on a 32bit platform.

Note: resize is only possible from init_netns.

---
 net/netfilter/nf_conntrack_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/netfilter/nf_conntrack_core.c +7 -0
index 2bbb962..11daca5 100644
@@ -1563,8 +1563,15 @@ void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
 	unsigned int nr_slots, i;
 	size_t sz;
 
+	if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;
+
 	BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
 	nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
+
+	if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;
+
 	sz = nr_slots * sizeof(struct hlist_nulls_head);
 	hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
 					get_order(sz));
Signature · 1 line
2.7.3
#1 lore raw
Quoted 15 lines · Florian Westphal

Can overflow so we might allocate very small table when bucket count is high on a 32bit platform.

Note: resize is only possible from init_netns.

---
 net/netfilter/nf_conntrack_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/netfilter/nf_conntrack_core.c +0 -0
index 2bbb962..11daca5 100644

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1567

Quoted 5 lines
net/netfilter/nf_conntrack_core.c +2 -0
@@ -1563,8 +1563,15 @@ void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
 	unsigned int nr_slots, i;
 	size_t sz;
 
+	if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;

*sizep gets initially set to the number of buckets.

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1570

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +0 -0
 	BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
 	nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));

Then, this value is divided by the number of hlist heads that fit into a page.

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1573

Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3 -0
+
+	if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;

So, isn't is enough with this sole check? I might be missing anything.

Quoted 11 lines
net/netfilter/nf_conntrack_core.c +1 -0
+
 	sz = nr_slots * sizeof(struct hlist_nulls_head);
 	hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
 					get_order(sz));
Signature · 6 lines
2.7.3

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#2 lore raw

Pablo Neira Ayuso <[email protected]> wrote:

[ Sorry for late reply ]

Quoted 19 lines · Florian Westphal
Quoted 18 lines

Can overflow so we might allocate very small table when bucket count is high on a 32bit platform.

Note: resize is only possible from init_netns.

---
 net/netfilter/nf_conntrack_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/netfilter/nf_conntrack_core.c +0 -0
index 2bbb962..11daca5 100644
@@ -1563,8 +1563,15 @@ void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
 	unsigned int nr_slots, i;
 	size_t sz;
 
Quoted 4 lines

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1567

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +2 -0
+	if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;

*sizep gets initially set to the number of buckets.

Yes.

Quoted 5 lines

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1570

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +0 -0
 	BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
 	nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));

Then, this value is divided by the number of hlist heads that fit into a page.

No, its rounded up to a multiple of PAGE/hlist heads, so for very large values of *sizep nr_slots would be 0.

Quoted 2 lines

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1573

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +2 -0
+	if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;

Alternative would be to change this to:

if (nr_slots == 0 || nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))

	return NULL;

Or, add this check:

if (nr_slots < roundup(1, PAGE_SIZE / sizeof(struct hlist_nulls_head))
nr_slots = roundup(1, PAGE_SIZE / sizeof(struct hlist_nulls_head)))

I wasn't sure if its better to fail or if we should just pretend a sane value was given.

Let me know what you think and I'll submit a v2.

[ Its not a big deal, but eventually I'd like to make the sysctl
writeable so users can just increase that, no need to use this obscure module parameter/sysfs param ... ]

Cheers,
Florian

#3 lore raw
Quoted 47 lines · Florian Westphal

Pablo Neira Ayuso <[email protected]> wrote:

[ Sorry for late reply ]

Quoted 19 lines · Florian Westphal
Quoted 18 lines

Can overflow so we might allocate very small table when bucket count is high on a 32bit platform.

Note: resize is only possible from init_netns.

---
 net/netfilter/nf_conntrack_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/netfilter/nf_conntrack_core.c +0 -0
index 2bbb962..11daca5 100644
@@ -1563,8 +1563,15 @@ void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
 	unsigned int nr_slots, i;
 	size_t sz;
 
Quoted 4 lines

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1567

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +2 -0
+	if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;

*sizep gets initially set to the number of buckets.

Yes.

Quoted 5 lines

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1570

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +0 -0
 	BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
 	nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));

Then, this value is divided by the number of hlist heads that fit into a page.

No, its rounded up to a multiple of PAGE/hlist heads, so for very large values of *sizep nr_slots would be 0.

Quoted 2 lines

net/netfilter/nf_conntrack_core.c void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) · line 1573

Quoted 2 lines
net/netfilter/nf_conntrack_core.c +2 -0
+	if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
+		return NULL;

Alternative would be to change this to:

if (nr_slots == 0 || nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))

	return NULL;
Quoted 7 lines

Or, add this check:

if (nr_slots < roundup(1, PAGE_SIZE / sizeof(struct hlist_nulls_head))
nr_slots = roundup(1, PAGE_SIZE / sizeof(struct hlist_nulls_head)))

I wasn't sure if its better to fail or if we should just pretend a sane value was given.

I prefer an explicit failure, so the user knows that what is asking for is not supported, rather than masking the problem.

Quoted 5 lines

Let me know what you think and I'll submit a v2.

[ Its not a big deal, but eventually I'd like to make the sysctl
writeable so users can just increase that, no need to use this obscure module parameter/sysfs param ... ]

Sounds good to me.