Merge tag 'keys-v7.4-rc3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd

Pull key fixes from Jarkko Sakkinen:

 - Invoke the match callback before the state checks in
   keyring_search_iterator(), so that a non-matching revoked,
   expired or invalidated key cannot decide the search result

 - Take key->sem for reading around the ->asym_*() method calls
   in the keyctl pkey operations, as a key reference does not
   pin the module providing the key type

* tag 'keys-v7.4-rc3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd:
  KEYS: Protect pkey method checks against type removal
  KEYS: do not let an unmatched key decide the search result
This commit is contained in:
Linus Torvalds
2026-11-13 18:14:32 -08:00
2 changed files with 82 additions and 30 deletions
+76 -24
View File
@@ -129,45 +129,77 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
if (ret < 0)
return ret;
down_read(&params->key->sem);
if (!params->key->type->asym_query) {
ret = -EOPNOTSUPP;
goto out_up_key_sem;
}
ret = params->key->type->asym_query(params, &info);
if (ret < 0)
return ret;
goto out_up_key_sem;
switch (op) {
case KEYCTL_PKEY_ENCRYPT:
if (uparams.in_len > info.max_dec_size ||
uparams.out_len > info.max_enc_size)
return -EINVAL;
uparams.out_len > info.max_enc_size) {
ret = -EINVAL;
goto out_up_key_sem;
}
params->out_len = info.max_enc_size;
break;
case KEYCTL_PKEY_DECRYPT:
if (uparams.in_len > info.max_enc_size ||
uparams.out_len > info.max_dec_size)
return -EINVAL;
uparams.out_len > info.max_dec_size) {
ret = -EINVAL;
goto out_up_key_sem;
}
params->out_len = info.max_dec_size;
break;
case KEYCTL_PKEY_SIGN:
if (uparams.in_len > info.max_data_size ||
uparams.out_len > info.max_sig_size)
return -EINVAL;
uparams.out_len > info.max_sig_size) {
ret = -EINVAL;
goto out_up_key_sem;
}
params->out_len = info.max_sig_size;
break;
case KEYCTL_PKEY_VERIFY:
if (uparams.in_len > info.max_data_size ||
uparams.in2_len > info.max_sig_size)
return -EINVAL;
uparams.in2_len > info.max_sig_size) {
ret = -EINVAL;
goto out_up_key_sem;
}
params->out_len = info.max_sig_size;
break;
default:
return -EOPNOTSUPP;
ret = -EOPNOTSUPP;
goto out_up_key_sem;
}
params->in_len = uparams.in_len;
return 0;
switch (op) {
case KEYCTL_PKEY_VERIFY:
if (!params->key->type->asym_verify_signature)
ret = -EOPNOTSUPP;
break;
case KEYCTL_PKEY_ENCRYPT:
case KEYCTL_PKEY_DECRYPT:
case KEYCTL_PKEY_SIGN:
if (!params->key->type->asym_eds_op)
ret = -EOPNOTSUPP;
break;
}
out_up_key_sem:
up_read(&params->key->sem);
return ret;
}
/*
@@ -185,7 +217,16 @@ long keyctl_pkey_query(key_serial_t id,
if (ret < 0)
goto error;
down_read(&params.key->sem);
if (!params.key->type->asym_query) {
ret = -EOPNOTSUPP;
goto out_up_key_sem;
}
ret = params.key->type->asym_query(&params, &res);
out_up_key_sem:
up_read(&params.key->sem);
if (ret < 0)
goto error;
@@ -230,10 +271,6 @@ long keyctl_pkey_e_d_s(int op,
if (ret < 0)
goto error_params;
ret = -EOPNOTSUPP;
if (!params.key->type->asym_eds_op)
goto error_params;
switch (op) {
case KEYCTL_PKEY_ENCRYPT:
params.op = kernel_pkey_encrypt;
@@ -260,7 +297,16 @@ long keyctl_pkey_e_d_s(int op,
if (!out)
goto error_in;
down_read(&params.key->sem);
if (!params.key->type->asym_eds_op) {
ret = -EOPNOTSUPP;
goto out_up_key_sem;
}
ret = params.key->type->asym_eds_op(&params, in, out);
out_up_key_sem:
up_read(&params.key->sem);
if (ret < 0)
goto error_out;
@@ -305,31 +351,37 @@ long keyctl_pkey_verify(const struct keyctl_pkey_params __user *_params,
ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
&params);
if (ret < 0)
goto error_params;
ret = -EOPNOTSUPP;
if (!params.key->type->asym_verify_signature)
goto error_params;
goto out_free_params;
in = memdup_user(_in, params.in_len);
if (IS_ERR(in)) {
ret = PTR_ERR(in);
goto error_params;
goto out_free_params;
}
in2 = memdup_user(_in2, params.in2_len);
if (IS_ERR(in2)) {
ret = PTR_ERR(in2);
goto error_in;
goto out_free_in;
}
params.op = kernel_pkey_verify;
down_read(&params.key->sem);
if (!params.key->type->asym_verify_signature) {
ret = -EOPNOTSUPP;
goto out_up_key_sem;
}
ret = params.key->type->asym_verify_signature(&params, in, in2);
out_up_key_sem:
up_read(&params.key->sem);
kfree(in2);
error_in:
out_free_in:
kfree(in);
error_params:
out_free_params:
keyctl_pkey_params_free(&params);
return ret;
}
+6 -6
View File
@@ -598,6 +598,12 @@ static int keyring_search_iterator(const void *object, void *iterator_data)
return 0;
}
/* A key the caller did not ask for must not decide the result. */
if (!ctx->match_data.cmp(key, &ctx->match_data)) {
kleave(" = 0 [!match]");
return 0;
}
/* skip invalidated, revoked and expired keys */
if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
time64_t expiry = READ_ONCE(key->expiry);
@@ -617,12 +623,6 @@ static int keyring_search_iterator(const void *object, void *iterator_data)
}
}
/* keys that don't match */
if (!ctx->match_data.cmp(key, &ctx->match_data)) {
kleave(" = 0 [!match]");
return 0;
}
/* key must have search permissions */
if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
key_task_permission(make_key_ref(key, ctx->possessed),