Skip to contentSkip to conversation

iio: iio_alloc_device(): Free device correctly on error

PATCH In review Lars-Peter Clausen 3 participants 5 messages Oct 31, 2021 – Dec 5, 2021 1 message not in the archive IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 iio: iio_alloc_device(): Free device correctly on error

Once device_initialize() has been called on a struct device the device must be freed by decreasing the reference count rather than directly freeing the underlying memory.

This is so that any additional state and resources associated with the device get properly freed.

In this particular case there are no additional resources associated with the device and no additional reference count. So there is no resource leak or use-after-free by freeing the struct device directly

But in order to follow best practices and avoid accidental future breakage use put_device() instead of kfree() to free the device when an error occurs.

---
No fixes tag since, while the code is wrong, it works. No leaks and no use-after-free.

 drivers/iio/industrialio-core.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/iio/industrialio-core.c +9 -8
index 463a63d5bf56..669218365277 100644
@@ -1615,7 +1615,8 @@ static void iio_dev_release(struct device *device)
 
 	iio_device_detach_buffers(indio_dev);
 
-	ida_simple_remove(&iio_ida, iio_dev_opaque->id);
+	if (iio_dev_opaque->id >= 0)
+		ida_simple_remove(&iio_ida, iio_dev_opaque->id);
 	kfree(iio_dev_opaque);
 }
 
@@ -1662,20 +1663,20 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
 	if (iio_dev_opaque->id < 0) {
 		/* cannot use a dev_err as the name isn't available */
 		pr_err("failed to get device id\n");
-		kfree(iio_dev_opaque);
-		return NULL;
+		goto err_put_device;
 	}
 
-	if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
-		ida_simple_remove(&iio_ida, iio_dev_opaque->id);
-		kfree(iio_dev_opaque);
-		return NULL;
-	}
+	if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id))
+		goto err_put_device;
 
 	INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
 	INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
 
 	return indio_dev;
+
+err_put_device:
+	put_device(&indio_dev->dev);
+	return NULL;
 }
 EXPORT_SYMBOL(iio_device_alloc);
 
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 iio: viio_trigger_alloc(): Correctly free trigger on error

Once device_initialize() has been called on a struct device the device must be freed by decreasing the reference count rather than directly freeing the underlying memory.

This is so that any additional state and resources associated with the device get properly freed.

In this particular case there are no additional resources associated with the device and no additional reference count. So there is no resource leak or use-after-free by freeing the struct device directly

But in order to follow best practices and avoid accidental future breakage use put_device() instead of kfree() to free the device when an error occurs.

---
 drivers/iio/industrialio-trigger.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/iio/industrialio-trigger.c +5 -7
index 93990ff1dfe3..d566e8d4a14b 100644
@@ -480,7 +480,7 @@ static void iio_trig_release(struct device *device)
 	struct iio_trigger *trig = to_iio_trigger(device);
 	int i;
 
-	if (trig->subirq_base) {
+	if (trig->subirq_base > 0) {
 		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
 			irq_modify_status(trig->subirq_base + i,
 					  IRQ_NOAUTOEN,
@@ -541,11 +541,11 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 					    CONFIG_IIO_CONSUMERS_PER_TRIGGER,
 					    0);
 	if (trig->subirq_base < 0)
-		goto free_trig;
+		goto err_put_trig;
 
 	trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
 	if (trig->name == NULL)
-		goto free_descs;
+		goto err_put_trig;
 
 	trig->subirq_chip.name = trig->name;
 	trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
@@ -559,10 +559,8 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 
 	return trig;
 
-free_descs:
-	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
-free_trig:
-	kfree(trig);
+err_put_trig:
+	put_device(&trig->dev);
 	return NULL;
 }
 
Signature · 1 line
2.20.1

A message that is not in the archive (sent off-list or not yet received): CAHp75VfT-VgMODDdZCy8ERh1Uw8HVR6YuzmTukeP+nHbrx++sg@mail.gmail.com

#3 lore raw

Re: [PATCH 2/2] iio: viio_trigger_alloc(): Correctly free trigger on error

Quoted 40 lines · Andy Shevchenko

On Sunday, October 31, 2021, Lars-Peter Clausen <[email protected] <mailto:[email protected]>> wrote:

Once device_initialize() has been called on a struct device the device must
be freed by decreasing the reference count rather than directly freeing the
underlying memory.

This is so that any additional state and resources associated with the device get properly freed.

In this particular case there are no additional resources associated with
the device and no additional reference count. So there is no resource leak
or use-after-free by freeing the struct device directly

But in order to follow best practices and avoid accidental future breakage
use put_device() instead of kfree() to free the device when an error occurs.

<mailto:[email protected]>>
---
 drivers/iio/industrialio-trigger.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 93990ff1dfe3..d566e8d4a14b 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -480,7 +480,7 @@ static void iio_trig_release(struct device *device)
        struct iio_trigger *trig = to_iio_trigger(device); int i;

Quoted 6 lines

-       if (trig->subirq_base) {
+       if (trig->subirq_base > 0) {

Quoted 1 line

= ?

I don't know. 0 is not supposed to be a valid irq number. And we kzalloc() the struct, so if it hasn't been explicitly initialized we'd get 0.

The way the code is at the moment we'd never end up here without calling irq_alloc_descs(), so it is either a valid irq or a negative error code and I can see why you might want to use >= for consistency and symmetry.

Quoted 45 lines
                    for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER;
    i++) {
                            irq_modify_status(trig->subirq_base + i,
                                              IRQ_NOAUTOEN,
    @@ -541,11 +541,11 @@ struct iio_trigger
    *viio_trigger_alloc(struct device *parent,
    CONFIG_IIO_CONSUMERS_PER_TRIGGER,
                                                0);
            if (trig->subirq_base < 0)
    -               goto free_trig;
    +               goto err_put_trig;

            trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
            if (trig->name == NULL)
    -               goto free_descs;
    +               goto err_put_trig;

            trig->subirq_chip.name <http://subirq_chip.name> = trig->name;
            trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
    @@ -559,10 +559,8 @@ struct iio_trigger *viio_trigger_alloc(struct
    device *parent,

            return trig;

    -free_descs:
    -       irq_free_descs(trig->subirq_base,
    CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    -free_trig:
    -       kfree(trig);
    +err_put_trig:
    +       put_device(&trig->dev);
            return NULL;
     }

    -- 
    2.20.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH 2/2] iio: viio_trigger_alloc(): Correctly free trigger on error

Quoted 3 lines · Lars-Peter Clausen
Quoted 2 lines · Andy Shevchenko

On Sunday, October 31, 2021, Lars-Peter Clausen <[email protected] <mailto:[email protected]>> wrote:

...

Quoted 8 lines
Quoted 4 lines

- if (trig->subirq_base) {
+ if (trig->subirq_base > 0) {

Quoted 1 line

= ?

I don't know. 0 is not supposed to be a valid irq number. And we kzalloc() the struct, so if it hasn't been explicitly initialized we'd get 0.

But it will change the behaviour of the code.

Quoted 6 lines

=0 is the opposite of replacing < 0.

The way the code is at the moment we'd never end up here without calling irq_alloc_descs(), so it is either a valid irq or a negative error code and I can see why you might want to use >= for consistency and symmetry.

Right!

(But on some architectures and cases 0 might be a valid vIRQ)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH 2/2] iio: viio_trigger_alloc(): Correctly free trigger on error

Quoted 20 lines · On Sun, 31 Oct 2021 15:00:38 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Lars-Peter Clausen
Quoted 2 lines · Andy Shevchenko

On Sunday, October 31, 2021, Lars-Peter Clausen <[email protected] <mailto:[email protected]>> wrote:

...

Quoted 8 lines
Quoted 4 lines

- if (trig->subirq_base) {
+ if (trig->subirq_base > 0) {

Quoted 1 line

= ?

I don't know. 0 is not supposed to be a valid irq number. And we kzalloc() the struct, so if it hasn't been explicitly initialized we'd get 0.

But it will change the behaviour of the code.

Quoted 1 line

=0 is the opposite of replacing < 0.

Quoted 8 lines
Quoted 3 lines

The way the code is at the moment we'd never end up here without calling irq_alloc_descs(), so it is either a valid irq or a negative error code and I can see why you might want to use >= for consistency and symmetry.

Right!

(But on some architectures and cases 0 might be a valid vIRQ)

Given I'm fairly sure this will be after any other irqs we should be fine but I don't think it would be a problem to allow 0.

If that's fine with both of you I can just change it to >= 0 whilst applying, or Lars can do a v2 when has time.

Thanks,

Jonathan