media: v4l2-ioctl.c: fix incorrect error path
authorHans Verkuil <hverkuil-cisco@xs4all.nl>
Wed, 18 May 2022 13:06:31 +0000 (14:06 +0100)
committerMauro Carvalho Chehab <mchehab@kernel.org>
Tue, 27 Sep 2022 08:24:43 +0000 (10:24 +0200)
If allocating array_buf fails, or copying data from userspace into that
buffer fails, then just free memory and return the error. Don't attempt
to call video_put_user() since there is no point, and it would copy back
data on error even if INFO_FL_ALWAYS_COPY wasn't set.

So if writing the array back to userspace fails, then don't go to
out_array_args, instead just continue with the regular code that just
returns the error unless 'always_copy' is set.

Update the VIDIOC_G/S/TRY_EXT_CTRLS ioctls to set the ALWAYS_COPY flag
since they now need it. Before this worked due to this buggy code, but
now that that is fixed these ioctls need to set this flag explicitly.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
drivers/media/v4l2-core/v4l2-ioctl.c

index c314025..e6fd355 100644 (file)
@@ -2872,9 +2872,9 @@ static const struct v4l2_ioctl_info v4l2_ioctls[] = {
        IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
        IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
        IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
-       IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
-       IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
-       IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
+       IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
+       IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
+       IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
        IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
        IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
        IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
@@ -3367,8 +3367,7 @@ video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
                array_buf = kvmalloc(array_size, GFP_KERNEL);
                err = -ENOMEM;
                if (array_buf == NULL)
-                       goto out_array_args;
-               err = -EFAULT;
+                       goto out;
                if (in_compat_syscall())
                        err = v4l2_compat_get_array_args(file, array_buf,
                                                         user_ptr, array_size,
@@ -3377,7 +3376,7 @@ video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
                        err = copy_from_user(array_buf, user_ptr, array_size) ?
                                                                -EFAULT : 0;
                if (err)
-                       goto out_array_args;
+                       goto out;
                *kernel_ptr = array_buf;
        }
 
@@ -3395,6 +3394,13 @@ video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
                        trace_v4l2_qbuf(video_devdata(file)->minor, parg);
        }
 
+       /*
+        * Some ioctls can return an error, but still have valid
+        * results that must be returned.
+        */
+       if (err < 0 && !always_copy)
+               goto out;
+
        if (has_array_args) {
                *kernel_ptr = (void __force *)user_ptr;
                if (in_compat_syscall()) {
@@ -3409,16 +3415,8 @@ video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
                } else if (copy_to_user(user_ptr, array_buf, array_size)) {
                        err = -EFAULT;
                }
-               goto out_array_args;
        }
-       /*
-        * Some ioctls can return an error, but still have valid
-        * results that must be returned.
-        */
-       if (err < 0 && !always_copy)
-               goto out;
 
-out_array_args:
        if (video_put_user((void __user *)arg, parg, cmd, orig_cmd))
                err = -EFAULT;
 out: