Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp656278pxb; Thu, 26 Aug 2021 11:15:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxW+F6y28jQ6h8LOPCjb1v/aRoD0I26ZDeR9iFn0XAHiAedAbCIx/pWC7YcXKmTOZKwg9Ed X-Received: by 2002:a02:cbb0:: with SMTP id v16mr4579240jap.114.1630001731420; Thu, 26 Aug 2021 11:15:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630001731; cv=none; d=google.com; s=arc-20160816; b=CFFfrRwvNw/OarM+bLS3hIDBhfqrS/ycmEEC9HZgRgX9bjlz6jIm/2TMeNNL4GDVF3 Vua7PILpUiUolYr76Bis3pPeIZZ6ivsTKrFazs7A1a3UTFVicPEMQO6Kgi5YsDWGsyF5 0B0Wv74snkQLr225CQ5ihGuyEnaQ2C/2aQT/Rf+mPn2eWEFG+8Dr/DyEB8ivqM+K6F4a Htkzm7YYBXySpWA0rDSc5WXv/2DxtG0XycW3jGKka9OB6IZc4iru0RtfqVRypKFzaILp 8W7JwWzM034hYaCE36SwcMtbP0XIWGpY8HHhfUtAH7dVFPD9ilzpPWA6hDcf40zalyaH 0hRw== 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=sRuC5V6T0HI9RSkyu52RSwPk8VQRtun3hwD4VTXAwFo=; b=ZB3PyRBw35RqJUACU0fQtHUcb1vx4GE1jpIuUzb2j0ZtRNh6qQtfVhhr2L0rLAeTRZ /2Hzh1RyOCcyafCnD6EtU+pXbUbpbPKYbr83Z1M/HsdWNUXf2tp8EDwsxMMbp0aHCz5a 1fedbpksbMjkwvjR5T8jUEM4tO0N4MSoaicp4EDMgYTxXVK9ED02Ybfa+BVE2n7LHvFY TMv6xi3VV4bVEohJR0S0IOVlMeeVqy1CJBYBij5zadQEP/fXZLzz5vsoNa55Tjg9ezWb zMdVy0JardxHy1E2SX3TdILoisE+42ImmMSg217qxHbUVGW2REjM7MZIgRQdWM4esJtP RWdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Ls3+/dzy"; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g8si3290898ioq.76.2021.08.26.11.15.18; Thu, 26 Aug 2021 11:15:31 -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=@redhat.com header.s=mimecast20190719 header.b="Ls3+/dzy"; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243312AbhHZSOv (ORCPT + 99 others); Thu, 26 Aug 2021 14:14:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:38593 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232729AbhHZSOu (ORCPT ); Thu, 26 Aug 2021 14:14:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630001643; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sRuC5V6T0HI9RSkyu52RSwPk8VQRtun3hwD4VTXAwFo=; b=Ls3+/dzyKAyFZf5T6W6xIqiYXIErqeplQ5VaWL1X9S+nuFb4QgU8yt8o5nhTaKYFOjotaN gV08HhE1YwhROM7Z2Tj4lgKusUczIWWFmNMoWBauVOzsLfHdND/v9UaGgwdJ8q6FJV4Ihg iDqlAySAL/GiHhf4c74YjpjCnBwRn3E= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-275-7Z0Km1TOOo-84TcMbwWzYA-1; Thu, 26 Aug 2021 14:14:01 -0400 X-MC-Unique: 7Z0Km1TOOo-84TcMbwWzYA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5BABF18C89C4; Thu, 26 Aug 2021 18:14:00 +0000 (UTC) Received: from localhost (unknown [10.22.10.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 52D055D9C6; Thu, 26 Aug 2021 18:13:57 +0000 (UTC) Date: Thu, 26 Aug 2021 14:13:56 -0400 From: Eduardo Habkost To: Sean Christopherson Cc: Vitaly Kuznetsov , Maxim Levitsky , kvm@vger.kernel.org, Paolo Bonzini , Wanpeng Li , Jim Mattson , "Dr. David Alan Gilbert" , Nitesh Narayan Lal , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 4/4] KVM: x86: Fix stack-out-of-bounds memory access from ioapic_write_indirect() Message-ID: <20210826181356.xhsie7kkqoeukeju@habkost.net> References: <87k0kakip9.fsf@vitty.brq.redhat.com> <2df0b6d18115fb7f2701587b7937d8ddae38e36a.camel@redhat.com> <87h7fej5ov.fsf@vitty.brq.redhat.com> <36b6656637d1e6aaa2ab5098f7ebc27644466294.camel@redhat.com> <87bl5lkgfm.fsf@vitty.brq.redhat.com> <87tujcidka.fsf@vitty.brq.redhat.com> <20210826145210.gpfbiagntwoswrzp@habkost.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 26, 2021 at 06:01:15PM +0000, Sean Christopherson wrote: > On Thu, Aug 26, 2021, Eduardo Habkost wrote: > > > @@ -918,7 +918,7 @@ static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src, > > > static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm, > > > struct kvm_lapic **src, struct kvm_lapic_irq *irq, > > > struct kvm_apic_map *map, struct kvm_lapic ***dst, > > > - unsigned long *bitmap) > > > + unsigned long *bitmap64) > > > > You can communicate the expected bitmap size to the compiler > > without typedefs if using DECLARE_BITMAP inside the function > > parameter list is acceptable coding style (is it?). > > > > For example, the following would have allowed the compiler to > > catch the bug you are fixing: > > > > Signed-off-by: Eduardo Habkost > > --- > > diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h > > index d7c25d0c1354..e8c64747121a 100644 > > --- a/arch/x86/kvm/lapic.h > > +++ b/arch/x86/kvm/lapic.h > > @@ -236,7 +236,7 @@ bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector); > > void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu); > > > > void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq, > > - unsigned long *vcpu_bitmap); > > + DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS)); > > > > bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq, > > struct kvm_vcpu **dest_vcpu); > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > > index 76fb00921203..1df113894cba 100644 > > --- a/arch/x86/kvm/lapic.c > > +++ b/arch/x86/kvm/lapic.c > > @@ -1166,7 +1166,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, > > * each available vcpu to identify the same. > > */ > > void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq, > > - unsigned long *vcpu_bitmap) > > + DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS)) > > { > > struct kvm_lapic **dest_vcpu = NULL; > > struct kvm_lapic *src = NULL; > > Sadly, that would not have actually caught the bug. In C++, an array param does > indeed have a fixed size, but in C an array param is nothing more than syntatic > sugar that is demoted to a plain ol' pointer. E.g. gcc-10 and clang-11 both > happily compile with "DECLARE_BITMAP(vcpu_bitmap, 0)" and the original single > "unsigned long vcpu_bitmap". Maybe there are gcc extensions to enforce array > sizes? But if there are, they are not (yet) enabled for kernel builds. The compiler wouldn't have caught it today only because Linux is compiled with `-Wno-stringop-overflow`. I have some hope that eventually the warning will be enabled, as indicated on the commit message if commit 5a76021c2eff ("gcc-10: disable 'stringop-overflow' warning for now"). Even if the warning isn't enabled, the bitmap size declaration would be a hint for humans reading the code. -- Eduardo