Skip to contentSkip to conversation

booke/wdt: fix incorrect WDIOC_GETSUPPORT return path

PATCH In review Tiejun Chen 3 participants 6 messages Jul 30, 2012 – Aug 7, 2012 Watchdog device drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 booke/wdt: fix incorrect WDIOC_GETSUPPORT return path

We miss that correct WDIOC_GETSUPPORT return path when perform copy_to_user() properly.

---
 drivers/watchdog/booke_wdt.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
drivers/watchdog/booke_wdt.c +4 -3
index 3fe82d0..2be7f29 100644
@@ -162,12 +162,13 @@ static long booke_wdt_ioctl(struct file *file,
 				unsigned int cmd, unsigned long arg)
 {
 	u32 tmp = 0;
-	u32 __user *p = (u32 __user *)arg;
+	void __user *argp = (u32 __user *)arg;
+	u32 __user *p = argp;
 
 	switch (cmd) {
 	case WDIOC_GETSUPPORT:
-		if (copy_to_user((void *)arg, &ident, sizeof(ident)))
-			return -EFAULT;
+		return copy_to_user(argp, &ident,
+				sizeof(ident)) ? -EFAULT : 0;
 	case WDIOC_GETSTATUS:
 		return put_user(0, p);
 	case WDIOC_GETBOOTSTATUS:
Signature · 1 line
1.5.6
#1 lore raw
Quoted 2 lines · Tiejun Chen

We miss that correct WDIOC_GETSUPPORT return path when perform copy_to_user() properly.

Any comments?

Thanks
Tiejun

Quoted 28 lines
---
 drivers/watchdog/booke_wdt.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
drivers/watchdog/booke_wdt.c +4 -3
index 3fe82d0..2be7f29 100644
@@ -162,12 +162,13 @@ static long booke_wdt_ioctl(struct file *file,
 				unsigned int cmd, unsigned long arg)
 {
 	u32 tmp = 0;
-	u32 __user *p = (u32 __user *)arg;
+	void __user *argp = (u32 __user *)arg;
+	u32 __user *p = argp;
 
 	switch (cmd) {
 	case WDIOC_GETSUPPORT:
-		if (copy_to_user((void *)arg, &ident, sizeof(ident)))
-			return -EFAULT;
+		return copy_to_user(argp, &ident,
+				sizeof(ident)) ? -EFAULT : 0;
 	case WDIOC_GETSTATUS:
 		return put_user(0, p);
 	case WDIOC_GETBOOTSTATUS:
#2 lore raw
Quoted 8 lines · tiejun.chen
Quoted 2 lines · Tiejun Chen

We miss that correct WDIOC_GETSUPPORT return path when perform copy_to_user() properly.

Any comments?

Thanks
Tiejun

Adding Timur, as he's touched watchdog last.

- k

Quoted 27 lines
Quoted 27 lines

---
drivers/watchdog/booke_wdt.c | 7 ++++---

1 files changed, 4 insertions(+), 3 deletions(-)
drivers/watchdog/booke_wdt.c +4 -3
index 3fe82d0..2be7f29 100644
@@ -162,12 +162,13 @@ static long booke_wdt_ioctl(struct file *file,
 				unsigned int cmd, unsigned long arg)
 {
 	u32 tmp = 0;
-	u32 __user *p = (u32 __user *)arg;
+	void __user *argp = (u32 __user *)arg;
+	u32 __user *p = argp;
 
 	switch (cmd) {
 	case WDIOC_GETSUPPORT:
-		if (copy_to_user((void *)arg, &ident, sizeof(ident)))
-			return -EFAULT;
+		return copy_to_user(argp, &ident,
+				sizeof(ident)) ? -EFAULT : 0;
 	case WDIOC_GETSTATUS:
 		return put_user(0, p);
 	case WDIOC_GETBOOTSTATUS:
#3 lore raw
In reply to Tiejun Chen

On Mon, Jul 30, 2012 at 3:15 AM, Tiejun Chen <[email protected]> wr= ote:

Quoted 2 lines

We miss that correct WDIOC_GETSUPPORT return path when perform copy_to_user() properly.

Thanks for catching this. I'm amazed that this driver still has bugs like = this.

drivers/watchdog/booke_wdt.c static long booke_wdt_ioctl(struct file *file, · line 166

Quoted 11 lines
drivers/watchdog/booke_wdt.c +2 -1
index 3fe82d0..2be7f29 100644
@@ -162,12 +162,13 @@ static long booke_wdt_ioctl(struct file *file,
                                unsigned int cmd, unsigned long arg)
 {
        u32 tmp =3D 0;
-       u32 __user *p =3D (u32 __user *)arg;
+       void __user *argp =3D (u32 __user *)arg;
+       u32 __user *p =3D argp;

You don't need to create 'argp'. The existing 'p' variable will work in the copy_to_user() call.

drivers/watchdog/booke_wdt.c static long booke_wdt_ioctl(struct file *file, · line 171

Quoted 2 lines
drivers/watchdog/booke_wdt.c +2 -0
+               return copy_to_user(argp, &ident,
+                               sizeof(ident)) ? -EFAULT : 0;

This can fit in one line, especially if you use 'p' instead of 'argp'.

--=20
Timur Tabi
Linux kernel developer at Freescale=

#4 lore raw
Quoted 5 lines · On Mon, Aug 6, 2012 at 2:12 PM, Tabi Timur-B04825 <[email protected]> wrote:
Quoted 2 lines · Tiejun Chen

We miss that correct WDIOC_GETSUPPORT return path when perform copy_to_user() properly.

Thanks for catching this. I'm amazed that this driver still has bugs like this.

While you're at it, I found a few related bugs. Can you fix these, also?

1. case WDIOC_SETOPTIONS:

		if (get_user(tmp, p))
			return -EINVAL;

This should return -EFAULT.

2. case WDIOC_GETBOOTSTATUS:

		/* XXX: something is clearing TSR */
		tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
		/* returns CARDRESET if last reset was caused by the WDT */
		return (tmp ? WDIOF_CARDRESET : 0);

This should use put_user() to return the value, instead of returning it as a return code.

You can title the new patch something like, "booke/wdt: some ioctls do not return values properly"

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#5 lore raw
Quoted 21 lines · On 08/07/2012 09:19 AM, Tabi Timur-B04825 wrote:
Quoted 5 lines · On Mon, Aug 6, 2012 at 2:12 PM, Tabi Timur-B04825 <[email protected]> wrote:
Quoted 2 lines · Tiejun Chen

We miss that correct WDIOC_GETSUPPORT return path when perform copy_to_user() properly.

Thanks for catching this. I'm amazed that this driver still has bugs like this.

While you're at it, I found a few related bugs. Can you fix these, also?

1. case WDIOC_SETOPTIONS:

		if (get_user(tmp, p))
			return -EINVAL;

This should return -EFAULT.

2. case WDIOC_GETBOOTSTATUS:

		/* XXX: something is clearing TSR */
		tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
		/* returns CARDRESET if last reset was caused by the WDT */
		return (tmp ? WDIOF_CARDRESET : 0);
Quoted 5 lines

This should use put_user() to return the value, instead of returning it as a return code.

You can title the new patch something like, "booke/wdt: some ioctls do not return values properly"

Will regenerate this patch including these error as v2.

Thanks
Tiejun