Skip to content

Commit 727b8a6

Browse files
herbertxsmb49
authored andcommitted
crypto: null - Use spin lock instead of mutex
BugLink: https://bugs.launchpad.net/bugs/2111268 [ Upstream commit dcc47a028c24e793ce6d6efebfef1a1e92f80297 ] As the null algorithm may be freed in softirq context through af_alg, use spin locks instead of mutexes to protect the default null algorithm. Reported-by: syzbot+b3e02953598f447d4d2a@syzkaller.appspotmail.com Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <sashal@kernel.org> CVE-2025-37808 Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent f092236 commit 727b8a6

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

crypto/crypto_null.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include <crypto/internal/skcipher.h>
1818
#include <linux/init.h>
1919
#include <linux/module.h>
20-
#include <linux/mm.h>
20+
#include <linux/spinlock.h>
2121
#include <linux/string.h>
2222

23-
static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
23+
static DEFINE_SPINLOCK(crypto_default_null_skcipher_lock);
2424
static struct crypto_sync_skcipher *crypto_default_null_skcipher;
2525
static int crypto_default_null_skcipher_refcnt;
2626

@@ -152,36 +152,49 @@ MODULE_ALIAS_CRYPTO("cipher_null");
152152

153153
struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
154154
{
155+
struct crypto_sync_skcipher *ntfm = NULL;
155156
struct crypto_sync_skcipher *tfm;
156157

157-
mutex_lock(&crypto_default_null_skcipher_lock);
158+
spin_lock_bh(&crypto_default_null_skcipher_lock);
158159
tfm = crypto_default_null_skcipher;
159160

160161
if (!tfm) {
161-
tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
162-
if (IS_ERR(tfm))
163-
goto unlock;
164-
165-
crypto_default_null_skcipher = tfm;
162+
spin_unlock_bh(&crypto_default_null_skcipher_lock);
163+
164+
ntfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
165+
if (IS_ERR(ntfm))
166+
return ntfm;
167+
168+
spin_lock_bh(&crypto_default_null_skcipher_lock);
169+
tfm = crypto_default_null_skcipher;
170+
if (!tfm) {
171+
tfm = ntfm;
172+
ntfm = NULL;
173+
crypto_default_null_skcipher = tfm;
174+
}
166175
}
167176

168177
crypto_default_null_skcipher_refcnt++;
178+
spin_unlock_bh(&crypto_default_null_skcipher_lock);
169179

170-
unlock:
171-
mutex_unlock(&crypto_default_null_skcipher_lock);
180+
crypto_free_sync_skcipher(ntfm);
172181

173182
return tfm;
174183
}
175184
EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
176185

177186
void crypto_put_default_null_skcipher(void)
178187
{
179-
mutex_lock(&crypto_default_null_skcipher_lock);
188+
struct crypto_sync_skcipher *tfm = NULL;
189+
190+
spin_lock_bh(&crypto_default_null_skcipher_lock);
180191
if (!--crypto_default_null_skcipher_refcnt) {
181-
crypto_free_sync_skcipher(crypto_default_null_skcipher);
192+
tfm = crypto_default_null_skcipher;
182193
crypto_default_null_skcipher = NULL;
183194
}
184-
mutex_unlock(&crypto_default_null_skcipher_lock);
195+
spin_unlock_bh(&crypto_default_null_skcipher_lock);
196+
197+
crypto_free_sync_skcipher(tfm);
185198
}
186199
EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
187200

0 commit comments

Comments
 (0)