linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Bjorn Andersson' <bjorn.andersson@linaro.org>,
	Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Cc: "linux-arm-msm@vger.kernel.org" <linux-arm-msm@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"thara.gopinath@linaro.org" <thara.gopinath@linaro.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: RE: [PATCH] soc: qcom: aoss: Fix the out of bound usage of cooling_devs
Date: Tue, 29 Jun 2021 15:54:51 +0000	[thread overview]
Message-ID: <4a0f56e3cb02435b911e8d35f6a83529@AcuMS.aculab.com> (raw)
In-Reply-To: <YNs/o7VVh+JnbxK9@yoga>

From: Bjorn Andersson
> Sent: 29 June 2021 16:44
> 
> On Mon 28 Jun 12:27 CDT 2021, Manivannan Sadhasivam wrote:
> 
> > In "qmp_cooling_devices_register", the count value is initially
> > QMP_NUM_COOLING_RESOURCES, which is 2. Based on the initial count value,
> > the memory for cooling_devs is allocated. Then while calling the
> > "qmp_cooling_device_add" function, count value is post-incremented for
> > each child node.
> >
> > This makes the out of bound access to the cooling_dev array. Fix it by
> > resetting the count value to zero before adding cooling devices.
> >
> > While at it, let's also free the memory allocated to cooling_dev if no
> > cooling device is found in DT and during unroll phase.
> >
> > Cc: stable@vger.kernel.org # 5.4
> > Fixes: 05589b30b21a ("soc: qcom: Extend AOSS QMP driver to support resources that are used to wake
> up the SoC.")
> > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > ---
> >
> > Bjorn: I've just compile tested this patch.
> >
> >  drivers/soc/qcom/qcom_aoss.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/drivers/soc/qcom/qcom_aoss.c b/drivers/soc/qcom/qcom_aoss.c
> > index 934fcc4d2b05..98c665411768 100644
> > --- a/drivers/soc/qcom/qcom_aoss.c
> > +++ b/drivers/soc/qcom/qcom_aoss.c
> > @@ -488,6 +488,7 @@ static int qmp_cooling_devices_register(struct qmp *qmp)
> >  	if (!qmp->cooling_devs)
> >  		return -ENOMEM;
> >
> > +	count = 0;
> 
> This will address the immediate problem, which is that we assign
> cooling_devs[2..] in this loop. But, like Matthias I don't think we
> should use "count" as a constant in the first hunk and then reset it
> and use it as a counter in the second hunk.
> 
> Frankly, I don't see why cooling_devs is dynamically allocated (without
> being conditional on there being any children).

Not to mention what happens if there are 3 matching nodes.
Given that qmp_cooling_device is only 4 pointers why bother
allocating a pointer to it at all.
Just instantiate 2 of them in the outer structure.
No is going to notice 56 bytes - it is probably hidden in the
padding when the outer structure is allocated.


> So, could you please make the cooling_devs an array of size
> QMP_NUM_COOLING_RESOURCES, remove the dynamic allocation here, just
> initialize count to 0 and add a check in the loop to generate an error
> if count == QMP_NUM_COOLING_RESOURCES?
> 

You should set count to 0 just before this loop - not at the top
of the function.

> >  	for_each_available_child_of_node(np, child) {
> >  		if (!of_find_property(child, "#cooling-cells", NULL))
> >  			continue;
> > @@ -497,12 +498,16 @@ static int qmp_cooling_devices_register(struct qmp *qmp)
> >  			goto unroll;
> >  	}
> >
> > +	if (!count)
> > +		devm_kfree(qmp->dev, qmp->cooling_devs);
> 
> I presume this is just an optimization, to get some memory back when
> there's no cooling devices specified in DT.  I don't think this is
> necessary and this made me want the static sizing of the array..
> 
> > +
> >  	return 0;
> >
> >  unroll:
> >  	while (--count >= 0)
> >  		thermal_cooling_device_unregister
> >  			(qmp->cooling_devs[count].cdev);
> > +	devm_kfree(qmp->dev, qmp->cooling_devs);
> 
> I don't remember why we don't fail probe() when this happens, seems like
> the DT properties should be optional but the errors adding them should
> be fatal. But that's a separate issue.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


      reply	other threads:[~2021-06-29 15:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-28 17:27 [PATCH] soc: qcom: aoss: Fix the out of bound usage of cooling_devs Manivannan Sadhasivam
2021-06-28 23:03 ` Matthias Kaehlcke
2021-06-29  4:25   ` Manivannan Sadhasivam
2021-06-29 14:17     ` Matthias Kaehlcke
2021-06-29 15:28       ` Manivannan Sadhasivam
2021-06-29 15:43 ` Bjorn Andersson
2021-06-29 15:54   ` David Laight [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4a0f56e3cb02435b911e8d35f6a83529@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=bjorn.andersson@linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manivannan.sadhasivam@linaro.org \
    --cc=stable@vger.kernel.org \
    --cc=thara.gopinath@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).