Re: [PATCH net-next] crypto: af_alg/hash: Fix recvmsg() after sendmsg(MSG_MORE)

From: Herbert Xu
Date: Tue Jun 20 2023 - 00:48:33 EST


On Mon, Jun 19, 2023 at 05:47:26PM +0100, David Howells wrote:
>
> The free here:
>
> if (!continuing) {
> if ((msg->msg_flags & MSG_MORE))
> hash_free_result(sk, ctx);
>
> only happens in the following case:
>
> send(hashfd, "", 0, 0);
> send(hashfd, "", 0, MSG_MORE); <--- by this

Yes and that's what I'm complaining about.

> and the patch changes how this case works if no data is given. In Linus's
> tree, it will create a result, init the crypto and finalise it in
> hash_sendmsg(); with this patch that case is then handled by hash_recvmsg().
> If you consider the following sequence:
>
> send(hashfd, "", 0, 0);
> send(hashfd, "", 0, 0);
> send(hashfd, "", 0, 0);
> send(hashfd, "", 0, 0);
>
> Upstream, the first one will create a result and then each of them will init
> and finalise a hash, whereas with my patch, the first one will release any
> outstanding result and then none of them will do any crypto ops.

This is correct. If MSG_MORE is not set, then the hash will be
finalised. In which case if there is already a result allocated
then we should reuse it and not free it.

If MSG_MORE is set, then we can delay the allocation of the result,
in which case it makes sense to free any previous results since
the next request may not come for a very long time (or perhaps even
never).

Cheers,
--
Email: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt