Skip to contentSkip to conversation

sctp: walk the list of asoc safely

PATCH In review Greg Kroah-Hartman 4 participants 6 messages Feb 1, 2019

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

PATCH 1/1 sctp: walk the list of asoc safely

In sctp_sendmesg(), when walking the list of endpoint associations, the association can be dropped from the list, making the list corrupt. Properly handle this by using list_for_each_entry_safe()

Fixes 4910280503f3 ("sctp: add support for snd flag SCTP_SENDALL process in sendmsg")
net/sctp/socket.c +2 -2
index f93c3cf9e567..65d6d04546ae 100644
@@ -2027,7 +2027,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 	struct sctp_endpoint *ep = sctp_sk(sk)->ep;
 	struct sctp_transport *transport = NULL;
 	struct sctp_sndrcvinfo _sinfo, *sinfo;
-	struct sctp_association *asoc;
+	struct sctp_association *asoc, *tmp;
 	struct sctp_cmsgs cmsgs;
 	union sctp_addr *daddr;
 	bool new = false;
@@ -2053,7 +2053,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 
 	/* SCTP_SENDALL process */
 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
-		list_for_each_entry(asoc, &ep->asocs, asocs) {
+		list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
 			err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
 							msg_len);
 			if (err = 0)
#1 lore raw

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)

Quoted 23 lines · Greg Kroah-Hartman

In sctp_sendmesg(), when walking the list of endpoint associations, the association can be dropped from the list, making the list corrupt. Properly handle this by using list_for_each_entry_safe()

Fixes 4910280503f3 ("sctp: add support for snd flag SCTP_SENDALL process in sendmsg")
net/sctp/socket.c +1 -1
index f93c3cf9e567..65d6d04546ae 100644
@@ -2027,7 +2027,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 	struct sctp_endpoint *ep = sctp_sk(sk)->ep;
 	struct sctp_transport *transport = NULL;
 	struct sctp_sndrcvinfo _sinfo, *sinfo;
-	struct sctp_association *asoc;
+	struct sctp_association *asoc, *tmp;
 	struct sctp_cmsgs cmsgs;
 	union sctp_addr *daddr;
 	bool new = false;
@@ -2053,7 +2053,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)

Extending the context here by 1 line:

        lock_sock(sk);

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) · line 2056

Quoted 5 lines
net/sctp/socket.c +1 -1
 	/* SCTP_SENDALL process */
 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
-		list_for_each_entry(asoc, &ep->asocs, asocs) {
+		list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {

With the socket being locked by here, how can an asoc be removed while the list is being traversed? The socket lock should be protecting from it.

Quoted 3 lines
net/sctp/socket.c +0 -0
 			err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
 							msg_len);
 			if (err = 0)
#2 lore raw
Quoted 28 lines · Marcelo Ricardo Leitner

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)

Quoted 23 lines · Greg Kroah-Hartman

In sctp_sendmesg(), when walking the list of endpoint associations, the association can be dropped from the list, making the list corrupt. Properly handle this by using list_for_each_entry_safe()

Fixes 4910280503f3 ("sctp: add support for snd flag SCTP_SENDALL process in sendmsg")
net/sctp/socket.c +1 -1
index f93c3cf9e567..65d6d04546ae 100644
@@ -2027,7 +2027,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 	struct sctp_endpoint *ep = sctp_sk(sk)->ep;
 	struct sctp_transport *transport = NULL;
 	struct sctp_sndrcvinfo _sinfo, *sinfo;
-	struct sctp_association *asoc;
+	struct sctp_association *asoc, *tmp;
 	struct sctp_cmsgs cmsgs;
 	union sctp_addr *daddr;
 	bool new = false;
@@ -2053,7 +2053,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)

Extending the context here by 1 line:

        lock_sock(sk);
Quoted 1 line
Quoted 8 lines

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) · line 2056

Quoted 4 lines
net/sctp/socket.c +1 -1
 	/* SCTP_SENDALL process */
 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
-		list_for_each_entry(asoc, &ep->asocs, asocs) {
+		list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {

With the socket being locked by here, how can an asoc be removed while the list is being traversed? The socket lock should be protecting from it.

What about when the SCTP_ABORT flag is set with SCTP_SENDALL at the same time?

:(

maintainer Acked-by
#3 lore raw
Quoted 38 lines · Greg Kroah-Hartman
Quoted 28 lines · Marcelo Ricardo Leitner

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)

Quoted 23 lines · Greg Kroah-Hartman

In sctp_sendmesg(), when walking the list of endpoint associations, the association can be dropped from the list, making the list corrupt. Properly handle this by using list_for_each_entry_safe()

Fixes 4910280503f3 ("sctp: add support for snd flag SCTP_SENDALL process in sendmsg")
net/sctp/socket.c +1 -1
index f93c3cf9e567..65d6d04546ae 100644
@@ -2027,7 +2027,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 	struct sctp_endpoint *ep = sctp_sk(sk)->ep;
 	struct sctp_transport *transport = NULL;
 	struct sctp_sndrcvinfo _sinfo, *sinfo;
-	struct sctp_association *asoc;
+	struct sctp_association *asoc, *tmp;
 	struct sctp_cmsgs cmsgs;
 	union sctp_addr *daddr;
 	bool new = false;
@@ -2053,7 +2053,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)

Extending the context here by 1 line:

        lock_sock(sk);
Quoted 1 line
Quoted 8 lines

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) · line 2056

Quoted 4 lines
net/sctp/socket.c +1 -1
 	/* SCTP_SENDALL process */
 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
-		list_for_each_entry(asoc, &ep->asocs, asocs) {
+		list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {

With the socket being locked by here, how can an asoc be removed while the list is being traversed? The socket lock should be protecting from it.

Quoted 4 lines

What about when the SCTP_ABORT flag is set with SCTP_SENDALL at the same time?

:(

Good point! Thanks.

Acked-by on v1 1/1
#4 lore raw
In reply to Greg Kroah-Hartman
Quoted 24 lines · Greg Kroah-Hartman

In sctp_sendmesg(), when walking the list of endpoint associations, the association can be dropped from the list, making the list corrupt. Properly handle this by using list_for_each_entry_safe()

Fixes 4910280503f3 ("sctp: add support for snd flag SCTP_SENDALL process in sendmsg")
net/sctp/socket.c +1 -1
index f93c3cf9e567..65d6d04546ae 100644
@@ -2027,7 +2027,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 	struct sctp_endpoint *ep = sctp_sk(sk)->ep;
 	struct sctp_transport *transport = NULL;
 	struct sctp_sndrcvinfo _sinfo, *sinfo;
-	struct sctp_association *asoc;
+	struct sctp_association *asoc, *tmp;
 	struct sctp_cmsgs cmsgs;
 	union sctp_addr *daddr;
 	bool new = false;

net/sctp/socket.c static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) · line 2059

Quoted 8 lines
net/sctp/socket.c +1 -1
@@ -2053,7 +2053,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 
 	/* SCTP_SENDALL process */
 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
-		list_for_each_entry(asoc, &ep->asocs, asocs) {
+		list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
 			err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
 							msg_len);
 			if (err = 0)
maintainer
#5 lore raw
In reply to Greg Kroah-Hartman

From: Greg Kroah-Hartman <[email protected]>
Date: Fri, 1 Feb 2019 15:15:22 +0100

Quoted 8 lines

In sctp_sendmesg(), when walking the list of endpoint associations, the association can be dropped from the list, making the list corrupt. Properly handle this by using list_for_each_entry_safe()

Fixes 4910280503f3 ("sctp: add support for snd flag SCTP_SENDALL process in sendmsg")

Applied and queued up for -stable.