Skip to content

Commit 5f567ff

Browse files
committed
crypto: api - Retain alg refcount in crypto_grab_spawn
This patch changes crypto_grab_spawn to retain the reference count on the algorithm. This is because the caller needs to access the algorithm parameters and without the reference count the algorithm can be freed at any time. The reference count will be subsequently dropped by the crypto API once the instance has been registered. The helper crypto_drop_spawn will also conditionally drop the reference count depending on whether it has been registered. Note that the code is actually added to crypto_init_spawn. However, unless the caller activates this by setting spawn->dropref beforehand then nothing happens. The only caller that sets dropref is currently crypto_grab_spawn. Once all legacy users of crypto_init_spawn disappear, then we can kill the dropref flag. Internally each instance will maintain a list of its spawns prior to registration. This memory used by this list is shared with other fields that are only used after registration. In order for this to work a new flag spawn->registered is added to indicate whether spawn->inst can be used. Fixes: d6ef2f1 ("crypto: api - Add crypto_grab_spawn primitive") Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3932aa1 commit 5f567ff

2 files changed

Lines changed: 55 additions & 10 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ static void crypto_remove_instance(struct crypto_instance *inst,
124124
return;
125125

126126
inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
127-
if (hlist_unhashed(&inst->list))
128-
return;
129127

130128
if (!tmpl || !crypto_tmpl_get(tmpl))
131129
return;
@@ -175,17 +173,26 @@ void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
175173
list);
176174
inst = spawn->inst;
177175

178-
BUG_ON(&inst->alg == alg);
179-
180176
list_move(&spawn->list, &stack);
177+
spawn->dead = !spawn->registered || &inst->alg != nalg;
178+
179+
if (!spawn->registered)
180+
break;
181+
182+
BUG_ON(&inst->alg == alg);
181183

182184
if (&inst->alg == nalg)
183185
break;
184186

185-
spawn->dead = true;
186187
spawns = &inst->alg.cra_users;
187188

188189
/*
190+
* Even if spawn->registered is true, the
191+
* instance itself may still be unregistered.
192+
* This is because it may have failed during
193+
* registration. Therefore we still need to
194+
* make the following test.
195+
*
189196
* We may encounter an unregistered instance here, since
190197
* an instance's spawns are set up prior to the instance
191198
* being registered. An unregistered instance will have
@@ -208,7 +215,7 @@ void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
208215
list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
209216
if (!spawn->dead)
210217
list_move(&spawn->list, &spawn->alg->cra_users);
211-
else
218+
else if (spawn->registered)
212219
crypto_remove_instance(spawn->inst, list);
213220
}
214221
}
@@ -599,6 +606,7 @@ int crypto_register_instance(struct crypto_template *tmpl,
599606
struct crypto_instance *inst)
600607
{
601608
struct crypto_larval *larval;
609+
struct crypto_spawn *spawn;
602610
int err;
603611

604612
err = crypto_check_alg(&inst->alg);
@@ -610,6 +618,23 @@ int crypto_register_instance(struct crypto_template *tmpl,
610618

611619
down_write(&crypto_alg_sem);
612620

621+
larval = ERR_PTR(-EAGAIN);
622+
for (spawn = inst->spawns; spawn;) {
623+
struct crypto_spawn *next;
624+
625+
if (spawn->dead)
626+
goto unlock;
627+
628+
next = spawn->next;
629+
spawn->inst = inst;
630+
spawn->registered = true;
631+
632+
if (spawn->dropref)
633+
crypto_mod_put(spawn->alg);
634+
635+
spawn = next;
636+
}
637+
613638
larval = __crypto_register_alg(&inst->alg);
614639
if (IS_ERR(larval))
615640
goto unlock;
@@ -655,7 +680,9 @@ int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
655680
if (WARN_ON_ONCE(inst == NULL))
656681
return -EINVAL;
657682

658-
spawn->inst = inst;
683+
spawn->next = inst->spawns;
684+
inst->spawns = spawn;
685+
659686
spawn->mask = mask;
660687

661688
down_write(&crypto_alg_sem);
@@ -697,8 +724,10 @@ int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name,
697724
if (IS_ERR(alg))
698725
return PTR_ERR(alg);
699726

727+
spawn->dropref = true;
700728
err = crypto_init_spawn(spawn, alg, spawn->inst, mask);
701-
crypto_mod_put(alg);
729+
if (err)
730+
crypto_mod_put(alg);
702731
return err;
703732
}
704733
EXPORT_SYMBOL_GPL(crypto_grab_spawn);
@@ -709,6 +738,9 @@ void crypto_drop_spawn(struct crypto_spawn *spawn)
709738
if (!spawn->dead)
710739
list_del(&spawn->list);
711740
up_write(&crypto_alg_sem);
741+
742+
if (spawn->dropref && !spawn->registered)
743+
crypto_mod_put(spawn->alg);
712744
}
713745
EXPORT_SYMBOL_GPL(crypto_drop_spawn);
714746

‎include/crypto/algapi.h‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ struct crypto_instance {
4747
struct crypto_alg alg;
4848

4949
struct crypto_template *tmpl;
50-
struct hlist_node list;
50+
51+
union {
52+
/* Node in list of instances after registration. */
53+
struct hlist_node list;
54+
/* List of attached spawns before registration. */
55+
struct crypto_spawn *spawns;
56+
};
5157

5258
void *__ctx[] CRYPTO_MINALIGN_ATTR;
5359
};
@@ -67,10 +73,17 @@ struct crypto_template {
6773
struct crypto_spawn {
6874
struct list_head list;
6975
struct crypto_alg *alg;
70-
struct crypto_instance *inst;
76+
union {
77+
/* Back pointer to instance after registration.*/
78+
struct crypto_instance *inst;
79+
/* Spawn list pointer prior to registration. */
80+
struct crypto_spawn *next;
81+
};
7182
const struct crypto_type *frontend;
7283
u32 mask;
7384
bool dead;
85+
bool dropref;
86+
bool registered;
7487
};
7588

7689
struct crypto_queue {

0 commit comments

Comments
 (0)