Skip to content

Commit 5a9617d

Browse files
committed
Merge tag 'v7.0-p4' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
Pull crypto fixes from Herbert Xu: - Add missing async markers to tegra - Fix long hmac key DMA handling in caam - Fix spurious ENOSPC errors in deflate - Fix SG chaining in af_alg - Do not use in-place process in algif_aead - Fix out-of-place destination overflow in authencesn * tag 'v7.0-p4' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: crypto: authencesn - Do not place hiseq at end of dst for out-of-place decryption crypto: algif_aead - Revert to operating out-of-place crypto: af-alg - fix NULL pointer dereference in scatterwalk crypto: deflate - fix spurious -ENOSPC crypto: caam - fix overflow on long hmac keys crypto: caam - fix DMA corruption on long hmac keys crypto: tegra - Add missing CRYPTO_ALG_ASYNC
2 parents 19abf08 + e024941 commit 5a9617d

10 files changed

Lines changed: 101 additions & 169 deletions

File tree

crypto/af_alg.c

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,10 @@ static int af_alg_alloc_tsgl(struct sock *sk)
623623
sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
624624
sgl->cur = 0;
625625

626-
if (sg)
626+
if (sg) {
627+
sg_unmark_end(sg + MAX_SGL_ENTS - 1);
627628
sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
629+
}
628630

629631
list_add_tail(&sgl->list, &ctx->tsgl_list);
630632
}
@@ -635,15 +637,13 @@ static int af_alg_alloc_tsgl(struct sock *sk)
635637
/**
636638
* af_alg_count_tsgl - Count number of TX SG entries
637639
*
638-
* The counting starts from the beginning of the SGL to @bytes. If
639-
* an @offset is provided, the counting of the SG entries starts at the @offset.
640+
* The counting starts from the beginning of the SGL to @bytes.
640641
*
641642
* @sk: socket of connection to user space
642643
* @bytes: Count the number of SG entries holding given number of bytes.
643-
* @offset: Start the counting of SG entries from the given offset.
644644
* Return: Number of TX SG entries found given the constraints
645645
*/
646-
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
646+
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes)
647647
{
648648
const struct alg_sock *ask = alg_sk(sk);
649649
const struct af_alg_ctx *ctx = ask->private;
@@ -658,25 +658,11 @@ unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
658658
const struct scatterlist *sg = sgl->sg;
659659

660660
for (i = 0; i < sgl->cur; i++) {
661-
size_t bytes_count;
662-
663-
/* Skip offset */
664-
if (offset >= sg[i].length) {
665-
offset -= sg[i].length;
666-
bytes -= sg[i].length;
667-
continue;
668-
}
669-
670-
bytes_count = sg[i].length - offset;
671-
672-
offset = 0;
673661
sgl_count++;
674-
675-
/* If we have seen requested number of bytes, stop */
676-
if (bytes_count >= bytes)
662+
if (sg[i].length >= bytes)
677663
return sgl_count;
678664

679-
bytes -= bytes_count;
665+
bytes -= sg[i].length;
680666
}
681667
}
682668

@@ -688,19 +674,14 @@ EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
688674
* af_alg_pull_tsgl - Release the specified buffers from TX SGL
689675
*
690676
* If @dst is non-null, reassign the pages to @dst. The caller must release
691-
* the pages. If @dst_offset is given only reassign the pages to @dst starting
692-
* at the @dst_offset (byte). The caller must ensure that @dst is large
693-
* enough (e.g. by using af_alg_count_tsgl with the same offset).
677+
* the pages.
694678
*
695679
* @sk: socket of connection to user space
696680
* @used: Number of bytes to pull from TX SGL
697681
* @dst: If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
698682
* caller must release the buffers in dst.
699-
* @dst_offset: Reassign the TX SGL from given offset. All buffers before
700-
* reaching the offset is released.
701683
*/
702-
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
703-
size_t dst_offset)
684+
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst)
704685
{
705686
struct alg_sock *ask = alg_sk(sk);
706687
struct af_alg_ctx *ctx = ask->private;
@@ -725,18 +706,10 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
725706
* SG entries in dst.
726707
*/
727708
if (dst) {
728-
if (dst_offset >= plen) {
729-
/* discard page before offset */
730-
dst_offset -= plen;
731-
} else {
732-
/* reassign page to dst after offset */
733-
get_page(page);
734-
sg_set_page(dst + j, page,
735-
plen - dst_offset,
736-
sg[i].offset + dst_offset);
737-
dst_offset = 0;
738-
j++;
739-
}
709+
/* reassign page to dst after offset */
710+
get_page(page);
711+
sg_set_page(dst + j, page, plen, sg[i].offset);
712+
j++;
740713
}
741714

742715
sg[i].length -= plen;

crypto/algif_aead.c

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <crypto/internal/aead.h>
2727
#include <crypto/scatterwalk.h>
2828
#include <crypto/if_alg.h>
29-
#include <crypto/skcipher.h>
3029
#include <linux/init.h>
3130
#include <linux/list.h>
3231
#include <linux/kernel.h>
@@ -72,9 +71,8 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
7271
struct alg_sock *pask = alg_sk(psk);
7372
struct af_alg_ctx *ctx = ask->private;
7473
struct crypto_aead *tfm = pask->private;
75-
unsigned int i, as = crypto_aead_authsize(tfm);
74+
unsigned int as = crypto_aead_authsize(tfm);
7675
struct af_alg_async_req *areq;
77-
struct af_alg_tsgl *tsgl, *tmp;
7876
struct scatterlist *rsgl_src, *tsgl_src = NULL;
7977
int err = 0;
8078
size_t used = 0; /* [in] TX bufs to be en/decrypted */
@@ -154,23 +152,24 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
154152
outlen -= less;
155153
}
156154

155+
/*
156+
* Create a per request TX SGL for this request which tracks the
157+
* SG entries from the global TX SGL.
158+
*/
157159
processed = used + ctx->aead_assoclen;
158-
list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
159-
for (i = 0; i < tsgl->cur; i++) {
160-
struct scatterlist *process_sg = tsgl->sg + i;
161-
162-
if (!(process_sg->length) || !sg_page(process_sg))
163-
continue;
164-
tsgl_src = process_sg;
165-
break;
166-
}
167-
if (tsgl_src)
168-
break;
169-
}
170-
if (processed && !tsgl_src) {
171-
err = -EFAULT;
160+
areq->tsgl_entries = af_alg_count_tsgl(sk, processed);
161+
if (!areq->tsgl_entries)
162+
areq->tsgl_entries = 1;
163+
areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
164+
areq->tsgl_entries),
165+
GFP_KERNEL);
166+
if (!areq->tsgl) {
167+
err = -ENOMEM;
172168
goto free;
173169
}
170+
sg_init_table(areq->tsgl, areq->tsgl_entries);
171+
af_alg_pull_tsgl(sk, processed, areq->tsgl);
172+
tsgl_src = areq->tsgl;
174173

175174
/*
176175
* Copy of AAD from source to destination
@@ -179,76 +178,15 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
179178
* when user space uses an in-place cipher operation, the kernel
180179
* will copy the data as it does not see whether such in-place operation
181180
* is initiated.
182-
*
183-
* To ensure efficiency, the following implementation ensure that the
184-
* ciphers are invoked to perform a crypto operation in-place. This
185-
* is achieved by memory management specified as follows.
186181
*/
187182

188183
/* Use the RX SGL as source (and destination) for crypto op. */
189184
rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
190185

191-
if (ctx->enc) {
192-
/*
193-
* Encryption operation - The in-place cipher operation is
194-
* achieved by the following operation:
195-
*
196-
* TX SGL: AAD || PT
197-
* | |
198-
* | copy |
199-
* v v
200-
* RX SGL: AAD || PT || Tag
201-
*/
202-
memcpy_sglist(areq->first_rsgl.sgl.sgt.sgl, tsgl_src,
203-
processed);
204-
af_alg_pull_tsgl(sk, processed, NULL, 0);
205-
} else {
206-
/*
207-
* Decryption operation - To achieve an in-place cipher
208-
* operation, the following SGL structure is used:
209-
*
210-
* TX SGL: AAD || CT || Tag
211-
* | | ^
212-
* | copy | | Create SGL link.
213-
* v v |
214-
* RX SGL: AAD || CT ----+
215-
*/
216-
217-
/* Copy AAD || CT to RX SGL buffer for in-place operation. */
218-
memcpy_sglist(areq->first_rsgl.sgl.sgt.sgl, tsgl_src, outlen);
219-
220-
/* Create TX SGL for tag and chain it to RX SGL. */
221-
areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
222-
processed - as);
223-
if (!areq->tsgl_entries)
224-
areq->tsgl_entries = 1;
225-
areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
226-
areq->tsgl_entries),
227-
GFP_KERNEL);
228-
if (!areq->tsgl) {
229-
err = -ENOMEM;
230-
goto free;
231-
}
232-
sg_init_table(areq->tsgl, areq->tsgl_entries);
233-
234-
/* Release TX SGL, except for tag data and reassign tag data. */
235-
af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
236-
237-
/* chain the areq TX SGL holding the tag with RX SGL */
238-
if (usedpages) {
239-
/* RX SGL present */
240-
struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
241-
struct scatterlist *sg = sgl_prev->sgt.sgl;
242-
243-
sg_unmark_end(sg + sgl_prev->sgt.nents - 1);
244-
sg_chain(sg, sgl_prev->sgt.nents + 1, areq->tsgl);
245-
} else
246-
/* no RX SGL present (e.g. authentication only) */
247-
rsgl_src = areq->tsgl;
248-
}
186+
memcpy_sglist(rsgl_src, tsgl_src, ctx->aead_assoclen);
249187

250188
/* Initialize the crypto operation */
251-
aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
189+
aead_request_set_crypt(&areq->cra_u.aead_req, tsgl_src,
252190
areq->first_rsgl.sgl.sgt.sgl, used, ctx->iv);
253191
aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
254192
aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
@@ -450,7 +388,7 @@ static void aead_sock_destruct(struct sock *sk)
450388
struct crypto_aead *tfm = pask->private;
451389
unsigned int ivlen = crypto_aead_ivsize(tfm);
452390

453-
af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
391+
af_alg_pull_tsgl(sk, ctx->used, NULL);
454392
sock_kzfree_s(sk, ctx->iv, ivlen);
455393
sock_kfree_s(sk, ctx, ctx->len);
456394
af_alg_release_parent(sk);

crypto/algif_skcipher.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
138138
* Create a per request TX SGL for this request which tracks the
139139
* SG entries from the global TX SGL.
140140
*/
141-
areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
141+
areq->tsgl_entries = af_alg_count_tsgl(sk, len);
142142
if (!areq->tsgl_entries)
143143
areq->tsgl_entries = 1;
144144
areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
@@ -149,7 +149,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
149149
goto free;
150150
}
151151
sg_init_table(areq->tsgl, areq->tsgl_entries);
152-
af_alg_pull_tsgl(sk, len, areq->tsgl, 0);
152+
af_alg_pull_tsgl(sk, len, areq->tsgl);
153153

154154
/* Initialize the crypto operation */
155155
skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
@@ -363,7 +363,7 @@ static void skcipher_sock_destruct(struct sock *sk)
363363
struct alg_sock *pask = alg_sk(psk);
364364
struct crypto_skcipher *tfm = pask->private;
365365

366-
af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
366+
af_alg_pull_tsgl(sk, ctx->used, NULL);
367367
sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
368368
if (ctx->state)
369369
sock_kzfree_s(sk, ctx->state, crypto_skcipher_statesize(tfm));

crypto/authencesn.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,30 +207,35 @@ static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
207207
u8 *ohash = areq_ctx->tail;
208208
unsigned int cryptlen = req->cryptlen - authsize;
209209
unsigned int assoclen = req->assoclen;
210+
struct scatterlist *src = req->src;
210211
struct scatterlist *dst = req->dst;
211212
u8 *ihash = ohash + crypto_ahash_digestsize(auth);
212213
u32 tmp[2];
213214

214215
if (!authsize)
215216
goto decrypt;
216217

217-
/* Move high-order bits of sequence number back. */
218-
scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
219-
scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
220-
scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
218+
if (src == dst) {
219+
/* Move high-order bits of sequence number back. */
220+
scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
221+
scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
222+
scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
223+
} else
224+
memcpy_sglist(dst, src, assoclen);
221225

222226
if (crypto_memneq(ihash, ohash, authsize))
223227
return -EBADMSG;
224228

225229
decrypt:
226230

227-
sg_init_table(areq_ctx->dst, 2);
231+
if (src != dst)
232+
src = scatterwalk_ffwd(areq_ctx->src, src, assoclen);
228233
dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
229234

230235
skcipher_request_set_tfm(skreq, ctx->enc);
231236
skcipher_request_set_callback(skreq, flags,
232237
req->base.complete, req->base.data);
233-
skcipher_request_set_crypt(skreq, dst, dst, cryptlen, req->iv);
238+
skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
234239

235240
return crypto_skcipher_decrypt(skreq);
236241
}
@@ -255,31 +260,36 @@ static int crypto_authenc_esn_decrypt(struct aead_request *req)
255260
unsigned int assoclen = req->assoclen;
256261
unsigned int cryptlen = req->cryptlen;
257262
u8 *ihash = ohash + crypto_ahash_digestsize(auth);
263+
struct scatterlist *src = req->src;
258264
struct scatterlist *dst = req->dst;
259265
u32 tmp[2];
260266
int err;
261267

262268
if (assoclen < 8)
263269
return -EINVAL;
264270

265-
cryptlen -= authsize;
266-
267-
if (req->src != dst)
268-
memcpy_sglist(dst, req->src, assoclen + cryptlen);
271+
if (!authsize)
272+
goto tail;
269273

274+
cryptlen -= authsize;
270275
scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
271276
authsize, 0);
272277

273-
if (!authsize)
274-
goto tail;
275-
276278
/* Move high-order bits of sequence number to the end. */
277-
scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
278-
scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
279-
scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
280-
281-
sg_init_table(areq_ctx->dst, 2);
282-
dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
279+
scatterwalk_map_and_copy(tmp, src, 0, 8, 0);
280+
if (src == dst) {
281+
scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
282+
scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
283+
dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
284+
} else {
285+
scatterwalk_map_and_copy(tmp, dst, 0, 4, 1);
286+
scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen - 4, 4, 1);
287+
288+
src = scatterwalk_ffwd(areq_ctx->src, src, 8);
289+
dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
290+
memcpy_sglist(dst, src, assoclen + cryptlen - 8);
291+
dst = req->dst;
292+
}
283293

284294
ahash_request_set_tfm(ahreq, auth);
285295
ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);

crypto/deflate.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,21 @@ static int deflate_decompress_one(struct acomp_req *req,
164164

165165
do {
166166
unsigned int dcur;
167+
unsigned long avail_in;
167168

168169
dcur = acomp_walk_next_dst(&walk);
169-
if (!dcur) {
170-
out_of_space = true;
171-
break;
172-
}
173170

174171
stream->avail_out = dcur;
175172
stream->next_out = walk.dst.virt.addr;
173+
avail_in = stream->avail_in;
176174

177175
ret = zlib_inflate(stream, Z_NO_FLUSH);
178176

177+
if (!dcur && avail_in == stream->avail_in) {
178+
out_of_space = true;
179+
break;
180+
}
181+
179182
dcur -= stream->avail_out;
180183
acomp_walk_done_dst(&walk, dcur);
181184
} while (ret == Z_OK && stream->avail_in);

0 commit comments

Comments
 (0)