drivers/hid/hid-roccat.c: eliminate a null pointer dereference
Subsystems (3)
PATCH 3/5 drivers/hid/hid-roccat.c: eliminate a null pointer dereference
From: Julia Lawall <[email protected]>
It is not possible to take the lock in device if device is NULL. The mutex_lock is thus moved after the NULL test, and the relevant part of the shared error handling code is moved up.
The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)
// <smpl>
@r@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@
if (E = NULL)
{
... when != if (E = NULL || ...) S1 else S2
when != E = E1
*E->f
... when any
return ...;
}
else S3
// </smpl>
--- drivers/hid/hid-roccat.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hid/hid-roccat.c +5 -4
@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err; +kfree (reader); +mutex_lock (&devices_lock); +return -ENODEV ; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );
Quoted 6 lines · Julia Lawall
drivers/hid/hid-roccat.c +0
-0@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file)
drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 169
Quoted 10 lines
drivers/hid/hid-roccat.c +2
-4device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err; +kfree (reader); +mutex_lock (&devices_lock);
Typo. mutex_unlock() instead of mutex_lock().
drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 177
Quoted 9 lines
drivers/hid/hid-roccat.c +3
-0+return -ENODEV ; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );
regards,
dan carpenter
Quoted 9 lines · Dan Carpenter
Quoted 8 lines · Julia Lawall
drivers/hid/hid-roccat.c +0
-0@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor];
Quoted 10 lines
drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 169
Quoted 8 lines
drivers/hid/hid-roccat.c +2
-4-mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err; +kfree (reader); +mutex_lock (&devices_lock);Typo.
mutex_unlock()instead ofmutex_lock().
This is no typo, but simply wrong. Remove the mutex_lock() as we are leaving the function here in error flow.
Quoted 12 lines
drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 177
Quoted 9 lines
drivers/hid/hid-roccat.c +3
-0+return -ENODEV ; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );regards,
dan carpenter
Gr. AvS
Quoted 21 lines · Arend van Spriel
Quoted 9 lines · Dan Carpenter
Quoted 8 lines · Julia Lawall
drivers/hid/hid-roccat.c +0
-0@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor];Quoted 10 lines
drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 169
Quoted 8 lines
drivers/hid/hid-roccat.c +2
-4- mutex_lock(&device->readers_lock); - if (!device) { pr_emerg("roccat device with minor %d doesn't exist\n", minor); - error = -ENODEV; - goto exit_err; + kfree(reader); + mutex_lock(&devices_lock);Typo. mutex_unlock() instead of
mutex_lock().
Quoted 2 lines
This is no typo, but simply wrong. Remove the
mutex_lock()as we are leaving the function here in error flow.
No, this one is definitely needed. Its devices_lock, not device->readers_lock! And devices_lock is locked before so we need to unlock it if we return in this branch.
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
drivers/hid/hid-roccat.c +3
-0+ return -ENODEV; } + mutex_lock(&device->readers_lock); + if (!device->open++) { /* power on device on adding first reader */ error = hid_hw_power(device->hid, PM_HINT_FULLON);
Quoted 4 lines
Quoted 2 lines
regards,
dan carpenterGr. AvS
Regards
David
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
From: Julia Lawall <[email protected]>
It is not possible to take the lock in device if device is NULL. The mutex_lock is thus moved after the NULL test, and the relevant part of the shared error handling code is moved up.
The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)
// <smpl>
@r@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@
if (E = NULL)
{
... when != if (E = NULL || ...) S1 else S2
when != E = E1
*E->f
... when any
return ...;
}
else S3
// </smpl>
---mutex_lock changed to mutex_unlock in error handling code
drivers/hid/hid-roccat.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hid/hid-roccat.c +5 -4
@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err; +kfree (reader); +mutex_unlock (&devices_lock); +return -ENODEV ; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );
Quoted 49 lines · Julia Lawall
From: Julia Lawall <[email protected]>
It is not possible to take the lock in device if device is NULL. The
mutex_lockis thus moved after the NULL test, and the relevant part of the shared error handling code is moved up.The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)// <smpl>
@r@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@if (E = NULL)
{
... when != if (E = NULL || ...) S1 else S2when != E = E1
*E->f
... when any
return ...;
}
else S3
// </smpl>Signed-off-by Julia Lawall <[email protected]>---
mutex_lockchanged tomutex_unlockin error handling codedrivers/hid/hid-roccat.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/hid/hid-roccat.c +0
-4@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err;
drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 177
Quoted 10 lines
drivers/hid/hid-roccat.c +5
-0+kfree (reader); +mutex_unlock (&devices_lock); +return -ENODEV ; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );
Julia,
thanks a lot for fixing this.
Could you please redo the patch in a way that it adds second 'exit_unlock1' label (and renames 'exit_unlock' to 'exit_unlock2') (or any appropriate variation of the names) and preserve error path using goto instead of mixture of returns and gotos that this patch would introduce?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 64 lines · Jiri Kosina
Quoted 49 lines · Julia Lawall
From: Julia Lawall <[email protected]>
It is not possible to take the lock in device if device is NULL. The
mutex_lockis thus moved after the NULL test, and the relevant part of the shared error handling code is moved up.The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)// <smpl>
@r@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@if (E = NULL)
{
... when != if (E = NULL || ...) S1 else S2when != E = E1
*E->f
... when any
return ...;
}
else S3
// </smpl>Signed-off-by Julia Lawall <[email protected]>---
mutex_lockchanged tomutex_unlockin error handling codedrivers/hid/hid-roccat.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/hid/hid-roccat.c +0
-4@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err;drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 177
Quoted 10 lines
drivers/hid/hid-roccat.c +5
-0+kfree (reader); +mutex_unlock (&devices_lock); +return -ENODEV ; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );Julia,
Quoted 6 lines
thanks a lot for fixing this.
Could you please redo the patch in a way that it adds second '
exit_unlock1' label (and renames 'exit_unlock' to 'exit_unlock2') (or any appropriate variation of the names) and preserve error path using goto instead of mixture of returns and gotos that this patch would introduce?
OK. At first I couldn't see how to do this without duplicating the unlocks in the success and failure cases, but perhaps there is a solution by adding more gotos. I'll try for that.
julia
PATCH v3 3/5 drivers/hid/hid-roccat.c: eliminate a null pointer dereference
In reply to Jiri KosinaFrom nobody Sat Oct 29 01:38:55 CEST 2011
From: Julia Lawall <[email protected]>
To: Jiri Kosina <[email protected]>
Cc: [email protected],[email protected]
Subject: [PATCH 3/5 v3] drivers/hid/hid-roccat.c: eliminate a null pointer dereference
From: Julia Lawall <[email protected]>
It is not possible to take the lock in device if device is NULL. The mutex_lock is thus moved after the NULL test. New error handling labels are added at the end to differentiate between the cases where different sets of locks should be unlocks, and between whether or not reader should be freed (only on error).
The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)
// <smpl>
@r@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@
if (E = NULL)
{
... when != if (E = NULL || ...) S1 else S2
when != E = E1
*E->f
... when any
return ...;
}
else S3
// </smpl>
---
Moved the error handling code back to the end.
drivers/hid/hid-roccat.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/hid/hid-roccat.c +9 -9
@@ -163,27 +163,27 @@ static int roccat_open(struct inode *inode, struct file *file) device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); error = -ENODEV ; - goto exit_err; + goto exit_err_devices; } +mutex_lock (&device->readers_lock); +if (!device->open++) {/* power on device on adding first reader */ error =hid_hw_power (device->hid,PM_HINT_FULLON );if (error <0 ) { --device->open; - goto exit_err; + goto exit_err_readers; } error =hid_hw_open (device->hid);if (error <0 ) {hid_hw_power (device->hid,PM_HINT_NORMAL ); --device->open; - goto exit_err; + goto exit_err_readers; } }
@@ -194,13 +194,13 @@ static int roccat_open(struct inode *inode, struct file *file)list_add_tail (&reader->node, &device->readers); file->private_data = reader; -exit_unlock: +exit_err_readers:mutex_unlock (&device->readers_lock); +exit_err_devices:mutex_unlock (&devices_lock); +if (error) +kfree (reader);return error; -exit_err: -kfree (reader); - goto exit_unlock; }static int roccat_release (struct inode *inode,struct file *file)
Jiri Kosina
AppliedQuoted 20 lines · Julia Lawall
It is not possible to take the lock in device if device is NULL. The
mutex_lockis thus moved after the NULL test. New error handling labels are added at the end to differentiate between the cases where different sets of locks should be unlocks, and between whether or not reader should be freed (only on error).The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)// <smpl>
@r@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@if (E = NULL)
{
... when != if (E = NULL || ...) S1 else S2when != E = E1
Quoted 8 lines
*E->f
... when any
return ...;
}
else S3
// </smpl>Signed-off-by Julia Lawall <[email protected]>
Applied, thank you.
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 22 lines · David Herrmann
Quoted 21 lines · Arend van Spriel
Quoted 19 lines · Dan Carpenter
Quoted 6 lines · Julia Lawall
drivers/hid/hid-roccat.c +0
-0@@ -163,14 +163,15 @@ static int roccat_open(struct inode *inode, struct file *file)drivers/hid/hid-roccat.c static int roccat_open(struct inode *inode, struct file *file) · line 169
Quoted 10 lines
drivers/hid/hid-roccat.c +2
-4device = devices[minor]; -mutex_lock (&device->readers_lock); -if (!device) {pr_emerg ("roccat device with minor %d doesn't exist\n" , minor); - error = -ENODEV ; - goto exit_err; +kfree (reader); +mutex_lock (&devices_lock);Typo.
mutex_unlock()instead ofmutex_lock().
Quoted 6 lines
Quoted 2 lines
This is no typo, but simply wrong. Remove the
mutex_lock()as we are leaving the function here in error flow.No, this one is definitely needed. Its
devices_lock, notdevice->readers_lock! Anddevices_lockis locked before so we need to unlock it if we return in this branch.
You are right. I missed that. As usual Dan's eye is sharper ;-)
Gr. AvS