net/tls: support setting the maximum payload size
Subsystems (5)
PATCH v6 1/2 net/tls: support setting the maximum payload 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.
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.
Currently, there is no way to inform the kernel of such a limit. This patch adds support to a new setsockopt() option TLS_TX_MAX_PAYLOAD_LEN that allows for setting the maximum plaintext fragment size. Once set, outgoing records are no larger than the size specified. This option can be used to specify the record size limit.
[1] https://www.rfc-editor.org/rfc/rfc8449
---
Changes V5 -> V6:
- Add NULL check for sw_ctx. Reported by syzbot.
V5: https://lore.kernel.org/netdev/[email protected]/
--- Documentation/networking/tls.rst | 11 ++++++ include/net/tls.h | 3 ++ include/uapi/linux/tls.h | 2 ++ net/tls/tls_device.c | 2 +- net/tls/tls_main.c | 62 ++++++++++++++++++++++++++++++++ net/tls/tls_sw.c | 2 +- 6 files changed, 80 insertions(+), 2 deletions(-)
Documentation/networking/tls.rst +11 -0
@@ -280,6 +280,17 @@ If the record decrypted turns out to had been padded or is not a data record it will be decrypted again into a kernel buffer without zero copy. Such events are counted in the ``TlsDecryptRetry`` statistic. +TLS_TX_MAX_PAYLOAD_LEN +~~~~~~~~~~~~~~~~~~~~~~ + +Sets the maximum size for the plaintext of a protected record. + +When this option is set, the kernel enforces this limit on all transmitted TLS +records, ensuring no plaintext fragment exceeds the specified size. This can be +used to specify the TLS Record Size Limit [1]. + +[1] https://datatracker.ietf.org/doc/html/rfc8449 + Statistics ==========
include/net/tls.h +3 -0
@@ -53,6 +53,8 @@ struct tls_rec;/* Maximum data size carried in a TLS record */ #define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14) +/* Minimum record size limit as per RFC8449 */ +#define TLS_MIN_RECORD_SIZE_LIM ((size_t)1 << 6)#define TLS_HEADER_SIZE 5#define TLS_NONCE_OFFSET TLS_HEADER_SIZE
@@ -226,6 +228,7 @@ struct tls_context { u8 rx_conf:3; u8 zerocopy_sendfile:1; u8 rx_no_pad:1; +u16 tx_max_payload_len;int (*push_pending_record)(struct sock *sk,int flags);void (*sk_write_space)(struct sock *sk);
include/uapi/linux/tls.h +2 -0
@@ -41,6 +41,7 @@#define TLS_RX 2/* Set receive parameters */ #define TLS_TX_ZEROCOPY_RO 3/* TX zerocopy (only sendfile now) */ #define TLS_RX_EXPECT_NO_PAD 4/* Attempt opportunistic zero-copy */ +#define TLS_TX_MAX_PAYLOAD_LEN 5/* Maximum plaintext size */ /* Supported versions */ #define TLS_VERSION_MINOR (ver) ((ver) & 0xFF)
@@ -194,6 +195,7 @@ enum {TLS_INFO_RXCONF ,TLS_INFO_ZC_RO_TX ,TLS_INFO_RX_NO_PAD , +TLS_INFO_TX_MAX_PAYLOAD_LEN , __TLS_INFO_MAX, };#define TLS_INFO_MAX (__TLS_INFO_MAX - 1)
net/tls/tls_device.c +1 -1
@@ -461,7 +461,7 @@ static int tls_push_data(struct sock *sk,/* TLS_HEADER_SIZE is not counted as part of the TLS record, and * we need to leave room for an authentication tag. */ - max_open_record_len =TLS_MAX_PAYLOAD_SIZE + + max_open_record_len = tls_ctx->tx_max_payload_len + prot->prepend_size; do { rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size);
net/tls/tls_main.c +62 -0
104 lines not on this page · raw message
net/tls/tls_sw.c +1 -1
@@ -1079,7 +1079,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_ctx->tx_max_payload_len - msg_pl->sg.size;if (try_to_copy >= record_room) { try_to_copy = record_room; full_record = true;
Signature · 1 line
2.51.0
PATCH v6 2/2 selftests: tls: add tls record_size_limit test
From: Wilfred Mallawa <[email protected]>
Test that outgoing plaintext records respect the tls TLS_TX_MAX_PAYLOAD_LEN set using setsockopt(). The limit is set to be 128, thus, in all received records, the plaintext must not exceed this amount.
Also test that setting a new record size limit whilst a pending open record exists is handled correctly by discarding the request.
--- tools/testing/selftests/net/tls.c | 141 ++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+)
tools/testing/selftests/net/tls.c +141 -0
@@ -2791,6 +2791,147 @@ TEST_F(tls_err, oob_pressure)EXPECT_EQ (send (self->fd2, buf,5 ,MSG_OOB ),5 ); } +/* +* Parse a stream of TLS records and ensure that each record respects +* the specified @max_payload_len. +*/ +static size_t parse_tls_records (struct __test_metadata *_metadata, +const __u8 *rx_buf,int rx_len,int overhead, +__u16 max_payload_len) +{ +const __u8 *rec = rx_buf; +size_t total_plaintext_rx =0 ; +const __u8 rec_header_len =5 ; + +while (rec < rx_buf + rx_len) { +__u16 record_payload_len; +__u16 plaintext_len; + +/* Sanity check that it's a TLS header for application data */ +ASSERT_EQ (rec[0 ],23 ); +ASSERT_EQ (rec[1 ],0x3 ); +ASSERT_EQ (rec[2 ],0x3 ); + +memcpy (&record_payload_len, rec +3 ,2 ); + record_payload_len =ntohs (record_payload_len); +ASSERT_GE (record_payload_len, overhead); + + plaintext_len = record_payload_len - overhead; + total_plaintext_rx += plaintext_len; + +/* Plaintext must not exceed the specified limit */ +ASSERT_LE (plaintext_len, max_payload_len); + rec += rec_header_len + record_payload_len; + } + +return total_plaintext_rx; +} + +TEST (tx_max_payload_len) +{ +struct tls_crypto_info_keys tls12; +int cfd, ret, fd, overhead; +size_t total_plaintext_rx =0 ; +__u8 tx[1024 ], rx[2000 ]; +__u16 limit =128 ; +__u16 opt =0 ; +unsigned int optlen =sizeof (opt); +bool notls; + +tls_crypto_info_init (TLS_1_2_VERSION ,TLS_CIPHER_AES_CCM_128 , + &tls12,0 ); + +ulp_sock_pair (_metadata, &fd, &cfd, ¬ls); + +if (notls) +exit (KSFT_SKIP ); + +/* Don't install keys on fd, we'll parse raw records */ + ret =setsockopt (cfd,SOL_TLS ,TLS_TX , &tls12, tls12.len); +ASSERT_EQ (ret,0 ); + + ret =setsockopt (cfd,SOL_TLS ,TLS_TX_MAX_PAYLOAD_LEN , &limit, +sizeof (limit)); +ASSERT_EQ (ret,0 ); + + ret =getsockopt (cfd,SOL_TLS ,TLS_TX_MAX_PAYLOAD_LEN , &opt, &optlen); +EXPECT_EQ (ret,0 ); +EXPECT_EQ (limit, opt); +EXPECT_EQ (optlen,sizeof (limit)); + +memset (tx,0 ,sizeof (tx)); +ASSERT_EQ (send (cfd, tx,sizeof (tx),0 ),sizeof (tx)); +close (cfd); + + ret =recv (fd, rx,sizeof (rx),0 ); + +/* +* 16B tag + 8B IV -- record header (5B) is not counted but we'll +* need it to walk the record stream +*/ + overhead =16 +8 ; + total_plaintext_rx =parse_tls_records (_metadata, rx, ret, overhead, + limit); + +ASSERT_EQ (total_plaintext_rx,sizeof (tx)); +close (fd); +} + +TEST (tx_max_payload_len_open_rec) +{ +struct tls_crypto_info_keys tls12; +int cfd, ret, fd, overhead; +size_t total_plaintext_rx =0 ; +__u8 tx[1024 ], rx[2000 ]; +__u16 tx_partial =256 ; +__u16 og_limit =512 , limit =128 ; +bool notls; + +tls_crypto_info_init (TLS_1_2_VERSION ,TLS_CIPHER_AES_CCM_128 , + &tls12,0 ); + +ulp_sock_pair (_metadata, &fd, &cfd, ¬ls); + +if (notls) +exit (KSFT_SKIP ); + +/* Don't install keys on fd, we'll parse raw records */ + ret =setsockopt (cfd,SOL_TLS ,TLS_TX , &tls12, tls12.len); +ASSERT_EQ (ret,0 ); + + ret =setsockopt (cfd,SOL_TLS ,TLS_TX_MAX_PAYLOAD_LEN , &og_limit, +sizeof (og_limit)); +ASSERT_EQ (ret,0 ); + +memset (tx,0 ,sizeof (tx)); +ASSERT_EQ (send (cfd, tx, tx_partial,MSG_MORE ), tx_partial); + +/* +* Changing the payload limit with a pending open record should +* not be allowed. +*/ + ret =setsockopt (cfd,SOL_TLS ,TLS_TX_MAX_PAYLOAD_LEN , &limit, +sizeof (limit)); +ASSERT_EQ (ret,-1 ); +ASSERT_EQ (errno,EBUSY ); + +ASSERT_EQ (send (cfd, tx + tx_partial,sizeof (tx) - tx_partial,MSG_EOR ), +sizeof (tx) - tx_partial); +close (cfd); + + ret =recv (fd, rx,sizeof (rx),0 ); + +/* +* 16B tag + 8B IV -- record header (5B) is not counted but we'll +* need it to walk the record stream +*/ + overhead =16 +8 ; + total_plaintext_rx =parse_tls_records (_metadata, rx, ret, overhead, + og_limit); +ASSERT_EQ (total_plaintext_rx,sizeof (tx)); +close (fd); +} +TEST (non_established) {struct tls12_crypto_info_aes_gcm_256 tls12;struct sockaddr_in addr;
Signature · 1 line
2.51.0
Sabrina Dubroca
maintainerQuoted 6 lines · Wilfred Mallawa
Documentation/networking/tls.rst +0
-0@@ -280,6 +280,17 @@ If the record decrypted turns out to had been padded or is not a data record it will be decrypted again into a kernel buffer without zero copy.
Documentation/networking/tls.rst If the record decrypted turns out to had been padded or is not a data · line 290
Quoted 10 lines
Documentation/networking/tls.rst +8
-0Such events are counted in the ``TlsDecryptRetry`` statistic. +TLS_TX_MAX_PAYLOAD_LEN +~~~~~~~~~~~~~~~~~~~~~~ + +Sets the maximum size for the plaintext of a protected record. + +When this option is set, the kernel enforces this limit on all transmitted TLS +records, ensuring no plaintext fragment exceeds the specified size. This can be +used to specify the TLS Record Size Limit [1].
Since this is now "max payload" instead of directly the record size, we should probably add something to describe how to derive the value to pass to TLS_TX_MAX_PAYLOAD_LEN from the record size limit:
For TLS1.2, the record size limit can be used directly. For TLS1.3, limit-1 should be passed, as the record size limit includes 1B for the ContentType.
And possibly mention that TLS1.3 record padding is currently unsupported, so whether it should be counted in the value passed via this setsockopt or not is undecided. (I'm not sure we need to go that far. Jakub, WDYT?)
[...]
Quoted 7 lines
Documentation/networking/tls.rst +7
-0+static int do_tls_setsockopt_tx_payload_len(struct sock *sk, sockptr_t optval, + unsigned int optlen) +{ + struct tls_context *ctx = tls_get_ctx(sk); + struct tls_sw_context_tx *sw_ctx = tls_sw_ctx_tx(ctx); + u16 value; +
Documentation/networking/tls.rst If the record decrypted turns out to had been padded or is not a data
Quoted 10 lines
Documentation/networking/tls.rst +10
-0+ if (sw_ctx && sw_ctx->open_rec) + return -EBUSY; + + if (sockptr_is_null(optval) || optlen != sizeof(value)) + return -EINVAL; + + if (copy_from_sockptr(&value, optval, sizeof(value))) + return -EFAULT; + + if (value < TLS_MIN_RECORD_SIZE_LIM || value > TLS_MAX_PAYLOAD_SIZE)
For 1.3, should we allow TLS_MIN_RECORD_SIZE_LIM-1? The smallest valid record size limit (according to rfc8449) is 64 (TLS_MIN_RECORD_SIZE_LIM), so after userspace subtracts 1 we would get TLS_MIN_RECORD_SIZE_LIM-1?
(but this would bring back one "are we 1.2 or 1.3?" check :/)
Quoted 6 lines
Documentation/networking/tls.rst +6
-0+ return -EINVAL; + + ctx->tx_max_payload_len = value; + + return 0; +}
Signature · 1 line
Sabrina
Quoted 27 lines · Sabrina Dubroca
Quoted 17 lines · Wilfred Mallawa
Documentation/networking/tls.rst +0
-0@@ -280,6 +280,17 @@ If the record decrypted turns out to had beenpadded or is not a data
record it will be decrypted again into a kernel buffer without zero copy.
Such events are counted in the ``TlsDecryptRetry`` statistic.Documentation/networking/tls.rst +6
-0+TLS_TX_MAX_PAYLOAD_LEN +~~~~~~~~~~~~~~~~~~~~~~ + +Sets the maximum size for the plaintext of a protected record. + +When this option is set, the kernel enforces this limit on allQuoted 4 lines
transmitted TLS
+records, ensuring no plaintext fragment exceeds the specified size. This can be
+used to specify the TLS Record Size Limit [1].Since this is now "max payload" instead of directly the record size, we should probably add something to describe how to derive the value to pass to
TLS_TX_MAX_PAYLOAD_LENfrom the record size limit:
Quoted 5 lines
For TLS1.2, the record size limit can be used directly. For TLS1.3, limit-1 should be passed, as the record size limit includes 1B for the ContentType.
Good idea, I will add this on.
Quoted 34 lines
And possibly mention that TLS1.3 record padding is currently unsupported, so whether it should be counted in the value passed via this setsockopt or not is undecided. (I'm not sure we need to go that far. Jakub, WDYT?)
[...]
Quoted 18 lines
+static int do_tls_setsockopt_tx_payload_len(struct sock *sk,
sockptr_toptval,Documentation/networking/tls.rst +16
-0+ unsigned int optlen) +{ + struct tls_context *ctx = tls_get_ctx(sk); + struct tls_sw_context_tx *sw_ctx = tls_sw_ctx_tx(ctx); + u16 value; + + if (sw_ctx && sw_ctx->open_rec) + return -EBUSY; + + if (sockptr_is_null(optval) || optlen != sizeof(value)) + return -EINVAL; + + if (copy_from_sockptr(&value, optval, sizeof(value))) + return -EFAULT; + + if (value < TLS_MIN_RECORD_SIZE_LIM || value >Quoted 1 line
TLS_MAX_PAYLOAD_SIZE)For 1.3, should we allow TLS_MIN_RECORD_SIZE_LIM-1? The smallest valid
record size limit (according to rfc8449) is 64 (TLS_MIN_RECORD_SIZE_LIM), so after userspace subtracts 1 we would get
TLS_MIN_RECORD_SIZE_LIM-1?
Quoted 1 line
(but this would bring back one "are we 1.2 or 1.3?" check :/)
Yeah I don't think there's a way around this...? I will update the description to specify these details and add the limit checks. I do think the payload size approach makes more sense, since, it could be used for reasons other than just record_size_limit.
Regards,
Wilfred
Quoted 7 lines
Quoted 7 lines
Documentation/networking/tls.rst +6
-0+ return -EINVAL; + + ctx->tx_max_payload_len = value; + + return 0; +}