[PATCH v2] drm/atomic-helpers: fix uaf in drm_atomic_helper_wait_for_vblanks

2024-02-15 Thread Edward Adam Davis
To briefly summarize the issues reported by syzbot, there are two task call 
stacks
as follows:

Task A Task B
   --
   drm_atomic_commit
   drm_atomic_helper_commit
commit_work
commit_tail
drm_atomic_helper_commit_tail  commit_tail 
   drm_atomic_helper_commit_tail
   drm_client_modeset_commit_atomic
   drm_atomic_state_default_clear
drm_atomic_helper_wait_for_vblanks

When two prerequisites are met simultaneously, the current issue will be 
triggered:
1. There is an overlap in the memory range occupied by the crtcs member set 
contained
   in the instance state of "struct drm_atomic_state" created by Task A and 
Task B
2. The context of drm_atomic_helper_commit_tail() has no lock protection, 
resulting
   in the instance state->crtcs sub item being released by other task

The solution is to add a lock in drm_atomic_helper_commit_tail() to ensure that
there is no other task interference when accessing the instance state.

Reported-and-tested-by: syzbot+0f999d26a4fd79c3a...@syzkaller.appspotmail.com
Signed-off-by: Edward Adam Davis 
---
 drivers/gpu/drm/drm_atomic_helper.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/gpu/drm/drm_atomic_helper.c 
b/drivers/gpu/drm/drm_atomic_helper.c
index 39ef0a6addeb..b16ff9020097 100644
--- a/drivers/gpu/drm/drm_atomic_helper.c
+++ b/drivers/gpu/drm/drm_atomic_helper.c
@@ -1743,7 +1743,9 @@ EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
 {
struct drm_device *dev = old_state->dev;
+   static DEFINE_MUTEX(lock);
 
+   mutex_lock();
drm_atomic_helper_commit_modeset_disables(dev, old_state);
 
drm_atomic_helper_commit_planes(dev, old_state, 0);
@@ -1757,6 +1759,7 @@ void drm_atomic_helper_commit_tail(struct 
drm_atomic_state *old_state)
drm_atomic_helper_wait_for_vblanks(dev, old_state);
 
drm_atomic_helper_cleanup_planes(dev, old_state);
+   mutex_unlock();
 }
 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
 
-- 
2.43.0



[PATCH] drm/atomic-helpers: fix uaf in drm_atomic_helper_wait_for_vblanks

2024-02-15 Thread Edward Adam Davis
To briefly summarize the issues reported by syzbot, there are two task call 
stacks
as follows:

Task A Task B
   --
drm_atomic_nonblocking_commit  drm_atomic_commit
drm_atomic_helper_commit   drm_atomic_helper_commit
commit_work
commit_tail
drm_atomic_helper_commit_tail  commit_tail 
   drm_atomic_helper_commit_tail
   drm_client_modeset_commit_atomic
   drm_atomic_state_default_clear
drm_atomic_helper_wait_for_vblanks

When two prerequisites are met simultaneously, the current issue will be 
triggered:
1. There is an overlap in the memory range occupied by the crtcs member set 
contained
   in the instance state of "struct drm_atomic_state" created by Task A and 
Task B
2. The context of drm_atomic_helper_commit_tail() has no lock protection, 
resulting
   in the instance state->crtcs sub item being released by other task

The solution is to add a lock in drm_atomic_helper_commit_tail() to ensure that
there is no other task interference when accessing the instance state.

Reported-and-tested-by: syzbot+0f999d26a4fd79c3a...@syzkaller.appspotmail.com
Signed-off-by: Edward Adam Davis 
---
 drivers/gpu/drm/drm_atomic_helper.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/gpu/drm/drm_atomic_helper.c 
b/drivers/gpu/drm/drm_atomic_helper.c
index 39ef0a6addeb..b16ff9020097 100644
--- a/drivers/gpu/drm/drm_atomic_helper.c
+++ b/drivers/gpu/drm/drm_atomic_helper.c
@@ -1743,7 +1743,9 @@ EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
 {
struct drm_device *dev = old_state->dev;
+   static DEFINE_MUTEX(lock);
 
+   mutex_lock();
drm_atomic_helper_commit_modeset_disables(dev, old_state);
 
drm_atomic_helper_commit_planes(dev, old_state, 0);
@@ -1757,6 +1759,7 @@ void drm_atomic_helper_commit_tail(struct 
drm_atomic_state *old_state)
drm_atomic_helper_wait_for_vblanks(dev, old_state);
 
drm_atomic_helper_cleanup_planes(dev, old_state);
+   mutex_unlock();
 }
 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
 
-- 
2.43.0



[PATCH V2] drm/modes: Fix divide error in drm_mode_debug_printmodeline

2023-11-20 Thread Edward Adam Davis
[Syz Log]
divide error:  [#1] PREEMPT SMP KASAN
CPU: 0 PID: 5068 Comm: syz-executor357 Not tainted 
6.6.0-syzkaller-16039-gac347a0655db #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 
10/09/2023
RIP: 0010:drm_mode_vrefresh drivers/gpu/drm/drm_modes.c:1303 [inline]
RIP: 0010:drm_mode_debug_printmodeline+0x118/0x4e0 
drivers/gpu/drm/drm_modes.c:60
Code: 00 41 0f b7 07 66 83 f8 02 b9 01 00 00 00 0f 43 c8 0f b7 c1 0f af e8 44 
89 f0 48 69 c8 e8 03 00 00 89 e8 d1 e8 48 01 c8 31 d2 <48> f7 f5 49 89 c6 eb 0c 
e8 fb 07 66 fc eb 05 e8 f4 07 66 fc 48 89
RSP: 0018:c9000391f8d0 EFLAGS: 00010246
RAX: 0001f400 RBX: 888025045000 RCX: 0001f400
RDX:  RSI: 8000 RDI: 888025045018
RBP:  R08: 8528b9af R09: 
R10: c9000391f8a0 R11: f52000723f17 R12: 0080
R13: dc00 R14: 0080 R15: 888025045016
FS:  56932380() GS:8880b980() knlGS:
CS:  0010 DS:  ES:  CR0: 80050033
CR2: 005fdeb8 CR3: 7fcff000 CR4: 003506f0
DR0:  DR1:  DR2: 
DR3:  DR6: fffe0ff0 DR7: 0400
Call Trace:
 
 drm_mode_setcrtc+0x83b/0x1880 drivers/gpu/drm/drm_crtc.c:794
 drm_ioctl_kernel+0x362/0x500 drivers/gpu/drm/drm_ioctl.c:792
 drm_ioctl+0x636/0xb00 drivers/gpu/drm/drm_ioctl.c:895
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:871 [inline]
 __se_sys_ioctl+0xf8/0x170 fs/ioctl.c:857
 do_syscall_x64 arch/x86/entry/common.c:51 [inline]
 do_syscall_64+0x44/0x110 arch/x86/entry/common.c:82
 entry_SYSCALL_64_after_hwframe+0x63/0x6b

[Analysis]
When calculating den in drm_mode_vrefresh(), if the vscan value is too large, 
there is a probability of unsigned integer overflow.

[Fix]
Before multiplying by vscan, first check if their product will overflow. 
If overflow occurs, return 0 and exit the subsequent process.

Reported-and-tested-by: syzbot+2e93e6fb36e6fdc56...@syzkaller.appspotmail.com
Fixes: ea40d7857d52 ("drm/vkms: fbdev emulation support")
Signed-off-by: Edward Adam Davis 
---
 drivers/gpu/drm/drm_modes.c | 7 +--
 1 file changed, 5 insertions(+), 2 deletion(-)

diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
index ac9a406250c5..60739d861da2 100644
--- a/drivers/gpu/drm/drm_modes.c
+++ b/drivers/gpu/drm/drm_modes.c
@@ -36,6 +36,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -1297,8 +1298,10 @@ int drm_mode_vrefresh(const struct drm_display_mode 
*mode)
num *= 2;
if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
den *= 2;
-   if (mode->vscan > 1)
-   den *= mode->vscan;
+   if (mode->vscan > 1) {
+   if (unlikely(check_mul_overflow(den, mode->vscan, )))
+   return 0;
+   }
 
return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(num, 1000), den);
 }
-- 
2.25.1



[PATCH] drm/modes: Fix divide error in drm_mode_debug_printmodeline

2023-11-18 Thread Edward Adam Davis
[Syz Log]
divide error:  [#1] PREEMPT SMP KASAN
CPU: 0 PID: 5068 Comm: syz-executor357 Not tainted 
6.6.0-syzkaller-16039-gac347a0655db #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 
10/09/2023
RIP: 0010:drm_mode_vrefresh drivers/gpu/drm/drm_modes.c:1303 [inline]
RIP: 0010:drm_mode_debug_printmodeline+0x118/0x4e0 
drivers/gpu/drm/drm_modes.c:60
Code: 00 41 0f b7 07 66 83 f8 02 b9 01 00 00 00 0f 43 c8 0f b7 c1 0f af e8 44 
89 f0 48 69 c8 e8 03 00 00 89 e8 d1 e8 48 01 c8 31 d2 <48> f7 f5 49 89 c6 eb 0c 
e8 fb 07 66 fc eb 05 e8 f4 07 66 fc 48 89
RSP: 0018:c9000391f8d0 EFLAGS: 00010246
RAX: 0001f400 RBX: 888025045000 RCX: 0001f400
RDX:  RSI: 8000 RDI: 888025045018
RBP:  R08: 8528b9af R09: 
R10: c9000391f8a0 R11: f52000723f17 R12: 0080
R13: dc00 R14: 0080 R15: 888025045016
FS:  56932380() GS:8880b980() knlGS:
CS:  0010 DS:  ES:  CR0: 80050033
CR2: 005fdeb8 CR3: 7fcff000 CR4: 003506f0
DR0:  DR1:  DR2: 
DR3:  DR6: fffe0ff0 DR7: 0400
Call Trace:
 
 drm_mode_setcrtc+0x83b/0x1880 drivers/gpu/drm/drm_crtc.c:794
 drm_ioctl_kernel+0x362/0x500 drivers/gpu/drm/drm_ioctl.c:792
 drm_ioctl+0x636/0xb00 drivers/gpu/drm/drm_ioctl.c:895
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:871 [inline]
 __se_sys_ioctl+0xf8/0x170 fs/ioctl.c:857
 do_syscall_x64 arch/x86/entry/common.c:51 [inline]
 do_syscall_64+0x44/0x110 arch/x86/entry/common.c:82
 entry_SYSCALL_64_after_hwframe+0x63/0x6b

[Analysis]
When calculating den in drm_mode_vrefresh(), if the vscan value is too large, 
there is a probability of unsigned integer overflow.

[Fix]
Before multiplying by vscan, first determine their ilog2. When their total 
exceeds 32, return -EINVAL and exit the subsequent calculation.

Reported-and-tested-by: syzbot+2e93e6fb36e6fdc56...@syzkaller.appspotmail.com
Fixes: ea40d7857d52 ("drm/vkms: fbdev emulation support")
Signed-off-by: Edward Adam Davis 
---
 drivers/gpu/drm/drm_modes.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
index ac9a406250c5..c7ec1ab041f8 100644
--- a/drivers/gpu/drm/drm_modes.c
+++ b/drivers/gpu/drm/drm_modes.c
@@ -36,6 +36,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -1297,8 +1298,11 @@ int drm_mode_vrefresh(const struct drm_display_mode 
*mode)
num *= 2;
if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
den *= 2;
-   if (mode->vscan > 1)
+   if (mode->vscan > 1) {
+   if (ilog2(den) + ilog2(mode->vscan) >= 32)
+   return -EINVAL;
den *= mode->vscan;
+   }
 
return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(num, 1000), den);
 }
-- 
2.25.1