* [patch 3/3] [media] lirc_dev: fixes in lirc_dev_fop_read()
@ 2010-11-17 5:20 Dan Carpenter
2010-11-17 20:23 ` Jarod Wilson
0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2010-11-17 5:20 UTC (permalink / raw)
To: Mauro Carvalho Chehab
Cc: Jarod Wilson, Zimny Lech, linux-media, kernel-janitors
This makes several changes but they're in one function and sort of
related:
"buf" was leaked on error. The leak if we try to read an invalid
length is the main concern because it could be triggered over and
over.
If the copy_to_user() failed, then the original code returned the
number of bytes remaining. read() is supposed to be the opposite way,
where we return the number of bytes copied. I changed it to just return
-EFAULT on errors.
Also I changed the debug output from "-EFAULT" to just "<fail>" because
it isn't -EFAULT necessarily. And since we go though that path if the
length is invalid now, there was another debug print that I removed.
Signed-off-by: Dan Carpenter <error27@gmail.com>
diff --git a/drivers/media/IR/lirc_dev.c b/drivers/media/IR/lirc_dev.c
index fbca94f..6b9fc74 100644
--- a/drivers/media/IR/lirc_dev.c
+++ b/drivers/media/IR/lirc_dev.c
@@ -647,18 +647,18 @@ ssize_t lirc_dev_fop_read(struct file *file,
if (!buf)
return -ENOMEM;
- if (mutex_lock_interruptible(&ir->irctl_lock))
- return -ERESTARTSYS;
+ if (mutex_lock_interruptible(&ir->irctl_lock)) {
+ ret = -ERESTARTSYS;
+ goto out_unlocked;
+ }
if (!ir->attached) {
- mutex_unlock(&ir->irctl_lock);
- return -ENODEV;
+ ret = -ENODEV;
+ goto out_locked;
}
if (length % ir->chunk_size) {
- dev_dbg(ir->d.dev, LOGHEAD "read result = -EINVAL\n",
- ir->d.name, ir->d.minor);
- mutex_unlock(&ir->irctl_lock);
- return -EINVAL;
+ ret = -EINVAL;
+ goto out_locked;
}
/*
@@ -709,18 +709,23 @@ ssize_t lirc_dev_fop_read(struct file *file,
lirc_buffer_read(ir->buf, buf);
ret = copy_to_user((void *)buffer+written, buf,
ir->buf->chunk_size);
- written += ir->buf->chunk_size;
+ if (!ret)
+ written += ir->buf->chunk_size;
+ else
+ ret = -EFAULT;
}
}
remove_wait_queue(&ir->buf->wait_poll, &wait);
set_current_state(TASK_RUNNING);
+
+out_locked:
mutex_unlock(&ir->irctl_lock);
out_unlocked:
kfree(buf);
dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n",
- ir->d.name, ir->d.minor, ret ? "-EFAULT" : "OK", ret);
+ ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret);
return ret ? ret : written;
}
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [patch 3/3] [media] lirc_dev: fixes in lirc_dev_fop_read()
2010-11-17 5:20 [patch 3/3] [media] lirc_dev: fixes in lirc_dev_fop_read() Dan Carpenter
@ 2010-11-17 20:23 ` Jarod Wilson
0 siblings, 0 replies; 2+ messages in thread
From: Jarod Wilson @ 2010-11-17 20:23 UTC (permalink / raw)
To: Dan Carpenter
Cc: Mauro Carvalho Chehab, Zimny Lech, linux-media, kernel-janitors
On Wed, Nov 17, 2010 at 08:20:15AM +0300, Dan Carpenter wrote:
> This makes several changes but they're in one function and sort of
> related:
>
> "buf" was leaked on error. The leak if we try to read an invalid
> length is the main concern because it could be triggered over and
> over.
>
> If the copy_to_user() failed, then the original code returned the
> number of bytes remaining. read() is supposed to be the opposite way,
> where we return the number of bytes copied. I changed it to just return
> -EFAULT on errors.
>
> Also I changed the debug output from "-EFAULT" to just "<fail>" because
> it isn't -EFAULT necessarily. And since we go though that path if the
> length is invalid now, there was another debug print that I removed.
>
> Signed-off-by: Dan Carpenter <error27@gmail.com>
Looks good, thanks much.
Reviewed-by: Jarod Wilson <jarod@redhat.com>
Acked-by: Jarod Wilson <jarod@redhat.com>
--
Jarod Wilson
jarod@redhat.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-11-17 20:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-17 5:20 [patch 3/3] [media] lirc_dev: fixes in lirc_dev_fop_read() Dan Carpenter
2010-11-17 20:23 ` Jarod Wilson
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).