crypto: drbg - prevent invalid SG mappings
authorStephan Mueller <smueller@chronox.de>
Tue, 29 Nov 2016 08:45:04 +0000 (09:45 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Wed, 30 Nov 2016 11:46:44 +0000 (19:46 +0800)
When using SGs, only heap memory (memory that is valid as per
virt_addr_valid) is allowed to be referenced. The CTR DRBG used to
reference the caller-provided memory directly in an SG. In case the
caller provided stack memory pointers, the SG mapping is not considered
to be valid. In some cases, this would even cause a paging fault.

The change adds a new scratch buffer that is used unconditionally to
catch the cases where the caller-provided buffer is not suitable for
use in an SG. The crypto operation of the CTR DRBG produces its output
with that scratch buffer and finally copies the content of the
scratch buffer to the caller's buffer.

The scratch buffer is allocated during allocation time of the CTR DRBG
as its access is protected with the DRBG mutex.

Signed-off-by: Stephan Mueller <smueller@chronox.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/drbg.c
include/crypto/drbg.h

index fb33f7d3b052f5bb016342c1da2ef61b9f74ad8e..053035b5c8f85686446df108ccbf3675a19cd056 100644 (file)
@@ -262,6 +262,7 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
                              u8 *inbuf, u32 inbuflen,
                              u8 *outbuf, u32 outlen);
 #define DRBG_CTR_NULL_LEN 128
+#define DRBG_OUTSCRATCHLEN DRBG_CTR_NULL_LEN
 
 /* BCC function for CTR DRBG as defined in 10.4.3 */
 static int drbg_ctr_bcc(struct drbg_state *drbg,
@@ -1644,6 +1645,9 @@ static int drbg_fini_sym_kernel(struct drbg_state *drbg)
        kfree(drbg->ctr_null_value_buf);
        drbg->ctr_null_value = NULL;
 
+       kfree(drbg->outscratchpadbuf);
+       drbg->outscratchpadbuf = NULL;
+
        return 0;
 }
 
@@ -1708,6 +1712,15 @@ static int drbg_init_sym_kernel(struct drbg_state *drbg)
        drbg->ctr_null_value = (u8 *)PTR_ALIGN(drbg->ctr_null_value_buf,
                                               alignmask + 1);
 
+       drbg->outscratchpadbuf = kmalloc(DRBG_OUTSCRATCHLEN + alignmask,
+                                        GFP_KERNEL);
+       if (!drbg->outscratchpadbuf) {
+               drbg_fini_sym_kernel(drbg);
+               return -ENOMEM;
+       }
+       drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf,
+                                             alignmask + 1);
+
        return alignmask;
 }
 
@@ -1737,15 +1750,16 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
                              u8 *outbuf, u32 outlen)
 {
        struct scatterlist sg_in;
+       int ret;
 
        sg_init_one(&sg_in, inbuf, inlen);
 
        while (outlen) {
-               u32 cryptlen = min_t(u32, inlen, outlen);
+               u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
                struct scatterlist sg_out;
-               int ret;
 
-               sg_init_one(&sg_out, outbuf, cryptlen);
+               /* Output buffer may not be valid for SGL, use scratchpad */
+               sg_init_one(&sg_out, drbg->outscratchpad, cryptlen);
                skcipher_request_set_crypt(drbg->ctr_req, &sg_in, &sg_out,
                                           cryptlen, drbg->V);
                ret = crypto_skcipher_encrypt(drbg->ctr_req);
@@ -1761,14 +1775,19 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
                                break;
                        }
                default:
-                       return ret;
+                       goto out;
                }
                init_completion(&drbg->ctr_completion);
 
+               memcpy(outbuf, drbg->outscratchpad, cryptlen);
+
                outlen -= cryptlen;
        }
+       ret = 0;
 
-       return 0;
+out:
+       memzero_explicit(drbg->outscratchpad, DRBG_OUTSCRATCHLEN);
+       return ret;
 }
 #endif /* CONFIG_CRYPTO_DRBG_CTR */
 
index 61580b19f9f6e1e31e42fc6afcd0f416cc2c5f2a..22f884c97387e9929a4fb19e7ee5328bccb34c8d 100644 (file)
@@ -124,6 +124,8 @@ struct drbg_state {
        struct skcipher_request *ctr_req;       /* CTR mode request handle */
        __u8 *ctr_null_value_buf;               /* CTR mode unaligned buffer */
        __u8 *ctr_null_value;                   /* CTR mode aligned zero buf */
+       __u8 *outscratchpadbuf;                 /* CTR mode output scratchpad */
+        __u8 *outscratchpad;                   /* CTR mode aligned outbuf */
        struct completion ctr_completion;       /* CTR mode async handler */
        int ctr_async_err;                      /* CTR mode async error */