smb: server: fix the nonce of encrypted responses and a listener leak
Subsystems (4)
Stefan Metzmacher
reviewercover smb: server: fix the nonce of encrypted responses and a listener leak
Hi Namjae,
these are fixes for ksmbd:
- The nonce of encrypted responses and of the payload of encrypted RDMA reads is a 64-bit counter of the session followed by random bits of the session, like Samba does, instead of random bytes, which only made a reuse unlikely. With AES-GCM a reused nonce reveals the authentication key.
- smb_direct_listen() leaked the smbdirect socket if binding or listening failed, e.g. with -EADDRINUSE (reported by Sashiko).
The client got the same nonce fix in the urgent client series, the urgent smbdirect fixes are posted separately as well, they are all independent of each other.
These patches were developed with the help of Claude (Anthropic), which was used for code analysis, writing parts of the code and the commit messages. I reviewed all changes and take full responsibility for them.
Testing: on real hardware, client and server running the urgent client, smbdirect and ksmbd series together:
- encrypted (seal) mounts over TCP and over RDMA (RoCE with mlx5 and irdma) with AES128-GCM and data verification
- ksmbd started while the RDMA port 445 is already in use, smbdirect_socket_bind() fails, no warnings and ksmbd starts again afterwards
- the RDMA tests of the urgent smbdirect series, including 10 ksmbd restarts per device while I/O is running
As we are already late in the 7.4 cycle, I leave it up to you to decide if they should still go into 7.4 or into the next merge window, both are marked for stable.
Base: v7.4-rc4, like the other smb series. The patches also apply cleanly (git am) on cifs-next (61c581ea62ee), on ksmbd-for-next (3c31349456c5) and on both merged, with or without the urgent client and smbdirect series, and build on both merged.
Stefan Metzmacher (2):
smb: server: use a counter for the nonce of encrypted responses
smb: server: don't leak the smbdirect listener socket on errors
fs/smb/server/auth.c | 43 +++++++++++++++++++++++++++++++ fs/smb/server/auth.h | 4 +++ fs/smb/server/mgmt/user_session.c | 1 + fs/smb/server/mgmt/user_session.h | 3 +++ fs/smb/server/smb2pdu.c | 25 +++++++++++------- fs/smb/server/transport_rdma.c | 2 +- 6 files changed, 67 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerPATCH 2/2 smb: server: don't leak the smbdirect listener socket on errors
smb_direct_listen() only assigned listener->socket after smbdirect_socket_listen() succeeded. If setting the parameters, binding (e.g. -EADDRINUSE) or listening failed, the error path called smb_direct_listener_destroy() with listener->socket still NULL, so the socket and its rdma cm id were never released.
Assign listener->socket right after the socket is created, so smb_direct_listener_destroy() releases it on all error paths.
--- fs/smb/server/transport_rdma.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/server/transport_rdma.c +1 -1
@@ -418,6 +418,7 @@ static int smb_direct_listen(struct smb_direct_listener *listener, ret, ERR_PTR(ret));return ret; } + listener->socket = sc;/* * Create the initial parameters
@@ -467,7 +468,6 @@ static int smb_direct_listen(struct smb_direct_listener *listener, } listener->port = port; - listener->socket = sc; kthread = kthread_run(smb_direct_listener_kthread_fn, listener,
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerPATCH 1/2 smb: server: use a counter for the nonce of encrypted responses
In reply to Stefan MetzmacherThe nonce of an encrypted message must not be used for any other encrypted message within the session (MS-SMB2 3.1.4.3). This also covers the payload of RDMA reads with an SMB2_RDMA_TRANSFORM_TYPE_ENCRYPTION transform, which is encrypted with the same key. fill_transform_hdr() and smb2_prep_rdma_read() filled it with random bytes, which only makes a collision unlikely: with 11 random bytes for AES-CCM and 12 for AES-GCM the probability of a collision after n messages is about n^2 / 2^89 or n^2 / 2^97. But with AES-CCM and AES-GCM a reused nonce reveals the XOR of the two plaintexts and, for AES-GCM, the authentication key, which allows forging messages.
Like Samba, use a 64-bit counter of the session for the first 8 bytes of the nonce, followed by random bits chosen when the session is created, 32 bits for AES-GCM and 24 bits for AES-CCM. This makes the nonce unique for the session, also
when several channels encrypt in parallel, see ksmbd_new_enc_nonce().
--- fs/smb/server/auth.c | 43 +++++++++++++++++++++++++++++++ fs/smb/server/auth.h | 4 +++ fs/smb/server/mgmt/user_session.c | 1 + fs/smb/server/mgmt/user_session.h | 3 +++ fs/smb/server/smb2pdu.c | 25 +++++++++++------- 5 files changed, 66 insertions(+), 10 deletions(-)
fs/smb/server/auth.c +43 -0
@@ -18,6 +18,7 @@#include <crypto/utils.h> #include <linux/random.h> #include <linux/scatterlist.h> +#include <linux/unaligned.h> #include "auth.h" #include "glob.h"
@@ -713,6 +714,48 @@ int ksmbd_gen_preauth_integrity_hash(struct ksmbd_conn *conn, char *buf,return 0 ; } +/** +* ksmbd_new_enc_nonce() - generate the nonce for an encrypted response +* @sess: session whose encryption key is used +* @cipher_type: cipher of the connection +* @nonce: nonce output, 11 bytes for AES-CCM, 12 bytes for AES-GCM +* +* The nonce must not be used for any other encrypted message within the +* session (MS-SMB2 3.1.4.3), this also covers the payload of RDMA reads with +* an SMB2_RDMA_TRANSFORM_TYPE_ENCRYPTION transform, which uses the same key. +* A random nonce only makes a collision unlikely, but with AES-GCM and +* AES-CCM a reused nonce reveals the XOR of the plaintexts and allows +* forgeries. +* +* So, like Samba, the nonce is a 64-bit counter of the session followed by +* random bits chosen when the session is created, 32 bits for AES-GCM and +* 24 bits for AES-CCM. +* +* Return: 0 on success, otherwise a negative errno +*/ +int ksmbd_new_enc_nonce (struct ksmbd_session *sess,__le16 cipher_type, +u8 *nonce) +{ +u64 low =atomic64_inc_return (&sess->enc_nonce_low); +u32 high = sess->enc_nonce_high; + +/* the counter can't wrap in practice, but never reuse a nonce */ +if (WARN_ON_ONCE (low ==0 )) +return -EIO ; + +put_unaligned_le64 (low, nonce); +if (cipher_type ==SMB2_ENCRYPTION_AES128_GCM || + cipher_type ==SMB2_ENCRYPTION_AES256_GCM ) { +put_unaligned_le32 (high, nonce +8 ); + }else { + nonce[8 ] = high; + nonce[9 ] = high >>8 ; + nonce[10 ] = high >>16 ; + } + +return 0 ; +} +static int ksmbd_get_encryption_key (struct ksmbd_work *work,__u64 ses_id,int enc,u8 *key) {
fs/smb/server/auth.h +4 -0
@@ -36,6 +36,10 @@ struct ksmbd_conn;struct ksmbd_work ;struct kvec ; +struct ksmbd_session ; + +int ksmbd_new_enc_nonce (struct ksmbd_session *sess,__le16 cipher_type, +u8 *nonce);int ksmbd_crypt_message (struct ksmbd_work *work,struct kvec *iov,unsigned int nvec,int enc); int ksmbd_crypt_rdma(struct ksmbd_conn *conn, const u8 *key,
fs/smb/server/mgmt/user_session.c +1 -0
@@ -1042,6 +1042,7 @@ static struct ksmbd_session *__session_create(struct ksmbd_conn *conn, int protoxa_init (&sess->rpc_handle_list);INIT_LIST_HEAD (&sess->dispose_entry); sess->sequence_number =1 ; +get_random_bytes (&sess->enc_nonce_high,sizeof (sess->enc_nonce_high));atomic_set (&sess->refcnt,2 );init_rwsem (&sess->tree_conns_lock);init_rwsem (&sess->rpc_lock);
fs/smb/server/mgmt/user_session.h +3 -0
@@ -75,6 +75,9 @@ struct ksmbd_session {struct xarray rpc_handle_list;__u8 smb3encryptionkey[SMB3_ENC_DEC_KEY_SIZE ]; +/* see ksmbd_new_enc_nonce() */ +atomic64_t enc_nonce_low; +u32 enc_nonce_high;__u8 smb3decryptionkey[SMB3_ENC_DEC_KEY_SIZE ];__u8 smb3signingkey[SMB3_SIGN_KEY_SIZE ];
fs/smb/server/smb2pdu.c +15 -10
@@ -9631,7 +9631,9 @@ static int smb2_prep_rdma_read(struct ksmbd_work *work, crypto->NonceLength =cpu_to_le16 (nonce_len); nonce = crypto->Signature +SMB2_SIGNATURE_SIZE ; -get_random_bytes (nonce, nonce_len); + err =ksmbd_new_enc_nonce (work->sess, conn->cipher_type, nonce); +if (err) +return err; err = ksmbd_crypt_rdma(conn, work->sess->smb3encryptionkey, data, datalen, nonce, nonce_len,
@@ -13118,24 +13120,23 @@ void smb3_preauth_hash_rsp(struct ksmbd_work *work) } } -static void fill_transform_hdr (void *tr_buf,char *old_buf,__le16 cipher_type) +static int fill_transform_hdr (struct ksmbd_work *work,void *tr_buf) {struct smb2_transform_hdr *tr_hdr = tr_buf +4 ; -struct smb2_hdr *hdr =smb_get_msg (old_buf); -unsigned int orig_len =get_rfc1002_len (old_buf); +struct smb2_hdr *hdr =smb_get_msg (work->response_buf); +unsigned int orig_len =get_rfc1002_len (work->response_buf);/* tr_buf must be cleared by the caller */ tr_hdr->ProtocolId =SMB2_TRANSFORM_PROTO_NUM ; tr_hdr->OriginalMessageSize =cpu_to_le32 (orig_len); tr_hdr->Flags =cpu_to_le16 (TRANSFORM_FLAG_ENCRYPTED ); -if (cipher_type ==SMB2_ENCRYPTION_AES128_GCM || - cipher_type ==SMB2_ENCRYPTION_AES256_GCM ) -get_random_bytes (&tr_hdr->Nonce,SMB3_AES_GCM_NONCE ); -else -get_random_bytes (&tr_hdr->Nonce,SMB3_AES_CCM_NONCE );memcpy (&tr_hdr->SessionId, &hdr->SessionId,8 );inc_rfc1001_len (tr_buf,sizeof (struct smb2_transform_hdr ));inc_rfc1001_len (tr_buf, orig_len); + +/* the same session as in ksmbd_get_encryption_key() */ +return ksmbd_new_enc_nonce (work->sess, work->conn->cipher_type, + tr_hdr->Nonce); }int smb3_encrypt_resp (struct ksmbd_work *work)
@@ -13149,7 +13150,11 @@ int smb3_encrypt_resp(struct ksmbd_work *work)return rc;/* fill transform header */ -fill_transform_hdr (tr_buf, work->response_buf, work->conn->cipher_type); + rc =fill_transform_hdr (work, tr_buf); +if (rc) { +kfree (tr_buf); +return rc; + } iov[0 ].iov_base = tr_buf; iov[0 ].iov_len =sizeof (struct smb2_transform_hdr ) +4 ;
Signature · 1 line
2.43.0