Skip to contentSkip to conversation

drm/exynos: fix race condition UAF in exynos_g2d_exec_ioctl

PATCH Superseded Min Li 3 participants 10 messages May 27, 2023 – Jun 2, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Min Li

author
#0 lore raw

PATCH 1/1 drm/exynos: fix race condition UAF in exynos_g2d_exec_ioctl

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
index ec784e58da5c..414e585ec7dd 100644
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
 	/* Let the runqueue know that there is work to do. */
 	queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-	if (runqueue_node->async)
+	if (req->async)
 		goto out;
 
 	wait_for_completion(&runqueue_node->complete);
Signature · 1 line
2.34.1
#1 lore raw

Hi Min,

Quoted 14 lines · Min Li

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
index ec784e58da5c..414e585ec7dd 100644

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data, · line 1338

Quoted 5 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
 	/* Let the runqueue know that there is work to do. */
 	queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-	if (runqueue_node->async)
+	if (req->async)

did you actually hit this? If you did, then the fix is not OK.

Andi

Quoted 6 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
 		goto out;
 
 	wait_for_completion(&runqueue_node->complete);
Signature · 1 line
2.34.1
author
#2 lore raw

Hi Andi,

Quoted 21 lines · Andi Shyti

Hi Min,

Quoted 17 lines · Min Li

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
index ec784e58da5c..414e585ec7dd 100644
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
Quoted 4 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data, · line 1338

Quoted 2 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

No, I didn't actually hit this. I found it through code review. This is only a theoretical issue that can only be triggered in extreme cases.

Quoted 9 lines

Andi

Quoted 6 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -1
              goto out;
 
      wait_for_completion(&runqueue_node->complete);
--

2.34.1

Signature · 1 line
Min Li
#3 lore raw

Hi Min,

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
index ec784e58da5c..414e585ec7dd 100644
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
Quoted 8 lines
Quoted 4 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data, · line 1338

Quoted 2 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

No, I didn't actually hit this. I found it through code review. This is only a theoretical issue that can only be triggered in extreme cases.

first of all runqueue is used again two lines below this, which means that if you don't hit the uaf here you will hit it immediately after.

Second, if runqueue is freed, than we need to remove the part where it's freed because it doesn't make sense to free runqueue at this stage.

Finally, can you elaborate on the code review that you did so that we all understand it?

Andi

author
#4 lore raw

Hi Andi,

Quoted 25 lines · Andi Shyti

Hi Min,

Quoted 22 lines
Quoted 21 lines
Quoted 14 lines

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
index ec784e58da5c..414e585ec7dd 100644

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data, · line 1338

Quoted 5 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

Quoted 7 lines
Quoted 3 lines

No, I didn't actually hit this. I found it through code review. This is only a theoretical issue that can only be triggered in extreme cases.

first of all runqueue is used again two lines below this, which means that if you don't hit the uaf here you will hit it immediately after.

No, if async is true, then it will goto out, which will directly return.

if (runqueue_node->async)
goto out; // here, go to out, will directly return

wait_for_completion(&runqueue_node->complete); // not hit
g2d_free_runqueue_node(g2d, runqueue_node);

out:
return 0;

Quoted 4 lines

Second, if runqueue is freed, than we need to remove the part where it's freed because it doesn't make sense to free runqueue at this stage.

It is freed by g2d_free_runqueue_node in g2d_runqueue_worker

static void g2d_runqueue_worker(struct work_struct *work)

{
    ......
    if (runqueue_node) {
        pm_runtime_mark_last_busy(g2d->dev);
        pm_runtime_put_autosuspend(g2d->dev);

        complete(&runqueue_node->complete);
        if (runqueue_node->async)
            g2d_free_runqueue_node(g2d, runqueue_node);        // freed here
    }
Quoted 3 lines

Finally, can you elaborate on the code review that you did so that we all understand it?

queue_work(g2d->g2d_workq, &g2d->runqueue_work);
msleep(100); // add sleep here to let g2d_runqueue_worker run first
if (runqueue_node->async)

    goto out;
Quoted 2 lines

Andi

--
Min Li

Reviewed-by
#5 lore raw

Hi Min,

Quoted 38 lines · On Wed, May 31, 2023 at 06:54:34PM +0800, lm0963 wrote:

Hi Andi,

Quoted 25 lines · Andi Shyti

Hi Min,

Quoted 22 lines
Quoted 21 lines
Quoted 14 lines

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
index ec784e58da5c..414e585ec7dd 100644

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data, · line 1338

Quoted 5 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

Quoted 7 lines
Quoted 3 lines

No, I didn't actually hit this. I found it through code review. This is only a theoretical issue that can only be triggered in extreme cases.

first of all runqueue is used again two lines below this, which means that if you don't hit the uaf here you will hit it immediately after.

No, if async is true, then it will goto out, which will directly return.

Quoted 8 lines

if (runqueue_node->async)
goto out; // here, go to out, will directly return

wait_for_completion(&runqueue_node->complete); // not hit
g2d_free_runqueue_node(g2d, runqueue_node);

out:
return 0;

that's right, sorry, I misread it.

Quoted 7 lines
Quoted 3 lines

Second, if runqueue is freed, than we need to remove the part where it's freed because it doesn't make sense to free runqueue at this stage.

It is freed by g2d_free_runqueue_node in g2d_runqueue_worker

static void g2d_runqueue_worker(struct work_struct *work)

Quoted 9 lines
{
    ......
    if (runqueue_node) {
        pm_runtime_mark_last_busy(g2d->dev);
        pm_runtime_put_autosuspend(g2d->dev);

        complete(&runqueue_node->complete);
        if (runqueue_node->async)
            g2d_free_runqueue_node(g2d, runqueue_node);        // freed here

this is what I'm wondering: is it correct to free a resource here? The design looks to me a bit fragile and prone to mistakes.

The patch per se is OK. It doesn't make much difference to me where you actually read async, although this patch looks a bit safer:

However some refactoring might be needed to make it a bit more robust.

Thanks,
Andi

Quoted 19 lines
    }
Quoted 3 lines

Finally, can you elaborate on the code review that you did so that we all understand it?

queue_work(g2d->g2d_workq, &g2d->runqueue_work);
msleep(100); // add sleep here to let g2d_runqueue_worker run first
if (runqueue_node->async)

    goto out;
Quoted 2 lines

Andi

--
Min Li

#6 lore raw

Hi Andi,

Quoted 82 lines

-----Original Message-----
From: Andi Shyti <[email protected]>
Sent: Wednesday, May 31, 2023 9:06 PM
To: lm0963 <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dri- [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] drm/exynos: fix race condition UAF in exynos_g2d_exec_ioctl

Hi Min,

Quoted 7 lines · On Wed, May 31, 2023 at 06:54:34PM +0800, lm0963 wrote:

Hi Andi,

Quoted 4 lines · Andi Shyti

Hi Min,

Quoted 1 line
Quoted 1 line
Quoted 1 line

If it is async, runqueue_node is freed in g2d_runqueue_worker on

another

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

worker thread. So in extreme cases, if g2d_runqueue_worker runs

first, and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

then executes the following if statement, there will be use-

after-free.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines
---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/exynos/exynos_drm_g2d.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct

Quoted 4 lines

index ec784e58da5c..414e585ec7dd 100644

drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct

drm_device *drm_dev, void *data,

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data, · line 1338

Quoted 5 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

Quoted 7 lines
Quoted 3 lines

No, I didn't actually hit this. I found it through code review. This is only a theoretical issue that can only be triggered in extreme cases.

first of all runqueue is used again two lines below this, which means that if you don't hit the uaf here you will hit it immediately after.

No, if async is true, then it will goto out, which will directly return.

Quoted 8 lines

if (runqueue_node->async)
goto out; // here, go to out, will directly return

wait_for_completion(&runqueue_node->complete); // not hit
g2d_free_runqueue_node(g2d, runqueue_node);

out:
return 0;

that's right, sorry, I misread it.

Quoted 13 lines
Quoted 3 lines

Second, if runqueue is freed, than we need to remove the part where it's freed because it doesn't make sense to free runqueue at this stage.

It is freed by g2d_free_runqueue_node in g2d_runqueue_worker

static void g2d_runqueue_worker(struct work_struct *work)

{
    ......
    if (runqueue_node) {
        pm_runtime_mark_last_busy(g2d->dev);
        pm_runtime_put_autosuspend(g2d->dev);
Quoted 6 lines
Quoted 3 lines
        complete(&runqueue_node->complete);
        if (runqueue_node->async)
            g2d_free_runqueue_node(g2d, runqueue_node);        // freed here

this is what I'm wondering: is it correct to free a resource here? The design looks to me a bit fragile and prone to mistakes.

This question seems to deviate from the purpose of this patch. If you are providing additional opinions for code quality improvement unrelated to this patch, it would be more appropriate for me to answer instead of him.

The runqueue node - which contains command list for g2d rendering - is generated when the user calls the ioctl system call. Therefore, if the user-requested command list is rendered by g2d device then there is no longer a reason to keep it. :)

Quoted 6 lines

The patch per se is OK. It doesn't make much difference to me where you actually read async, although this patch looks a bit safer:

Thanks,
Inki Dae

Quoted 26 lines

However some refactoring might be needed to make it a bit more robust.

Thanks,
Andi

Quoted 19 lines
    }
Quoted 3 lines

Finally, can you elaborate on the code review that you did so that we all understand it?

queue_work(g2d->g2d_workq, &g2d->runqueue_work);
msleep(100); // add sleep here to let g2d_runqueue_worker run first
if (runqueue_node->async)

    goto out;
Quoted 2 lines

Andi

--
Min Li

#7 lore raw

Hi Inki,

Quoted 61 lines
Quoted 61 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

If it is async, runqueue_node is freed in g2d_runqueue_worker on

another

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

worker thread. So in extreme cases, if g2d_runqueue_worker runs

first, and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

then executes the following if statement, there will be use-

after-free.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines
---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/exynos/exynos_drm_g2d.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct

Quoted 4 lines

index ec784e58da5c..414e585ec7dd 100644

drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct

drm_device *drm_dev, void *data,

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct · line 1338

Quoted 5 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

Quoted 7 lines
Quoted 3 lines

No, I didn't actually hit this. I found it through code review. This is only a theoretical issue that can only be triggered in extreme cases.

first of all runqueue is used again two lines below this, which means that if you don't hit the uaf here you will hit it immediately after.

No, if async is true, then it will goto out, which will directly return.

Quoted 8 lines

if (runqueue_node->async)
goto out; // here, go to out, will directly return

wait_for_completion(&runqueue_node->complete); // not hit
g2d_free_runqueue_node(g2d, runqueue_node);

out:
return 0;

that's right, sorry, I misread it.

Quoted 13 lines
Quoted 3 lines

Second, if runqueue is freed, than we need to remove the part where it's freed because it doesn't make sense to free runqueue at this stage.

It is freed by g2d_free_runqueue_node in g2d_runqueue_worker

static void g2d_runqueue_worker(struct work_struct *work)

{
    ......
    if (runqueue_node) {
        pm_runtime_mark_last_busy(g2d->dev);
        pm_runtime_put_autosuspend(g2d->dev);
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
        complete(&runqueue_node->complete);
        if (runqueue_node->async)
            g2d_free_runqueue_node(g2d, runqueue_node);        // freed here

this is what I'm wondering: is it correct to free a resource here? The design looks to me a bit fragile and prone to mistakes.

This question seems to deviate from the purpose of this patch. If you are providing additional opinions for code quality improvement unrelated to this patch, it would be more appropriate for me to answer instead of him.

It's not deviating as the question was already made in my first review. It just looks strange to me that a piece of data shared amongst processes can be freed up without sinchronizing. A bunch of if's do not make it robust enough.

The patch itself, in my point of view, is not really fixing much and won't make any difference, it's just exposing the weakness I mentioned.

However, honestly speaking, I don't know the driver well enough to suggest architectural changes and that's why I r-b'ed this one. But the first thing that comes to my mind, without looking much at the code, is using kref's as a way to make sure that a resource doesn't magically disappear under your nose.

But, of course, this is up to you and if in your opinion this is OK and it fixes it... then you definitely know better :)

Thanks for this discussion,
Andi

Quoted 40 lines

The runqueue node - which contains command list for g2d rendering - is generated when the user calls the ioctl system call. Therefore, if the user-requested command list is rendered by g2d device then there is no longer a reason to keep it. :)

Quoted 6 lines

The patch per se is OK. It doesn't make much difference to me where you actually read async, although this patch looks a bit safer:

Thanks,
Inki Dae

Quoted 26 lines

However some refactoring might be needed to make it a bit more robust.

Thanks,
Andi

Quoted 19 lines
    }
Quoted 3 lines

Finally, can you elaborate on the code review that you did so that we all understand it?

queue_work(g2d->g2d_workq, &g2d->runqueue_work);
msleep(100); // add sleep here to let g2d_runqueue_worker run first
if (runqueue_node->async)

    goto out;
Quoted 2 lines

Andi

--
Min Li

#8 lore raw

Andi~ :)

Quoted 47 lines

-----Original Message-----
From: Andi Shyti <[email protected]>
Sent: Thursday, June 1, 2023 5:29 PM
To: 대인기/Tizen Platform Lab(SR)/삼성전자 <[email protected]>
Cc: 'lm0963' <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dri- [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] drm/exynos: fix race condition UAF in exynos_g2d_exec_ioctl

Hi Inki,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

If it is async, runqueue_node is freed in

g2d_runqueue_worker on

Quoted 2 lines
Quoted 2 lines

another

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

worker thread. So in extreme cases, if g2d_runqueue_worker

runs

Quoted 19 lines
Quoted 19 lines

first, and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

then executes the following if statement, there will be use-

after-free.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines
---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/exynos/exynos_drm_g2d.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct

Quoted 4 lines

index ec784e58da5c..414e585ec7dd 100644

drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct

drm_device *drm_dev, void *data,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
      /* Let the runqueue know that there is work to do. */
      queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct · line 1338

Quoted 2 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
-     if (runqueue_node->async)
+     if (req->async)

did you actually hit this? If you did, then the fix is not OK.

No, I didn't actually hit this. I found it through code review.

This

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

is only a theoretical issue that can only be triggered in

extreme

Quoted 36 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

cases.

first of all runqueue is used again two lines below this, which means that if you don't hit the uaf here you will hit it immediately after.

No, if async is true, then it will goto out, which will directly

return.

Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

if (runqueue_node->async)
goto out; // here, go to out, will directly return

wait_for_completion(&runqueue_node->complete); // not hit
g2d_free_runqueue_node(g2d, runqueue_node);

out:
return 0;

that's right, sorry, I misread it.

Quoted 16 lines
Quoted 3 lines

Second, if runqueue is freed, than we need to remove the part where it's freed because it doesn't make sense to free runqueue at this stage.

It is freed by g2d_free_runqueue_node in g2d_runqueue_worker

static void g2d_runqueue_worker(struct work_struct *work)

{
    ......
    if (runqueue_node) {
        pm_runtime_mark_last_busy(g2d->dev);
        pm_runtime_put_autosuspend(g2d->dev);

        complete(&runqueue_node->complete);
        if (runqueue_node->async)
            g2d_free_runqueue_node(g2d, runqueue_node);        //

freed

Quoted 5 lines

here

Quoted 4 lines
Quoted 3 lines

this is what I'm wondering: is it correct to free a resource here? The design looks to me a bit fragile and prone to mistakes.

Quoted 8 lines
Quoted 1 line

This question seems to deviate from the purpose of this patch. If you

are providing additional opinions for code quality improvement unrelated to this patch, it would be more appropriate for me to answer instead of him.

It's not deviating as the question was already made in my first review. It just looks strange to me that a piece of data shared amongst processes can be freed up without sinchronizing. A bunch

I believe that if we overlook any doubts or concerns about worrisome aspects without completely resolving them, it wouldn't be helpful to the community.
Therefore, I would like to clarify more explicitly in order to ensure a better understanding.

AFAIK, the data you mentioned isn't shared between processes. This data is generated driver-internally when the user makes a rendering request and will be removed once the 2D GPU finishes rendering.

However, there may be another issue that I'm not aware of, so if there is any, give me it more specifically as it would help improve driver stability.

Thanks again,
Inki Dae

Quoted 62 lines

of if's do not make it robust enough.

The patch itself, in my point of view, is not really fixing much and won't make any difference, it's just exposing the weakness I mentioned.

However, honestly speaking, I don't know the driver well enough to suggest architectural changes and that's why I r-b'ed this one. But the first thing that comes to my mind, without looking much at the code, is using kref's as a way to make sure that a resource doesn't magically disappear under your nose.

But, of course, this is up to you and if in your opinion this is OK and it fixes it... then you definitely know better :)

Thanks for this discussion,
Andi

Quoted 1 line

The runqueue node - which contains command list for g2d rendering - is

generated when the user calls the ioctl system call. Therefore, if the user-requested command list is rendered by g2d device then there is no longer a reason to keep it. :)

Quoted 26 lines
Quoted 7 lines

The patch per se is OK. It doesn't make much difference to me where you actually read async, although this patch looks a bit safer:

Thanks,
Inki Dae

Quoted 15 lines

However some refactoring might be needed to make it a bit more robust.

Thanks,
Andi

Quoted 8 lines
    }
Quoted 3 lines

Finally, can you elaborate on the code review that you did so that we all understand it?

queue_work(g2d->g2d_workq, &g2d->runqueue_work);
msleep(100); // add sleep here to let g2d_runqueue_worker run

first

Quoted 13 lines
Quoted 11 lines
Quoted 11 lines

if (runqueue_node->async)

    goto out;
Quoted 2 lines

Andi

--
Min Li

#9 lore raw
In reply to Min Li

Hi,

Quoted 11 lines

-----Original Message-----
From: Min Li <[email protected]>
Sent: Friday, May 26, 2023 10:02 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH] drm/exynos: fix race condition UAF in exynos_g2d_exec_ioctl

Quoted 4 lines

If it is async, runqueue_node is freed in g2d_runqueue_worker on another worker thread. So in extreme cases, if g2d_runqueue_worker runs first, and then executes the following if statement, there will be use-after-free.

I received a report about the related issue from a white hacker before. Thanks for contribution. :)

drivers/gpu/drm/exynos/exynos_drm_g2d.c int exynos_g2d_exec_ioctl(struct drm_device

Quoted 11 lines
---
 drivers/gpu/drm/exynos/exynos_drm_g2d.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +0 -0
index ec784e58da5c..414e585ec7dd 100644
@@ -1335,7 +1335,7 @@ int exynos_g2d_exec_ioctl(struct drm_device

*drm_dev,

Quoted 11 lines
drivers/gpu/drm/exynos/exynos_drm_g2d.c +1 -1
 void *data,
 	/* Let the runqueue know that there is work to do. */
 	queue_work(g2d->g2d_workq, &g2d->runqueue_work);
 
-	if (runqueue_node->async)
+	if (req->async)
 		goto out;
 

wait_for_completion(&runqueue_node->complete);
--
2.34.1