Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2564309ybb; Sun, 5 Apr 2020 10:39:00 -0700 (PDT) X-Google-Smtp-Source: APiQypIatMAW4rcbJQ3gYtchJrhpVnDVX4TMJ/7wVaPMin6P3Iyr6qi09ulrJTVsD3qtJXmpg/5E X-Received: by 2002:a05:6808:206:: with SMTP id l6mr10409826oie.147.1586108340060; Sun, 05 Apr 2020 10:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586108340; cv=none; d=google.com; s=arc-20160816; b=y2N8+BC3LJyopPu36EmiQRmhb8UMtNxsW5u9HL9AfzgzOJd7o5q+IYAQ9X2gXYA3lj lt7ejQKInOcU82vmft8uoreUbECWcAvIX2M9M6Jqx0g500aYUy88sKnkslrqdt0XKQ1m wZ5lW6FL4lyKgiRN9qw0uJ1eZ9ej9YailTAF05MuoY37O61ekOvmVBRvCLB4RPcxxP3j eeF1OcTz12Jp0vmB+Zlz8PmI8WFxW2ExmDYhinsIroisljMl4A6fOHstNR4ovxvvuUgc 9RTjnxC/+3xvQzr+oFUOxHfCT5AISLtdhyOFDdb/YshsnMY7vgazzzeOZN/rt3izKgqO gMEQ== 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:mime-version :message-id:date:subject:cc:to:from; bh=9vXstYIbVIBU1/HM/d9/21tLM+XSJ4GDeLjec+ZLc1s=; b=01x6+bDKasVhP0nAOCnRVpHftdOPwVC8+MOu+hig4LTfXJrHJYf7zXvX9qKkskdQvr X0gu8Z3Z1rjxm9Nh+kQVc/RihMyNyO+EdzqGtuBxG7DivZzKdLcAOQAJ1sxm3IhmQU8x 1MOq9RL/hBYIPHfU9OedLy9lyAZV48HapFHOjgFGlGwg7QiKUKmAe6ufrI87ob2EwLli pAex4/EVcU+5vKk3f5hImAOpEZB+m2KwBztwJL2Pj8XihIpjq/ouu9nCLUHUAr16pp0G 7bu2m6QG9D0DPFBe+T0QjzA0pVdl6mngFuK9bfxKHvwsEXxCs6pAjplB97gQaFud0Wu4 H82A== 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 e11si6423248oif.131.2020.04.05.10.38.34; Sun, 05 Apr 2020 10:39:00 -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 S1727126AbgDERid (ORCPT + 99 others); Sun, 5 Apr 2020 13:38:33 -0400 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:35208 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726901AbgDERid (ORCPT ); Sun, 5 Apr 2020 13:38:33 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R311e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04428;MF=wuyihao@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0TugAysl_1586108297; Received: from localhost(mailfrom:wuyihao@linux.alibaba.com fp:SMTPD_---0TugAysl_1586108297) by smtp.aliyun-inc.com(127.0.0.1); Mon, 06 Apr 2020 01:38:18 +0800 From: Yihao Wu To: "J . Bruce Fields" , Chuck Lever , NeilBrown , Sasha Levin Cc: linux-nfs@vger.kernel.org Subject: [PATCH v2] SUNRPC/cache: Fix unsafe traverse caused double-free in cache_purge Date: Mon, 6 Apr 2020 01:38:16 +0800 Message-Id: <4568a7cf87f110b8e59fda6f53fda34c550ab403.1586108200.git.wuyihao@linux.alibaba.com> X-Mailer: git-send-email 2.20.1.2432.ga663e714 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- net/sunrpc/cache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c index af0ddd28b081..9649c7fcccd2 100644 --- a/net/sunrpc/cache.c +++ b/net/sunrpc/cache.c @@ -541,7 +541,8 @@ 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