Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp4105549iog; Tue, 28 Jun 2022 09:05:38 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uTaRvCjL7p2xkecpqmi13qlV2+Vs/6wuYqlQogxKXNdzk6OSc82mTJ0jOh5+5KKU6pIuHb X-Received: by 2002:a05:6a00:21c8:b0:4fd:f89f:ec0e with SMTP id t8-20020a056a0021c800b004fdf89fec0emr5440670pfj.83.1656432337928; Tue, 28 Jun 2022 09:05:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656432337; cv=none; d=google.com; s=arc-20160816; b=C5/p1Qxji9PQXYfDx+0WXqU2Gx19uN/XootiC2+l+jP1O4aanldJa+V4GCoZc0Rjcx pGB5FwBHe9iYsG6Bv+E4LMy5bU/9yoNTM/aCYeKWDZw9hKaU7LnCFF2YnGIaRbThmWeW s3Pq9ANCKNE8d3kfOor/ImTxvHbhHl+GX20Ch2gljKgWFRiT1p4B+tBx3yOTWME1WD8L kMJzCSyjqT3p3gh0oWZNTj6ssuvi8DpLhWV1QnezVmuf1wH1ZB9o/F/0nmyfQyMF1SPx EXJemXXQMYYv5YsHrDxEbs6nGSPXkGA0ChzVtP5IUpvrSg3xiaXjiE1nF0XlShiBtArB McTg== 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=+p5rrSS5gmHyiQeGIkyhRA+tlGeOsOERv89U/jZX2uc=; b=rRSdHfk+uYSPr0HPgEqHfIDoZSx+O4zKDPTySoBAlcqvSum065s9sGAafmlDTCqi4R xQErqRyP0/j6A4lPYdwd7NYoTh6ifa4irQy3jYt4IzuIS7Opzanc6K3GvQEbdKPAfT22 rryPR4O1RBn9rjB0EqCMlYcTmaezfX4qMwXoKQlVN92PFHrnQre4b7UtRmyUnAC1I+Gh b8V/biuU/mSQSphTxFyC9PQYpheTESLUKJLUrXoXyeqFVb4ikBlH4QYPepq0W/y4bcti FFJgGVfdxLvCD97XgIsMNyOSBWVcBg6MKmWN/CQ3Rs/Rnp0K2roeUWLv19SCNOKLhUPh xHsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Nw4WjXz4; 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 il10-20020a17090b164a00b001ecb5fc2814si1444pjb.66.2022.06.28.09.05.22; Tue, 28 Jun 2022 09:05:37 -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=Nw4WjXz4; 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 S1348261AbiF1P6h (ORCPT + 99 others); Tue, 28 Jun 2022 11:58:37 -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 X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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, 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); >> }