crypto: powerpc/p10-aes-gcm - Fix IPsec stall on bond failover
Subsystems (3)
PATCH 1/1 crypto: powerpc/p10-aes-gcm - Fix IPsec stall on bond failover
The Power10 AES-GCM driver registers its gcm(aes) and rfc4106(gcm(aes)) algorithms through the crypto SIMD helper. When crypto_simd_usable() is false, for example in softirq context, the helper defers requests to cryptd and completes them asynchronously with -EINPROGRESS.
This breaks IPsec traffic during a bond failover. With the crypto offload device active, traffic is switched to a passive device that has only software encryption, and the ESP requests then go through aes_gcm_p10_crypto. Under heavy load, some or all of the TCP streams drop to zero throughput and do not recover. The stack is seen to drop the ACKs as invalid, which points to the cryptd queueing delaying or reordering the packets.
Fix this by registering the algorithms directly as aead_alg with CRYPTO_ALG_NEED_FALLBACK, and allocating a "gcm(aes)" or "rfc4106(gcm(aes))" fallback tfm in ->init(). When VSX cannot be used (!crypto_simd_usable()), the key was not set up for SIMD, or cryptlen < 64, the request is handed to the fallback and completed synchronously in the caller's context. The driver no longer defers work to cryptd.
---
Tested with tcrypt.ko (including the self-tests) and with IPsec traffic across a bond failover. TCP throughput no longer drops to zero.
arch/powerpc/crypto/aes-gcm-p10-glue.c | 143 ++++++++++++++++++------- 1 file changed, 107 insertions(+), 36 deletions(-)
arch/powerpc/crypto/aes-gcm-p10-glue.c +107 -36
@@ -2,7 +2,7 @@/* * Glue code for accelerated AES-GCM stitched implementation for ppc64le. * -* Copyright 2022- IBM Inc. All rights reserved +* Copyright IBM Corp. 2022, 2023, 2024, 2025, 2026 */ #include <linux/unaligned.h>
@@ -63,8 +63,12 @@ struct Hash_ctx { };struct p10_aes_gcm_ctx { +struct crypto_aead *fallback_tfm;struct p10_aes_key enc_key; -u8 nonce[RFC4106_NONCE_SIZE ]; +u8 nonce[RFC4106_NONCE_SIZE ]__aligned(16) ; +unsigned int keylen; +u8 key[AES_MAX_KEYLENGTH ]__aligned(16) ; +unsigned int simd_set_key; };static void vsx_begin (void )
@@ -173,6 +177,40 @@ static void finish_tag(struct gcm_ctx *gctx, struct Hash_ctx *hash, int len) hash->Htable[i] ^= gctx->ivtag[i]; } +static int p10_aead_init (struct crypto_aead *tfm) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm); + + ctx->fallback_tfm =crypto_alloc_aead ("gcm(aes)" ,0 ,CRYPTO_ALG_NEED_FALLBACK ); +if (IS_ERR (ctx->fallback_tfm)) +return PTR_ERR (ctx->fallback_tfm); + +crypto_aead_set_reqsize (tfm,sizeof (struct aead_request ) + +crypto_aead_reqsize (ctx->fallback_tfm)); +return 0 ; +} + +static int rfc4106_init (struct crypto_aead *tfm) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm); + + ctx->fallback_tfm =crypto_alloc_aead ("rfc4106(gcm(aes))" ,0 ,CRYPTO_ALG_NEED_FALLBACK ); +if (IS_ERR (ctx->fallback_tfm)) +return PTR_ERR (ctx->fallback_tfm); + +crypto_aead_set_reqsize (tfm,sizeof (struct aead_request ) + +crypto_aead_reqsize (ctx->fallback_tfm)); +return 0 ; +} + +static void p10_aead_exit (struct crypto_aead *tfm) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm); + +if (ctx->fallback_tfm) +crypto_free_aead (ctx->fallback_tfm); +} +static int set_authsize (struct crypto_aead *tfm,unsigned int authsize) {switch (authsize) {
@@ -194,13 +232,34 @@ static int set_authsize(struct crypto_aead *tfm, unsigned int authsize)static int p10_aes_gcm_setkey (struct crypto_aead *aead,const u8 *key,unsigned int keylen) { -struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct p10_aes_gcm_ctx *ctx =crypto_tfm_ctx (tfm); +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (aead); +int ret =0 ; + + ctx->simd_set_key =0 ; +if (crypto_simd_usable ()) { +vsx_begin (); + ret =aes_p10_set_encrypt_key (key, keylen *8 , &ctx->enc_key); +vsx_end (); + ctx->simd_set_key =1 ; +if (ret) + ctx->simd_set_key =0 ; + } + + ctx->keylen = keylen; +memcpy (ctx->key, key, keylen); + +return ret ? -EINVAL :0 ; +} + +static int gcm_setkey (struct crypto_aead *aead,const u8 *key, +unsigned int keylen) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (aead);int ret; -vsx_begin (); - ret =aes_p10_set_encrypt_key (key, keylen *8 , &ctx->enc_key); -vsx_end (); + ret =p10_aes_gcm_setkey (aead, key, keylen); + + ret |=crypto_aead_setkey (ctx->fallback_tfm, key, keylen);return ret ? -EINVAL :0 ; }
@@ -208,8 +267,8 @@ static int p10_aes_gcm_setkey(struct crypto_aead *aead, const u8 *key,static int p10_aes_gcm_crypt (struct aead_request *req,u8 *riv,int assoclen,int enc) { -struct crypto_tfm *tfm = req->base.tfm; -struct p10_aes_gcm_ctx *ctx =crypto_tfm_ctx (tfm); +struct crypto_aead *tfm =crypto_aead_reqtfm (req); +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm);u8 databuf[sizeof (struct gcm_ctx ) +PPC_ALIGN ];struct gcm_ctx *gctx =PTR_ALIGN ((void *)databuf,PPC_ALIGN );u8 hashbuf[sizeof (struct Hash_ctx ) +PPC_ALIGN ];
@@ -220,12 +279,24 @@ static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv,unsigned int cryptlen = req->cryptlen;unsigned char ivbuf[AES_BLOCK_SIZE +PPC_ALIGN ];unsigned char *iv =PTR_ALIGN ((void *)ivbuf,PPC_ALIGN ); -int ret; -unsigned long auth_tag_len =crypto_aead_authsize (__crypto_aead_cast (tfm)); +int ret =0 ; +unsigned long auth_tag_len =crypto_aead_authsize (tfm);u8 otag[16 ];int total_processed =0 ;int nbytes; +if (!crypto_simd_usable () || !ctx->simd_set_key || req->cryptlen <64 ) { +struct aead_request *subreq =aead_request_ctx (req); + +aead_request_set_tfm (subreq, ctx->fallback_tfm); +aead_request_set_crypt (subreq, req->src, req->dst, + req->cryptlen, req->iv); +aead_request_set_ad (subreq, req->assoclen); + +return enc ?crypto_aead_encrypt (subreq) : +crypto_aead_decrypt (subreq); + } +memset (databuf,0 ,sizeof (databuf));memset (hashbuf,0 ,sizeof (hashbuf));memset (ivbuf,0 ,sizeof (ivbuf));
@@ -260,6 +331,7 @@ static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv,if (ret)return ret; +vsx_begin ();while ((nbytes = walk.nbytes) >0 && ret ==0 ) {const u8 *src = walk.src.virt.addr;u8 *dst = walk.dst.virt.addr;
@@ -268,7 +340,6 @@ static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv,if (unlikely (nbytes >0 && nbytes <AES_BLOCK_SIZE )) src = dst =memcpy (buf, src, nbytes); -vsx_begin ();if (enc)aes_p10_gcm_encrypt (src, dst, nbytes, &ctx->enc_key, gctx->iv, hash->Htable);
@@ -283,13 +354,13 @@ static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv, total_processed += walk.nbytes; ret =skcipher_walk_done (&walk,0 ); - } +if (ret) +return ret; -if (ret) -return ret; +vsx_begin (); + }/* Finalize hash */ -vsx_begin ();gcm_update (gctx->iv, hash->Htable);finish_tag (gctx, hash, total_processed);vsx_end ();
@@ -305,11 +376,11 @@ static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv,if (crypto_memneq (otag, hash->Htable, auth_tag_len)) {memzero_explicit (hash->Htable,16 ); -return -EBADMSG ; + ret = -EBADMSG ; } } -return 0 ; +return ret; } static int rfc4106_setkey(struct crypto_aead *tfm, const u8 *inkey,
@@ -318,6 +389,10 @@ static int rfc4106_setkey(struct crypto_aead *tfm, const u8 *inkey,struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm);int err; + err =crypto_aead_setkey (ctx->fallback_tfm, inkey, keylen); +if (err) +return err; + keylen -=RFC4106_NONCE_SIZE ; err =p10_aes_gcm_setkey (tfm, inkey, keylen);if (err)
@@ -373,18 +448,20 @@ static struct aead_alg gcm_aes_algs[] = {{ .maxauthsize = 16, .setauthsize = set_authsize, - .setkey = p10_aes_gcm_setkey, + .setkey = gcm_setkey, .encrypt = p10_aes_gcm_encrypt, .decrypt = p10_aes_gcm_decrypt, + .init = p10_aead_init, + .exit = p10_aead_exit, - .base.cra_name = "__gcm(aes)", - .base.cra_driver_name = "__aes_gcm_p10", + .base.cra_name = "gcm(aes)", + .base.cra_driver_name = "aes_gcm_p10", .base.cra_priority = 2100, .base.cra_blocksize = 1, .base.cra_ctxsize =sizeof (struct p10_aes_gcm_ctx )+ 4 * sizeof(u64[2]), .base.cra_module = THIS_MODULE, - .base.cra_flags = CRYPTO_ALG_INTERNAL, + .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK, }, { .ivsize = GCM_RFC4106_IV_SIZE, .maxauthsize = 16,
@@ -392,19 +469,19 @@ static struct aead_alg gcm_aes_algs[] = {{ .setauthsize = rfc4106_setauthsize, .encrypt = rfc4106_encrypt, .decrypt = rfc4106_decrypt, + .init = rfc4106_init, + .exit = p10_aead_exit, - .base.cra_name = "__rfc4106(gcm(aes))", - .base.cra_driver_name = "__rfc4106_aes_gcm_p10", + .base.cra_name = "rfc4106(gcm(aes))", + .base.cra_driver_name = "rfc4106_aes_gcm_p10", .base.cra_priority = 2100, .base.cra_blocksize = 1, .base.cra_ctxsize =sizeof (struct p10_aes_gcm_ctx ) + 4 * sizeof(u64[2]), .base.cra_module = THIS_MODULE, - .base.cra_flags = CRYPTO_ALG_INTERNAL, + .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK, }}; -static struct simd_aead_alg *p10_simd_aeads[ARRAY_SIZE (gcm_aes_algs)]; -static int __init p10_init (void ) {int ret;
@@ -412,21 +489,15 @@ static int __init p10_init(void)if (!cpu_has_feature (CPU_FTR_ARCH_31 ))return 0 ; - ret =simd_register_aeads_compat (gcm_aes_algs, -ARRAY_SIZE (gcm_aes_algs), - p10_simd_aeads); -if (ret) { -simd_unregister_aeads (gcm_aes_algs,ARRAY_SIZE (gcm_aes_algs), - p10_simd_aeads); + ret =crypto_register_aeads (gcm_aes_algs,ARRAY_SIZE (gcm_aes_algs)); +if (ret)return ret; - }return 0 ; } static void __exit p10_exit(void) { -simd_unregister_aeads (gcm_aes_algs,ARRAY_SIZE (gcm_aes_algs), - p10_simd_aeads); +crypto_unregister_aeads (gcm_aes_algs,ARRAY_SIZE (gcm_aes_algs)); }module_init (p10_init);
Signature · 1 line
2.47.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Critical] crypto: p10-aes-gcm - unchecked key memcpy overflow in p10_aes_gcm_setkey() - [High] crypto…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Critical] crypto: p10-aes-gcm - unchecked key memcpy overflow in p10_aes_gcm_setkey()
- [High] crypto: p10-aes-gcm - missing fallback authsize update in set_authsize()
- [High] crypto: p10-aes-gcm - uninitialized fallback subreq in p10_aes_gcm_crypt()
- [Medium] crypto: p10-aes-gcm - missing CRYPTO_ALG_ASYNC mask in p10_aead_init()
- [Medium] crypto: p10-aes-gcm - missing CRYPTO_GCM select for CRYPTO_AES_GCM_P10
--
commit 0ea59836db10f05695eb5711fdb48763c557e6c5
Author: Danny Tsen <[email protected]>
crypto: powerpc/p10-aes-gcm - Fix IPsec stall on bond failover
This commit replaces the crypto SIMD wrapper registration for Power10 AES-GCM with direct aead_alg registration and synchronous fallback transforms for requests that cannot use VSX or have cryptlen < 64.
Quoted 4 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +0
-0
[ ... ]
Quoted 19 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +15
-0@@ -173,6 +177,40 @@ static void finish_tag(struct gcm_ctx *gctx, struct Hash_ctx *hash, int len) hash->Htable[i] ^= gctx->ivtag[i]; } +static int p10_aead_init (struct crypto_aead *tfm) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm); + + ctx->fallback_tfm =crypto_alloc_aead ("gcm(aes)" ,0 ,CRYPTO_ALG_NEED_FALLBACK ); +if (IS_ERR (ctx->fallback_tfm)) +return PTR_ERR (ctx->fallback_tfm); + +crypto_aead_set_reqsize (tfm,sizeof (struct aead_request ) + +crypto_aead_reqsize (ctx->fallback_tfm)); +return 0 ; +} + +static int rfc4106_init (struct crypto_aead *tfm) +{
arch/powerpc/crypto/aes-gcm-p10-glue.c static void finish_tag(struct gcm_ctx *gctx, struct Hash_ctx *hash, int len) · line 204
Quoted 10 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +10
-0+struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm); + + ctx->fallback_tfm =crypto_alloc_aead ("rfc4106(gcm(aes))" ,0 ,CRYPTO_ALG_NEED_FALLBACK ); +if (IS_ERR (ctx->fallback_tfm)) +return PTR_ERR (ctx->fallback_tfm); + +crypto_aead_set_reqsize (tfm,sizeof (struct aead_request ) + +crypto_aead_reqsize (ctx->fallback_tfm)); +return 0 ; +}
[Severity: Medium]
Should p10_aead_init() and rfc4106_init() pass CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC as the mask to crypto_alloc_aead()?
gcm_aes_algs[] registers synchronous AEAD algorithms without CRYPTO_ALG_ASYNC, so if an asynchronous implementation with higher priority than the generic fallback is bound to ctx->fallback_tfm, it can return -EINPROGRESS or -EBUSY to callers expecting synchronous completion.
[Severity: Medium]
Should CRYPTO_AES_GCM_P10 in arch/powerpc/crypto/Kconfig also select CRYPTO_GCM and CRYPTO_AES now that p10_aead_init() and rfc4106_init() unconditionally allocate fallback gcm(aes) and rfc4106(gcm(aes)) transforms?
On powerpc, the library-based gcm-aes-lib AEAD registrations in crypto/aes.c are disabled under !CONFIG_PPC, so transform initialization fails with -ENOENT when CONFIG_CRYPTO_GCM or CONFIG_CRYPTO_AES is not enabled.
Quoted 42 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +32
-2+ +static void p10_aead_exit (struct crypto_aead *tfm) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm); + +if (ctx->fallback_tfm) +crypto_free_aead (ctx->fallback_tfm); +} +static int set_authsize (struct crypto_aead *tfm,unsigned int authsize) {switch (authsize) {@@ -194,13 +232,34 @@ static int set_authsize(struct crypto_aead *tfm, unsigned int authsize)static int p10_aes_gcm_setkey (struct crypto_aead *aead,const u8 *key,unsigned int keylen) { -struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct p10_aes_gcm_ctx *ctx =crypto_tfm_ctx (tfm); +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (aead); +int ret =0 ; + + ctx->simd_set_key =0 ; +if (crypto_simd_usable ()) { +vsx_begin (); + ret =aes_p10_set_encrypt_key (key, keylen *8 , &ctx->enc_key); +vsx_end (); + ctx->simd_set_key =1 ; +if (ret) + ctx->simd_set_key =0 ; + } + + ctx->keylen = keylen; +memcpy (ctx->key, key, keylen); + +return ret ? -EINVAL :0 ; +} + +static int gcm_setkey (struct crypto_aead *aead,const u8 *key, +unsigned int keylen) +{ +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (aead);int ret;
arch/powerpc/crypto/aes-gcm-p10-glue.c static int set_authsize(struct crypto_aead *tfm, unsigned int authsize) · line 265
Quoted 10 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +3
-3-vsx_begin (); - ret =aes_p10_set_encrypt_key (key, keylen *8 , &ctx->enc_key); -vsx_end (); + ret =p10_aes_gcm_setkey (aead, key, keylen); + + ret |=crypto_aead_setkey (ctx->fallback_tfm, key, keylen);return ret ? -EINVAL :0 ; }
[Severity: Critical]
Can memcpy(ctx->key, key, keylen) in p10_aes_gcm_setkey() overflow ctx->key[AES_MAX_KEYLENGTH]?
gcm_setkey() calls p10_aes_gcm_setkey() before crypto_aead_setkey() validates keylen, and p10_aes_gcm_setkey() copies keylen bytes even when aes_p10_set_encrypt_key() fails or is skipped. An unprivileged user can pass keylen > 240 via setsockopt(ALG_SET_KEY) on an AF_ALG socket bound to gcm(aes) and write past ctx->key in struct p10_aes_gcm_ctx. ctx->key and ctx->keylen are also never read anywhere in this driver and can be dropped.
Quoted 24 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +5
-4@@ -208,8 +267,8 @@ static int p10_aes_gcm_setkey(struct crypto_aead *aead, const u8 *key,static int p10_aes_gcm_crypt (struct aead_request *req,u8 *riv,int assoclen,int enc) { -struct crypto_tfm *tfm = req->base.tfm; -struct p10_aes_gcm_ctx *ctx =crypto_tfm_ctx (tfm); +struct crypto_aead *tfm =crypto_aead_reqtfm (req); +struct p10_aes_gcm_ctx *ctx =crypto_aead_ctx (tfm);u8 databuf[sizeof (struct gcm_ctx ) +PPC_ALIGN ];struct gcm_ctx *gctx =PTR_ALIGN ((void *)databuf,PPC_ALIGN );u8 hashbuf[sizeof (struct Hash_ctx ) +PPC_ALIGN ];@@ -220,12 +279,24 @@ static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv,unsigned int cryptlen = req->cryptlen;unsigned char ivbuf[AES_BLOCK_SIZE +PPC_ALIGN ];unsigned char *iv =PTR_ALIGN ((void *)ivbuf,PPC_ALIGN ); -int ret; -unsigned long auth_tag_len =crypto_aead_authsize (__crypto_aead_cast (tfm)); +int ret =0 ; +unsigned long auth_tag_len =crypto_aead_authsize (tfm);u8 otag[16 ];int total_processed =0 ;int nbytes; +if (!crypto_simd_usable () || !ctx->simd_set_key || req->cryptlen <64 ) {
arch/powerpc/crypto/aes-gcm-p10-glue.c static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv, · line 298
Quoted 10 lines
arch/powerpc/crypto/aes-gcm-p10-glue.c +10
-0+struct aead_request *subreq =aead_request_ctx (req); + +aead_request_set_tfm (subreq, ctx->fallback_tfm); +aead_request_set_crypt (subreq, req->src, req->dst, + req->cryptlen, req->iv); +aead_request_set_ad (subreq, req->assoclen); + +return enc ?crypto_aead_encrypt (subreq) : +crypto_aead_decrypt (subreq); + }
[Severity: High]
Does the fallback path use the wrong tag size when authsize is configured to less than 16 bytes?
Neither set_authsize() nor rfc4106_setauthsize() calls crypto_aead_setauthsize(ctx->fallback_tfm, authsize), so ctx->fallback_tfm->authsize stays at 16. When a transform configured with a shorter tag (such as 8 or 12 bytes) takes the fallback path, encryption writes a 16-byte tag past the caller's tag buffer (or walks off the end of req->dst) and decryption fails with -EINVAL or -EBADMSG.
[Severity: High]
Does the fallback path leave subreq->base.flags, subreq->base.complete, and subreq->base.data uninitialized by not calling aead_request_set_callback()?
crypto_gcm_encrypt() and crypto_gcm_decrypt() read aead_request_flags(req) and forward it to the underlying skcipher request. When p10_aes_gcm_crypt() falls back in softirq context (or during crypto self-tests, where testmgr poisons the request context with 0xfe) and CRYPTO_TFM_REQ_MAY_SLEEP happens to be set in subreq->base.flags, the skcipher walk can perform GFP_KERNEL allocations and call cond_resched() in atomic context.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1