Skip to content

Commit a664bf3

Browse files
committed
crypto: algif_aead - Revert to operating out-of-place
This mostly reverts commit 72548b0 except for the copying of the associated data. There is no benefit in operating in-place in algif_aead since the source and destination come from different mappings. Get rid of all the complexity added for in-place operation and just copy the AD directly. Fixes: 72548b0 ("crypto: algif_aead - copy AAD from src to dst") Reported-by: Taeyang Lee <0wn@theori.io> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 62397b4 commit a664bf3

4 files changed

Lines changed: 34 additions & 126 deletions

File tree

crypto/af_alg.c

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,13 @@ static int af_alg_alloc_tsgl(struct sock *sk)
637637
/**
638638
* af_alg_count_tsgl - Count number of TX SG entries
639639
*
640-
* The counting starts from the beginning of the SGL to @bytes. If
641-
* 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.
642641
*
643642
* @sk: socket of connection to user space
644643
* @bytes: Count the number of SG entries holding given number of bytes.
645-
* @offset: Start the counting of SG entries from the given offset.
646644
* Return: Number of TX SG entries found given the constraints
647645
*/
648-
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)
649647
{
650648
const struct alg_sock *ask = alg_sk(sk);
651649
const struct af_alg_ctx *ctx = ask->private;
@@ -660,25 +658,11 @@ unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
660658
const struct scatterlist *sg = sgl->sg;
661659

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

681-
bytes -= bytes_count;
665+
bytes -= sg[i].length;
682666
}
683667
}
684668

@@ -690,19 +674,14 @@ EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
690674
* af_alg_pull_tsgl - Release the specified buffers from TX SGL
691675
*
692676
* If @dst is non-null, reassign the pages to @dst. The caller must release
693-
* the pages. If @dst_offset is given only reassign the pages to @dst starting
694-
* at the @dst_offset (byte). The caller must ensure that @dst is large
695-
* enough (e.g. by using af_alg_count_tsgl with the same offset).
677+
* the pages.
696678
*
697679
* @sk: socket of connection to user space
698680
* @used: Number of bytes to pull from TX SGL
699681
* @dst: If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
700682
* caller must release the buffers in dst.
701-
* @dst_offset: Reassign the TX SGL from given offset. All buffers before
702-
* reaching the offset is released.
703683
*/
704-
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
705-
size_t dst_offset)
684+
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst)
706685
{
707686
struct alg_sock *ask = alg_sk(sk);
708687
struct af_alg_ctx *ctx = ask->private;
@@ -727,18 +706,10 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
727706
* SG entries in dst.
728707
*/
729708
if (dst) {
730-
if (dst_offset >= plen) {
731-
/* discard page before offset */
732-
dst_offset -= plen;
733-
} else {
734-
/* reassign page to dst after offset */
735-
get_page(page);
736-
sg_set_page(dst + j, page,
737-
plen - dst_offset,
738-
sg[i].offset + dst_offset);
739-
dst_offset = 0;
740-
j++;
741-
}
709+
/* reassign page to dst after offset */
710+
get_page(page);
711+
sg_set_page(dst + j, page, plen, sg[i].offset);
712+
j++;
742713
}
743714

744715
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));

include/crypto/if_alg.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,8 @@ static inline bool af_alg_readable(struct sock *sk)
230230
return PAGE_SIZE <= af_alg_rcvbuf(sk);
231231
}
232232

233-
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
234-
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
235-
size_t dst_offset);
233+
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes);
234+
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst);
236235
void af_alg_wmem_wakeup(struct sock *sk);
237236
int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
238237
int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,

0 commit comments

Comments
 (0)