Skip to contentSkip to conversation

vme_user: tighten slave window bounds and validate offsets

PATCH In review Lucas Faria Mendes 3 participants 3 messages May 27 – May 28

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

PATCH 1/1 vme_user: tighten slave window bounds and validate offsets

Limit slave read/write operations to the allocated buffer size to prevent out-of-bounds access when a slave window is configured larger than its buffer.
Treat zero-sized windows and invalid file positions as errors, and reject VME_SET_SLAVE requests that exceed the slave buffer. This makes the user access driver more robust and avoids silent EOF on invalid offsets.

drivers/staging/vme_user/vme_user.c +29 -8
index 11e25c2f6..ca65cb57c 100644
@@ -181,6 +181,7 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
 	unsigned int minor = iminor(file_inode(file));
 	ssize_t retval;
 	size_t image_size;
+	size_t max_size;
 
 	if (minor == CONTROL_MINOR)
 		return 0;
@@ -189,16 +190,24 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
 
 	/* XXX Do we *really* want this helper - we can use vme_*_get ? */
 	image_size = vme_get_size(image[minor].resource);
+	if (!image_size) {
+		mutex_unlock(&image[minor].mutex);
+		return -EINVAL;
+	}
+
+	max_size = image_size;
+	if (type[minor] == SLAVE_MINOR && max_size > image[minor].size_buf)
+		max_size = image[minor].size_buf;
 
 	/* Ensure we are starting at a valid location */
-	if ((*ppos < 0) || (*ppos > (image_size - 1))) {
+	if ((*ppos < 0) || (*ppos >= max_size)) {
 		mutex_unlock(&image[minor].mutex);
-		return 0;
+		return -EINVAL;
 	}
 
 	/* Ensure not reading past end of the image */
-	if (*ppos + count > image_size)
-		count = image_size - *ppos;
+	if (*ppos + count > max_size)
+		count = max_size - *ppos;
 
 	switch (type[minor]) {
 	case MASTER_MINOR:
@@ -224,6 +233,7 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
 	unsigned int minor = iminor(file_inode(file));
 	ssize_t retval;
 	size_t image_size;
+	size_t max_size;
 
 	if (minor == CONTROL_MINOR)
 		return 0;
@@ -231,16 +241,24 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
 	mutex_lock(&image[minor].mutex);
 
 	image_size = vme_get_size(image[minor].resource);
+	if (!image_size) {
+		mutex_unlock(&image[minor].mutex);
+		return -EINVAL;
+	}
+
+	max_size = image_size;
+	if (type[minor] == SLAVE_MINOR && max_size > image[minor].size_buf)
+		max_size = image[minor].size_buf;
 
 	/* Ensure we are starting at a valid location */
-	if ((*ppos < 0) || (*ppos > (image_size - 1))) {
+	if ((*ppos < 0) || (*ppos >= max_size)) {
 		mutex_unlock(&image[minor].mutex);
-		return 0;
+		return -EINVAL;
 	}
 
 	/* Ensure not reading past end of the image */
-	if (*ppos + count > image_size)
-		count = image_size - *ppos;
+	if (*ppos + count > max_size)
+		count = max_size - *ppos;
 
 	switch (type[minor]) {
 	case MASTER_MINOR:
@@ -394,6 +412,9 @@ static int vme_user_ioctl(struct inode *inode, struct file *file,
 				return -EFAULT;
 			}
 
+			if (slave.size > image[minor].size_buf)
+				return -EINVAL;
+
 			/* XXX	We do not want to push aspace, cycle and width
 			 *	to userspace as they are
 			 */
---
 drivers/staging/vme_user/vme_user.c | 37 ++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 8 deletions(-)
drivers/staging/vme_user/vme_user.c +29 -8
index 11e25c2f6..ca65cb57c 100644
@@ -181,6 +181,7 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
 	unsigned int minor = iminor(file_inode(file));
 	ssize_t retval;
 	size_t image_size;
+	size_t max_size;
 
 	if (minor == CONTROL_MINOR)
 		return 0;
@@ -189,16 +190,24 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
 
 	/* XXX Do we *really* want this helper - we can use vme_*_get ? */
 	image_size = vme_get_size(image[minor].resource);
+	if (!image_size) {
+		mutex_unlock(&image[minor].mutex);
+		return -EINVAL;
+	}
+
+	max_size = image_size;
+	if (type[minor] == SLAVE_MINOR && max_size > image[minor].size_buf)
+		max_size = image[minor].size_buf;
 
 	/* Ensure we are starting at a valid location */
-	if ((*ppos < 0) || (*ppos > (image_size - 1))) {
+	if ((*ppos < 0) || (*ppos >= max_size)) {
 		mutex_unlock(&image[minor].mutex);
-		return 0;
+		return -EINVAL;
 	}
 
 	/* Ensure not reading past end of the image */
-	if (*ppos + count > image_size)
-		count = image_size - *ppos;
+	if (*ppos + count > max_size)
+		count = max_size - *ppos;
 
 	switch (type[minor]) {
 	case MASTER_MINOR:
@@ -224,6 +233,7 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
 	unsigned int minor = iminor(file_inode(file));
 	ssize_t retval;
 	size_t image_size;
+	size_t max_size;
 
 	if (minor == CONTROL_MINOR)
 		return 0;
@@ -231,16 +241,24 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
 	mutex_lock(&image[minor].mutex);
 
 	image_size = vme_get_size(image[minor].resource);
+	if (!image_size) {
+		mutex_unlock(&image[minor].mutex);
+		return -EINVAL;
+	}
+
+	max_size = image_size;
+	if (type[minor] == SLAVE_MINOR && max_size > image[minor].size_buf)
+		max_size = image[minor].size_buf;
 
 	/* Ensure we are starting at a valid location */
-	if ((*ppos < 0) || (*ppos > (image_size - 1))) {
+	if ((*ppos < 0) || (*ppos >= max_size)) {
 		mutex_unlock(&image[minor].mutex);
-		return 0;
+		return -EINVAL;
 	}
 
 	/* Ensure not reading past end of the image */
-	if (*ppos + count > image_size)
-		count = image_size - *ppos;
+	if (*ppos + count > max_size)
+		count = max_size - *ppos;
 
 	switch (type[minor]) {
 	case MASTER_MINOR:
@@ -394,6 +412,9 @@ static int vme_user_ioctl(struct inode *inode, struct file *file,
 				return -EFAULT;
 			}
 
+			if (slave.size > image[minor].size_buf)
+				return -EINVAL;
+
 			/* XXX	We do not want to push aspace, cycle and width
 			 *	to userspace as they are
 			 */
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 7 lines · Lucas Faria Mendes

Limit slave read/write operations to the allocated buffer size to prevent out-of-bounds access when a slave window is configured larger than its buffer.
Treat zero-sized windows and invalid file positions as errors, and reject VME_SET_SLAVE requests that exceed the slave buffer. This makes the user access driver more robust and avoids silent EOF on invalid offsets.

Odd line-wrapping :(

How did you find this issue? How was it tested?

thanks,

greg k-h

#2 lore raw
In reply to Lucas Faria Mendes
Quoted 9 lines · Lucas Faria Mendes

Limit slave read/write operations to the allocated buffer size to prevent out-of-bounds access when a slave window is configured larger than its buffer.
Treat zero-sized windows and invalid file positions as errors, and reject VME_SET_SLAVE requests that exceed the slave buffer. This makes the user access driver more robust and avoids silent EOF on invalid offsets.

No Fixes tag.

This is a grab bag of changes. It needs to be split up into multiple changes.

Quoted 14 lines
drivers/staging/vme_user/vme_user.c +1 -0
index 11e25c2f6..ca65cb57c 100644
@@ -181,6 +181,7 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
 	unsigned int minor = iminor(file_inode(file));
 	ssize_t retval;
 	size_t image_size;
+	size_t max_size;
 
 	if (minor == CONTROL_MINOR)
 		return 0;

drivers/staging/vme_user/vme_user.c static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, · line 195

Quoted 6 lines
drivers/staging/vme_user/vme_user.c +3 -0
@@ -189,16 +190,24 @@ static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
 
 	/* XXX Do we *really* want this helper - we can use vme_*_get ? */
 	image_size = vme_get_size(image[minor].resource);
+	if (!image_size) {
+		mutex_unlock(&image[minor].mutex);
+		return -EINVAL;

The original code looks pretty shady. Sure. This is fine.

drivers/staging/vme_user/vme_user.c static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, · line 200

Quoted 5 lines
drivers/staging/vme_user/vme_user.c +5 -0
+	}
+
+	max_size = image_size;
+	if (type[minor] == SLAVE_MINOR && max_size > image[minor].size_buf)
+		max_size = image[minor].size_buf;

I don't understand this chunk. It seems unrelated to a zero return from vme_get_size(). It needs to be in a separate patch.

drivers/staging/vme_user/vme_user.c static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, · line 203

Quoted 4 lines
drivers/staging/vme_user/vme_user.c +1 -1
 	/* Ensure we are starting at a valid location */
-	if ((*ppos < 0) || (*ppos > (image_size - 1))) {
+	if ((*ppos < 0) || (*ppos >= max_size)) {

Fine. The zero size - 1 is no good.

drivers/staging/vme_user/vme_user.c static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, · line 205

Quoted 3 lines
drivers/staging/vme_user/vme_user.c +1 -1
 		mutex_unlock(&image[minor].mutex);
-		return 0;
+		return -EINVAL;

No. You can't change this because it's API. Also unrelated.

drivers/staging/vme_user/vme_user.c static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count, · line 210

Quoted 7 lines
drivers/staging/vme_user/vme_user.c +2 -2
 	}
 
 	/* Ensure not reading past end of the image */
-	if (*ppos + count > image_size)
-		count = image_size - *ppos;
+	if (*ppos + count > max_size)
+		count = max_size - *ppos;

This is unrelated to zero size so it would go in the other patch.

Quoted 30 lines
drivers/staging/vme_user/vme_user.c +11 -2
 	switch (type[minor]) {
 	case MASTER_MINOR:
@@ -224,6 +233,7 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
 	unsigned int minor = iminor(file_inode(file));
 	ssize_t retval;
 	size_t image_size;
+	size_t max_size;
 
 	if (minor == CONTROL_MINOR)
 		return 0;
@@ -231,16 +241,24 @@ static ssize_t vme_user_write(struct file *file, const char __user *buf,
 	mutex_lock(&image[minor].mutex);
 
 	image_size = vme_get_size(image[minor].resource);
+	if (!image_size) {
+		mutex_unlock(&image[minor].mutex);
+		return -EINVAL;
+	}
+
+	max_size = image_size;
+	if (type[minor] == SLAVE_MINOR && max_size > image[minor].size_buf)
+		max_size = image[minor].size_buf;
 
 	/* Ensure we are starting at a valid location */
-	if ((*ppos < 0) || (*ppos > (image_size - 1))) {
+	if ((*ppos < 0) || (*ppos >= max_size)) {
 		mutex_unlock(&image[minor].mutex);
-		return 0;
+		return -EINVAL;

drivers/staging/vme_user/vme_user.c static ssize_t vme_user_write(struct file *file, const char __user *buf, · line 264

Quoted 10 lines
drivers/staging/vme_user/vme_user.c +2 -2
 	}
 
 	/* Ensure not reading past end of the image */
-	if (*ppos + count > image_size)
-		count = image_size - *ppos;
+	if (*ppos + count > max_size)
+		count = max_size - *ppos;
 
 	switch (type[minor]) {
 	case MASTER_MINOR:

Same review comments.

drivers/staging/vme_user/vme_user.c static int vme_user_ioctl(struct inode *inode, struct file *file, · line 416

Quoted 6 lines
drivers/staging/vme_user/vme_user.c +2 -0
@@ -394,6 +412,9 @@ static int vme_user_ioctl(struct inode *inode, struct file *file,
 				return -EFAULT;
 			}
 
+			if (slave.size > image[minor].size_buf)
+				return -EINVAL;

This change is not explained well. It would need to be in it's own commit message.

The size is supposed to be checked in vme_check_window() so this is the wrong place. I looked at the checking and it seems okay to me.

The rest is just more of the same.

regards,
dan carpenter