Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2737628rwi; Fri, 28 Oct 2022 10:33:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4/7Y2ETEcyBWIu3mQzdHphFituDhXjtcHzYuI/dgn667QjMaOEhPMVizgEg3RmLg3CeFl1 X-Received: by 2002:a17:90b:3141:b0:20d:1690:d3d2 with SMTP id ip1-20020a17090b314100b0020d1690d3d2mr17679106pjb.230.1666978414861; Fri, 28 Oct 2022 10:33:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666978414; cv=none; d=google.com; s=arc-20160816; b=kWyAqIky1+aWvHbAwjavQa9pwa4F5KETO3kWHh98GpcAggaKAMCJo2Wtk3iIxbn/vY lyzUsvbJ/ijiVjFvd3WGLWqlOmiC49vYPJPOw9eQolDU3vmZYq6p7qwabTnYhcBAOVf5 55bfAn2UJLWy/THp3bM6QjSWvbZntUHHDGS0+Xg25sA/3dtEK89UFvZBYPId+9Uoc3o6 5mXOdDqzFiGv16p6nEbHwxuBExkFppcCV0NAitY3GmX+2HXFimqXvCbdqnG1jTBMwzSL y0HMxFeWx9L8UbSrr110ZUnxxpjbO+VzrCsS2OZkDZ0KTSE5/v9eq/Az+djbEr5b+aIU swbw== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=LySexlPUtzLuiqijp03u4qPQFZvUnwDE7D7mZqts+BI=; b=00pP0VBU0TM0S/msWlwM2BdmhIs1/LSZ0TetfHb6H9nujvV+Lkuz9lJuIkmwAhsIbP KRRxyVo2KRfISg8RqkTqFhnKP3qpKy3pMXeHxuo5y9kH8Y4zyqV5Diyy1u4/Dt77bAr8 RtEk2jRLy0XX2LdJQboM71PJtn+IdmQ5eJah2AlJslxE+PUCSMNmBs2OXuMwYOE/SMoS rRvO2MDXaDpqBlGRIwW6BWL4J0D9pzbQSfjE/jh2xfYvmw2NyAbwHVWyaU0S/kHRUq21 LXjH0A0b2Qk/aegArRGx6p52S23XIacdIzChhhr5c+w8FNAD59FS/PoaQjQUayYrFMKP X8xA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=gBrCAOhU; 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=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jd5-20020a170903260500b00178b5e035fcsi5215567plb.496.2022.10.28.10.33.22; Fri, 28 Oct 2022 10:33:34 -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=@bootlin.com header.s=gm1 header.b=gBrCAOhU; 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=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbiJ1Q7Y (ORCPT + 99 others); Fri, 28 Oct 2022 12:59:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230413AbiJ1Q6y (ORCPT ); Fri, 28 Oct 2022 12:58:54 -0400 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23EFFDAC57; Fri, 28 Oct 2022 09:58:51 -0700 (PDT) Received: from booty (unknown [77.244.183.192]) (Authenticated sender: luca.ceresoli@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id DD13EC0007; Fri, 28 Oct 2022 16:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1666976330; 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=LySexlPUtzLuiqijp03u4qPQFZvUnwDE7D7mZqts+BI=; b=gBrCAOhUwLUGsvMweUyfoN8BdOVVq8XipO6tM3tckCusB3+p5Bb5QLW3LLvVzCNiHSq9iK zfMsafSKiKtLbbDrm74Uw7IBCEysdFaqaltS/jDrWPTbYSjuByeNCsJDmiIjkxiBQjYPBa uG+fCs7yBVYyCNQ+24ThievHvkpC/GthSm76c5IpWX4dRANvk2twPYVtw/CRZ+vTPL01d+ /FlbIoitf9IZCbBPfYAHeEtIa1xBOWtTJt1PMz2GwHA7OZHOx9IU+TtZ/O9tg8d5rpTf0n 8jAkD2X+SUt4BKERIBmBeagazD3niMfwFiNbuWWgfylEdYZltoEmfZHRllVlIQ== Date: Fri, 28 Oct 2022 18:58:47 +0200 From: Luca Ceresoli To: Dan Carpenter Cc: Thierry Reding , Jonathan Hunter , Sowjanya Komatineni , Mauro Carvalho Chehab , Greg Kroah-Hartman , Hans Verkuil , Thomas Petazzoni , stable@vger.kernel.org, Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-tegra@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH] staging: media: tegra-video: fix device_node use after free Message-ID: <20221028185847.5454a98d@booty> In-Reply-To: References: <20221028081926.2320663-1-luca.ceresoli@bootlin.com> Organization: Bootlin X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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 Hello Dan, On Fri, 28 Oct 2022 15:34:39 +0300 Dan Carpenter wrote: > On Fri, Oct 28, 2022 at 10:19:26AM +0200, luca.ceresoli@bootlin.com wrote: > > From: Luca Ceresoli > > > > At probe time this code path is followed: > > > > * tegra_csi_init > > * tegra_csi_channels_alloc > > * for_each_child_of_node(node, channel) -- iterates over channels > > * automatically gets 'channel' > > * tegra_csi_channel_alloc() > > * saves into chan->of_node a pointer to the channel OF node > > * automatically gets and puts 'channel' > > * now the node saved in chan->of_node has refcount 0, can disappear > > * tegra_csi_channels_init > > * iterates over channels > > * tegra_csi_channel_init -- uses chan->of_node > > > > After that, chan->of_node keeps storing the node until the device is > > removed. > > > > of_node_get() the node and of_node_put() it during teardown to avoid any > > risk. > > > > Fixes: 1ebaeb09830f ("media: tegra-video: Add support for external sensor capture") > > Cc: stable@vger.kernel.org > > Cc: Sowjanya Komatineni > > Signed-off-by: Luca Ceresoli > > --- > > drivers/staging/media/tegra-video/csi.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/staging/media/tegra-video/csi.c b/drivers/staging/media/tegra-video/csi.c > > index b26e44adb2be..1b05f620b476 100644 > > --- a/drivers/staging/media/tegra-video/csi.c > > +++ b/drivers/staging/media/tegra-video/csi.c > > @@ -433,7 +433,7 @@ static int tegra_csi_channel_alloc(struct tegra_csi *csi, > > for (i = 0; i < chan->numgangports; i++) > > chan->csi_port_nums[i] = port_num + i * CSI_PORTS_PER_BRICK; > > > > - chan->of_node = node; > > + chan->of_node = of_node_get(node); > > chan->numpads = num_pads; > > if (num_pads & 0x2) { > > chan->pads[0].flags = MEDIA_PAD_FL_SINK; > > @@ -640,6 +640,7 @@ static void tegra_csi_channels_cleanup(struct tegra_csi *csi) > > media_entity_cleanup(&subdev->entity); > > } > > > > + of_node_put(chan->of_node); > > list_del(&chan->list); > > kfree(chan); > > Not related to your patch, but this kind of "one function cleans up > everything" style is always buggy. For example, here it should be: > > - if (chan->mipi) > + if (!IS_ERR_OR_NULL(chan->mipi)) > tegra_mipi_free(chan->mipi); I sort of agree the code could be clearer here, but looking at the code in detail, this cannot happen. chan->mipi is set in one place only, and if it is an error the whole probe fails. So it can be either NULL or a valid pointer here. Regarding my patch, do you think it is valid? Best regards. -- Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com