Skip to content

Commit f8d33fa

Browse files
ebiggersherbertx
authored andcommitted
crypto: skcipher - prevent using skciphers without setting key
Similar to what was done for the hash API, update the skcipher API to track whether each transform has been keyed, and reject encryption/decryption if a key is needed but one hasn't been set. This isn't as important as the equivalent fix for the hash API because symmetric ciphers almost always require a key (the "null cipher" is the only exception), so are unlikely to be used without one. Still, tracking the key will prevent accidental unkeyed use. algif_skcipher also had to track the key anyway, so the new flag replaces that and simplifies the algif_skcipher implementation. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 4e1d14b commit f8d33fa

3 files changed

Lines changed: 45 additions & 55 deletions

File tree

‎crypto/algif_skcipher.c‎

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,14 @@
3838
#include <linux/net.h>
3939
#include <net/sock.h>
4040

41-
struct skcipher_tfm {
42-
struct crypto_skcipher *skcipher;
43-
bool has_key;
44-
};
45-
4641
static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
4742
size_t size)
4843
{
4944
struct sock *sk = sock->sk;
5045
struct alg_sock *ask = alg_sk(sk);
5146
struct sock *psk = ask->parent;
5247
struct alg_sock *pask = alg_sk(psk);
53-
struct skcipher_tfm *skc = pask->private;
54-
struct crypto_skcipher *tfm = skc->skcipher;
48+
struct crypto_skcipher *tfm = pask->private;
5549
unsigned ivsize = crypto_skcipher_ivsize(tfm);
5650

5751
return af_alg_sendmsg(sock, msg, size, ivsize);
@@ -65,8 +59,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
6559
struct sock *psk = ask->parent;
6660
struct alg_sock *pask = alg_sk(psk);
6761
struct af_alg_ctx *ctx = ask->private;
68-
struct skcipher_tfm *skc = pask->private;
69-
struct crypto_skcipher *tfm = skc->skcipher;
62+
struct crypto_skcipher *tfm = pask->private;
7063
unsigned int bs = crypto_skcipher_blocksize(tfm);
7164
struct af_alg_async_req *areq;
7265
int err = 0;
@@ -221,7 +214,7 @@ static int skcipher_check_key(struct socket *sock)
221214
int err = 0;
222215
struct sock *psk;
223216
struct alg_sock *pask;
224-
struct skcipher_tfm *tfm;
217+
struct crypto_skcipher *tfm;
225218
struct sock *sk = sock->sk;
226219
struct alg_sock *ask = alg_sk(sk);
227220

@@ -235,7 +228,7 @@ static int skcipher_check_key(struct socket *sock)
235228

236229
err = -ENOKEY;
237230
lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
238-
if (!tfm->has_key)
231+
if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
239232
goto unlock;
240233

241234
if (!pask->refcnt++)
@@ -314,41 +307,17 @@ static struct proto_ops algif_skcipher_ops_nokey = {
314307

315308
static void *skcipher_bind(const char *name, u32 type, u32 mask)
316309
{
317-
struct skcipher_tfm *tfm;
318-
struct crypto_skcipher *skcipher;
319-
320-
tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
321-
if (!tfm)
322-
return ERR_PTR(-ENOMEM);
323-
324-
skcipher = crypto_alloc_skcipher(name, type, mask);
325-
if (IS_ERR(skcipher)) {
326-
kfree(tfm);
327-
return ERR_CAST(skcipher);
328-
}
329-
330-
tfm->skcipher = skcipher;
331-
332-
return tfm;
310+
return crypto_alloc_skcipher(name, type, mask);
333311
}
334312

335313
static void skcipher_release(void *private)
336314
{
337-
struct skcipher_tfm *tfm = private;
338-
339-
crypto_free_skcipher(tfm->skcipher);
340-
kfree(tfm);
315+
crypto_free_skcipher(private);
341316
}
342317

343318
static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
344319
{
345-
struct skcipher_tfm *tfm = private;
346-
int err;
347-
348-
err = crypto_skcipher_setkey(tfm->skcipher, key, keylen);
349-
tfm->has_key = !err;
350-
351-
return err;
320+
return crypto_skcipher_setkey(private, key, keylen);
352321
}
353322

354323
static void skcipher_sock_destruct(struct sock *sk)
@@ -357,8 +326,7 @@ static void skcipher_sock_destruct(struct sock *sk)
357326
struct af_alg_ctx *ctx = ask->private;
358327
struct sock *psk = ask->parent;
359328
struct alg_sock *pask = alg_sk(psk);
360-
struct skcipher_tfm *skc = pask->private;
361-
struct crypto_skcipher *tfm = skc->skcipher;
329+
struct crypto_skcipher *tfm = pask->private;
362330

363331
af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
364332
sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
@@ -370,22 +338,21 @@ static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
370338
{
371339
struct af_alg_ctx *ctx;
372340
struct alg_sock *ask = alg_sk(sk);
373-
struct skcipher_tfm *tfm = private;
374-
struct crypto_skcipher *skcipher = tfm->skcipher;
341+
struct crypto_skcipher *tfm = private;
375342
unsigned int len = sizeof(*ctx);
376343

377344
ctx = sock_kmalloc(sk, len, GFP_KERNEL);
378345
if (!ctx)
379346
return -ENOMEM;
380347

381-
ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher),
348+
ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(tfm),
382349
GFP_KERNEL);
383350
if (!ctx->iv) {
384351
sock_kfree_s(sk, ctx, len);
385352
return -ENOMEM;
386353
}
387354

388-
memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher));
355+
memset(ctx->iv, 0, crypto_skcipher_ivsize(tfm));
389356

390357
INIT_LIST_HEAD(&ctx->tsgl_list);
391358
ctx->len = len;
@@ -405,9 +372,9 @@ static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
405372

406373
static int skcipher_accept_parent(void *private, struct sock *sk)
407374
{
408-
struct skcipher_tfm *tfm = private;
375+
struct crypto_skcipher *tfm = private;
409376

410-
if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher))
377+
if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
411378
return -ENOKEY;
412379

413380
return skcipher_accept_parent_nokey(private, sk);

‎crypto/skcipher.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,11 @@ static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
598598
err = crypto_blkcipher_setkey(blkcipher, key, keylen);
599599
crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
600600
CRYPTO_TFM_RES_MASK);
601+
if (err)
602+
return err;
601603

602-
return err;
604+
crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
605+
return 0;
603606
}
604607

605608
static int skcipher_crypt_blkcipher(struct skcipher_request *req,
@@ -674,6 +677,9 @@ static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
674677
skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
675678
skcipher->keysize = calg->cra_blkcipher.max_keysize;
676679

680+
if (skcipher->keysize)
681+
crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY);
682+
677683
return 0;
678684
}
679685

@@ -692,8 +698,11 @@ static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
692698
crypto_skcipher_set_flags(tfm,
693699
crypto_ablkcipher_get_flags(ablkcipher) &
694700
CRYPTO_TFM_RES_MASK);
701+
if (err)
702+
return err;
695703

696-
return err;
704+
crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
705+
return 0;
697706
}
698707

699708
static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
@@ -767,6 +776,9 @@ static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
767776
sizeof(struct ablkcipher_request);
768777
skcipher->keysize = calg->cra_ablkcipher.max_keysize;
769778

779+
if (skcipher->keysize)
780+
crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY);
781+
770782
return 0;
771783
}
772784

@@ -796,16 +808,23 @@ static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
796808
{
797809
struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
798810
unsigned long alignmask = crypto_skcipher_alignmask(tfm);
811+
int err;
799812

800813
if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
801814
crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
802815
return -EINVAL;
803816
}
804817

805818
if ((unsigned long)key & alignmask)
806-
return skcipher_setkey_unaligned(tfm, key, keylen);
819+
err = skcipher_setkey_unaligned(tfm, key, keylen);
820+
else
821+
err = cipher->setkey(tfm, key, keylen);
822+
823+
if (err)
824+
return err;
807825

808-
return cipher->setkey(tfm, key, keylen);
826+
crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
827+
return 0;
809828
}
810829

811830
static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
@@ -834,6 +853,9 @@ static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
834853
skcipher->ivsize = alg->ivsize;
835854
skcipher->keysize = alg->max_keysize;
836855

856+
if (skcipher->keysize)
857+
crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY);
858+
837859
if (alg->exit)
838860
skcipher->base.exit = crypto_skcipher_exit_tfm;
839861

‎include/crypto/skcipher.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,6 @@ static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
401401
return tfm->setkey(tfm, key, keylen);
402402
}
403403

404-
static inline bool crypto_skcipher_has_setkey(struct crypto_skcipher *tfm)
405-
{
406-
return tfm->keysize;
407-
}
408-
409404
static inline unsigned int crypto_skcipher_default_keysize(
410405
struct crypto_skcipher *tfm)
411406
{
@@ -442,6 +437,9 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
442437
{
443438
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
444439

440+
if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
441+
return -ENOKEY;
442+
445443
return tfm->encrypt(req);
446444
}
447445

@@ -460,6 +458,9 @@ static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
460458
{
461459
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
462460

461+
if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
462+
return -ENOKEY;
463+
463464
return tfm->decrypt(req);
464465
}
465466

0 commit comments

Comments
 (0)