@@ -280,11 +280,7 @@ EXPORT_SYMBOL(keyring_alloc);
280
280
/**
281
281
* keyring_search_aux - Search a keyring tree for a key matching some criteria
282
282
* @keyring_ref: A pointer to the keyring with possession indicator.
283
- * @cred: The credentials to use for permissions checks.
284
- * @type: The type of key to search for.
285
- * @description: Parameter for @match.
286
- * @match: Function to rule on whether or not a key is the one required.
287
- * @no_state_check: Don't check if a matching key is bad
283
+ * @ctx: The keyring search context.
288
284
*
289
285
* Search the supplied keyring tree for a key that matches the criteria given.
290
286
* The root keyring and any linked keyrings must grant Search permission to the
@@ -314,11 +310,7 @@ EXPORT_SYMBOL(keyring_alloc);
314
310
* @keyring_ref is propagated to the returned key reference.
315
311
*/
316
312
key_ref_t keyring_search_aux (key_ref_t keyring_ref ,
317
- const struct cred * cred ,
318
- struct key_type * type ,
319
- const void * description ,
320
- key_match_func_t match ,
321
- bool no_state_check )
313
+ struct keyring_search_context * ctx )
322
314
{
323
315
struct {
324
316
/* Need a separate keylist pointer for RCU purposes */
@@ -328,20 +320,18 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
328
320
} stack [KEYRING_SEARCH_MAX_DEPTH ];
329
321
330
322
struct keyring_list * keylist ;
331
- struct timespec now ;
332
323
unsigned long kflags ;
333
324
struct key * keyring , * key ;
334
325
key_ref_t key_ref ;
335
- bool possessed ;
336
326
long err ;
337
327
int sp , nkeys , kix ;
338
328
339
329
keyring = key_ref_to_ptr (keyring_ref );
340
- possessed = is_key_possessed (keyring_ref );
330
+ ctx -> possessed = is_key_possessed (keyring_ref );
341
331
key_check (keyring );
342
332
343
333
/* top keyring must have search permission to begin the search */
344
- err = key_task_permission (keyring_ref , cred , KEY_SEARCH );
334
+ err = key_task_permission (keyring_ref , ctx -> cred , KEY_SEARCH );
345
335
if (err < 0 ) {
346
336
key_ref = ERR_PTR (err );
347
337
goto error ;
@@ -353,24 +343,25 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
353
343
354
344
rcu_read_lock ();
355
345
356
- now = current_kernel_time ();
346
+ ctx -> now = current_kernel_time ();
357
347
err = - EAGAIN ;
358
348
sp = 0 ;
359
349
360
350
/* firstly we should check to see if this top-level keyring is what we
361
351
* are looking for */
362
352
key_ref = ERR_PTR (- EAGAIN );
363
353
kflags = keyring -> flags ;
364
- if (keyring -> type == type && match (keyring , description )) {
354
+ if (keyring -> type == ctx -> index_key .type &&
355
+ ctx -> match (keyring , ctx -> match_data )) {
365
356
key = keyring ;
366
- if (no_state_check )
357
+ if (ctx -> flags & KEYRING_SEARCH_NO_STATE_CHECK )
367
358
goto found ;
368
359
369
360
/* check it isn't negative and hasn't expired or been
370
361
* revoked */
371
362
if (kflags & (1 << KEY_FLAG_REVOKED ))
372
363
goto error_2 ;
373
- if (key -> expiry && now .tv_sec >= key -> expiry )
364
+ if (key -> expiry && ctx -> now .tv_sec >= key -> expiry )
374
365
goto error_2 ;
375
366
key_ref = ERR_PTR (key -> type_data .reject_error );
376
367
if (kflags & (1 << KEY_FLAG_NEGATIVE ))
@@ -384,7 +375,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
384
375
if (kflags & ((1 << KEY_FLAG_INVALIDATED ) |
385
376
(1 << KEY_FLAG_REVOKED ) |
386
377
(1 << KEY_FLAG_NEGATIVE )) ||
387
- (keyring -> expiry && now .tv_sec >= keyring -> expiry ))
378
+ (keyring -> expiry && ctx -> now .tv_sec >= keyring -> expiry ))
388
379
goto error_2 ;
389
380
390
381
/* start processing a new keyring */
@@ -406,29 +397,29 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
406
397
kflags = key -> flags ;
407
398
408
399
/* ignore keys not of this type */
409
- if (key -> type != type )
400
+ if (key -> type != ctx -> index_key . type )
410
401
continue ;
411
402
412
403
/* skip invalidated, revoked and expired keys */
413
- if (!no_state_check ) {
404
+ if (!( ctx -> flags & KEYRING_SEARCH_NO_STATE_CHECK ) ) {
414
405
if (kflags & ((1 << KEY_FLAG_INVALIDATED ) |
415
406
(1 << KEY_FLAG_REVOKED )))
416
407
continue ;
417
408
418
- if (key -> expiry && now .tv_sec >= key -> expiry )
409
+ if (key -> expiry && ctx -> now .tv_sec >= key -> expiry )
419
410
continue ;
420
411
}
421
412
422
413
/* keys that don't match */
423
- if (!match (key , description ))
414
+ if (!ctx -> match (key , ctx -> match_data ))
424
415
continue ;
425
416
426
417
/* key must have search permissions */
427
- if (key_task_permission (make_key_ref (key , possessed ),
428
- cred , KEY_SEARCH ) < 0 )
418
+ if (key_task_permission (make_key_ref (key , ctx -> possessed ),
419
+ ctx -> cred , KEY_SEARCH ) < 0 )
429
420
continue ;
430
421
431
- if (no_state_check )
422
+ if (ctx -> flags & KEYRING_SEARCH_NO_STATE_CHECK )
432
423
goto found ;
433
424
434
425
/* we set a different error code if we pass a negative key */
@@ -456,8 +447,8 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
456
447
if (sp >= KEYRING_SEARCH_MAX_DEPTH )
457
448
continue ;
458
449
459
- if (key_task_permission (make_key_ref (key , possessed ),
460
- cred , KEY_SEARCH ) < 0 )
450
+ if (key_task_permission (make_key_ref (key , ctx -> possessed ),
451
+ ctx -> cred , KEY_SEARCH ) < 0 )
461
452
continue ;
462
453
463
454
/* stack the current position */
@@ -489,12 +480,12 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
489
480
/* we found a viable match */
490
481
found :
491
482
atomic_inc (& key -> usage );
492
- key -> last_used_at = now .tv_sec ;
493
- keyring -> last_used_at = now .tv_sec ;
483
+ key -> last_used_at = ctx -> now .tv_sec ;
484
+ keyring -> last_used_at = ctx -> now .tv_sec ;
494
485
while (sp > 0 )
495
- stack [-- sp ].keyring -> last_used_at = now .tv_sec ;
486
+ stack [-- sp ].keyring -> last_used_at = ctx -> now .tv_sec ;
496
487
key_check (key );
497
- key_ref = make_key_ref (key , possessed );
488
+ key_ref = make_key_ref (key , ctx -> possessed );
498
489
error_2 :
499
490
rcu_read_unlock ();
500
491
error :
@@ -514,11 +505,20 @@ key_ref_t keyring_search(key_ref_t keyring,
514
505
struct key_type * type ,
515
506
const char * description )
516
507
{
517
- if (!type -> match )
508
+ struct keyring_search_context ctx = {
509
+ .index_key .type = type ,
510
+ .index_key .description = description ,
511
+ .cred = current_cred (),
512
+ .match = type -> match ,
513
+ .match_data = description ,
514
+ .flags = (type -> def_lookup_type |
515
+ KEYRING_SEARCH_DO_STATE_CHECK ),
516
+ };
517
+
518
+ if (!ctx .match )
518
519
return ERR_PTR (- ENOKEY );
519
520
520
- return keyring_search_aux (keyring , current -> cred ,
521
- type , description , type -> match , false);
521
+ return keyring_search_aux (keyring , & ctx );
522
522
}
523
523
EXPORT_SYMBOL (keyring_search );
524
524
0 commit comments