sunrpc: fix handling of server side tls alerts
Subsystems (5)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Olga Kornievskaia
reviewerPATCH 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.
--- net/sunrpc/svcsock.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-)
net/sunrpc/svcsock.c +35 -8
@@ -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
Olga Kornievskaia
reviewerPATCH 2/4 sunrpc: fix client side handling of tls alerts
In reply to a message not in the archiveA 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.
--- net/sunrpc/xprtsock.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-)
net/sunrpc/xprtsock.c +30 -10
@@ -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
Olga Kornievskaia
reviewerPATCH 3/4 nvme: fix handling of tls alerts
In reply to a message not in the archiveRevert 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.
--- drivers/nvme/target/tcp.c | 48 +++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 10 deletions(-)
drivers/nvme/target/tcp.c +38 -10
@@ -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