Commit 45267226 authored by Ian Abbott's avatar Ian Abbott Committed by Luis Henriques

staging: comedi: fix memory leak / bad pointer freeing for chanlist

commit 238b5ad8 upstream.

As a follow-up to commit 6cab7a37 ("staging: comedi: (regression)
channel list must be set for COMEDI_CMD ioctl"), Hartley Sweeten pointed
out another couple of bugs stemming from commit 6cab7a37 ("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: c6cd0eef ("staging: comedi: comedi_fops: introduce __comedi_get_user_chanlist()")
Reported-by: default avatarH Hartley Sweeten <hsweeten@visionengravers.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent 78894ccc
...@@ -1466,6 +1466,7 @@ static int __comedi_get_user_chanlist(struct comedi_device *dev, ...@@ -1466,6 +1466,7 @@ static int __comedi_get_user_chanlist(struct comedi_device *dev,
unsigned int *chanlist; unsigned int *chanlist;
int ret; int ret;
cmd->chanlist = NULL;
chanlist = memdup_user(user_chanlist, chanlist = memdup_user(user_chanlist,
cmd->chanlist_len * sizeof(unsigned int)); cmd->chanlist_len * sizeof(unsigned int));
if (IS_ERR(chanlist)) if (IS_ERR(chanlist))
...@@ -1619,6 +1620,8 @@ static int do_cmdtest_ioctl(struct comedi_device *dev, ...@@ -1619,6 +1620,8 @@ static int do_cmdtest_ioctl(struct comedi_device *dev,
ret = s->do_cmdtest(dev, s, &cmd); ret = s->do_cmdtest(dev, s, &cmd);
kfree(cmd.chanlist); /* free kernel copy of user chanlist */
/* restore chanlist pointer before copying back */ /* restore chanlist pointer before copying back */
cmd.chanlist = (unsigned int __force *)user_chanlist; cmd.chanlist = (unsigned int __force *)user_chanlist;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment