Commit aef43211 authored by Prasanna Meda's avatar Prasanna Meda Committed by Linus Torvalds

[PATCH] mempool: protect buffer overflow in mempool_resize

- Race in mempool_resize: memcpy can copy at the end of the kmalloced
  elements.

- When new_min_nr is same as min_nr, instead of reallocate and copy, just
  return, changed '<' to '<='.

- Changed while condition to the same sense of if condition from '>' to
  '<'; it is easy to think with only one of the left and right brains at a
  time.
Signed-off-by: default avatarPrasanna Meda <pmeda@akamai.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 6f2584c3
...@@ -114,8 +114,8 @@ int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask) ...@@ -114,8 +114,8 @@ int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask)
BUG_ON(new_min_nr <= 0); BUG_ON(new_min_nr <= 0);
spin_lock_irqsave(&pool->lock, flags); spin_lock_irqsave(&pool->lock, flags);
if (new_min_nr < pool->min_nr) { if (new_min_nr <= pool->min_nr) {
while (pool->curr_nr > new_min_nr) { while (new_min_nr < pool->curr_nr) {
element = remove_element(pool); element = remove_element(pool);
spin_unlock_irqrestore(&pool->lock, flags); spin_unlock_irqrestore(&pool->lock, flags);
pool->free(element, pool->pool_data); pool->free(element, pool->pool_data);
...@@ -132,6 +132,12 @@ int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask) ...@@ -132,6 +132,12 @@ int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask)
return -ENOMEM; return -ENOMEM;
spin_lock_irqsave(&pool->lock, flags); spin_lock_irqsave(&pool->lock, flags);
if (unlikely(new_min_nr <= pool->min_nr)) {
/* Raced, other resize will do our work */
spin_unlock_irqrestore(&pool->lock, flags);
kfree(new_elements);
goto out;
}
memcpy(new_elements, pool->elements, memcpy(new_elements, pool->elements,
pool->curr_nr * sizeof(*new_elements)); pool->curr_nr * sizeof(*new_elements));
kfree(pool->elements); kfree(pool->elements);
...@@ -149,7 +155,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask) ...@@ -149,7 +155,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask)
} else { } else {
spin_unlock_irqrestore(&pool->lock, flags); spin_unlock_irqrestore(&pool->lock, flags);
pool->free(element, pool->pool_data); /* Raced */ pool->free(element, pool->pool_data); /* Raced */
spin_lock_irqsave(&pool->lock, flags); goto out;
} }
} }
out_unlock: out_unlock:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment