Skip to contentSkip to conversation

crypto: af_alg - limit RX SG extraction by receive buffer budget

PATCH In review Artem Dinaburg 2 participants 2 messages Oct 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: af_alg - limit RX SG extraction by receive buffer budget

From: Douya Le <[email protected]>

[ Upstream commit 8eceab19eba9dcbfd2a0daec72e1bf48aa100170 ]

Make af_alg_get_rsgl() limit each RX scatterlist extraction to the remaining receive buffer budget.

af_alg_get_rsgl() currently uses af_alg_readable() only as a gate before extracting data into the RX scatterlist. Limit each extraction to the remaining af_alg_rcvbuf(sk) budget so that receive-side accounting matches the amount of data attached to the request.

If skcipher cannot obtain enough RX space for at least one chunk while more data remains to be processed, reject the recvmsg call instead of rounding the request length down to zero.

Fixes e870456d8e7c8d57c059ea479b5aadbb55ff4c3a ("crypto: algif_skcipher - overhaul memory management")
Assisted-by LLM

[ Backport to 6.6.y: Add braces to the older block-size conditional so the
new short-block error check and existing rounding remain under the same condition. ]

---
Hi,

Please consider this backport of the fix for CVE-2026-31677 to 6.6.y.

The code changes match upstream, with braces added to the older block-size conditional. See the bracketed backport note above.

The complete fix is already present in 6.12.y, 6.18.y and 7.2.y.

Build-tested only as part of a cumulative 6.6.y series on x86_64 (allmodconfig, CONFIG_WERROR=y); no runtime testing.

Thanks,
Artem Dinaburg

 crypto/af_alg.c         |    2 ++
 crypto/algif_skcipher.c |    8 +++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
crypto/af_alg.c +2 -0
index e7fea668473d9ede47c7d0e2005c333d34147b29..e6e94b34e0afe78f4d2a8a60c6a194bd0a958beb 100644
@@ -1207,6 +1207,8 @@ int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
 
 		seglen = min_t(size_t, (maxsize - len),
 			       msg_data_left(msg));
+		/* Never pin more pages than the remaining RX accounting budget. */
+		seglen = min_t(size_t, seglen, af_alg_rcvbuf(sk));
 
 		if (list_empty(&areq->rsgl_list)) {
 			rsgl = &areq->first_rsgl;
crypto/algif_skcipher.c +7 -1
index b12df4544d0bbba3f3d156f0e279ac896cd81ec0..8729ef268d9d83cea537abaf23c6cdbb45f59ccd 100644
@@ -82,8 +82,14 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 	 * If more buffers are to be expected to be processed, process only
 	 * full block size buffers.
 	 */
-	if (ctx->more || len < ctx->used)
+	if (ctx->more || len < ctx->used) {
+		if (len < bs) {
+			err = -EINVAL;
+			goto free;
+		}
+
 		len -= len % bs;
+	}
 
 	/*
 	 * Create a per request TX SGL for this request which tracks the
#1 lore raw
Quoted 4 lines · Artem Dinaburg

[ Backport to 6.6.y: Add braces to the older block-size conditional so the
new short-block error check and existing rounding remain under the same condition. ]

Seems this was already done in 7e345de2e69475430af85e09210795d8eb8e719c, which is the same custom backport diff. It was recently released in 6.6.y without any public discussion. Fortunately the backport looks okay, I think. Odd that the stable maintainers are applying custom backports to the crypto/ directory without Cc'ing linux-crypto, though.

- Eric