usb: gadget: rndis: add spinlock for rndis response list
commit aaaba1c86d04dac8e49bf508b492f81506257da3 upstream.
There's no lock for rndis response list. It could cause list corruption
if there're two different list_add at the same time like below.
It's better to add in rndis_add_response / rndis_free_response
/ rndis_get_next_response to prevent any race condition on response list.
[ 361.894299] [1: irq/191-dwc3:16979] list_add corruption.
next->prev should be prev (ffffff80651764d0),
but was ffffff883dc36f80. (next=ffffff80651764d0).
[ 361.904380] [1: irq/191-dwc3:16979] Call trace:
[ 361.904391] [1: irq/191-dwc3:16979] __list_add_valid+0x74/0x90
[ 361.904401] [1: irq/191-dwc3:16979] rndis_msg_parser+0x168/0x8c0
[ 361.904409] [1: irq/191-dwc3:16979] rndis_command_complete+0x24/0x84
[ 361.904417] [1: irq/191-dwc3:16979] usb_gadget_giveback_request+0x20/0xe4
[ 361.904426] [1: irq/191-dwc3:16979] dwc3_gadget_giveback+0x44/0x60
[ 361.904434] [1: irq/191-dwc3:16979] dwc3_ep0_complete_data+0x1e8/0x3a0
[ 361.904442] [1: irq/191-dwc3:16979] dwc3_ep0_interrupt+0x29c/0x3dc
[ 361.904450] [1: irq/191-dwc3:16979] dwc3_process_event_entry+0x78/0x6cc
[ 361.904457] [1: irq/191-dwc3:16979] dwc3_process_event_buf+0xa0/0x1ec
[ 361.904465] [1: irq/191-dwc3:16979] dwc3_thread_interrupt+0x34/0x5c
Fixes: f6281af9d6
("usb: gadget: rndis: use list_for_each_entry_safe")
Cc: stable <stable@kernel.org>
Signed-off-by: Daehwan Jung <dh10.jung@samsung.com>
Link: https://lore.kernel.org/r/1645507768-77687-1-git-send-email-dh10.jung@samsung.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:

committed by
Greg Kroah-Hartman

parent
ddc254fc88
commit
4ce247af3f
@@ -922,6 +922,7 @@ struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
|
|||||||
params->resp_avail = resp_avail;
|
params->resp_avail = resp_avail;
|
||||||
params->v = v;
|
params->v = v;
|
||||||
INIT_LIST_HEAD(¶ms->resp_queue);
|
INIT_LIST_HEAD(¶ms->resp_queue);
|
||||||
|
spin_lock_init(¶ms->resp_lock);
|
||||||
pr_debug("%s: configNr = %d\n", __func__, i);
|
pr_debug("%s: configNr = %d\n", __func__, i);
|
||||||
|
|
||||||
return params;
|
return params;
|
||||||
@@ -1015,12 +1016,14 @@ void rndis_free_response(struct rndis_params *params, u8 *buf)
|
|||||||
{
|
{
|
||||||
rndis_resp_t *r, *n;
|
rndis_resp_t *r, *n;
|
||||||
|
|
||||||
|
spin_lock(¶ms->resp_lock);
|
||||||
list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) {
|
list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) {
|
||||||
if (r->buf == buf) {
|
if (r->buf == buf) {
|
||||||
list_del(&r->list);
|
list_del(&r->list);
|
||||||
kfree(r);
|
kfree(r);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
spin_unlock(¶ms->resp_lock);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(rndis_free_response);
|
EXPORT_SYMBOL_GPL(rndis_free_response);
|
||||||
|
|
||||||
@@ -1030,14 +1033,17 @@ u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
|
|||||||
|
|
||||||
if (!length) return NULL;
|
if (!length) return NULL;
|
||||||
|
|
||||||
|
spin_lock(¶ms->resp_lock);
|
||||||
list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) {
|
list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) {
|
||||||
if (!r->send) {
|
if (!r->send) {
|
||||||
r->send = 1;
|
r->send = 1;
|
||||||
*length = r->length;
|
*length = r->length;
|
||||||
|
spin_unlock(¶ms->resp_lock);
|
||||||
return r->buf;
|
return r->buf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
spin_unlock(¶ms->resp_lock);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(rndis_get_next_response);
|
EXPORT_SYMBOL_GPL(rndis_get_next_response);
|
||||||
@@ -1054,7 +1060,9 @@ static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
|
|||||||
r->length = length;
|
r->length = length;
|
||||||
r->send = 0;
|
r->send = 0;
|
||||||
|
|
||||||
|
spin_lock(¶ms->resp_lock);
|
||||||
list_add_tail(&r->list, ¶ms->resp_queue);
|
list_add_tail(&r->list, ¶ms->resp_queue);
|
||||||
|
spin_unlock(¶ms->resp_lock);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -174,6 +174,7 @@ typedef struct rndis_params {
|
|||||||
void (*resp_avail)(void *v);
|
void (*resp_avail)(void *v);
|
||||||
void *v;
|
void *v;
|
||||||
struct list_head resp_queue;
|
struct list_head resp_queue;
|
||||||
|
spinlock_t resp_lock;
|
||||||
} rndis_params;
|
} rndis_params;
|
||||||
|
|
||||||
/* RNDIS Message parser and other useless functions */
|
/* RNDIS Message parser and other useless functions */
|
||||||
|
Reference in New Issue
Block a user