Skip to contentSkip to conversation

audio/AVCTP: Fix crash

PATCH In review Luiz Augusto von Dentz 2 participants 4 messages Dec 3, 2013 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/1 audio/AVCTP: Fix crash

From: Luiz Augusto von Dentz <[email protected]>

The following crash happens because the list l is modified within the loop so it is no longer safe to call l->next directly:

Invalid read of size 8
at 0x41F276: pending_create (avctp.c:1491)
by 0x41F7C0: avctp_send_req.isra.6 (avctp.c:1539)
by 0x41F887: avctp_passthrough_release (avctp.c:1643)
by 0x41F9DF: avctp_passthrough_rsp (avctp.c:1698)
by 0x41E9AC: session_cb (avctp.c:782)
by 0x31D1647DF5: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.3600.3) by 0x31D1648147: ??? (in /usr/lib64/libglib-2.0.so.0.3600.3)
by 0x31D1648549: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.3600.3) by 0x40A49F: main (main.c:587)
Address 0x8 is not stack'd, malloc'd or (recently) free'd

---
 profiles/audio/avctp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
profiles/audio/avctp.c +1 -1
index 4de981c..6669ddc 100644
@@ -1488,7 +1488,7 @@ static struct avctp_pending_req *pending_create(struct avctp_channel *chan,
 	tmp = g_slist_copy(chan->processed);
 
 	/* Find first unused transaction id */
-	for (l = tmp; l; l = l->next) {
+	for (l = tmp; l; l = g_slist_next(l)) {
 		struct avctp_pending_req *req = l->data;
 
 		if (req->transaction == chan->transaction) {
Signature · 1 line
1.8.3.1
#1 lore raw

Hi Luiz,

profiles/audio/avctp.c static struct avctp_pending_req *pending_create(struct avctp_channel *chan, · line 1491

Quoted 8 lines · Luiz Augusto von Dentz
profiles/audio/avctp.c +1 -1
@@ -1488,7 +1488,7 @@ static struct avctp_pending_req *pending_create(struct avctp_channel *chan,
        tmp = g_slist_copy(chan->processed);
 
        /* Find first unused transaction id */
-       for (l = tmp; l; l = l->next) {
+       for (l = tmp; l; l = g_slist_next(l)) {

Are you sure this fixes the problem? AFAIK g_list_next() will still access invalid memory unless the "next" pointer is saved *before* the current entry is freed. See e.g. remove_temp_devices() in src/adapter.c.

profiles/audio/avctp.c static struct avctp_pending_req *pending_create(struct avctp_channel *chan, · line 1494

Quoted 3 lines
profiles/audio/avctp.c +0 -0
                struct avctp_pending_req *req = l->data;
 
                if (req->transaction == chan->transaction) {

Best Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil
author maintainer
#2 lore raw

Hi Anderson,

Quoted 9 lines · Anderson Lizardo

Hi Luiz,

Quoted 5 lines · Luiz Augusto von Dentz
profiles/audio/avctp.c +0 -0
@@ -1488,7 +1488,7 @@ static struct avctp_pending_req *pending_create(struct avctp_channel *chan,
        tmp = g_slist_copy(chan->processed);
 
Quoted 8 lines

profiles/audio/avctp.c static struct avctp_pending_req *pending_create(struct avctp_channel *chan, · line 1491

Quoted 3 lines
profiles/audio/avctp.c +1 -1
        /* Find first unused transaction id */
-       for (l = tmp; l; l = l->next) {
+       for (l = tmp; l; l = g_slist_next(l)) {

Are you sure this fixes the problem? AFAIK g_list_next() will still access invalid memory unless the "next" pointer is saved *before* the current entry is freed. See e.g. remove_temp_devices() in src/adapter.c.

Yep, I tested and it fixes the problem. The problem is not actually removing the item, but reassigning tmp to the head of the list which can be NULL so doing l = l->next before deleting the node doesn't help in this case. Anyway this is now applied but we will be changing the logic how to check the available transaction to use bitmask operations so we find the next transaction without looping in a list.

#3 lore raw

Hi Luiz,

Quoted 22 lines · Luiz Augusto von Dentz

Hi Anderson,

Quoted 9 lines · Anderson Lizardo

Hi Luiz,

Quoted 5 lines · Luiz Augusto von Dentz
profiles/audio/avctp.c +0 -0
@@ -1488,7 +1488,7 @@ static struct avctp_pending_req *pending_create(struct avctp_channel *chan,
        tmp = g_slist_copy(chan->processed);
 
Quoted 8 lines

profiles/audio/avctp.c static struct avctp_pending_req *pending_create(struct avctp_channel *chan, · line 1491

Quoted 3 lines
profiles/audio/avctp.c +1 -1
        /* Find first unused transaction id */
-       for (l = tmp; l; l = l->next) {
+       for (l = tmp; l; l = g_slist_next(l)) {

Are you sure this fixes the problem? AFAIK g_list_next() will still access invalid memory unless the "next" pointer is saved *before* the current entry is freed. See e.g. remove_temp_devices() in src/adapter.c.

Quoted 6 lines

Yep, I tested and it fixes the problem. The problem is not actually removing the item, but reassigning tmp to the head of the list which can be NULL so doing l = l->next before deleting the node doesn't help in this case. Anyway this is now applied but we will be changing the logic how to check the available transaction to use bitmask operations so we find the next transaction without looping in a list.

In this case I agree that it is not the same situation as I described. I was looking only at the patch itself, not the original code :/ In any case, good to know you are going to simplify it.

BTW, I noticed that the "chan->transaction %= 16;" in that same function (in two places) is unnecessary, because chan->transaction is "uint8_t transaction:4" (4-bit bitfield) so the post increment operation before this statement will always overflow "transaction" and it will go back to 0 once it reaches 15.

Best Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil