Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3734466iog; Tue, 28 Jun 2022 01:21:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sJ5lX3gzzuAYoCiXrxHCyGXDwEJsyIoAY63QQrt970iwkKI8XaPNVNxGtTjeSyN/TIYwef X-Received: by 2002:a17:90a:2f87:b0:1ec:87b2:ff15 with SMTP id t7-20020a17090a2f8700b001ec87b2ff15mr20145143pjd.135.1656404466844; Tue, 28 Jun 2022 01:21:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656404466; cv=none; d=google.com; s=arc-20160816; b=anza83KPOGw2k/FjeXX0nXLMrI9YYcj1E+WxLdsdp6JCUtrHgsjd7tFePynk+JGixR tvHFA7imTh4KDXPy0C0p2bqb8WdVBDQGZiIhYmlyT0khL8GGAYZnLWIMqyeKpcEzsdaK kxSSzlcjJBKn171kD7Ee77X4A/SDWM4Zo+Od+Fn4TDcEDLKjxSxL96hfa0dYlmH2+iAm lb5B+KgVpgSWJrW+shQvHmmtK0ybOrBz6EM4cFKeKFU0V0VxSlwrE8wFdgr9+hzk9kkc y81Gr+yyaVjNfenVScfI/KkAeV2g06LRdX1PYu25ROoyBWBS1vJjUIz2b7+WdbvV5fv0 Fxfw== 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:user-agent :references:organization:in-reply-to:subject:cc:to:from :dkim-signature; bh=4ANCIygcKkNb0sxE6zYRICe30C7lk84k3shbREy4O7w=; b=TJ6T1xzhyDFb5o8Fo6+SvNdi75nZ8IvbjjQ4KEP8rxMkYvmMw+Y/oSwO6VmERlV6iK hEAAGHbkAdhgPFzVdUM5CYmBw+lBtcST7cMvPWZpzzCPRwRMhOuGcGJkyPXYWO/RBPgs 096sgvGnmx/QK2ppnWe7LrkkcufT7B0MbHfjHrXipcIZZpq/WrbDInd/SfZ64vbjomZY SmCT1pheC8JJI9PVsA7teEZXjz6WHQCMUZcoAhM01jem5NvtbOzdM87VtKD1EgR+dB6o WFM1Fxdu+3vxV95agF34qactVm3HIiZ4iI6A7YC0o3fBsa2/7aZnhQjK7GJMktHtcajC iC9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MLH8MRsX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c3-20020a170902d90300b0016a496aededsi18607090plz.175.2022.06.28.01.20.53; Tue, 28 Jun 2022 01:21:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MLH8MRsX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S245066AbiF1ITz (ORCPT + 99 others); Tue, 28 Jun 2022 04:19:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242502AbiF1ITE (ORCPT ); Tue, 28 Jun 2022 04:19:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 98EBC2D1C6 for ; Tue, 28 Jun 2022 01:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656404278; 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=4ANCIygcKkNb0sxE6zYRICe30C7lk84k3shbREy4O7w=; b=MLH8MRsXhRTwdZmgM/DUodzguWNIqENHONo7EgdW02hmvBHbi+eZrDFg5Rj5QjMnbNsyoc 1yg7t2k05ulyn6UO+GvxeRJ3rWubqjUX7RtYsz4HjYu3eQbkogWUlFgWhhm4AmFEb5vsex bwEtGc0F6WykfDcVqWjalJQUyDsvcy4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-446-qCu0jKULMfmE6tRgknBLRg-1; Tue, 28 Jun 2022 04:17:54 -0400 X-MC-Unique: qCu0jKULMfmE6tRgknBLRg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0DD2F1C00AC5; Tue, 28 Jun 2022 08:17:54 +0000 (UTC) Received: from localhost (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5F3229D54; Tue, 28 Jun 2022 08:17:53 +0000 (UTC) From: Cornelia Huck To: Schspa Shi , alex.williamson@redhat.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, zhaohui.shi@horizon.ai, Schspa Shi Subject: Re: [PATCH] vfio: Fix double free for caps->buf In-Reply-To: <20220628050711.74945-1-schspa@gmail.com> Organization: Red Hat GmbH References: <20220628050711.74945-1-schspa@gmail.com> User-Agent: Notmuch/0.36 (https://notmuchmail.org) Date: Tue, 28 Jun 2022 10:17:52 +0200 Message-ID: <8735fpcibz.fsf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 28 2022, Schspa Shi wrote: > There is a double free, if vfio_iommu_dma_avail_build_caps > calls failed. > > The following call path will call vfio_info_cap_add multiple times > > vfio_iommu_type1_get_info > if (!ret) > ret = vfio_iommu_dma_avail_build_caps(iommu, &caps); > > if (!ret) > ret = vfio_iommu_iova_build_caps(iommu, &caps); > > If krealloc failed on vfio_info_cap_add, there will be a double free. But it will only call it several times if the last call didn't fail, won't it? > > Signed-off-by: Schspa Shi > --- > drivers/vfio/vfio.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c > index 61e71c1154be..a0fb93866f61 100644 > --- a/drivers/vfio/vfio.c > +++ b/drivers/vfio/vfio.c > @@ -1812,6 +1812,7 @@ struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps, > buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL); > if (!buf) { > kfree(caps->buf); > + caps->buf = NULL; We could add this as some kind of hardening, I guess. Current callers all seem to deal with failure correctly. > caps->size = 0; > return ERR_PTR(-ENOMEM); > }