* [PATCH] staging: comedi: Fix comedi_device refcnt leak in comedi_open
@ 2020-04-20 5:44 Xiyu Yang
2020-04-20 10:15 ` Ian Abbott
0 siblings, 1 reply; 2+ messages in thread
From: Xiyu Yang @ 2020-04-20 5:44 UTC (permalink / raw)
To: Ian Abbott, H Hartley Sweeten, Greg Kroah-Hartman, Xiyu Yang,
Xin Tan, devel, linux-kernel
Cc: yuanxzhang, kjlu
comedi_open() invokes comedi_dev_get_from_minor(), which returns a
reference of the COMEDI device to "dev" with increased refcount.
When comedi_open() returns, "dev" becomes invalid, so the refcount
should be decreased to keep refcount balanced.
The reference counting issue happens in one exception handling path of
comedi_open(). When "cfp" allocation is failed, the refcnt increased by
comedi_dev_get_from_minor() is not decreased, causing a refcnt leak.
Fix this issue by calling comedi_dev_put() on this error path when "cfp"
allocation is failed.
Fixes: 20f083c07565 ("staging: comedi: prepare support for per-file read
and write subdevices")
Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
drivers/staging/comedi/comedi_fops.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c
index 08d1bbbebf2d..e84b4fb493d6 100644
--- a/drivers/staging/comedi/comedi_fops.c
+++ b/drivers/staging/comedi/comedi_fops.c
@@ -2725,8 +2725,10 @@ static int comedi_open(struct inode *inode, struct file *file)
}
cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
- if (!cfp)
+ if (!cfp) {
+ comedi_dev_put(dev);
return -ENOMEM;
+ }
cfp->dev = dev;
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] staging: comedi: Fix comedi_device refcnt leak in comedi_open
2020-04-20 5:44 [PATCH] staging: comedi: Fix comedi_device refcnt leak in comedi_open Xiyu Yang
@ 2020-04-20 10:15 ` Ian Abbott
0 siblings, 0 replies; 2+ messages in thread
From: Ian Abbott @ 2020-04-20 10:15 UTC (permalink / raw)
To: Xiyu Yang, H Hartley Sweeten, Greg Kroah-Hartman, Xin Tan, devel,
linux-kernel
Cc: yuanxzhang, kjlu
On 20/04/2020 06:44, Xiyu Yang wrote:
> comedi_open() invokes comedi_dev_get_from_minor(), which returns a
> reference of the COMEDI device to "dev" with increased refcount.
>
> When comedi_open() returns, "dev" becomes invalid, so the refcount
> should be decreased to keep refcount balanced.
>
> The reference counting issue happens in one exception handling path of
> comedi_open(). When "cfp" allocation is failed, the refcnt increased by
> comedi_dev_get_from_minor() is not decreased, causing a refcnt leak.
>
> Fix this issue by calling comedi_dev_put() on this error path when "cfp"
> allocation is failed.
>
> Fixes: 20f083c07565 ("staging: comedi: prepare support for per-file read
> and write subdevices")
> Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
> ---
> drivers/staging/comedi/comedi_fops.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c
> index 08d1bbbebf2d..e84b4fb493d6 100644
> --- a/drivers/staging/comedi/comedi_fops.c
> +++ b/drivers/staging/comedi/comedi_fops.c
> @@ -2725,8 +2725,10 @@ static int comedi_open(struct inode *inode, struct file *file)
> }
>
> cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
> - if (!cfp)
> + if (!cfp) {
> + comedi_dev_put(dev);
> return -ENOMEM;
> + }
>
> cfp->dev = dev;
>
>
Thanks for spotting that!
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
--
-=( Ian Abbott <abbotti@mev.co.uk> || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268. Registered address: )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-04-20 10:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-20 5:44 [PATCH] staging: comedi: Fix comedi_device refcnt leak in comedi_open Xiyu Yang
2020-04-20 10:15 ` Ian Abbott
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).