Received: by 10.213.65.68 with SMTP id h4csp495048imn; Fri, 16 Mar 2018 09:27:50 -0700 (PDT) X-Google-Smtp-Source: AG47ELsSVHjvMF0b2w8ep8bPUg7r84Hme0oWmY5bxQHvdg9x5Sp8rCDPAOeg41Qb8YHxLcMVFXXL X-Received: by 10.98.70.89 with SMTP id t86mr2064991pfa.215.1521217670224; Fri, 16 Mar 2018 09:27:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521217670; cv=none; d=google.com; s=arc-20160816; b=Fu0yOYPthSKNNVqcyEtg1n02U9GvQhi0Jtg6o306pTRFKfe5TXM6e5+xjr4zqmJd2w ayBsLC2DFg3VwYyznw5Azo8evRzGOdBKmKf4qSkTm6iRJQYXYnOYVvuOXQgHUp8TGsEp bsyr9No00s3RiRZ1tNV5+IAHZGwpIUzvab5dBUtLafKSF8TuxNxMl+vZek4s07uEXHPm 9yGAGq8tR6/kn5Eq6/s8EwfsN6rHt3UzYB3soWgpoh+8EKdKOy7mal3f1ibwCJTLR6C/ sGqA8uoWhMwYKFIzMkJc/k2U6ONpf8vT3+Gn1V0X0PJlv8ZuloGrAhf5iNqhYHp9gLjT 3jTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=L84Y1r64jX6icNJpi02WBkf0q8fNPb2a6awXT/Vto74=; b=YTkVMUA42EK5aVyWVdqy/0NExmmbGgcE+q+uaVX1jWrDtirMks0dN3bZU8TlvpXGw9 avuG7faCAWdWdjFjCMe4rI82MSzfWvkfmjiPie/PctPeQMTHOFKgbas8DQh6l7/uUTLp sieE+tFFZGDXkb50RWt9TSnzMqvZyUkSwkJ3os0Qq4saM662De1CJX0n1IPvaxNoHHrJ etjd4Y60aEHh9LZMahv9Bw5tKkxJ38RF2PpebMyfk1oi6TGww8gotx9Ud4Sw+/L0Xd3S leJ8c3fVg4FZ7GKT4YH1v0/ovSLlzSB/c9zXZbEJA32PGEgnQu5UrBOi/uh5r7eLbQY1 rdpg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g1si5228773pge.694.2018.03.16.09.27.36; Fri, 16 Mar 2018 09:27:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752489AbeCPQ0I (ORCPT + 99 others); Fri, 16 Mar 2018 12:26:08 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:43296 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964812AbeCPPjc (ORCPT ); Fri, 16 Mar 2018 11:39:32 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 6AE5ACDE; Fri, 16 Mar 2018 15:39:31 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "David.butterfield" , Damien Le Moal , Bart Van Assche , "Martin K. Petersen" Subject: [PATCH 4.15 014/128] scsi: sd_zbc: Fix potential memory leak Date: Fri, 16 Mar 2018 16:22:35 +0100 Message-Id: <20180316152337.075974962@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180316152336.199007505@linuxfoundation.org> References: <20180316152336.199007505@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Damien Le Moal commit 4b433924b2755a94f99258c178684a0e05c344de upstream. Rework sd_zbc_check_zone_size() to avoid a memory leak due to an early return if sd_zbc_report_zones() fails. Reported-by: David.butterfield Signed-off-by: Damien Le Moal Cc: stable@vger.kernel.org Reviewed-by: Bart Van Assche Signed-off-by: Martin K. Petersen Signed-off-by: Greg Kroah-Hartman --- drivers/scsi/sd_zbc.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) --- a/drivers/scsi/sd_zbc.c +++ b/drivers/scsi/sd_zbc.c @@ -486,7 +486,7 @@ static int sd_zbc_check_capacity(struct */ static int sd_zbc_check_zone_size(struct scsi_disk *sdkp) { - u64 zone_blocks; + u64 zone_blocks = 0; sector_t block = 0; unsigned char *buf; unsigned char *rec; @@ -504,10 +504,8 @@ static int sd_zbc_check_zone_size(struct /* Do a report zone to get the same field */ ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0); - if (ret) { - zone_blocks = 0; - goto out; - } + if (ret) + goto out_free; same = buf[4] & 0x0f; if (same > 0) { @@ -547,7 +545,7 @@ static int sd_zbc_check_zone_size(struct ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, block); if (ret) - return ret; + goto out_free; } } while (block < sdkp->capacity); @@ -555,35 +553,32 @@ static int sd_zbc_check_zone_size(struct zone_blocks = sdkp->zone_blocks; out: - kfree(buf); - if (!zone_blocks) { if (sdkp->first_scan) sd_printk(KERN_NOTICE, sdkp, "Devices with non constant zone " "size are not supported\n"); - return -ENODEV; - } - - if (!is_power_of_2(zone_blocks)) { + ret = -ENODEV; + } else if (!is_power_of_2(zone_blocks)) { if (sdkp->first_scan) sd_printk(KERN_NOTICE, sdkp, "Devices with non power of 2 zone " "size are not supported\n"); - return -ENODEV; - } - - if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { + ret = -ENODEV; + } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { if (sdkp->first_scan) sd_printk(KERN_NOTICE, sdkp, "Zone size too large\n"); - return -ENODEV; + ret = -ENODEV; + } else { + sdkp->zone_blocks = zone_blocks; + sdkp->zone_shift = ilog2(zone_blocks); } - sdkp->zone_blocks = zone_blocks; - sdkp->zone_shift = ilog2(zone_blocks); +out_free: + kfree(buf); - return 0; + return ret; } static int sd_zbc_setup(struct scsi_disk *sdkp)