FAILED: patch "[PATCH] KEYS: Protect pkey method checks against type removal" failed to apply to 6.18-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x bab48a1044475f62d6e910c6d7267ce4bae847f9
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112436-utility-parasitic-7f63@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From bab48a1044475f62d6e910c6d7267ce4bae847f9 Mon Sep 17 00:00:00 2001
From: Yuansheng Yang <[email protected]>
Date: Tue, 10 Nov 2026 11:02:52 +0800
Subject: [PATCH] KEYS: Protect pkey method checks against type removal
keyctl_pkey_query(), keyctl_pkey_e_d_s() and keyctl_pkey_verify() check that key->type has the ->asym_*() method they need and call it later, without holding key->sem. A reference keeps the key alive, but does not pin the module that provides its type.
When that module is unloaded, unregister_key_type() waits for the key GC to process every remaining key of the type. For each such key, the GC takes key->sem for writing, switches key->type to key_type_dead, whose ->asym_*() methods are NULL, and destroys the payload. If that happens between the check and the call, the keyctl calls a NULL method; if it happens while the method runs, the method returns into freed module text. In-tree, turris-signing-key provides ->asym_query() and ->asym_eds_op() and can be built as a module.
Take key->sem for reading around each method call, and check the method again under it, right before the call. While the lock is held the GC cannot switch the key to key_type_dead, so unregister_key_type() and the module unload cannot complete until the method returns. If the type is already gone, the check finds key_type_dead and the keyctl fails with -EOPNOTSUPP, as it does for any key type without the method.
memdup_user() and copy_to_user() stay outside key->sem, as in keyctl_read_key(), so no page fault is taken with key->sem held. The in-tree ->asym_*() methods do not take key->sem themselves. Nothing else in the kernel calls these methods, so these four call sites are the full coverage:
- keyctl_pkey_query(): ->asym_query() for KEYCTL_PKEY_QUERY
- keyctl_pkey_params_get_2(): ->asym_query(), and the op-specific check for KEYCTL_PKEY_ENCRYPT, _DECRYPT, _SIGN and _VERIFY
- keyctl_pkey_e_d_s(): ->asym_eds_op() for _ENCRYPT, _DECRYPT and _SIGN
- keyctl_pkey_verify(): ->asym_verify_signature() for _VERIFY
This problem was recently fixed for keyctl_read_key() by 925c84e3cc21 ("keys: Protect the read method check against type removal") and for key_update() by aef3a8a4f988 ("KEYS: Protect the update method check against type removal").
security/keys/keyctl_pkey.c +76 -24
@@ -129,45 +129,77 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_parif (ret <0 )return ret; +down_read (¶ms->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 (¶ms->key->sem); +return ret; }/*
@@ -185,7 +217,16 @@ long keyctl_pkey_query(key_serial_t id,if (ret <0 ) goto error; +down_read (¶ms.key->sem); +if (!params.key->type->asym_query) { + ret = -EOPNOTSUPP ; + goto out_up_key_sem; + } + ret = params.key->type->asym_query (¶ms, &res); + +out_up_key_sem: +up_read (¶ms.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 (¶ms.key->sem); +if (!params.key->type->asym_eds_op) { + ret = -EOPNOTSUPP ; + goto out_up_key_sem; + } + ret = params.key->type->asym_eds_op (¶ms, in, out); + +out_up_key_sem: +up_read (¶ms.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 , ¶ms);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 (¶ms.key->sem); +if (!params.key->type->asym_verify_signature) { + ret = -EOPNOTSUPP ; + goto out_up_key_sem; + } + ret = params.key->type->asym_verify_signature (¶ms, in, in2); +out_up_key_sem: +up_read (¶ms.key->sem);kfree (in2); -error_in: + +out_free_in:kfree (in); -error_params: + +out_free_params:keyctl_pkey_params_free (¶ms);return ret; }
Sasha Levin
maintainerPATCH 1/2 keys: keyctl_pkey: replace BUG with return -EOPNOTSUPP
From: Mohammed EL Kadiri <[email protected]>
[ Upstream commit 1b9524250996b1f2f49833a1b2ae21c34e486f85 ]
Replace two BUG() calls in keyctl_pkey_params_get_2() and keyctl_pkey_e_d_s() default cases with -EOPNOTSUPP, matching the error style already used in these functions.
Stable-dep-of: bab48a104447 ("KEYS: Protect pkey method checks against type removal")
--- security/keys/keyctl_pkey.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
security/keys/keyctl_pkey.c +3 -2
@@ -163,7 +163,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par params->out_len = info.max_sig_size;break ;default : -BUG (); +return -EOPNOTSUPP ; } params->in_len = uparams.in_len;
@@ -245,7 +245,8 @@ long keyctl_pkey_e_d_s(int op, params.op = kernel_pkey_sign;break ;default : -BUG (); + ret = -EOPNOTSUPP ; + goto error_params; } in =memdup_user (_in, params.in_len);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 KEYS: Protect pkey method checks against type removal
From: Yuansheng Yang <[email protected]>
[ Upstream commit bab48a1044475f62d6e910c6d7267ce4bae847f9 ]
keyctl_pkey_query(), keyctl_pkey_e_d_s() and keyctl_pkey_verify() check that key->type has the ->asym_*() method they need and call it later, without holding key->sem. A reference keeps the key alive, but does not pin the module that provides its type.
When that module is unloaded, unregister_key_type() waits for the key GC to process every remaining key of the type. For each such key, the GC takes key->sem for writing, switches key->type to key_type_dead, whose ->asym_*() methods are NULL, and destroys the payload. If that happens between the check and the call, the keyctl calls a NULL method; if it happens while the method runs, the method returns into freed module text. In-tree, turris-signing-key provides ->asym_query() and ->asym_eds_op() and can be built as a module.
Take key->sem for reading around each method call, and check the method again under it, right before the call. While the lock is held the GC cannot switch the key to key_type_dead, so unregister_key_type() and the module unload cannot complete until the method returns. If the type is already gone, the check finds key_type_dead and the keyctl fails with -EOPNOTSUPP, as it does for any key type without the method.
memdup_user() and copy_to_user() stay outside key->sem, as in keyctl_read_key(), so no page fault is taken with key->sem held. The in-tree ->asym_*() methods do not take key->sem themselves. Nothing else in the kernel calls these methods, so these four call sites are the full coverage:
- keyctl_pkey_query(): ->asym_query() for KEYCTL_PKEY_QUERY
- keyctl_pkey_params_get_2(): ->asym_query(), and the op-specific check for KEYCTL_PKEY_ENCRYPT, _DECRYPT, _SIGN and _VERIFY
- keyctl_pkey_e_d_s(): ->asym_eds_op() for _ENCRYPT, _DECRYPT and _SIGN
- keyctl_pkey_verify(): ->asym_verify_signature() for _VERIFY
This problem was recently fixed for keyctl_read_key() by 925c84e3cc21 ("keys: Protect the read method check against type removal") and for key_update() by aef3a8a4f988 ("KEYS: Protect the update method check against type removal").
--- security/keys/keyctl_pkey.c | 100 +++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 24 deletions(-)
security/keys/keyctl_pkey.c +76 -24
@@ -129,45 +129,77 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_parif (ret <0 )return ret; +down_read (¶ms->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 (¶ms->key->sem); +return ret; }/*
@@ -185,7 +217,16 @@ long keyctl_pkey_query(key_serial_t id,if (ret <0 ) goto error; +down_read (¶ms.key->sem); +if (!params.key->type->asym_query) { + ret = -EOPNOTSUPP ; + goto out_up_key_sem; + } + ret = params.key->type->asym_query (¶ms, &res); + +out_up_key_sem: +up_read (¶ms.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 (¶ms.key->sem); +if (!params.key->type->asym_eds_op) { + ret = -EOPNOTSUPP ; + goto out_up_key_sem; + } + ret = params.key->type->asym_eds_op (¶ms, in, out); + +out_up_key_sem: +up_read (¶ms.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 , ¶ms);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 (¶ms.key->sem); +if (!params.key->type->asym_verify_signature) { + ret = -EOPNOTSUPP ; + goto out_up_key_sem; + } + ret = params.key->type->asym_verify_signature (¶ms, in, in2); +out_up_key_sem: +up_read (¶ms.key->sem);kfree (in2); -error_in: + +out_free_in:kfree (in); -error_params: + +out_free_params:keyctl_pkey_params_free (¶ms);return ret; }
Signature · 1 line
2.53.0