From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382974; b=ZARSVz2Fx5lLrxz4yi8xJpXvRipNTFfZSWUvH0026ZF73cpb9heDeMce7SRXsiHWUxM5 sY+a81awsxUpOp7P5qAjbqSvIfmWwgM4NeJ90/T+HsMoYgd56WhBzidmUREaOTT2249j0 wLyAHdNWDeuPWICh6xgCPMeIZWBUziSWzqKUPxWWjvVSYd8QXxkdIYRjSf+H7VrsyATGQ S8KMSKw7FlB5LR2+CcXNT9xpu+cmL/PFHBIxApUqLrLQtbF2MsZy+lxFLNg1fOqdO8Zid 2teZ8Z4ag1Secj1p56ixmrK+oqfIKXJFM2ARgvFyqa4bNjq84Vih6Al/jW+6RBXeEOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382974; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=fO6+kirK61/405Fl4EZW0iy612Rt+XBfrbKuhotrk6g=; b=QWArmvTdnloeOrWYWrws5zEhIT/unjB+V+Sdl0Ksz7qAdZJecVN+MEF1gkK9N74cmDWR wG0w5V7EuG2NmiD3H3kJEfhv+Q8u0YwZ+0S3wDMaxTa8xGm89gbHgJ6xEsP9QLzxO5Z8l hYHLygjlxI7l5TVdkKOHb2oXOEwXDcCdzxw8jgqJmgD48Dk12LuDtlupCZQT4GWwFdzK/ Hy4kxFuW1szBOFjKHVQhhNupCYiqw/sqNo3PYf2E6Neht+pNYOUnzlzMX0D4MMrOGGSU3 2pP9gU27153+Mrtd9XwfdOU0WNfx0Ltd/kBeymQhURKMJ/iBc3iT6pOwJDCSHEt2oBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=tTL+p60g; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 872C5606A32D; Fri, 4 Dec 2026 11:16:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=tTL+p60g Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00C2D1F000C2; Fri, 4 Dec 2026 11:16:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382974; bh=fO6+kirK61/405Fl4EZW0iy612Rt+XBfrbKuhotrk6g=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tTL+p60gd8M6Kt+P2stpI3/0DDjLz9KGHDJi8pR48HwaKbJ1aI8Af72fMCcGllNFO GuzU6sR6Gx92HMWgbca+BMVVLaFexbaBUrWagGQdnyzNgUNBYytTe7Xa+xcmq/vZ43 sS0YhMKzjgWeB50qRCbdGNn5d0Pe/noaLE3HPEoM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Guicai Ma , Jarkko Sakkinen Subject: [PATCH 6.18 632/703] KEYS: Fix userspace buffer overflow in keyctl_pkey_e_d_s() Date: Fri, 4 Dec 2026 19:08:51 +0900 Message-ID: <20261204100644.597861151@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Guicai Ma commit 484dc27f031229b374446cf038fa9c7a84d0f725 upstream. Commit cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") changed keyctl_pkey_params_get_2() to set params->out_len to the maximum output size of the operation instead of uparams.out_len, so that the kernel buffer allocated in keyctl_pkey_e_d_s() is large enough. Because uparams.out_len is no longer kept, keyctl_pkey_e_d_s() copies ret bytes to _out without checking whether the result fits in the caller's buffer. If a smaller out_len was passed, copy_to_user() writes past the end of _out instead of failing with -EOVERFLOW. uparams.out_len cannot simply be required to be at least the maximum output size in keyctl_pkey_params_get_2(), since KEYCTL_PKEY_DECRYPT with enc=pkcs1 can return a plaintext shorter than max_dec_size into a matching smaller buffer. Pass uparams.out_len back to keyctl_pkey_e_d_s() and return -EOVERFLOW before copy_to_user() if ret exceeds it. Cc: stable@vger.kernel.org # v7.2+ Fixes: cb481e59ea6c ("KEYS: fix overflow in keyctl_pkey_params_get_2()") Signed-off-by: Guicai Ma Reviewed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Greg Kroah-Hartman --- security/keys/keyctl_pkey.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/security/keys/keyctl_pkey.c +++ b/security/keys/keyctl_pkey.c @@ -113,7 +113,8 @@ static int keyctl_pkey_params_get(key_se static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_params, const char __user *_info, int op, - struct kernel_pkey_params *params) + struct kernel_pkey_params *params, + u32 *user_out_len) { struct keyctl_pkey_params uparams; struct kernel_pkey_query info; @@ -183,6 +184,8 @@ static int keyctl_pkey_params_get_2(cons } params->in_len = uparams.in_len; + if (user_out_len) + *user_out_len = uparams.out_len; switch (op) { case KEYCTL_PKEY_VERIFY: @@ -264,10 +267,12 @@ long keyctl_pkey_e_d_s(int op, void __user *_out) { struct kernel_pkey_params params; + u32 user_out_len; void *in, *out; long ret; - ret = keyctl_pkey_params_get_2(_params, _info, op, ¶ms); + ret = keyctl_pkey_params_get_2(_params, _info, op, ¶ms, + &user_out_len); if (ret < 0) goto error_params; @@ -310,6 +315,10 @@ out_up_key_sem: if (ret < 0) goto error_out; + if (ret > user_out_len) { + ret = -EOVERFLOW; + goto error_out; + } if (copy_to_user(_out, out, ret) != 0) ret = -EFAULT; @@ -349,7 +358,7 @@ long keyctl_pkey_verify(const struct key long ret; ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY, - ¶ms); + ¶ms, NULL); if (ret < 0) goto out_free_params;