From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8818C38145 for ; Thu, 8 Sep 2022 22:25:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229817AbiIHWZo (ORCPT ); Thu, 8 Sep 2022 18:25:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbiIHWZl (ORCPT ); Thu, 8 Sep 2022 18:25:41 -0400 Received: from cae.in-ulm.de (cae.in-ulm.de [217.10.14.231]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 56A46A3455 for ; Thu, 8 Sep 2022 15:25:39 -0700 (PDT) Received: by cae.in-ulm.de (Postfix, from userid 1000) id 9E7A914002E; Fri, 9 Sep 2022 00:25:36 +0200 (CEST) Date: Fri, 9 Sep 2022 00:25:36 +0200 From: "Christian A. Ehrhardt" To: Tejun Heo Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: Re: [PATCH] kernfs: fix use-after-free in __kernfs_remove Message-ID: References: <20220907200811.654034-1-lk@c--e.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Tejun, On Thu, Sep 08, 2022 at 07:14:43AM -1000, Tejun Heo wrote: > Hello, Christian. > > On Wed, Sep 07, 2022 at 10:08:11PM +0200, Christian A. Ehrhardt wrote: > > Concurrent calls to __kernfs_remove can race on the removal > > of the root node: The race occurs if the root node(kn) is freed > > during kernfs_drain. The child node(pos) is explicitly protected > > with an additional ref count. Do the same for the root node. > > I don't think this is right. We don't support parallel invocations of > __kernfs_remove() this way. If @kn can be freed during kernfs_drain(), it > also means that it can be freed before kernfs_rwsem is grabbed in > kernfs_remove(). Point taken. However, the syzkaller reproducer reliably triggers the bug without the patch and the bug is gone with the patch. > The caller must be responsible for ensuring that @kn > remains allocated. Otherwise, it can't be made reliable. In this case the caller of __kernfs_remove is not kernfs_remove but kernfs_remove_by_name_ns and it fails to take a reference for the node that it looks up and deletes. Thus a second call to kernfs_remove_by_name_ns can remove the node while kernfs_drain drops the semaphore. I'll post an updated patch tomorrow. regards Christian