Skip to content

Commit f3c802a

Browse files
committed
crypto: algif_aead - Only wake up when ctx->more is zero
AEAD does not support partial requests so we must not wake up while ctx->more is set. In order to distinguish between the case of no data sent yet and a zero-length request, a new init flag has been added to ctx. SKCIPHER has also been modified to ensure that at least a block of data is available if there is more data to come. Fixes: 2d97591 ("crypto: af_alg - consolidation of...") Signed-off-by: Herbert Xu <[email protected]>
1 parent 1532e31 commit f3c802a

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

crypto/af_alg.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
635635

636636
if (!ctx->used)
637637
ctx->merge = 0;
638+
ctx->init = ctx->more;
638639
}
639640
EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
640641

@@ -734,9 +735,10 @@ EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
734735
*
735736
* @sk socket of connection to user space
736737
* @flags If MSG_DONTWAIT is set, then only report if function would sleep
738+
* @min Set to minimum request size if partial requests are allowed.
737739
* @return 0 when writable memory is available, < 0 upon error
738740
*/
739-
int af_alg_wait_for_data(struct sock *sk, unsigned flags)
741+
int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
740742
{
741743
DEFINE_WAIT_FUNC(wait, woken_wake_function);
742744
struct alg_sock *ask = alg_sk(sk);
@@ -754,7 +756,9 @@ int af_alg_wait_for_data(struct sock *sk, unsigned flags)
754756
if (signal_pending(current))
755757
break;
756758
timeout = MAX_SCHEDULE_TIMEOUT;
757-
if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more),
759+
if (sk_wait_event(sk, &timeout,
760+
ctx->init && (!ctx->more ||
761+
(min && ctx->used >= min)),
758762
&wait)) {
759763
err = 0;
760764
break;
@@ -843,7 +847,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
843847
}
844848

845849
lock_sock(sk);
846-
if (!ctx->more && ctx->used) {
850+
if (ctx->init && (init || !ctx->more)) {
847851
err = -EINVAL;
848852
goto unlock;
849853
}
@@ -854,6 +858,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
854858
memcpy(ctx->iv, con.iv->iv, ivsize);
855859

856860
ctx->aead_assoclen = con.aead_assoclen;
861+
ctx->init = true;
857862
}
858863

859864
while (size) {

crypto/algif_aead.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
106106
size_t usedpages = 0; /* [in] RX bufs to be used from user */
107107
size_t processed = 0; /* [in] TX bufs to be consumed */
108108

109-
if (!ctx->used) {
110-
err = af_alg_wait_for_data(sk, flags);
109+
if (!ctx->init || ctx->more) {
110+
err = af_alg_wait_for_data(sk, flags, 0);
111111
if (err)
112112
return err;
113113
}

crypto/algif_skcipher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
6161
int err = 0;
6262
size_t len = 0;
6363

64-
if (!ctx->used) {
65-
err = af_alg_wait_for_data(sk, flags);
64+
if (!ctx->init || (ctx->more && ctx->used < bs)) {
65+
err = af_alg_wait_for_data(sk, flags, bs);
6666
if (err)
6767
return err;
6868
}

include/crypto/if_alg.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ struct af_alg_async_req {
135135
* SG?
136136
* @enc: Cryptographic operation to be performed when
137137
* recvmsg is invoked.
138+
* @init: True if metadata has been sent.
138139
* @len: Length of memory allocated for this data structure.
139140
*/
140141
struct af_alg_ctx {
@@ -151,6 +152,7 @@ struct af_alg_ctx {
151152
bool more;
152153
bool merge;
153154
bool enc;
155+
bool init;
154156

155157
unsigned int len;
156158
};
@@ -226,7 +228,7 @@ unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
226228
void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
227229
size_t dst_offset);
228230
void af_alg_wmem_wakeup(struct sock *sk);
229-
int af_alg_wait_for_data(struct sock *sk, unsigned flags);
231+
int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
230232
int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
231233
unsigned int ivsize);
232234
ssize_t af_alg_sendpage(struct socket *sock, struct page *page,

0 commit comments

Comments
 (0)