Skip to content

Commit 0f6bb83

Browse files
committed
crypto: algif_skcipher - Fixed overflow when sndbuf is page aligned
When sk_sndbuf is not a multiple of PAGE_SIZE, the limit tests in sendmsg fail as the limit variable becomes negative and we're using an unsigned comparison. The same thing can happen if sk_sndbuf is lowered after a sendmsg call. This patch fixes this by always taking the signed maximum of limit and 0 before we perform the comparison. It also rounds the value of sk_sndbuf down to a multiple of PAGE_SIZE so that we don't end up allocating a page only to use a small number of bytes in it because we're bound by sk_sndbuf. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 7451708 commit 0f6bb83

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

‎crypto/algif_skcipher.c‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ struct skcipher_ctx {
5252
#define MAX_SGL_ENTS ((PAGE_SIZE - sizeof(struct skcipher_sg_list)) / \
5353
sizeof(struct scatterlist) - 1)
5454

55-
static inline bool skcipher_writable(struct sock *sk)
55+
static inline int skcipher_sndbuf(struct sock *sk)
5656
{
5757
struct alg_sock *ask = alg_sk(sk);
5858
struct skcipher_ctx *ctx = ask->private;
5959

60-
return ctx->used + PAGE_SIZE <= max_t(int, sk->sk_sndbuf, PAGE_SIZE);
60+
return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
61+
ctx->used, 0);
62+
}
63+
64+
static inline bool skcipher_writable(struct sock *sk)
65+
{
66+
return PAGE_SIZE <= skcipher_sndbuf(sk);
6167
}
6268

6369
static int skcipher_alloc_sgl(struct sock *sk)
@@ -245,7 +251,6 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
245251
struct af_alg_control con = {};
246252
long copied = 0;
247253
bool enc = 0;
248-
int limit;
249254
int err;
250255
int i;
251256

@@ -281,9 +286,6 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
281286
memcpy(ctx->iv, con.iv->iv, ivsize);
282287
}
283288

284-
limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
285-
limit -= ctx->used;
286-
287289
while (size) {
288290
struct scatterlist *sg;
289291
unsigned long len = size;
@@ -309,20 +311,16 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
309311
ctx->used += len;
310312
copied += len;
311313
size -= len;
312-
limit -= len;
313314
continue;
314315
}
315316

316-
if (limit < PAGE_SIZE) {
317+
if (!skcipher_writable(sk)) {
317318
err = skcipher_wait_for_wmem(sk, msg->msg_flags);
318319
if (err)
319320
goto unlock;
320-
321-
limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
322-
limit -= ctx->used;
323321
}
324322

325-
len = min_t(unsigned long, len, limit);
323+
len = min_t(unsigned long, len, skcipher_sndbuf(sk));
326324

327325
err = skcipher_alloc_sgl(sk);
328326
if (err)
@@ -352,7 +350,6 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
352350
ctx->used += plen;
353351
copied += plen;
354352
size -= plen;
355-
limit -= plen;
356353
sgl->cur++;
357354
} while (len && sgl->cur < MAX_SGL_ENTS);
358355

@@ -380,7 +377,6 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
380377
struct skcipher_ctx *ctx = ask->private;
381378
struct skcipher_sg_list *sgl;
382379
int err = -EINVAL;
383-
int limit;
384380

385381
lock_sock(sk);
386382
if (!ctx->more && ctx->used)
@@ -389,16 +385,10 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
389385
if (!size)
390386
goto done;
391387

392-
limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
393-
limit -= ctx->used;
394-
395-
if (limit < PAGE_SIZE) {
388+
if (!skcipher_writable(sk)) {
396389
err = skcipher_wait_for_wmem(sk, flags);
397390
if (err)
398391
goto unlock;
399-
400-
limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
401-
limit -= ctx->used;
402392
}
403393

404394
err = skcipher_alloc_sgl(sk);

0 commit comments

Comments
 (0)