Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3738011iog; Tue, 21 Jun 2022 05:06:02 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vgVaWLM6uhucTVaMjZP1m7IJGVQY0gVgLoCUlVsIWReVXun+Iv0hM1FnqApZT6PWKS9JLb X-Received: by 2002:a05:6402:50c7:b0:435:923b:9b23 with SMTP id h7-20020a05640250c700b00435923b9b23mr5125316edb.336.1655813162398; Tue, 21 Jun 2022 05:06:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655813162; cv=none; d=google.com; s=arc-20160816; b=tn9f+vUS1oWYde0rVFbfIPZOEH6E4mctLgQ9soxjZpjdsurA6noPdXtpkzz9zsqESV r6PX7NsxyIT3u5SZDGgu7vHLJbGKLe38SKbWfptFVNSniK9+vMc9PwHIls1Fjj32bqHq trDqyRPiL1Aofc4rBYBrKf1ns9JhdZvjqEoIlmf7Gady0Rko8a/XYS+HGCcKWipHbZG7 hV8GxE8VVsM6Rl56zBYBSmw0GlwsNWPaVmvCi9B6jWHgP7wJFq7ycLsRyxa6XqF8nEXz naKjk7Llc6qoIryF6b4+yP1LCXYEVexqeKWDT+gn4GgCFak1Z77LMr8oZ/y43QzqLzUS omhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=JJaowy2nFVuUKm+MuiIK677vovOZvZ2HLOFhOAhG+jA=; b=hOxUP1ajYBIkkIy9CUKGzDsjhzFIwTAWZdCIe/B7cd0ia0RSkfhTGcGM4n2Tzqr4nT 8wMOxDp6KeX6jAcSVHy0j4hZDzoRYokfce32xYbsf3hRvCbDjwhEqYi3M4HFqOA4JN6L yEs8fDg76pFnQeSWtW75eiVUUnuMIBHR7z8TBhz+ulI/Ljz+xdKIAx4qZVOVGsSgEeh2 Os02WcQNoRFu0m7cHwaJpHEtCBdAlhAp6xbEJ1Q52/gmXTLVify4SHRYj+7UA4ijbyuZ UsNEDpO3XB83z0qfB3QY4lLo1uK57qA7TT2S2G4Kl50dtCBT0pdVh3F6neR7/Q9Ru46g A2RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=L4tvklvj; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z12-20020a056402274c00b0043588fb03b6si398575edd.409.2022.06.21.05.05.32; Tue, 21 Jun 2022 05:06:02 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=L4tvklvj; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350380AbiFUMDl (ORCPT + 99 others); Tue, 21 Jun 2022 08:03:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350297AbiFUMDj (ORCPT ); Tue, 21 Jun 2022 08:03:39 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B3A02AE0A for ; Tue, 21 Jun 2022 05:03:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 865D0B8175F for ; Tue, 21 Jun 2022 12:03:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C353C3411C; Tue, 21 Jun 2022 12:03:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655813016; bh=x0F8lQOWpfyP6V0sr7PF7lDJooRfhL/Hx6J8uYMt0yQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=L4tvklvjUzFvOF906LQj+6uhge9ejd2hL+BDBvEOkG8gNJSW/iAEmc/auhdaqnZjt g5VhIzxt2L7krqpoxN/5TGWAmiH8ImDTP/yfASWx2BJ5p7Ia3NlnhY/yad++YpEHY6 ElKC+TfhCa9oqzra6++kW84uCgH20CwFR0oMxROfkykUnln30GHOpAo0mJn+DUy/d3 7CeACcrofZ8DFulkNfJNFyu4LksvAeDrL4dXtrLePmHqGkCubjLD0Fj0u4AchKp8t/ bazZ+7U5rpoPmyuW14r1CLqXzHqoAXl4oyMsJlR59WMfhZnNY5vt0hWPAUwgAqfMQD h0OH/aZ68X9ew== Received: from johan by xi.lan with local (Exim 4.94.2) (envelope-from ) id 1o3cbS-0006Bu-Ex; Tue, 21 Jun 2022 14:03:30 +0200 Date: Tue, 21 Jun 2022 14:03:30 +0200 From: Johan Hovold To: =?utf-8?B?5pm65a6L?= Cc: gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] node: put_device after failing to device_register Message-ID: References: <20220615151738.1766206-1-zhi.song@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Tue, Jun 21, 2022 at 06:17:22PM +0800, 智宋 wrote: > > On Jun 20, 2022, at 21:55, Johan Hovold wrote: > > That's a pretty obvious use-after-free you just added here. You can't > > access dev after you've just freed it. > > > > The name is freed along with the rest of the struct device so you need > > to remove the second explicit free. And you should rename the label too. > > > >> free: > >> kfree(access_node); > > > > But here's another use after free... The put_device() call you added > > will have freed access_node by calling node_access_release(). > dev_set_name() allocates new space to dev->name and assigns the address of space to dev->name if it allocates successfully. But if we fail to allocate space, there isn’t any new space for dev->name. Therefore, there’s no need for calling kfree_const(dev->kobj.name) in dev_set_name()’s error handling. Note that your mails are being rejected by the mailing list since they include HTML. Can you see if you can fix your mail client to send as text instead? That may fix lack line breaks too (break lines at 72 columns or so). > If we fail to do device_register(dev), we just need to put_device(dev) which will do free access_node, kobj.name and other cleanup. > > Maybe the code is: > > --- a/drivers/base/node.c > +++ b/drivers/base/node.c > @@ -144,21 +144,19 @@ static struct node_access_nodes *node_init_node_access(struct node *node, > dev->parent = &node->dev; > dev->release = node_access_release; > dev->groups = node_access_node_groups; > - if (dev_set_name(dev, "access%u", access)) > - goto free; > + if (dev_set_name(dev, "access%u", access)) { > + kfree(access_node); > + return NULL; > + } > > - if (device_register(dev)) > - goto free_name; > + if (device_register(dev)) { > + put_device(dev); > + return NULL; > + } > > pm_runtime_no_callbacks(dev); > list_add_tail(&access_node->list_node, &node->access_list); > return access_node; > -free_name: > - put_device(dev); > - kfree_const(dev->kobj.name); > -free: > - kfree(access_node); > - return NULL; > } The above fix looks correct now. > Thanks for your patience. :) You're welcome. Thanks for finding and fixing the bug. Johan