2019-05-27 09:55:01 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Cryptographic API.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
|
2005-11-05 08:58:14 +03:00
|
|
|
* Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
#ifndef _CRYPTO_INTERNAL_H
|
|
|
|
#define _CRYPTO_INTERNAL_H
|
2006-08-21 15:08:13 +04:00
|
|
|
|
|
|
|
#include <crypto/algapi.h>
|
2006-08-06 15:23:26 +04:00
|
|
|
#include <linux/completion.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/init.h>
|
2005-11-05 08:58:14 +03:00
|
|
|
#include <linux/list.h>
|
2006-08-06 15:16:34 +04:00
|
|
|
#include <linux/module.h>
|
2005-07-07 00:53:29 +04:00
|
|
|
#include <linux/kernel.h>
|
2006-08-06 15:23:26 +04:00
|
|
|
#include <linux/notifier.h>
|
2005-11-05 08:58:14 +03:00
|
|
|
#include <linux/rwsem.h>
|
2005-09-02 04:43:05 +04:00
|
|
|
#include <linux/slab.h>
|
2008-08-05 10:13:08 +04:00
|
|
|
|
2006-08-06 15:16:34 +04:00
|
|
|
struct crypto_instance;
|
|
|
|
struct crypto_template;
|
|
|
|
|
2006-08-06 15:23:26 +04:00
|
|
|
struct crypto_larval {
|
|
|
|
struct crypto_alg alg;
|
|
|
|
struct crypto_alg *adult;
|
|
|
|
struct completion completion;
|
2006-09-21 05:35:17 +04:00
|
|
|
u32 mask;
|
2006-08-06 15:23:26 +04:00
|
|
|
};
|
|
|
|
|
2005-11-05 08:58:14 +03:00
|
|
|
extern struct list_head crypto_alg_list;
|
|
|
|
extern struct rw_semaphore crypto_alg_sem;
|
2006-08-06 15:23:26 +04:00
|
|
|
extern struct blocking_notifier_head crypto_chain;
|
2005-11-05 08:58:14 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
void __init crypto_init_proc(void);
|
2006-08-21 15:08:13 +04:00
|
|
|
void __exit crypto_exit_proc(void);
|
2005-04-17 02:20:36 +04:00
|
|
|
#else
|
|
|
|
static inline void crypto_init_proc(void)
|
|
|
|
{ }
|
2006-08-21 15:08:13 +04:00
|
|
|
static inline void crypto_exit_proc(void)
|
|
|
|
{ }
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif
|
|
|
|
|
2007-01-27 02:05:15 +03:00
|
|
|
static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg)
|
2005-07-07 00:53:29 +04:00
|
|
|
{
|
2007-01-27 02:05:15 +03:00
|
|
|
return alg->cra_ctxsize;
|
2005-07-07 00:53:29 +04:00
|
|
|
}
|
|
|
|
|
2007-01-27 02:05:15 +03:00
|
|
|
static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
|
2005-07-07 00:53:29 +04:00
|
|
|
{
|
|
|
|
return alg->cra_ctxsize;
|
|
|
|
}
|
|
|
|
|
2006-08-06 15:23:26 +04:00
|
|
|
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
|
2006-09-21 05:35:17 +04:00
|
|
|
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
|
2006-08-06 15:23:26 +04:00
|
|
|
|
2008-08-03 17:15:23 +04:00
|
|
|
struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
|
2007-12-04 04:46:48 +03:00
|
|
|
void crypto_larval_kill(struct crypto_alg *alg);
|
2008-08-03 17:15:23 +04:00
|
|
|
void crypto_alg_tested(const char *name, int err);
|
2006-08-06 15:23:26 +04:00
|
|
|
|
2011-09-27 09:22:08 +04:00
|
|
|
void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
|
|
|
|
struct crypto_alg *nalg);
|
2011-09-27 09:23:07 +04:00
|
|
|
void crypto_remove_final(struct list_head *list);
|
2007-01-24 12:50:26 +03:00
|
|
|
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
|
|
|
|
u32 mask);
|
2009-02-18 11:56:59 +03:00
|
|
|
void *crypto_create_tfm(struct crypto_alg *alg,
|
|
|
|
const struct crypto_type *frontend);
|
2009-07-08 13:53:16 +04:00
|
|
|
struct crypto_alg *crypto_find_alg(const char *alg_name,
|
|
|
|
const struct crypto_type *frontend,
|
|
|
|
u32 type, u32 mask);
|
2009-02-18 11:56:59 +03:00
|
|
|
void *crypto_alloc_tfm(const char *alg_name,
|
|
|
|
const struct crypto_type *frontend, u32 type, u32 mask);
|
2006-08-06 15:16:34 +04:00
|
|
|
|
2008-08-03 17:15:23 +04:00
|
|
|
int crypto_probing_notify(unsigned long val, void *v);
|
2006-08-06 15:23:26 +04:00
|
|
|
|
2015-04-20 08:39:00 +03:00
|
|
|
unsigned int crypto_alg_extsize(struct crypto_alg *alg);
|
|
|
|
|
2016-01-23 08:51:01 +03:00
|
|
|
int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
|
|
|
|
u32 type, u32 mask);
|
|
|
|
|
crypto: algboss - Hold ref count on larval
On Thu, Jun 20, 2013 at 10:00:21AM +0200, Daniel Borkmann wrote:
> After having fixed a NULL pointer dereference in SCTP 1abd165e ("net:
> sctp: fix NULL pointer dereference in socket destruction"), I ran into
> the following NULL pointer dereference in the crypto subsystem with
> the same reproducer, easily hit each time:
>
> BUG: unable to handle kernel NULL pointer dereference at (null)
> IP: [<ffffffff81070321>] __wake_up_common+0x31/0x90
> PGD 0
> Oops: 0000 [#1] SMP
> Modules linked in: padlock_sha(F-) sha256_generic(F) sctp(F) libcrc32c(F) [..]
> CPU: 6 PID: 3326 Comm: cryptomgr_probe Tainted: GF 3.10.0-rc5+ #1
> Hardware name: Dell Inc. PowerEdge T410/0H19HD, BIOS 1.6.3 02/01/2011
> task: ffff88007b6cf4e0 ti: ffff88007b7cc000 task.ti: ffff88007b7cc000
> RIP: 0010:[<ffffffff81070321>] [<ffffffff81070321>] __wake_up_common+0x31/0x90
> RSP: 0018:ffff88007b7cde08 EFLAGS: 00010082
> RAX: ffffffffffffffe8 RBX: ffff88003756c130 RCX: 0000000000000000
> RDX: 0000000000000000 RSI: 0000000000000003 RDI: ffff88003756c130
> RBP: ffff88007b7cde48 R08: 0000000000000000 R09: ffff88012b173200
> R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000282
> R13: ffff88003756c138 R14: 0000000000000000 R15: 0000000000000000
> FS: 0000000000000000(0000) GS:ffff88012fc60000(0000) knlGS:0000000000000000
> CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
> CR2: 0000000000000000 CR3: 0000000001a0b000 CR4: 00000000000007e0
> DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
> DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
> Stack:
> ffff88007b7cde28 0000000300000000 ffff88007b7cde28 ffff88003756c130
> 0000000000000282 ffff88003756c128 ffffffff81227670 0000000000000000
> ffff88007b7cde78 ffffffff810722b7 ffff88007cdcf000 ffffffff81a90540
> Call Trace:
> [<ffffffff81227670>] ? crypto_alloc_pcomp+0x20/0x20
> [<ffffffff810722b7>] complete_all+0x47/0x60
> [<ffffffff81227708>] cryptomgr_probe+0x98/0xc0
> [<ffffffff81227670>] ? crypto_alloc_pcomp+0x20/0x20
> [<ffffffff8106760e>] kthread+0xce/0xe0
> [<ffffffff81067540>] ? kthread_freezable_should_stop+0x70/0x70
> [<ffffffff815450dc>] ret_from_fork+0x7c/0xb0
> [<ffffffff81067540>] ? kthread_freezable_should_stop+0x70/0x70
> Code: 41 56 41 55 41 54 53 48 83 ec 18 66 66 66 66 90 89 75 cc 89 55 c8
> 4c 8d 6f 08 48 8b 57 08 41 89 cf 4d 89 c6 48 8d 42 e
> RIP [<ffffffff81070321>] __wake_up_common+0x31/0x90
> RSP <ffff88007b7cde08>
> CR2: 0000000000000000
> ---[ end trace b495b19270a4d37e ]---
>
> My assumption is that the following is happening: the minimal SCTP
> tool runs under ``echo 1 > /proc/sys/net/sctp/auth_enable'', hence
> it's making use of crypto_alloc_hash() via sctp_auth_init_hmacs().
> It forks itself, heavily allocates, binds, listens and waits in
> accept on sctp sockets, and then randomly kills some of them (no
> need for an actual client in this case to hit this). Then, again,
> allocating, binding, etc, and then killing child processes.
>
> The problem that might be happening here is that cryptomgr requests
> the module to probe/load through cryptomgr_schedule_probe(), but
> before the thread handler cryptomgr_probe() returns, we return from
> the wait_for_completion_interruptible() function and probably already
> have cleared up larval, thus we run into a NULL pointer dereference
> when in cryptomgr_probe() complete_all() is being called.
>
> If we wait with wait_for_completion() instead, this panic will not
> occur anymore. This is valid, because in case a signal is pending,
> cryptomgr_probe() returns from probing anyway with properly calling
> complete_all().
The use of wait_for_completion_interruptible is intentional so that
we don't lock up the thread if a bug causes us to never wake up.
This bug is caused by the helper thread using the larval without
holding a reference count on it. If the helper thread completes
after the original thread requesting for help has gone away and
destroyed the larval, then we get the crash above.
So the fix is to hold a reference count on the larval.
Cc: <stable@vger.kernel.org> # 3.6+
Reported-by: Daniel Borkmann <dborkman@redhat.com>
Tested-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-06-25 15:15:17 +04:00
|
|
|
static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
|
|
|
|
{
|
2017-12-29 19:00:46 +03:00
|
|
|
refcount_inc(&alg->cra_refcnt);
|
crypto: algboss - Hold ref count on larval
On Thu, Jun 20, 2013 at 10:00:21AM +0200, Daniel Borkmann wrote:
> After having fixed a NULL pointer dereference in SCTP 1abd165e ("net:
> sctp: fix NULL pointer dereference in socket destruction"), I ran into
> the following NULL pointer dereference in the crypto subsystem with
> the same reproducer, easily hit each time:
>
> BUG: unable to handle kernel NULL pointer dereference at (null)
> IP: [<ffffffff81070321>] __wake_up_common+0x31/0x90
> PGD 0
> Oops: 0000 [#1] SMP
> Modules linked in: padlock_sha(F-) sha256_generic(F) sctp(F) libcrc32c(F) [..]
> CPU: 6 PID: 3326 Comm: cryptomgr_probe Tainted: GF 3.10.0-rc5+ #1
> Hardware name: Dell Inc. PowerEdge T410/0H19HD, BIOS 1.6.3 02/01/2011
> task: ffff88007b6cf4e0 ti: ffff88007b7cc000 task.ti: ffff88007b7cc000
> RIP: 0010:[<ffffffff81070321>] [<ffffffff81070321>] __wake_up_common+0x31/0x90
> RSP: 0018:ffff88007b7cde08 EFLAGS: 00010082
> RAX: ffffffffffffffe8 RBX: ffff88003756c130 RCX: 0000000000000000
> RDX: 0000000000000000 RSI: 0000000000000003 RDI: ffff88003756c130
> RBP: ffff88007b7cde48 R08: 0000000000000000 R09: ffff88012b173200
> R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000282
> R13: ffff88003756c138 R14: 0000000000000000 R15: 0000000000000000
> FS: 0000000000000000(0000) GS:ffff88012fc60000(0000) knlGS:0000000000000000
> CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
> CR2: 0000000000000000 CR3: 0000000001a0b000 CR4: 00000000000007e0
> DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
> DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
> Stack:
> ffff88007b7cde28 0000000300000000 ffff88007b7cde28 ffff88003756c130
> 0000000000000282 ffff88003756c128 ffffffff81227670 0000000000000000
> ffff88007b7cde78 ffffffff810722b7 ffff88007cdcf000 ffffffff81a90540
> Call Trace:
> [<ffffffff81227670>] ? crypto_alloc_pcomp+0x20/0x20
> [<ffffffff810722b7>] complete_all+0x47/0x60
> [<ffffffff81227708>] cryptomgr_probe+0x98/0xc0
> [<ffffffff81227670>] ? crypto_alloc_pcomp+0x20/0x20
> [<ffffffff8106760e>] kthread+0xce/0xe0
> [<ffffffff81067540>] ? kthread_freezable_should_stop+0x70/0x70
> [<ffffffff815450dc>] ret_from_fork+0x7c/0xb0
> [<ffffffff81067540>] ? kthread_freezable_should_stop+0x70/0x70
> Code: 41 56 41 55 41 54 53 48 83 ec 18 66 66 66 66 90 89 75 cc 89 55 c8
> 4c 8d 6f 08 48 8b 57 08 41 89 cf 4d 89 c6 48 8d 42 e
> RIP [<ffffffff81070321>] __wake_up_common+0x31/0x90
> RSP <ffff88007b7cde08>
> CR2: 0000000000000000
> ---[ end trace b495b19270a4d37e ]---
>
> My assumption is that the following is happening: the minimal SCTP
> tool runs under ``echo 1 > /proc/sys/net/sctp/auth_enable'', hence
> it's making use of crypto_alloc_hash() via sctp_auth_init_hmacs().
> It forks itself, heavily allocates, binds, listens and waits in
> accept on sctp sockets, and then randomly kills some of them (no
> need for an actual client in this case to hit this). Then, again,
> allocating, binding, etc, and then killing child processes.
>
> The problem that might be happening here is that cryptomgr requests
> the module to probe/load through cryptomgr_schedule_probe(), but
> before the thread handler cryptomgr_probe() returns, we return from
> the wait_for_completion_interruptible() function and probably already
> have cleared up larval, thus we run into a NULL pointer dereference
> when in cryptomgr_probe() complete_all() is being called.
>
> If we wait with wait_for_completion() instead, this panic will not
> occur anymore. This is valid, because in case a signal is pending,
> cryptomgr_probe() returns from probing anyway with properly calling
> complete_all().
The use of wait_for_completion_interruptible is intentional so that
we don't lock up the thread if a bug causes us to never wake up.
This bug is caused by the helper thread using the larval without
holding a reference count on it. If the helper thread completes
after the original thread requesting for help has gone away and
destroyed the larval, then we get the crash above.
So the fix is to hold a reference count on the larval.
Cc: <stable@vger.kernel.org> # 3.6+
Reported-by: Daniel Borkmann <dborkman@redhat.com>
Tested-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-06-25 15:15:17 +04:00
|
|
|
return alg;
|
|
|
|
}
|
|
|
|
|
2006-09-21 05:39:29 +04:00
|
|
|
static inline void crypto_alg_put(struct crypto_alg *alg)
|
|
|
|
{
|
2017-12-29 19:00:46 +03:00
|
|
|
if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
|
2006-09-21 05:39:29 +04:00
|
|
|
alg->cra_destroy(alg);
|
|
|
|
}
|
|
|
|
|
2006-08-06 15:16:34 +04:00
|
|
|
static inline int crypto_tmpl_get(struct crypto_template *tmpl)
|
|
|
|
{
|
|
|
|
return try_module_get(tmpl->module);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void crypto_tmpl_put(struct crypto_template *tmpl)
|
|
|
|
{
|
|
|
|
module_put(tmpl->module);
|
|
|
|
}
|
|
|
|
|
2006-08-06 15:23:26 +04:00
|
|
|
static inline int crypto_is_larval(struct crypto_alg *alg)
|
|
|
|
{
|
|
|
|
return alg->cra_flags & CRYPTO_ALG_LARVAL;
|
|
|
|
}
|
|
|
|
|
2006-09-21 05:39:29 +04:00
|
|
|
static inline int crypto_is_dead(struct crypto_alg *alg)
|
|
|
|
{
|
|
|
|
return alg->cra_flags & CRYPTO_ALG_DEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int crypto_is_moribund(struct crypto_alg *alg)
|
|
|
|
{
|
|
|
|
return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
|
|
|
|
}
|
|
|
|
|
2008-08-03 17:15:23 +04:00
|
|
|
static inline void crypto_notify(unsigned long val, void *v)
|
2006-08-06 15:23:26 +04:00
|
|
|
{
|
2008-08-03 17:15:23 +04:00
|
|
|
blocking_notifier_call_chain(&crypto_chain, val, v);
|
2006-08-06 15:23:26 +04:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif /* _CRYPTO_INTERNAL_H */
|
|
|
|
|