Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8827141rwb; Tue, 13 Dec 2022 10:55:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf5vjZjx8fyvzvd5pZLWznfFfJfIOs8fWJtsZhmctUfDR9HklC3VLqgYlZ3gFNoOUUyHBsMF X-Received: by 2002:a17:906:e104:b0:7c1:22ad:655a with SMTP id gj4-20020a170906e10400b007c122ad655amr16381468ejb.23.1670957758620; Tue, 13 Dec 2022 10:55:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670957758; cv=none; d=google.com; s=arc-20160816; b=FiAy2qFlzU5//KWIBuh4hz3lXxCKvRZvd920so0sagoBL+goyI/sVC4NMUNT1+eOdW nI5XqDJhQdyIAjxdeoPlb3wkMe8yNjA0n14uUvBQqVErg0rNHA/Ez6yCwfVxPY7Ljdfh 5JkJhzfqjTdDkzSVPI7yPhEC/qAz1Jmb91M8mf1Lmiddwt+BzjqTL/2reulbFSZOnnZ4 70HblaNygCsmGdjICwCwzQinhj7hziuf+gMw8qIrBgOEvXZ+z+8C70zg4EljfbFtnwlr yoBuHFFWiAO6AfFX9p7+Ibb5s+dvKRqW2VFTojgjv//h2upND4oA5Y1lLLeDQeI6mpI7 bwBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XKtEb7DX5sCj2p62DhFAWIKVGv8SYAUBUDgGPKa8O/0=; b=w3OBQikDOfb0sNiv/7O32gV7/LNR7ZRU2P8yu3UrFoaE5Fs15UbqEed6q6O7uEofgl T2eYA3RHGd5gTrqo0EQoHY++ZeDPRE8rtXKERe+x+OLl+58JW6vjiHycE+gqH6UWlmgH kcYCgU1xROLAGSmVwQ9Z17i1PcBu0PQnjyQ4iL2iJfMGZ+pJ4++Cl8nFaj/E/W37oWEH 3x8pwd0uQuHkThgRP/kfY0HKiUDXrm9TPvbCIZ9W4hKub0eyRPqKD3ShTKdgU2bDd3L6 XqkPc8kFo9X/wRWvGvnCA0txRK8kQrVqO25BJDE5U7K2nvOELouLigKXxPBpXARVZtUD Rqtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=D9uTc9m1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dr14-20020a170907720e00b0078d484e0e7esi9334769ejc.488.2022.12.13.10.55.41; Tue, 13 Dec 2022 10:55:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=D9uTc9m1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236116AbiLMSpw (ORCPT + 72 others); Tue, 13 Dec 2022 13:45:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236405AbiLMSpn (ORCPT ); Tue, 13 Dec 2022 13:45:43 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B4E424BF1 for ; Tue, 13 Dec 2022 10:45:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670957102; 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=D9uTc9m1OhSc9IPUc5yr5NhnWvhMRdMI8mHlRTivRTkSXVZ9XqF+/o9WGNVjWc1O6CW2r9 P1FwSBgoNdNwz/Vwd0XClTSqvFUtSfEkP2915Oyh9FUm5syahrc79KSx5lp94IZa8NQd7j /QIIG/DMNa9c1R6ueISwMyus8cPn5a8= 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=20Koutn=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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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); -- 2.31.1