Skip to contentSkip to conversation

media:usb:cpia2: Properly check framebuffer mmap offsets

PATCH In review Omer Shalev 3 participants 7 messages Nov 8, 2019 – Nov 11, 2019 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media:usb:cpia2: Properly check framebuffer mmap offsets

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

---
 drivers/media/usb/cpia2/cpia2_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/media/usb/cpia2/cpia2_core.c +6 -2
index 20c50c2d042e..9d621cfb2d74 100644
@@ -2390,18 +2390,22 @@ int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma)
 {
 	const char *adr = (const char *)vma->vm_start;
 	unsigned long size = vma->vm_end-vma->vm_start;
-	unsigned long start_offset = vma->vm_pgoff << PAGE_SHIFT;
 	unsigned long start = (unsigned long) adr;
+	unsigned long start_offset;
 	unsigned long page, pos;
 
 	DBG("mmap offset:%ld size:%ld\n", start_offset, size);
 
 	if (!video_is_registered(&cam->vdev))
 		return -ENODEV;
+
+	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
+		return -EINVAL;
 
+	start_offset = vma->vm_pgoff << PAGE_SHIFT;
 	if (size > cam->frame_size*cam->num_frames  ||
 	    (start_offset % cam->frame_size) != 0 ||
-	    (start_offset+size > cam->frame_size*cam->num_frames))
+	    (start_offset > cam->frame_size*cam->num_frames - size))
 		return -EINVAL;
 
 	pos = ((unsigned long) (cam->frame_buffer)) + start_offset;
Signature · 1 line
2.23.0
#1 lore raw
Quoted 3 lines · Omer Shalev

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

I thought we checked that in the core of the kernel now, to keep all drivers from not having to do this type of thing (as they obviously all forgot to.) Why is this still needed here as well?

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 8 lines · Greg Kroah-Hartman
Quoted 3 lines · Omer Shalev

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

I thought we checked that in the core of the kernel now, to keep all drivers from not having to do this type of thing (as they obviously all forgot to.) Why is this still needed here as well?

Where is that checked in the core? I couldn't find anything, but I might have been looking in the wrong place.

Regards,

Hans

Quoted 5 lines

thanks,

greg k-h

#3 lore raw
Quoted 8 lines · Hans Verkuil

Hi Greg,

Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines · Omer Shalev

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

Quoted 6 lines
Quoted 3 lines

I thought we checked that in the core of the kernel now, to keep all drivers from not having to do this type of thing (as they obviously all forgot to.) Why is this still needed here as well?

Where is that checked in the core? I couldn't find anything, but I might have been looking in the wrong place.

Sorry, took me a while to find it. Look at be83bbf80682 ("mmap:
introduce sane default mmap limits") as I think this should handle the problem already.

thanks,

greg k-h

#4 lore raw
Quoted 16 lines · Greg Kroah-Hartman
Quoted 8 lines · Hans Verkuil

Hi Greg,

Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines · Omer Shalev

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

Quoted 6 lines
Quoted 3 lines

I thought we checked that in the core of the kernel now, to keep all drivers from not having to do this type of thing (as they obviously all forgot to.) Why is this still needed here as well?

Where is that checked in the core? I couldn't find anything, but I might have been looking in the wrong place.

Quoted 7 lines

Sorry, took me a while to find it. Look at be83bbf80682 ("mmap:
introduce sane default mmap limits") as I think this should handle the problem already.

thanks,

greg k-h

Thanks Greg. But All other drivers I've seen implement it like that: if(size > total_size || offset > total_size - size). Which I think, is a better way to write this code, and generally more secure. Plus, no extra code is needed (just changing this line).

Please let me know what you think.

Best regards,

Omer

#5 lore raw
Quoted 21 lines · Omer Shalev
Quoted 20 lines · Greg Kroah-Hartman
Quoted 8 lines · Hans Verkuil

Hi Greg,

Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines · Omer Shalev

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

Quoted 6 lines
Quoted 3 lines

I thought we checked that in the core of the kernel now, to keep all drivers from not having to do this type of thing (as they obviously all forgot to.) Why is this still needed here as well?

Where is that checked in the core? I couldn't find anything, but I might have been looking in the wrong place.

Sorry, took me a while to find it. Look at be83bbf80682 ("mmap:
introduce sane default mmap limits") as I think this should handle the problem already.

Quoted 7 lines
Quoted 3 lines

thanks,

greg k-h

Thanks Greg. But All other drivers I've seen implement it like that: if(size > total_size || offset > total_size - size). Which I think, is a better way to write this code, and generally more secure. Plus, no extra code is needed (just changing this line).

The point of the above commit that is in the tree is that no driver has to do this check at all, it's already been done before the driver ever gets called, right?

So yes, there's lots of history of drivers doing the check themselves (and getting it wrong as you point out), but that should not matter anymore.

Can you verify that your change isn't even needed due to the above mentioned core check for valid values?

thanks,

greg k-h

#6 lore raw
Quoted 38 lines · Greg Kroah-Hartman
Quoted 21 lines · Omer Shalev
Quoted 20 lines · Greg Kroah-Hartman
Quoted 8 lines · Hans Verkuil

Hi Greg,

Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines · Omer Shalev

The cpai2 driver's mmap implementation wasn't properly check for all possible offset values. Given a huge offset value , the calculation start_offset + size can wrap around to a low value and pass the check

Quoted 6 lines
Quoted 3 lines

I thought we checked that in the core of the kernel now, to keep all drivers from not having to do this type of thing (as they obviously all forgot to.) Why is this still needed here as well?

Where is that checked in the core? I couldn't find anything, but I might have been looking in the wrong place.

Sorry, took me a while to find it. Look at be83bbf80682 ("mmap:
introduce sane default mmap limits") as I think this should handle the problem already.

Quoted 7 lines
Quoted 3 lines

thanks,

greg k-h

Thanks Greg. But All other drivers I've seen implement it like that: if(size > total_size || offset > total_size - size). Which I think, is a better way to write this code, and generally more secure. Plus, no extra code is needed (just changing this line).

The point of the above commit that is in the tree is that no driver has to do this check at all, it's already been done before the driver ever gets called, right?

So yes, there's lots of history of drivers doing the check themselves (and getting it wrong as you point out), but that should not matter anymore.

Quoted 6 lines

Can you verify that your change isn't even needed due to the above mentioned core check for valid values?

thanks,

greg k-h

Yes I got it , and thanks again. I think that programmatically , its better to write that this way, And therefore I suggested this patch.

thanks,

Omer