Skip to contentSkip to conversation

sunrpc: fix handling of server side tls alerts

PATCH In review Olga Kornievskaia 3 participants 7 messages Jul 29, 2025 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Olga Kornievskaia

author reviewer
#1 lore raw

PATCH 1/4 sunrpc: fix handling of server side tls alerts

Scott Mayhew discovered a security exploit in NFS over TLS in tls_alert_recv() due to its assumption it can read data from the msg iterator's kvec..

kTLS implementation splits TLS non-data record payload between the control message buffer (which includes the type such as TLS aler or TLS cipher change) and the rest of the payload (say TLS alert's level/description) which goes into the msg payload buffer.

This patch proposes to rework how control messages are setup and used by sock_recvmsg().

If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, NFS can setup a kvec backed msg buffer and read in the control message such as a TLS alert. Msg iterator can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes 5e052dda121e ("SUNRPC: Recognize control messages in server-side TCP socket code")
---
 net/sunrpc/svcsock.c | 43 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)
net/sunrpc/svcsock.c +35 -8
index 46c156b121db..e2c5e0e626f9 100644
@@ -257,20 +257,47 @@ svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
 }
 
 static int
-svc_tcp_sock_recv_cmsg(struct svc_sock *svsk, struct msghdr *msg)
+svc_tcp_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags)
 {
 	union {
 		struct cmsghdr	cmsg;
 		u8		buf[CMSG_SPACE(sizeof(u8))];
 	} u;
-	struct socket *sock = svsk->sk_sock;
+	u8 alert[2];
+	struct kvec alert_kvec = {
+		.iov_base = alert,
+		.iov_len = sizeof(alert),
+	};
+	struct msghdr msg = {
+		.msg_flags = *msg_flags,
+		.msg_control = &u,
+		.msg_controllen = sizeof(u),
+	};
+	int ret;
+
+	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
+		      alert_kvec.iov_len);
+	ret = sock_recvmsg(sock, &msg, MSG_DONTWAIT);
+	if (ret > 0 &&
+	    tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+		iov_iter_revert(&msg.msg_iter, ret);
+		ret = svc_tcp_sock_process_cmsg(sock, &msg, &u.cmsg, -EAGAIN);
+	}
+	return ret;
+}
+
+static int
+svc_tcp_sock_recvmsg(struct svc_sock *svsk, struct msghdr *msg)
+{
 	int ret;
+	struct socket *sock = svsk->sk_sock;
 
-	msg->msg_control = &u;
-	msg->msg_controllen = sizeof(u);
 	ret = sock_recvmsg(sock, msg, MSG_DONTWAIT);
-	if (unlikely(msg->msg_controllen != sizeof(u)))
-		ret = svc_tcp_sock_process_cmsg(sock, msg, &u.cmsg, ret);
+	if (msg->msg_flags & MSG_CTRUNC) {
+		msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
+		if (ret == 0 || ret == -EIO)
+			ret = svc_tcp_sock_recv_cmsg(sock, &msg->msg_flags);
+	}
 	return ret;
 }
 
@@ -321,7 +348,7 @@ static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
 		iov_iter_advance(&msg.msg_iter, seek);
 		buflen -= seek;
 	}
-	len = svc_tcp_sock_recv_cmsg(svsk, &msg);
+	len = svc_tcp_sock_recvmsg(svsk, &msg);
 	if (len > 0)
 		svc_flush_bvec(bvec, len, seek);
 
@@ -1018,7 +1045,7 @@ static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
 		iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
 		iov.iov_len  = want;
 		iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
-		len = svc_tcp_sock_recv_cmsg(svsk, &msg);
+		len = svc_tcp_sock_recvmsg(svsk, &msg);
 		if (len < 0)
 			return len;
 		svsk->sk_tcplen += len;
Signature · 1 line
2.47.1
#2 lore raw
Quoted 70 lines · Olga Kornievskaia

Scott Mayhew discovered a security exploit in NFS over TLS in tls_alert_recv() due to its assumption it can read data from the msg iterator's kvec..

kTLS implementation splits TLS non-data record payload between the control message buffer (which includes the type such as TLS aler or TLS cipher change) and the rest of the payload (say TLS alert's level/description) which goes into the msg payload buffer.

This patch proposes to rework how control messages are setup and used by sock_recvmsg().

If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, NFS can setup a kvec backed msg buffer and read in the control message such as a TLS alert. Msg iterator can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes 5e052dda121e ("SUNRPC: Recognize control messages in server-side TCP socket code")
---
 net/sunrpc/svcsock.c | 43 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)
net/sunrpc/svcsock.c +26 -2
index 46c156b121db..e2c5e0e626f9 100644
@@ -257,20 +257,47 @@ svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
 }
 
 static int
-svc_tcp_sock_recv_cmsg(struct svc_sock *svsk, struct msghdr *msg)
+svc_tcp_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags)
 {
 	union {
 		struct cmsghdr	cmsg;
 		u8		buf[CMSG_SPACE(sizeof(u8))];
 	} u;
-	struct socket *sock = svsk->sk_sock;
+	u8 alert[2];
+	struct kvec alert_kvec = {
+		.iov_base = alert,
+		.iov_len = sizeof(alert),
+	};
+	struct msghdr msg = {
+		.msg_flags = *msg_flags,
+		.msg_control = &u,
+		.msg_controllen = sizeof(u),
+	};
+	int ret;
+
+	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
+		      alert_kvec.iov_len);
+	ret = sock_recvmsg(sock, &msg, MSG_DONTWAIT);
+	if (ret > 0 &&
+	    tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+		iov_iter_revert(&msg.msg_iter, ret);
+		ret = svc_tcp_sock_process_cmsg(sock, &msg, &u.cmsg, -EAGAIN);
+	}
+	return ret;
+}
+
+static int
+svc_tcp_sock_recvmsg(struct svc_sock *svsk, struct msghdr *msg)

net/sunrpc/svcsock.c svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg, · line 296

Quoted 10 lines
net/sunrpc/svcsock.c +3 -4
+{
 	int ret;
+	struct socket *sock = svsk->sk_sock;
 
-	msg->msg_control = &u;
-	msg->msg_controllen = sizeof(u);
 	ret = sock_recvmsg(sock, msg, MSG_DONTWAIT);
-	if (unlikely(msg->msg_controllen != sizeof(u)))
-		ret = svc_tcp_sock_process_cmsg(sock, msg, &u.cmsg, ret);
+	if (msg->msg_flags & MSG_CTRUNC) {

Nit: can we leave the unlikely() here?

Quoted 25 lines
net/sunrpc/svcsock.c +6 -2
+		msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
+		if (ret == 0 || ret == -EIO)
+			ret = svc_tcp_sock_recv_cmsg(sock, &msg->msg_flags);
+	}
 	return ret;
 }
 
@@ -321,7 +348,7 @@ static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
 		iov_iter_advance(&msg.msg_iter, seek);
 		buflen -= seek;
 	}
-	len = svc_tcp_sock_recv_cmsg(svsk, &msg);
+	len = svc_tcp_sock_recvmsg(svsk, &msg);
 	if (len > 0)
 		svc_flush_bvec(bvec, len, seek);
 
@@ -1018,7 +1045,7 @@ static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
 		iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
 		iov.iov_len  = want;
 		iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
-		len = svc_tcp_sock_recv_cmsg(svsk, &msg);
+		len = svc_tcp_sock_recvmsg(svsk, &msg);
 		if (len < 0)
 			return len;
 		svsk->sk_tcplen += len;
Signature · 1 line
Chuck Lever
author reviewer
#3 lore raw

@[email protected] I apologize that git send-mail went to stable email. Please ignore this email as this is an internal discussion for now. But will be public soon.

Quoted 76 lines · Chuck Lever
Quoted 74 lines · Olga Kornievskaia

Scott Mayhew discovered a security exploit in NFS over TLS in tls_alert_recv() due to its assumption it can read data from the msg iterator's kvec..

kTLS implementation splits TLS non-data record payload between the control message buffer (which includes the type such as TLS aler or TLS cipher change) and the rest of the payload (say TLS alert's level/description) which goes into the msg payload buffer.

This patch proposes to rework how control messages are setup and used by sock_recvmsg().

If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, NFS can setup a kvec backed msg buffer and read in the control message such as a TLS alert. Msg iterator can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes 5e052dda121e ("SUNRPC: Recognize control messages in server-side TCP socket code")
---
 net/sunrpc/svcsock.c | 43 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)
net/sunrpc/svcsock.c +28 -2
index 46c156b121db..e2c5e0e626f9 100644
@@ -257,20 +257,47 @@ svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
 }
 
 static int
-svc_tcp_sock_recv_cmsg(struct svc_sock *svsk, struct msghdr *msg)
+svc_tcp_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags)
 {
      union {
              struct cmsghdr  cmsg;
              u8              buf[CMSG_SPACE(sizeof(u8))];
      } u;
-     struct socket *sock = svsk->sk_sock;
+     u8 alert[2];
+     struct kvec alert_kvec = {
+             .iov_base = alert,
+             .iov_len = sizeof(alert),
+     };
+     struct msghdr msg = {
+             .msg_flags = *msg_flags,
+             .msg_control = &u,
+             .msg_controllen = sizeof(u),
+     };
+     int ret;
+
+     iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
+                   alert_kvec.iov_len);
+     ret = sock_recvmsg(sock, &msg, MSG_DONTWAIT);
+     if (ret > 0 &&
+         tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+             iov_iter_revert(&msg.msg_iter, ret);
+             ret = svc_tcp_sock_process_cmsg(sock, &msg, &u.cmsg, -EAGAIN);
+     }
+     return ret;
+}
+
+static int
+svc_tcp_sock_recvmsg(struct svc_sock *svsk, struct msghdr *msg)
+{
      int ret;
+     struct socket *sock = svsk->sk_sock;
 
Quoted 8 lines

net/sunrpc/svcsock.c svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg, · line 296

Quoted 6 lines
net/sunrpc/svcsock.c +1 -4
-     msg->msg_control = &u;
-     msg->msg_controllen = sizeof(u);
      ret = sock_recvmsg(sock, msg, MSG_DONTWAIT);
-     if (unlikely(msg->msg_controllen != sizeof(u)))
-             ret = svc_tcp_sock_process_cmsg(sock, msg, &u.cmsg, ret);
+     if (msg->msg_flags & MSG_CTRUNC) {

Nit: can we leave the unlikely() here?

Will do.

Quoted 32 lines
Quoted 19 lines
net/sunrpc/svcsock.c +5 -1
+             msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
+             if (ret == 0 || ret == -EIO)
+                     ret = svc_tcp_sock_recv_cmsg(sock, &msg->msg_flags);
+     }
      return ret;
 }
 
@@ -321,7 +348,7 @@ static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
              iov_iter_advance(&msg.msg_iter, seek);
              buflen -= seek;
      }
-     len = svc_tcp_sock_recv_cmsg(svsk, &msg);
+     len = svc_tcp_sock_recvmsg(svsk, &msg);
      if (len > 0)
              svc_flush_bvec(bvec, len, seek);
 

net/sunrpc/svcsock.c static ssize_t svc_tcp_read_marker(struct svc_sock *svsk, · line 1051

Quoted 8 lines
net/sunrpc/svcsock.c +1 -1
@@ -1018,7 +1045,7 @@ static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
              iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
              iov.iov_len  = want;
              iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
-             len = svc_tcp_sock_recv_cmsg(svsk, &msg);
+             len = svc_tcp_sock_recvmsg(svsk, &msg);
              if (len < 0)
                      return len;
              svsk->sk_tcplen += len;

--
Chuck Lever

author reviewer
#4 lore raw

PATCH 2/4 sunrpc: fix client side handling of tls alerts

In reply to a message not in the archive

A security exploit was discovered in NFS over TLS in tls_alert_recv due to its assumption that there is valid data in the msghdr's iterator's kvec.

Instead, this patch proposes the rework how control messages are setup and used by sock_recvmsg().

If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, NFS can setup a kvec backed control buffer and read in the control message such as a TLS alert. Msg iterator can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes dea034b963c8 ("SUNRPC: Capture CMSG metadata on client-side receive")
---
 net/sunrpc/xprtsock.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
net/sunrpc/xprtsock.c +30 -10
index 04ff66758fc3..c5f7bbf5775f 100644
@@ -358,7 +358,7 @@ xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
 
 static int
 xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
-		     struct cmsghdr *cmsg, int ret)
+		     unsigned int *msg_flags, struct cmsghdr *cmsg, int ret)
 {
 	u8 content_type = tls_get_record_type(sock->sk, cmsg);
 	u8 level, description;
@@ -371,7 +371,7 @@ xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
 		 * record, even though there might be more frames
 		 * waiting to be decrypted.
 		 */
-		msg->msg_flags &= ~MSG_EOR;
+		*msg_flags &= ~MSG_EOR;
 		break;
 	case TLS_RECORD_TYPE_ALERT:
 		tls_alert_recv(sock->sk, msg, &level, &description);
@@ -386,19 +386,33 @@ xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
 }
 
 static int
-xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags)
+xs_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags, int flags)
 {
 	union {
 		struct cmsghdr	cmsg;
 		u8		buf[CMSG_SPACE(sizeof(u8))];
 	} u;
+	u8 alert[2];
+	struct kvec alert_kvec = {
+		.iov_base = alert,
+		.iov_len = sizeof(alert),
+	};
+	struct msghdr msg = {
+		.msg_flags = *msg_flags,
+		.msg_control = &u,
+		.msg_controllen = sizeof(u),
+	};
 	int ret;
 
-	msg->msg_control = &u;
-	msg->msg_controllen = sizeof(u);
-	ret = sock_recvmsg(sock, msg, flags);
-	if (msg->msg_controllen != sizeof(u))
-		ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret);
+	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
+		      alert_kvec.iov_len);
+	ret = sock_recvmsg(sock, &msg, flags);
+	if (ret > 0 &&
+	    tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+		iov_iter_revert(&msg.msg_iter, ret);
+		ret = xs_sock_process_cmsg(sock, &msg, msg_flags, &u.cmsg,
+					   -EAGAIN);
+	}
 	return ret;
 }
 
@@ -408,7 +422,13 @@ xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
 	ssize_t ret;
 	if (seek != 0)
 		iov_iter_advance(&msg->msg_iter, seek);
-	ret = xs_sock_recv_cmsg(sock, msg, flags);
+	ret = sock_recvmsg(sock, msg, flags);
+	/* Handle TLS inband control message lazily */
+	if (msg->msg_flags & MSG_CTRUNC) {
+		msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
+		if (ret == 0 || ret == -EIO)
+			ret = xs_sock_recv_cmsg(sock, &msg->msg_flags, flags);
+	}
 	return ret > 0 ? ret + seek : ret;
 }
 
@@ -434,7 +454,7 @@ xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
 		size_t count)
 {
 	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
-	return xs_sock_recv_cmsg(sock, msg, flags);
+	return xs_sock_recvmsg(sock, msg, flags, 0);
 }
 
 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
Signature · 1 line
2.47.1
author reviewer
#5 lore raw

PATCH 3/4 nvme: fix handling of tls alerts

In reply to a message not in the archive

Revert kvec msg iterator before trying to process a TLS alert when possible.

In nvmet_tcp_try_recv_data(), it's assumed that no msg control message buffer is set prior to sock_recvmsg(). If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, we setup a kvec backed control buffer and read in the control message such as a TLS alert. Msg can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes a1c5dd8355b1 ("nvmet-tcp: control messages for recvmsg()")
---
 drivers/nvme/target/tcp.c | 48 +++++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 10 deletions(-)
drivers/nvme/target/tcp.c +38 -10
index 688033b88d38..cf3336ddc9a3 100644
@@ -1161,6 +1161,7 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
 	if (unlikely(len < 0))
 		return len;
 	if (queue->tls_pskid) {
+		iov_iter_revert(&msg.msg_iter, len);
 		ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
 		if (ret < 0)
 			return ret;
@@ -1218,18 +1219,47 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
 {
 	struct nvmet_tcp_cmd  *cmd = queue->cmd;
 	int len, ret;
+	union {
+		struct cmsghdr cmsg;
+		u8 buf[CMSG_SPACE(sizeof(u8))];
+	} u;
+	u8 alert[2];
+	struct kvec alert_kvec = {
+		.iov_base = alert,
+		.iov_len = sizeof(alert),
+	};
+	struct msghdr msg = {
+		.msg_control = &u,
+		.msg_controllen = sizeof(u),
+	};
 
 	while (msg_data_left(&cmd->recv_msg)) {
+		/* assumed that cmg->recv_msg's control buffer is not setup
+		 */
+		WARN_ON_ONCE(cmd->recv_msg.msg_controllen > 0);
+
 		len = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
 			cmd->recv_msg.msg_flags);
+		if (cmd->recv_msg.msg_flags & MSG_CTRUNC) {
+			cmd->recv_msg.msg_flags &= ~(MSG_CTRUNC | MSG_EOF);
+			if (len == 0 || len == -EIO) {
+				iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec,
+					      1, alert_kvec.iov_len);
+				len = sock_recvmsg(cmd->queue->sock, &msg,
+						   MSG_DONTWAIT);
+				if (len > 0 &&
+				    tls_get_record_type(cmd->queue->sock->sk,
+					    &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+					iov_iter_revert(&msg.msg_iter, len);
+					ret = nvmet_tcp_tls_record_ok(cmd->queue,
+							&msg, u.buf);
+					if (ret < 0)
+						return ret;
+				}
+			}
+		}
 		if (len <= 0)
 			return len;
-		if (queue->tls_pskid) {
-			ret = nvmet_tcp_tls_record_ok(cmd->queue,
-					&cmd->recv_msg, cmd->recv_cbuf);
-			if (ret < 0)
-				return ret;
-		}
 
 		cmd->pdu_recv += len;
 		cmd->rbytes_done += len;
@@ -1267,6 +1297,7 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
 	if (unlikely(len < 0))
 		return len;
 	if (queue->tls_pskid) {
+		iov_iter_revert(&msg.msg_iter, len);
 		ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
 		if (ret < 0)
 			return ret;
@@ -1453,10 +1484,6 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
 	if (!c->r2t_pdu)
 		goto out_free_data;
 
-	if (queue->state == NVMET_TCP_Q_TLS_HANDSHAKE) {
-		c->recv_msg.msg_control = c->recv_cbuf;
-		c->recv_msg.msg_controllen = sizeof(c->recv_cbuf);
-	}
 	c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
 
 	list_add_tail(&c->entry, &queue->free_list);
@@ -1736,6 +1763,7 @@ static int nvmet_tcp_try_peek_pdu(struct nvmet_tcp_queue *queue)
 		return len;
 	}
 
+	iov_iter_revert(&msg.msg_iter, len);
 	ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.47.1
#6 lore raw

Re: [PATCH 3/4] nvme: fix handling of tls alerts

Quoted 48 lines · Olga Kornievskaia

Revert kvec msg iterator before trying to process a TLS alert when possible.

In nvmet_tcp_try_recv_data(), it's assumed that no msg control message buffer is set prior to sock_recvmsg(). If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, we setup a kvec backed control buffer and read in the control message such as a TLS alert. Msg can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes a1c5dd8355b1 ("nvmet-tcp: control messages for recvmsg()")
---
  drivers/nvme/target/tcp.c | 48 +++++++++++++++++++++++++++++++--------
  1 file changed, 38 insertions(+), 10 deletions(-)
drivers/nvme/target/tcp.c +13 -0
index 688033b88d38..cf3336ddc9a3 100644
@@ -1161,6 +1161,7 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
  	if (unlikely(len < 0))
  		return len;
  	if (queue->tls_pskid) {
+		iov_iter_revert(&msg.msg_iter, len);
  		ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
  		if (ret < 0)
  			return ret;
@@ -1218,18 +1219,47 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
  {
  	struct nvmet_tcp_cmd  *cmd = queue->cmd;
  	int len, ret;
+	union {
+		struct cmsghdr cmsg;
+		u8 buf[CMSG_SPACE(sizeof(u8))];
+	} u;
+	u8 alert[2];
+	struct kvec alert_kvec = {
+		.iov_base = alert,
+		.iov_len = sizeof(alert),
+	};
+	struct msghdr msg = {
+		.msg_control = &u,
+		.msg_controllen = sizeof(u),

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1243

Quoted 10 lines
drivers/nvme/target/tcp.c +6 -0
+	};
 
  	while (msg_data_left(&cmd->recv_msg)) {
+		/* assumed that cmg->recv_msg's control buffer is not setup
+		 */
+		WARN_ON_ONCE(cmd->recv_msg.msg_controllen > 0);
+
  		len = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
  			cmd->recv_msg.msg_flags);
+		if (cmd->recv_msg.msg_flags & MSG_CTRUNC) {

Hmm. Looks as if we were getting MSG_CTRUNC even if no buffer is passed. OK.

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1244

Quoted 1 line
drivers/nvme/target/tcp.c +1 -0
+			cmd->recv_msg.msg_flags &= ~(MSG_CTRUNC | MSG_EOF);

Not sure with this. We had _terrible_ issues with MSG_EOF not set correctly (basically the TLS layer would wait for more data to be received before the record is shipped out, causing massive delays and connection resets).
Any reason for clearing MSG_EOF here?

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1255

Quoted 11 lines
drivers/nvme/target/tcp.c +11 -0
+			if (len == 0 || len == -EIO) {
+				iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec,
+					      1, alert_kvec.iov_len);
+				len = sock_recvmsg(cmd->queue->sock, &msg,
+						   MSG_DONTWAIT);
+				if (len > 0 &&
+				    tls_get_record_type(cmd->queue->sock->sk,
+					    &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+					iov_iter_revert(&msg.msg_iter, len);
+					ret = nvmet_tcp_tls_record_ok(cmd->queue,
+							&msg, u.buf);

Can't we just skip this part (ie _not_ receiving / reading the control message contents? It's not that we're not doing anything useful here; for any TLS Alert we'll be resetting the connection.

Quoted 36 lines
drivers/nvme/target/tcp.c +6 -10
+					if (ret < 0)
+						return ret;
+				}
+			}
+		}
  		if (len <= 0)
  			return len;
-		if (queue->tls_pskid) {
-			ret = nvmet_tcp_tls_record_ok(cmd->queue,
-					&cmd->recv_msg, cmd->recv_cbuf);
-			if (ret < 0)
-				return ret;
-		}
 
  		cmd->pdu_recv += len;
  		cmd->rbytes_done += len;
@@ -1267,6 +1297,7 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
  	if (unlikely(len < 0))
  		return len;
  	if (queue->tls_pskid) {
+		iov_iter_revert(&msg.msg_iter, len);
  		ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
  		if (ret < 0)
  			return ret;
@@ -1453,10 +1484,6 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
  	if (!c->r2t_pdu)
  		goto out_free_data;
 
-	if (queue->state == NVMET_TCP_Q_TLS_HANDSHAKE) {
-		c->recv_msg.msg_control = c->recv_cbuf;
-		c->recv_msg.msg_controllen = sizeof(c->recv_cbuf);
-	}
  	c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
 
  	list_add_tail(&c->entry, &queue->free_list);

drivers/nvme/target/tcp.c static int nvmet_tcp_try_peek_pdu(struct nvmet_tcp_queue *queue) · line 1769

Quoted 7 lines
drivers/nvme/target/tcp.c +1 -0
@@ -1736,6 +1763,7 @@ static int nvmet_tcp_try_peek_pdu(struct nvmet_tcp_queue *queue)
  		return len;
  	}
 
+	iov_iter_revert(&msg.msg_iter, len);
  	ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
  	if (ret < 0)
  		return ret;

Huh? Why do we need to do that?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author reviewer
#7 lore raw

Re: [PATCH 3/4] nvme: fix handling of tls alerts

@[email protected] I apologize that git send-mail went to stable email. Please ignore this email as this is an internal discussion for now. But will be public soon.

Quoted 52 lines · Hannes Reinecke
Quoted 50 lines · Olga Kornievskaia

Revert kvec msg iterator before trying to process a TLS alert when possible.

In nvmet_tcp_try_recv_data(), it's assumed that no msg control message buffer is set prior to sock_recvmsg(). If no control message structure is setup, kTLS layer will read and process TLS data record types. As soon as it encounters a TLS control message, it would return an error. At that point, we setup a kvec backed control buffer and read in the control message such as a TLS alert. Msg can advance the kvec pointer as a part of the copy process thus we need to revert the iterator before calling into the tls_alert_recv.

Fixes a1c5dd8355b1 ("nvmet-tcp: control messages for recvmsg()")
---
  drivers/nvme/target/tcp.c | 48 +++++++++++++++++++++++++++++++--------
  1 file changed, 38 insertions(+), 10 deletions(-)
drivers/nvme/target/tcp.c +14 -0
index 688033b88d38..cf3336ddc9a3 100644
@@ -1161,6 +1161,7 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
      if (unlikely(len < 0))
              return len;
      if (queue->tls_pskid) {
+             iov_iter_revert(&msg.msg_iter, len);
              ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
              if (ret < 0)
                      return ret;
@@ -1218,18 +1219,47 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
  {
      struct nvmet_tcp_cmd  *cmd = queue->cmd;
      int len, ret;
+     union {
+             struct cmsghdr cmsg;
+             u8 buf[CMSG_SPACE(sizeof(u8))];
+     } u;
+     u8 alert[2];
+     struct kvec alert_kvec = {
+             .iov_base = alert,
+             .iov_len = sizeof(alert),
+     };
+     struct msghdr msg = {
+             .msg_control = &u,
+             .msg_controllen = sizeof(u),
+     };
 
Quoted 11 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1243

Quoted 8 lines
drivers/nvme/target/tcp.c +5 -0
      while (msg_data_left(&cmd->recv_msg)) {
+             /* assumed that cmg->recv_msg's control buffer is not setup
+              */
+             WARN_ON_ONCE(cmd->recv_msg.msg_controllen > 0);
+
              len = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
                      cmd->recv_msg.msg_flags);
+             if (cmd->recv_msg.msg_flags & MSG_CTRUNC) {

Hmm. Looks as if we were getting MSG_CTRUNC even if no buffer is passed. OK.

Correct. When no control buffer is supplied, put_cmsg() would always set MSG_CTRUNC. What's important is that in combination with -EIO return that signals it's a TLS control message. Just looking at CTRUNC message would get us into trouble.

Quoted 8 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1244

Quoted 2 lines
drivers/nvme/target/tcp.c +1 -0
+                     cmd->recv_msg.msg_flags &= ~(MSG_CTRUNC | MSG_EOF);

Not sure with this. We had _terrible_ issues with MSG_EOF not set correctly (basically the TLS layer would wait for more data to be received before the record is shipped out, causing massive delays and connection resets).
Any reason for clearing MSG_EOF here?

Oh my, thank you for catching this. It should have been EOR and not EOF. As to why clearing MSG_EOR was based on a comment in NFS code that TLS always sets EOF for every data record (and looking at the TLS tls_sw_recvmsg).

Quoted 12 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1255

Quoted 12 lines
drivers/nvme/target/tcp.c +11 -0
+                     if (len == 0 || len == -EIO) {
+                             iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec,
+                                           1, alert_kvec.iov_len);
+                             len = sock_recvmsg(cmd->queue->sock, &msg,
+                                                MSG_DONTWAIT);
+                             if (len > 0 &&
+                                 tls_get_record_type(cmd->queue->sock->sk,
+                                         &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
+                                     iov_iter_revert(&msg.msg_iter, len);
+                                     ret = nvmet_tcp_tls_record_ok(cmd->queue,
+                                                     &msg, u.buf);
Quoted 3 lines

Can't we just skip this part (ie _not_ receiving / reading the control message contents? It's not that we're not doing anything useful here; for any TLS Alert we'll be resetting the connection.

Yes we certainly can do something like what you had previously if (len == 0 || len == -EIO) {
pr_err("queue %d: unhandled control message\n", queue->idx); return -EAGAIN;
}

But not that in that case TLS alert bytes would still be unconsumed staying in the socket. If what follows is a prompt closing of the connection, then who cares. What has been worrying me (even in the NFS) code is what happens when TLS alert (though we do consume it) isn't followed by the connection closing.

Now let me know if you want me to re-do this patch based on what you suggested and ignore when len=-EIO. And then I can redo the patch#4 based on that.

Quoted 40 lines
Quoted 40 lines
drivers/nvme/target/tcp.c +6 -10
+                                     if (ret < 0)
+                                             return ret;
+                             }
+                     }
+             }
              if (len <= 0)
                      return len;
-             if (queue->tls_pskid) {
-                     ret = nvmet_tcp_tls_record_ok(cmd->queue,
-                                     &cmd->recv_msg, cmd->recv_cbuf);
-                     if (ret < 0)
-                             return ret;
-             }
 
              cmd->pdu_recv += len;
              cmd->rbytes_done += len;
@@ -1267,6 +1297,7 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
      if (unlikely(len < 0))
              return len;
      if (queue->tls_pskid) {
+             iov_iter_revert(&msg.msg_iter, len);
              ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
              if (ret < 0)
                      return ret;
@@ -1453,10 +1484,6 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
      if (!c->r2t_pdu)
              goto out_free_data;
 
-     if (queue->state == NVMET_TCP_Q_TLS_HANDSHAKE) {
-             c->recv_msg.msg_control = c->recv_cbuf;
-             c->recv_msg.msg_controllen = sizeof(c->recv_cbuf);
-     }
      c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
 
      list_add_tail(&c->entry, &queue->free_list);
@@ -1736,6 +1763,7 @@ static int nvmet_tcp_try_peek_pdu(struct nvmet_tcp_queue *queue)
              return len;
      }
 
Quoted 6 lines

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) · line 1167

Quoted 4 lines
drivers/nvme/target/tcp.c +1 -0
+     iov_iter_revert(&msg.msg_iter, len);
      ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
      if (ret < 0)
              return ret;

Huh? Why do we need to do that?

Why do we need an iov_iter_revert? As it's also done in the other 3 chucks.

kernel_recvmsg which ultimately calls sock_recvmsg will ultimately call into copy_to_iter() which eventually will a function to advance a kvec so when tls_alert_recv() tries to look into kvec inside the msg iterator is pointing into invalid memory.

sock_recvmsg -> inet_recvmsg ->tls_sw_recvmsg -> skb_copy_datagram_iter-> simple_copy_to_iter-> copy_to_iter ->_copy_to_iter ->iterate_and_advance ->iterate_and_advance-> ->iterate_and_advance2 ->iterate_kvec

A typical usage of sock_recvmsg is to initialize the iterator's kvec (iov_base) with some buffer but afterwards processing of that payload is done by looking thru the pointer used in the initialization. You don't look into the iterator's kvec.

Quoted 10 lines

Cheers,

Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
[email protected] +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich