Skip to contentSkip to conversation

drm: check drm_format_info hsub and vsub to avoid divide by zero

PATCH In review George Kennedy 6 participants 15 messages Oct 28, 2021 – Dec 7, 2021

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

PATCH 1/1 drm: check drm_format_info hsub and vsub to avoid divide by zero

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
 drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
 drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
 drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
 drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +10 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
 	/* now let the driver pick its own format info */
 	info = drm_get_format_info(dev, r);
 
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < info->num_planes; i++) {
 		unsigned int width = fb_plane_width(r->width, info, i);
 		unsigned int height = fb_plane_height(r->height, info, i);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 43 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
 drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
 drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
 drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
 drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
 	/* now let the driver pick its own format info */
 	info = drm_get_format_info(dev, r);

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
 
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 6 lines
drivers/gpu/drm/drm_framebuffer.c +1 -0
+
 	for (i = 0; i < info->num_planes; i++) {
 		unsigned int width = fb_plane_width(r->width, info, i);
 		unsigned int height = fb_plane_height(r->height, info, i);
Signature · 1 line
1.8.3.1
Signature · 2 lines
Ville Syrjälä
Intel
#2 lore raw
Quoted 45 lines · Ville Syrjälä
Quoted 44 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
  drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
  drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
  drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
  drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
  vfs_ioctl fs/ioctl.c:51 [inline]
  __do_sys_ioctl fs/ioctl.c:874 [inline]
  __se_sys_ioctl fs/ioctl.c:860 [inline]
  __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
  do_syscall_x64 arch/x86/entry/common.c:50 [inline]
  do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
  entry_SYSCALL_64_after_hwframe+0x44/0xae
---
  drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
  	/* now let the driver pick its own format info */
  	info = drm_get_format_info(dev, r);
 
Quoted 12 lines

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 9 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Adding the authors and reviewer of the patch in question to CC.

94b292b277343190175d39172c903c0c5fb814f1 drm: drm_fourcc: add NV15, Q410, Q401 YUV formats

Asking if you have any input on how to deal with hsub and vsub = zero?

The sanity checks of hsub and vsub in the proposed patch are similar to other error checking done in framebuffer_check() preceding the proposed patch.

Thank you,
George

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/drm_framebuffer.c +1 -0
+
  	for (i = 0; i < info->num_planes; i++) {
  		unsigned int width = fb_plane_width(r->width, info, i);
  		unsigned int height = fb_plane_height(r->height, info, i);
Signature · 1 line
1.8.3.1
#3 lore raw

Hi,

Quoted 2 lines · George Kennedy

Asking if you have any input on how to deal with hsub and vsub = zero?

That's just a straight mistake on those formats - they should be 1. My bad for not spotting it in review.

On the one hand, having formats in this table is a nice machine-readable way to describe them. On the other, as drm_fourcc is being used as the canonical repository for formats, including ones not used in DRM, we can end up with situations like this. (R10/R12 being another example of formats not used in DRM:
[email protected])

Thanks,
-Brian

#4 lore raw
Quoted 7 lines · Brian Starkey

Hi,

Quoted 1 line · George Kennedy

Asking if you have any input on how to deal with hsub and vsub = zero?

That's just a straight mistake on those formats - they should be 1. My bad for not spotting it in review.

Quoted 6 lines

On the one hand, having formats in this table is a nice machine-readable way to describe them. On the other, as drm_fourcc is being used as the canonical repository for formats, including ones not used in DRM, we can end up with situations like this. (R10/R12 being another example of formats not used in DRM:
[email protected])

Wondering if there is an alternate fix to the one proposed?

Thank you,
George

Quoted 3 lines

Thanks,
-Brian

#5 lore raw
Quoted 10 lines · George Kennedy
Quoted 7 lines · Brian Starkey

Hi,

Quoted 1 line · George Kennedy

Asking if you have any input on how to deal with hsub and vsub = zero?

That's just a straight mistake on those formats - they should be 1. My bad for not spotting it in review.

Quoted 8 lines
Quoted 6 lines

On the one hand, having formats in this table is a nice machine-readable way to describe them. On the other, as drm_fourcc is being used as the canonical repository for formats, including ones not used in DRM, we can end up with situations like this. (R10/R12 being another example of formats not used in DRM:
[email protected])

Wondering if there is an alternate fix to the one proposed?

I think if the cost of defining formats correctly for everyone is that drm carries a bunch of nice machine-readable entries in its tables that it never uses, then we should do that. The very few bytes saved aren't worth any headaches (because on any soc system you anyway have tons more formats than what your driver is using).
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#6 lore raw
In reply to Ville Syrjälä
Quoted 45 lines · Ville Syrjälä
Quoted 44 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
 drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
 drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
 drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
 drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
 	/* now let the driver pick its own format info */
 	info = drm_get_format_info(dev, r);
 
Quoted 13 lines

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 9 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.
-Daniel

Quoted 11 lines
Quoted 7 lines
drivers/gpu/drm/drm_framebuffer.c +1 -0
+
 	for (i = 0; i < info->num_planes; i++) {
 		unsigned int width = fb_plane_width(r->width, info, i);
 		unsigned int height = fb_plane_height(r->height, info, i);
Signature · 1 line
1.8.3.1
Signature · 2 lines
Ville Syrjälä
Intel
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#7 lore raw
Quoted 60 lines · Daniel Vetter
Quoted 45 lines · Ville Syrjälä
Quoted 44 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
 drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
 drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
 drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
 drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
 	/* now let the driver pick its own format info */
 	info = drm_get_format_info(dev, r);
 
Quoted 13 lines

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 9 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Signature · 2 lines
Ville Syrjälä
Intel
#8 lore raw
Quoted 61 lines · Ville Syrjälä
Quoted 60 lines · Daniel Vetter
Quoted 58 lines · Ville Syrjälä
Quoted 43 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
 drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
 drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
 drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
 drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
 	/* now let the driver pick its own format info */
 	info = drm_get_format_info(dev, r);

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
 
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 8 lines
Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Python preprocess or so seems to be the usual answer, and that then just generates the C table after it's all checked.

Or a post-processor which fishes the table out from the .o (or just links against it).

But yeah doing this in cpp isn't going to work, aside from it'd be really ugly.
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#9 lore raw
Quoted 71 lines · Daniel Vetter
Quoted 61 lines · Ville Syrjälä
Quoted 60 lines · Daniel Vetter
Quoted 58 lines · Ville Syrjälä
Quoted 43 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
 drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
 drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
 drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
 drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:874 [inline]
 __se_sys_ioctl fs/ioctl.c:860 [inline]
 __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
 	/* now let the driver pick its own format info */
 	info = drm_get_format_info(dev, r);

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
 
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 8 lines
Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Quoted 8 lines

Python preprocess or so seems to be the usual answer, and that then just generates the C table after it's all checked.

Or a post-processor which fishes the table out from the .o (or just links against it).

But yeah doing this in cpp isn't going to work, aside from it'd be really ugly.

Kbuild does have support for hostprogs which are typically used in the build. The obvious idea is to use that for code generation, but it would also be interesting to see how that could be used for compile-time evaluation of sorts. Kind of like compile-time selftests? And, of course, how badly that would be frowned upon.

git grep says there are only four hostprogs users in drivers/, so it certainly isn't a popularity contest winner. (One of them is "mkregtable" in radeon.)

BR,
Jani.

Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
#10 lore raw
Quoted 82 lines · Jani Nikula
Quoted 67 lines · Daniel Vetter
Quoted 59 lines · Ville Syrjälä
Quoted 58 lines · Daniel Vetter
Quoted 57 lines · Ville Syrjälä
Quoted 43 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
  drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
  drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
  drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
  drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
  vfs_ioctl fs/ioctl.c:51 [inline]
  __do_sys_ioctl fs/ioctl.c:874 [inline]
  __se_sys_ioctl fs/ioctl.c:860 [inline]
  __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
  do_syscall_x64 arch/x86/entry/common.c:50 [inline]
  do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
  entry_SYSCALL_64_after_hwframe+0x44/0xae
---
  drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
  	/* now let the driver pick its own format info */
  	info = drm_get_format_info(dev, r);

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 206

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
 
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 7 lines
Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Quoted 8 lines

Python preprocess or so seems to be the usual answer, and that then just generates the C table after it's all checked.

Or a post-processor which fishes the table out from the .o (or just links against it).

But yeah doing this in cpp isn't going to work, aside from it'd be really ugly.

Kbuild does have support for hostprogs which are typically used in the build. The obvious idea is to use that for code generation, but it would also be interesting to see how that could be used for compile-time evaluation of sorts. Kind of like compile-time selftests? And, of course, how badly that would be frowned upon.

Quoted 3 lines

git grep says there are only four hostprogs users in drivers/, so it certainly isn't a popularity contest winner. (One of them is "mkregtable" in radeon.)

So, can someone suggest a fix? A cpp type of approach does not seem feasible.

Adding the sanity checks that are in the patch, which are similar to the sanity checks preceding them in framebuffer_check(), along with a self-test that ran through all the table entries, might address all the concerns brought up in this thread.

Thank you,
George

Quoted 5 lines

BR,
Jani.

#11 lore raw
Quoted 88 lines · George Kennedy
Quoted 81 lines · Jani Nikula
Quoted 66 lines · Daniel Vetter
Quoted 58 lines · Ville Syrjälä
Quoted 57 lines · Daniel Vetter
Quoted 56 lines · Ville Syrjälä
Quoted 42 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
  drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
  drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
  drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
  drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
  vfs_ioctl fs/ioctl.c:51 [inline]
  __do_sys_ioctl fs/ioctl.c:874 [inline]
  __se_sys_ioctl fs/ioctl.c:860 [inline]
  __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
  do_syscall_x64 arch/x86/entry/common.c:50 [inline]
  do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
  entry_SYSCALL_64_after_hwframe+0x44/0xae
---
  drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
  	/* now let the driver pick its own format info */

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 205

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
  	info = drm_get_format_info(dev, r);
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 7 lines
Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Quoted 8 lines

Python preprocess or so seems to be the usual answer, and that then just generates the C table after it's all checked.

Or a post-processor which fishes the table out from the .o (or just links against it).

But yeah doing this in cpp isn't going to work, aside from it'd be really ugly.

Kbuild does have support for hostprogs which are typically used in the build. The obvious idea is to use that for code generation, but it would also be interesting to see how that could be used for compile-time evaluation of sorts. Kind of like compile-time selftests? And, of course, how badly that would be frowned upon.

Quoted 3 lines

git grep says there are only four hostprogs users in drivers/, so it certainly isn't a popularity contest winner. (One of them is "mkregtable" in radeon.)

Quoted 7 lines

So, can someone suggest a fix? A cpp type of approach does not seem feasible.

Adding the sanity checks that are in the patch, which are similar to the sanity checks preceding them in framebuffer_check(), along with a self-test that ran through all the table entries, might address all the concerns brought up in this thread.

drm selftest sounds like a reasonable approach to me. -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#12 lore raw
Quoted 87 lines · Daniel Vetter
Quoted 86 lines · George Kennedy
Quoted 84 lines · Jani Nikula
Quoted 66 lines · Daniel Vetter
Quoted 58 lines · Ville Syrjälä
Quoted 57 lines · Daniel Vetter
Quoted 56 lines · Ville Syrjälä
Quoted 42 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
   drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
   drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
   drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
   drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
   vfs_ioctl fs/ioctl.c:51 [inline]
   __do_sys_ioctl fs/ioctl.c:874 [inline]
   __se_sys_ioctl fs/ioctl.c:860 [inline]
   __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
   do_syscall_x64 arch/x86/entry/common.c:50 [inline]
   do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
   entry_SYSCALL_64_after_hwframe+0x44/0xae
---
   drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
   1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
   	/* now let the driver pick its own format info */

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 205

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
   	info = drm_get_format_info(dev, r);
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 7 lines
Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Quoted 8 lines

Python preprocess or so seems to be the usual answer, and that then just generates the C table after it's all checked.

Or a post-processor which fishes the table out from the .o (or just links against it).

But yeah doing this in cpp isn't going to work, aside from it'd be really ugly.

Kbuild does have support for hostprogs which are typically used in the build. The obvious idea is to use that for code generation, but it would also be interesting to see how that could be used for compile-time evaluation of sorts. Kind of like compile-time selftests? And, of course, how badly that would be frowned upon.

git grep says there are only four hostprogs users in drivers/, so it certainly isn't a popularity contest winner. (One of them is "mkregtable" in radeon.)

Quoted 8 lines
Quoted 7 lines

So, can someone suggest a fix? A cpp type of approach does not seem feasible.

Adding the sanity checks that are in the patch, which are similar to the sanity checks preceding them in framebuffer_check(), along with a self-test that ran through all the table entries, might address all the concerns brought up in this thread.

drm selftest sounds like a reasonable approach to me.

In the meantime, should a bugzilla bug be opened to track the issue? From this thread it does not seem as though there is a drm selftest in the works.

Thanks,
George

Quoted 1 line

-Daniel

#13 lore raw
Quoted 93 lines · George Kennedy
Quoted 90 lines · Daniel Vetter
Quoted 89 lines · George Kennedy
Quoted 81 lines · Jani Nikula
Quoted 66 lines · Daniel Vetter
Quoted 58 lines · Ville Syrjälä
Quoted 57 lines · Daniel Vetter
Quoted 56 lines · Ville Syrjälä
Quoted 42 lines · George Kennedy

Do a sanity check on struct drm_format_info hsub and vsub values to avoid divide by zero.

Syzkaller reported a divide error in framebuffer_check() when the DRM_FORMAT_Q410 or DRM_FORMAT_Q401 pixel_format is passed in via the DRM_IOCTL_MODE_ADDFB2 ioctl. The drm_format_info struct for the DRM_FORMAT_Q410 pixel_pattern has ".hsub = 0" and ".vsub = 0". fb_plane_width() uses hsub as a divisor and fb_plane_height() uses vsub as a divisor. These divisors need to be sanity checked for zero before use.

divide error: 0000 [#1] SMP KASAN NOPTI

CPU: 0 PID: 14995 Comm: syz-executor709 Not tainted 5.15.0-rc6-syzk #1
Hardware name: Red Hat KVM, BIOS 1.13.0-2
RIP: 0010:framebuffer_check drivers/gpu/drm/drm_framebuffer.c:199 [inline]
RIP: 0010:drm_internal_framebuffer_create+0x604/0xf90

drivers/gpu/drm/drm_framebuffer.c:317

Call Trace:
   drm_mode_addfb2+0xdc/0x320 drivers/gpu/drm/drm_framebuffer.c:355
   drm_mode_addfb2_ioctl+0x2a/0x40 drivers/gpu/drm/drm_framebuffer.c:391
   drm_ioctl_kernel+0x23a/0x2e0 drivers/gpu/drm/drm_ioctl.c:795
   drm_ioctl+0x589/0xac0 drivers/gpu/drm/drm_ioctl.c:898
   vfs_ioctl fs/ioctl.c:51 [inline]
   __do_sys_ioctl fs/ioctl.c:874 [inline]
   __se_sys_ioctl fs/ioctl.c:860 [inline]
   __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
   do_syscall_x64 arch/x86/entry/common.c:50 [inline]
   do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
   entry_SYSCALL_64_after_hwframe+0x44/0xae
---
   drivers/gpu/drm/drm_framebuffer.c | 10 ++++++++++
   1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_framebuffer.c +0 -0
index 07f5abc..a146e4b 100644
@@ -195,6 +195,16 @@ static int framebuffer_check(struct drm_device *dev,
   	/* now let the driver pick its own format info */

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 205

Quoted 10 lines
drivers/gpu/drm/drm_framebuffer.c +9 -0
   	info = drm_get_format_info(dev, r);
+	if (info->hsub == 0) {
+		DRM_DEBUG_KMS("bad horizontal chroma subsampling factor %u\n", info->hsub);
+		return -EINVAL;
+	}
+
+	if (info->vsub == 0) {
+		DRM_DEBUG_KMS("bad vertical chroma subsampling factor %u\n", info->vsub);
+		return -EINVAL;
+	}

Looks like duct tape to me. I think we need to either fix those formats to have valid format info, or just revert the whole patch that added such broken things.

Quoted 7 lines
Quoted 2 lines

Yeah maybe even a compile-time check of the format table(s) to validate them properly and scream ... Or at least a selftest.

I really wish C had (even very limited) compile time evaluation so one could actually loop over arrays like at compile time to check each element. As it stands you either have to check each array element by hand, or you do some cpp macro horrors to pretend you're iterating the array.

Quoted 8 lines

Python preprocess or so seems to be the usual answer, and that then just generates the C table after it's all checked.

Or a post-processor which fishes the table out from the .o (or just links against it).

But yeah doing this in cpp isn't going to work, aside from it'd be really ugly.

Kbuild does have support for hostprogs which are typically used in the build. The obvious idea is to use that for code generation, but it would also be interesting to see how that could be used for compile-time evaluation of sorts. Kind of like compile-time selftests? And, of course, how badly that would be frowned upon.

Quoted 3 lines

git grep says there are only four hostprogs users in drivers/, so it certainly isn't a popularity contest winner. (One of them is "mkregtable" in radeon.)

So, can someone suggest a fix? A cpp type of approach does not seem feasible.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

Adding the sanity checks that are in the patch, which are similar to the sanity checks preceding them in framebuffer_check(), along with a self-test that ran through all the table entries, might address all the concerns brought up in this thread.

drm selftest sounds like a reasonable approach to me.

In the meantime, should a bugzilla bug be opened to track the issue? From this thread it does not seem as though there is a drm selftest in the works.

I think if you don't end up typing it, it's just not going to happen. We're not really doing a great job in tracking these kinds of tasks unfortunately :-/

Best we have is Documentation/gpu/todo.rst, but that's kinda high-overhead.
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch