btree: Fix tree corruption in btree_get_prev()
PATCH 1/2 btree: Fix tree corruption in btree_get_prev()
From: Roland Dreier <[email protected]>
The memory the parameter __key points to is used as an iterator in btree_get_prev(), so if we save off a bkey() pointer in retry_key and then assign that to __key, we'll end up corrupting the btree internals when we do eg
longcpy(__key, bkey(geo, node, i), geo->keylen);
to return the key value. What we should do instead is use longcpy() to copy the key value that retry_key points to __key.
This can cause a btree to get corrupted by seemingly read-only operations such as btree_for_each_safe.
--- lib/btree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/btree.c +1 -1
@@ -351,7 +351,7 @@ retry: } miss:if (retry_key) { - __key = retry_key; +longcpy (__key, retry_key, geo->keylen); retry_key =NULL ; goto retry; }
Signature · 1 line
1.7.9.5
PATCH 2/2 btree: Catch NULL value before it does harm
From: Joern Engel <[email protected]>
Storing NULL values in the btree is illegal and can lead to memory corruption and possible other fun as well. Catch it on insert, instead of waiting for the inevitable.
--- lib/btree.c | 1 + 1 file changed, 1 insertion(+)
lib/btree.c +1 -0
@@ -509,6 +509,7 @@ retry:int btree_insert (struct btree_head *head,struct btree_geo *geo,unsigned long *key,void *val,gfp_t gfp) { +BUG_ON (!val);return btree_insert_level (head, geo, key, val,1 , gfp); }EXPORT_SYMBOL_GPL (btree_insert);
Signature · 1 line
1.7.9.5
Andrew Morton
maintainerQuoted 27 lines · On Wed, 6 Jun 2012 10:17:26 -0700 Roland Dreier <[email protected]> wrote:
From: Roland Dreier <[email protected]>
The memory the parameter
__keypoints to is used as an iterator inbtree_get_prev(), so if we save off abkey()pointer inretry_keyand then assign that to__key, we'll end up corrupting the btree internals when we do eglongcpy(__key, bkey(geo, node, i), geo->keylen);to return the key value. What we should do instead is use
longcpy()to copy the key value thatretry_keypoints to__key.This can cause a btree to get corrupted by seemingly read-only operations such as
btree_for_each_safe.Acked-by Joern Engel <[email protected]>Cc <[email protected]>Signed-off-by Roland Dreier <[email protected]>--- lib/btree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/btree.c +0
-0
lib/btree.c retry: · line 357
Quoted 8 lines
lib/btree.c +1
-1@@ -351,7 +351,7 @@ retry: } miss:if (retry_key) { - __key = retry_key; +longcpy (__key, retry_key, geo->keylen); retry_key =NULL ; goto retry; }
Fair enough.
I think we can do this to save a few cycles?
lib/btree.c renamed from lib/btree.c~btree-fix-tree-corruption-in-btree_get_prev-fix +2 -2
@@ -319,8 +319,8 @@ void *btree_get_prev(struct btree_head *if (head->height ==0 )return NULL ; -retry:longcpy (key, __key, geo->keylen); +retry:dec_key (geo, key); node = head->node;
@@ -351,7 +351,7 @@ retry: } miss:if (retry_key) { -longcpy (__key, retry_key, geo->keylen); +longcpy (key, retry_key, geo->keylen); retry_key =NULL ; goto retry; }
_
Quoted 16 lines · Andrew Morton
I think we can do this to save a few cycles?
lib/btree.c renamed from lib/btree.c~btree-fix-tree-corruption-in-btree_get_prev-fix +1
-1@@ -319,8 +319,8 @@ void *btree_get_prev(struct btree_head * if (head->height == 0) return NULL; -retry: longcpy(key, __key, geo->keylen); +retry: dec_key(geo, key); node = head->node;@@ -351,7 +351,7 @@ retry: }
Quoted 7 lines
miss:
if (retry_key) {
-longcpy(__key, retry_key, geo->keylen);
+longcpy(key, retry_key, geo->keylen);retry_key= NULL;
goto retry;
}
Looks reasonable to me, and passes my hacky userspace test harness.
On the other hand, your change makes me think we don't even need a separate iterator (and we can avoid the variable length array declaration), ie we could do
lib/btree.c +0 -0
@@ -312,7 +312,7 @@ void *btree_get_prev(struct btree_head *head,
struct btree_geo *geo,
{
int i, height;
unsigned long *node, *oldnode;
- unsigned long *retry_key = NULL, key[geo->keylen];
+ unsigned long *retry_key = NULL;
if (keyzero(geo, __key))
return NULL;
@@ -320,13 +320,12 @@ void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
if (head->height == 0)
return NULL;
retry:
- longcpy(key, __key, geo->keylen);
- dec_key(geo, key);
+ dec_key(geo, __key);
node = head->node;
for (height = head->height ; height > 1; height--) {
for (i = 0; i < geo->no_pairs; i++)
- if (keycmp(geo, node, i, key) <= 0)
+ if (keycmp(geo, node, i, __key) <= 0)
break;
if (i == geo->no_pairs)
goto miss;
@@ -341,7 +340,7 @@ retry:
goto miss;
for (i = 0; i < geo->no_pairs; i++) {
- if (keycmp(geo, node, i, key) <= 0) {
+ if (keycmp(geo, node, i, __key) <= 0) {
if (bval(geo, node, i)) {
longcpy(__key, bkey(geo, node, i), geo->keylen); return bval(geo, node, i);
but this means even if we fail and return NULL we mess with the caller's __key. So your way may be better.
Joern?
- R.
Quoted 3 lines · Roland Dreier
On the other hand, your change makes me think we don't even need a separate iterator (and we can avoid the variable length array declaration)
FWIW with that change on top of my patch, I see
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-123 (-123)
function old new delta btree_get_prev 646 523 -123
on x86-64, so avoiding the variable length array is definitely worth something.
So the issue for me is whether messing with the caller's __key storage is OK, or if it's worth having a temporary local variable.
- R.
Andrew Morton
maintainerQuoted 11 lines · On Wed, 6 Jun 2012 17:44:20 -0700 Roland Dreier <[email protected]> wrote:
Quoted 3 lines · Roland Dreier
On the other hand, your change makes me think we don't even need a separate iterator (and we can avoid the variable length array declaration)
FWIW with that change on top of my patch, I see
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-123 (-123)
function old new delta btree_get_prev 646 523 -123
Quoted 7 lines
on x86-64, so avoiding the variable length array is definitely worth something.
So the issue for me is whether messing with the caller's
__keystorage is OK, or if it's worth having a temporary local variable.
Sometimes altering the caller's *__key when lookup fails is pretty rude behavior :(
Perhaps we could add an arg to btree_get_prev(), provide it with separate input and output key pointers?