From: Alexander Duyck <alexander.h.duyck@redhat.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net
Subject: [net-next PATCH v2 8/8] fib_trie: Prevent allocating tnode if bits is too big for size_t
Date: Wed, 04 Mar 2015 15:04:46 -0800 [thread overview]
Message-ID: <20150304230409.1612.98286.stgit@ahduyck-vm-fedora20> (raw)
In-Reply-To: <20150304225459.1612.45514.stgit@ahduyck-vm-fedora20>
This patch adds code to prevent us from attempting to allocate a tnode with
a size larger than what can be represented by size_t.
Signed-off-by: Alexander Duyck <alexander.h.duyck@redhat.com>
---
net/ipv4/fib_trie.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 08676c5..fae34ad 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -277,6 +277,8 @@ static inline void alias_free_mem_rcu(struct fib_alias *fa)
#define TNODE_KMALLOC_MAX \
ilog2((PAGE_SIZE - TNODE_SIZE(0)) / sizeof(struct tnode *))
+#define TNODE_VMALLOC_MAX \
+ ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct tnode *))
static void __node_free_rcu(struct rcu_head *head)
{
@@ -292,8 +294,17 @@ static void __node_free_rcu(struct rcu_head *head)
#define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
-static struct tnode *tnode_alloc(size_t size)
+static struct tnode *tnode_alloc(int bits)
{
+ size_t size;
+
+ /* verify bits is within bounds */
+ if (bits > TNODE_VMALLOC_MAX)
+ return NULL;
+
+ /* determine size and verify it is non-zero and didn't overflow */
+ size = TNODE_SIZE(1ul << bits);
+
if (size <= PAGE_SIZE)
return kzalloc(size, GFP_KERNEL);
else
@@ -334,8 +345,7 @@ static struct tnode *leaf_new(t_key key, struct fib_alias *fa)
static struct tnode *tnode_new(t_key key, int pos, int bits)
{
- size_t sz = TNODE_SIZE(1ul << bits);
- struct tnode *tn = tnode_alloc(sz);
+ struct tnode *tn = tnode_alloc(bits);
unsigned int shift = pos + bits;
/* verify bits and pos their msb bits clear and values are valid */
next prev parent reply other threads:[~2015-03-04 23:04 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-04 22:57 [net-next PATCH v2 0/8] ipv4/fib_trie: Cleanups to prepare for introduction of key vector Alexander Duyck
2015-03-04 22:58 ` [net-next PATCH v2 1/8] fib_trie: Only resize tnodes once instead of on each leaf removal in fib_table_flush Alexander Duyck
2015-03-04 22:59 ` [net-next PATCH v2 2/8] fib_trie: Fib walk rcu should take a tnode and key instead of a trie and a leaf Alexander Duyck
2015-03-04 23:01 ` [net-next PATCH v2 3/8] fib_trie: Fib find node should return parent Alexander Duyck
2015-03-04 23:02 ` [net-next PATCH v2 4/8] fib_trie: Update insert and delete to make use of tp from find_node Alexander Duyck
2015-03-04 23:02 ` [net-next PATCH v2 5/8] fib_trie: move leaf and tnode to occupy the same spot in the key vector Alexander Duyck
2015-03-04 23:02 ` [net-next PATCH v2 6/8] fib_trie: Make fib_table rcu safe Alexander Duyck
2015-03-04 23:04 ` [net-next PATCH v2 7/8] fib_trie: Update last spot w/ idx >> n->bits code and explanation Alexander Duyck
2015-03-04 23:04 ` Alexander Duyck [this message]
2015-03-05 4:37 ` [net-next PATCH v2 0/8] ipv4/fib_trie: Cleanups to prepare for introduction of key vector David Miller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150304230409.1612.98286.stgit@ahduyck-vm-fedora20 \
--to=alexander.h.duyck@redhat.com \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox