Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp82501rdb; Thu, 5 Oct 2023 17:59:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFMUmtT4+PtO14kXOIicmrqCDcMzzBioGVq/deT1kcbRQgW6sKc3AQvUdrhQs2cUyKbSDJu X-Received: by 2002:aca:1115:0:b0:3a9:a334:907e with SMTP id 21-20020aca1115000000b003a9a334907emr6033194oir.16.1696553999370; Thu, 05 Oct 2023 17:59:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696553999; cv=none; d=google.com; s=arc-20160816; b=Pc1PDKbg80sSpAd+oy2mRts7AgRlGnLtXuErvF5UwoRcsl/Rzr1bdKEyjyAiEQ3oEF WCC+pnPRuv/O8ulP1SRtfxMERk6fftpRWrtQN6HMEY/uxGY6JNLX9vqYBBJ3YLNpFTFB 0ur9hQ8O6aRgSXlCDACn2xuhy7VAUCiNPiSp9gyKg2Wz1/e/TA1Hrl7vAJBUV1TNrUt7 vr8gXtpGb1VkpsYvS5rBxZINJbIF04YPjncnhd+Y/qD+9nRJZeV+2a9uWhlhGQ/9cGOp V2+ZX++LG4BJBoGbxs4Kq0UNz+1ePmP5ZojfoeMt6UuTcrHFGonS6M+gUYrpRoVF5bJG Il8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=Wngn7j6yKtSFTPMW7k+qGiUH7BBA15u51HDdOm1cg3E=; fh=rGwc+OXoDZTmyz7gdBIjFFgzGyB8T/XX2uGOYA/xwMg=; b=eY0w5WuzUy6RQT/bz72BGFbb5oHsFfeKCI/I89PvVg3F1gCaWLLH6iM/fblIzG24cu 1dq7aIVQMLHnCqhgEXskgcz5cCdc55GoEexVVDWssa5+qFeh4zcvsU4ej0ij+K0b4E7X gh9SJFw14/yG/8wXt0D2htCsgXtVFMzELGZBnvjZLetKpmNB2s0syDI4kT7LvW1wG3oH o6380ClYUvX6dA0fGM7sxQydUFW7jAlJMajKR+PF54Qw12LQlrt4CKsFJbpEvQ5P59ml qnQ1V9TFmj54BxR2bM0Wu3UW+/xWzvvnFUeLsMBmWufDFDMLys6mQCQ5VgNbHvZo6/s9 3x+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zmOccEuN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id i125-20020a639d83000000b0055fd1bfacafsi2540161pgd.755.2023.10.05.17.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 17:59:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zmOccEuN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 8248582DFD22; Thu, 5 Oct 2023 17:59:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229963AbjJFA7q (ORCPT + 99 others); Thu, 5 Oct 2023 20:59:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230027AbjJFA64 (ORCPT ); Thu, 5 Oct 2023 20:58:56 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6B901A3 for ; Thu, 5 Oct 2023 17:57:00 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59f7d4bbfc7so23146937b3.3 for ; Thu, 05 Oct 2023 17:57:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696553820; x=1697158620; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=Wngn7j6yKtSFTPMW7k+qGiUH7BBA15u51HDdOm1cg3E=; b=zmOccEuN7EtJyN+GduajTOivHJfdjEbg7zz/wq/JNF8rg2erEaRmf5A3R2+W7fKKXH jCp6THfIZRWnf6Qkrwe4SZD3UKVsnmKffdUDJ3Tfo7vux3mi5+4DwhPe13dEskmqEdj8 nfv7tjtnWhxNhniVLhEQ0TsP91fb/1I3lJdb/Qd9A1zP9HHgDFmgVTez0PSnWwlIBpeg xKkJtvyvIY4+qf1FETs8xcrXScMwMkrBy4pL+1HiHKv0DA5HMc144olJrLJE+q1kdvE1 vj4dWQv8rhc4V+hAqNB3KNVer2uWRcjFG6hkfZ2dz40y/8j212RpHUmPka9yqpjw2hpi DzZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696553820; x=1697158620; h=content-transfer-encoding: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=Wngn7j6yKtSFTPMW7k+qGiUH7BBA15u51HDdOm1cg3E=; b=dOJv7dqve1wvrybSoQ+dZpwjNO8w/K4YVjvBbYIqKBPSrnWZivDo6UbHBHq9WSveqW 6/NyVZmQP99gPFb9WZdQHayvafZbsJNL0VyW0SjWMFeGsIqJGmqWZPtxGU11SW+T8VNm PJRId5B3unlqP05268b0Lx5wgB+lGeTuVfVRnORDpdp07cwlSH/6bkDe9hoe4SmxEEnl eIIPzOzrijUBL7K8utB+9SPDEcWz7P6eRy1GmAtw7a2QjcDcgds6dZjcDttHj+iQHIbm Ta7TlC/bdAvLexabkB2ahEHQ9sCSQX8EhEKn0j5McGfVS3V3kHXMzY7Z3LXwlfSE+PJ6 GxAQ== X-Gm-Message-State: AOJu0YxahRqprpnid68ZAr8zMnTBHic5E+mAai/gZbSyJmse0yGI66y9 0p7uRbr26HkL57l/LTj0ePaI1GKYRhg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:bcd:0:b0:d7f:2cb6:7d8c with SMTP id 196-20020a250bcd000000b00d7f2cb67d8cmr95392ybl.13.1696553820052; Thu, 05 Oct 2023 17:57:00 -0700 (PDT) Date: Fri, 6 Oct 2023 00:56:58 +0000 In-Reply-To: <1b0252b29c19cc08c41e1b58b26fbcf1f3fb06e4.camel@cyberus-technology.de> Mime-Version: 1.0 References: <20231004133827.107-1-julian.stecklina@cyberus-technology.de> <1b0252b29c19cc08c41e1b58b26fbcf1f3fb06e4.camel@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: "x86@kernel.org" , "dave.hansen@linux.intel.com" , "hpa@zytor.com" , "mingo@redhat.com" , "tglx@linutronix.de" , "bp@alien8.de" , "kvm@vger.kernel.org" , "pbonzini@redhat.com" , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 howler.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 (howler.vger.email [0.0.0.0]); Thu, 05 Oct 2023 17:59:56 -0700 (PDT) On Thu, Oct 05, 2023, Julian Stecklina wrote: > On Wed, 2023-10-04 at 08:07 -0700, Sean Christopherson wrote: > > On Wed, Oct 04, 2023, Julian Stecklina wrote: > > > Most code gives a pointer to an uninitialized unsigned long as dest i= n > > > emulate_pop. len is usually the word width of the guest. > > >=20 > > > 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. > > >=20 > > > 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. > > >=20 > > > Fix this by explicitly requiring a unsigned long pointer and > > > initializing it with zero in all cases. > >=20 > > NAK, this will break em_leave() as it will zero RBP regardless of how m= any > > bytes > > are actually supposed to be written.=C2=A0 Specifically, KVM would inco= rrectly > > clobber > > RBP[31:16] if LEAVE is executed with a 16-bit stack. >=20 > Thanks, Sean! Great catch. I didn't see this. Is there already a test sui= te for > this? No, I'm just excessively paranoid when it comes to the emulator :-) > > I generally like defense-in-depth approaches, but zeroing data that the= caller > > did not ask to be written is not a net positive. >=20 > I'll rewrite the patch to just initialize variables where they are curren= tly > not. This should be a bit more conservative and have less risk of breakin= g > anything. In all honesty, I wouldn't bother. Trying to harden the emulator code for = things like this will be a never ending game of whack-a-mole. The operands, of wh= ich there are many, have multiple unions with fields of varying size, and all k= inds of subtle rules/logic for which field is used, how many bytes within a give= n field are valid, etc. It pains me a bit to say this, but I think we're best off leaving the emula= tor as-is, and relying on things like fancy compiler features, UBSAN, and fuzze= rs to detect any lurking bugs. struct operand { enum { OP_REG, OP_MEM, OP_MEM_STR, OP_IMM, OP_XMM, OP_MM, OP_NONE } type; unsigned int bytes; unsigned int count; union { unsigned long orig_val; u64 orig_val64; }; union { unsigned long *reg; struct segmented_address { ulong ea; unsigned seg; } mem; unsigned xmm; unsigned mm; } addr; union { unsigned long val; u64 val64; char valptr[sizeof(sse128_t)]; sse128_t vec_val; u64 mm_val; void *data; }; };