Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp3754974rwi; Wed, 2 Nov 2022 02:44:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM679U3ZNUp3ZfOnZjMzlpGhhznedGPheSh+L/MnyMmZguRQLfqRdEcLrC+PgLSngt7A0rd6 X-Received: by 2002:a17:90a:e38b:b0:212:d42a:c0a9 with SMTP id b11-20020a17090ae38b00b00212d42ac0a9mr41518063pjz.179.1667382251794; Wed, 02 Nov 2022 02:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667382251; cv=none; d=google.com; s=arc-20160816; b=b4Ojj7wIVCQfJeq+puQvblkx5KJPJcuN5J0pQ6kHjjaOV3nyof+FmlD2eVS3FArYiY Srv6Dz8W2vmEcFYXAPEgMW4fkWmK7YwN4tYOrboL6mEwNAcRKniT0mEiPrGzG6k6c8+V 17t4twoiqURCppKno5acJdjbDYe14hXNRuoyuxGeM/qMT/BC6mP7RM6SZdQMAwVI5ngw T7kFoUAnXwvvLUTOR1lsA8R8DtLdknRtAojlQ2sjcSc0d5/ZjTtODcRtmQNKXrQYx2oT LnjFcygWSPsOquzK1jpoCBLihnvGoVcnQODWl3/Tcw9i36iau0L14K3CZ7ooo/a4AsWz RX7g== 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=Fxxn1EtGefbBmnWxZ8XrxEIUXbiJwyzDeE5y9WnF5dA=; b=xuec+Ohs0flyPKt56XXIlGvQyF+BUjtKx3A2xGfc346um1Y27K067HRZiBE1uB4NTA HbU2hhV+dPDa0TixvS+BvZqEfEKaTsboPtJ97pMnlx7Pu+qM+sdq7YFoDhCz5FHinEnK Mvh5VlPLxWdhP38Y4tBbOpJhL9+50VLujhKG3DeRIGkh3aCpOKOofjSOqLIIGbKx8S1A ehcRvrBZe3hSPZnznIbYTE2OfQZkWt6oKBizP+bejxb6RRjH3uMyhWIzyQHSHmOJn6IK tcsNF2vnDSBx4ZCe+Fbv49KcfU54WQIhHRqa6l3gRLt4QndsFjt1uEFm5uB9VHmamKPK si6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b="PtXD1S/+"; 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 m5-20020a170902e40500b00186e8c3782esi12735990ple.386.2022.11.02.02.43.58; Wed, 02 Nov 2022 02:44:11 -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="PtXD1S/+"; 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 S230307AbiKBJgG (ORCPT + 99 others); Wed, 2 Nov 2022 05:36:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229713AbiKBJgE (ORCPT ); Wed, 2 Nov 2022 05:36:04 -0400 Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 649C46589; Wed, 2 Nov 2022 02:36:01 -0700 (PDT) Received: from booty (unknown [77.244.183.192]) (Authenticated sender: luca.ceresoli@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id B6BC1240004; Wed, 2 Nov 2022 09:35:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1667381759; 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=Fxxn1EtGefbBmnWxZ8XrxEIUXbiJwyzDeE5y9WnF5dA=; b=PtXD1S/+PYPuTw+0ABw0LDMREsiFR8UIk9Rt+urGLzui62MyUwWUmpQ0TGuc0oOJlrvEcR HKCCoSQDA1vC2gq8eVHDUmrPtp6VIuEtEB/nLGQJqcAJ2LWxhc05PoyWr5wZF8gMmbHTM2 ADuYsLLkMSWr3yX/j/Z5v0yDa+Tp2q1cABNreOV+QTrHtOPP6SXc8Ske81OELMNWVvR613 /ahgljVxTG2akxnhZXa9Soj/ZlujC2ECgzl9Usa7qzJyJ3G5/be3YU0/4qSX7UPvAQ8Uxc jnYoyijD3fep9LEncAlhn7lSZpKYbKljWydwwp1XQbXjlxduzu81XWZT/j+pFA== Date: Wed, 2 Nov 2022 10:35:56 +0100 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: <20221102103556.4f556d6c@booty> In-Reply-To: References: <20221028081926.2320663-1-luca.ceresoli@bootlin.com> <20221028185847.5454a98d@booty> 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,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 20:06:34 +0300 Dan Carpenter wrote: > On Fri, Oct 28, 2022 at 06:58:47PM +0200, Luca Ceresoli wrote: > > 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. > > I assumed that tegra_csi_channels_cleanup() would clean up if > tegra_csi_channel_alloc() fails. Otherwise then that's several > different even worse bugs. > > HINT: Let's all hope my initial analysis was correct. Indeed you have a point. Apologies for having replied in a hurry and for the resulting noise. I'm going to send a patch to fix the chan->mipi mess. However I think the best way to do so would be a oneliner: @@ -491,6 +491,7 @@ static int tegra_csi_channel_alloc(struct tegra_csi *csi, chan->mipi = tegra_mipi_request(csi->dev, node); if (IS_ERR(chan->mipi)) { ret = PTR_ERR(chan->mipi); + chan->mipi = NULL; dev_err(csi->dev, "failed to get mipi device: %d\n", ret); } This would be a correct implementation of the initial intent as it can be inferred from the code, i.e. chan->mipi being either NULL or a valid pointer. This makes sense as chan->mipi is assigned in a single place. -- Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com