Skip to contentSkip to conversation

crypto: af_alg - convert inflight to atomic_t to fix data race

PATCH In review 0nsec 3 participants 4 messages May 3 – May 4 4 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): rM7uJ0oBopViOraMoC0Ya0_hMtNwV4CLor-vdwN4vIH7BOKqCuuC0OWUOQoOS-0XOcJmSIT5vhR4UmZlIJxD7mllIkq1UVqEop3T0e4bjis=@proton.me

A message that is not in the archive (sent off-list or not yet received): 2026050304-greeting-prankster-910b@gregkh

A message that is not in the archive (sent off-list or not yet received): QACE4BCfRIeL8Dm_ETPxjem791yvR3Lj6Iw3ArtLWxEU5FwAmjTCS6DZA_hdQfyhi2MYJTIu-p36nDpUwQbhWwxc1X2LgZSCMikbNFdOGCE=@proton.me

A message that is not in the archive (sent off-list or not yet received): 2026050328-civic-monoxide-0a54@gregkh

0nsec

author
#4 lore raw

PATCH 1/1 crypto: af_alg - convert inflight to atomic_t to fix data race

The inflight field in struct af_alg_ctx guards the invariant that only one AIO crypto request may be in flight at a time. It is declared as a plain unsigned int but accessed from two unsynchronized contexts:
process context under lock_sock() and the async crypto completion callback which runs without any socket lock.

Data race under the C11 memory model. On weakly-ordered architectures the store in the completion path could be observed out of order relative to the preceding areq free, widening the window for state confusion between a completing first request and a newly allocated second one.

Convert inflight to atomic_t. Use atomic_xchg() for the check-and-set in af_alg_alloc_areq() so check and set are one atomic operation, eliminating the TOCTOU that separate atomic_read + atomic_set would leave. The ENOMEM rollback path must also clear inflight since atomic_xchg() sets it before the allocation attempt; without this a failed allocation permanently blocks further AIO on that socket.

Follows the precedent of af955bf15d2c ("crypto: af_alg - Fix race around ctx->rcvused by making it atomic_t"). The inflight field introduced in 67b164a871af repeated the same locking gap.

CVE-2025-71113 fixed uninitialized garbage in inflight via memset. That is a distinct bug. This race exists independently.

Fixes 67b164a871af ("crypto: af_alg - Disallow multiple in-flight AIO requests")
---
 crypto/af_alg.c         | 10 +++++-----
 include/crypto/if_alg.h |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
crypto/af_alg.c +5 -5
index 5a00c18eb..3e62b7e6d 100644
@@ -1102,7 +1102,7 @@ void af_alg_free_resources(struct af_alg_async_req *areq)
 	sock_kfree_s(sk, areq, areq->areqlen);
 
 	ctx = alg_sk(sk)->private;
-	ctx->inflight = false;
+	atomic_set(&ctx->inflight, 0);
 }
 EXPORT_SYMBOL_GPL(af_alg_free_resources);
 
@@ -1176,17 +1176,17 @@ struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
 	struct af_alg_async_req *areq;
 
 	/* Only one AIO request can be in flight. */
-	if (ctx->inflight)
+	if (atomic_xchg(&ctx->inflight, 1))
 		return ERR_PTR(-EBUSY);
 
 	areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
-	if (unlikely(!areq))
+	if (unlikely(!areq)) {
+		atomic_set(&ctx->inflight, 0);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	memset(areq, 0, areqlen);
 
-	ctx->inflight = true;
-
 	areq->areqlen = areqlen;
 	areq->sk = sk;
 	areq->first_rsgl.sgl.sgt.sgl = areq->first_rsgl.sgl.sgl;
include/crypto/if_alg.h +1 -1
index 0cc8fa749..b3b1908dd 100644
@@ -160,7 +160,7 @@ struct af_alg_ctx {
 
 	unsigned int len;
 
-	unsigned int inflight;
+	atomic_t inflight;
 };
 
 int af_alg_register_type(const struct af_alg_type *type);
Signature · 1 line
2.54.0
maintainer
#5 lore raw
Quoted 22 lines · On Sun, May 03, 2026 at 09:54:27AM +0000, 0nsec wrote:

The inflight field in struct af_alg_ctx guards the invariant that only one AIO crypto request may be in flight at a time. It is declared as a plain unsigned int but accessed from two unsynchronized contexts:
process context under lock_sock() and the async crypto completion callback which runs without any socket lock.

Data race under the C11 memory model. On weakly-ordered architectures the store in the completion path could be observed out of order relative to the preceding areq free, widening the window for state confusion between a completing first request and a newly allocated second one.

Convert inflight to atomic_t. Use atomic_xchg() for the check-and-set in af_alg_alloc_areq() so check and set are one atomic operation, eliminating the TOCTOU that separate atomic_read + atomic_set would leave. The ENOMEM rollback path must also clear inflight since atomic_xchg() sets it before the allocation attempt; without this a failed allocation permanently blocks further AIO on that socket.

Follows the precedent of af955bf15d2c ("crypto: af_alg - Fix race around ctx->rcvused by making it atomic_t"). The inflight field introduced in 67b164a871af repeated the same locking gap.

Quoted 6 lines

CVE-2025-71113 fixed uninitialized garbage in inflight via memset. That is a distinct bug. This race exists independently.

Fixes 67b164a871af ("crypto: af_alg - Disallow multiple in-flight AIO requests")

We need a real name for the author and signed off by line.

thanks,

greg k-h

author
#6 lore raw

PATCH 1/1 crypto: af_alg - convert inflight to atomic_t to fix data race

From: Muhammad Bilal <[email protected]>

The inflight field in struct af_alg_ctx guards the invariant that only one AIO crypto request may be in flight at a time. It is declared as a plain unsigned int but accessed from two unsynchronized contexts:
process context under lock_sock() and the async crypto completion callback which runs without any socket lock.

Data race under the C11 memory model. On weakly-ordered architectures the store in the completion path could be observed out of order relative to the preceding areq free, widening the window for state confusion between a completing first request and a newly allocated second one.

Convert inflight to atomic_t. Use atomic_xchg() for the check-and-set in af_alg_alloc_areq() so check and set are one atomic operation, eliminating the TOCTOU that separate atomic_read + atomic_set would leave. The ENOMEM rollback path must also clear inflight since atomic_xchg() sets it before the allocation attempt; without this a failed allocation permanently blocks further AIO on that socket.

Follows the precedent of af955bf15d2c ("crypto: af_alg - Fix race around ctx->rcvused by making it atomic_t"). The inflight field introduced in 67b164a871af repeated the same locking gap.

CVE-2025-71113 fixed uninitialized garbage in inflight via memset. That is a distinct bug. This race exists independently.

Fixes 67b164a871af ("crypto: af_alg - Disallow multiple in-flight AIO requests")
---
 crypto/af_alg.c         | 10 +++++-----
 include/crypto/if_alg.h |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
crypto/af_alg.c +5 -5
index 5a00c18eb..3e62b7e6d 100644
@@ -1102,7 +1102,7 @@ void af_alg_free_resources(struct af_alg_async_req *areq)
 	sock_kfree_s(sk, areq, areq->areqlen);
 
 	ctx = alg_sk(sk)->private;
-	ctx->inflight = false;
+	atomic_set(&ctx->inflight, 0);
 }
 EXPORT_SYMBOL_GPL(af_alg_free_resources);
 
@@ -1176,17 +1176,17 @@ struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
 	struct af_alg_async_req *areq;
 
 	/* Only one AIO request can be in flight. */
-	if (ctx->inflight)
+	if (atomic_xchg(&ctx->inflight, 1))
 		return ERR_PTR(-EBUSY);
 
 	areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
-	if (unlikely(!areq))
+	if (unlikely(!areq)) {
+		atomic_set(&ctx->inflight, 0);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	memset(areq, 0, areqlen);
 
-	ctx->inflight = true;
-
 	areq->areqlen = areqlen;
 	areq->sk = sk;
 	areq->first_rsgl.sgl.sgt.sgl = areq->first_rsgl.sgl.sgl;
include/crypto/if_alg.h +1 -1
index 0cc8fa749..b3b1908dd 100644
@@ -160,7 +160,7 @@ struct af_alg_ctx {
 
 	unsigned int len;
 
-	unsigned int inflight;
+	atomic_t inflight;
 };
 
 int af_alg_register_type(const struct af_alg_type *type);
Signature · 1 line
2.54.0
maintainer
#7 lore raw
In reply to Greg KH
Quoted 23 lines · Greg KH
Quoted 22 lines · On Sun, May 03, 2026 at 09:54:27AM +0000, 0nsec wrote:

The inflight field in struct af_alg_ctx guards the invariant that only one AIO crypto request may be in flight at a time. It is declared as a plain unsigned int but accessed from two unsynchronized contexts:
process context under lock_sock() and the async crypto completion callback which runs without any socket lock.

Data race under the C11 memory model. On weakly-ordered architectures the store in the completion path could be observed out of order relative to the preceding areq free, widening the window for state confusion between a completing first request and a newly allocated second one.

Convert inflight to atomic_t. Use atomic_xchg() for the check-and-set in af_alg_alloc_areq() so check and set are one atomic operation, eliminating the TOCTOU that separate atomic_read + atomic_set would leave. The ENOMEM rollback path must also clear inflight since atomic_xchg() sets it before the allocation attempt; without this a failed allocation permanently blocks further AIO on that socket.

Follows the precedent of af955bf15d2c ("crypto: af_alg - Fix race around ctx->rcvused by making it atomic_t"). The inflight field introduced in 67b164a871af repeated the same locking gap.

Quoted 8 lines
Quoted 6 lines

CVE-2025-71113 fixed uninitialized garbage in inflight via memset. That is a distinct bug. This race exists independently.

Fixes 67b164a871af ("crypto: af_alg - Disallow multiple in-flight AIO requests")

We need a real name for the author and signed off by line.

This is not a bug at all.

It's a plain int that's being written to with either zero or one as the value.

Sure we may not notice the async write of zero soon enough, but that would only be because user-space is doing something naughty and trying to perform two async operations at the same time.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt