Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp606330pxf; Wed, 31 Mar 2021 11:11:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNKMOiLrJxCRQVlIGhGooGNfea9BUyDUUBIyGUf7JYqMkF59NJz2kvAeL5RFxkP0SOC210 X-Received: by 2002:a17:906:26d4:: with SMTP id u20mr5170704ejc.114.1617214281531; Wed, 31 Mar 2021 11:11:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617214281; cv=none; d=google.com; s=arc-20160816; b=YGppJvp/X2snjDj/AaIJxFvVzHT2x0cYCzwd64NUR2VQKAYeK67iie0W8MW/+o0XN+ 49g74u/qfc7024BQtU0RVygNHgToxaqD5QLlCJ/zaCHFPufC5R6CBID6RIu2g1S43ifJ YFtsSR2SR8N4+JMFwr+1LsFlqyzPbjbyKcyoPTmuIIEY4ieaaUxTKVdH5UzBAaNuSoug AH/UJQozGRtDXIpR8a6IabxCC7/lyhsZJ6zwWcyP8y71PHe7bmo14ZAFlFi0n6Ir0QcD ei4V/LZJu9z82hQx55ZTGRQcZMMAjFI4OTl4KTHBK0DREBGLi6n2X9ApeFMYLUYiPvT4 yE7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=fzUxfJHs4m96o0h93XY6hLbrWDklPfvA/cxbRjLDuiE=; b=qYKW/Verj+IWYBMQRAuamWi2SaRT6BqXkjDE/E5lQ+1WFJvQuey/SOfLNxe7sRnQD4 xae4ikp/TtcRjoeRUIqHk+wyPZ/d+eex/697Y0rLL0bY/NDBnug4ox2sA5G/J+mKKCVX O1/c+ClErRoS9HoGifgehKtSsM1O5/kLmlOZ9BCHMGIzo2lxObXlvE1sdkqWUJda1lpE Zb6vOmQXoCDUEWyS5nLeCFfwxIumbITXGxtCAuOr3yThOymDbATcgbNnYqAB7jJcshhw VomLc23BEQ954lm55r9fgn+aqIVhgpr5kM6wPUfzCkbNHyYWdf/c1A6k2ilLWLHhUpZf 6fBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AW9hN1Dj; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t19si2179083eds.454.2021.03.31.11.10.57; Wed, 31 Mar 2021 11:11:21 -0700 (PDT) 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=@google.com header.s=20161025 header.b=AW9hN1Dj; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234754AbhCaSHh (ORCPT + 99 others); Wed, 31 Mar 2021 14:07:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234880AbhCaSHb (ORCPT ); Wed, 31 Mar 2021 14:07:31 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A64A0C06174A for ; Wed, 31 Mar 2021 11:07:30 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id j6-20020a17090adc86b02900cbfe6f2c96so1677581pjv.1 for ; Wed, 31 Mar 2021 11:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=fzUxfJHs4m96o0h93XY6hLbrWDklPfvA/cxbRjLDuiE=; b=AW9hN1Djcg4K3j6y1d6rLhacaldp5/qFhVVlaCM4Y4E4DJEIsgt2V+ZwiR2/a+Dazb 1JRokka2xSlR+ur/y4YTI5lunennSWPSD6CCusxkJwPorqyEwKEiZHy3jxOSbXjsIztY CL2ZvlWfAFL4bxmN1KzcCzGxE72CVrjbkQA36b76+r7MAKX2GuYTIg4fHXGUizObFMGr qh2q7GOSLeDC8hiM1lbp5K+V3Pr6tbnPU6z460UOE0JuPs6kxAj1rLgbjpxzYV18kUlX 62h2/pIh85HeBtJv5VaQXUJ3OYSnMML6O9vO1R/AkJUgrjd3zbEdDT4qVPfZstkbhIb0 7nwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=fzUxfJHs4m96o0h93XY6hLbrWDklPfvA/cxbRjLDuiE=; b=XYT82xwUNl5zoMxr+Q3v1aPxQqC7T0ENTo+aCTZF6bxKafXR2IP+6dXY8/WCSqdEjb 4NvDr8+aZzWj0/ZmBghJWI11X+G1uzOjaWm5xdPXXpHA9jS4QRGedMGsCbr2edO8jxkH AYVk/D7mAm5TBsqSrCiyMgbWVUVpiBxYbHmVmPlrh4Rko7Zag9lUdYYp0dNg7sKVJxsY +K5ABP1lohibGiVugm1c1FtABhcz20KIlovJqxOKTK7EcpH2tz0dhr2/eZEpYtDd4GfB 3XwxrgDuj/dCWg5ullEGG9H0l4lrHrEGMlkWL39XMG5wDDSgW+6uDNMhiJKx9RKCrA4U +ITw== X-Gm-Message-State: AOAM533HcyTN1YrrAJpv6HbU+xY01frDqdIDb7RlFXPceacvUgJqHj48 nVvRIDOMHsZriFLYqa+QdliBrw== X-Received: by 2002:a17:90a:8b16:: with SMTP id y22mr4558382pjn.191.1617214049909; Wed, 31 Mar 2021 11:07:29 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id e1sm2979522pfi.175.2021.03.31.11.07.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Mar 2021 11:07:29 -0700 (PDT) Date: Wed, 31 Mar 2021 18:07:25 +0000 From: Sean Christopherson To: Yang Li Cc: pbonzini@redhat.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86: Fix potential memory access error Message-ID: References: <1617182122-112315-1-git-send-email-yang.lee@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1617182122-112315-1-git-send-email-yang.lee@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 31, 2021, Yang Li wrote: > Using __set_bit() to set a bit in an integer is not a good idea, since > the function expects an unsigned long as argument, which can be 64bit wide. > Coverity reports this problem as > > High:Out-of-bounds access(INCOMPATIBLE_CAST) > CWE119: Out-of-bounds access to a scalar > Pointer "&vcpu->arch.regs_avail" points to an object whose effective > type is "unsigned int" (32 bits, unsigned) but is dereferenced as a > wider "unsigned long" (64 bits, unsigned). This may lead to memory > corruption. > > /home/heyuan.shy/git-repo/linux/arch/x86/kvm/kvm_cache_regs.h: > kvm_register_is_available > > Just use BIT instead. Meh, we're hosed either way. Using BIT() will either result in undefined behavior due to SHL shifting beyond the size of a u64, or setting random bits if the truncated shift ends up being less than 63. I suppose one could argue that undefined behavior is better than memory corruption, but KVM is very broken if 'reg' is out-of-bounds so IMO it's not worth changing. There are only two call sites that don't use a hardcoded value, and both are guarded by WARN. kvm_register_write() bails without calling kvm_register_mark_dirty(), so that's guaranteed safe. vmx_cache_reg() WARNs after kvm_register_mark_available(), but except for kvm_register_read(), all calls to vmx_cache_reg() use a hardcoded value, and kvm_register_read() also WARNs and bails. Note, all of the above holds true for kvm_register_is_{available,dirty}(), too. So in the current code, it's impossible for this to be a problem. Theoretically future code could introduce bugs, but IMO we should never accept code that uses a non-hardcoded 'reg' and doesn't pre-validate. The number of uops is basically a wash because "BTS , " is fairly expensive; depending on the uarch, the difference is ~1-2 uops in favor of BIT(). On the flip side, __set_bit() shaves 8 bytes. Of course, none these flows are anywhere near that senstive. TL;DR: I'm not opposed to using BIT(), I just don't see the point. __set_bit(): 0x00000000000104e6 <+6>: mov %esi,%eax 0x00000000000104e8 <+8>: mov %rdi,%rbp 0x00000000000104eb <+11>: sub $0x8,%rsp 0x00000000000104ef <+15>: bts %rax,0x2a0(%rdi) |= BIT(): 0x0000000000010556 <+6>: mov %esi,%ecx 0x0000000000010558 <+8>: mov $0x1,%eax 0x000000000001055d <+13>: mov %rdi,%rbp 0x0000000000010560 <+16>: sub $0x8,%rsp 0x0000000000010564 <+20>: shl %cl,%rax 0x0000000000010567 <+23>: or %eax,0x2a0(%rdi) > Reported-by: Abaci Robot > Signed-off-by: Yang Li > --- > arch/x86/kvm/kvm_cache_regs.h | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h > index 2e11da2..cfa45d88 100644 > --- a/arch/x86/kvm/kvm_cache_regs.h > +++ b/arch/x86/kvm/kvm_cache_regs.h > @@ -52,14 +52,14 @@ static inline bool kvm_register_is_dirty(struct kvm_vcpu *vcpu, > static inline void kvm_register_mark_available(struct kvm_vcpu *vcpu, > enum kvm_reg reg) > { > - __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); > + vcpu->arch.regs_avail |= BIT(reg); > } > > static inline void kvm_register_mark_dirty(struct kvm_vcpu *vcpu, > enum kvm_reg reg) > { > - __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); > - __set_bit(reg, (unsigned long *)&vcpu->arch.regs_dirty); > + vcpu->arch.regs_avail |= BIT(reg); > + vcpu->arch.regs_dirty |= BIT(reg); > } > > static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg) > -- > 1.8.3.1 >