Commit Graph

23 Commits

Author SHA1 Message Date
Sabrina Dubroca ad53885663 tls: fix lockless read of strp->msg_ready in ->poll
JIRA: https://issues.redhat.com/browse/RHEL-29306

commit 0844370f8945086eb9335739d10205dcea8d707b
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Wed Apr 24 12:25:47 2024 +0200

    tls: fix lockless read of strp->msg_ready in ->poll

    tls_sk_poll is called without locking the socket, and needs to read
    strp->msg_ready (via tls_strp_msg_ready). Convert msg_ready to a bool
    and use READ_ONCE/WRITE_ONCE where needed. The remaining reads are
    only performed when the socket is locked.

    Fixes: 121dca784fc0 ("tls: suppress wakeups unless we have a full record")
    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Link: https://lore.kernel.org/r/0b7ee062319037cf86af6b317b3d72f7bfcd2e97.1713797701.git.sd@queasysnail.net
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2024-05-01 17:48:15 +02:00
Sabrina Dubroca e243e5133b tls: don't reset prot->aad_size and prot->tail_size for TLS_HW
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit b7c4f5730a9fa258c8e79f6387a03f3a95c681a2
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Fri Oct 20 16:00:55 2023 +0200

    tls: don't reset prot->aad_size and prot->tail_size for TLS_HW

    Prior to commit 1a074f7618e8 ("tls: also use init_prot_info in
    tls_set_device_offload"), setting TLS_HW on TX didn't touch
    prot->aad_size and prot->tail_size. They are set to 0 during context
    allocation (tls_prot_info is embedded in tls_context, kzalloc'd by
    tls_ctx_create).

    When the RX key is configured, tls_set_sw_offload is called (for both
    TLS_SW and TLS_HW). If the TX key is configured in TLS_HW mode after
    the RX key has been installed, init_prot_info will now overwrite the
    correct values of aad_size and tail_size, breaking SW decryption and
    causing -EBADMSG errors to be returned to userspace.

    Since TLS_HW doesn't use aad_size and tail_size at all (for TLS1.2,
    tail_size is always 0, and aad_size is equal to TLS_HEADER_SIZE +
    rec_seq_size), we can simply drop this hunk.

    Fixes: 1a074f7618e8 ("tls: also use init_prot_info in tls_set_device_offload")
    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Acked-by: Jakub Kicinski <kuba@kernel.org>
    Tested-by: Ran Rozenstein <ranro@nvidia.com>
    Link: https://lore.kernel.org/r/979d2f89a6a994d5bb49cae49a80be54150d094d.1697653889.git.sd@queasysnail.net
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:49 +01:00
Sabrina Dubroca e1a47432ca tls: remove tls_context argument from tls_set_device_offload
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit 4f4866991847738a216bb5920b3d3902cee13fd0
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Mon Oct 9 22:50:51 2023 +0200

    tls: remove tls_context argument from tls_set_device_offload

    It's not really needed since we end up refetching it as tls_ctx. We
    can also remove the NULL check, since we have already dereferenced ctx
    in do_tls_setsockopt_conf.

    While at it, fix up the reverse xmas tree ordering.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:48 +01:00
Sabrina Dubroca 78f2836ff2 tls: remove tls_context argument from tls_set_sw_offload
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit b6a30ec9239a1fa1a622608176bb78646a539608
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Mon Oct 9 22:50:50 2023 +0200

    tls: remove tls_context argument from tls_set_sw_offload

    It's not really needed since we end up refetching it as tls_ctx. We
    can also remove the NULL check, since we have already dereferenced ctx
    in do_tls_setsockopt_conf.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:48 +01:00
Sabrina Dubroca 50f0648bd8 tls: also use init_prot_info in tls_set_device_offload
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit 1a074f7618e8b82a7cebf45df6e005d2284446ce
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Mon Oct 9 22:50:48 2023 +0200

    tls: also use init_prot_info in tls_set_device_offload

    Most values are shared. Nonce size turns out to be equal to IV size
    for all offloadable ciphers.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:48 +01:00
Sabrina Dubroca e9b62cd0a3 tls: rename MAX_IV_SIZE to TLS_MAX_IV_SIZE
JIRA: https://issues.redhat.com/browse/RHEL-14902

Conflicts: tls_decrypt_ctx doesn't have the sk member, missing commit
    8d338c76f7cf  ("tls: Only use data field in crypto completion function")

commit bee6b7b30706e7693d91cb28c8ff3cb69e094f65
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Mon Oct 9 22:50:44 2023 +0200

    tls: rename MAX_IV_SIZE to TLS_MAX_IV_SIZE

    It's defined in include/net/tls.h, avoid using an overly generic name.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:48 +01:00
Sabrina Dubroca 2bd88890c6 tls: extend tls_cipher_desc to fully describe the ciphers
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit 176a3f50bc6a327c82c6b051b0bedd19917081a2
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Fri Aug 25 23:35:13 2023 +0200

    tls: extend tls_cipher_desc to fully describe the ciphers

    - add nonce, usually equal to iv_size but not for chacha
     - add offsets into the crypto_info for each field
     - add algorithm name
     - add offloadable flag

    Also add helpers to access each field of a crypto_info struct
    described by a tls_cipher_desc.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Link: https://lore.kernel.org/r/39d5f476d63c171097764e8d38f6f158b7c109ae.1692977948.git.sd@queasysnail.net
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:46 +01:00
Sabrina Dubroca f6eee1183c tls: rename tls_cipher_size_desc to tls_cipher_desc
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit 8db44ab26bebe969851468bea6072d9a094b2ace
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Fri Aug 25 23:35:12 2023 +0200

    tls: rename tls_cipher_size_desc to tls_cipher_desc

    We're going to add other fields to it to fully describe a cipher, so
    the "_size" name won't match the contents.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Link: https://lore.kernel.org/r/76ca6c7686bd6d1534dfa188fb0f1f6fabebc791.1692977948.git.sd@queasysnail.net
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:46 +01:00
Sabrina Dubroca 8ef2226d61 tls: reduce size of tls_cipher_size_desc
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit 037303d6760751fdb95ba62cf448ecbc1ac29c98
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Fri Aug 25 23:35:11 2023 +0200

    tls: reduce size of tls_cipher_size_desc

    tls_cipher_size_desc indexes ciphers by their type, but we're not
    using indices 0..50 of the array. Each struct tls_cipher_size_desc is
    20B, so that's a lot of unused memory. We can reindex the array
    starting at the lowest used cipher_type.

    Introduce the get_cipher_size_desc helper to find the right item and
    avoid out-of-bounds accesses, and make tls_cipher_size_desc's size
    explicit so that gcc reminds us to update TLS_CIPHER_MIN/MAX when we
    add a new cipher.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Link: https://lore.kernel.org/r/5e054e370e240247a5d37881a1cd93a67c15f4ca.1692977948.git.sd@queasysnail.net
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:46 +01:00
Sabrina Dubroca 3b06cf4816 tls: move tls_cipher_size_desc to net/tls/tls.h
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit fd0fc6fdd8896a195cb7b0210a5ee46774718fc8
Author: Sabrina Dubroca <sd@queasysnail.net>
Date:   Fri Aug 25 23:35:09 2023 +0200

    tls: move tls_cipher_size_desc to net/tls/tls.h

    It's only used in net/tls/*, no need to bloat include/net/tls.h.

    Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
    Link: https://lore.kernel.org/r/dd9fad80415e5b3575b41f56b331871038362eab.1692977948.git.sd@queasysnail.net
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:46 +01:00
Sabrina Dubroca 63ea298f54 net/tls: Remove unused function declarations
JIRA: https://issues.redhat.com/browse/RHEL-14902

commit f6ecb68b38a5c23f20160dd49718d1a9d395a86d
Author: Yue Haibing <yuehaibing@huawei.com>
Date:   Sat Aug 5 18:48:11 2023 +0800

    net/tls: Remove unused function declarations

    Commit 3c4d755915 ("tls: kernel TLS support") declared but never implemented
    these functions.

    Signed-off-by: Yue Haibing <yuehaibing@huawei.com>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-12-13 14:27:46 +01:00
Jeffrey Layton 67631364e1 net/tls: Move TLS protocol elements to a separate header
JIRA: https://issues.redhat.com/browse/RHEL-7936
Conflicts: minor differences in #include handling

commit 6a7eccef47b205ae66371a26d36dfb2529835075
Author: Chuck Lever <chuck.lever@oracle.com>
Date:   Thu Jul 27 13:35:23 2023 -0400

    net/tls: Move TLS protocol elements to a separate header

    Kernel TLS consumers will need definitions of various parts of the
    TLS protocol, but often do not need the function declarations and
    other infrastructure provided in <net/tls.h>.

    Break out existing standardized protocol elements into a separate
    header, and make room for a few more elements in subsequent patches.

    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
    Link: https://lore.kernel.org/r/169047931374.5241.7713175865185969309.stgit@oracle-102.nfsv4bat.org
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2023-12-02 05:12:25 -05:00
Jeffrey Layton fdcc17d962 net/tls: implement ->read_sock()
JIRA: https://issues.redhat.com/browse/RHEL-7936

commit 662fbcec32f4af6bdcf5b4006b792ebe9543d945
Author: Hannes Reinecke <hare@suse.de>
Date:   Wed Jul 26 21:15:56 2023 +0200

    net/tls: implement ->read_sock()

    Implement ->read_sock() function for use with nvme-tcp.

    Signed-off-by: Hannes Reinecke <hare@suse.de>
    Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
    Reviewed-by: Jakub Kicinski <kuba@kernel.org>
    Cc: Boris Pismenny <boris.pismenny@gmail.com>
    Link: https://lore.kernel.org/r/20230726191556.41714-7-hare@suse.de
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2023-12-02 05:12:25 -05:00
Sabrina Dubroca efbf566cfa tls: rx: strp: preserve decryption status of skbs when needed
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2219775
Tested: tls selftests

commit eca9bfafee3a0487e59c59201ae14c7594ba940a
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Tue May 16 18:50:41 2023 -0700

    tls: rx: strp: preserve decryption status of skbs when needed

    When receive buffer is small we try to copy out the data from
    TCP into a skb maintained by TLS to prevent connection from
    stalling. Unfortunately if a single record is made up of a mix
    of decrypted and non-decrypted skbs combining them into a single
    skb leads to loss of decryption status, resulting in decryption
    errors or data corruption.

    Similarly when trying to use TCP receive queue directly we need
    to make sure that all the skbs within the record have the same
    status. If we don't the mixed status will be detected correctly
    but we'll CoW the anchor, again collapsing it into a single paged
    skb without decrypted status preserved. So the "fixup" code will
    not know which parts of skb to re-encrypt.

    Fixes: 84c61fe1a75b ("tls: rx: do not use the standard strparser")
    Tested-by: Shai Amiram <samiram@nvidia.com>
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>
    Reviewed-by: Simon Horman <simon.horman@corigine.com>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2023-07-05 15:14:28 +02:00
Sabrina Dubroca 68df2ddfe7 tls: rx: do not use the standard strparser
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit 84c61fe1a75b4255df1e1e7c054c9e6d048da417
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Fri Jul 22 16:50:33 2022 -0700

    tls: rx: do not use the standard strparser

    TLS is a relatively poor fit for strparser. We pause the input
    every time a message is received, wait for a read which will
    decrypt the message, start the parser, repeat. strparser is
    built to delineate the messages, wrap them in individual skbs
    and let them float off into the stack or a different socket.
    TLS wants the data pages and nothing else. There's no need
    for TLS to keep cloning (and occasionally skb_unclone()'ing)
    the TCP rx queue.

    This patch uses a pre-allocated skb and attaches the skbs
    from the TCP rx queue to it as frags. TLS is careful never
    to modify the input skb without CoW'ing / detaching it first.

    Since we call TCP rx queue cleanup directly we also get back
    the benefit of skb deferred free.

    Overall this results in a 6% gain in my benchmarks.

    Acked-by: Paolo Abeni <pabeni@redhat.com>
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-12-02 08:54:44 +01:00
Sabrina Dubroca a1e97fa711 tls: rx: device: add input CoW helper
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit 8b3c59a7a0bed6fe365755ac211dcf94fdac81b4
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Fri Jul 22 16:50:32 2022 -0700

    tls: rx: device: add input CoW helper

    Wrap the remaining skb_cow_data() into a helper, so it's easier
    to replace down the lane. The new version will change the skb
    so make sure relevant pointers get reloaded after the call.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-12-02 08:54:44 +01:00
Sabrina Dubroca 67891e93e3 tls: rx: device: keep the zero copy status with offload
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit d4e5db6452211467f668521f5a3bd3c3928918e1
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Fri Jul 22 16:50:30 2022 -0700

    tls: rx: device: keep the zero copy status with offload

    The non-zero-copy path assumes a full skb with decrypted contents.
    This means the device offload would have to CoW the data. Try
    to keep the zero-copy status instead, copy the data to user space.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-12-02 08:54:44 +01:00
Sabrina Dubroca df622be158 tls: rx: wrap recv_pkt accesses in helpers
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit b92a13d488de2e87c869a4a6c0393d1f9eebe6dd
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Fri Jul 22 16:50:27 2022 -0700

    tls: rx: wrap recv_pkt accesses in helpers

    To allow for the logic to change later wrap accesses
    which interrogate the input skb in helper functions.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-11-30 23:43:08 +01:00
Sabrina Dubroca 7a342e6ceb tls: rx: decrypt into a fresh skb
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit fd31f3996af2627106e22a9f8072764fede51161
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Thu Jul 14 22:22:35 2022 -0700

    tls: rx: decrypt into a fresh skb

    We currently CoW Rx skbs whenever we can't decrypt to a user
    space buffer. The skbs can be enormous (64kB) and CoW does
    a linear alloc which has a strong chance of failing under
    memory pressure. Or even without, skb_cow_data() assumes
    GFP_ATOMIC.

    Allocate a new frag'd skb and decrypt into it. We finally
    take advantage of the decrypted skb getting returned via
    darg.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-11-30 23:43:07 +01:00
Sabrina Dubroca 47a97f1f78 tls: rx: async: hold onto the input skb
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit c618db2afe7c31d13ca8cf05b60f17165fbdc282
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Thu Jul 14 22:22:33 2022 -0700

    tls: rx: async: hold onto the input skb

    Async crypto currently benefits from the fact that we decrypt
    in place. When we allow input and output to be different skbs
    we will have to hang onto the input while we move to the next
    record. Clone the inputs and keep them on a list.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-11-30 23:43:07 +01:00
Sabrina Dubroca 7562af4dc6 tls: rx: read the input skb from ctx->recv_pkt
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

commit 541cc48be3b141e8529fef05ad6cedbca83f9e80
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Thu Jul 14 22:22:30 2022 -0700

    tls: rx: read the input skb from ctx->recv_pkt

    Callers always pass ctx->recv_pkt into decrypt_skb_update(),
    and it propagates it to its callees. This may give someone
    the false impression that those functions can accept any valid
    skb containing a TLS record. That's not the case, the record
    sequence number is read from the context, and they can only
    take the next record coming out of the strp.

    Let the functions get the skb from the context instead of
    passing it in. This will also make it cleaner to return
    a different skb than ctx->recv_pkt as the decrypted one
    later on.

    Since we're touching the definition of decrypt_skb_update()
    use this as an opportunity to rename it.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-11-30 23:43:06 +01:00
Sabrina Dubroca fcc8960586 net/tls: Check for errors in tls_device_init
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

Conflicts: tls_device_init was moved from include/net/tls.h to
    net/tls/tls.h in commit 587903142308 ("tls: create an internal
    header")

commit 3d8c51b25a235e283e37750943bbf356ef187230
Author: Tariq Toukan <tariqt@nvidia.com>
Date:   Thu Jul 14 10:07:54 2022 +0300

    net/tls: Check for errors in tls_device_init

    Add missing error checks in tls_device_init.

    Fixes: e8f6979981 ("net/tls: Add generic NIC offload infrastructure")
    Reported-by: Jakub Kicinski <kuba@kernel.org>
    Reviewed-by: Maxim Mikityanskiy <maximmi@nvidia.com>
    Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
    Link: https://lore.kernel.org/r/20220714070754.1428-1-tariqt@nvidia.com
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-11-30 23:43:04 +01:00
Sabrina Dubroca 4502d7fd21 tls: create an internal header
Tested: selftests
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2143700

Conflicts: tls_sw_recvmsg still has the nonblock argument, missing
    commit ec095263a965 ("net: remove noblock parameter from recvmsg()
    entities")

commit 5879031423089b2e19b769f30fc618af742264c3
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Thu Jul 7 18:03:13 2022 -0700

    tls: create an internal header

    include/net/tls.h is getting a little long, and is probably hard
    for driver authors to navigate. Split out the internals into a
    header which will live under net/tls/. While at it move some
    static inlines with a single user into the source files, add
    a few tls_ prefixes and fix spelling of 'proccess'.

    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Sabrina Dubroca <sdubroca@redhat.com>
2022-11-30 23:43:03 +01:00