net/tls: add support for limiting the max record size
Subsystems (5)
v2 cover net/tls: add support for limiting the max record size
From: Wilfred Mallawa <[email protected]>
During a handshake, an endpoint may specify a maximum record size limit. Currently, the kernel defaults to TLS_MAX_PAYLOAD_SIZE (16KB) for the maximum record size. Meaning that, outgoing records from the kernel can exceed the negotiated size during a handshake. In such a case, the TLS endpoint must send a fatal "record_overflow" alert [1], and thus the record is discarded.
Upcoming Western Digital NVMe-TCP hardware controllers implement TLS support. For these devices, supporting TLS record size negotiation is necessary because the maximum TLS record size supported by the controller is less than the default 16KB currently used by the kernel.
This patch adds support for retrieving the negotiated record size limit during a handshake, and enforcing it at the TLS layer such that outgoing records are no larger than the size negotiated. This patch depends on the respective userspace support in tlshd [2] and GnuTLS [3]. GnuTLS patches have been merged.
[1] https://www.rfc-editor.org/rfc/rfc8449
[2] https://github.com/oracle/ktls-utils/pull/112
[3] https://gitlab.com/gnutls/gnutls/-/merge_requests/2005
Wilfred Mallawa (1):
net/tls: allow limiting maximum record size
Documentation/netlink/specs/handshake.yaml | 3 +++ include/net/tls.h | 2 ++ include/uapi/linux/handshake.h | 1 + net/handshake/genl.c | 5 ++-- net/handshake/tlshd.c | 29 +++++++++++++++++++++- net/tls/tls_sw.c | 6 ++++- 6 files changed, 42 insertions(+), 4 deletions(-)
Signature · 1 line
2.50.1
RFC v2 1/1 net/tls: allow limiting maximum record size
From: Wilfred Mallawa <[email protected]>
During a handshake, an endpoint may specify a maximum record size limit. Currently, the kernel defaults to TLS_MAX_PAYLOAD_SIZE (16KB) for the maximum record size. Meaning that, the outgoing records from the kernel can exceed a lower size negotiated during the handshake. In such a case, the TLS endpoint must send a fatal "record_overflow" alert [1], and thus the record is discarded.
This patch adds support for retrieving the negotiated record size limit during a handshake, and enforcing it at the TLS layer such that outgoing records are no larger than the size negotiated. This patch depends on the respective userspace support in tlshd [2] and GnuTLS [3].
[1] https://www.rfc-editor.org/rfc/rfc8449
[2] https://github.com/oracle/ktls-utils/pull/112
[3] https://gitlab.com/gnutls/gnutls/-/merge_requests/2005
--- Documentation/netlink/specs/handshake.yaml | 3 +++ include/net/tls.h | 2 ++ include/uapi/linux/handshake.h | 1 + net/handshake/genl.c | 5 ++-- net/handshake/tlshd.c | 29 +++++++++++++++++++++- net/tls/tls_sw.c | 6 ++++- 6 files changed, 42 insertions(+), 4 deletions(-)
Documentation/netlink/specs/handshake.yaml +3 -0
@@ -84,6 +84,9 @@ attribute-sets: name: remote-auth type: u32 multi-attr: true + - + name: record-size-limit + type: u32 operations: list:
include/net/tls.h +2 -0
@@ -250,6 +250,8 @@ struct tls_context {*/ unsigned long flags; +u32 tls_record_size_limit; +/* cache cold stuff */ struct proto *sk_proto;struct sock *sk;
include/uapi/linux/handshake.h +1 -0
@@ -54,6 +54,7 @@ enum {HANDSHAKE_A_DONE_STATUS =1 ,HANDSHAKE_A_DONE_SOCKFD ,HANDSHAKE_A_DONE_REMOTE_AUTH , +HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT , __HANDSHAKE_A_DONE_MAX,HANDSHAKE_A_DONE_MAX = (__HANDSHAKE_A_DONE_MAX -1 )
net/handshake/genl.c +3 -2
@@ -16,10 +16,11 @@ static const struct nla_policy handshake_accept_nl_policy[HANDSHAKE_A_ACCEPT_HAN };/* HANDSHAKE_CMD_DONE - do */ -static const struct nla_policy handshake_done_nl_policy[HANDSHAKE_A_DONE_REMOTE_AUTH +1 ] = { +static const struct nla_policy handshake_done_nl_policy[HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT +1 ] = { [HANDSHAKE_A_DONE_STATUS ] = { .type =NLA_U32 , }, [HANDSHAKE_A_DONE_SOCKFD ] = { .type =NLA_S32 , }, [HANDSHAKE_A_DONE_REMOTE_AUTH ] = { .type =NLA_U32 , }, + [HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT ] = { .type =NLA_U32 , }, };/* Ops table for handshake */
@@ -35,7 +36,7 @@ static const struct genl_split_ops handshake_nl_ops[] = { .cmd = HANDSHAKE_CMD_DONE, .doit = handshake_nl_done_doit, .policy = handshake_done_nl_policy, - .maxattr = HANDSHAKE_A_DONE_REMOTE_AUTH, + .maxattr = HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT, .flags = GENL_CMD_CAP_DO, }, };
net/handshake/tlshd.c +28 -1
@@ -19,6 +19,7 @@#include <net/handshake.h> #include <net/genetlink.h> #include <net/tls_prot.h> +#include <net/tls.h> #include <uapi/linux/keyctl.h> #include <uapi/linux/handshake.h>
@@ -37,6 +38,8 @@ struct tls_handshake_req {key_serial_t th_certificate;key_serial_t th_privkey; +struct socket *th_sock; +unsigned int th_num_peerids;key_serial_t th_peerid[5 ]; };
@@ -52,6 +55,7 @@ tls_handshake_req_init(struct handshake_req *req, treq->th_consumer_data = args->ta_data; treq->th_peername = args->ta_peername; treq->th_keyring = args->ta_keyring; + treq->th_sock = args->ta_sock; treq->th_num_peerids =0 ; treq->th_certificate =TLS_NO_CERT ; treq->th_privkey =TLS_NO_PRIVKEY ;
@@ -85,6 +89,27 @@ static void tls_handshake_remote_peerids(struct tls_handshake_req *treq, } } +static void tls_handshake_record_size (struct tls_handshake_req *treq, +struct genl_info *info) +{ +struct tls_context *tls_ctx; +struct nlattr *head =nlmsg_attrdata (info->nlhdr,GENL_HDRLEN ); +struct nlattr *nla; +u32 record_size_limit; +int rem, len =nlmsg_attrlen (info->nlhdr,GENL_HDRLEN ); + +nla_for_each_attr (nla, head, len, rem) { +if (nla_type (nla) ==HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT ) { + record_size_limit =nla_get_u32 (nla); +if (treq->th_sock) { + tls_ctx =tls_get_ctx (treq->th_sock->sk); + tls_ctx->tls_record_size_limit = record_size_limit; + } +break ; + } + } +} +/** * tls_handshake_done - callback to handle a CMD_DONE request * @req: socket on which the handshake was performed
@@ -98,8 +123,10 @@ static void tls_handshake_done(struct handshake_req *req,struct tls_handshake_req *treq =handshake_req_private (req); treq->th_peerid[0 ] =TLS_NO_PEERID ; -if (info) +if (info) {tls_handshake_remote_peerids (treq, info); +tls_handshake_record_size (treq, info); + }if (!status)set_bit (HANDSHAKE_F_REQ_SESSION , &req->hr_flags);
net/tls/tls_sw.c +5 -1
@@ -1024,6 +1024,7 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,ssize_t copied =0 ;struct sk_msg *msg_pl, *msg_en;struct tls_rec *rec; +u32 tls_record_size_limit;int required_size;int num_async =0 ;bool full_record;
@@ -1045,6 +1046,9 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, } } + tls_record_size_limit =min_not_zero (tls_ctx->tls_record_size_limit, +TLS_MAX_PAYLOAD_SIZE ); +while (msg_data_left (msg)) {if (sk->sk_err) { ret = -sk->sk_err;
@@ -1066,7 +1070,7 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, orig_size = msg_pl->sg.size; full_record = false; try_to_copy =msg_data_left (msg); - record_room =TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size; + record_room = tls_record_size_limit - msg_pl->sg.size;if (try_to_copy >= record_room) { try_to_copy = record_room; full_record = true;
Signature · 1 line
2.50.1
Re: [RFC v2 1/1] net/tls: allow limiting maximum record size
Quoted 72 lines · Wilfred Mallawa
From: Wilfred Mallawa <[email protected]>
During a handshake, an endpoint may specify a maximum record size limit. Currently, the kernel defaults to
TLS_MAX_PAYLOAD_SIZE(16KB) for the maximum record size. Meaning that, the outgoing records from the kernel can exceed a lower size negotiated during the handshake. In such a case, the TLS endpoint must send a fatal "record_overflow" alert [1], and thus the record is discarded.This patch adds support for retrieving the negotiated record size limit during a handshake, and enforcing it at the TLS layer such that outgoing records are no larger than the size negotiated. This patch depends on the respective userspace support in tlshd [2] and GnuTLS [3].
[1] https://www.rfc-editor.org/rfc/rfc8449
[2] https://github.com/oracle/ktls-utils/pull/112
[3] https://gitlab.com/gnutls/gnutls/-/merge_requests/2005Signed-off-by Wilfred Mallawa <[email protected]>--- Documentation/netlink/specs/handshake.yaml | 3 +++ include/net/tls.h | 2 ++ include/uapi/linux/handshake.h | 1 + net/handshake/genl.c | 5 ++-- net/handshake/tlshd.c | 29 +++++++++++++++++++++- net/tls/tls_sw.c | 6 ++++- 6 files changed, 42 insertions(+), 4 deletions(-)Documentation/netlink/specs/handshake.yaml +3
-0@@ -84,6 +84,9 @@ attribute-sets: name: remote-auth type: u32 multi-attr: true + - + name: record-size-limit + type: u32 operations: list:include/net/tls.h +2
-0@@ -250,6 +250,8 @@ struct tls_context {*/ unsigned long flags; +u32 tls_record_size_limit; +/* cache cold stuff */ struct proto *sk_proto;struct sock *sk;include/uapi/linux/handshake.h +1
-0@@ -54,6 +54,7 @@ enum {HANDSHAKE_A_DONE_STATUS =1 ,HANDSHAKE_A_DONE_SOCKFD ,HANDSHAKE_A_DONE_REMOTE_AUTH , +HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT , __HANDSHAKE_A_DONE_MAX,HANDSHAKE_A_DONE_MAX = (__HANDSHAKE_A_DONE_MAX -1 )net/handshake/genl.c +0
-0
net/handshake/genl.c static const struct nla_policy handshake_accept_nl_policy[HANDSHAKE_A_ACCEPT_HAN · line 19
Quoted 5 lines
net/handshake/genl.c +1
-1@@ -16,10 +16,11 @@ static const struct nla_policy handshake_accept_nl_policy[HANDSHAKE_A_ACCEPT_HAN };/* HANDSHAKE_CMD_DONE - do */ -static const struct nla_policy handshake_done_nl_policy[HANDSHAKE_A_DONE_REMOTE_AUTH +1 ] = { +static const struct nla_policy handshake_done_nl_policy[HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT +1 ] = {
Shouldn't that be 'HANDSHAKE_A_DONE_MAX'?
net/handshake/genl.c static const struct genl_split_ops handshake_nl_ops[] = { · line 39
Quoted 13 lines
net/handshake/genl.c +2
-1[HANDSHAKE_A_DONE_STATUS] = { .type = NLA_U32, }, [HANDSHAKE_A_DONE_SOCKFD] = { .type = NLA_S32, }, [HANDSHAKE_A_DONE_REMOTE_AUTH] = { .type = NLA_U32, }, + [HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT] = { .type = NLA_U32, }, };/* Ops table for handshake */ @@ -35,7 +36,7 @@ static const struct genl_split_ops handshake_nl_ops[] = { .cmd = HANDSHAKE_CMD_DONE, .doit = handshake_nl_done_doit, .policy = handshake_done_nl_policy, - .maxattr = HANDSHAKE_A_DONE_REMOTE_AUTH, + .maxattr = HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT,
HANDSHAKE_A_DONE_MAX - 1?
Quoted 95 lines
net/handshake/genl.c +0
-0.flags = GENL_CMD_CAP_DO, }, };net/handshake/tlshd.c +28
-1@@ -19,6 +19,7 @@#include <net/handshake.h> #include <net/genetlink.h> #include <net/tls_prot.h> +#include <net/tls.h> #include <uapi/linux/keyctl.h> #include <uapi/linux/handshake.h> @@ -37,6 +38,8 @@ struct tls_handshake_req {key_serial_t th_certificate;key_serial_t th_privkey; +struct socket *th_sock; +unsigned int th_num_peerids;key_serial_t th_peerid[5 ]; };@@ -52,6 +55,7 @@ tls_handshake_req_init(struct handshake_req *req, treq->th_consumer_data = args->ta_data; treq->th_peername = args->ta_peername; treq->th_keyring = args->ta_keyring; + treq->th_sock = args->ta_sock; treq->th_num_peerids =0 ; treq->th_certificate =TLS_NO_CERT ; treq->th_privkey =TLS_NO_PRIVKEY ;@@ -85,6 +89,27 @@ static void tls_handshake_remote_peerids(struct tls_handshake_req *treq, } } +static void tls_handshake_record_size (struct tls_handshake_req *treq, +struct genl_info *info) +{ +struct tls_context *tls_ctx; +struct nlattr *head =nlmsg_attrdata (info->nlhdr,GENL_HDRLEN ); +struct nlattr *nla; +u32 record_size_limit; +int rem, len =nlmsg_attrlen (info->nlhdr,GENL_HDRLEN ); + +nla_for_each_attr (nla, head, len, rem) { +if (nla_type (nla) ==HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT ) { + record_size_limit =nla_get_u32 (nla); +if (treq->th_sock) { + tls_ctx =tls_get_ctx (treq->th_sock->sk); + tls_ctx->tls_record_size_limit = record_size_limit; + } +break ; + } + } +} +/** * tls_handshake_done - callback to handle a CMD_DONE request * @req: socket on which the handshake was performed @@ -98,8 +123,10 @@ static void tls_handshake_done(struct handshake_req *req,struct tls_handshake_req *treq =handshake_req_private (req); treq->th_peerid[0 ] =TLS_NO_PEERID ; -if (info) +if (info) {tls_handshake_remote_peerids (treq, info); +tls_handshake_record_size (treq, info); + }if (!status)set_bit (HANDSHAKE_F_REQ_SESSION , &req->hr_flags);net/tls/tls_sw.c +4
-0@@ -1024,6 +1024,7 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,ssize_t copied =0 ;struct sk_msg *msg_pl, *msg_en;struct tls_rec *rec; +u32 tls_record_size_limit;int required_size;int num_async =0 ;bool full_record;@@ -1045,6 +1046,9 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, } } + tls_record_size_limit =min_not_zero (tls_ctx->tls_record_size_limit, +TLS_MAX_PAYLOAD_SIZE ); +while (msg_data_left (msg)) {if (sk->sk_err) { ret = -sk->sk_err;
net/tls/tls_sw.c static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, · line 1076
Quoted 8 lines
net/tls/tls_sw.c +1
-1@@ -1066,7 +1070,7 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, orig_size = msg_pl->sg.size; full_record = false; try_to_copy =msg_data_left (msg); - record_room =TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size; + record_room = tls_record_size_limit - msg_pl->sg.size;if (try_to_copy >= record_room) { try_to_copy = record_room; full_record = true;
Otherwise:
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
Re: [RFC v2 1/1] net/tls: allow limiting maximum record size
Quoted 1 line · Hannes Reinecke
[snip]
Quoted 16 lines
Quoted 16 lines
include/uapi/linux/handshake.h +1
-0@@ -54,6 +54,7 @@ enum { HANDSHAKE_A_DONE_STATUS = 1, HANDSHAKE_A_DONE_SOCKFD, HANDSHAKE_A_DONE_REMOTE_AUTH, + HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT,
__HANDSHAKE_A_DONE_MAX,HANDSHAKE_A_DONE_MAX= (__HANDSHAKE_A_DONE_MAX- 1)net/handshake/genl.c +0
-0@@ -16,10 +16,11 @@ static const struct nla_policyhandshake_accept_nl_policy[HANDSHAKE_A_ACCEPT_HAN
Quoted 9 lines
Quoted 8 lines
};
/*HANDSHAKE_CMD_DONE- do */
-static const structnla_policyhandshake_done_nl_policy[HANDSHAKE_A_DONE_REMOTE_AUTH + 1] = { +static const structnla_policyhandshake_done_nl_policy[HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT + 1] = {
Hey Hannes,
I did consider using HANDSHAKE_A_DONE_MAX, but wasn't sure if the existing convention is there for some reason. But I can switch over if you think that is best.
Quoted 13 lines
Shouldn't that be '
HANDSHAKE_A_DONE_MAX'?Quoted 11 lines
[
HANDSHAKE_A_DONE_STATUS] = { .type =NLA_U32, }, [HANDSHAKE_A_DONE_SOCKFD] = { .type =NLA_S32, }, [HANDSHAKE_A_DONE_REMOTE_AUTH] = { .type =NLA_U32, },net/handshake/genl.c +1
-0+ [HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT] = { .type = NLA_U32,},
};
/* Ops table for handshake */net/handshake/genl.c +0
-0@@ -35,7 +36,7 @@ static const struct genl_split_ops handshake_nl_ops[] = { .cmd = HANDSHAKE_CMD_DONE,
Quoted 8 lines
Quoted 6 lines
net/handshake/genl.c +0
-0.doit = handshake_nl_done_doit,.policy =
handshake_done_nl_policy,net/handshake/genl.c +1
-1- .maxattr = HANDSHAKE_A_DONE_REMOTE_AUTH, + .maxattr =
HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT,
HANDSHAKE_A_DONE_MAX- 1?
Shouldn't it be HANDSHAKE_A_DONE_MAX? Unless the existing HANDSHAKE_A_DONE_REMOTE_AUTH is incorrect?
Thanks for the review!
Regards,
Wilfred
Quoted 1 line
Re: [RFC v2 1/1] net/tls: allow limiting maximum record size
Quoted 28 lines · Wilfred Mallawa
Quoted 1 line · Hannes Reinecke
[snip]
Quoted 18 lines
Quoted 18 lines
include/uapi/linux/handshake.h +1
-0@@ -54,6 +54,7 @@ enum { HANDSHAKE_A_DONE_STATUS = 1, HANDSHAKE_A_DONE_SOCKFD, HANDSHAKE_A_DONE_REMOTE_AUTH, +HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT , __HANDSHAKE_A_DONE_MAX, HANDSHAKE_A_DONE_MAX = (__HANDSHAKE_A_DONE_MAX - 1)net/handshake/genl.c +0
-0@@ -16,10 +16,11 @@ static const struct nla_policyhandshake_accept_nl_policy[HANDSHAKE_A_ACCEPT_HAN
};Quoted 7 lines
Quoted 6 lines
/*
HANDSHAKE_CMD_DONE- do */
-static const structnla_policyhandshake_done_nl_policy[HANDSHAKE_A_DONE_REMOTE_AUTH + 1] = { +static const structnla_policyhandshake_done_nl_policy[HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT + 1] = {
Quoted 6 lines
Hey Hannes,
I did consider using
HANDSHAKE_A_DONE_MAX, but wasn't sure if the existing convention is there for some reason. But I can switch over if you think that is best.
I guess, no reason, just an oversight.
Quoted 22 lines
Quoted 9 lines
Shouldn't that be '
HANDSHAKE_A_DONE_MAX'?Quoted 7 lines
[
HANDSHAKE_A_DONE_STATUS] = { .type =NLA_U32, }, [HANDSHAKE_A_DONE_SOCKFD] = { .type =NLA_S32, }, [HANDSHAKE_A_DONE_REMOTE_AUTH] = { .type =NLA_U32, },net/handshake/genl.c +1
-0+ [HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT] = { .type = NLA_U32,},
};Quoted 12 lines
net/handshake/genl.c static const struct genl_split_ops · line 42
Quoted 10 lines
/* Ops table for handshake */
net/handshake/genl.c +1
-1@@ -35,7 +36,7 @@ static const struct genl_split_ops handshake_nl_ops[] = { .cmd = HANDSHAKE_CMD_DONE, .doit = handshake_nl_done_doit, .policy = handshake_done_nl_policy, - .maxattr = HANDSHAKE_A_DONE_REMOTE_AUTH, + .maxattr = HANDSHAKE_A_DONE_RECORD_SIZE_LIMIT,
HANDSHAKE_A_DONE_MAX- 1?
Quoted 3 lines
Shouldn't it be
HANDSHAKE_A_DONE_MAX? Unless the existingHANDSHAKE_A_DONE_REMOTE_AUTHis incorrect?
Yes, you are right.
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