cifs: introduce support for AES-GMAC signing
Subsystems (2)
v2 cover cifs: introduce support for AES-GMAC signing
Hi,
This patch series introduce the support for AES-GMAC signing for SMB 3.1.1, when the negotiate signing context is successfully negotiated.
This is an implementation of the MS-SMB2 spec, section 3.1.4.1 "Signing An Outgoing Message".
AES-GMAC uses AES-128-GCM (kernel crypto algorithm "gcm(aes)" from the gcm module) as its base.
AES-GCM, an authenticated encryption algorithm, takes 4 inputs for encryption:
1. a secret key
2. an initialization vector (IV), or nonce
3. the plaintext data (the message to be encrypted)
4. an extra buffer called Additional Authenticated data (AAD), or sometimes Associated Data (AD)
Superficially describing it, it encrypts the plaintext using the key and then uses the nonce to generate an authentication tag (our signature, in this context). It then produces 2 outputs; the encrypted data, along with its computed authentication tag. The AAD buffer is left intact.
Because of this design, AES-GCM can be used to "encrypt" an empty plaintext buffer, and still run its authentication generation algorithm over the AAD buffer (which is set to our SMB2 message), making it practically a MAC algorithm.
Compared to AES-CMAC, AES-GMAC has shown (*) to be much faster because its internal authentication algorithm can be paralellized by taking advantage of especialized, optimized CPU instructions (on certain supported platforms).
With these patches, I hope cifs.ko can get a performance improvemente when AES-GMAC is used for message signing.
Patch 1/3: add the core functions to perform AES-GMAC signing (see commit
message for more details)
Patch 2/3: set the "enable_negotiate_signing" module param as deprecated,
making cifs.ko always try to negotiate AES-GMAC first, but use AES-CMAC if fail
Patch 3/3: show the signing algorithm name being used in DebugData
To do/discuss:
- serious benchmark and comparison (vs AES-CMAC) on cifs.ko workloads (*)
- complete removal of "enable_negotiate_signing" module param
- RDMA/SMB Direct; I have no experience with it, nor an RDMA-capable setup
- bugs?
Any kind of feedback is welcome.
Cheers,
Enzo Matsumiya (3):
cifs: introduce AES-GMAC signing support for SMB 3.1.1
cifs: deprecate 'enable_negotiate_signing' module param
cifs: show signing algorithm name in DebugData
v2: update patch 1/3 to fix some warnings from kernel test robot (see patch for details)
fs/cifs/cifs_debug.c | 7 +- fs/cifs/cifsencrypt.c | 5 + fs/cifs/cifsfs.c | 8 +- fs/cifs/cifsglob.h | 12 +- fs/cifs/sess.c | 2 + fs/cifs/smb2glob.h | 10 + fs/cifs/smb2misc.c | 4 + fs/cifs/smb2ops.c | 482 +++++++++++++++++++++++++++++++++------- fs/cifs/smb2pdu.c | 97 ++++++-- fs/cifs/smb2proto.h | 9 +- fs/cifs/smb2transport.c | 87 ++++++-- 11 files changed, 590 insertions(+), 133 deletions(-)
Signature · 1 line
2.35.3
PATCH v2 1/3 cifs: introduce AES-GMAC signing support for SMB 3.1.1
This patch implements the support for AES-GMAC message signing, as specified in MS-SMB2 3.1.4.1 "Signing An Outgoing Message".
The core function is smb311_calc_aes_gmac(), which is to be used as the ->calc_signature op when a signing negotiate context (SMB2_SIGNING_CAPABILITIES) has been sent ("enable_negotiate_signing" module parameter) to the server and replied to with SIGNING_ALG_AES_GMAC (i.e. the server supports it).
If "enable_negotiate_signing" is false (default) or if the server reply with SIGNING_ALG_AES_CMAC, ->calc_signature will point to smb3_calc_aes_cmac() (this was renamed from smb3_calc_signature() to better identify which functions are dealing with which algorithms).
Since, in the crypto API context, AES-GMAC is not a hashing algorithm, but rather AES-128-GCM with an empty plaintext buffer, the following modifications were made:
- Introduce smb311_crypt_sign() to accommodate the common code for encrypt/decrypt and signing operations that deals with the crypto API
- crypt_message() has been modified to adopt smb311_crypt_sign() usage; no change of behaviour whatsoever
- init_sg() now takes a bool argument 'crypt' to indicate when it's initializing an SG list for crypt operations, or set to false when it's for initializing it for AES-GMAC signing. This is needed because crypt operations use a transform header, and it needs to skip the first 20 bytes of the first iov. For AES-GMAC signing this is not needed because iov[0] is already the beginning of the message
- Introduce smb311_aes_gmac_alloc() to allocate the secmech. This is called only once in smb311_update_preauth_hash(). The TFM has the lifetime of the TCP session, so it's only free when the TCP session is put() (via cifs_crypto_secmech_release())
- Introduce smb311_aes_gmac_nonce() to produce the nonce for AES-GMAC signing (as per MS-SMB2 3.1.4.1, item 2)
More implementation-specific information can be found in the kernel-doc comments for the introduced functions.
Other smaller modifications:
- Check if the request is encrypted in smb2_setup_request(), as we must not sign a message if it is (MS-SMB2 3.2.4.1.1) -- AES-GCM and AES-CCM will generate their signatures based on the ciphertext (AES-GCM) or the plaintext (AES-CCM)
- smb2_verify_signature():
- Remove extra call to memset to zero the header signature as this is
already done in the beginning ->calc_signature() implementations
- Remove useless call to check for "BSRSPYL" signature as it's SMB1 only
- Add checks for if command is 0xFFFFFFFFFFFFFFFF or if status is
STATUS_PENDING (MS-SMB2 3.2.5.1.3)- Remove useless variable from smb2_sign_rqst()
- smb2_get_sign_key() is no longer static as it's now used by smb311_calc_aes_gmac()
- Use sizeof u16/__le16 in build_signing_ctxt() instead of hardcoded values
---
v2:
- Fix check in smb2_verify_signature() to check for MessageId instead of Command (MS-SMB2 3.2.5.1.3), mistake done by me being careless (reported by kernel test robot and Dan Carpenter)
- Add prototypes for smb2_get_sign_key() (not static anymore) and smb311_aes_gmac_alloc() (new) to smb2proto.h (reported by kernel test robot)
fs/cifs/cifsencrypt.c | 5 + fs/cifs/cifsglob.h | 9 +- fs/cifs/sess.c | 2 + fs/cifs/smb2misc.c | 4 + fs/cifs/smb2ops.c | 482 +++++++++++++++++++++++++++++++++------- fs/cifs/smb2pdu.c | 93 ++++++-- fs/cifs/smb2proto.h | 9 +- fs/cifs/smb2transport.c | 87 ++++++-- 8 files changed, 569 insertions(+), 122 deletions(-)
fs/cifs/cifsencrypt.c +5 -0
@@ -743,6 +743,11 @@ cifs_crypto_secmech_release(struct TCP_Server_Info *server) server->secmech.hmacmd5 =NULL ; } +if (server->secmech.aes_gmac) { +crypto_free_aead (server->secmech.aes_gmac); + server->secmech.aes_gmac =NULL ; + } +if (server->secmech.ccmaesencrypt) {crypto_free_aead (server->secmech.ccmaesencrypt); server->secmech.ccmaesencrypt =NULL ;
fs/cifs/cifsglob.h +8 -1
@@ -171,6 +171,7 @@ struct cifs_secmech {struct sdesc *sdeschmacsha256;/* ctxt to generate smb2 signature */ struct sdesc *sdesccmacaes;/* ctxt to generate smb3 signature */ struct sdesc *sdescsha512;/* ctxt to generate smb3.11 signing key */ +struct crypto_aead *aes_gmac;/* to generate SMB3.1.1 signature */ struct crypto_aead *ccmaesencrypt;/* smb3 encryption aead */ struct crypto_aead *ccmaesdecrypt;/* smb3 decryption aead */ };
@@ -704,7 +705,13 @@ struct TCP_Server_Info {unsigned int max_write;unsigned int min_offload;__le16 compress_algorithm; -__u16 signing_algorithm; +/* +* algorithm to be used to sign messages: +* SMB 2.x - HMAC-SHA256 (unsupported in SMB 3.1.1) +* SMB 3.0.x - AES-CMAC +* SMB 3.1.1 - AES-GMAC, if server negotiated it, or AES-CMAC otherwise +*/ +__u16 signing_algorithm;__le16 cipher_type;/* save initital negprot hash */ __u8 preauth_sha_hash[SMB2_PREAUTH_HASH_SIZE ];
fs/cifs/sess.c +2 -0
@@ -458,6 +458,8 @@ cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,* We need to allocate the server crypto now as we will need * to sign packets before we generate the channel signing key * (we sign with the session key) +* +* AES-GMAC secmech is allocated in smb311_update_preauth_hash() call. */ rc =smb311_crypto_shash_allocate (chan->server);if (rc) {
fs/cifs/smb2misc.c +4 -0
@@ -897,6 +897,10 @@ smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,return 0 ; ok: + rc =smb311_aes_gmac_alloc (&server->secmech.aes_gmac); +if (rc) +return rc; + rc =smb311_crypto_shash_allocate (server);if (rc)return rc;
fs/cifs/smb2ops.c +403 -79
566 lines not on this page · raw message
fs/cifs/smb2pdu.c +76 -17
171 lines not on this page · raw message
fs/cifs/smb2proto.h +7 -2
@@ -36,6 +36,7 @@ extern struct mid_q_entry *smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *, struct smb_rqst *rqst);extern struct mid_q_entry *smb2_setup_async_request ( +extern int smb311_aes_gmac_alloc(struct crypto_aead **); struct TCP_Server_Info *server, struct smb_rqst *rqst);extern struct cifs_ses *smb2_find_smb_ses (struct TCP_Server_Info *server,__u64 ses_id);
@@ -44,9 +45,12 @@ extern struct cifs_tcon *smb2_find_smb_tcon(struct TCP_Server_Info *server,extern int smb2_calc_signature (struct smb_rqst *rqst,struct TCP_Server_Info *server,bool allocate_crypto); -extern int smb3_calc_signature (struct smb_rqst *rqst, +extern int smb3_calc_aes_cmac (struct smb_rqst *rqst, +struct TCP_Server_Info *server, +bool allocate_crypto); +extern int smb311_calc_aes_gmac (struct smb_rqst *rqst,struct TCP_Server_Info *server, -bool allocate_crypto); +bool alloc);extern void smb2_echo_request (struct work_struct *work);extern __le32 smb2_get_lease_state (struct cifsInodeInfo *cinode); extern bool smb2_is_valid_oplock_break(char *buffer,
@@ -268,6 +272,7 @@ extern void smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, struct kstatfs *kst);extern int smb311_crypto_shash_allocate (struct TCP_Server_Info *server); +extern int smb2_get_sign_key (__u64 ,struct TCP_Server_Info *,u8 *);extern int smb311_update_preauth_hash (struct cifs_ses *ses,struct TCP_Server_Info *server,struct kvec *iov,int nvec);
fs/cifs/smb2transport.c +64 -23
155 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [PATCH v2 1/3] cifs: introduce AES-GMAC signing support for SMB 3.1.1
Quoted 8 lines · Enzo Matsumiya
- Add prototypes for
smb2_get_sign_key()(not static anymore) andsmb311_aes_gmac_alloc()(new) tosmb2proto.h(reported by kernel test robot)...
extern struct
mid_q_entry*smb2_setup_async_request(+extern int smb311_aes_gmac_alloc(struct crypto_aead **); struct TCP_Server_Info *server, struct smb_rqst *rqst);
Steve pointed out an error when building v2, which I fat-finger pasted the prototype in between smb2_setup_async_request parameters.
Fixed it.
But meanwhile I also found that, after some time copying a 5GB file, the signatures will be miscalculated, some memory leak occurs, and then a crash in unlink or umount.
I'm assessing those issues right now, will send v3 later.
Enzo
PATCH v2 2/3 cifs: deprecate 'enable_negotiate_signing' module param
In reply to Enzo MatsumiyaWe can blindly send a negotiate signing context on every negotiate call, as it doesn't affect the current behaviour; even though we set AES-GMAC as the first signing algorithm (i.e. our preferred algorithm), it will use AES-CMAC if:
a) the server doesn't respond to signing negotiations
b) the server responds that it only supports AES-GMAC
So this is a safe change.
Throw a warning if 'enable_negotiate_signing=0' was explicitly set, but ignore it and set it back to true.
--- fs/cifs/cifsfs.c | 8 ++++++-- fs/cifs/cifsglob.h | 3 ++- fs/cifs/smb2pdu.c | 21 ++++++++++++--------- 3 files changed, 20 insertions(+), 12 deletions(-)
fs/cifs/cifsfs.c +6 -2
@@ -65,7 +65,7 @@ bool lookupCacheEnabled = true;bool disable_legacy_dialects;/* false by default */ bool enable_gcm_256 = true;bool require_gcm_256;/* false by default */ -bool enable_negotiate_signing;/* false by default */ +bool enable_negotiate_signing = true;/* deprecated -- always true now */ unsigned int global_secflags =CIFSSEC_DEF ;/* unsigned int ntlmv2_support = 0; */ unsigned int sign_CIFS_PDUs =1 ;
@@ -133,8 +133,12 @@ MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encrmodule_param (require_gcm_256, bool,0644 );MODULE_PARM_DESC (require_gcm_256,"Require strongest (256 bit) GCM encryption. Default: n/N/0" ); +/* XXX: remove this at some point */ module_param (enable_negotiate_signing, bool,0644 ); -MODULE_PARM_DESC (enable_negotiate_signing,"Enable negotiating packet signing algorithm with server. Default: n/N/0" ); +MODULE_PARM_DESC (enable_negotiate_signing, +"(deprecated) Enable negotiating packet signing algorithm with the server. " +"Default: always y/Y/1. Changing this setting no longer has any effect, cifs.ko " +"will always try to negotiate the signing algorithm on SMB 3.1.1 mounts." );module_param (disable_legacy_dialects, bool,0644 ); MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
fs/cifs/cifsglob.h +2 -1
@@ -2015,7 +2015,8 @@ extern unsigned int global_secflags; /* if on, session setup sentextern unsigned int sign_CIFS_PDUs;/* enable smb packet signing */ extern bool enable_gcm_256;/* allow optional negotiate of strongest signing (aes-gcm-256) */ extern bool require_gcm_256;/* require use of strongest signing (aes-gcm-256) */ -extern bool enable_negotiate_signing;/* request use of faster (GMAC) signing if available */ +/* XXX: remove enable_negotiate_signing at some point */ +extern bool enable_negotiate_signing;/* (deprecated) request to use AES-GMAC signing if supported */ extern bool linuxExtEnabled;/*enable Linux/Unix CIFS extensions*/ extern unsigned int CIFSMaxBufSize;/* max size not including hdr */ extern unsigned int cifs_min_rcv;/* min size of big ntwrk buf pool */
fs/cifs/smb2pdu.c +12 -9
@@ -614,14 +614,18 @@ assemble_neg_contexts(struct smb2_negotiate_req *req, neg_context_count++; } -if (enable_negotiate_signing) { - ctxt_len =build_signing_ctxt ((struct smb2_signing_capabilities *) - pneg_ctxt); - *total_len += ctxt_len; - pneg_ctxt += ctxt_len; - neg_context_count++; +if (!enable_negotiate_signing) { +pr_warn ("cifs.ko loaded with param 'enable_negotiate_signing=0', but this parameter " +"is deprecated. Trying to negotiate signing capabilities anyway..." ); + enable_negotiate_signing = true; } + ctxt_len =build_signing_ctxt ((struct smb2_signing_capabilities *) + pneg_ctxt); + *total_len += ctxt_len; + pneg_ctxt += ctxt_len; + neg_context_count++; +/* check for and add transport_capabilities and signing capabilities */ req->NegotiateContextCount =cpu_to_le16 (neg_context_count); }
@@ -818,8 +822,7 @@ static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp, }/* -* Throw a warning if user requested signing to be negotiated, but it -* wasn't. +* Throw a warning if signing context was not negotiated. * * Some servers will not send a SMB2_SIGNING_CAPABILITIES context (*), * so we use AES-CMAC (default in smb311 ops) as it is expected to be
@@ -827,7 +830,7 @@ static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,* * (*) see note "<125> Section 3.2.4.2.2.2" in MS-SMB2 */ -if (!server->signing_negotiated && enable_negotiate_signing) +if (!server->signing_negotiated)cifs_dbg (VFS ,"signing capabilities were not negotiated, using " "AES-CMAC for message signing\n" );
Signature · 1 line
2.35.3
PATCH v2 3/3 cifs: show signing algorithm name in DebugData
In reply to Enzo Matsumiya- Move smb3_signing_algo_str() to smb2glob.h
- Rename it to smb2_signing_algo_str()
- Add a case for HMAC-SHA256
- Show the algorithm name in DebugData, when signing is enabled
--- fs/cifs/cifs_debug.c | 7 +++++-- fs/cifs/smb2glob.h | 10 ++++++++++ fs/cifs/smb2pdu.c | 13 +------------ 3 files changed, 16 insertions(+), 14 deletions(-)
fs/cifs/cifs_debug.c +5 -2
@@ -13,6 +13,7 @@#include <linux/uaccess.h> #include "cifspdu.h" #include "cifsglob.h" +#include "smb2glob.h" #include "cifsproto.h" #include "cifs_debug.h" #include "cifsfs.h"
@@ -354,7 +355,8 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)else if (server->compress_algorithm ==SMB3_COMPRESS_LZ77_HUFF )seq_printf (m," COMPRESS_LZ77_HUFF" );if (server->sign) -seq_printf (m," signed" ); +seq_printf (m," signed (%s)" , +smb2_signing_algo_str (server->signing_algorithm));if (server->posix_ext_supported)seq_printf (m," posix" );if (server->nosharesock)
@@ -405,7 +407,8 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)if (ses->session_flags &SMB2_SESSION_FLAG_ENCRYPT_DATA )seq_puts (m," encrypted" );if (ses->sign) -seq_puts (m," signed" ); +seq_printf (m," signed (%s)" , +smb2_signing_algo_str (server->signing_algorithm)); seq_printf(m, "\n\tUser: %d Cred User: %d", from_kuid(&init_user_ns, ses->linux_uid),
fs/cifs/smb2glob.h +10 -0
@@ -41,4 +41,14 @@#define END_OF_CHAIN 4#define RELATED_REQUEST 8 +static inline const char *smb2_signing_algo_str (u16 algo) +{ +switch (algo) { +case SIGNING_ALG_HMAC_SHA256 :return "HMAC-SHA256" ; +case SIGNING_ALG_AES_CMAC :return "AES-CMAC" ; +case SIGNING_ALG_AES_GMAC :return "AES-GMAC" ; +default :return "unknown algorithm" ; + } +} +#endif /* _SMB2_GLOB_H */
fs/cifs/smb2pdu.c +1 -12
@@ -716,17 +716,6 @@ static int decode_encrypt_ctx(struct TCP_Server_Info *server,return 0 ; } -/* XXX: maybe move this somewhere else? */ -static const char *smb3_signing_algo_str (u16 algo) -{ -switch (algo) { -case SIGNING_ALG_AES_CMAC :return "AES-CMAC" ; -case SIGNING_ALG_AES_GMAC :return "AES-GMAC" ; -/* HMAC-SHA256 is unused in SMB3+ context */ -default :return "unknown" ; - } -} -static void decode_signing_ctx (struct TCP_Server_Info *server,struct smb2_signing_capabilities *pctxt) {
@@ -757,7 +746,7 @@ static void decode_signing_ctx(struct TCP_Server_Info *server,/* AES-CMAC is already the default, in case AES-GMAC wasn't negotiated */ cifs_dbg (FYI ,"negotiated signing algorithm '%s'\n" , -smb3_signing_algo_str (server->signing_algorithm)); +smb2_signing_algo_str (server->signing_algorithm)); } static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Signature · 1 line
2.35.3