From nobody Fri Dec 19 18:42:01 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEAF4FA3741 for ; Mon, 24 Oct 2022 14:05:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236903AbiJXOED (ORCPT ); Mon, 24 Oct 2022 10:04:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232974AbiJXOCb (ORCPT ); Mon, 24 Oct 2022 10:02:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E2477FF89; Mon, 24 Oct 2022 05:48:14 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B5AA161350; Mon, 24 Oct 2022 12:41:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9D58C4FF11; Mon, 24 Oct 2022 12:41:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615264; bh=qW/y+SpQOzhXIh0/KfoZtJFDvfzj91U1xVxwuMJXj78=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nTyDgeTO3HlSV29rufFdKdp3ozHbDCMOMpToaWzJH3qDj9T/wb7jnRN1X6BmZ2C51 2HZhRBSRdX8x00ezxDcl/lGOqF9ceDmIwR9xZLz7wAW6my8gzFP0e3ziAVoBcHyFQ4 m53Xmg2WbuvMcqaz9FHM2EmrPebDMCGezEn9CgLg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Micay , Kees Cook , Borislav Petkov , Sasha Levin Subject: [PATCH 5.15 184/530] x86/microcode/AMD: Track patch allocation size explicitly Date: Mon, 24 Oct 2022 13:28:48 +0200 Message-Id: <20221024113053.361326503@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kees Cook [ Upstream commit 712f210a457d9c32414df246a72781550bc23ef6 ] In preparation for reducing the use of ksize(), record the actual allocation size for later memcpy(). This avoids copying extra (uninitialized!) bytes into the patch buffer when the requested allocation size isn't exactly the size of a kmalloc bucket. Additionally, fix potential future issues where runtime bounds checking will notice that the buffer was allocated to a smaller value than returned by ksize(). Fixes: 757885e94a22 ("x86, microcode, amd: Early microcode patch loading su= pport for AMD") Suggested-by: Daniel Micay Signed-off-by: Kees Cook Signed-off-by: Borislav Petkov Link: https://lore.kernel.org/lkml/CA+DvKQ+bp7Y7gmaVhacjv9uF6Ar-o4tet872h4Q= 8RPYPJjcJQA@mail.gmail.com/ Signed-off-by: Sasha Levin --- arch/x86/include/asm/microcode.h | 1 + arch/x86/kernel/cpu/microcode/amd.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microc= ode.h index fcbfe94903bb..d130d21f4862 100644 --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -9,6 +9,7 @@ struct ucode_patch { struct list_head plist; void *data; /* Intel uses only this one */ + unsigned int size; u32 patch_id; u16 equiv_cpu; }; diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/micr= ocode/amd.c index 3d4a48336084..5a16844b99d3 100644 --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -782,6 +782,7 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsi= gned int leftover, kfree(patch); return -EINVAL; } + patch->size =3D *patch_size; =20 mc_hdr =3D (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); proc_id =3D mc_hdr->processor_rev_id; @@ -863,7 +864,7 @@ load_microcode_amd(bool save, u8 family, const u8 *data= , size_t size) return ret; =20 memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); - memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZ= E)); + memcpy(amd_ucode_patch, p->data, min_t(u32, p->size, PATCH_MAX_SIZE)); =20 return ret; } --=20 2.35.1