Skip to content

Commit 34c86f4

Browse files
committed
crypto: af_alg - fix use-after-free in af_alg_accept() due to bh_lock_sock()
The locking in af_alg_release_parent is broken as the BH socket lock can only be taken if there is a code-path to handle the case where the lock is owned by process-context. Instead of adding such handling, we can fix this by changing the ref counts to atomic_t. This patch also modifies the main refcnt to include both normal and nokey sockets. This way we don't have to fudge the nokey ref count when a socket changes from nokey to normal. Credits go to Mauricio Faria de Oliveira who diagnosed this bug and sent a patch for it: https://lore.kernel.org/linux-crypto/[email protected]/ Reported-by: Brian Moyles <[email protected]> Reported-by: Mauricio Faria de Oliveira <[email protected]> Fixes: 37f9669 ("crypto: af_alg - Use bh_lock_sock in...") Cc: <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 819966c commit 34c86f4

File tree

5 files changed

+22
-35
lines changed

5 files changed

+22
-35
lines changed

crypto/af_alg.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,15 @@ EXPORT_SYMBOL_GPL(af_alg_release);
128128
void af_alg_release_parent(struct sock *sk)
129129
{
130130
struct alg_sock *ask = alg_sk(sk);
131-
unsigned int nokey = ask->nokey_refcnt;
132-
bool last = nokey && !ask->refcnt;
131+
unsigned int nokey = atomic_read(&ask->nokey_refcnt);
133132

134133
sk = ask->parent;
135134
ask = alg_sk(sk);
136135

137-
local_bh_disable();
138-
bh_lock_sock(sk);
139-
ask->nokey_refcnt -= nokey;
140-
if (!last)
141-
last = !--ask->refcnt;
142-
bh_unlock_sock(sk);
143-
local_bh_enable();
136+
if (nokey)
137+
atomic_dec(&ask->nokey_refcnt);
144138

145-
if (last)
139+
if (atomic_dec_and_test(&ask->refcnt))
146140
sock_put(sk);
147141
}
148142
EXPORT_SYMBOL_GPL(af_alg_release_parent);
@@ -187,7 +181,7 @@ static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
187181

188182
err = -EBUSY;
189183
lock_sock(sk);
190-
if (ask->refcnt | ask->nokey_refcnt)
184+
if (atomic_read(&ask->refcnt))
191185
goto unlock;
192186

193187
swap(ask->type, type);
@@ -236,7 +230,7 @@ static int alg_setsockopt(struct socket *sock, int level, int optname,
236230
int err = -EBUSY;
237231

238232
lock_sock(sk);
239-
if (ask->refcnt)
233+
if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
240234
goto unlock;
241235

242236
type = ask->type;
@@ -301,12 +295,14 @@ int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
301295
if (err)
302296
goto unlock;
303297

304-
if (nokey || !ask->refcnt++)
298+
if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
305299
sock_hold(sk);
306-
ask->nokey_refcnt += nokey;
300+
if (nokey) {
301+
atomic_inc(&ask->nokey_refcnt);
302+
atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
303+
}
307304
alg_sk(sk2)->parent = sk;
308305
alg_sk(sk2)->type = type;
309-
alg_sk(sk2)->nokey_refcnt = nokey;
310306

311307
newsock->ops = type->ops;
312308
newsock->state = SS_CONNECTED;

crypto/algif_aead.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static int aead_check_key(struct socket *sock)
384384
struct alg_sock *ask = alg_sk(sk);
385385

386386
lock_sock(sk);
387-
if (ask->refcnt)
387+
if (!atomic_read(&ask->nokey_refcnt))
388388
goto unlock_child;
389389

390390
psk = ask->parent;
@@ -396,11 +396,8 @@ static int aead_check_key(struct socket *sock)
396396
if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
397397
goto unlock;
398398

399-
if (!pask->refcnt++)
400-
sock_hold(psk);
401-
402-
ask->refcnt = 1;
403-
sock_put(psk);
399+
atomic_dec(&pask->nokey_refcnt);
400+
atomic_set(&ask->nokey_refcnt, 0);
404401

405402
err = 0;
406403

crypto/algif_hash.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static int hash_check_key(struct socket *sock)
301301
struct alg_sock *ask = alg_sk(sk);
302302

303303
lock_sock(sk);
304-
if (ask->refcnt)
304+
if (!atomic_read(&ask->nokey_refcnt))
305305
goto unlock_child;
306306

307307
psk = ask->parent;
@@ -313,11 +313,8 @@ static int hash_check_key(struct socket *sock)
313313
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
314314
goto unlock;
315315

316-
if (!pask->refcnt++)
317-
sock_hold(psk);
318-
319-
ask->refcnt = 1;
320-
sock_put(psk);
316+
atomic_dec(&pask->nokey_refcnt);
317+
atomic_set(&ask->nokey_refcnt, 0);
321318

322319
err = 0;
323320

crypto/algif_skcipher.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static int skcipher_check_key(struct socket *sock)
211211
struct alg_sock *ask = alg_sk(sk);
212212

213213
lock_sock(sk);
214-
if (ask->refcnt)
214+
if (!atomic_read(&ask->nokey_refcnt))
215215
goto unlock_child;
216216

217217
psk = ask->parent;
@@ -223,11 +223,8 @@ static int skcipher_check_key(struct socket *sock)
223223
if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
224224
goto unlock;
225225

226-
if (!pask->refcnt++)
227-
sock_hold(psk);
228-
229-
ask->refcnt = 1;
230-
sock_put(psk);
226+
atomic_dec(&pask->nokey_refcnt);
227+
atomic_set(&ask->nokey_refcnt, 0);
231228

232229
err = 0;
233230

include/crypto/if_alg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct alg_sock {
2929

3030
struct sock *parent;
3131

32-
unsigned int refcnt;
33-
unsigned int nokey_refcnt;
32+
atomic_t refcnt;
33+
atomic_t nokey_refcnt;
3434

3535
const struct af_alg_type *type;
3636
void *private;

0 commit comments

Comments
 (0)