[PATCH v2] crypto: af_alg - wake up waiters on zero-length final message
From: Guicai Ma
Date: Fri Nov 27 2026 - 02:12:31 EST
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@xxxxxxxxxxxxxxx
Suggested-by: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Guicai Ma <maguicai@xxxxxxxxxx>
---
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