Skip to contentSkip to conversation

dma-buf: dma-fence: Fix potential NULL pointer dereference

PATCH In review Philipp Stanner 6 participants 7 messages Jun 29

Subsystems (4)
lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

PATCH v2 1/1 dma-buf: dma-fence: Fix potential NULL pointer dereference

The commit mentioned in the fixes tag below introduced a mechanism through which fence producers can fully decouple from fence consumers. This, desirable, mechanism is based on the fence's signaled-bit as the "decoupling point".

A sophisticated interaction between RCU and atomic instructions attempts to ensure that fence consumers can still interact with fence producers through the dma_fence_ops (callback pointers into the producer).

This is the desired behavior: to check for decoupling, the signaled-bit is first checked. If it's not yet signaled, RCU ensures that the ops pointer cannot yet be NULL.

Hereby, dma_fence_signal_timestamp_locked() first sets the signaled-bit, and then sets the ops pointer to NULL. Readers first load the ops pointer, and then check through the signaled-bit whether the pointer can legally be accessed.

These set and load operations could occur out of order on weakly ordered platforms. This problem can be solved very elegantly by using the ops pointer itself as the synchronization point. The pointer is either NULL, or cannot become NULL while it is being used thanks to RCU.

Replace the signaled-bit check in dma_fence_timeline_name() and dma_fence_driver_name().

Fixes f4cc3ab824d6 ("dma-buf: protected fence ops by RCU v8")

---
Changes since v1:
- Use ops pointer instead of memory barriers. (Christian)
- Rephrase commit message.

---
 drivers/dma-buf/dma-fence.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +2 -2
index c7ea1e75d38a..0a025dfdf131 100644
@@ -1170,7 +1170,7 @@ const char __rcu *dma_fence_driver_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"detached-driver";
@@ -1203,7 +1203,7 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"signaled-timeline";

base-commit: cdeb2ccd993ed8647adbbda2c3b103aa717fd6f7

Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Replacing the signaled-bit check with an `ops` null-check causes driver callbacks to be executed…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Replacing the signaled-bit check with an ops null-check causes driver callbacks to be executed on signaled fences, violating the dma-fence contract and leading to a Use-After-Free. --

commit f3771b2534b5990287b2b373aaf5a26b9dea2923
Author: Philipp Stanner <[email protected]>

dma-buf: dma-fence: Fix potential NULL pointer dereference

The patch removes the signaled-bit check in dma_fence_timeline_name() and dma_fence_driver_name(), replacing it with a check for a non-NULL ops pointer to prevent potential out-of-order execution issues.

drivers/dma-buf/dma-fence.c const char __rcu *dma_fence_driver_name(struct dma_fence *fence) · line 1173

Quoted 10 lines
drivers/dma-buf/dma-fence.c +1 -1
index c7ea1e75d38a2..0a025dfdf131e 100644
@@ -1170,7 +1170,7 @@ const char __rcu *dma_fence_driver_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)

[Severity: High]
Does this expose a use-after-free for signaled fences?

If a fence has a .release or .wait callback, dma_fence_signal_timestamp_locked() does not set fence->ops to NULL. For those fences, ops will remain non-NULL indefinitely even after they are signaled.

By removing the signaled-bit check, won't ops->get_driver_name(fence) be called on signaled fences? The dma-fence contract states that backing data can be freed after an RCU grace period once the fence is signaled, so calling this callback could access freed memory.

drivers/dma-buf/dma-fence.c const char __rcu *dma_fence_timeline_name(struct dma_fence *fence) · line 1206

Quoted 9 lines
drivers/dma-buf/dma-fence.c +1 -1
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"detached-driver";
@@ -1203,7 +1203,7 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)

[Severity: High]
Could this lead to the same use-after-free as above?

Since ops can remain non-NULL for signaled fences that provide a .release or .wait callback, this allows the ops callback to be executed after the fence is signaled and its backing data is potentially freed.

Quoted 3 lines
drivers/dma-buf/dma-fence.c +0 -0
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"signaled-timeline";
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Philipp Stanner
Quoted 23 lines · On Mon, 29 Jun 2026 09:56:37 +0200 Philipp Stanner <[email protected]> wrote:

The commit mentioned in the fixes tag below introduced a mechanism through which fence producers can fully decouple from fence consumers. This, desirable, mechanism is based on the fence's signaled-bit as the "decoupling point".

A sophisticated interaction between RCU and atomic instructions attempts to ensure that fence consumers can still interact with fence producers through the dma_fence_ops (callback pointers into the producer).

This is the desired behavior: to check for decoupling, the signaled-bit is first checked. If it's not yet signaled, RCU ensures that the ops pointer cannot yet be NULL.

Hereby, dma_fence_signal_timestamp_locked() first sets the signaled-bit, and then sets the ops pointer to NULL. Readers first load the ops pointer, and then check through the signaled-bit whether the pointer can legally be accessed.

These set and load operations could occur out of order on weakly ordered platforms. This problem can be solved very elegantly by using the ops pointer itself as the synchronization point. The pointer is either NULL, or cannot become NULL while it is being used thanks to RCU.

Quoted 6 lines

Replace the signaled-bit check in dma_fence_timeline_name() and dma_fence_driver_name().

Fixes f4cc3ab824d6 ("dma-buf: protected fence ops by RCU v8")
Quoted 32 lines

---
Changes since v1:
- Use ops pointer instead of memory barriers. (Christian)
- Rephrase commit message.

---
 drivers/dma-buf/dma-fence.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +2 -2
index c7ea1e75d38a..0a025dfdf131 100644
@@ -1170,7 +1170,7 @@ const char __rcu *dma_fence_driver_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"detached-driver";
@@ -1203,7 +1203,7 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"signaled-timeline";

base-commit: cdeb2ccd993ed8647adbbda2c3b103aa717fd6f7

maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to Philipp Stanner
Quoted 23 lines · Philipp Stanner

The commit mentioned in the fixes tag below introduced a mechanism through which fence producers can fully decouple from fence consumers. This, desirable, mechanism is based on the fence's signaled-bit as the "decoupling point".

A sophisticated interaction between RCU and atomic instructions attempts to ensure that fence consumers can still interact with fence producers through the dma_fence_ops (callback pointers into the producer).

This is the desired behavior: to check for decoupling, the signaled-bit is first checked. If it's not yet signaled, RCU ensures that the ops pointer cannot yet be NULL.

Hereby, dma_fence_signal_timestamp_locked() first sets the signaled-bit, and then sets the ops pointer to NULL. Readers first load the ops pointer, and then check through the signaled-bit whether the pointer can legally be accessed.

These set and load operations could occur out of order on weakly ordered platforms. This problem can be solved very elegantly by using the ops pointer itself as the synchronization point. The pointer is either NULL, or cannot become NULL while it is being used thanks to RCU.

Quoted 6 lines

Replace the signaled-bit check in dma_fence_timeline_name() and dma_fence_driver_name().

Fixes f4cc3ab824d6 ("dma-buf: protected fence ops by RCU v8")
Quoted 32 lines

---
Changes since v1:
- Use ops pointer instead of memory barriers. (Christian)
- Rephrase commit message.

---
 drivers/dma-buf/dma-fence.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +2 -2
index c7ea1e75d38a..0a025dfdf131 100644
@@ -1170,7 +1170,7 @@ const char __rcu *dma_fence_driver_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"detached-driver";
@@ -1203,7 +1203,7 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (!dma_fence_test_signaled_flag(fence))
+	if (ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"signaled-timeline";

base-commit: cdeb2ccd993ed8647adbbda2c3b103aa717fd6f7

maintainer
#5 lore raw
Quoted 9 lines · Philipp Stanner
Quoted 6 lines · Christian König
Quoted 3 lines · Philipp Stanner
Fixes f4cc3ab824d6 ("dma-buf: protected fence ops by RCU v8")

As the maintainer you push that one, don't you Christian?

Already done so, but you based the patch on the wrong branch and I had to manually rebase it to drm-misc-fixes.

Christian.

Quoted 3 lines

Regards
P.

Reviewed-by on v2 1/1
#6 lore raw
In reply to Philipp Stanner
Quoted 23 lines · Philipp Stanner

The commit mentioned in the fixes tag below introduced a mechanism through which fence producers can fully decouple from fence consumers. This, desirable, mechanism is based on the fence's signaled-bit as the "decoupling point".

A sophisticated interaction between RCU and atomic instructions attempts to ensure that fence consumers can still interact with fence producers through the dma_fence_ops (callback pointers into the producer).

This is the desired behavior: to check for decoupling, the signaled-bit is first checked. If it's not yet signaled, RCU ensures that the ops pointer cannot yet be NULL.

Hereby, dma_fence_signal_timestamp_locked() first sets the signaled-bit, and then sets the ops pointer to NULL. Readers first load the ops pointer, and then check through the signaled-bit whether the pointer can legally be accessed.

These set and load operations could occur out of order on weakly ordered platforms. This problem can be solved very elegantly by using the ops pointer itself as the synchronization point. The pointer is either NULL, or cannot become NULL while it is being used thanks to RCU.

Quoted 6 lines

Replace the signaled-bit check in dma_fence_timeline_name() and dma_fence_driver_name().

Fixes f4cc3ab824d6 ("dma-buf: protected fence ops by RCU v8")