[13/13] media: atomisp: Ensure that USERPTR pointers are page aligned

Message ID 20220821215027.461344-13-hdegoede@redhat.com (mailing list archive)
State Accepted
Headers
Series [01/13] media: atomisp_gmin_platform: Switch to use acpi_evaluate_dsm_typed() |

Commit Message

Hans de Goede Aug. 21, 2022, 9:50 p.m. UTC
  The atomisp code needs USERPTR pointers to be page aligned,
otherwise bad things (scribbling over other parts of the
process' RAM) happen.

Add a check to ensure this and exit VIDIOC_QBUF calls with
unaligned pointers with -EINVAL.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/staging/media/atomisp/pci/atomisp_ioctl.c | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

Andy Shevchenko Aug. 22, 2022, 1:03 p.m. UTC | #1
On Mon, Aug 22, 2022 at 12:50 AM Hans de Goede <hdegoede@redhat.com> wrote:
>
> The atomisp code needs USERPTR pointers to be page aligned,
> otherwise bad things (scribbling over other parts of the
> process' RAM) happen.
>
> Add a check to ensure this and exit VIDIOC_QBUF calls with
> unaligned pointers with -EINVAL.

...

>         if (buf->memory == V4L2_MEMORY_USERPTR) {
> +               if (buf->m.userptr & ~PAGE_MASK) {

offset_in_page() ?

Further we may utilize helpers from pfn.h in the driver.

> +                       dev_err(isp->dev, "Error userptr is not page aligned.\n");
> +                       ret = -EINVAL;
> +                       goto error;
> +               }
  
Hans de Goede Aug. 22, 2022, 3:03 p.m. UTC | #2
Hi,

On 8/22/22 15:03, Andy Shevchenko wrote:
> On Mon, Aug 22, 2022 at 12:50 AM Hans de Goede <hdegoede@redhat.com> wrote:
>>
>> The atomisp code needs USERPTR pointers to be page aligned,
>> otherwise bad things (scribbling over other parts of the
>> process' RAM) happen.
>>
>> Add a check to ensure this and exit VIDIOC_QBUF calls with
>> unaligned pointers with -EINVAL.
> 
> ...
> 
>>         if (buf->memory == V4L2_MEMORY_USERPTR) {
>> +               if (buf->m.userptr & ~PAGE_MASK) {
> 
> offset_in_page() ?

Ack I've switched to offset_in_page() for v2.

Regards,

Hans


> 
> Further we may utilize helpers from pfn.h in the driver.
> 
>> +                       dev_err(isp->dev, "Error userptr is not page aligned.\n");
>> +                       ret = -EINVAL;
>> +                       goto error;
>> +               }
>
  

Patch

diff --git a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
index 7ecee39ef5a4..c8c6f9f8f0b8 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
@@ -1345,6 +1345,12 @@  static int atomisp_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
 	 * address and reprograme out page table properly
 	 */
 	if (buf->memory == V4L2_MEMORY_USERPTR) {
+		if (buf->m.userptr & ~PAGE_MASK) {
+			dev_err(isp->dev, "Error userptr is not page aligned.\n");
+			ret = -EINVAL;
+			goto error;
+		}
+
 		vb = pipe->capq.bufs[buf->index];
 		vm_mem = vb->priv;
 		if (!vm_mem) {