Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp156922pxu; Wed, 2 Dec 2020 18:07:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3XVcLGFUyArb7atRkMHC+37AsMRTZlemyjz/FDTGnumcnNeU2i3qK9KdwHybAP+klN+9H X-Received: by 2002:a50:e18c:: with SMTP id k12mr879138edl.58.1606961264898; Wed, 02 Dec 2020 18:07:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606961264; cv=none; d=google.com; s=arc-20160816; b=0K9XbOYZdHelrInBKTQlSFbJvyzCf4tmF00ih4Rgq/SxPlH8ad/6rtigMdnC2IWSpi 3Fkx1YzEIXJkAmo+uLng5aVgI7xREqZNzQ0v1icYQ1F5hDzKlWQtLOEZXaMMOv2Ta7BV zmF6td4EjljPQWWhET4EtGE873FmL+agTJe2/+6KKx8rQRmMsk8z1ICP2zUUWhFwSdGH NWt8zvxJynp95Vu/ZCJzi5+H7H3+iW84sfLHFrtGn1UHcAgwvZLAgqUhysn/ZmxBvXra J14WyaMnYJx4FTfgHTwfrNz5sP2C5aWqSFrLxoXh0AIBnzFeK2zQkcnNF9mIcOBlt9iK bH9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:dkim-signature :date; bh=NDWp43bOXzvl7JrTI1SpJBuHlbUYYI1YoWqpNYjZvvk=; b=OfyRDrk5gYVMSQD08suzPwZn56dPw69kCTG0cKXPXoTQb4IxdX68gksx4q7W41nd+J 53c2/cdZ8CRpEfY+UyfdKBghk9s3hPuAlDp4vTJiFCJ61laPLqsORQD1lfpCdQdhcDfs RlHrfdJ6xT/MCHHppqgfS9ja0Eai1Bb6DXlYKPBFMscitsnpRCoT1YFtzjdc19yoHow0 oF1PBb5U95UAFahrAJaEVsEsemdlKIKsI65q+4g68MjVV7/mLvInPNtXkJ+4QfwvK09g 7gtgoTGAyRECgAkp4J36bEmJyrrzn+crYC1aMecFcAu5wgU68KmXdmWJbpw2WL2w6mQK puOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="pPY/59Ns"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g10si72801edv.139.2020.12.02.18.07.21; Wed, 02 Dec 2020 18:07:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="pPY/59Ns"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727258AbgLCCDt (ORCPT + 99 others); Wed, 2 Dec 2020 21:03:49 -0500 Received: from mail.kernel.org ([198.145.29.99]:44926 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725859AbgLCCDt (ORCPT ); Wed, 2 Dec 2020 21:03:49 -0500 Date: Thu, 3 Dec 2020 11:03:02 +0900 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1606960988; bh=uJVG156LJ2Xk3EybB328xWcZ6wvKtQV2KDv5WfLuRLc=; h=From:To:Cc:Subject:In-Reply-To:References:From; b=pPY/59Ns3ZPEFTSKguFAk5uuU4PuvB94Qmh3H51svOReB4jFBR/wF7W+dtFc9gVe1 HM7znNlclMESLwzZNtjre0IfyaoZODgEsVSgVbzwq8lvdyJQrThMsc5mxlkqDIt8bW Pj0rwQDH3Hg4V5dk2/UZ7xHDlSs9ZVFBAPhVE2XUm1UfsO68x2rrlXuoP+qa/mr/J/ NxJxYKqgPe3FV6NMdmcd9k5ciF2dsJ8db5xVzURJ/JlHQw5DIEViwBlckJcGQc02sm 4IEAUB/khKXXLnb/l1gkCfeoZyie+8wYqdPuHcrf0ubtlDU8iPcIC8GqDtME78Jc8u 2gbcRLkD+5AEQ== From: Masami Hiramatsu To: Kees Cook Cc: Tom Lendacky , Masami Hiramatsu , x86@kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Joerg Roedel , "Gustavo A . R . Silva" , Jann Horn , Srikar Dronamraju , Ricardo Neri , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/3] x86/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes Message-Id: <20201203110302.79bb318c885b4673d2b0be19@kernel.org> In-Reply-To: <202012021104.0C38FB7FD@keescook> References: <160689905099.3084105.7880450206184269465.stgit@devnote2> <160689906460.3084105.3134729514028168934.stgit@devnote2> <54417a56-241b-14f9-2540-11b23e40e2b2@amd.com> <202012021104.0C38FB7FD@keescook> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2 Dec 2020 11:07:26 -0800 Kees Cook wrote: > On Wed, Dec 02, 2020 at 09:31:57AM -0600, Tom Lendacky wrote: > > On 12/2/20 2:51 AM, Masami Hiramatsu wrote: > > > Since the insn.prefixes.nbytes can be bigger than the size of > > > insn.prefixes.bytes[] when a same prefix is repeated, we have to > > > check whether the insn.prefixes.bytes[i] != 0 and i < 4 instead > > > of insn.prefixes.nbytes. > > > > > > Fixes: 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions") > > > Reported-by: Kees Cook > > > Signed-off-by: Masami Hiramatsu > > > --- > > > arch/x86/boot/compressed/sev-es.c | 2 +- > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > diff --git a/arch/x86/boot/compressed/sev-es.c b/arch/x86/boot/compressed/sev-es.c > > > index 954cb2702e23..6a7a3027c9ac 100644 > > > --- a/arch/x86/boot/compressed/sev-es.c > > > +++ b/arch/x86/boot/compressed/sev-es.c > > > @@ -36,7 +36,7 @@ static bool insn_has_rep_prefix(struct insn *insn) > > > insn_get_prefixes(insn); > > > - for (i = 0; i < insn->prefixes.nbytes; i++) { > > > + for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) { > > You must test "i" before bytes[i] or you still do the out-of-bounds-read. Oops, thanks. > > > > > Wouldn't it be better to create a #define for the size rather than hard > > coding 4 in the various files? That would protect everything should the > > bytes array size ever change in the future. > > Agreed, and perhaps instead of repeating the idiom in the for loop, add > a helper like: > > #define insn_prefix_valid(prefixes, i) (i >=0 && i < 4 && prefixes->bytes[i]) > > to be used like: > > for (i = 0; insn_prefix_valid(&insn->prefixes, i); i++) { Hm, for all of these usage, they are looping on the prefixes, so for_each_insn_prefix(insn, idx, prefix) { ... } will be simpler. Thank you, > > > > > Thanks, > > Tom > > > > > insn_byte_t p = insn->prefixes.bytes[i]; > > > if (p == 0xf2 || p == 0xf3) > > > > > -- > Kees Cook -- Masami Hiramatsu