Skip to contentSkip to conversation

binder: fix atomic sleep when get extended error

PATCH Superseded Schspa Shi 2 participants 3 messages May 17, 2022 – May 18, 2022 Android drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Schspa Shi

author
#0 lore raw

PATCH 1/1 binder: fix atomic sleep when get extended error

binder_inner_proc_lock(thread->proc) is a spin lock, copy_to_user can't be called with in this lock.

Copy it as a local variable, and check the id to make sure the user space gets the latest error message

Reported-by [email protected]
Fixes bd32889e841c ("binder: add BINDER_GET_EXTENDED_ERROR ioctl")
---
 drivers/android/binder.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/android/binder.c +10 -3
index d9253b2a7bd9..5f2e1fa3da74 100644
@@ -5163,13 +5163,20 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,
 					   void __user *ubuf)
 {
 	struct binder_extended_error *ee = &thread->ee;
+	struct binder_extended_error eeb;
 
 	binder_inner_proc_lock(thread->proc);
-	if (copy_to_user(ubuf, ee, sizeof(*ee))) {
-		binder_inner_proc_unlock(thread->proc);
+retry:
+	eeb = *ee;
+	binder_inner_proc_unlock(thread->proc);
+	if (copy_to_user(ubuf, &eeb, sizeof(eeb)))
 		return -EFAULT;
-	}
 
+	binder_inner_proc_lock(thread->proc);
+	if (eeb.id != ee->id) {
+		/* retry to get newest error info */
+		goto retry;
+	}
 	ee->id = 0;
 	ee->command = BR_OK;
 	ee->param = 0;
Signature · 1 line
2.24.3 (Apple Git-128)
maintainer
#1 lore raw
Quoted 44 lines · Schspa Shi

binder_inner_proc_lock(thread->proc) is a spin lock, copy_to_user can't be called with in this lock.

Copy it as a local variable, and check the id to make sure the user space gets the latest error message

Reported-by [email protected]
Fixes bd32889e841c ("binder: add BINDER_GET_EXTENDED_ERROR ioctl")
---
 drivers/android/binder.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/android/binder.c +10 -3
index d9253b2a7bd9..5f2e1fa3da74 100644
@@ -5163,13 +5163,20 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,
 					   void __user *ubuf)
 {
 	struct binder_extended_error *ee = &thread->ee;
+	struct binder_extended_error eeb;
 
 	binder_inner_proc_lock(thread->proc);
-	if (copy_to_user(ubuf, ee, sizeof(*ee))) {
-		binder_inner_proc_unlock(thread->proc);
+retry:
+	eeb = *ee;
+	binder_inner_proc_unlock(thread->proc);
+	if (copy_to_user(ubuf, &eeb, sizeof(eeb)))
 		return -EFAULT;
-	}
 
+	binder_inner_proc_lock(thread->proc);
+	if (eeb.id != ee->id) {
+		/* retry to get newest error info */
+		goto retry;
+	}
 	ee->id = 0;
 	ee->command = BR_OK;
 	ee->param = 0;
Signature · 1 line
2.24.3 (Apple Git-128)

Oops! Thank you for your patch. In this case the local copy would be enough, no need for the retry logic as this is already taken care of. You could also leverage the binder_set_extended_error() to reset the thread->ee. Would you mind sending these updates? I was thinking something like this:

 drivers/android/binder.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/android/binder.c +6 -9
index 83facfa1a5c3..f92021cd384b 100644
@@ -5163,19 +5163,16 @@ static int binder_ioctl_get_freezer_info(
 static int binder_ioctl_get_extended_error(struct binder_thread *thread,
 					   void __user *ubuf)
 {
-	struct binder_extended_error *ee = &thread->ee;
+	struct binder_extended_error ee;
 
 	binder_inner_proc_lock(thread->proc);
-	if (copy_to_user(ubuf, ee, sizeof(*ee))) {
-		binder_inner_proc_unlock(thread->proc);
-		return -EFAULT;
-	}
-
-	ee->id = 0;
-	ee->command = BR_OK;
-	ee->param = 0;
+	ee = thread->ee;
+	binder_set_extended_error(&thread->ee, 0, BR_OK, 0);
 	binder_inner_proc_unlock(thread->proc);
 
+	if (copy_to_user(ubuf, &ee, sizeof(ee)))
+		return -EFAULT;
+
 	return 0;
 }
 

--

Carlos Llamas

author
#2 lore raw
Quoted 43 lines · Carlos Llamas
Quoted 42 lines · Schspa Shi

binder_inner_proc_lock(thread->proc) is a spin lock, copy_to_user can't be called with in this lock.

Copy it as a local variable, and check the id to make sure the user space gets the latest error message

Reported-by [email protected]
Fixes bd32889e841c ("binder: add BINDER_GET_EXTENDED_ERROR ioctl")
---
 drivers/android/binder.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/android/binder.c +10 -4
index d9253b2a7bd9..5f2e1fa3da74 100644
@@ -5163,13 +5163,20 @@ static int binder_ioctl_get_extended_error(struct binder_thread *thread,
                                        void __user *ubuf)
 {
     struct binder_extended_error *ee = &thread->ee;
+    struct binder_extended_error eeb;
 
     binder_inner_proc_lock(thread->proc);
-    if (copy_to_user(ubuf, ee, sizeof(*ee))) {
-            binder_inner_proc_unlock(thread->proc);
+retry:
+    eeb = *ee;
+    binder_inner_proc_unlock(thread->proc);
+    if (copy_to_user(ubuf, &eeb, sizeof(eeb)))
             return -EFAULT;
-    }
 
+    binder_inner_proc_lock(thread->proc);
+    if (eeb.id != ee->id) {
+            /* retry to get newest error info */
+            goto retry;
+    }
     ee->id = 0;
     ee->command = BR_OK;
     ee->param = 0;
--
Quoted 8 lines
Quoted 2 lines

2.24.3 (Apple Git-128)

Oops! Thank you for your patch. In this case the local copy would be enough, no need for the retry logic as this is already taken care of. You could also leverage the binder_set_extended_error() to reset the thread->ee. Would you mind sending these updates? I was thinking something like this:

Yes, I have sent a v2 patch for this, please review it.

Quoted 23 lines
 drivers/android/binder.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/android/binder.c +1 -8
index 83facfa1a5c3..f92021cd384b 100644
@@ -5163,19 +5163,16 @@ static int binder_ioctl_get_freezer_info(
 static int binder_ioctl_get_extended_error(struct binder_thread *thread,
                                         void __user *ubuf)
 {
-     struct binder_extended_error *ee = &thread->ee;
+     struct binder_extended_error ee;
 
      binder_inner_proc_lock(thread->proc);
-     if (copy_to_user(ubuf, ee, sizeof(*ee))) {
-             binder_inner_proc_unlock(thread->proc);
-             return -EFAULT;
-     }
-
-     ee->id = 0;
-     ee->command = BR_OK;

drivers/android/binder.c static int binder_ioctl_get_freezer_info( · line 5177

Quoted 10 lines
drivers/android/binder.c +5 -1
-     ee->param = 0;
+     ee = thread->ee;
+     binder_set_extended_error(&thread->ee, 0, BR_OK, 0);
      binder_inner_proc_unlock(thread->proc);
 
+     if (copy_to_user(ubuf, &ee, sizeof(ee)))
+             return -EFAULT;
+
      return 0;
 }

--
BRs
Schspa Shi