Skip to content

Commit e4c1ec1

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>
1 parent e15de80 commit e4c1ec1

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

crypto/af_alg.c

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

864864
lock_sock(sk);
865+
if (ctx->write) {
866+
release_sock(sk);
867+
return -EBUSY;
868+
}
869+
ctx->write = true;
870+
865871
if (ctx->init && !ctx->more) {
866872
if (ctx->used) {
867873
err = -EINVAL;
@@ -969,6 +975,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
969975

970976
unlock:
971977
af_alg_data_wakeup(sk);
978+
ctx->write = false;
972979
release_sock(sk);
973980

974981
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)