Skip to contentSkip to conversation

drm/xe: Fix possible UAF in guc_exec_queue_process_msg

PATCH Superseded Matthew Brost 4 participants 9 messages Jul 23, 2024 – Jul 25, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Matthew Brost

author maintainer
#0 lore raw

PATCH v2 1/1 drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

---
 drivers/gpu/drm/xe/xe_guc_submit.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_guc_submit.c +3 -1
index da2ead86b9ae..b8f938539a90 100644
@@ -1395,6 +1395,8 @@ static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
 
 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
 {
+	struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data));
+
 	trace_xe_sched_msg_recv(msg);
 
 	switch (msg->opcode) {
@@ -1414,7 +1416,7 @@ static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
 		XE_WARN_ON("Unknown message type");
 	}
 
-	xe_pm_runtime_put(guc_to_xe(exec_queue_to_guc(msg->private_data)));
+	xe_pm_runtime_put(xe);
 }
 
 static const struct drm_sched_backend_ops drm_sched_ops = {
Signature · 1 line
2.34.1
#1 lore raw

✓ CI.Patch_applied: success for drm/xe: Fix possible UAF in guc_exec_queue_process_msg (rev2)

== Series Details ==

== Series Details ==

Series: drm/xe: Fix possible UAF in guc_exec_queue_process_msg (rev2)
URL : https://patchwork.freedesktop.org/series/136397/
State : success

== Summary ==

=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: 8a52c2cc81b4 drm-tip: 2024y-07m-23d-20h-01m-00s UTC integration manifest === git am output follows ===
Applying: drm/xe: Fix possible UAF in guc_exec_queue_process_msg

#2 lore raw

✗ CI.checkpatch: warning for drm/xe: Fix possible UAF in guc_exec_queue_process_msg (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/xe: Fix possible UAF in guc_exec_queue_process_msg (rev2)
URL : https://patchwork.freedesktop.org/series/136397/
State : warning

== Summary ==

+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master 5ce3e132caaa5b45e5e50201b574a097d130967c
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit a3c7bb198ddd661a528e739b6416610ad982edc8

Author: Matthew Brost <[email protected]>
Date:   Tue Jul 23 12:19:03 2024 -0700

drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

+ /mt/dim checkpatch 8a52c2cc81b4c9cfee85520ec8a9f29cfc319e7e drm-intel
a3c7bb198ddd drm/xe: Fix possible UAF in guc_exec_queue_process_msg
-:12: WARNING:BAD_REPORTED_BY_LINK: Reported-by: should be immediately followed by Closes: with a URL to the report #12:

total: 0 errors, 1 warnings, 0 checks, 16 lines checked

#3 lore raw

✗ CI.KUnit: failure for drm/xe: Fix possible UAF in guc_exec_queue_process_msg (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/xe: Fix possible UAF in guc_exec_queue_process_msg (rev2)
URL : https://patchwork.freedesktop.org/series/136397/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:../drivers/gpu/drm/xe/xe_device.c: In function ‘xe_file_close’:
../drivers/gpu/drm/xe/xe_device.c:158:20: error: ‘xe’ undeclared (first use in this function); did you mean ‘xef’?

  158 |  xe_pm_runtime_get(xe);
      |                    ^~
      |                    xef

../drivers/gpu/drm/xe/xe_device.c:158:20: note: each undeclared identifier is reported only once for each function it appears in make[7]: *** [../scripts/Makefile.build:244: drivers/gpu/drm/xe/xe_device.o] Error 1
make[7]: *** Waiting for unfinished jobs....
make[6]: *** [../scripts/Makefile.build:485: drivers/gpu/drm/xe] Error 2
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [../scripts/Makefile.build:485: drivers/gpu/drm] Error 2
make[4]: *** [../scripts/Makefile.build:485: drivers/gpu] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [../scripts/Makefile.build:485: drivers] Error 2
make[3]: *** Waiting for unfinished jobs....
../lib/iomap.c:156:5: warning: no previous prototype for ‘ioread64_lo_hi’ [-Wmissing-prototypes]
156 | u64 ioread64_lo_hi(const void __iomem *addr)

      |     ^~~~~~~~~~~~~~
../lib/iomap.c:163:5: warning: no previous prototype for ‘ioread64_hi_lo’ [-Wmissing-prototypes]
  163 | u64 ioread64_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~
../lib/iomap.c:170:5: warning: no previous prototype for ‘ioread64be_lo_hi’ [-Wmissing-prototypes]
  170 | u64 ioread64be_lo_hi(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:178:5: warning: no previous prototype for ‘ioread64be_hi_lo’ [-Wmissing-prototypes]
  178 | u64 ioread64be_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:264:6: warning: no previous prototype for ‘iowrite64_lo_hi’ [-Wmissing-prototypes]
  264 | void iowrite64_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:272:6: warning: no previous prototype for ‘iowrite64_hi_lo’ [-Wmissing-prototypes]
  272 | void iowrite64_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:280:6: warning: no previous prototype for ‘iowrite64be_lo_hi’ [-Wmissing-prototypes]
  280 | void iowrite64be_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
../lib/iomap.c:288:6: warning: no previous prototype for ‘iowrite64be_hi_lo’ [-Wmissing-prototypes]
  288 | void iowrite64be_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~

make[2]: *** [/kernel/Makefile:1934: .] Error 2 make[1]: *** [/kernel/Makefile:240: __sub-make] Error 2
make: *** [Makefile:240: __sub-make] Error 2

[20:16:14] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[20:16:18] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make ARCH=um O=.kunit --jobs=48
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel
#4 lore raw
In reply to Matthew Brost
Quoted 32 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Wednesday, July 24, 2024 12:49 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH v2] drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

---
 drivers/gpu/drm/xe/xe_guc_submit.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_guc_submit.c +0 -0
index da2ead86b9ae..b8f938539a90 100644

drivers/gpu/drm/xe/xe_guc_submit.c static void · line 1399

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
@@ -1395,6 +1395,8 @@ static void
 __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
 
 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)  {
+	struct xe_device *xe =
+guc_to_xe(exec_queue_to_guc(msg->private_data));

If msg freed at this point, don't you need to protect against NULL, just in case?

Thanks,
Tejas

Quoted 17 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
+
 	trace_xe_sched_msg_recv(msg);
 
 	switch (msg->opcode) {
@@ -1414,7 +1416,7 @@ static void guc_exec_queue_process_msg(struct
 xe_sched_msg *msg)
 		XE_WARN_ON("Unknown message type");
 	}
 
-	xe_pm_runtime_put(guc_to_xe(exec_queue_to_guc(msg-
 >private_data)));
+	xe_pm_runtime_put(xe);
 }

static const struct drm_sched_backend_ops drm_sched_ops = {
--
2.34.1

author maintainer
#5 lore raw
Quoted 34 lines · Tejas
Quoted 34 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Wednesday, July 24, 2024 12:49 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH v2] drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

---
 drivers/gpu/drm/xe/xe_guc_submit.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_guc_submit.c +0 -0
index da2ead86b9ae..b8f938539a90 100644
@@ -1395,6 +1395,8 @@ static void
 __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
 
Quoted 6 lines

drivers/gpu/drm/xe/xe_guc_submit.c static void · line 1399

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)  {
+	struct xe_device *xe =
+guc_to_xe(exec_queue_to_guc(msg->private_data));

If msg freed at this point, don't you need to protect against NULL, just in case?

The msg is valid here, it can be freed in the below swicth statement hence the bug in referencing it after the switch statement.

Matt

Quoted 20 lines

Thanks,
Tejas

Quoted 17 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
+
 	trace_xe_sched_msg_recv(msg);
 
 	switch (msg->opcode) {
@@ -1414,7 +1416,7 @@ static void guc_exec_queue_process_msg(struct
 xe_sched_msg *msg)
 		XE_WARN_ON("Unknown message type");
 	}
 
-	xe_pm_runtime_put(guc_to_xe(exec_queue_to_guc(msg-
 >private_data)));
+	xe_pm_runtime_put(xe);
 }

static const struct drm_sched_backend_ops drm_sched_ops = {
--
2.34.1

#6 lore raw
Quoted 51 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Wednesday, July 24, 2024 9:14 PM
To: Upadhyay, Tejas <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2] drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Quoted 35 lines · Tejas
Quoted 35 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Wednesday, July 24, 2024 12:49 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH v2] drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

Closes:
https://lore.kernel.org/r/[email protected]/

Fixes d930c19fdff3 ("drm/xe: Build PM into GuC CT layer")
---
 drivers/gpu/drm/xe/xe_guc_submit.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_guc_submit.c +0 -0
index da2ead86b9ae..b8f938539a90 100644
@@ -1395,6 +1395,8 @@ static void
 __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
 
Quoted 5 lines

drivers/gpu/drm/xe/xe_guc_submit.c static void · line 1399

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)  {
+	struct xe_device *xe =
+guc_to_xe(exec_queue_to_guc(msg->private_data));

If msg freed at this point, don't you need to protect against NULL, just in

Quoted 5 lines

case?

Quoted 1 line

The msg is valid here, it can be freed in the below swicth statement hence the bug in referencing it after the switch statement.

Ok, LGTM,

Tejas

Quoted 23 lines

Matt

Quoted 20 lines

Thanks,
Tejas

Quoted 17 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
+
 	trace_xe_sched_msg_recv(msg);
 
 	switch (msg->opcode) {
@@ -1414,7 +1416,7 @@ static void guc_exec_queue_process_msg(struct
 xe_sched_msg *msg)
 		XE_WARN_ON("Unknown message type");
 	}
 
-	xe_pm_runtime_put(guc_to_xe(exec_queue_to_guc(msg-
 >private_data)));
+	xe_pm_runtime_put(xe);
 }

static const struct drm_sched_backend_ops drm_sched_ops = {
--
2.34.1

#7 lore raw
In reply to Upadhyay, Tejas
Quoted 34 lines · Tejas
Quoted 34 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Wednesday, July 24, 2024 12:49 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH v2] drm/xe: Fix possible UAF in guc_exec_queue_process_msg

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

---
  drivers/gpu/drm/xe/xe_guc_submit.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_guc_submit.c +0 -0
index da2ead86b9ae..b8f938539a90 100644
@@ -1395,6 +1395,8 @@ static void
 __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
 
Quoted 8 lines

drivers/gpu/drm/xe/xe_guc_submit.c static void · line 1399

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
  static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)  {
+	struct xe_device *xe =
+guc_to_xe(exec_queue_to_guc(msg->private_data));

If msg freed at this point, don't you need to protect against NULL, just in case?

Thanks,
Tejas

xe_sched_process_msg_work the caller of the ops ensures the routine is called only if msg is valid.

Below msg->opcode for eg like CLEANUP can clean it uo.

Quoted 18 lines
Quoted 17 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
+
  	trace_xe_sched_msg_recv(msg);
 
  	switch (msg->opcode) {
@@ -1414,7 +1416,7 @@ static void guc_exec_queue_process_msg(struct
 xe_sched_msg *msg)
  		XE_WARN_ON("Unknown message type");
  	}
 
-	xe_pm_runtime_put(guc_to_xe(exec_queue_to_guc(msg-
 > private_data)));
+	xe_pm_runtime_put(xe);
  }

static const struct drm_sched_backend_ops drm_sched_ops = {
--
2.34.1

Reviewed-by on v2 1/1
#8 lore raw
In reply to Matthew Brost
Quoted 29 lines · Matthew Brost

Store xe_device ahead of processing message as message can be free'd in some cases.

v2:
- Including missing local changes

---
  drivers/gpu/drm/xe/xe_guc_submit.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
index da2ead86b9ae..b8f938539a90 100644
@@ -1395,6 +1395,8 @@ static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
 
  static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
  {
+	struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data));
+
  	trace_xe_sched_msg_recv(msg);
 
  	switch (msg->opcode) {

drivers/gpu/drm/xe/xe_guc_submit.c static void guc_exec_queue_process_msg(struct xe_sched_msg *msg) · line 1419

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
@@ -1414,7 +1416,7 @@ static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
  		XE_WARN_ON("Unknown message type");
  	}
 
-	xe_pm_runtime_put(guc_to_xe(exec_queue_to_guc(msg->private_data)));
+	xe_pm_runtime_put(xe);

Patch LGTM.

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +0 -0
  }
 
  static const struct drm_sched_backend_ops drm_sched_ops = {