sctp: fix refcount bug in sctp_wfree
Subsystems (3)
PATCH v4 1/1 sctp: fix refcount bug in sctp_wfree
sctp_sock_migrate should iterate over the datamsgs to modify all trunks(skbs) to newsk. For this, out_msg_list is added to sctp_outq to maintain datamsgs list.
The following case cause the bug:
for the trouble SKB, it was in outq->transmitted list
sctp_outq_sack
sctp_check_transmitted
SKB was moved to outq->sacked list
then throw away the sack queue
SKB was deleted from outq->sacked(but it was held by datamsg at sctp_datamsg_to_asoc So, sctp_wfree was not called here)
then migrate happened
sctp_for_each_tx_datachunk(
sctp_clear_owner_w);
sctp_assoc_migrate();
sctp_for_each_tx_datachunk(
sctp_set_owner_w);SKB was not in the outq, and was not changed to newsk
finally
__sctp_outq_teardown
sctp_chunk_put (for another skb)
sctp_datamsg_put
__kfree_skb(msg->frag_list)
sctp_wfree (for SKB)
SKB->sk was still oldsk (skb->sk != asoc->base.sk).
--- include/net/sctp/structs.h | 5 +++++ net/sctp/chunk.c | 4 ++++ net/sctp/outqueue.c | 1 + net/sctp/sm_sideeffect.c | 1 + net/sctp/socket.c | 27 +++++++-------------------- 5 files changed, 18 insertions(+), 20 deletions(-)
include/net/sctp/structs.h +5 -0
@@ -522,6 +522,8 @@ struct sctp_pf {struct sctp_datamsg {/* Chunks waiting to be submitted to lower layer. */ struct list_head chunks; +/* List in outq. */ +struct list_head list;/* Reference counting. */ refcount_t refcnt;/* When is this message no longer interesting to the peer? */
@@ -1063,6 +1065,9 @@ struct sctp_outq {/* Data pending that has never been transmitted. */ struct list_head out_chunk_list; +/* Data msg list. */ +struct list_head out_msg_list; +/* Stream scheduler being used */ struct sctp_sched_ops *sched;
net/sctp/chunk.c +4 -0
@@ -41,6 +41,7 @@ static void sctp_datamsg_init(struct sctp_datamsg *msg) msg->abandoned =0 ; msg->expires_at =0 ;INIT_LIST_HEAD (&msg->chunks); +INIT_LIST_HEAD (&msg->list); }/* Allocate and initialize datamsg. */
@@ -111,6 +112,9 @@ static void sctp_datamsg_destroy(struct sctp_datamsg *msg)sctp_chunk_put (chunk); } +if (!list_empty (&msg->list)) +list_del_init (&msg->list); +SCTP_DBG_OBJCNT_DEC (datamsg);kfree (msg); }
net/sctp/outqueue.c +1 -0
@@ -194,6 +194,7 @@ void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q) q->asoc = asoc;INIT_LIST_HEAD (&q->out_chunk_list); +INIT_LIST_HEAD (&q->out_msg_list);INIT_LIST_HEAD (&q->control_chunk_list);INIT_LIST_HEAD (&q->retransmit);INIT_LIST_HEAD (&q->sacked);
net/sctp/sm_sideeffect.c +1 -0
@@ -1099,6 +1099,7 @@ static void sctp_cmd_send_msg(struct sctp_association *asoc,list_for_each_entry (chunk, &msg->chunks, frag_list)sctp_outq_tail (&asoc->outqueue, chunk, gfp); +list_add_tail (&msg->list, &asoc->outqueue.out_msg_list); asoc->outqueue.sched->enqueue (&asoc->outqueue, msg); }
net/sctp/socket.c +7 -20
@@ -147,29 +147,16 @@ static void sctp_clear_owner_w(struct sctp_chunk *chunk)skb_orphan (chunk->skb); } -static void sctp_for_each_tx_datachunk (struct sctp_association *asoc, -void (*cb)(struct sctp_chunk *)) +static void sctp_for_each_tx_datamsg (struct sctp_association *asoc, +void (*cb)(struct sctp_chunk *)) { -struct sctp_outq *q = &asoc->outqueue; -struct sctp_transport *t;struct sctp_chunk *chunk; +struct sctp_datamsg *msg; -list_for_each_entry (t, &asoc->peer.transport_addr_list, transports) -list_for_each_entry (chunk, &t->transmitted, transmitted_list) +list_for_each_entry (msg, &asoc->outqueue.out_msg_list, list) +list_for_each_entry (chunk, &msg->chunks, frag_list)cb (chunk); - -list_for_each_entry (chunk, &q->retransmit, transmitted_list) -cb (chunk); - -list_for_each_entry (chunk, &q->sacked, transmitted_list) -cb (chunk); - -list_for_each_entry (chunk, &q->abandoned, transmitted_list) -cb (chunk); - -list_for_each_entry (chunk, &q->out_chunk_list, list) -cb (chunk); } static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
@@ -9574,9 +9561,9 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,* paths won't try to lock it and then oldsk. */ lock_sock_nested (newsk,SINGLE_DEPTH_NESTING ); -sctp_for_each_tx_datachunk (assoc, sctp_clear_owner_w); +sctp_for_each_tx_datamsg (assoc, sctp_clear_owner_w);sctp_assoc_migrate (assoc, newsk); -sctp_for_each_tx_datachunk (assoc, sctp_set_owner_w); +sctp_for_each_tx_datamsg (assoc, sctp_set_owner_w);/* If the association on the newsk is already closed before accept() * is called, set RCV_SHUTDOWN flag.
Signature · 1 line
2.17.1
Marcelo Ricardo Leitner
maintainerQuoted 2 lines · Qiujun Huang
sctp_sock_migrateshould iterate over the datamsgs to modify all trunks(skbs) to newsk. For this,out_msg_listis added to
s/trunks/chunks/
Quoted 1 line
sctp_outqto maintain datamsgs list.
It is an interesting approach. It speeds up the migration, yes, but it will also use more memory per datamsg, for an operation that, when performed, the socket is usually calm.
It's also another list to be handled, and I'm not seeing the patch here move the datamsg itself now to the new outq. It would need something along these lines:sctp_sock_migrate()
{
...
/* Move any messages in the old socket's receive queue that are for the
* peeled off association to the new socket's receive queue.
*/
sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
event = sctp_skb2event(skb);
...
/* Walk through the pd_lobby, looking for skbs that
* need moved to the new socket.
*/
sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
event = sctp_skb2event(skb);That said, I don't think it's worth this new list.
Marcelo
Quoted 6 lines · Marcelo Ricardo Leitner
Quoted 2 lines · Qiujun Huang
sctp_sock_migrateshould iterate over the datamsgs to modify all trunks(skbs) to newsk. For this,out_msg_listis added tos/trunks/chunks/
My :p.
Quoted 10 lines
Quoted 2 lines
sctp_outqto maintain datamsgs list.It is an interesting approach. It speeds up the migration, yes, but it will also use more memory per datamsg, for an operation that, when performed, the socket is usually calm.
It's also another list to be handled, and I'm not seeing the patch here move the datamsg itself now to the new outq. It would need something along these lines:
Are all the rx chunks in the rx queues?
Quoted 15 lines
sctp_sock_migrate()
{
.../* Move any messages in the old socket's receive queue that are for the * peeled off association to the new socket's receive queue. */ sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { event = sctp_skb2event(skb); ... /* Walk through the pd_lobby, looking for skbs that * need moved to the new socket. */ sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { event = sctp_skb2event(skb);
Quoted 1 line
That said, I don't think it's worth this new list.
About this case:
datamsg
->chunk0 chunk1
chunk2 queue ->transmitted ->retransmit
->not in any queue
Also need to maintain a datamsg list to record which datamsg is processed avoiding repetitive
processing.
So, list it to outq. Maybe it will be used sometime.
Quoted 2 lines
Marcelo
Marcelo Ricardo Leitner
maintainerQuoted 18 lines · Qiujun Huang
Quoted 6 lines · Marcelo Ricardo Leitner
Quoted 2 lines · Qiujun Huang
sctp_sock_migrateshould iterate over the datamsgs to modify all trunks(skbs) to newsk. For this,out_msg_listis added tos/trunks/chunks/
My :p.
Quoted 7 lines
Quoted 2 lines
sctp_outqto maintain datamsgs list.It is an interesting approach. It speeds up the migration, yes, but it will also use more memory per datamsg, for an operation that, when performed, the socket is usually calm.
Quoted 5 lines
Quoted 3 lines
It's also another list to be handled, and I'm not seeing the patch here move the datamsg itself now to the new outq. It would need something along these lines:
Are all the rx chunks in the rx queues?
Yes, even with GSO.
Quoted 16 lines
Quoted 16 lines
sctp_sock_migrate()
{
.../* Move any messages in the old socket's receive queue that are for the * peeled off association to the new socket's receive queue. */ sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { event = sctp_skb2event(skb); ... /* Walk through the pd_lobby, looking for skbs that * need moved to the new socket. */ sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { event = sctp_skb2event(skb);
Quoted 8 lines
Quoted 1 line
That said, I don't think it's worth this new list.
About this case:
datamsg->chunk0 chunk1 chunk2queue ->transmitted ->retransmit
->not in any queue
We always can find it through the other chunks, otherwise it's freed.
Quoted 4 lines
Also need to maintain a datamsg list to record which datamsg is processed avoiding repetitive
processing.
Right, but for that we can add a simple check onsctp_for_each_tx_datamsg() based on a parameter.
Quoted 1 line
So, list it to outq. Maybe it will be used sometime.
We can change it when the time comes. For now, if we can avoid growing sctp_datamsg, it's better. With this patch, it grows from 40 to 56 bytes, leaving just 8 left before it starts using a slab of 128 bytes for it.
The patched list_for_each_entry() can/should be factored out into __sctp_for_each_tx_datachunk, whose first parameter then is the queue instead the asoc.
---8<---
net/sctp/socket.c +16 -5
@@ -148,19 +148,30 @@ static void sctp_clear_owner_w(struct sctp_chunk *chunk) }static void sctp_for_each_tx_datachunk (struct sctp_association *asoc, +bool clear,void (*cb)(struct sctp_chunk *)) { +struct sctp_datamsg *msg, *prev_msg =NULL ;struct sctp_outq *q = &asoc->outqueue; +struct sctp_chunk *chunk, *c;struct sctp_transport *t; -struct sctp_chunk *chunk;list_for_each_entry (t, &asoc->peer.transport_addr_list, transports)list_for_each_entry (chunk, &t->transmitted, transmitted_list)cb (chunk); -list_for_each_entry (chunk, &q->retransmit, transmitted_list) -cb (chunk); +list_for_each_entry (chunk, &q->sacked, transmitted_list) { + msg = chunk->msg; +if (msg = prev_msg) +continue ; +list_for_each_entry (c, &msg->chunks, frag_list) { +if ((clear && asoc->base.sk = c->skb->sk) || + (!clear && asoc->base.sk != c->skb->sk)) +cb (c); + } + prev_msg = msg; + }list_for_each_entry (chunk, &q->sacked, transmitted_list)cb (chunk);
@@ -9574,9 +9585,9 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,* paths won't try to lock it and then oldsk. */ lock_sock_nested (newsk,SINGLE_DEPTH_NESTING ); -sctp_for_each_tx_datachunk (assoc, sctp_clear_owner_w); +sctp_for_each_tx_datachunk (assoc, true, sctp_clear_owner_w);sctp_assoc_migrate (assoc, newsk); -sctp_for_each_tx_datachunk (assoc, sctp_set_owner_w); +sctp_for_each_tx_datachunk (assoc, false, sctp_set_owner_w);/* If the association on the newsk is already closed before accept() * is called, set RCV_SHUTDOWN flag.
Quoted 46 lines · Marcelo Ricardo Leitner
Quoted 18 lines · Qiujun Huang
Quoted 6 lines · Marcelo Ricardo Leitner
Quoted 2 lines · Qiujun Huang
sctp_sock_migrateshould iterate over the datamsgs to modify all trunks(skbs) to newsk. For this,out_msg_listis added tos/trunks/chunks/
My :p.
Quoted 7 lines
Quoted 2 lines
sctp_outqto maintain datamsgs list.It is an interesting approach. It speeds up the migration, yes, but it will also use more memory per datamsg, for an operation that, when performed, the socket is usually calm.
Quoted 5 lines
Quoted 3 lines
It's also another list to be handled, and I'm not seeing the patch here move the datamsg itself now to the new outq. It would need something along these lines:
Are all the rx chunks in the rx queues?
Yes, even with GSO.
Quoted 18 lines
Quoted 17 lines
sctp_sock_migrate()
{
.../* Move any messages in the old socket's receive queue that are for the * peeled off association to the new socket's receive queue. */ sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { event = sctp_skb2event(skb); ... /* Walk through the pd_lobby, looking for skbs that * need moved to the new socket. */ sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { event = sctp_skb2event(skb);That said, I don't think it's worth this new list.
Quoted 8 lines
Quoted 6 lines
About this case:
datamsg->chunk0 chunk1 chunk2queue ->transmitted ->retransmit
->not in any queueWe always can find it through the other chunks, otherwise it's freed.
Yes.
datamsg (chunk0, chunk1, chunk2)
chunk1 in transmiited queue chunk2 in retransmit queue chunk0 not in any queue
We also need to check chunk2->msg processed or not, when we iterate
retransmit queue.
Quoted 9 lines
Quoted 5 lines
Also need to maintain a datamsg list to record which datamsg is processed avoiding repetitive
processing.Right, but for that we can add a simple check on
sctp_for_each_tx_datamsg()based on a parameter.
Quoted 6 lines
Quoted 1 line
So, list it to outq. Maybe it will be used sometime.
We can change it when the time comes. For now, if we can avoid growing
sctp_datamsg, it's better. With this patch, it grows from 40 to 56 bytes, leaving just 8 left before it starts using a slab of 128 bytes for it.
I get that, thanks.
Quoted 33 lines
The patched
list_for_each_entry()can/should be factored out into__sctp_for_each_tx_datachunk, whose first parameter then is the queue instead the asoc.---8<---
net/sctp/socket.c +4
-3@@ -148,19 +148,30 @@ static void sctp_clear_owner_w(struct sctp_chunk *chunk) }static void sctp_for_each_tx_datachunk (struct sctp_association *asoc, +bool clear,void (*cb)(struct sctp_chunk *)) { +struct sctp_datamsg *msg, *prev_msg =NULL ;struct sctp_outq *q = &asoc->outqueue; +struct sctp_chunk *chunk, *c;struct sctp_transport *t; -struct sctp_chunk *chunk;list_for_each_entry (t, &asoc->peer.transport_addr_list, transports)list_for_each_entry (chunk, &t->transmitted, transmitted_list)cb (chunk); -list_for_each_entry (chunk, &q->retransmit, transmitted_list) -cb (chunk); +list_for_each_entry (chunk, &q->sacked, transmitted_list) {
net/sctp/socket.c static void sctp_clear_owner_w(struct sctp_chunk *chunk) · line 174
Quoted 10 lines
net/sctp/socket.c +10
-0+ msg = chunk->msg; +if (msg = prev_msg) +continue ; +list_for_each_entry (c, &msg->chunks, frag_list) { +if ((clear && asoc->base.sk = c->skb->sk) || + (!clear && asoc->base.sk != c->skb->sk)) +cb (c); + } + prev_msg = msg; + }
The case exists?
datamsg (chunk0, chunk1, chunk2)
chunk1 chunk2 in &q->retransmit
chunk0 not in any queue (deleted in sctp_outq_sack)
Quoted 14 lines
net/sctp/socket.c +2
-2list_for_each_entry (chunk, &q->sacked, transmitted_list)cb (chunk);@@ -9574,9 +9585,9 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,* paths won't try to lock it and then oldsk. */ lock_sock_nested (newsk,SINGLE_DEPTH_NESTING ); -sctp_for_each_tx_datachunk (assoc, sctp_clear_owner_w); +sctp_for_each_tx_datachunk (assoc, true, sctp_clear_owner_w);sctp_assoc_migrate (assoc, newsk); -sctp_for_each_tx_datachunk (assoc, sctp_set_owner_w); +sctp_for_each_tx_datachunk (assoc, false, sctp_set_owner_w);/* If the association on the newsk is already closed before accept() * is called, set RCV_SHUTDOWN flag.
Quoted 56 lines · Marcelo Ricardo Leitner
Quoted 18 lines · Qiujun Huang
Quoted 6 lines · Marcelo Ricardo Leitner
Quoted 2 lines · Qiujun Huang
sctp_sock_migrateshould iterate over the datamsgs to modify all trunks(skbs) to newsk. For this,out_msg_listis added tos/trunks/chunks/
My :p.
Quoted 7 lines
Quoted 2 lines
sctp_outqto maintain datamsgs list.It is an interesting approach. It speeds up the migration, yes, but it will also use more memory per datamsg, for an operation that, when performed, the socket is usually calm.
Quoted 5 lines
Quoted 3 lines
It's also another list to be handled, and I'm not seeing the patch here move the datamsg itself now to the new outq. It would need something along these lines:
Are all the rx chunks in the rx queues?
Yes, even with GSO.
Quoted 16 lines
Quoted 16 lines
sctp_sock_migrate()
{
.../* Move any messages in the old socket's receive queue that are for the * peeled off association to the new socket's receive queue. */ sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { event = sctp_skb2event(skb); ... /* Walk through the pd_lobby, looking for skbs that * need moved to the new socket. */ sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { event = sctp_skb2event(skb);Quoted 8 lines
Quoted 1 line
That said, I don't think it's worth this new list.
About this case:
datamsg->chunk0 chunk1 chunk2queue ->transmitted ->retransmit
->not in any queueWe always can find it through the other chunks, otherwise it's freed.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Also need to maintain a datamsg list to record which datamsg is processed avoiding repetitive
processing.Right, but for that we can add a simple check on
sctp_for_each_tx_datamsg()based on a parameter.
Great! I get it, thanks!
Quoted 65 lines
Quoted 2 lines
So, list it to outq. Maybe it will be used sometime.
We can change it when the time comes. For now, if we can avoid growing
sctp_datamsg, it's better. With this patch, it grows from 40 to 56 bytes, leaving just 8 left before it starts using a slab of 128 bytes for it.The patched
list_for_each_entry()can/should be factored out into__sctp_for_each_tx_datachunk, whose first parameter then is the queue instead the asoc.---8<---
net/sctp/socket.c +16
-5@@ -148,19 +148,30 @@ static void sctp_clear_owner_w(struct sctp_chunk *chunk) }static void sctp_for_each_tx_datachunk (struct sctp_association *asoc, +bool clear,void (*cb)(struct sctp_chunk *)) { +struct sctp_datamsg *msg, *prev_msg =NULL ;struct sctp_outq *q = &asoc->outqueue; +struct sctp_chunk *chunk, *c;struct sctp_transport *t; -struct sctp_chunk *chunk;list_for_each_entry (t, &asoc->peer.transport_addr_list, transports)list_for_each_entry (chunk, &t->transmitted, transmitted_list)cb (chunk); -list_for_each_entry (chunk, &q->retransmit, transmitted_list) -cb (chunk); +list_for_each_entry (chunk, &q->sacked, transmitted_list) { + msg = chunk->msg; +if (msg = prev_msg) +continue ; +list_for_each_entry (c, &msg->chunks, frag_list) { +if ((clear && asoc->base.sk = c->skb->sk) || + (!clear && asoc->base.sk != c->skb->sk)) +cb (c); + } + prev_msg = msg; + }list_for_each_entry (chunk, &q->sacked, transmitted_list)cb (chunk);@@ -9574,9 +9585,9 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,* paths won't try to lock it and then oldsk. */ lock_sock_nested (newsk,SINGLE_DEPTH_NESTING ); -sctp_for_each_tx_datachunk (assoc, sctp_clear_owner_w); +sctp_for_each_tx_datachunk (assoc, true, sctp_clear_owner_w);sctp_assoc_migrate (assoc, newsk); -sctp_for_each_tx_datachunk (assoc, sctp_set_owner_w); +sctp_for_each_tx_datachunk (assoc, false, sctp_set_owner_w);/* If the association on the newsk is already closed before accept() * is called, set RCV_SHUTDOWN flag.