From patchwork Tue Dec 13 18:44:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13072328 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 935C2C4332F for ; Tue, 13 Dec 2022 18:45:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB5C98E0003; Tue, 13 Dec 2022 13:45:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B3E668E0002; Tue, 13 Dec 2022 13:45:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B8168E0003; Tue, 13 Dec 2022 13:45:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 84B328E0002 for ; Tue, 13 Dec 2022 13:45:04 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 24887C0D4E for ; Tue, 13 Dec 2022 18:45:04 +0000 (UTC) X-FDA: 80238160128.02.9DBC94F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 832FF40017 for ; Tue, 13 Dec 2022 18:45:01 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iDIaUpzp; spf=pass (imf11.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670957102; a=rsa-sha256; cv=none; b=ZGAsyiTSp/c+NHYSbCn/Fm+w0UMYQV2n/cLLB3Yi3Ru9ZeRfkSsI1p9S4+8mz31XlEb6nE 5YVPzys8SgD3o9AQ/mFmsejS/x8kZHEP9APsR/N0Y41yqGCfgmg/2kTSCqo2Ls0xeMwrbC Hgif2Yq6qZ3OX/pPTzLm3nnGdnRnVJQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iDIaUpzp; spf=pass (imf11.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670957102; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XKtEb7DX5sCj2p62DhFAWIKVGv8SYAUBUDgGPKa8O/0=; b=MXv+Mwt3jDwCz6ed2nk8Z9mwCNmbShD7Z2LM7b9r4UvEBqD9mVQuCyCLko4UxnfIbDHIGl 2g8I1SZw9BfjEbVGlzdQF38puICE46xDMGTqwI2ZM8ZqrGGZ77Qzu5OjIvqy6puGNjY+Cl 3oQA2OrrBg9NIU70+KEW7J44IhMgRZU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670957101; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XKtEb7DX5sCj2p62DhFAWIKVGv8SYAUBUDgGPKa8O/0=; b=iDIaUpzprq52elQpiRHUJVffhpDXNtGXcLCzjCz0pMdR4N14y/inz3v0URz3btPViytDnk vcjhXNWqRRCPdT3AjHwxjkCxBR/5h2lDvG4e/sGkROiZDa2+qYtcbvuCiPCIB8fUAvwZ08 MfpOBbqf9BEC/Zos/9OiDPG0kxJRx6M= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-261-QAxSZuDjMui-EVsI4VfzIg-1; Tue, 13 Dec 2022 13:44:59 -0500 X-MC-Unique: QAxSZuDjMui-EVsI4VfzIg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9962D3C10147; Tue, 13 Dec 2022 18:44:58 +0000 (UTC) Received: from llong.com (unknown [10.22.32.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id F370BC15BA0; Tue, 13 Dec 2022 18:44:57 +0000 (UTC) From: Waiman Long To: Jens Axboe , Tejun Heo , Josef Bacik , Zefan Li , Johannes Weiner , Andrew Morton Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, =?utf-8?q?Michal_Koutn?= =?utf-8?q?=C3=BD?= , "Dennis Zhou (Facebook)" , Waiman Long , Yi Zhang Subject: [PATCH-block v3 1/2] bdi, blk-cgroup: Fix potential UAF of blkcg Date: Tue, 13 Dec 2022 13:44:45 -0500 Message-Id: <20221213184446.50181-2-longman@redhat.com> In-Reply-To: <20221213184446.50181-1-longman@redhat.com> References: <20221213184446.50181-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 832FF40017 X-Stat-Signature: 1mjtst75o8f8qbajk8apgoaf95m51jft X-HE-Tag: 1670957101-134003 X-HE-Meta: U2FsdGVkX19U3M/aEgjOc4GQ1dPWzJRLnykP6Fx4+XXM3/F8wp13ieWRYfzg4xyk1uUV56uj2QQSNXaD41LuIJrAxoIZWLIZ82mdHCM7Z/SIKn4Q1eDajLDIxyiL9M76vLYcxAa8kxIDuwSFzPqXtUdkKwR0XSIRYIFX/snGLImDWn69e5DDmqS8DY4MwEb7VKDQ+uUH/czBBjWFHn/gYoFlTop5XdP9k0mYqz20VyWDKptSfUrYaWWSHWsBJQxYQW4DpndCpWjfbJfNGHXoazEuChgXqvN+CIpptZSCGzgSTp2vGlr9FF3q3m76JqQ/xackDcaFaoEccirULol82ALJ9oYf4tPMkGx+YJBizm8iiAkEMFteFkn0tnXxpEgVBrQEfjobcAV+XAcaC3xSmcd94SLo+uPKhliVI39hciXgWnYiBwR5OaQadlQPIAZWdItD0KvlJCWIwmtBHbnEdrXAeQi6I6y6Cu6j+wM/yIg6jQyAMKj0x2A2paZRhBBC98QM9fiSjuvPQsx7eyEaMXsWxsoTrDUXwB6i6a+MvYr/wsL7TCvb7WnQK4neLbV0ys2NQbWABXHcQ59M7Q9O4IerEpgRM7D4jTuYkEp0HiX/P5c+JvE1C/+I5suQtBAz8eWMZtra89sRzQUrNHmTB24wiJUlufLm7GRCjDDpYxEcPGFAwkAcnPJ14iDf5H1OMZKjxJymP/RfMfwTROyEwSjokgu/Rkx4CCDTfzCWLbtwykuoTSIWDPmQeUkGwgNDghW7zSqhOU5fOqwmyx1+cjAie24+mMPrAp4d3fnPo4DG42vWqdgE6S5nqmwes9Wl0TZ9fiTHHF33IJRUSYrxCJW62EW7V831tiK+Jy+6NeqkrEVTAa7UNzyjMmqTw8Cv X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Commit 59b57717fff8 ("blkcg: delay blkg destruction until after writeback has finished") delayed call to blkcg_destroy_blkgs() to cgwb_release_workfn(). However, it is done after a css_put() of blkcg which may be the final put that causes the blkcg to be freed as RCU read lock isn't held. Another place where blkcg_destroy_blkgs() can be called indirectly via blkcg_unpin_online() is from the offline_css() function called from css_killed_work_fn(). Over there, the potentially final css_put() call is issued after offline_css(). By adding a css_tryget() into blkcg_destroy_blkgs() and warning its failure, the following stack trace was produced in a test system on bootup. [ 34.254240] RIP: 0010:blkcg_destroy_blkgs+0x16a/0x1a0 : [ 34.339943] Call Trace: [ 34.344510] blkcg_unpin_online+0x38/0x60 [ 34.348523] cgwb_release_workfn+0x6a/0x200 [ 34.352708] process_one_work+0x1e5/0x3b0 [ 34.360758] worker_thread+0x50/0x3a0 [ 34.368447] kthread+0xd9/0x100 [ 34.376386] ret_from_fork+0x22/0x30 This confirms that a potential UAF situation can really happen in cgwb_release_workfn(). Fix that by delaying the css_put() until after the blkcg_unpin_online() call. Also use css_tryget() in blkcg_destroy_blkgs() and issue a warning if css_tryget() fails. The reproducing system can no longer produce a warning with this patch. All the runnable block/0* tests including block/027 were run successfully without failure. Fixes: 59b57717fff8 ("blkcg: delay blkg destruction until after writeback has finished") Suggested-by: Michal Koutný Reported-by: Yi Zhang Signed-off-by: Waiman Long Acked-by: Tejun Heo --- block/blk-cgroup.c | 7 +++++++ mm/backing-dev.c | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 1bb939d3b793..ca28306aa1b1 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1084,6 +1084,13 @@ struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css) */ static void blkcg_destroy_blkgs(struct blkcg *blkcg) { + /* + * blkcg_destroy_blkgs() shouldn't be called with all the blkcg + * references gone. + */ + if (WARN_ON_ONCE(percpu_ref_is_zero(&blkcg->css.refcnt))) + return; + might_sleep(); spin_lock_irq(&blkcg->lock); diff --git a/mm/backing-dev.c b/mm/backing-dev.c index c30419a5e119..36f75b072325 100644 --- a/mm/backing-dev.c +++ b/mm/backing-dev.c @@ -390,11 +390,15 @@ static void cgwb_release_workfn(struct work_struct *work) wb_shutdown(wb); css_put(wb->memcg_css); - css_put(wb->blkcg_css); mutex_unlock(&wb->bdi->cgwb_release_mutex); - /* triggers blkg destruction if no online users left */ + /* + * Triggers blkg destruction if no online users left + * The final blkcg css_put() has to be done after blkcg_unpin_online() + * to avoid use-after-free. + */ blkcg_unpin_online(wb->blkcg_css); + css_put(wb->blkcg_css); fprop_local_destroy_percpu(&wb->memcg_completions);