Skip to content

Commit ee97dc7

Browse files
Harald Freudenbergerherbertx
authored andcommitted
crypto: s390 - fix des and des3_ede ctr concurrency issue
In s390 des and 3des ctr mode there is one preallocated page used to speed up the en/decryption. This page is not protected against concurrent usage and thus there is a potential of data corruption with multiple threads. The fix introduces locking/unlocking the ctr page and a slower fallback solution at concurrency situations. Cc: stable@vger.kernel.org Signed-off-by: Harald Freudenberger <freude@linux.vnet.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent adc3fcf commit ee97dc7

1 file changed

Lines changed: 48 additions & 21 deletions

File tree

‎arch/s390/crypto/des_s390.c‎

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
2626

2727
static u8 *ctrblk;
28+
static DEFINE_SPINLOCK(ctrblk_lock);
2829

2930
struct s390_des_ctx {
3031
u8 iv[DES_BLOCK_SIZE];
@@ -368,54 +369,80 @@ static struct crypto_alg cbc_des3_alg = {
368369
}
369370
};
370371

372+
static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
373+
{
374+
unsigned int i, n;
375+
376+
/* align to block size, max. PAGE_SIZE */
377+
n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
378+
for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
379+
memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
380+
crypto_inc(ctrptr + i, DES_BLOCK_SIZE);
381+
}
382+
return n;
383+
}
384+
371385
static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
372-
struct s390_des_ctx *ctx, struct blkcipher_walk *walk)
386+
struct s390_des_ctx *ctx,
387+
struct blkcipher_walk *walk)
373388
{
374389
int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
375-
unsigned int i, n, nbytes;
376-
u8 buf[DES_BLOCK_SIZE];
377-
u8 *out, *in;
390+
unsigned int n, nbytes;
391+
u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
392+
u8 *out, *in, *ctrptr = ctrbuf;
393+
394+
if (!walk->nbytes)
395+
return ret;
378396

379-
memcpy(ctrblk, walk->iv, DES_BLOCK_SIZE);
397+
if (spin_trylock(&ctrblk_lock))
398+
ctrptr = ctrblk;
399+
400+
memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
380401
while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
381402
out = walk->dst.virt.addr;
382403
in = walk->src.virt.addr;
383404
while (nbytes >= DES_BLOCK_SIZE) {
384-
/* align to block size, max. PAGE_SIZE */
385-
n = (nbytes > PAGE_SIZE) ? PAGE_SIZE :
386-
nbytes & ~(DES_BLOCK_SIZE - 1);
387-
for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
388-
memcpy(ctrblk + i, ctrblk + i - DES_BLOCK_SIZE,
389-
DES_BLOCK_SIZE);
390-
crypto_inc(ctrblk + i, DES_BLOCK_SIZE);
391-
}
392-
ret = crypt_s390_kmctr(func, ctx->key, out, in, n, ctrblk);
393-
if (ret < 0 || ret != n)
405+
if (ctrptr == ctrblk)
406+
n = __ctrblk_init(ctrptr, nbytes);
407+
else
408+
n = DES_BLOCK_SIZE;
409+
ret = crypt_s390_kmctr(func, ctx->key, out, in,
410+
n, ctrptr);
411+
if (ret < 0 || ret != n) {
412+
if (ctrptr == ctrblk)
413+
spin_unlock(&ctrblk_lock);
394414
return -EIO;
415+
}
395416
if (n > DES_BLOCK_SIZE)
396-
memcpy(ctrblk, ctrblk + n - DES_BLOCK_SIZE,
417+
memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
397418
DES_BLOCK_SIZE);
398-
crypto_inc(ctrblk, DES_BLOCK_SIZE);
419+
crypto_inc(ctrptr, DES_BLOCK_SIZE);
399420
out += n;
400421
in += n;
401422
nbytes -= n;
402423
}
403424
ret = blkcipher_walk_done(desc, walk, nbytes);
404425
}
405-
426+
if (ctrptr == ctrblk) {
427+
if (nbytes)
428+
memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
429+
else
430+
memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
431+
spin_unlock(&ctrblk_lock);
432+
}
406433
/* final block may be < DES_BLOCK_SIZE, copy only nbytes */
407434
if (nbytes) {
408435
out = walk->dst.virt.addr;
409436
in = walk->src.virt.addr;
410437
ret = crypt_s390_kmctr(func, ctx->key, buf, in,
411-
DES_BLOCK_SIZE, ctrblk);
438+
DES_BLOCK_SIZE, ctrbuf);
412439
if (ret < 0 || ret != DES_BLOCK_SIZE)
413440
return -EIO;
414441
memcpy(out, buf, nbytes);
415-
crypto_inc(ctrblk, DES_BLOCK_SIZE);
442+
crypto_inc(ctrbuf, DES_BLOCK_SIZE);
416443
ret = blkcipher_walk_done(desc, walk, 0);
444+
memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
417445
}
418-
memcpy(walk->iv, ctrblk, DES_BLOCK_SIZE);
419446
return ret;
420447
}
421448

0 commit comments

Comments
 (0)