Skip to content

Commit 43e3392

Browse files
Yihao Wuchucklever
authored andcommitted
SUNRPC/cache: Fix unsafe traverse caused double-free in cache_purge
Deleting list entry within hlist_for_each_entry_safe is not safe unless next pointer (tmp) is protected too. It's not, because once hash_lock is released, cache_clean may delete the entry that tmp points to. Then cache_purge can walk to a deleted entry and tries to double free it. Fix this bug by holding only the deleted entry's reference. Suggested-by: NeilBrown <[email protected]> Signed-off-by: Yihao Wu <[email protected]> Reviewed-by: NeilBrown <[email protected]> [ cel: removed unused variable ] Signed-off-by: Chuck Lever <[email protected]>
1 parent e1e8399 commit 43e3392

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

net/sunrpc/cache.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ void cache_purge(struct cache_detail *detail)
529529
{
530530
struct cache_head *ch = NULL;
531531
struct hlist_head *head = NULL;
532-
struct hlist_node *tmp = NULL;
533532
int i = 0;
534533

535534
spin_lock(&detail->hash_lock);
@@ -541,7 +540,9 @@ void cache_purge(struct cache_detail *detail)
541540
dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
542541
for (i = 0; i < detail->hash_size; i++) {
543542
head = &detail->hash_table[i];
544-
hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
543+
while (!hlist_empty(head)) {
544+
ch = hlist_entry(head->first, struct cache_head,
545+
cache_list);
545546
sunrpc_begin_cache_remove_entry(ch, detail);
546547
spin_unlock(&detail->hash_lock);
547548
sunrpc_end_cache_remove_entry(ch, detail);

0 commit comments

Comments
 (0)