mirror of
https://kernel.googlesource.com/pub/scm/linux/kernel/git/stable/linux-stable.git
synced 2025-09-14 11:19:08 +10:00
crypto: api - Add crypto_clone_tfm
[ Upstream commit3c3a24cb0a
] This patch adds the helper crypto_clone_tfm. The purpose is to allocate a tfm object with GFP_ATOMIC. As we cannot sleep, the object has to be cloned from an existing tfm object. This allows code paths that cannot otherwise allocate a crypto_tfm object to do so. Once a new tfm has been obtained its key could then be changed without impacting other users. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Reviewed-by: Simon Horman <simon.horman@corigine.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Stable-dep-of:1465036b10
("llc: Improve setsockopt() handling of malformed user input") Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
e470d423b0
commit
f688979e42
59
crypto/api.c
59
crypto/api.c
@ -489,28 +489,44 @@ err:
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(crypto_alloc_base);
|
EXPORT_SYMBOL_GPL(crypto_alloc_base);
|
||||||
|
|
||||||
void *crypto_create_tfm_node(struct crypto_alg *alg,
|
static void *crypto_alloc_tfmmem(struct crypto_alg *alg,
|
||||||
const struct crypto_type *frontend,
|
const struct crypto_type *frontend, int node,
|
||||||
int node)
|
gfp_t gfp)
|
||||||
{
|
{
|
||||||
char *mem;
|
struct crypto_tfm *tfm;
|
||||||
struct crypto_tfm *tfm = NULL;
|
|
||||||
unsigned int tfmsize;
|
unsigned int tfmsize;
|
||||||
unsigned int total;
|
unsigned int total;
|
||||||
int err = -ENOMEM;
|
char *mem;
|
||||||
|
|
||||||
tfmsize = frontend->tfmsize;
|
tfmsize = frontend->tfmsize;
|
||||||
total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
|
total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
|
||||||
|
|
||||||
mem = kzalloc_node(total, GFP_KERNEL, node);
|
mem = kzalloc_node(total, gfp, node);
|
||||||
if (mem == NULL)
|
if (mem == NULL)
|
||||||
goto out_err;
|
return ERR_PTR(-ENOMEM);
|
||||||
|
|
||||||
tfm = (struct crypto_tfm *)(mem + tfmsize);
|
tfm = (struct crypto_tfm *)(mem + tfmsize);
|
||||||
tfm->__crt_alg = alg;
|
tfm->__crt_alg = alg;
|
||||||
tfm->node = node;
|
tfm->node = node;
|
||||||
refcount_set(&tfm->refcnt, 1);
|
refcount_set(&tfm->refcnt, 1);
|
||||||
|
|
||||||
|
return mem;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *crypto_create_tfm_node(struct crypto_alg *alg,
|
||||||
|
const struct crypto_type *frontend,
|
||||||
|
int node)
|
||||||
|
{
|
||||||
|
struct crypto_tfm *tfm;
|
||||||
|
char *mem;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
mem = crypto_alloc_tfmmem(alg, frontend, node, GFP_KERNEL);
|
||||||
|
if (IS_ERR(mem))
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
|
||||||
|
|
||||||
err = frontend->init_tfm(tfm);
|
err = frontend->init_tfm(tfm);
|
||||||
if (err)
|
if (err)
|
||||||
goto out_free_tfm;
|
goto out_free_tfm;
|
||||||
@ -526,13 +542,38 @@ out_free_tfm:
|
|||||||
if (err == -EAGAIN)
|
if (err == -EAGAIN)
|
||||||
crypto_shoot_alg(alg);
|
crypto_shoot_alg(alg);
|
||||||
kfree(mem);
|
kfree(mem);
|
||||||
out_err:
|
|
||||||
mem = ERR_PTR(err);
|
mem = ERR_PTR(err);
|
||||||
out:
|
out:
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
|
EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
|
||||||
|
|
||||||
|
void *crypto_clone_tfm(const struct crypto_type *frontend,
|
||||||
|
struct crypto_tfm *otfm)
|
||||||
|
{
|
||||||
|
struct crypto_alg *alg = otfm->__crt_alg;
|
||||||
|
struct crypto_tfm *tfm;
|
||||||
|
char *mem;
|
||||||
|
|
||||||
|
mem = ERR_PTR(-ESTALE);
|
||||||
|
if (unlikely(!crypto_mod_get(alg)))
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
mem = crypto_alloc_tfmmem(alg, frontend, otfm->node, GFP_ATOMIC);
|
||||||
|
if (IS_ERR(mem)) {
|
||||||
|
crypto_mod_put(alg);
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
|
||||||
|
tfm->crt_flags = otfm->crt_flags;
|
||||||
|
tfm->exit = otfm->exit;
|
||||||
|
|
||||||
|
out:
|
||||||
|
return mem;
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(crypto_clone_tfm);
|
||||||
|
|
||||||
struct crypto_alg *crypto_find_alg(const char *alg_name,
|
struct crypto_alg *crypto_find_alg(const char *alg_name,
|
||||||
const struct crypto_type *frontend,
|
const struct crypto_type *frontend,
|
||||||
u32 type, u32 mask)
|
u32 type, u32 mask)
|
||||||
|
@ -86,6 +86,8 @@ struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
|
|||||||
u32 mask);
|
u32 mask);
|
||||||
void *crypto_create_tfm_node(struct crypto_alg *alg,
|
void *crypto_create_tfm_node(struct crypto_alg *alg,
|
||||||
const struct crypto_type *frontend, int node);
|
const struct crypto_type *frontend, int node);
|
||||||
|
void *crypto_clone_tfm(const struct crypto_type *frontend,
|
||||||
|
struct crypto_tfm *otfm);
|
||||||
|
|
||||||
static inline void *crypto_create_tfm(struct crypto_alg *alg,
|
static inline void *crypto_create_tfm(struct crypto_alg *alg,
|
||||||
const struct crypto_type *frontend)
|
const struct crypto_type *frontend)
|
||||||
|
Loading…
Reference in New Issue
Block a user