Skip to contentSkip to conversation

drm: avoid passing null pointer to memset

PATCH In review Rodrigo Vivi 2 participants 4 messages Oct 9, 2012 – Oct 10, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: avoid passing null pointer to memset

When cmd isn't IOC_IN | IOC_OUT a null "kdata" goes to "memset", which dereferences it.

---
 drivers/gpu/drm/drm_drv.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_drv.c +9 -1
index 1490e76..5945920 100644
@@ -452,8 +452,16 @@ long drm_ioctl(struct file *filp,
 				retcode = -EFAULT;
 				goto err_i1;
 			}
-		} else
+		} else {
+			if (!kdata) {
+				kdata = kmalloc(usize, GFP_KERNEL);
+		    		if (!kdata) {
+					retcode = -ENOMEM;
+					goto err_i1;
+				}
+			}
 			memset(kdata, 0, usize);
+		}
 
 		if (ioctl->flags & DRM_UNLOCKED)
 			retcode = func(dev, kdata, file_priv);
Signature · 1 line
1.7.11.4
#1 lore raw
Quoted 2 lines · Rodrigo Vivi

When cmd isn't IOC_IN | IOC_OUT a null "kdata" goes to "memset", which dereferences it.

usize should be 0 in that case, since the ioctl is neither copying data in or out, for example I915_GEM_THROTTLE. To be on the safe side:
if (IOC_IN | IOC_OUT) { /* blah */ } else usize = 0;
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#2 lore raw

PATCH 1/1 drm: avoid passing null pointer to memset

In reply to Rodrigo Vivi

When cmd isn't IOC_IN | IOC_OUT a null "kdata" goes to "memset", which dereferences it.

v2: simpler version just using usize = 0 instead of allocating useless memory

---
 drivers/gpu/drm/drm_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_drv.c +2 -1
index 1490e76..f72dce5 100644
@@ -444,7 +444,8 @@ long drm_ioctl(struct file *filp,
 			}
 			if (asize > usize)
 				memset(kdata + usize, 0, asize - usize);
-		}
+		} else
+			usize = 0;
 
 		if (cmd & IOC_IN) {
 			if (copy_from_user(kdata, (void __user *)arg,
Signature · 1 line
1.7.11.4
Reviewed-by on v1 1/1
#3 lore raw
Quoted 5 lines · Rodrigo Vivi

When cmd isn't IOC_IN | IOC_OUT a null "kdata" goes to "memset", which dereferences it.

v2: simpler version just using usize = 0 instead of allocating useless memory

Presuming that coverity is smart enough not to complain about memcpy(NULL, src, 0),

-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre