diff options
author | Ian Abbott <abbotti@mev.co.uk> | 2014-10-20 10:10:40 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2014-10-29 04:18:31 -0400 |
commit | 238b5ad855924919e5b98d0c772d9dc78795639b (patch) | |
tree | 06c1c2a7cfa0947cfb36155a1b14d42e5a8f6fa3 /drivers/staging | |
parent | 0ea6fa037e961dc77cb2bb7a4ad91d172ee4884a (diff) |
staging: comedi: fix memory leak / bad pointer freeing for chanlist
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
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/staging')
-rw-r--r-- | drivers/staging/comedi/comedi_fops.c | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c index f9f61bda92e2..9c32f0276009 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, | |||
1462 | unsigned int *chanlist; | 1462 | unsigned int *chanlist; |
1463 | int ret; | 1463 | int ret; |
1464 | 1464 | ||
1465 | cmd->chanlist = NULL; | ||
1465 | chanlist = memdup_user(user_chanlist, | 1466 | chanlist = memdup_user(user_chanlist, |
1466 | cmd->chanlist_len * sizeof(unsigned int)); | 1467 | cmd->chanlist_len * sizeof(unsigned int)); |
1467 | if (IS_ERR(chanlist)) | 1468 | if (IS_ERR(chanlist)) |
@@ -1615,6 +1616,8 @@ static int do_cmdtest_ioctl(struct comedi_device *dev, | |||
1615 | 1616 | ||
1616 | ret = s->do_cmdtest(dev, s, &cmd); | 1617 | ret = s->do_cmdtest(dev, s, &cmd); |
1617 | 1618 | ||
1619 | kfree(cmd.chanlist); /* free kernel copy of user chanlist */ | ||
1620 | |||
1618 | /* restore chanlist pointer before copying back */ | 1621 | /* restore chanlist pointer before copying back */ |
1619 | cmd.chanlist = (unsigned int __force *)user_chanlist; | 1622 | cmd.chanlist = (unsigned int __force *)user_chanlist; |
1620 | 1623 | ||