Return-Path: Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:39819 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726775AbgKTIAK (ORCPT ); Fri, 20 Nov 2020 03:00:10 -0500 Date: Fri, 20 Nov 2020 09:00:00 +0100 From: Cornelia Huck Subject: Re: [PATCH] s390: cmf: fix use-after-free in enable_cmf Message-ID: <20201120090000.5ac4b5b8.cohuck@redhat.com> In-Reply-To: <20201120074850.31609-1-miaoqinglang@huawei.com> References: <20201120074850.31609-1-miaoqinglang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-ID: To: Qinglang Miao Cc: Vineeth Vijayan , Peter Oberparleiter , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org On Fri, 20 Nov 2020 15:48:50 +0800 Qinglang Miao wrote: > kfree(cdev) is called in put_device in the error branch. So that > device_unlock(&cdev->dev) would raise a use-after-free bug. In fact, > there's no need to call device_unlock after put_device. > > Fix it by adding simply return after put_device. > > Fixes: a6ef15652d26 ("s390/cio: fix use after free in cmb processing") > Reported-by: Hulk Robot > Signed-off-by: Qinglang Miao > --- > drivers/s390/cio/cmf.c | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/drivers/s390/cio/cmf.c b/drivers/s390/cio/cmf.c > index 72dd2471e..e95ca476f 100644 > --- a/drivers/s390/cio/cmf.c > +++ b/drivers/s390/cio/cmf.c > @@ -1149,9 +1149,12 @@ int enable_cmf(struct ccw_device *cdev) > sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group); > cmbops->free(cdev); > } > + > out: > - if (ret) > + if (ret) { > put_device(&cdev->dev); The put_device() here undoes a get_device() further up in the function. There is at least one more reference remaining, held by the caller of enable_cmf(). Returning here would actually introduce a bug (missing unlock). > + return ret; > + } > out_unlock: > device_unlock(&cdev->dev); > return ret;