Skip to contentSkip to conversation

input: Null-termination of strings returned to userspace

PATCH New Magnus Vigerlöf 1 participant 1 message Aug 2, 2006 Input drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 input: Null-termination of strings returned to userspace

Removes the risk of returning non-null terminated strings to userspace in those cases the provided buffer is too small.

---

drivers/input/evdev.c +6 -4
index 12c7ab8..667333c 100644
@@ -377,11 +377,13 @@ static int str_to_user(const char *str, 
 	if (!str)
 		return -ENOENT;
 
-	len = strlen(str) + 1;
-	if (len > maxlen)
-		len = maxlen;
+	len = strlen(str);
+	if (len >= maxlen)
+		len = maxlen - 1;
 
-	return copy_to_user(p, str, len) ? -EFAULT : len;
+	if (copy_to_user(p, str, len))
+		return -EFAULT;
+	return put_user('\0', (char __user *)p + len) ? -EFAULT : len + 1;
 }
 
 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,