* [PATCH] crypto: af_alg - wake up waiters on zero-length final message
@ 2026-11-20 8:32 Guicai Ma
2026-11-27 4:45 ` Herbert Xu
2026-11-27 7:12 ` [PATCH v2] " Guicai Ma
0 siblings, 2 replies; 3+ messages in thread
From: Guicai Ma @ 2026-11-20 8:32 UTC (permalink / raw)
To: Herbert Xu, David S . Miller; +Cc: linux-crypto, linux-kernel, Guicai Ma
af_alg_data_wakeup() returns early whenever ctx->used is zero, whereas
af_alg_wait_for_data() waits for:
ctx->init && (!ctx->more || (min && ctx->used >= min))
When a reader is blocked in recvmsg() and a sender submits a zero-length
final message (no MSG_MORE), such as an empty gcm(aes) AEAD encryption
or an empty send to finish a MSG_MORE stream, af_alg_sendmsg() sets
ctx->init = true and ctx->more = false while ctx->used remains 0.
af_alg_data_wakeup() then bails out on !ctx->used, leaving the reader
blocked indefinitely.
Only skip the wakeup if the context is not initialized yet or more data
is still expected with nothing queued.
Fixes: 2d97591ef43d ("crypto: af_alg - consolidation of duplicate code")
Cc: stable@vger.kernel.org
Signed-off-by: Guicai Ma <maguicai@kylinos.cn>
---
Verified with a two-threaded AF_ALG test (zero-length final sendmsg
on gcm(aes) and ecb(aes) while recvmsg() is blocked): recvmsg() hangs
before this patch and completes immediately after.
crypto/af_alg.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index 7524070f26d8..9b4f8c1d2e3a 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -938,7 +938,7 @@ static void af_alg_data_wakeup(struct sock *sk)
struct af_alg_ctx *ctx = ask->private;
struct socket_wq *wq;
- if (!ctx->used)
+ if (!ctx->init || (ctx->more && !ctx->used))
return;
rcu_read_lock();
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] crypto: af_alg - wake up waiters on zero-length final message
2026-11-20 8:32 [PATCH] crypto: af_alg - wake up waiters on zero-length final message Guicai Ma
@ 2026-11-27 4:45 ` Herbert Xu
2026-11-27 7:12 ` [PATCH v2] " Guicai Ma
1 sibling, 0 replies; 3+ messages in thread
From: Herbert Xu @ 2026-11-27 4:45 UTC (permalink / raw)
To: Guicai Ma; +Cc: David S . Miller, linux-crypto, linux-kernel
On Fri, Nov 20, 2026 at 03:32:44AM -0500, Guicai Ma wrote:
>
> diff --git a/crypto/af_alg.c b/crypto/af_alg.c
> index 7524070f26d8..9b4f8c1d2e3a 100644
> --- a/crypto/af_alg.c
> +++ b/crypto/af_alg.c
> @@ -938,7 +938,7 @@ static void af_alg_data_wakeup(struct sock *sk)
> struct af_alg_ctx *ctx = ask->private;
> struct socket_wq *wq;
>
> - if (!ctx->used)
> + if (!ctx->init || (ctx->more && !ctx->used))
Good catch. However, this is still slightly out-of-sync with the
waiting side's condition:
> ctx->init && (!ctx->more || (min && ctx->used >= min))
How about putting that whole thing into a helper and have both sides
use it to ensure that this never happens again?
Thanks,
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2] crypto: af_alg - wake up waiters on zero-length final message
2026-11-20 8:32 [PATCH] crypto: af_alg - wake up waiters on zero-length final message Guicai Ma
2026-11-27 4:45 ` Herbert Xu
@ 2026-11-27 7:12 ` Guicai Ma
1 sibling, 0 replies; 3+ messages in thread
From: Guicai Ma @ 2026-11-27 7:12 UTC (permalink / raw)
To: Herbert Xu, David S . Miller; +Cc: linux-crypto, linux-kernel, Guicai Ma
af_alg_data_wakeup() checks !ctx->used before waking readers, which
does not match the condition af_alg_wait_for_data() waits on:
ctx->init && (!ctx->more || (min && ctx->used >= min))
When a zero-length final sendmsg() (without MSG_MORE) sets ctx->init
and clears ctx->more while ctx->used remains 0, af_alg_data_wakeup()
skips the wakeup and leaves the reader sleeping forever.
Move the condition into a shared af_alg_data_ready() helper, pass min
through af_alg_sendmsg() to af_alg_data_wakeup(), and use the helper on
both the waiting and waking sides.
Fixes: 2d97591ef43d ("crypto: af_alg - consolidation of duplicate code")
Cc: stable@vger.kernel.org
Suggested-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Guicai Ma <maguicai@kylinos.cn>
---
v2:
- Move the wait condition into af_alg_data_ready() and use it on both
sides, as suggested by Herbert.
Tested with two-threaded AF_ALG tests on gcm(aes) and ecb(aes)
(zero-length final sendmsg, MSG_MORE sub-block and multi-chunk sends).
crypto/af_alg.c | 13 +++++++------
crypto/algif_aead.c | 4 ++--
crypto/algif_skcipher.c | 5 +++--
include/crypto/if_alg.h | 15 ++++++++++++++-
4 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index ab84c4488a15..d6104a6ee510 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -912,8 +912,7 @@ int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
break;
timeout = MAX_SCHEDULE_TIMEOUT;
if (sk_wait_event(sk, &timeout,
- ctx->init && (!ctx->more ||
- (min && ctx->used >= min)),
+ af_alg_data_ready(ctx, min),
&wait)) {
err = 0;
break;
@@ -931,14 +930,15 @@ EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
* af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
*
* @sk: socket of connection to user space
+ * @min: minimum request size if partial requests are allowed
*/
-static void af_alg_data_wakeup(struct sock *sk)
+static void af_alg_data_wakeup(struct sock *sk, unsigned int min)
{
struct alg_sock *ask = alg_sk(sk);
struct af_alg_ctx *ctx = ask->private;
struct socket_wq *wq;
- if (!ctx->used)
+ if (!af_alg_data_ready(ctx, min))
return;
rcu_read_lock();
@@ -965,10 +965,11 @@ static void af_alg_data_wakeup(struct sock *sk)
* @size: size of message from user space
* @ivsize: the size of the IV for the cipher operation to verify that the
* user-space-provided IV has the right size
+ * @min: minimum request size if partial requests are allowed
* Return: the number of copied data upon success, < 0 upon error
*/
int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
- unsigned int ivsize)
+ unsigned int ivsize, unsigned int min)
{
struct sock *sk = sock->sk;
struct alg_sock *ask = alg_sk(sk);
@@ -1119,7 +1120,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
ctx->more = msg->msg_flags & MSG_MORE;
unlock:
- af_alg_data_wakeup(sk);
+ af_alg_data_wakeup(sk, min);
ctx->write = false;
release_sock(sk);
diff --git a/crypto/algif_aead.c b/crypto/algif_aead.c
index 5574e2d70539..137a947f0620 100644
--- a/crypto/algif_aead.c
+++ b/crypto/algif_aead.c
@@ -64,7 +64,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
struct crypto_aead *tfm = pask->private;
unsigned int ivsize = crypto_aead_ivsize(tfm);
- return af_alg_sendmsg(sock, msg, size, ivsize);
+ return af_alg_sendmsg(sock, msg, size, ivsize, 0);
}
static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
@@ -87,7 +87,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
size_t usedpages = 0; /* [in] RX bufs to be used from user */
size_t processed = 0; /* [in] TX bufs to be consumed */
- if (!ctx->init || ctx->more) {
+ if (!af_alg_data_ready(ctx, 0)) {
err = af_alg_wait_for_data(sk, flags, 0);
if (err)
return err;
diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c
index 291dbd698932..cece9cf387cc 100644
--- a/crypto/algif_skcipher.c
+++ b/crypto/algif_skcipher.c
@@ -63,7 +63,8 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
struct crypto_skcipher *tfm = pask->private;
unsigned ivsize = crypto_skcipher_ivsize(tfm);
- return af_alg_sendmsg(sock, msg, size, ivsize);
+ return af_alg_sendmsg(sock, msg, size, ivsize,
+ crypto_skcipher_chunksize(tfm));
}
static int algif_skcipher_export(struct sock *sk, struct skcipher_request *req)
@@ -113,7 +114,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
int err = 0;
size_t len = 0;
- if (!ctx->init || (ctx->more && ctx->used < bs)) {
+ if (!af_alg_data_ready(ctx, bs)) {
err = af_alg_wait_for_data(sk, flags, bs);
if (err)
return err;
diff --git a/include/crypto/if_alg.h b/include/crypto/if_alg.h
index 0d51428c1da4..cb0951f33612 100644
--- a/include/crypto/if_alg.h
+++ b/include/crypto/if_alg.h
@@ -244,12 +244,25 @@ static inline bool af_alg_readable(struct sock *sk)
return PAGE_SIZE <= af_alg_rcvbuf(sk);
}
+/**
+ * af_alg_data_ready - Is there enough TX data to proceed?
+ *
+ * @ctx: socket context of connection to user space
+ * @min: minimum request size if partial requests are allowed
+ *
+ * Returns: true => data is ready, false => not ready
+ */
+static inline bool af_alg_data_ready(struct af_alg_ctx *ctx, unsigned int min)
+{
+ return ctx->init && (!ctx->more || (min && ctx->used >= min));
+}
+
unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes);
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst);
void af_alg_wmem_wakeup(struct sock *sk);
int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
- unsigned int ivsize);
+ unsigned int ivsize, unsigned int min);
void af_alg_free_resources(struct af_alg_async_req *areq);
__poll_t af_alg_poll(struct file *file, struct socket *sock,
poll_table *wait);
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-27 7:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 8:32 [PATCH] crypto: af_alg - wake up waiters on zero-length final message Guicai Ma
2026-11-27 4:45 ` Herbert Xu
2026-11-27 7:12 ` [PATCH v2] " Guicai Ma
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®