Skip to content

Commit 452eaff

Browse files
shjalaclaude
authored andcommitted
crypto: af_alg/algif_aead: fix CVE-2026-31431 splice write-to-page-cache
Backport of upstream fix fafe0fa2995a0f7073c1c358d7d3145bcc9aedd8. Fixes: 72548b0 ("crypto: algif_aead - copy AAD from src to dst") CVE: CVE-2026-31431 Signed-off-by: Shahriyar Jalayeri <shahriyar@posteo.de> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a9757a5 commit 452eaff

4 files changed

Lines changed: 38 additions & 133 deletions

File tree

kernel/kernel-noble/crypto/af_alg.c

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,13 @@ static int af_alg_alloc_tsgl(struct sock *sk)
634634
/**
635635
* af_alg_count_tsgl - Count number of TX SG entries
636636
*
637-
* The counting starts from the beginning of the SGL to @bytes. If
638-
* an @offset is provided, the counting of the SG entries starts at the @offset.
637+
* The counting starts from the beginning of the SGL to @bytes.
639638
*
640639
* @sk: socket of connection to user space
641640
* @bytes: Count the number of SG entries holding given number of bytes.
642-
* @offset: Start the counting of SG entries from the given offset.
643641
* Return: Number of TX SG entries found given the constraints
644642
*/
645-
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
643+
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes)
646644
{
647645
const struct alg_sock *ask = alg_sk(sk);
648646
const struct af_alg_ctx *ctx = ask->private;
@@ -657,25 +655,11 @@ unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
657655
const struct scatterlist *sg = sgl->sg;
658656

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

678-
bytes -= bytes_count;
662+
bytes -= sg[i].length;
679663
}
680664
}
681665

@@ -687,19 +671,14 @@ EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
687671
* af_alg_pull_tsgl - Release the specified buffers from TX SGL
688672
*
689673
* If @dst is non-null, reassign the pages to @dst. The caller must release
690-
* the pages. If @dst_offset is given only reassign the pages to @dst starting
691-
* at the @dst_offset (byte). The caller must ensure that @dst is large
692-
* enough (e.g. by using af_alg_count_tsgl with the same offset).
674+
* the pages.
693675
*
694676
* @sk: socket of connection to user space
695677
* @used: Number of bytes to pull from TX SGL
696678
* @dst: If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
697679
* caller must release the buffers in dst.
698-
* @dst_offset: Reassign the TX SGL from given offset. All buffers before
699-
* reaching the offset is released.
700680
*/
701-
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
702-
size_t dst_offset)
681+
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst)
703682
{
704683
struct alg_sock *ask = alg_sk(sk);
705684
struct af_alg_ctx *ctx = ask->private;
@@ -724,18 +703,10 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
724703
* SG entries in dst.
725704
*/
726705
if (dst) {
727-
if (dst_offset >= plen) {
728-
/* discard page before offset */
729-
dst_offset -= plen;
730-
} else {
731-
/* reassign page to dst after offset */
732-
get_page(page);
733-
sg_set_page(dst + j, page,
734-
plen - dst_offset,
735-
sg[i].offset + dst_offset);
736-
dst_offset = 0;
737-
j++;
738-
}
706+
/* reassign page to dst after offset */
707+
get_page(page);
708+
sg_set_page(dst + j, page, plen, sg[i].offset);
709+
j++;
739710
}
740711

741712
sg[i].length -= plen;

kernel/kernel-noble/crypto/algif_aead.c

Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
9696
struct aead_tfm *aeadc = pask->private;
9797
struct crypto_aead *tfm = aeadc->aead;
9898
struct crypto_sync_skcipher *null_tfm = aeadc->null_tfm;
99-
unsigned int i, as = crypto_aead_authsize(tfm);
99+
unsigned int as = crypto_aead_authsize(tfm);
100100
struct af_alg_async_req *areq;
101-
struct af_alg_tsgl *tsgl, *tmp;
102101
struct scatterlist *rsgl_src, *tsgl_src = NULL;
103102
int err = 0;
104103
size_t used = 0; /* [in] TX bufs to be en/decrypted */
@@ -178,23 +177,24 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
178177
outlen -= less;
179178
}
180179

180+
/*
181+
* Create a per request TX SGL for this request which tracks the
182+
* SG entries from the global TX SGL.
183+
*/
181184
processed = used + ctx->aead_assoclen;
182-
list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
183-
for (i = 0; i < tsgl->cur; i++) {
184-
struct scatterlist *process_sg = tsgl->sg + i;
185-
186-
if (!(process_sg->length) || !sg_page(process_sg))
187-
continue;
188-
tsgl_src = process_sg;
189-
break;
190-
}
191-
if (tsgl_src)
192-
break;
193-
}
194-
if (processed && !tsgl_src) {
195-
err = -EFAULT;
185+
areq->tsgl_entries = af_alg_count_tsgl(sk, processed);
186+
if (!areq->tsgl_entries)
187+
areq->tsgl_entries = 1;
188+
areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
189+
areq->tsgl_entries),
190+
GFP_KERNEL);
191+
if (!areq->tsgl) {
192+
err = -ENOMEM;
196193
goto free;
197194
}
195+
sg_init_table(areq->tsgl, areq->tsgl_entries);
196+
af_alg_pull_tsgl(sk, processed, areq->tsgl);
197+
tsgl_src = areq->tsgl;
198198

199199
/*
200200
* Copy of AAD from source to destination
@@ -203,83 +203,18 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
203203
* when user space uses an in-place cipher operation, the kernel
204204
* will copy the data as it does not see whether such in-place operation
205205
* is initiated.
206-
*
207-
* To ensure efficiency, the following implementation ensure that the
208-
* ciphers are invoked to perform a crypto operation in-place. This
209-
* is achieved by memory management specified as follows.
210206
*/
211207

212-
/* Use the RX SGL as source (and destination) for crypto op. */
208+
/* Use the RX SGL as destination; TX SGL as crypto source. */
213209
rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
214210

215-
if (ctx->enc) {
216-
/*
217-
* Encryption operation - The in-place cipher operation is
218-
* achieved by the following operation:
219-
*
220-
* TX SGL: AAD || PT
221-
* | |
222-
* | copy |
223-
* v v
224-
* RX SGL: AAD || PT || Tag
225-
*/
226-
err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
227-
areq->first_rsgl.sgl.sgt.sgl,
228-
processed);
229-
if (err)
230-
goto free;
231-
af_alg_pull_tsgl(sk, processed, NULL, 0);
232-
} else {
233-
/*
234-
* Decryption operation - To achieve an in-place cipher
235-
* operation, the following SGL structure is used:
236-
*
237-
* TX SGL: AAD || CT || Tag
238-
* | | ^
239-
* | copy | | Create SGL link.
240-
* v v |
241-
* RX SGL: AAD || CT ----+
242-
*/
243-
244-
/* Copy AAD || CT to RX SGL buffer for in-place operation. */
245-
err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
246-
areq->first_rsgl.sgl.sgt.sgl,
247-
outlen);
248-
if (err)
249-
goto free;
250-
251-
/* Create TX SGL for tag and chain it to RX SGL. */
252-
areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
253-
processed - as);
254-
if (!areq->tsgl_entries)
255-
areq->tsgl_entries = 1;
256-
areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
257-
areq->tsgl_entries),
258-
GFP_KERNEL);
259-
if (!areq->tsgl) {
260-
err = -ENOMEM;
261-
goto free;
262-
}
263-
sg_init_table(areq->tsgl, areq->tsgl_entries);
264-
265-
/* Release TX SGL, except for tag data and reassign tag data. */
266-
af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
267-
268-
/* chain the areq TX SGL holding the tag with RX SGL */
269-
if (usedpages) {
270-
/* RX SGL present */
271-
struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
272-
struct scatterlist *sg = sgl_prev->sgt.sgl;
273-
274-
sg_unmark_end(sg + sgl_prev->sgt.nents - 1);
275-
sg_chain(sg, sgl_prev->sgt.nents + 1, areq->tsgl);
276-
} else
277-
/* no RX SGL present (e.g. authentication only) */
278-
rsgl_src = areq->tsgl;
279-
}
211+
err = crypto_aead_copy_sgl(null_tfm, tsgl_src, rsgl_src,
212+
ctx->aead_assoclen);
213+
if (err)
214+
goto free;
280215

281216
/* Initialize the crypto operation */
282-
aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
217+
aead_request_set_crypt(&areq->cra_u.aead_req, tsgl_src,
283218
areq->first_rsgl.sgl.sgt.sgl, used, ctx->iv);
284219
aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
285220
aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
@@ -514,7 +449,7 @@ static void aead_sock_destruct(struct sock *sk)
514449
struct crypto_aead *tfm = aeadc->aead;
515450
unsigned int ivlen = crypto_aead_ivsize(tfm);
516451

517-
af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
452+
af_alg_pull_tsgl(sk, ctx->used, NULL);
518453
sock_kzfree_s(sk, ctx->iv, ivlen);
519454
sock_kfree_s(sk, ctx, ctx->len);
520455
af_alg_release_parent(sk);

kernel/kernel-noble/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));

kernel/kernel-noble/include/crypto/if_alg.h

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

230-
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
231-
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
232-
size_t dst_offset);
230+
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes);
231+
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst);
233232
void af_alg_wmem_wakeup(struct sock *sk);
234233
int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
235234
int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,

0 commit comments

Comments
 (0)