Skip to contentSkip to conversation

crypto: algif/hash: Fix race between MORE and non-MORE sends

PATCH In review David Howells 2 participants 2 messages Jul 7, 2023 – Jul 10, 2023

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

PATCH 1/1 crypto: algif/hash: Fix race between MORE and non-MORE sends

The 'MSG_MORE' state of the previous sendmsg() is fetched without the socket lock held, so two sendmsg calls can race. This can be seen with a large sendfile() as that now does a series of sendmsg() calls, and if a write() comes in on the same socket at an inopportune time, it can flip the state.

Fix this by moving the fetch of ctx->more inside the socket lock.

Fixes c662b043cdca ("crypto: af_alg/hash: Support MSG_SPLICE_PAGES")
Reported-by [email protected]
cc Herbert Xu <[email protected]>
cc Paolo Abeni <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
---
 crypto/algif_hash.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/algif_hash.c +3 -1
index 0ab43e149f0e..82c44d4899b9 100644
@@ -68,13 +68,15 @@ static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
 	struct hash_ctx *ctx = ask->private;
 	ssize_t copied = 0;
 	size_t len, max_pages, npages;
-	bool continuing = ctx->more, need_init = false;
+	bool continuing, need_init = false;
 	int err;
 
 	max_pages = min_t(size_t, ALG_MAX_PAGES,
 			  DIV_ROUND_UP(sk->sk_sndbuf, PAGE_SIZE));
 
 	lock_sock(sk);
+	continuing = ctx->more;
+
 	if (!continuing) {
 		/* Discard a previous request that wasn't marked MSG_MORE. */
 		hash_free_result(sk, ctx);
maintainer
#1 lore raw
Quoted 20 lines · David Howells

The 'MSG_MORE' state of the previous sendmsg() is fetched without the socket lock held, so two sendmsg calls can race. This can be seen with a large sendfile() as that now does a series of sendmsg() calls, and if a write() comes in on the same socket at an inopportune time, it can flip the state.

Fix this by moving the fetch of ctx->more inside the socket lock.

Fixes c662b043cdca ("crypto: af_alg/hash: Support MSG_SPLICE_PAGES")
Reported-by [email protected]
cc Herbert Xu <[email protected]>
cc Paolo Abeni <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
Quoted 3 lines
---
 crypto/algif_hash.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Patch applied. Thanks.

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