Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2383623rdb; Thu, 21 Sep 2023 18:01:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGIr4RGgF6TMtnSzoJPnyZ/p2BV48lJaNZjFSAaUwEV7MuB98okI8gQydxpGhaRLjfetI5C X-Received: by 2002:a05:6808:319:b0:3ad:fdfb:d384 with SMTP id i25-20020a056808031900b003adfdfbd384mr3889764oie.53.1695344481792; Thu, 21 Sep 2023 18:01:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695344481; cv=none; d=google.com; s=arc-20160816; b=K0vCpoiK6LU+HxZtH/f1Ez0Hb2wg2nO8Sm+nLT6FIn/iKYtEHBzDbcBrxWkTa66VAm BR0U++NnE/YywPUIY8iD/gvnVrJqHShGJw88/qdqjiRrZvhtxyB92XkaR9Fye0kPZIZu 7K32uWsc9YPh2zw68XFrxIDIl0utIqyoP9jSFZ5NNtjh+0kb+UWxe7xfH4f3DBjsaxjW QGBy+53eEgHKNALCsfNobRlYn0Xgb8BH0EgUMGXojrL3VgTMcljqq7LoIChZSG7c/JxM FWnxnoO1USVdAKQtwnqBYfCkPC6WO2v5pbwwNiRqEnhhAzb/A8y1HC9jBTgZ1l4Uf+FF fd9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=Hw98Zupb8UHh+CqlMSyJaPhi0/ITpRz5yyNHO/GwHao=; fh=BbQZtnGGchltJYTjPsJ+R0KXMRLP/IiPP0gvgqHkAkY=; b=nGdW+OJtYh7Xo73h1xBsP9Rf14VEM9qzq/xiABDsmFT/YdaUltH1I4KvGlH77nJv3n 6UwOBDCIoLws0wYTVaH89cs4Hmu8GaYcleg+NgId3hXtbfbEK6LjOmgmLq/pEKv58tpH /2XYIIGVBaEPzvl+weUQiqCqOoXFGyCTWwT6Y1zjioM2P/eNMgPDkJLavdD/DjsyWr0Q em80qx7pcn9qmIn7xCoVc7hml2+7ES7creoRLqMHx4tOhQNrdcGdhd1tkdO6ypZe+NJQ +Wv/ldNpJ+Uz2yO6Cgp/+D/0VxRKRnefGEBcdN0/6t6dwcKuMpeu4c2237Pek691Rxhg vaAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OXQoiftL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id u8-20020a056a00158800b00690f8ed35dbsi2870179pfk.345.2023.09.21.18.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 18:01:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OXQoiftL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 2C03E80ACC58; Thu, 21 Sep 2023 13:46:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232209AbjIUUp4 (ORCPT + 99 others); Thu, 21 Sep 2023 16:45:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232179AbjIUUpi (ORCPT ); Thu, 21 Sep 2023 16:45:38 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E09DCFF31 for ; Thu, 21 Sep 2023 11:20:35 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59beea5ce93so24115917b3.0 for ; Thu, 21 Sep 2023 11:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695320434; x=1695925234; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Hw98Zupb8UHh+CqlMSyJaPhi0/ITpRz5yyNHO/GwHao=; b=OXQoiftLHS+vkpFjzdG4wZSNGwcqJ3uPKs1hCgcLuhDOUa4fAa3bxLzV4wOcS0nGfv jlj4CgYH2GaRKu5Cq3oAYiPW+xjn96HA+p76PI9twOmKaJ+L7bgv53lV0Mn2nOol+kqR X3RmpKfHu0vlGMA+l+CDDPoXyLx8VAZrNL6FR48BTXykeqvmQNui27vq02b6x+TGSX/C gIo6cVMxXeBCl3dKBFCkLgZWryF0nHyzwDvlR9XZArQ6MZ6vzCkHtqPVSlUJSnPsp0xC l3q7GCXxUPoxoaXo2qfeu/Stf6uHSEPELPvNgIAkcPvtaQuIXlN1nrI3L7UUrhVssir0 ZV2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695320434; x=1695925234; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Hw98Zupb8UHh+CqlMSyJaPhi0/ITpRz5yyNHO/GwHao=; b=Q6VHglsexV/AUmv9fpLcDQ4+B6lj87Ci3uJoihbEASLDcw+ArcF7u45I90pfIjv8qc JFw6qdT7ywaU6XFi6kVEDCLnH/eYfRstjVsoARTyKgwkEYkqdn0nVSNCf+wAOCg1Mv7/ lYAm0zp9RkammprWZ2hFHoAx6OgGQTp2MLD/JCgLDwvQSE7kRfrmbQDU0H5urHZkq1QR 9o2+2XpHubU+iu/00QrKTLhT2n6w5TxhmxeOQfYMfWjtx2SWo0nWic1kPMgwKf1TAaKy SJ87CK5NOxmOvsUPsgwbFWZsRv0N5/sl18SejhFGYDao7R06blZjo3jc9bB+N3KDRV+a wR3Q== X-Gm-Message-State: AOJu0YxvUScMo9HA4fHFoFue+v5EN3tLcGvXplyNRhKtrv/VhxA0rog3 PhwlJJjFZtBfJZbJ/JWAVJJ8NuiVLuQAk1FZEBo= X-Received: from saranyamohan.svl.corp.google.com ([2620:15c:2a3:200:5dbd:3ff1:e146:3806]) (user=saranyamohan job=sendgmr) by 2002:a05:690c:831:b0:589:a533:405b with SMTP id by17-20020a05690c083100b00589a533405bmr6592ywb.3.1695320434564; Thu, 21 Sep 2023 11:20:34 -0700 (PDT) Date: Thu, 21 Sep 2023 11:20:12 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230921182012.3965572-1-saranyamohan@google.com> Subject: [PATCH] block: fix use-after-free of q->q_usage_counter From: Saranya Muruganandam To: Jens Axboe , Tejun Heo , Ming Lei , stable@vger.kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Zhang Wensheng , Zhong Jinghua , Hillf Danton , Yu Kuai , Dennis Zhou , Saranya Muruganandam Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:46:18 -0700 (PDT) From: Ming Lei commit d36a9ea5e7766961e753ee38d4c331bbe6ef659b upstream. For blk-mq, queue release handler is usually called after blk_mq_freeze_queue_wait() returns. However, the q_usage_counter->release() handler may not be run yet at that time, so this can cause a use-after-free. Fix the issue by moving percpu_ref_exit() into blk_free_queue_rcu(). Since ->release() is called with rcu read lock held, it is agreed that the race should be covered in caller per discussion from the two links. Backport-notes: Not a clean cherry-pick since a lot has changed, however essentially the same fix. Reported-by: Zhang Wensheng Reported-by: Zhong Jinghua Link: https://lore.kernel.org/linux-block/Y5prfOjyyjQKUrtH@T590/T/#u Link: https://lore.kernel.org/lkml/Y4%2FmzMd4evRg9yDi@fedora/ Cc: Hillf Danton Cc: Yu Kuai Cc: Dennis Zhou Fixes: 2b0d3d3e4fcf ("percpu_ref: reduce memory footprint of percpu_ref in fast path") Signed-off-by: Ming Lei Link: https://lore.kernel.org/r/20221215021629.74870-1-ming.lei@redhat.com Signed-off-by: Jens Axboe Signed-off-by: Saranya Muruganandam --- block/blk-core.c | 2 -- block/blk-sysfs.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/block/blk-core.c b/block/blk-core.c index d0d0dd8151f7..e5eeec801f56 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -414,8 +414,6 @@ void blk_cleanup_queue(struct request_queue *q) blk_mq_sched_free_requests(q); mutex_unlock(&q->sysfs_lock); - percpu_ref_exit(&q->q_usage_counter); - /* @q is and will stay empty, shutdown and put */ blk_put_queue(q); } diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c index 8c5816364dd1..9174137a913c 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -726,6 +726,8 @@ static void blk_free_queue_rcu(struct rcu_head *rcu_head) { struct request_queue *q = container_of(rcu_head, struct request_queue, rcu_head); + + percpu_ref_exit(&q->q_usage_counter); kmem_cache_free(blk_requestq_cachep, q); } -- 2.42.0.515.g380fc7ccd1-goog