Skip to contentSkip to conversation

misc: fastrpc: Fix a Use after-free-bug by race condition

PATCH In review Sangsup Lee 2 participants 6 messages Mar 23, 2023 – Apr 28, 2023

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

PATCH v2 1/1 misc: fastrpc: Fix a Use after-free-bug by race condition

From: Sangsup lee <[email protected]>

This patch adds mutex_lock for fixing an Use-after-free bug. fastrpc_req_munmap_impl can be called concurrently in multi-threded environments. The buf which is allocated by list_for_each_safe can be used after another thread frees it.

---
V1 -> V2: moving the locking to ioctl.

 drivers/misc/fastrpc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/fastrpc.c +2 -0
index 93ebd174d848..aa1cf0e9f4ed 100644
@@ -1901,7 +1901,9 @@ static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
 		err = fastrpc_req_mmap(fl, argp);
 		break;
 	case FASTRPC_IOCTL_MUNMAP:
+		mutex_lock(&fl->mutex);
 		err = fastrpc_req_munmap(fl, argp);
+		mutex_unlock(&fl->mutex);
 		break;
 	case FASTRPC_IOCTL_MEM_MAP:
 		err = fastrpc_req_mem_map(fl, argp);
Signature · 1 line
2.25.1
#1 lore raw

Is there any comment for this issue?
(reference: https://www.spinics.net/lists/kernel/msg4731408.html)

2023년 3월 23일 (목) 오전 10:37, Sangsup Lee <[email protected]>님이 작성:

Quoted 31 lines

From: Sangsup lee <[email protected]>

This patch adds mutex_lock for fixing an Use-after-free bug. fastrpc_req_munmap_impl can be called concurrently in multi-threded environments. The buf which is allocated by list_for_each_safe can be used after another thread frees it.

---
V1 -> V2: moving the locking to ioctl.

 drivers/misc/fastrpc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/fastrpc.c +2 -1
index 93ebd174d848..aa1cf0e9f4ed 100644
@@ -1901,7 +1901,9 @@ static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
                err = fastrpc_req_mmap(fl, argp);
                break;
        case FASTRPC_IOCTL_MUNMAP:
+               mutex_lock(&fl->mutex);
                err = fastrpc_req_munmap(fl, argp);
+               mutex_unlock(&fl->mutex);
                break;
        case FASTRPC_IOCTL_MEM_MAP:
                err = fastrpc_req_mem_map(fl, argp);
--

2.25.1

#2 lore raw
Quoted 1 line · sangsup lee

Is there any comment for this issue?

What issue?

Quoted 1 line

(reference: https://www.spinics.net/lists/kernel/msg4731408.html)

Please use lore.kernel.org links, we have no control over any other random email archive .

And the above link just points to this proposed patch.

Quoted 9 lines

2023년 3월 23일 (목) 오전 10:37, Sangsup Lee <[email protected]>님이 작성:

Quoted 6 lines

From: Sangsup lee <[email protected]>

This patch adds mutex_lock for fixing an Use-after-free bug. fastrpc_req_munmap_impl can be called concurrently in multi-threded environments. The buf which is allocated by list_for_each_safe can be used after another thread frees it.

How was this tested?

Quoted 8 lines
Quoted 8 lines

---
V1 -> V2: moving the locking to ioctl.

 drivers/misc/fastrpc.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 11 lines

drivers/misc/fastrpc.c static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, · line 1906

Quoted 11 lines
drivers/misc/fastrpc.c +2 -0
index 93ebd174d848..aa1cf0e9f4ed 100644
@@ -1901,7 +1901,9 @@ static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
                err = fastrpc_req_mmap(fl, argp);
                break;
        case FASTRPC_IOCTL_MUNMAP:
+               mutex_lock(&fl->mutex);
                err = fastrpc_req_munmap(fl, argp);
+               mutex_unlock(&fl->mutex);

Are you sure you can call this function with the lock? If so, why isn't the mmap ioctl also locked?

thanks,

greg k-h

#3 lore raw

I reported fastrpc bug in Feb,2023.(https://lore.kernel.org/lkml/[email protected])

And Srinivas recommended this patch code for patch v2. That's why I sent this patch v2 however, I haven't received any reply after that.

I just want to know the next step for patching this code. Should I just keep waiting ? Or Please let me know if I need to provide you with more information.

(Ps. I'm sorry, i re-send this reply because of missing text-mode )

Best regards.

2023년 4월 27일 (목) 오후 6:52, Greg Kroah-Hartman <[email protected]>님이 작성:

Quoted 51 lines · sangsup lee
Quoted 1 line

Is there any comment for this issue?

What issue?

Quoted 1 line

(reference: https://www.spinics.net/lists/kernel/msg4731408.html)

Please use lore.kernel.org links, we have no control over any other random email archive .

And the above link just points to this proposed patch.

Quoted 9 lines

2023년 3월 23일 (목) 오전 10:37, Sangsup Lee <[email protected]>님이 작성:

Quoted 6 lines

From: Sangsup lee <[email protected]>

This patch adds mutex_lock for fixing an Use-after-free bug. fastrpc_req_munmap_impl can be called concurrently in multi-threded environments. The buf which is allocated by list_for_each_safe can be used after another thread frees it.

How was this tested?

Quoted 8 lines
Quoted 8 lines

---
V1 -> V2: moving the locking to ioctl.

 drivers/misc/fastrpc.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 11 lines

drivers/misc/fastrpc.c static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, · line 1906

Quoted 11 lines
drivers/misc/fastrpc.c +2 -0
index 93ebd174d848..aa1cf0e9f4ed 100644
@@ -1901,7 +1901,9 @@ static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
                err = fastrpc_req_mmap(fl, argp);
                break;
        case FASTRPC_IOCTL_MUNMAP:
+               mutex_lock(&fl->mutex);
                err = fastrpc_req_munmap(fl, argp);
+               mutex_unlock(&fl->mutex);

Are you sure you can call this function with the lock? If so, why isn't the mmap ioctl also locked?

thanks,

greg k-h

#4 lore raw

A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I include quotations after my reply?

http://daringfireball.net/2007/07/on_top

Quoted 2 lines · sangsup lee

I reported fastrpc bug in Feb,2023.(https://lore.kernel.org/lkml/[email protected])

That was a patch, not a bug report. How was this tested?

Quoted 7 lines

And Srinivas recommended this patch code for patch v2. That's why I sent this patch v2 however, I haven't received any reply after that.

I just want to know the next step for patching this code. Should I just keep waiting ? Or Please let me know if I need to provide you with more information.

Please read my comments on my last email on the patch for what you need to address.

thanks,

greg k-h

#5 lore raw
In reply to Greg Kroah-Hartman

I apologize for recognizing the email in the TOP POST format. I missed the comment in the previous email.

2023년 4월 27일 (목) 오후 6:52, Greg Kroah-Hartman <[email protected]>님이 작성:

Quoted 17 lines · sangsup lee
Quoted 1 line

Is there any comment for this issue?

What issue?

Quoted 1 line

(reference: https://www.spinics.net/lists/kernel/msg4731408.html)

Please use lore.kernel.org links, we have no control over any other random email archive .

And the above link just points to this proposed patch.

Quoted 4 lines

2023년 3월 23일 (목) 오전 10:37, Sangsup Lee <[email protected]>님이 작성:

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

From: Sangsup lee <[email protected]>

This patch adds mutex_lock for fixing an Use-after-free bug. fastrpc_req_munmap_impl can be called concurrently in multi-threded environments. The buf which is allocated by list_for_each_safe can be used after another thread frees it.

How was this tested?

I was unable to configure the environment in which this code is running. Therefore, I could not go through dynamic testing for the related issue. However, I found vulnerabilities in similar types of functions in other drivers in the mobile environment, so I think this code is also problematic.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

---
V1 -> V2: moving the locking to ioctl.

 drivers/misc/fastrpc.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 14 lines
Quoted 11 lines

drivers/misc/fastrpc.c static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, · line 1906

Quoted 11 lines
drivers/misc/fastrpc.c +2 -0
index 93ebd174d848..aa1cf0e9f4ed 100644
@@ -1901,7 +1901,9 @@ static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
                err = fastrpc_req_mmap(fl, argp);
                break;
        case FASTRPC_IOCTL_MUNMAP:
+               mutex_lock(&fl->mutex);
                err = fastrpc_req_munmap(fl, argp);
+               mutex_unlock(&fl->mutex);

Are you sure you can call this function with the lock? If so, why isn't the mmap ioctl also locked?

I am convinced that FASTRPC_IOCTL_MUNMAP can lead to a race condition bug. However, as I mentioned in patch v1, I am not an expert in fastrpc. I worried about the side effects of the code I suggested.
So, I asked you to recommend which code to use, and this is the code that was recommended to me.
And I didn't check the mmap function because I couldn't find a bug in mmap.

Quoted 4 lines

thanks,

greg k-h