Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2670704rdb; Wed, 4 Oct 2023 08:07:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLCZqszk4muf1nLuWYlIH40HfwIbxSaUsiTt1zRHIsRuhswIxvY07uHBfF36xE1syw8oZy X-Received: by 2002:a05:6a20:1593:b0:155:5c28:ea74 with SMTP id h19-20020a056a20159300b001555c28ea74mr3040759pzj.12.1696432056733; Wed, 04 Oct 2023 08:07:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696432056; cv=none; d=google.com; s=arc-20160816; b=JnDwIQPDP1+HXM4v36VLhwCkttien4hr+VofnKzV2gQMYTiVotY4zGX+bhkxRttH7u l7Qs3nwEdgQwTZlleuWzeeZeX/QlUUvgQiWQ8xw9bzVNoyyASUS4oiYPhxh9p4LBkivI gbG6pdNNV5qgy9izyxVNgBT22XYHfD1UDuWp8kbReImlnNrRhkQFT+QuFTP95vT2ta4I H3TcymCiG9qk6C9fjSRNzLZnVntTn59QAuIiNh9/Wrjbf7dUO5cQHGTxuRANB7KQjqfM n9kYpYb9T59ziRkaWoSGKhra0uAGROoRUDYxKvfgLrk9ak2MJyNVgD85GknC99bp+Uw0 AE+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=+eVgljSrAaP4g9db6ArVQwSMJVgICRyWVUAk78dKeWs=; fh=h83YpKJuUa8fa+MA3d5Rb/da2BpzMfqTwYCJm07lxUI=; b=dZajJYV2X+yn19D7hRXtdgQdD+WfaR1tqkH7Fke51mLwsXzTQBWawzOZ/Ys3VP28xj fK69HRD6sqF+UDUJJESxbIq5VJiHMB/RoyRXbT3pY5xe4ygHTO2l4fn4P+Nx6Ten/s3B DZdSnVvLvddgQtW4BJmDE3B/fV9iond0ZFeb7Rp+DOv4rzoZPQ4itip+Ve7yn+ogBXsI rQSSFfWgRm9tQoGvDxSeyrlYWQNnYSEqTWEBN8CzDeWu+i0vOOjza4i/3cFKkiq6Wsx3 7+RA54ALmEwAAAwSN22vGAAa8yGANaP7yMRfCl8RGeNWv8KhA7Thh4aXM4vydCpilyVB zz7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=S7QcEe+n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id fo13-20020a056a00600d00b0068bc0b772d8si3739704pfb.194.2023.10.04.08.07.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 08:07:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=S7QcEe+n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C0016807747F; Wed, 4 Oct 2023 08:07:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233659AbjJDPHJ (ORCPT + 99 others); Wed, 4 Oct 2023 11:07:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232995AbjJDPHH (ORCPT ); Wed, 4 Oct 2023 11:07:07 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA91AB for ; Wed, 4 Oct 2023 08:07:04 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a21c283542so32575027b3.3 for ; Wed, 04 Oct 2023 08:07:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696432023; x=1697036823; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+eVgljSrAaP4g9db6ArVQwSMJVgICRyWVUAk78dKeWs=; b=S7QcEe+nGvfXl3YC5lbQ6Nqx7TNFcssVdM/Pk2rh8feEgGakzccv0v6F04TbOEobFB +z05k439al5OFrHa+wALunwfqy7uEjVw3Z54+o81NB2dlEXVm/A71cfqgFOoRtxuWk9M x8OVvENTVosc00bgHjGu+XLpz+oXiX6r7+b3u681drD0ySNQc3o58LM9V1mQ3pfXwcb0 eeVi/aX5VrbGSDdBJNiKvm5RpGa2Uy9+UTyagw1uAdfzlDMEpE2G08/gSaJCrigSO0Os OR5MNW22XmwuwtvjvRP9RL7NDN2M+WvEHfpRMdRQRZPPpjZUdO6lkvob1ZHHPjIn5cUK BRQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696432023; x=1697036823; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+eVgljSrAaP4g9db6ArVQwSMJVgICRyWVUAk78dKeWs=; b=Yv+lbCXWa4xKuEeN2FBn+bO23Hp2WSpYCC8xURxpojAHa08ohFncjy7C8x594U1zjP 9VmqovcBeV6nkYfWaWpDVVjQB8Lxg3SxOsYKmAPA/ArqADfRTG2cLS/A9MLk/e9WQwxD iFbeP/O8jrFnxJcTYrMae2h5cOZ0B5ZwNLiYQkHkt5EVpARaoqmdI58Dk9Kwguq3Ywni 0VZxW7MKWZQImmyponK5pxIPwjUlCcNG0AYDf+oTFX09/BkGE3xuUSD/yvherf5sXYmM f9kdKDIrkO4j8efCBwN+c9qraJ5pjFDA+XEM2RG1mTZPrPafBLOVFAkSENitrMRATXp8 G++w== X-Gm-Message-State: AOJu0YwHUcOsG8VPm2qfUgNk31H+9cGG4ndy0TARAjfbFEsX1gf4bhj2 E44vsO4KT0HQ14zqzUj9ZCDeR8awJYo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:b61a:0:b0:59b:e86f:ed2d with SMTP id u26-20020a81b61a000000b0059be86fed2dmr48698ywh.5.1696432023672; Wed, 04 Oct 2023 08:07:03 -0700 (PDT) Date: Wed, 4 Oct 2023 08:07:02 -0700 In-Reply-To: <20231004133827.107-1-julian.stecklina@cyberus-technology.de> Mime-Version: 1.0 References: <20231004133827.107-1-julian.stecklina@cyberus-technology.de> Message-ID: Subject: Re: [PATCH 1/2] KVM: x86: Fix partially uninitialized integer in emulate_pop From: Sean Christopherson To: Julian Stecklina Cc: Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 04 Oct 2023 08:07:31 -0700 (PDT) On Wed, Oct 04, 2023, Julian Stecklina wrote: > Most code gives a pointer to an uninitialized unsigned long as dest in > emulate_pop. len is usually the word width of the guest. > > If the guest runs in 16-bit or 32-bit modes, len will not cover the > whole unsigned long and we end up with uninitialized data in dest. > > Looking through the callers of this function, the issue seems > harmless, but given that none of this is performance critical, there > should be no issue with just always initializing the whole value. > > Fix this by explicitly requiring a unsigned long pointer and > initializing it with zero in all cases. NAK, this will break em_leave() as it will zero RBP regardless of how many bytes are actually supposed to be written. Specifically, KVM would incorrectly clobber RBP[31:16] if LEAVE is executed with a 16-bit stack. I generally like defense-in-depth approaches, but zeroing data that the caller did not ask to be written is not a net positive. > Signed-off-by: Julian Stecklina > --- > arch/x86/kvm/emulate.c | 14 ++++++++++---- > 1 file changed, 10 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > index 2673cd5c46cb..fc4a365a309f 100644 > --- a/arch/x86/kvm/emulate.c > +++ b/arch/x86/kvm/emulate.c > @@ -1838,18 +1838,24 @@ static int em_push(struct x86_emulate_ctxt *ctxt) > } > > static int emulate_pop(struct x86_emulate_ctxt *ctxt, > - void *dest, int len) > + unsigned long *dest, u8 op_bytes) > { > int rc; > struct segmented_address addr; > > + /* > + * segmented_read below will only partially initialize dest when > + * we are not in 64-bit mode. > + */ > + *dest = 0; > + > addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); > addr.seg = VCPU_SREG_SS; > - rc = segmented_read(ctxt, addr, dest, len); > + rc = segmented_read(ctxt, addr, dest, op_bytes); > if (rc != X86EMUL_CONTINUE) > return rc; > > - rsp_increment(ctxt, len); > + rsp_increment(ctxt, op_bytes); > return rc; > } > > @@ -1999,7 +2005,7 @@ static int em_popa(struct x86_emulate_ctxt *ctxt) > { > int rc = X86EMUL_CONTINUE; > int reg = VCPU_REGS_RDI; > - u32 val; > + unsigned long val; > > while (reg >= VCPU_REGS_RAX) { > if (reg == VCPU_REGS_RSP) { > -- > 2.40.1 >