Skip to content

Commit 1f323a4

Browse files
herbertxgregkh
authored andcommitted
crypto: af_alg - Disallow concurrent writes in af_alg_sendmsg
[ Upstream commit 1b34cbb ] Issuing two writes to the same af_alg socket is bogus as the data will be interleaved in an unpredictable fashion. Furthermore, concurrent writes may create inconsistencies in the internal socket state. Disallow this by adding a new ctx->write field that indiciates exclusive ownership for writing. Fixes: 8ff5909 ("crypto: algif_skcipher - User-space interface for skcipher operations") Reported-by: Muhammad Alifa Ramdhan <ramdhan@starlabs.sg> Reported-by: Bing-Jhong Billy Jheng <billy@starlabs.sg> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 2b8bbc6 commit 1f323a4

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

crypto/af_alg.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,12 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
859859
}
860860

861861
lock_sock(sk);
862+
if (ctx->write) {
863+
release_sock(sk);
864+
return -EBUSY;
865+
}
866+
ctx->write = true;
867+
862868
if (ctx->init && !ctx->more) {
863869
if (ctx->used) {
864870
err = -EINVAL;
@@ -974,6 +980,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
974980

975981
unlock:
976982
af_alg_data_wakeup(sk);
983+
ctx->write = false;
977984
release_sock(sk);
978985

979986
return copied ?: err;

include/crypto/if_alg.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct af_alg_async_req {
136136
* SG?
137137
* @enc: Cryptographic operation to be performed when
138138
* recvmsg is invoked.
139+
* @write: True if we are in the middle of a write.
139140
* @init: True if metadata has been sent.
140141
* @len: Length of memory allocated for this data structure.
141142
* @inflight: Non-zero when AIO requests are in flight.
@@ -151,10 +152,11 @@ struct af_alg_ctx {
151152
size_t used;
152153
atomic_t rcvused;
153154

154-
bool more;
155-
bool merge;
156-
bool enc;
157-
bool init;
155+
u32 more:1,
156+
merge:1,
157+
enc:1,
158+
write:1,
159+
init:1;
158160

159161
unsigned int len;
160162

0 commit comments

Comments
 (0)