Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4649209pxb; Tue, 31 Aug 2021 09:57:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZoGrPeKXjuH6MNeBasaLjzEPuH8kyGmGwg0003+CE2mF8Vmjo2rwQRpykN950Rc2l66fR X-Received: by 2002:a92:a801:: with SMTP id o1mr21110913ilh.128.1630429039666; Tue, 31 Aug 2021 09:57:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630429039; cv=none; d=google.com; s=arc-20160816; b=URqT8IQa+oWuzd4xtxbt9kAFMtWlSH5YriE18M5udiwqPS2SizGAxbMISeevhWHbvb jsbTHVKG9xz3NeQJu0Ow3Naa7QcRkec+Rx5bT16pBXsz12sJGk2TjVx48FI6XAPFD2SR eaZ2yHLdy5e+ImSriKVNRPkkxpREg4/aEiNF+EcjyXAqDxX6b8BRIpZkuX7u9XZ8c2mp JaWSJx07FdTsU1+t8bJ6S4hwVcRuNtcDX3RMyWppsVUAIHEZXt3VEuTkRSvP6SD9Tp6R QlA6bq4gYpwVg1SRvT6fMXrkkyvS3u0ggVDuU8HxAKeNizNkXvqxxKJkRPP1y8+pN1Fa 9Qgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=Cqcv7q3sxv359Hw+wbovVKAY99uifNtZJRnqtsftjkQ=; b=E6hiX/RgfhAqXEZfBP9KNIXNsNpolNmYM6mdxjgZO5q3oUATH+wkboiRVPMpSL7NWN CfEY7TfnRri6fKwGeDitGMDAGJK14CvNsEXM+3ng2irP+/esCQ64VE12dQZZIEhhuLIS v6rgSb5UIq8jal6ng6lhdB9eqWj4HM48tRZ0Wa7Y+SsHivzuNyC589Gs9fWfo9OnMX7/ pVLSRI62on2mJ/r7xW0kY7nhTz1rpl7cLOo4J31GtDpmontYpA6veL3wftqtr/G0cpp9 2uiw5+8WE0g3IfGIq9/EMZRqAq6tOyIiFE8R38+5eBiUGyX1rNHh4zH90QcLb7PNpSMW 2CZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AbbUgbPl; 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 m9si18969604iow.85.2021.08.31.09.57.08; Tue, 31 Aug 2021 09:57:19 -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=AbbUgbPl; 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 S239178AbhHaPy6 (ORCPT + 99 others); Tue, 31 Aug 2021 11:54:58 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:47329 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239058AbhHaPy5 (ORCPT ); Tue, 31 Aug 2021 11:54:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630425242; 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=Cqcv7q3sxv359Hw+wbovVKAY99uifNtZJRnqtsftjkQ=; b=AbbUgbPljbuejvXU4EdYCngtuVp1aFywPcR3prQV6p37jHmiK2u/EJ526SLrCNG9fFqjX4 0QNc2lkRRasg2odrX/kb6h/kB8HyMi+as9TrP0t8eCpdCH5P+YeRuF096PpRFAcWG+b2K1 i6TcFymZ69Lsq21A61ug64pmARREOmk= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-406-KO5PckmbMiO-t9oXRgyJaQ-1; Tue, 31 Aug 2021 11:54:00 -0400 X-MC-Unique: KO5PckmbMiO-t9oXRgyJaQ-1 Received: by mail-wr1-f70.google.com with SMTP id q14-20020a5d574e000000b00157b0978ddeso1581643wrw.5 for ; Tue, 31 Aug 2021 08:54:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=Cqcv7q3sxv359Hw+wbovVKAY99uifNtZJRnqtsftjkQ=; b=LxoC5+o5I0zw8jVKSsEjsBCuCvoXMk/3oeVmepK3FBg0rl+vhcumzl1NkUtRHe6MCp eCKdMXe9LBihbHP/vhHKjnOezXjcE8FtCR43uEod1TnBT4rUFyZuRd/FIc51Lnfq9sB/ FMjYy3/Sak4DMPOwbbK8kZ4CvtlZdP4N7E97/PBqofIOX0IqLFRx51gjWqPJmiuOqZPk MB/h04ZB08nSX+NkWrsHtXU4thSHeDoq8EwfsaWTMKejtMjnymB4LUbyIx3pqReybmQK RZAzKOcoEr7oavYxpEseGl3qgttPquzcZh1qVNSVlb9v2H+DdgGW06NDoDtKbWSW8lis zePg== X-Gm-Message-State: AOAM531uSjXhwHkJjBM/Y00x7LBEIxcGhCH5vsgSQn1M1WciTHy6NgaZ DNlku1hq1Z3TYxeTCwbowr3gOvy58vmsOab+KohWHmXSpRpzRG+gXw2k0DdaGwQu2HkNdEA0aWP EvloXm/YPOHbac6Z8UCp6d5Vw X-Received: by 2002:a5d:6daa:: with SMTP id u10mr32428377wrs.31.1630425239228; Tue, 31 Aug 2021 08:53:59 -0700 (PDT) X-Received: by 2002:a5d:6daa:: with SMTP id u10mr32428349wrs.31.1630425238976; Tue, 31 Aug 2021 08:53:58 -0700 (PDT) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id z19sm3139223wma.0.2021.08.31.08.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 08:53:58 -0700 (PDT) From: Vitaly Kuznetsov To: tcs.kernel@gmail.com Cc: Haimin Zhang , pbonzini@redhat.com, seanjc@google.com, wanpengli@tencent.com, jmattson@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, jarkko@kernel.org Subject: Re: [PATCH] KVM: x86: Add a return code and check kvm_page_track_init In-Reply-To: <1630376040-20567-1-git-send-email-tcs_kernel@tencent.com> References: <1630376040-20567-1-git-send-email-tcs_kernel@tencent.com> Date: Tue, 31 Aug 2021 17:53:57 +0200 Message-ID: <87wno1obje.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org tcs.kernel@gmail.com writes: > From: Haimin Zhang > > We found a null pointer deref by our modified syzkaller. > KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] > CPU: 1 PID: 13993 Comm: syz-executor.0 Kdump: loaded Tainted: > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), > BIOS rel-1.12.0-59-gc9ba5276e321-prebuilt.qemu.org 04/01/2014 > RIP: 0010:rcu_segcblist_enqueue+0xf5/0x1d0 > RSP: 0018:ffffc90001e1fc10 EFLAGS: 00010046 > RAX: dffffc0000000000 RBX: ffff888135c00080 RCX: ffffffff815ba8a1 > RDX: 0000000000000000 RSI: ffffc90001e1fd00 RDI: ffff888135c00080 > RBP: ffff888135c000a0 R08: 0000000000000004 R09: fffff520003c3f75 > R10: 0000000000000003 R11: fffff520003c3f75 R12: 0000000000000000 > R13: ffff888135c00080 R14: ffff888135c00040 R15: 0000000000000000 > FS: 00007fecc99f1700(0000) GS:ffff888135c00000(0000) knlGS:0000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: 0000001b2f225000 CR3: 0000000093d08000 CR4: 0000000000750ee0 > DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > PKRU: 55555554 > Call Trace: > srcu_gp_start_if_needed+0x158/0xc60 build/../kernel/rcu/srcutree.c:823 > __synchronize_srcu+0x1dc/0x250 build/../kernel/rcu/srcutree.c:929 > kvm_mmu_uninit_vm+0x18/0x30 build/../arch/x86/kvm/mmu/mmu.c:5585 > kvm_arch_destroy_vm+0x43f/0x5c0 build/../arch/x86/kvm/x86.c:11277 > kvm_create_vm build/../arch/x86/kvm/../../../virt/kvm/kvm_main.c:1060 > kvm_dev_ioctl_create_vm build/../arch/x86/kvm/../../../virt/kvm/kvm_main > kvm_dev_ioctl+0xdfb/0x1860 build/../arch/x86/kvm/../../../virt/kvm/kvm_main > vfs_ioctl build/../fs/ioctl.c:51 [inline] > __do_sys_ioctl build/../fs/ioctl.c:1069 [inline] > __se_sys_ioctl build/../fs/ioctl.c:1055 [inline] > __x64_sys_ioctl+0x183/0x210 build/../fs/ioctl.c:1055 > do_syscall_x64 build/../arch/x86/entry/common.c:50 [inline] > do_syscall_64+0x34/0xb0 build/../arch/x86/entry/common.c:80 > entry_SYSCALL_64_after_hwframe+0x44/0xae > This is because when init_srcu_struct() calls alloc_percpu(struct > srcu_data) failed, kvm_page_track_init() didn't check init_srcu_struct > return code. > > Signed-off-by: Haimin Zhang > Reported-by: TCS Robot > --- > arch/x86/include/asm/kvm_page_track.h | 2 +- > arch/x86/kvm/mmu/page_track.c | 8 ++++++-- > arch/x86/kvm/x86.c | 7 +++++-- > 3 files changed, 12 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_page_track.h b/arch/x86/include/asm/kvm_page_track.h > index 87bd6025d91d..6a5f3acf2b33 100644 > --- a/arch/x86/include/asm/kvm_page_track.h > +++ b/arch/x86/include/asm/kvm_page_track.h > @@ -46,7 +46,7 @@ struct kvm_page_track_notifier_node { > struct kvm_page_track_notifier_node *node); > }; > > -void kvm_page_track_init(struct kvm *kvm); > +int kvm_page_track_init(struct kvm *kvm); > void kvm_page_track_cleanup(struct kvm *kvm); > > void kvm_page_track_free_memslot(struct kvm_memory_slot *slot); > diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c > index 91a9f7e0fd91..44a67a50f6d2 100644 > --- a/arch/x86/kvm/mmu/page_track.c > +++ b/arch/x86/kvm/mmu/page_track.c > @@ -163,13 +163,17 @@ void kvm_page_track_cleanup(struct kvm *kvm) > cleanup_srcu_struct(&head->track_srcu); > } > > -void kvm_page_track_init(struct kvm *kvm) > +int kvm_page_track_init(struct kvm *kvm) > { > + int r = -ENOMEM; Nitpick: pointless initializer, the value is always overwritten by init_srcu_struct()'s return value. > struct kvm_page_track_notifier_head *head; > > head = &kvm->arch.track_notifier_head; > - init_srcu_struct(&head->track_srcu); > + r = init_srcu_struct(&head->track_srcu); > + if (r) > + return r; > INIT_HLIST_HEAD(&head->track_notifier_list); > + return r; > } > > /* > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index e5d5c5ed7dd4..5da76f989207 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -11086,8 +11086,9 @@ void kvm_arch_free_vm(struct kvm *kvm) > > int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) > { > + int r = -EINVAL; Blank line missing here. > if (type) > - return -EINVAL; > + return r; I'd keep this code as-is and dropped then-pointless initializer to -EINVAL, it's OK to return directly: int ret; if (type) return -EINVAL; ... ret = kvm_page_track_init(kvm); if (ret) return ret; > > INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); > INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); > @@ -11121,7 +11122,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) > > kvm_apicv_init(kvm); > kvm_hv_init_vm(kvm); > - kvm_page_track_init(kvm); > + r = kvm_page_track_init(kvm); > + if (r) > + return r; > kvm_mmu_init_vm(kvm); > > return static_call(kvm_x86_vm_init)(kvm); -- Vitaly