* [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' @ 2011-05-09 14:05 Vladimir Motyka 2011-05-09 14:32 ` Julia Lawall 0 siblings, 1 reply; 12+ messages in thread From: Vladimir Motyka @ 2011-05-09 14:05 UTC (permalink / raw) To: cjb; +Cc: kernel-janitors, linux-mmc, linux-kernel When allocation of idata fails there was a null dereferece. Signed-off-by: Vladimir Motyka <vladimir.motyka@gmail.com> --- 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, ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 14:05 [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' Vladimir Motyka @ 2011-05-09 14:32 ` Julia Lawall 2011-05-09 15:05 ` Vladimir Motyka 0 siblings, 1 reply; 12+ messages in thread From: Julia Lawall @ 2011-05-09 14:32 UTC (permalink / raw) To: Vladimir Motyka; +Cc: cjb, kernel-janitors, linux-mmc, linux-kernel 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 <vladimir.motyka@gmail.com> > > --- > 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 majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 14:32 ` Julia Lawall @ 2011-05-09 15:05 ` Vladimir Motyka 2011-05-09 15:13 ` Andy Shevchenko 2011-05-09 15:14 ` Julia Lawall 0 siblings, 2 replies; 12+ messages in thread From: Vladimir Motyka @ 2011-05-09 15:05 UTC (permalink / raw) To: Julia Lawall; +Cc: cjb, kernel-janitors, linux-mmc, linux-kernel 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 <vladimir.motyka@gmail.com> >> >> --- >> 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 majordomo@vger.kernel.org >> More majordomo info at http://vger.kernel.org/majordomo-info.html >> ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 15:05 ` Vladimir Motyka @ 2011-05-09 15:13 ` Andy Shevchenko 2011-05-09 15:14 ` Julia Lawall 1 sibling, 0 replies; 12+ messages in thread From: Andy Shevchenko @ 2011-05-09 15:13 UTC (permalink / raw) To: Vladimir Motyka Cc: Julia Lawall, cjb, kernel-janitors, linux-mmc, linux-kernel On Mon, May 9, 2011 at 6:05 PM, Vladimir Motyka <vladimir.motyka@gmail.com> 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 <vladimir.motyka@gmail.com> >>> >>> --- >>> 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 majordomo@vger.kernel.org >>> 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 majordomo@vger.kernel.org > 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 15:05 ` Vladimir Motyka 2011-05-09 15:13 ` Andy Shevchenko @ 2011-05-09 15:14 ` Julia Lawall 2011-05-09 16:08 ` Vladimir Motyka 1 sibling, 1 reply; 12+ messages in thread From: Julia Lawall @ 2011-05-09 15:14 UTC (permalink / raw) To: Vladimir Motyka; +Cc: cjb, kernel-janitors, linux-mmc, linux-kernel 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 <vladimir.motyka@gmail.com> > >> > >> --- > >> 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 majordomo@vger.kernel.org > >> 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 majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 15:14 ` Julia Lawall @ 2011-05-09 16:08 ` Vladimir Motyka 2011-05-09 16:12 ` Julia Lawall 0 siblings, 1 reply; 12+ messages in thread From: Vladimir Motyka @ 2011-05-09 16:08 UTC (permalink / raw) To: cjb; +Cc: Julia Lawall, kernel-janitors, linux-mmc, linux-kernel When allocation of idata fails there was a null dereference. Signed-off-by: Vladimir Motyka <vladimir.motyka@gmail.com> --- 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, ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 16:08 ` Vladimir Motyka @ 2011-05-09 16:12 ` Julia Lawall 2011-05-09 20:37 ` Vladimir Motyka ` (2 more replies) 0 siblings, 3 replies; 12+ messages in thread From: Julia Lawall @ 2011-05-09 16:12 UTC (permalink / raw) To: Vladimir Motyka; +Cc: cjb, kernel-janitors, linux-mmc, linux-kernel 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 <vladimir.motyka@gmail.com> > --- > 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 majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 16:12 ` Julia Lawall @ 2011-05-09 20:37 ` Vladimir Motyka 2011-05-09 20:37 ` Vladimir Motyka 2011-05-10 7:47 ` Andy Shevchenko 2 siblings, 0 replies; 12+ messages in thread From: Vladimir Motyka @ 2011-05-09 20:37 UTC (permalink / raw) To: Julia Lawall; +Cc: cjb, kernel-janitors, linux-mmc, linux-kernel 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 <vladimir.motyka@gmail.com> >> --- >> 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 majordomo@vger.kernel.org >> More majordomo info at http://vger.kernel.org/majordomo-info.html >> ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 16:12 ` Julia Lawall 2011-05-09 20:37 ` Vladimir Motyka @ 2011-05-09 20:37 ` Vladimir Motyka 2011-05-11 4:03 ` Chris Ball 2011-05-10 7:47 ` Andy Shevchenko 2 siblings, 1 reply; 12+ messages in thread From: Vladimir Motyka @ 2011-05-09 20:37 UTC (permalink / raw) To: Julia Lawall; +Cc: cjb, kernel-janitors, linux-mmc, linux-kernel 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, ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 20:37 ` Vladimir Motyka @ 2011-05-11 4:03 ` Chris Ball 0 siblings, 0 replies; 12+ messages in thread From: Chris Ball @ 2011-05-11 4:03 UTC (permalink / raw) To: Vladimir Motyka; +Cc: Julia Lawall, kernel-janitors, linux-mmc, linux-kernel 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 <cjb@laptop.org> <http://printf.net/> One Laptop Per Child ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-09 16:12 ` Julia Lawall 2011-05-09 20:37 ` Vladimir Motyka 2011-05-09 20:37 ` Vladimir Motyka @ 2011-05-10 7:47 ` Andy Shevchenko 2011-05-10 7:57 ` Julia Lawall 2 siblings, 1 reply; 12+ messages in thread From: Andy Shevchenko @ 2011-05-10 7:47 UTC (permalink / raw) To: Julia Lawall Cc: Vladimir Motyka, cjb, kernel-janitors, linux-mmc, linux-kernel On Mon, May 9, 2011 at 7:12 PM, Julia Lawall <julia@diku.dk> 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' 2011-05-10 7:47 ` Andy Shevchenko @ 2011-05-10 7:57 ` Julia Lawall 0 siblings, 0 replies; 12+ messages in thread From: Julia Lawall @ 2011-05-10 7:57 UTC (permalink / raw) To: Andy Shevchenko Cc: Vladimir Motyka, cjb, kernel-janitors, linux-mmc, linux-kernel On Tue, 10 May 2011, Andy Shevchenko wrote: > On Mon, May 9, 2011 at 7:12 PM, Julia Lawall <julia@diku.dk> 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 > ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2011-05-11 4:01 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2011-05-09 14:05 [PATCH] drivers/mmc/card/block.c: fix potential null dereference 'idata' Vladimir Motyka 2011-05-09 14:32 ` Julia Lawall 2011-05-09 15:05 ` Vladimir Motyka 2011-05-09 15:13 ` Andy Shevchenko 2011-05-09 15:14 ` Julia Lawall 2011-05-09 16:08 ` Vladimir Motyka 2011-05-09 16:12 ` Julia Lawall 2011-05-09 20:37 ` Vladimir Motyka 2011-05-09 20:37 ` Vladimir Motyka 2011-05-11 4:03 ` Chris Ball 2011-05-10 7:47 ` Andy Shevchenko 2011-05-10 7:57 ` Julia Lawall
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).