From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793907399; b=33OYohS6Zr6WEOmNhqs+JcTwdo85UymX6ImDd/26cpuhEQ1iUoxOwW8guze5qjjjH9UD LqWeawjBuOxnMnZ2EHCnwo5kCiBb2epdRFJ3yoE0Jf7Zblyme9UyWcTrfld3Z7Q44enlk neIgzYz7p58TudDFH65mrJfSa72x8kgdwBH/ArPRNjPsk8mT1/DxmXmNDRFrobVtWJ0ji DSMpby4wDJjERbOQQrP68ksZWL3Cow8xL0/KvuKJ7IoQt5c1vN+R9+qxxL2RUwjvNjd5i zddMyBU033jq37oNUMdUphpR80PZfGwPzqku/vAHAwsWVuEjLkhtDKEHK7bsvOqudDg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793907399; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=Varby6vTTgGosmDxdkbVVCPGhFaIy/1zgNtje3gNyzM=; b=vO16TkbQU/LGV50xGqLs+g6/ILwhGoLrnY3PKYIfpjfwKaozRUek0/2QA6xaZGULlxZp GfuJQy8Onkg7DtZe2fsU/hUds7qM36Ziva5hjS63eKNUwhBBTiJksvUghXjG4XTlKjBB3 9IXn5Ah/wgbH1woRmQqEe91t80Xwuw+n/cFWF5qtuYtAEGZnZ8ogqDKyNpyCdBC0BVXD9 htnP7yYlUjCt9V/GzJr+xkAj9GToVHlPzIUJLO0TAkt3aEWfgUW5xxFOX/A/Tw8bm9C7n G8MIyf6KLpdDhmsU7WUaCIUiBm1sIdZl/pxDp4+g4K8vksr2MbHUgiBB9Jue9iKygqw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="MrvVq/pA"; arc=none smtp.remote-ip=209.85.210.180 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36C16606A6F6 for ; Thu, 5 Nov 2026 19:36:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 36C16606A6F6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=MrvVq/pA Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-8b271e573adso445833b3a.2 for ; Thu, 05 Nov 2026 11:36:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793907397; x=1794512197; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Varby6vTTgGosmDxdkbVVCPGhFaIy/1zgNtje3gNyzM=; b=MrvVq/pAxeEpkWXJvxeD7gcR/Qrh87bKPPTXZNEO0tG57lXCu9eFrtFxefXkdMbQ+t +FiL/k3Ptej86tBsPDeoLoizkVTIBwROn8QWbNusVRS8R/bCWm9dd6AGcGB7ub/WtPGR UWy3DCVzTTXRt+1WOzTVq60M6NKtxfs+3JKFNAt/gZKsEsjG8TdLG+xv5InLaxogx2T5 OmYwab8IEf/IYxJ3jC3/1u/celUFJj8e96sXYWLdq5SO7gk63YV8hC7JdeKci8xMXjEq 4nI7GwwwC1aOwO/Mk+UIING/PbLJezKjiIG/tMhS17HnfXM8TKGnyuXPcUMKDSksixfU SLNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793907397; x=1794512197; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Varby6vTTgGosmDxdkbVVCPGhFaIy/1zgNtje3gNyzM=; b=z2ysv8JZ12sZeM2HLB42R6zVcUEhda2VAYJzIpf1VvaedKq8x5EqRMLmHC2DfY23Y3 xxGAKgh/DuoYpp7orScJ9Td59r1IjNitgPMPOrWqFwgZSUdGnivoZzsvfL3sHpU4Crrg SMdF5WAqSV3WAV2snj2anxIyQosYG0VAd+LfiNydCrKq7V8GPxBo+6hnld5okgaZDSXs HvyvaXVSV++NURg92CyBMJhDl4DnYlJcqW5C74/N49hRHl+MxFvgW4UkLGhYQYuZqFWF fptCR/AG0mKEEWuQJyGKWYe3AXTG+Pbab8OXHYR19fVZXwPf0Fqjz5JVbZorpHegI6yB SjDQ== X-Forwarded-Encrypted: i=1; AKwUvBzQb8jrytB/L1DbcLw//XSgl/vVIhc+LHteIgGmLOFNujc8peOjxb7ifLA6HXQrcy/FXJJQwZshUrTTeLU=@vger.kernel.org X-Gm-Message-State: APuU4Z+LEKTrDmCPKqnjyu2sHyLy5xEGloBEMn6IXn8k7s9agL72v7wm DKKE1aPxfph179ntUdZxhXgk4lGgPh/4l4vJ0faMESNy5D/4TOburZXK X-Gm-Gg: AYBFou3qF9YxbiLGdPC723sH4okl+KnC7gVj0BqJUSUM7I+5MvwjvJ/OoJidbMiJ7xz HafiiGbuRoJ3ir1kxTcUMiOxEMgxta9e4itHMi0quyFWCtsak28wwnmQNX+MTKxdrP3/tLi32IR kBlrPX2MI8arwc1ckRAA91oBDv7gBclj5bfIL7ZMdfAbOaVcK4UGtOAo5tGzE10nYSA5XUMNBOm 8hSkXlH0+LOkOet4JrAZyGjKCdgxWTz6vMuCUn+jmjatJKsHwE3yG6mAqgsf18MZ3lU5uuEzZGE hDgHh/ZEyJ2Yc/wT0v0aAAT70TvTP8vc53dYyIjp/Unoy2ZuPJeJmWayIukB1v0uRdzDbv6y1he Nj2s7xZ5YcgVzR9mUnjW4ejPNJhj+7sGLXNRMtDd7Idxa25vSS1IuNobsqzq6NyadJYChNKHEbM Bj2UjmLCHN/QsxYcV6LxD4/nht9sirPhs9qGhM9jzdHY6EuDxv0H+8MovXUkY7eUfRZcVxTJRH6 GCxOR8BiPpNMQ8ZvQShnLrXR+xdd1y4YTmCl8bIukKtZpiU/RoWbSjNOmgOTErORlJyJoOXPGqw E64Gh6UeODRj4Muy0uPa0yJLAWRZBm8FOOCiYIs4yuxz0mT+ X-Received: by 2002:a17:90b:448f:b0:3ab:9c73:147 with SMTP id 98e67ed59e1d1-3b0db69ee38mr32308a91.14.1793907397392; Thu, 05 Nov 2026 11:36:37 -0800 (PST) Received: from ubuntu24.vpn.host.chaitin.net ([124.77.124.225]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0d7c42932sm499331a91.6.2026.11.05.11.36.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 11:36:37 -0800 (PST) From: Yuansheng Yang <0xbeac@gmail.com> To: Jarkko Sakkinen Cc: David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v4] KEYS: Protect pkey method checks against type removal Date: Fri, 6 Nov 2026 03:36:31 +0800 Message-ID: <20261105193632.34363-1-0xbeac@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit keyctl_pkey_params_get() looks up the key, and keyctl_pkey_query(), keyctl_pkey_e_d_s() and keyctl_pkey_verify() then dereference key->type->asym_* outside key->sem. A reference keeps the key alive, but does not pin the module providing its type. unregister_key_type() calls key_gc_keytype(), which retypes every remaining key of the type to key_type_dead under key->sem for writing. The pkey paths read key->type again after their checks, so a concurrent module unload can turn the checked callback into the NULL key_type_dead member and call through a NULL function pointer, e.g.: KEYCTL_PKEY_SIGN task Module unload and key GC ------------------------ ------------------------ keyctl_pkey_e_d_s() if (!key->type->asym_eds_op) unregister_key_type() GC: down_write(&key->sem) GC: retype to key_type_dead GC: up_write(&key->sem) free_module() in = memdup_user(...) key->type->asym_eds_op(...) -> NULL call In-tree, turris-signing-key provides such methods. Remove the key->type->asym_query check from keyctl_pkey_params_get(): it runs without key->sem, so key_gc_keytype() can retype the key right after it. Instead, make keyctl_pkey_params_get_2(), keyctl_pkey_query(), keyctl_pkey_e_d_s() and keyctl_pkey_verify() each take key->sem for reading, check the key->type->asym_* method they are about to call, call it and release key->sem. As key_gc_keytype() takes key->sem for writing before it retypes the key, key->type cannot change between the check and the call. Drop the asym_eds_op and asym_verify_signature checks that keyctl_pkey_e_d_s() and keyctl_pkey_verify() did before memdup_user() for the same reason; -EOPNOTSUPP is now returned from under key->sem. memdup_user(), kmalloc() and copy_to_user() stay outside key->sem, as in __keyctl_read_key(). 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"). Fixes: 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- Changes in v4: - Rewrite the commit message: describe what changes in keyctl_pkey_params_get(), keyctl_pkey_params_get_2() and the three keyctl handlers and why, and cite the read/update fixes by subject. - Rename the out_unlock labels to out_up_key_sem. - Use out_up_key_sem / out_free_in / out_free_params in keyctl_pkey_verify(); leave the other functions' existing labels alone. - No functional change relative to v3. v3: https://lore.kernel.org/all/9d47e202bd11857b3555e6b95694b3c3a4f2460b.1793852399.git.0xbeac@gmail.com/ v2: https://lore.kernel.org/all/20261104021414.50696-1-0xbeac@gmail.com/ security/keys/keyctl_pkey.c | 58 +++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/security/keys/keyctl_pkey.c b/security/keys/keyctl_pkey.c index 15b6bf6399..3e3253330c 100644 --- a/security/keys/keyctl_pkey.c +++ b/security/keys/keyctl_pkey.c @@ -100,9 +100,6 @@ static int keyctl_pkey_params_get(key_serial_t id, return PTR_ERR(key_ref); params->key = key_ref_to_ptr(key_ref); - if (!params->key->type->asym_query) - return -EOPNOTSUPP; - return 0; } @@ -129,7 +126,16 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par if (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); + +out_up_key_sem: + up_read(¶ms->key->sem); if (ret < 0) return ret; @@ -185,7 +191,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 +245,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 +271,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 +325,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; } -- 2.43.0