From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98AAEC43334 for ; Tue, 28 Jun 2022 15:58:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348265AbiF1P6i (ORCPT ); Tue, 28 Jun 2022 11:58:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347308AbiF1P6O (ORCPT ); Tue, 28 Jun 2022 11:58:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3CCF6B85 for ; Tue, 28 Jun 2022 08:58:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656431886; 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=+p5rrSS5gmHyiQeGIkyhRA+tlGeOsOERv89U/jZX2uc=; b=Nw4WjXz4tOFclAh59w2z1MaTDG6oav1XhUdUEkBOnR/IF771fcouMJW9/1cNwdiSv405Ao jQNlKluyPNsLQX34XpnAcvN65la8WX4p8UPLGY3VhQnu4mjS9nX81qcnF9SegyHjSqMBHi lS2+0o1rDtm2aVVRvev8DGk5UIBm7tU= 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-489-wcmq9P1ZMKKRdbKYNTZyQg-1; Tue, 28 Jun 2022 11:58:03 -0400 X-MC-Unique: wcmq9P1ZMKKRdbKYNTZyQg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 03E01384F808; Tue, 28 Jun 2022 15:58:03 +0000 (UTC) Received: from localhost (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B2E7141615A; Tue, 28 Jun 2022 15:58:02 +0000 (UTC) From: Cornelia Huck To: Alex Williamson , Schspa Shi Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] vfio: Clear the caps->buf to NULL after free In-Reply-To: <20220628095013.266d4a40.alex.williamson@redhat.com> Organization: Red Hat GmbH References: <20220628152429.286-1-schspa@gmail.com> <20220628095013.266d4a40.alex.williamson@redhat.com> User-Agent: Notmuch/0.36 (https://notmuchmail.org) Date: Tue, 28 Jun 2022 17:58:01 +0200 Message-ID: <87ilokbx12.fsf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 28 2022, Alex Williamson wrote: > On Tue, 28 Jun 2022 23:24:29 +0800 > Schspa Shi wrote: > >> API vfio_info_cap_add will free caps->buf, clear it to NULL after >> free. > > Should this be something like: > > On buffer resize failure, vfio_info_cap_add() will free the buffer, > report zero for the size, and return -ENOMEM. As additional > hardening, also clear the buffer pointer to prevent any chance of a > double free. I like that better. With that, Reviewed-by: Cornelia Huck > > Thanks, > Alex > >> 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; >> caps->size = 0; >> return ERR_PTR(-ENOMEM); >> }