2011-05-09 14:07:00

by Vladimir Motyka

[permalink] [raw]
Subject: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

When allocation of idata fails there was a null dereferece.

Signed-off-by: Vladimir Motyka <[email protected]>

---
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 407836d..3dec493 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -266,10 +266,10 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
return idata;

copy_err:
- kfree(idata->buf);
+ if(idata)
+ kfree(idata->buf);
kfree(idata);
return ERR_PTR(err);
-
}

static int mmc_blk_ioctl_cmd(struct block_device *bdev,


2011-05-09 14:32:49

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On Mon, 9 May 2011, Vladimir Motyka wrote:

> When allocation of idata fails there was a null dereferece.

Why not have a different label for the two cases? That would make the
code easier to statically analyze, and perhaps be more understandable as
well.

julia


> Signed-off-by: Vladimir Motyka <[email protected]>
>
> ---
> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
> index 407836d..3dec493 100644
> --- a/drivers/mmc/card/block.c
> +++ b/drivers/mmc/card/block.c
> @@ -266,10 +266,10 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
> return idata;
>
> copy_err:
> - kfree(idata->buf);
> + if(idata)
> + kfree(idata->buf);
> kfree(idata);
> return ERR_PTR(err);
> -
> }
>
> static int mmc_blk_ioctl_cmd(struct block_device *bdev,
> --
> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

2011-05-09 15:06:25

by Vladimir Motyka

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On 05/09/2011 04:32 PM, Julia Lawall wrote:
> On Mon, 9 May 2011, Vladimir Motyka wrote:
>
>> When allocation of idata fails there was a null dereferece.
>
> Why not have a different label for the two cases? That would make the
> code easier to statically analyze, and perhaps be more understandable as
> well.
>
> julia
>
I think You are right. So it could be better like this?

diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 3dec493..a03cdc6 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -237,7 +237,7 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
idata = kzalloc(sizeof(*idata), GFP_KERNEL);
if (!idata) {
err = -ENOMEM;
- goto copy_err;
+ goto alloc_err;
}

if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
@@ -266,9 +266,9 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
return idata;

copy_err:
- if(idata)
- kfree(idata->buf);
+ kfree(idata->buf);
kfree(idata);
+alloc_err:
return ERR_PTR(err);
}

Or it could return right after allocation fails so there needn't be
goto. It is simplier, but maybe worse looking and to read. What is your
opinion?

Vladimir Motyka

>
>> Signed-off-by: Vladimir Motyka <[email protected]>
>>
>> ---
>> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
>> index 407836d..3dec493 100644
>> --- a/drivers/mmc/card/block.c
>> +++ b/drivers/mmc/card/block.c
>> @@ -266,10 +266,10 @@ static struct mmc_blk_ioc_data
>> *mmc_blk_ioctl_copy_from_user(
>> return idata;
>>
>> copy_err:
>> - kfree(idata->buf);
>> + if(idata)
>> + kfree(idata->buf);
>> kfree(idata);
>> return ERR_PTR(err);
>> -
>> }
>>
>> static int mmc_blk_ioctl_cmd(struct block_device *bdev,
>> --
>> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>

2011-05-09 15:13:58

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On Mon, May 9, 2011 at 6:05 PM, Vladimir Motyka
<[email protected]> wrote:
> On 05/09/2011 04:32 PM, Julia Lawall wrote:
>> On Mon, 9 May 2011, Vladimir Motyka wrote:
>>
>>> When allocation of idata fails there was a null dereferece.
>>
>> Why not have a different label for the two cases?  That would make the
>> code easier to statically analyze, and perhaps be more understandable as
>> well.
>>
>> julia
>>
> I think You are right. So it could be better like this?
>
> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
> index 3dec493..a03cdc6 100644
> --- a/drivers/mmc/card/block.c
> +++ b/drivers/mmc/card/block.c
> @@ -237,7 +237,7 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
>        idata = kzalloc(sizeof(*idata), GFP_KERNEL);
>        if (!idata) {
>                err = -ENOMEM;
> -               goto copy_err;
> +               goto alloc_err;
>        }
>
>        if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
> @@ -266,9 +266,9 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
>        return idata;
>
>  copy_err:
> -       if(idata)
> -               kfree(idata->buf);
> +       kfree(idata->buf);
Make it one patch not series.

>        kfree(idata);
> +alloc_err:
>        return ERR_PTR(err);
>  }
>
> Or it could return right after allocation fails so there needn't be
> goto. It is simplier, but maybe worse looking and to read. What is your
> opinion?
>
> Vladimir Motyka
>
>>
>>> Signed-off-by: Vladimir Motyka <[email protected]>
>>>
>>> ---
>>> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
>>> index 407836d..3dec493 100644
>>> --- a/drivers/mmc/card/block.c
>>> +++ b/drivers/mmc/card/block.c
>>> @@ -266,10 +266,10 @@ static struct mmc_blk_ioc_data
>>> *mmc_blk_ioctl_copy_from_user(
>>>      return idata;
>>>
>>>  copy_err:
>>> -    kfree(idata->buf);
>>> +    if(idata)
>>> +            kfree(idata->buf);
>>>      kfree(idata);
>>>      return ERR_PTR(err);
>>> -
>>>  }
>>>
>>>  static int mmc_blk_ioctl_cmd(struct block_device *bdev,
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
>>> the body of a message to [email protected]
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
>



--
With Best Regards,
Andy Shevchenko

2011-05-09 15:14:30

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On Mon, 9 May 2011, Vladimir Motyka wrote:

> On 05/09/2011 04:32 PM, Julia Lawall wrote:
> > On Mon, 9 May 2011, Vladimir Motyka wrote:
> >
> >> When allocation of idata fails there was a null dereferece.
> >
> > Why not have a different label for the two cases? That would make the
> > code easier to statically analyze, and perhaps be more understandable as
> > well.
> >
> > julia
> >
> I think You are right. So it could be better like this?
>
> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
> index 3dec493..a03cdc6 100644
> --- a/drivers/mmc/card/block.c
> +++ b/drivers/mmc/card/block.c
> @@ -237,7 +237,7 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
> idata = kzalloc(sizeof(*idata), GFP_KERNEL);
> if (!idata) {
> err = -ENOMEM;
> - goto copy_err;
> + goto alloc_err;
> }
>
> if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
> @@ -266,9 +266,9 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
> return idata;
>
> copy_err:
> - if(idata)
> - kfree(idata->buf);
> + kfree(idata->buf);
> kfree(idata);
> +alloc_err:
> return ERR_PTR(err);
> }
>
> Or it could return right after allocation fails so there needn't be
> goto. It is simplier, but maybe worse looking and to read. What is your
> opinion?

Perhaps it is also pointless to call kfree on something that is known to
be NULL. But I think that there is quite some code that does that, so
others might have another opinion.

julia


>
> Vladimir Motyka
>
> >
> >> Signed-off-by: Vladimir Motyka <[email protected]>
> >>
> >> ---
> >> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
> >> index 407836d..3dec493 100644
> >> --- a/drivers/mmc/card/block.c
> >> +++ b/drivers/mmc/card/block.c
> >> @@ -266,10 +266,10 @@ static struct mmc_blk_ioc_data
> >> *mmc_blk_ioctl_copy_from_user(
> >> return idata;
> >>
> >> copy_err:
> >> - kfree(idata->buf);
> >> + if(idata)
> >> + kfree(idata->buf);
> >> kfree(idata);
> >> return ERR_PTR(err);
> >> -
> >> }
> >>
> >> static int mmc_blk_ioctl_cmd(struct block_device *bdev,
> >> --
> >> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
> >> the body of a message to [email protected]
> >> More majordomo info at http://vger.kernel.org/majordomo-info.html
> >>
>
> --
> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

2011-05-09 16:09:28

by Vladimir Motyka

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

When allocation of idata fails there was a null dereference.

Signed-off-by: Vladimir Motyka <[email protected]>
---
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 407836d..a03cdc6 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -237,7 +237,7 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
idata = kzalloc(sizeof(*idata), GFP_KERNEL);
if (!idata) {
err = -ENOMEM;
- goto copy_err;
+ goto alloc_err;
}

if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
@@ -268,8 +268,8 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
copy_err:
kfree(idata->buf);
kfree(idata);
+alloc_err:
return ERR_PTR(err);
-
}

static int mmc_blk_ioctl_cmd(struct block_device *bdev,

2011-05-09 16:12:13

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

I guess there is also a point at which idata has been successfully
allocated but idata->buf has not.

julia


On Mon, 9 May 2011, Vladimir Motyka wrote:

> When allocation of idata fails there was a null dereference.
>
> Signed-off-by: Vladimir Motyka <[email protected]>
> ---
> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
> index 407836d..a03cdc6 100644
> --- a/drivers/mmc/card/block.c
> +++ b/drivers/mmc/card/block.c
> @@ -237,7 +237,7 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
> idata = kzalloc(sizeof(*idata), GFP_KERNEL);
> if (!idata) {
> err = -ENOMEM;
> - goto copy_err;
> + goto alloc_err;
> }
>
> if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
> @@ -268,8 +268,8 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(
> copy_err:
> kfree(idata->buf);
> kfree(idata);
> +alloc_err:
> return ERR_PTR(err);
> -
> }
>
> static int mmc_blk_ioctl_cmd(struct block_device *bdev,
> --
> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

2011-05-09 20:38:16

by Vladimir Motyka

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On 05/09/2011 06:12 PM, Julia Lawall wrote:
> I guess there is also a point at which idata has been successfully
> allocated but idata->buf has not.
>
> julia
>
Yes there is. Thank You for pointing out.

Vladimir Motyka

> On Mon, 9 May 2011, Vladimir Motyka wrote:
>
>> When allocation of idata fails there was a null dereference.
>>
>> Signed-off-by: Vladimir Motyka <[email protected]>
>> ---
>> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
>> index 407836d..a03cdc6 100644
>> --- a/drivers/mmc/card/block.c
>> +++ b/drivers/mmc/card/block.c
>> @@ -237,7 +237,7 @@ static struct mmc_blk_ioc_data
>> *mmc_blk_ioctl_copy_from_user(
>> idata = kzalloc(sizeof(*idata), GFP_KERNEL);
>> if (!idata) {
>> err = -ENOMEM;
>> - goto copy_err;
>> + goto alloc_err;
>> }
>>
>> if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
>> @@ -268,8 +268,8 @@ static struct mmc_blk_ioc_data
>> *mmc_blk_ioctl_copy_from_user(
>> copy_err:
>> kfree(idata->buf);
>> kfree(idata);
>> +alloc_err:
>> return ERR_PTR(err);
>> -
>> }
>>
>> static int mmc_blk_ioctl_cmd(struct block_device *bdev,
>> --
>> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>

2011-05-09 20:38:30

by Vladimir Motyka

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

When allocation of idata failed there was a null dereference. Also avoid
calling kfree where it is needn't.

---
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 407836d..126c7f4 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -237,24 +237,24 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
idata = kzalloc(sizeof(*idata), GFP_KERNEL);
if (!idata) {
err = -ENOMEM;
- goto copy_err;
+ goto out;
}

if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
err = -EFAULT;
- goto copy_err;
+ goto idata_err;
}

idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
err = -EOVERFLOW;
- goto copy_err;
+ goto idata_err;
}

idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
if (!idata->buf) {
err = -ENOMEM;
- goto copy_err;
+ goto idata_err;
}

if (copy_from_user(idata->buf, (void __user *)(unsigned long)
@@ -267,9 +267,10 @@ static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(

copy_err:
kfree(idata->buf);
+idata_err:
kfree(idata);
+out:
return ERR_PTR(err);
-
}

static int mmc_blk_ioctl_cmd(struct block_device *bdev,

2011-05-10 07:47:54

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On Mon, May 9, 2011 at 7:12 PM, Julia Lawall <[email protected]> wrote:
> I guess there is also a point at which idata has been successfully
> allocated but idata->buf has not.
And? kfree() simple ignores NULL pointers.
I would prefer to see previous version of patch, but let maintainer to choose.

--
With Best Regards,
Andy Shevchenko

2011-05-10 07:57:35

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

On Tue, 10 May 2011, Andy Shevchenko wrote:

> On Mon, May 9, 2011 at 7:12 PM, Julia Lawall <[email protected]> wrote:
> > I guess there is also a point at which idata has been successfully
> > allocated but idata->buf has not.
> And? kfree() simple ignores NULL pointers.

Unnecessarily calling a function suggests that calling that function is
necessary when it is not. But it is probably not a big deal, especially
for a well known function like kfree.

julia

> I would prefer to see previous version of patch, but let maintainer to choose.
>
> --
> With Best Regards,
> Andy Shevchenko
>

2011-05-11 04:01:35

by Chris Ball

[permalink] [raw]
Subject: Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata'

Hi,

On Mon, May 09 2011, Vladimir Motyka wrote:
> When allocation of idata failed there was a null dereference. Also avoid
> calling kfree where it is needn't.
>
> ---
> diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
> index 407836d..126c7f4 100644
> --- a/drivers/mmc/card/block.c
> +++ b/drivers/mmc/card/block.c
> @@ -237,24 +237,24 @@ static struct mmc_blk_ioc_data
> *mmc_blk_ioctl_copy_from_user(

Thanks, I've pushed this version of the patch to mmc-next.

(The patch you sent was corrupted by gmail; it added a line break on the
last line quoted above where there shouldn't be one. Please fix that
for next time.)

- Chris.
--
Chris Ball <[email protected]> <http://printf.net/>
One Laptop Per Child