stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: comedi: fix memory leak / bad pointer freeing for chanlist
@ 2014-10-20 14:10 Ian Abbott
  2014-10-20 21:38 ` Hartley Sweeten
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Abbott @ 2014-10-20 14:10 UTC (permalink / raw)
  To: driverdev-devel
  Cc: Greg Kroah-Hartman, Ian Abbott, H Hartley Sweeten, linux-kernel,
	stable

As a follow-up to commit 6cab7a37f5c04 ("staging: comedi: (regression)
channel list must be set for COMEDI_CMD ioctl"), Hartley Sweeten pointed
out another couple of bugs stemming from commit 6cab7a37f5c04 ("staging:
comedi: comedi_fops: introduce __comedi_get_user_chanlist()").

Firstly, `do_cmdtest_ioctl()` never frees the kernel copy of the user
chanlist allocated by `__comedi_get_user_chanlist()`, so that memory is
leaked.  Fix it by freeing the allocated kernel memory pointed to by
`cmd.chanlist` before that pointer is overwritten with its original
pointer to user memory before `cmd` is copied back to user-space.

Secondly, if `__comedi_get_user_chanlist()` returns an error,
`cmd->chanlist` is left unchanged and in fact will be a pointer to user
memory.  This causes `do_cmd_ioctl()` to `goto cleanup` and call
`do_become_nonbusy()` which would attempt to free the memory pointed to
by the user-space pointer.  Fix it by setting `cmd->chanlist` to NULL at
the start of `__comedi_get_user_chanlist()`.

Fixes: c6cd0eefb27b ("staging: comedi: comedi_fops: introduce __comedi_get_user_chanlist()")
Reported-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: <stable@vger.kernel.org> # 3.15.y 3.16.y 3.17.y: 6cab7a37f5c04
Cc: <stable@vger.kernel.org> # 3.15.y 3.16.y 3.17.y
---
Greg, this patch applies to your "staging-linus" branch.
---
 drivers/staging/comedi/comedi_fops.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c
index a9b7fe5..a1788e8 100644
--- a/drivers/staging/comedi/comedi_fops.c
+++ b/drivers/staging/comedi/comedi_fops.c
@@ -1462,6 +1462,7 @@ static int __comedi_get_user_chanlist(struct comedi_device *dev,
 	unsigned int *chanlist;
 	int ret;
 
+	cmd->chanlist = NULL;
 	chanlist = memdup_user(user_chanlist,
 			       cmd->chanlist_len * sizeof(unsigned int));
 	if (IS_ERR(chanlist))
@@ -1615,6 +1616,8 @@ static int do_cmdtest_ioctl(struct comedi_device *dev,
 
 	ret = s->do_cmdtest(dev, s, &cmd);
 
+	kfree(cmd.chanlist);	/* free kernel copy of user chanlist */
+
 	/* restore chanlist pointer before copying back */
 	cmd.chanlist = (unsigned int __force *)user_chanlist;
 
-- 
2.1.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [PATCH] staging: comedi: fix memory leak / bad pointer freeing for chanlist
  2014-10-20 14:10 [PATCH] staging: comedi: fix memory leak / bad pointer freeing for chanlist Ian Abbott
@ 2014-10-20 21:38 ` Hartley Sweeten
  2014-10-21  8:43   ` Ian Abbott
  0 siblings, 1 reply; 3+ messages in thread
From: Hartley Sweeten @ 2014-10-20 21:38 UTC (permalink / raw)
  To: Ian Abbott, driverdev-devel@linuxdriverproject.org
  Cc: Greg Kroah-Hartman, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org

On Monday, October 20, 2014 7:11 AM, Ian Abbott wrote:
> As a follow-up to commit 6cab7a37f5c04 ("staging: comedi: (regression)
> channel list must be set for COMEDI_CMD ioctl"), Hartley Sweeten pointed
> out another couple of bugs stemming from commit 6cab7a37f5c04 ("staging:
> comedi: comedi_fops: introduce __comedi_get_user_chanlist()").
>
> Firstly, `do_cmdtest_ioctl()` never frees the kernel copy of the user
> chanlist allocated by `__comedi_get_user_chanlist()`, so that memory is
> leaked.  Fix it by freeing the allocated kernel memory pointed to by
> `cmd.chanlist` before that pointer is overwritten with its original
> pointer to user memory before `cmd` is copied back to user-space.
>
> Secondly, if `__comedi_get_user_chanlist()` returns an error,
> `cmd->chanlist` is left unchanged and in fact will be a pointer to user
> memory.  This causes `do_cmd_ioctl()` to `goto cleanup` and call
> `do_become_nonbusy()` which would attempt to free the memory pointed to
> by the user-space pointer.  Fix it by setting `cmd->chanlist` to NULL at
> the start of `__comedi_get_user_chanlist()`.
>
> Fixes: c6cd0eefb27b ("staging: comedi: comedi_fops: introduce __comedi_get_user_chanlist()")
> Reported-by: H Hartley Sweeten <hsweeten@visionengravers.com>
> Cc: <stable@vger.kernel.org> # 3.15.y 3.16.y 3.17.y: 6cab7a37f5c04
> Cc: <stable@vger.kernel.org> # 3.15.y 3.16.y 3.17.y
> ---
> Greg, this patch applies to your "staging-linus" branch.
> ---
>  drivers/staging/comedi/comedi_fops.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c
> index a9b7fe5..a1788e8 100644
> --- a/drivers/staging/comedi/comedi_fops.c
> +++ b/drivers/staging/comedi/comedi_fops.c
> @@ -1462,6 +1462,7 @@ static int __comedi_get_user_chanlist(struct comedi_device *dev,
>  	unsigned int *chanlist;
>  	int ret;
>  
> +	cmd->chanlist = NULL;
>  	chanlist = memdup_user(user_chanlist,
>  			       cmd->chanlist_len * sizeof(unsigned int));
>  	if (IS_ERR(chanlist))

I don't think this covers all the problems in your second issue above.

It does fix the attempt to free the memory pointed to by the user-space pointer
if the memdub_user() fails.

But in do_cmd_ioctl() we still have an issue if the s->do_cmdtest() fails or
the CMDF_BOGUS flag is set. There we restore the users cmd.chanlist
pointer to allow copy_to_user() to return the cmd/ But the kernel allocated
chanlist is not freed. The goto cleanup will again try to free the user-space
pointer.

The remaining goto cleanup jumps will correctly free the kernel allocated
pointer stored in cmd.chanlist.

> @@ -1615,6 +1616,8 @@ static int do_cmdtest_ioctl(struct comedi_device *dev,
>  
>  	ret = s->do_cmdtest(dev, s, &cmd);
>  
> +	kfree(cmd.chanlist);	/* free kernel copy of user chanlist */
> +
>  	/* restore chanlist pointer before copying back */
>  	cmd.chanlist = (unsigned int __force *)user_chanlist;
>  

This one does fix the issue in do_cmdtest_ioctl().

Regards,
Hartley


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] staging: comedi: fix memory leak / bad pointer freeing for chanlist
  2014-10-20 21:38 ` Hartley Sweeten
@ 2014-10-21  8:43   ` Ian Abbott
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Abbott @ 2014-10-21  8:43 UTC (permalink / raw)
  To: Hartley Sweeten, driverdev-devel@linuxdriverproject.org
  Cc: Greg Kroah-Hartman, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org

On 20/10/14 22:38, Hartley Sweeten wrote:
> On Monday, October 20, 2014 7:11 AM, Ian Abbott wrote:
>> As a follow-up to commit 6cab7a37f5c04 ("staging: comedi: (regression)
>> channel list must be set for COMEDI_CMD ioctl"), Hartley Sweeten pointed
>> out another couple of bugs stemming from commit 6cab7a37f5c04 ("staging:
>> comedi: comedi_fops: introduce __comedi_get_user_chanlist()").
>>
>> Firstly, `do_cmdtest_ioctl()` never frees the kernel copy of the user
>> chanlist allocated by `__comedi_get_user_chanlist()`, so that memory is
>> leaked.  Fix it by freeing the allocated kernel memory pointed to by
>> `cmd.chanlist` before that pointer is overwritten with its original
>> pointer to user memory before `cmd` is copied back to user-space.
>>
>> Secondly, if `__comedi_get_user_chanlist()` returns an error,
>> `cmd->chanlist` is left unchanged and in fact will be a pointer to user
>> memory.  This causes `do_cmd_ioctl()` to `goto cleanup` and call
>> `do_become_nonbusy()` which would attempt to free the memory pointed to
>> by the user-space pointer.  Fix it by setting `cmd->chanlist` to NULL at
>> the start of `__comedi_get_user_chanlist()`.
>>
>> Fixes: c6cd0eefb27b ("staging: comedi: comedi_fops: introduce __comedi_get_user_chanlist()")
>> Reported-by: H Hartley Sweeten <hsweeten@visionengravers.com>
>> Cc: <stable@vger.kernel.org> # 3.15.y 3.16.y 3.17.y: 6cab7a37f5c04
>> Cc: <stable@vger.kernel.org> # 3.15.y 3.16.y 3.17.y
>> ---
>> Greg, this patch applies to your "staging-linus" branch.
>> ---
>>   drivers/staging/comedi/comedi_fops.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c
>> index a9b7fe5..a1788e8 100644
>> --- a/drivers/staging/comedi/comedi_fops.c
>> +++ b/drivers/staging/comedi/comedi_fops.c
>> @@ -1462,6 +1462,7 @@ static int __comedi_get_user_chanlist(struct comedi_device *dev,
>>   	unsigned int *chanlist;
>>   	int ret;
>>
>> +	cmd->chanlist = NULL;
>>   	chanlist = memdup_user(user_chanlist,
>>   			       cmd->chanlist_len * sizeof(unsigned int));
>>   	if (IS_ERR(chanlist))
>
> I don't think this covers all the problems in your second issue above.
>
> It does fix the attempt to free the memory pointed to by the user-space pointer
> if the memdub_user() fails.
>
> But in do_cmd_ioctl() we still have an issue if the s->do_cmdtest() fails or
> the CMDF_BOGUS flag is set. There we restore the users cmd.chanlist
> pointer to allow copy_to_user() to return the cmd/ But the kernel allocated
> chanlist is not freed. The goto cleanup will again try to free the user-space
> pointer.

No, 'async->cmd' is copied (via struct assignment) to local variable 
'cmd' and it is the local 'cmd.chanlist' pointer that is set to the 
original user-space pointer and the local 'cmd' that is copied back to 
user-space.  'async->cmd.chanlist' still points to the kernel copy of 
the chanlist that gets cleaned up by 'goto cleanup' / 'do_become_nonbusy().

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti@mev.co.uk> )=-
-=(                          Web: http://www.mev.co.uk/  )=-

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-10-21  8:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-20 14:10 [PATCH] staging: comedi: fix memory leak / bad pointer freeing for chanlist Ian Abbott
2014-10-20 21:38 ` Hartley Sweeten
2014-10-21  8:43   ` 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).