media: v4l2: compat ioctl fixes
Subsystems (10)
v2 cover media: v4l2: compat ioctl fixes
From: Arnd Bergmann <[email protected]>
There was a report from Syzbot a while ago that I tried fixed earlier, but my fix did not get picked up because of a merge conflict with another patch I had in my tree.
I finally managed to take a close enough look at the merge conflict to figure out that the subdev driver handling for VIDIOC_DQEVENT_TIME32 was wrong in all combinations of the patches and just needs to be removed. In the process I also came across a couple of other issues, so the series has now grown to seven patches.
I have done randconfig build testing and found no compile time issues, but the driver specific patches have not been tested so far.
Arnd
Arnd Bergmann (7):
media: v4l2-core: ignore native time32 ioctls on 64-bit
media: v4l2-core: explicitly clear ioctl input data
media: subdev: remove VIDIOC_DQEVENT_TIME32 handling
media: v4l2-core: return -ENODEV from ioctl when not registered
media: atomisp: remove compat_ioctl32 code
media: subdev: fix compat_ioctl32
media: subdev: disallow ioctl for saa6588/davinci
drivers/media/i2c/adv7842.c | 3 + drivers/media/i2c/saa6588.c | 4 +- drivers/media/pci/bt8xx/bttv-driver.c | 6 +- drivers/media/pci/saa7134/saa7134-video.c | 6 +- drivers/media/platform/davinci/vpbe_display.c | 2 +- drivers/media/platform/davinci/vpbe_venc.c | 6 +- drivers/media/radio/si4713/si4713.c | 3 + drivers/media/v4l2-core/v4l2-compat-ioctl32.c | 3 + drivers/media/v4l2-core/v4l2-ioctl.c | 57 +- drivers/media/v4l2-core/v4l2-subdev.c | 43 +- drivers/staging/media/atomisp/Makefile | 1 - drivers/staging/media/atomisp/TODO | 5 + .../atomisp/pci/atomisp_compat_ioctl32.c | 1202 ----------------- .../staging/media/atomisp/pci/atomisp_fops.c | 8 +- include/media/v4l2-subdev.h | 7 +- 15 files changed, 81 insertions(+), 1275 deletions(-) delete mode 100644 drivers/staging/media/atomisp/pci/atomisp_compat_ioctl32.c
Signature · 17 lines
2.29.2 Cc: Hans Verkuil <[email protected]> Cc: Mauro Carvalho Chehab <[email protected]> Cc: "Lad, Prabhakar" <[email protected]> Cc: Eduardo Valentin <[email protected]> Cc: Sakari Ailus <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Vaibhav Gupta <[email protected]> Cc: Liu Shixin <[email protected]> Cc: Laurent Pinchart <[email protected]> Cc: Jacopo Mondi <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected]
PATCH v2 1/7 media: v4l2-core: ignore native time32 ioctls on 64-bit
From: Arnd Bergmann <[email protected]>
Syzbot found that passing ioctl command 0xc0505609 into a 64-bit kernel from a 32-bit process causes uninitialized kernel memory to get passed to drivers instead of the user space data:
BUG: KMSAN: uninit-value in check_array_args drivers/media/v4l2-core/v4l2-ioctl.c:3041 [inline] BUG: KMSAN: uninit-value in video_usercopy+0x1631/0x3d30 drivers/media/v4l2-core/v4l2-ioctl.c:3315 CPU: 0 PID: 19595 Comm: syz-executor.4 Not tainted 5.11.0-rc7-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:79 [inline] dump_stack+0x21c/0x280 lib/dump_stack.c:120 kmsan_report+0xfb/0x1e0 mm/kmsan/kmsan_report.c:118 __msan_warning+0x5f/0xa0 mm/kmsan/kmsan_instr.c:197 check_array_args drivers/media/v4l2-core/v4l2-ioctl.c:3041 [inline] video_usercopy+0x1631/0x3d30 drivers/media/v4l2-core/v4l2-ioctl.c:3315 video_ioctl2+0x9f/0xb0 drivers/media/v4l2-core/v4l2-ioctl.c:3391 v4l2_ioctl+0x255/0x290 drivers/media/v4l2-core/v4l2-dev.c:360 v4l2_compat_ioctl32+0x2c6/0x370 drivers/media/v4l2-core/v4l2-compat-ioctl32.c:1248 __do_compat_sys_ioctl fs/ioctl.c:842 [inline] __se_compat_sys_ioctl+0x53d/0x1100 fs/ioctl.c:793 __ia32_compat_sys_ioctl+0x4a/0x70 fs/ioctl.c:793 do_syscall_32_irqs_on arch/x86/entry/common.c:79 [inline] __do_fast_syscall_32+0x102/0x160 arch/x86/entry/common.c:141 do_fast_syscall_32+0x6a/0xc0 arch/x86/entry/common.c:166 do_SYSENTER_32+0x73/0x90 arch/x86/entry/common.c:209 entry_SYSENTER_compat_after_hwframe+0x4d/0x5c
The time32 commands are defined but were never meant to be called on 64-bit machines, as those have always used time64 interfaces. I missed this in my patch that introduced the time64 handling on 32-bit platforms.
The problem in this case is the mismatch of one function checking for the numeric value of the command and another function checking for the type of process (native vs compat) instead, with the result being that for this combination, nothing gets copied into the buffer at all.
Avoid this by only trying to convert the time32 commands when running on a 32-bit kernel where these are defined in a meaningful way.
---
This patch adds two more changes than the version that Hans tested
--- drivers/media/v4l2-core/v4l2-ioctl.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-ioctl.c +3 -3
@@ -3073,7 +3073,7 @@ static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,static unsigned int video_translate_cmd (unsigned int cmd) {switch (cmd) { -#ifdef CONFIG_COMPAT_32BIT_TIME +#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME )case VIDIOC_DQEVENT_TIME32 :return VIDIOC_DQEVENT ;case VIDIOC_QUERYBUF_TIME32 :
@@ -3127,7 +3127,7 @@ static int video_get_user(void __user *arg, void *parg, err =v4l2_compat_get_user (arg, parg, cmd); }else {switch (cmd) { -#ifdef CONFIG_COMPAT_32BIT_TIME +#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME )case VIDIOC_QUERYBUF_TIME32 :case VIDIOC_QBUF_TIME32 :case VIDIOC_DQBUF_TIME32 :
@@ -3182,7 +3182,7 @@ static int video_put_user(void __user *arg, void *parg,return v4l2_compat_put_user (arg, parg, cmd);switch (cmd) { -#ifdef CONFIG_COMPAT_32BIT_TIME +#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME )case VIDIOC_DQEVENT_TIME32 : {struct v4l2_event *ev = parg;struct v4l2_event_time32 ev32;
Signature · 1 line
2.29.2
PATCH v2 2/7 media: v4l2-core: explicitly clear ioctl input data
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
As seen from a recent syzbot bug report, mistakes in the compat ioctl implementation can lead to uninitialized kernel stack data getting used as input for driver ioctl handlers.
The reported bug is now fixed, but it's possible that other related bugs are still present or get added in the future. As the drivers need to check user input already, the possible impact is fairly low, but it might still cause an information leak.
To be on the safe side, always clear the entire ioctl buffer before calling the conversion handler functions that are meant to initialize them.
--- drivers/media/v4l2-core/v4l2-ioctl.c | 51 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-)
drivers/media/v4l2-core/v4l2-ioctl.c +29 -22
@@ -3122,12 +3122,23 @@ static int video_get_user(void __user *arg, void *parg,if (cmd == real_cmd) {if (copy_from_user (parg, (void __user *)arg, n)) - err = -EFAULT ; - }else if (in_compat_syscall ()) { - err =v4l2_compat_get_user (arg, parg, cmd); - }else { -switch (cmd) { +return -EFAULT ; + +/* zero out anything we don't copy from userspace */ +if (n <_IOC_SIZE (real_cmd)) +memset ((u8 *)parg + n,0 ,_IOC_SIZE (real_cmd) - n); + +return 0 ; + } + +/* zero out whole buffer first to deal with missing emulation */ +memset (parg,0 ,_IOC_SIZE (real_cmd)); + +if (in_compat_syscall ()) +return v4l2_compat_get_user (arg, parg, cmd); +#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME ) +switch (cmd) {case VIDIOC_QUERYBUF_TIME32 :case VIDIOC_QBUF_TIME32 :case VIDIOC_DQBUF_TIME32 :
@@ -3140,28 +3151,24 @@ static int video_get_user(void __user *arg, void *parg, *vb = (struct v4l2_buffer ) { .index = vb32.index, - .type = vb32.type, - .bytesused = vb32.bytesused, - .flags = vb32.flags, - .field = vb32.field, - .timestamp.tv_sec = vb32.timestamp.tv_sec, - .timestamp.tv_usec = vb32.timestamp.tv_usec, - .timecode = vb32.timecode, - .sequence = vb32.sequence, - .memory = vb32.memory, - .m.userptr = vb32.m.userptr, - .length = vb32.length, - .request_fd = vb32.request_fd, + .type = vb32.type, + .bytesused = vb32.bytesused, + .flags = vb32.flags, + .field = vb32.field, + .timestamp.tv_sec = vb32.timestamp.tv_sec, + .timestamp.tv_usec = vb32.timestamp.tv_usec, + .timecode = vb32.timecode, + .sequence = vb32.sequence, + .memory = vb32.memory, + .m.userptr = vb32.m.userptr, + .length = vb32.length, + .request_fd = vb32.request_fd, }; break; } -#endif - } } +#endif -/* zero out anything we don't copy from userspace */ -if (!err && n <_IOC_SIZE (real_cmd)) -memset ((u8 *)parg + n,0 ,_IOC_SIZE (real_cmd) - n);return err; }
Signature · 1 line
2.29.2
Re: [PATCH v2 2/7] media: v4l2-core: explicitly clear ioctl input data
Quoted 28 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
As seen from a recent syzbot bug report, mistakes in the compat ioctl implementation can lead to uninitialized kernel stack data getting used as input for driver ioctl handlers.
The reported bug is now fixed, but it's possible that other related bugs are still present or get added in the future. As the drivers need to check user input already, the possible impact is fairly low, but it might still cause an information leak.
To be on the safe side, always clear the entire ioctl buffer before calling the conversion handler functions that are meant to initialize them.
Signed-off-by Arnd Bergmann <[email protected]>--- drivers/media/v4l2-core/v4l2-ioctl.c | 51 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-)drivers/media/v4l2-core/v4l2-ioctl.c +0
-0@@ -3122,12 +3122,23 @@ static int video_get_user(void __user *arg, void *parg,if (cmd == real_cmd) {if (copy_from_user (parg, (void __user *)arg, n))
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3129
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ioctl.c +5
-5- err = -EFAULT ; - }else if (in_compat_syscall ()) { - err =v4l2_compat_get_user (arg, parg, cmd); - }else { -switch (cmd) { +return -EFAULT ; + +/* zero out anything we don't copy from userspace */ +if (n <_IOC_SIZE (real_cmd)) +memset ((u8 *)parg + n,0 ,_IOC_SIZE (real_cmd) - n);
This should always happen, not just when cmd == real_cmd.
The comment is a bit misleading: besides zeroing what isn't copied from userspace, it also zeroes copied fields based on INFO_FL_CLEAR_MASK.
With this change that no longer happens and v4l2-compliance starts complaining.
Quoted 5 lines
drivers/media/v4l2-core/v4l2-ioctl.c +5
-0+ +return 0 ; + } + +/* zero out whole buffer first to deal with missing emulation */
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3144
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ioctl.c +6
-0+memset (parg,0 ,_IOC_SIZE (real_cmd)); + +if (in_compat_syscall ()) +return v4l2_compat_get_user (arg, parg, cmd); +#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME ) +switch (cmd) {case VIDIOC_QUERYBUF_TIME32 :case VIDIOC_QBUF_TIME32 :case VIDIOC_DQBUF_TIME32 :
The 'case' statements need to be indented one tab less.
Quoted 18 lines
drivers/media/v4l2-core/v4l2-ioctl.c +2
-12@@ -3140,28 +3151,24 @@ static int video_get_user(void __user *arg, void *parg, *vb = (struct v4l2_buffer ) { .index = vb32.index, - .type = vb32.type, - .bytesused = vb32.bytesused, - .flags = vb32.flags, - .field = vb32.field, - .timestamp.tv_sec = vb32.timestamp.tv_sec, - .timestamp.tv_usec = vb32.timestamp.tv_usec, - .timecode = vb32.timecode, - .sequence = vb32.sequence, - .memory = vb32.memory, - .m.userptr = vb32.m.userptr, - .length = vb32.length, - .request_fd = vb32.request_fd, + .type = vb32.type, + .bytesused = vb32.bytesused,
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3165
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ioctl.c +10
-0+ .flags = vb32.flags, + .field = vb32.field, + .timestamp.tv_sec = vb32.timestamp.tv_sec, + .timestamp.tv_usec = vb32.timestamp.tv_usec, + .timecode = vb32.timecode, + .sequence = vb32.sequence, + .memory = vb32.memory, + .m.userptr = vb32.m.userptr, + .length = vb32.length, + .request_fd = vb32.request_fd,
Can you put these whitespace changes in a separate patch?
Quoted 5 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0
-1};break ; } -#endif
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3174
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ioctl.c +1
-4- } } +#endif -/* zero out anything we don't copy from userspace */ -if (!err && n <_IOC_SIZE (real_cmd)) -memset ((u8 *)parg + n,0 ,_IOC_SIZE (real_cmd) - n);return err; }
I ended up with this code, and then my tests passed:
if (cmd == real_cmd) {
if (copy_from_user(parg, (void __user *)arg, n))
return -EFAULT;
} else if (in_compat_syscall()) {
memset(parg, 0, n);
err = v4l2_compat_get_user(arg, parg, cmd);
} else {#if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
memset(parg, 0, n);
switch (cmd) {
case VIDIOC_QUERYBUF_TIME32:
case VIDIOC_QBUF_TIME32:
case VIDIOC_DQBUF_TIME32:
case VIDIOC_PREPARE_BUF_TIME32: {
struct v4l2_buffer_time32 vb32; struct v4l2_buffer *vb = parg;
if (copy_from_user(&vb32, arg, sizeof(vb32)))
return -EFAULT;
*vb = (struct v4l2_buffer) {
.index = vb32.index,
.type = vb32.type,
.bytesused = vb32.bytesused,
.flags = vb32.flags,
.field = vb32.field,
.timestamp.tv_sec = vb32.timestamp.tv_sec,
.timestamp.tv_usec = vb32.timestamp.tv_usec,
.timecode = vb32.timecode,
.sequence = vb32.sequence,
.memory = vb32.memory,
.m.userptr = vb32.m.userptr,
.length = vb32.length,
.request_fd = vb32.request_fd,
};
break;
}
}
#endif
}
/* zero out anything we don't copy from userspace */
if (!err && n < _IOC_SIZE(real_cmd))
memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n);
return err;That said, I also ran the regression tests on a i686 VM, and there I got a bunch of failures, but that was *without* your patches, so I think something unrelated broke. I'll have to dig more into this in the next few days.
But I wanted to get this out first, since this patch is clearly wrong.
Regards,
Hans
Re: [PATCH v2 2/7] media: v4l2-core: explicitly clear ioctl input data
Quoted 17 lines · Hans Verkuil
Quoted 4 lines · Arnd Bergmann
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0@@ -3122,12 +3122,23 @@ static int video_get_user(void __user *arg, void *parg,if (cmd == real_cmd) {if (copy_from_user (parg, (void __user *)arg, n))drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3129
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ioctl.c +5
-5- err = -EFAULT ; - }else if (in_compat_syscall ()) { - err =v4l2_compat_get_user (arg, parg, cmd); - }else { -switch (cmd) { +return -EFAULT ; + +/* zero out anything we don't copy from userspace */ +if (n <_IOC_SIZE (real_cmd)) +memset ((u8 *)parg + n,0 ,_IOC_SIZE (real_cmd) - n);This should always happen, not just when cmd ==
real_cmd.
Ok, got it. I was trying to simplify this, but I went a little too far, so in the case of VIDIOC_QUERYBUF_TIME32 I dropped the final clearing of the extra data, leaving the user data in place.
Quoted 2 lines
The comment is a bit misleading: besides zeroing what isn't copied from userspace, it also zeroes copied fields based on
INFO_FL_CLEAR_MASK.
I'm not following here, isn't that the same? We copy 'n' bytes, and then we clear 'size - n' bytes, which is everything that wasn't copied.
Quoted 11 lines
With this change that no longer happens and v4l2-compliance starts complaining.
Quoted 9 lines
drivers/media/v4l2-core/v4l2-ioctl.c +9
-0+ +return 0 ; + } + +/* zero out whole buffer first to deal with missing emulation */ +memset (parg,0 ,_IOC_SIZE (real_cmd)); + +if (in_compat_syscall ()) +return v4l2_compat_get_user (arg, parg, cmd);
Quoted 8 lines
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3144
Quoted 6 lines
drivers/media/v4l2-core/v4l2-ioctl.c +2
-0+#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME ) +switch (cmd) {case VIDIOC_QUERYBUF_TIME32 :case VIDIOC_QBUF_TIME32 :case VIDIOC_DQBUF_TIME32 :The 'case' statements need to be indented one tab less.
It seems this is no longer needed when I go back to having the switch() inside the else{}.
Quoted 22 lines
Quoted 22 lines
drivers/media/v4l2-core/v4l2-ioctl.c +6
-12@@ -3140,28 +3151,24 @@ static int video_get_user(void __user *arg, void *parg, *vb = (struct v4l2_buffer ) { .index = vb32.index, - .type = vb32.type, - .bytesused = vb32.bytesused, - .flags = vb32.flags, - .field = vb32.field, - .timestamp.tv_sec = vb32.timestamp.tv_sec, - .timestamp.tv_usec = vb32.timestamp.tv_usec, - .timecode = vb32.timecode, - .sequence = vb32.sequence, - .memory = vb32.memory, - .m.userptr = vb32.m.userptr, - .length = vb32.length, - .request_fd = vb32.request_fd, + .type = vb32.type, + .bytesused = vb32.bytesused, + .flags = vb32.flags, + .field = vb32.field, + .timestamp.tv_sec = vb32.timestamp.tv_sec, + .timestamp.tv_usec = vb32.timestamp.tv_usec,
Quoted 8 lines
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3165
Quoted 6 lines
drivers/media/v4l2-core/v4l2-ioctl.c +6
-0+ .timecode = vb32.timecode, + .sequence = vb32.sequence, + .memory = vb32.memory, + .m.userptr = vb32.m.userptr, + .length = vb32.length, + .request_fd = vb32.request_fd,Can you put these whitespace changes in a separate patch?
Sure.
Quoted 40 lines
I ended up with this code, and then my tests passed:
if (cmd == real_cmd) { if (copy_from_user(parg, (void __user *)arg, n)) return -EFAULT; } else if (in_compat_syscall()) { memset(parg, 0, n); err = v4l2_compat_get_user(arg, parg, cmd); } else {#if !defined(CONFIG_64BIT) &&
defined(CONFIG_COMPAT_32BIT_TIME)
memset(parg, 0, n);
switch (cmd) {
caseVIDIOC_QUERYBUF_TIME32:
caseVIDIOC_QBUF_TIME32:
caseVIDIOC_DQBUF_TIME32:
caseVIDIOC_PREPARE_BUF_TIME32: {
structv4l2_buffer_time32vb32; structv4l2_buffer*vb = parg;if (
copy_from_user(&vb32, arg, sizeof(vb32)))
return-EFAULT;*vb = (struct
v4l2_buffer) {
.index = vb32.index,
.type = vb32.type,
.bytesused = vb32.bytesused,
.flags = vb32.flags,
.field = vb32.field,
.timestamp.tv_sec = vb32.timestamp.tv_sec,
.timestamp.tv_usec = vb32.timestamp.tv_usec,
.timecode = vb32.timecode,
.sequence = vb32.sequence,
.memory = vb32.memory,
.m.userptr = vb32.m.userptr,
.length = vb32.length,
.request_fd = vb32.request_fd,
};
break;
}
}
Quoted 8 lines
#endif
}
/* zero out anything we don't copy from userspace */
if (!err && n < _IOC_SIZE(real_cmd))
memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n);
return err;Ok, so this version just adds the two memset(), without any other changes. That is clearly the safest change, and I'll send it like this in v3.
Quoted 5 lines
That said, I also ran the regression tests on a i686 VM, and there I got a bunch of failures, but that was *without* your patches, so I think something unrelated broke. I'll have to dig more into this in the next few days.
But I wanted to get this out first, since this patch is clearly wrong.
Thanks a lot for taking a look and giving it an initial test. I have updated the git tree at
git://git.kernel.org:/pub/scm/linux/kernel/git/arnd/playground.git
playground/v4l2-compat-ioctl
with the changes you pointed out. Let me know when you have found out what was going on in the VM guest, and I'll send it as v3 or integrate additional fixes that you find necessary.
Arnd
Re: [PATCH v2 2/7] media: v4l2-core: explicitly clear ioctl input data
Hi Arnd,
Quoted 147 lines · Arnd Bergmann
Quoted 17 lines · Hans Verkuil
Quoted 4 lines · Arnd Bergmann
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0@@ -3122,12 +3122,23 @@ static int video_get_user(void __user *arg, void *parg,if (cmd == real_cmd) {if (copy_from_user (parg, (void __user *)arg, n))drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3129
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ioctl.c +5
-5- err = -EFAULT ; - }else if (in_compat_syscall ()) { - err =v4l2_compat_get_user (arg, parg, cmd); - }else { -switch (cmd) { +return -EFAULT ; + +/* zero out anything we don't copy from userspace */ +if (n <_IOC_SIZE (real_cmd)) +memset ((u8 *)parg + n,0 ,_IOC_SIZE (real_cmd) - n);This should always happen, not just when cmd ==
real_cmd.Ok, got it. I was trying to simplify this, but I went a little too far, so in the case of
VIDIOC_QUERYBUF_TIME32I dropped the final clearing of the extra data, leaving the user data in place.Quoted 2 lines
The comment is a bit misleading: besides zeroing what isn't copied from userspace, it also zeroes copied fields based on
INFO_FL_CLEAR_MASK.I'm not following here, isn't that the same? We copy 'n' bytes, and then we clear 'size - n' bytes, which is everything that wasn't copied.
Quoted 11 lines
With this change that no longer happens and v4l2-compliance starts complaining.
Quoted 9 lines
drivers/media/v4l2-core/v4l2-ioctl.c +9
-0+ +return 0 ; + } + +/* zero out whole buffer first to deal with missing emulation */ +memset (parg,0 ,_IOC_SIZE (real_cmd)); + +if (in_compat_syscall ()) +return v4l2_compat_get_user (arg, parg, cmd);Quoted 8 lines
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3144
Quoted 6 lines
drivers/media/v4l2-core/v4l2-ioctl.c +2
-0+#if !defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT_32BIT_TIME ) +switch (cmd) {case VIDIOC_QUERYBUF_TIME32 :case VIDIOC_QBUF_TIME32 :case VIDIOC_DQBUF_TIME32 :The 'case' statements need to be indented one tab less.
It seems this is no longer needed when I go back to having the
switch()inside the else{}.Quoted 22 lines
Quoted 22 lines
drivers/media/v4l2-core/v4l2-ioctl.c +6
-12@@ -3140,28 +3151,24 @@ static int video_get_user(void __user *arg, void *parg, *vb = (struct v4l2_buffer ) { .index = vb32.index, - .type = vb32.type, - .bytesused = vb32.bytesused, - .flags = vb32.flags, - .field = vb32.field, - .timestamp.tv_sec = vb32.timestamp.tv_sec, - .timestamp.tv_usec = vb32.timestamp.tv_usec, - .timecode = vb32.timecode, - .sequence = vb32.sequence, - .memory = vb32.memory, - .m.userptr = vb32.m.userptr, - .length = vb32.length, - .request_fd = vb32.request_fd, + .type = vb32.type, + .bytesused = vb32.bytesused, + .flags = vb32.flags, + .field = vb32.field, + .timestamp.tv_sec = vb32.timestamp.tv_sec, + .timestamp.tv_usec = vb32.timestamp.tv_usec,Quoted 8 lines
drivers/media/v4l2-core/v4l2-ioctl.c static int video_get_user(void __user *arg, void *parg, · line 3165
Quoted 6 lines
drivers/media/v4l2-core/v4l2-ioctl.c +6
-0+ .timecode = vb32.timecode, + .sequence = vb32.sequence, + .memory = vb32.memory, + .m.userptr = vb32.m.userptr, + .length = vb32.length, + .request_fd = vb32.request_fd,Can you put these whitespace changes in a separate patch?
Sure.
Quoted 40 lines
I ended up with this code, and then my tests passed:
if (cmd == real_cmd) { if (copy_from_user(parg, (void __user *)arg, n)) return -EFAULT; } else if (in_compat_syscall()) { memset(parg, 0, n); err = v4l2_compat_get_user(arg, parg, cmd); } else {#if !defined(CONFIG_64BIT) &&
defined(CONFIG_COMPAT_32BIT_TIME)
memset(parg, 0, n);
switch (cmd) {
caseVIDIOC_QUERYBUF_TIME32:
caseVIDIOC_QBUF_TIME32:
caseVIDIOC_DQBUF_TIME32:
caseVIDIOC_PREPARE_BUF_TIME32: {
structv4l2_buffer_time32vb32; structv4l2_buffer*vb = parg;if (
copy_from_user(&vb32, arg, sizeof(vb32)))
return-EFAULT;*vb = (struct
v4l2_buffer) {
.index = vb32.index,
.type = vb32.type,
.bytesused = vb32.bytesused,
.flags = vb32.flags,
.field = vb32.field,
.timestamp.tv_sec = vb32.timestamp.tv_sec,
.timestamp.tv_usec = vb32.timestamp.tv_usec,
.timecode = vb32.timecode,
.sequence = vb32.sequence,
.memory = vb32.memory,
.m.userptr = vb32.m.userptr,
.length = vb32.length,
.request_fd = vb32.request_fd,
};
break;
}
}Quoted 8 lines
#endif } /* zero out anything we don't copy from userspace */ if (!err && n < _IOC_SIZE(real_cmd)) memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n); return err;Ok, so this version just adds the two
memset(), without any other changes. That is clearly the safest change, and I'll send it like this in v3.Quoted 5 lines
That said, I also ran the regression tests on a i686 VM, and there I got a bunch of failures, but that was *without* your patches, so I think something unrelated broke. I'll have to dig more into this in the next few days.
But I wanted to get this out first, since this patch is clearly wrong.
Thanks a lot for taking a look and giving it an initial test. I have updated the git tree at
Quoted 6 lines
git://git.kernel.org:/pub/scm/linux/kernel/git/arnd/playground.git
playground/v4l2-compat-ioctlwith the changes you pointed out. Let me know when you have found out what was going on in the VM guest, and I'll send it as v3 or integrate additional fixes that you find necessary.
Please post v3: one issue I had turned out to be due to a misconfiguration causing some of the tests to run out of memory.
The other issue is with the X32 ABI: that is currently failing, with or without your series. I'm not sure why yet, from what I remember this worked fine last time I tested. But my memory may be faulty, since it is so long ago.
Regards,
Hans
Quoted 3 lines
Arnd
PATCH v2 3/7 media: subdev: remove VIDIOC_DQEVENT_TIME32 handling
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
Converting the VIDIOC_DQEVENT_TIME32/VIDIOC_DQEVENT32/ VIDIOC_DQEVENT32_TIME32 arguments to the canonical form is done in common code, but for some reason I ended up adding another conversion helper to subdev_do_ioctl() as well. I must have concluded that this does not go through the common conversion, but it has done that since the ioctl handler was first added.
I assume this one is harmless as there should be no way to arrive here from user space, but since it is dead code, it should just get removed.
--- drivers/media/v4l2-core/v4l2-subdev.c | 24 ------------------------ 1 file changed, 24 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +0 -24
@@ -428,30 +428,6 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)return v4l2_event_dequeue (vfh, arg, file->f_flags &O_NONBLOCK ); -case VIDIOC_DQEVENT_TIME32 : { -struct v4l2_event_time32 *ev32 = arg; -struct v4l2_event ev = { }; - -if (!(sd->flags &V4L2_SUBDEV_FL_HAS_EVENTS )) -return -ENOIOCTLCMD ; - - rval =v4l2_event_dequeue (vfh, &ev, file->f_flags &O_NONBLOCK ); - - *ev32 = (struct v4l2_event_time32 ) { - .type = ev.type, - .pending = ev.pending, - .sequence = ev.sequence, - .timestamp.tv_sec = ev.timestamp.tv_sec, - .timestamp.tv_nsec = ev.timestamp.tv_nsec, - .id = ev.id, - }; - -memcpy (&ev32->u, &ev.u,sizeof (ev.u)); -memcpy (&ev32->reserved, &ev.reserved,sizeof (ev.reserved)); - -return rval; - } -case VIDIOC_SUBSCRIBE_EVENT :return v4l2_subdev_call (sd, core, subscribe_event, vfh, arg);
Signature · 1 line
2.29.2
PATCH v2 4/7 media: v4l2-core: return -ENODEV from ioctl when not registered
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
I spotted a minor difference is handling of unregistered devices between native and compat ioctls: the native handler never tries to call into the driver if a device is not marked as registered.
I did not check whether this can cause issues in the kernel, or just a different between return codes, but it clearly makes sense that both should behave the same way.
--- drivers/media/v4l2-core/v4l2-compat-ioctl32.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/media/v4l2-core/v4l2-compat-ioctl32.c +3 -0
@@ -1244,6 +1244,9 @@ long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)if (!file->f_op->unlocked_ioctl)return ret; +if (!video_is_registered (vdev)) +return -ENODEV ; +if (_IOC_TYPE (cmd) =='V' &&_IOC_NR (cmd) <BASE_VIDIOC_PRIVATE ) ret = file->f_op->unlocked_ioctl (file, cmd, (unsigned long )compat_ptr (arg));
Signature · 1 line
2.29.2
PATCH v2 5/7 media: atomisp: remove compat_ioctl32 code
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
This is one of the last remaining users of compat_alloc_user_space() and copy_in_user(), which are in the process of getting removed.
As of commit 57e6b6f2303e ("media: atomisp_fops.c: disable atomisp_compat_ioctl32"), nothing in this file is actually getting used as the only reference has been stubbed out.
Remove the entire file -- anyone willing to restore the functionality can equally well just look up the contents in the git history if needed.
--- drivers/staging/media/atomisp/Makefile | 1 - drivers/staging/media/atomisp/TODO | 5 + .../atomisp/pci/atomisp_compat_ioctl32.c | 1202 ----------------- .../staging/media/atomisp/pci/atomisp_fops.c | 8 +- 4 files changed, 8 insertions(+), 1208 deletions(-) delete mode 100644 drivers/staging/media/atomisp/pci/atomisp_compat_ioctl32.c
drivers/staging/media/atomisp/Makefile +0 -1
@@ -16,7 +16,6 @@ atomisp-objs += \ pci/atomisp_acc.o \ pci/atomisp_cmd.o \ pci/atomisp_compat_css20.o \ - pci/atomisp_compat_ioctl32.o \ pci/atomisp_csi2.o \ pci/atomisp_drvfs.o \ pci/atomisp_file.o \
drivers/staging/media/atomisp/TODO +5 -0
@@ -120,6 +120,11 @@ TODO for this driver until the other work is done, as there will be a lot of code churn until this driver becomes functional again. +16. Fix private ioctls to not need a compat_ioctl handler for running + 32-bit tasks. The compat code has been removed because of bugs, + and should not be needed for modern drivers. Fixing this properly + unfortunately means an incompatible ABI change. + Limitations ===========
drivers/staging/media/atomisp/pci/atomisp_compat_ioctl32.c deleted +0 -1202
1,202 lines not on this page · raw message
drivers/staging/media/atomisp/pci/atomisp_fops.c +3 -5
@@ -1283,7 +1283,8 @@ const struct v4l2_file_operations atomisp_fops = { .unlocked_ioctl = video_ioctl2,#ifdef CONFIG_COMPAT /* -* There are problems with this code. Disable this for now. +* this was removed because of bugs, the interface +* needs to be made safe for compat tasks instead. .compat_ioctl32 = atomisp_compat_ioctl32, */ #endif
@@ -1297,10 +1298,7 @@ const struct v4l2_file_operations atomisp_file_fops = { .mmap = atomisp_file_mmap, .unlocked_ioctl = video_ioctl2,#ifdef CONFIG_COMPAT -/* -* There are problems with this code. Disable this for now. -.compat_ioctl32 = atomisp_compat_ioctl32, -*/ +/* .compat_ioctl32 = atomisp_compat_ioctl32, */ #endif .poll = atomisp_poll, };
Signature · 1 line
2.29.2
PATCH v2 6/7 media: subdev: fix compat_ioctl32
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
The adv7842 and si4713 drivers each define one private ioctl command that are handled through the subdev_ioctl() helpers, but that don't work in compat mode because this does not handle private ioctl commands.
The compat_ioctl32 callback for subdevs has outdated calling conventions, but as there are no users of that, it is easy to change the function pointer type and the caller to make it behave the same way as the normal ioctl callback and hook in the two drivers that need no argument conversion.
--- drivers/media/i2c/adv7842.c | 3 +++ drivers/media/radio/si4713/si4713.c | 3 +++ drivers/media/v4l2-core/v4l2-subdev.c | 19 ++++++++++++++++--- include/media/v4l2-subdev.h | 3 +-- 4 files changed, 23 insertions(+), 5 deletions(-)
drivers/media/i2c/adv7842.c +3 -0
@@ -3293,6 +3293,9 @@ static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {static const struct v4l2_subdev_core_ops adv7842_core_ops = { .log_status = adv7842_log_status, .ioctl = adv7842_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl32 = adv7842_ioctl, +#endif .interrupt_service_routine = adv7842_isr, .subscribe_event = adv7842_subscribe_event, .unsubscribe_event = v4l2_event_subdev_unsubscribe,
drivers/media/radio/si4713/si4713.c +3 -0
@@ -1398,6 +1398,9 @@ static const struct v4l2_ctrl_ops si4713_ctrl_ops = {static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = { .ioctl = si4713_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl32 = si4713_ioctl, +#endif };static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
drivers/media/v4l2-core/v4l2-subdev.c +16 -3
@@ -686,13 +686,26 @@ static long subdev_ioctl(struct file *file, unsigned int cmd, }#ifdef CONFIG_COMPAT -static long subdev_compat_ioctl32 (struct file *file,unsigned int cmd, -unsigned long arg) +static long subdev_do_compat_ioctl32 (struct file *file,unsigned int cmd,void *arg) {struct video_device *vdev =video_devdata (file);struct v4l2_subdev *sd =vdev_to_v4l2_subdev (vdev); +struct mutex *lock = vdev->lock; +long ret = -ENODEV ; -return v4l2_subdev_call (sd, core, compat_ioctl32, cmd, arg); +if (lock &&mutex_lock_interruptible (lock)) +return -ERESTARTSYS ; +if (video_is_registered (vdev)) + ret =v4l2_subdev_call (sd, core, compat_ioctl32, cmd, arg); +if (lock) +mutex_unlock (lock); +return ret; +} + +static long subdev_compat_ioctl32 (struct file *file,unsigned int cmd, +unsigned long arg) +{ +return video_usercopy (file, cmd, arg, subdev_do_compat_ioctl32); }#endif
include/media/v4l2-subdev.h +1 -2
@@ -195,8 +195,7 @@ struct v4l2_subdev_core_ops {int (*s_gpio)(struct v4l2_subdev *sd,u32 val);long (*ioctl)(struct v4l2_subdev *sd,unsigned int cmd,void *arg);#ifdef CONFIG_COMPAT -long (*compat_ioctl32)(struct v4l2_subdev *sd,unsigned int cmd, -unsigned long arg); +long (*compat_ioctl32)(struct v4l2_subdev *sd,unsigned int cmd,void *arg);#endif #ifdef CONFIG_VIDEO_ADV_DEBUG int (*g_register)(struct v4l2_subdev *sd,struct v4l2_dbg_register *reg);
Signature · 1 line
2.29.2
PATCH v2 7/7 media: subdev: disallow ioctl for saa6588/davinci
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
The saa6588_ioctl() function expects to get called from other kernel functions with a 'saa6588_command' pointer, but I found nothing stops it from getting called from user space instead, which seems rather dangerous.
The same thing happens in the davinci vpbe driver with its VENC_GET_FLD command.
As a quick fix, add a separate .command() callback pointer for this driver and change the two callers over to that. This change can easily get backported to stable kernels if necessary, but since there are only two drivers, we may want to eventually replace this with a set of more specialized callbacks in the long run.
--- drivers/media/i2c/saa6588.c | 4 ++-- drivers/media/pci/bt8xx/bttv-driver.c | 6 +++--- drivers/media/pci/saa7134/saa7134-video.c | 6 +++--- drivers/media/platform/davinci/vpbe_display.c | 2 +- drivers/media/platform/davinci/vpbe_venc.c | 6 ++---- include/media/v4l2-subdev.h | 4 ++++ 6 files changed, 15 insertions(+), 13 deletions(-)
drivers/media/i2c/saa6588.c +2 -2
@@ -380,7 +380,7 @@ static void saa6588_configure(struct saa6588 *s)/* ---------------------------------------------------------------------- */ -static long saa6588_ioctl (struct v4l2_subdev *sd,unsigned int cmd,void *arg) +static long saa6588_command (struct v4l2_subdev *sd,unsigned int cmd,void *arg) {struct saa6588 *s =to_saa6588 (sd);struct saa6588_command *a = arg;
@@ -433,7 +433,7 @@ static int saa6588_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)/* ----------------------------------------------------------------------- */ static const struct v4l2_subdev_core_ops saa6588_core_ops = { - .ioctl = saa6588_ioctl, + .command = saa6588_command, };static const struct v4l2_subdev_tuner_ops saa6588_tuner_ops = {
drivers/media/pci/bt8xx/bttv-driver.c +3 -3
@@ -3179,7 +3179,7 @@ static int radio_release(struct file *file) btv->radio_user--; -bttv_call_all (btv, core, ioctl,SAA6588_CMD_CLOSE , &cmd); +bttv_call_all (btv, core, command,SAA6588_CMD_CLOSE , &cmd);if (btv->radio_user ==0 ) btv->has_radio_tuner =0 ;
@@ -3260,7 +3260,7 @@ static ssize_t radio_read(struct file *file, char __user *data, cmd.result = -ENODEV ;radio_enable (btv); -bttv_call_all (btv, core, ioctl,SAA6588_CMD_READ , &cmd); +bttv_call_all (btv, core, command,SAA6588_CMD_READ , &cmd);return cmd.result; }
@@ -3281,7 +3281,7 @@ static __poll_t radio_poll(struct file *file, poll_table *wait) cmd.instance = file; cmd.event_list = wait; cmd.poll_mask = res; -bttv_call_all (btv, core, ioctl,SAA6588_CMD_POLL , &cmd); +bttv_call_all (btv, core, command,SAA6588_CMD_POLL , &cmd);return cmd.poll_mask; }
drivers/media/pci/saa7134/saa7134-video.c +3 -3
@@ -1181,7 +1181,7 @@ static int video_release(struct file *file)saa_call_all (dev, tuner, standby);if (vdev->vfl_type ==VFL_TYPE_RADIO ) -saa_call_all (dev, core, ioctl,SAA6588_CMD_CLOSE , &cmd); +saa_call_all (dev, core, command,SAA6588_CMD_CLOSE , &cmd);mutex_unlock (&dev->lock);return 0 ;
@@ -1200,7 +1200,7 @@ static ssize_t radio_read(struct file *file, char __user *data, cmd.result = -ENODEV ;mutex_lock (&dev->lock); -saa_call_all (dev, core, ioctl,SAA6588_CMD_READ , &cmd); +saa_call_all (dev, core, command,SAA6588_CMD_READ , &cmd);mutex_unlock (&dev->lock);return cmd.result;
@@ -1216,7 +1216,7 @@ static __poll_t radio_poll(struct file *file, poll_table *wait) cmd.event_list = wait; cmd.poll_mask =0 ;mutex_lock (&dev->lock); -saa_call_all (dev, core, ioctl,SAA6588_CMD_POLL , &cmd); +saa_call_all (dev, core, command,SAA6588_CMD_POLL , &cmd);mutex_unlock (&dev->lock);return rc | cmd.poll_mask;
drivers/media/platform/davinci/vpbe_display.c +1 -1
@@ -47,7 +47,7 @@ static int venc_is_second_field(struct vpbe_display *disp_dev) ret =v4l2_subdev_call (vpbe_dev->venc, core, - ioctl, + command,VENC_GET_FLD , &val);if (ret <0 ) {
drivers/media/platform/davinci/vpbe_venc.c +2 -4
@@ -521,9 +521,7 @@ static int venc_s_routing(struct v4l2_subdev *sd, u32 input, u32 output,return ret; } -static long venc_ioctl (struct v4l2_subdev *sd, -unsigned int cmd, -void *arg) +static long venc_command (struct v4l2_subdev *sd,unsigned int cmd,void *arg) {u32 val;
@@ -542,7 +540,7 @@ static long venc_ioctl(struct v4l2_subdev *sd, }static const struct v4l2_subdev_core_ops venc_core_ops = { - .ioctl = venc_ioctl, + .command = venc_command, };static const struct v4l2_subdev_video_ops venc_video_ops = {
include/media/v4l2-subdev.h +4 -0
@@ -162,6 +162,9 @@ struct v4l2_subdev_io_pin_config {* @s_gpio: set GPIO pins. Very simple right now, might need to be extended with * a direction argument if needed. * +* @command: called by in-kernel drivers in order to call functions internal +* to subdev drivers driver that have a separate callback. +* * @ioctl: called at the end of ioctl() syscall handler at the V4L2 core. * used to provide support for private ioctls used on the driver. *
@@ -193,6 +196,7 @@ struct v4l2_subdev_core_ops {int (*load_fw)(struct v4l2_subdev *sd);int (*reset)(struct v4l2_subdev *sd,u32 val);int (*s_gpio)(struct v4l2_subdev *sd,u32 val); +long (*command)(struct v4l2_subdev *sd,unsigned int cmd,void *arg);long (*ioctl)(struct v4l2_subdev *sd,unsigned int cmd,void *arg);#ifdef CONFIG_COMPAT long (*compat_ioctl32)(struct v4l2_subdev *sd,unsigned int cmd,void *arg);
Signature · 1 line
2.29.2