Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2575769ybb; Sun, 5 Apr 2020 11:00:11 -0700 (PDT) X-Google-Smtp-Source: APiQypIFwcio+QwmAPr0CtipxgcMRL2fU39dzdV/fj2zJtXBCVkJTPSvBh6lspALb1sL9YoczsI8 X-Received: by 2002:a4a:3ed7:: with SMTP id t206mr14623163oot.61.1586109610999; Sun, 05 Apr 2020 11:00:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586109610; cv=none; d=google.com; s=arc-20160816; b=X2iCh/OIsVXCdHIBPE33DTsrdlOk4sl5pOpEiJgmzJMMK9SeWF2GXvamtxp4FrIq/r xORQjVbwNGMfWOlhVD8nPgnjCRR0tLprBunqfoaCWMAloaSiNgt/h6os+8NGHUKxIOZQ A1G16aznlHor4Pq2gdN5kWXzbMH1IJFtzfY7NmnFar1m7NoHKqA2wQ8CNPnhk2p3FMZB 1nufEgCd1pyhFJszv4LcCGa38b1zJ+fdy5VNruv86DVQ6a/A7YF38tGx3gMtvr7tvAqw iZXJ9GOCD9SEC1fPgTDFBHh4Ky+FRv834+VyP0f40zv6fxtdG8k1prwaxZF8P5b//GJX y0mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:from:subject; bh=DrXJl+vEzH1lI9y/R6zN3B8VCl7wJgc0KpJ7ZW7fSfo=; b=tkgr2uS4lazViB7zabX8fvr1kFSU//zPgJISgml8NTRKNGijBZ5MrcBhHBQbxn1Y2B L3oYFKLH8t4N+wy5jjsgxEh5wNxhMiOAljktfs0raM2ComF3er73iNJ2TRHhXw1a0E8v ibAsplPu37a0aWvRGk6OQMrcYr6e6+eODj7hayXw/35/9v1P1SYU00Ozh0o0K75XtvUa ikPEBroAUnAoAZMd45UBNyPG+Sf0leP5inFZ7REesFnvnmkosusnHJY5FL6h3w8idFvi D5tbeHz6KX3zjDmtlJUcCcwuIAsIljseW5YTxnvLEx19nwL2bz7QvN3EGU/HRD6SOQ4g ZiNA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k30si6063341ool.81.2020.04.05.10.59.49; Sun, 05 Apr 2020 11:00:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726830AbgDER5g (ORCPT + 99 others); Sun, 5 Apr 2020 13:57:36 -0400 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:40115 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726776AbgDER5g (ORCPT ); Sun, 5 Apr 2020 13:57:36 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R291e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07488;MF=wuyihao@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0Tuft5DG_1586109442; Received: from Macintosh.local(mailfrom:wuyihao@linux.alibaba.com fp:SMTPD_---0Tuft5DG_1586109442) by smtp.aliyun-inc.com(127.0.0.1); Mon, 06 Apr 2020 01:57:23 +0800 Subject: [PATCH v3] SUNRPC/cache: Fix unsafe traverse caused double-free in cache_purge From: Yihao Wu To: "J . Bruce Fields" , Chuck Lever , NeilBrown , Sasha Levin Cc: linux-nfs@vger.kernel.org References: <4568a7cf87f110b8e59fda6f53fda34c550ab403.1586108200.git.wuyihao@linux.alibaba.com> Message-ID: Date: Mon, 6 Apr 2020 01:57:22 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <4568a7cf87f110b8e59fda6f53fda34c550ab403.1586108200.git.wuyihao@linux.alibaba.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org 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. Signed-off-by: Yihao Wu --- v1->v2: Use Neil's better solution v2->v3: Fix a checkscript warning net/sunrpc/cache.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c index af0ddd28b081..b445874e8e2f 100644 --- a/net/sunrpc/cache.c +++ b/net/sunrpc/cache.c @@ -541,7 +541,9 @@ void cache_purge(struct cache_detail *detail) dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name); for (i = 0; i < detail->hash_size; i++) { head = &detail->hash_table[i]; - hlist_for_each_entry_safe(ch, tmp, head, cache_list) { + while (!hlist_empty(head)) { + ch = hlist_entry(head->first, struct cache_head, + cache_list); sunrpc_begin_cache_remove_entry(ch, detail); spin_unlock(&detail->hash_lock); sunrpc_end_cache_remove_entry(ch, detail); -- 2.20.1.2432.ga663e714