@@ -394,6 +394,13 @@ static void keyring_destroy(struct key *keyring)
394
394
write_unlock (& keyring_name_lock );
395
395
}
396
396
397
+ if (keyring -> restrict_link ) {
398
+ struct key_restriction * keyres = keyring -> restrict_link ;
399
+
400
+ key_put (keyres -> key );
401
+ kfree (keyres );
402
+ }
403
+
397
404
assoc_array_destroy (& keyring -> keys , & keyring_assoc_array_ops );
398
405
}
399
406
@@ -492,7 +499,7 @@ static long keyring_read(const struct key *keyring,
492
499
struct key * keyring_alloc (const char * description , kuid_t uid , kgid_t gid ,
493
500
const struct cred * cred , key_perm_t perm ,
494
501
unsigned long flags ,
495
- key_restrict_link_func_t restrict_link ,
502
+ struct key_restriction * restrict_link ,
496
503
struct key * dest )
497
504
{
498
505
struct key * keyring ;
@@ -523,8 +530,8 @@ EXPORT_SYMBOL(keyring_alloc);
523
530
* passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
524
531
* adding a key to a keyring.
525
532
*
526
- * This is meant to be passed as the restrict_link parameter to
527
- * keyring_alloc().
533
+ * This is meant to be stored in a key_restriction structure which is passed
534
+ * in the restrict_link parameter to keyring_alloc().
528
535
*/
529
536
int restrict_link_reject (struct key * keyring ,
530
537
const struct key_type * type ,
@@ -1220,9 +1227,10 @@ void __key_link_end(struct key *keyring,
1220
1227
*/
1221
1228
static int __key_link_check_restriction (struct key * keyring , struct key * key )
1222
1229
{
1223
- if (!keyring -> restrict_link )
1230
+ if (!keyring -> restrict_link || ! keyring -> restrict_link -> check )
1224
1231
return 0 ;
1225
- return keyring -> restrict_link (keyring , key -> type , & key -> payload , NULL );
1232
+ return keyring -> restrict_link -> check (keyring , key -> type , & key -> payload ,
1233
+ keyring -> restrict_link -> key );
1226
1234
}
1227
1235
1228
1236
/**
@@ -1426,3 +1434,53 @@ void keyring_gc(struct key *keyring, time_t limit)
1426
1434
up_write (& keyring -> sem );
1427
1435
kleave (" [gc]" );
1428
1436
}
1437
+
1438
+ /*
1439
+ * Garbage collect restriction pointers from a keyring.
1440
+ *
1441
+ * Keyring restrictions are associated with a key type, and must be cleaned
1442
+ * up if the key type is unregistered. The restriction is altered to always
1443
+ * reject additional keys so a keyring cannot be opened up by unregistering
1444
+ * a key type.
1445
+ *
1446
+ * Not called with any keyring locks held. The keyring's key struct will not
1447
+ * be deallocated under us as only our caller may deallocate it.
1448
+ *
1449
+ * The caller is required to hold key_types_sem and dead_type->sem. This is
1450
+ * fulfilled by key_gc_keytype() holding the locks on behalf of
1451
+ * key_garbage_collector(), which it invokes on a workqueue.
1452
+ */
1453
+ void keyring_restriction_gc (struct key * keyring , struct key_type * dead_type )
1454
+ {
1455
+ struct key_restriction * keyres ;
1456
+
1457
+ kenter ("%x{%s}" , keyring -> serial , keyring -> description ?: "" );
1458
+
1459
+ /*
1460
+ * keyring->restrict_link is only assigned at key allocation time
1461
+ * or with the key type locked, so the only values that could be
1462
+ * concurrently assigned to keyring->restrict_link are for key
1463
+ * types other than dead_type. Given this, it's ok to check
1464
+ * the key type before acquiring keyring->sem.
1465
+ */
1466
+ if (!dead_type || !keyring -> restrict_link ||
1467
+ keyring -> restrict_link -> keytype != dead_type ) {
1468
+ kleave (" [no restriction gc]" );
1469
+ return ;
1470
+ }
1471
+
1472
+ /* Lock the keyring to ensure that a link is not in progress */
1473
+ down_write (& keyring -> sem );
1474
+
1475
+ keyres = keyring -> restrict_link ;
1476
+
1477
+ keyres -> check = restrict_link_reject ;
1478
+
1479
+ key_put (keyres -> key );
1480
+ keyres -> key = NULL ;
1481
+ keyres -> keytype = NULL ;
1482
+
1483
+ up_write (& keyring -> sem );
1484
+
1485
+ kleave (" [restriction gc]" );
1486
+ }
0 commit comments