Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp501922pxb; Fri, 15 Oct 2021 09:49:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfDorSTnGoSup+JjmHDbx44EzdWOysUmOpCcwqnM19hbnnN/UqDgAHkaFIoO6X/jNDJJ2R X-Received: by 2002:a17:906:e287:: with SMTP id gg7mr8095390ejb.387.1634316584764; Fri, 15 Oct 2021 09:49:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634316584; cv=none; d=google.com; s=arc-20160816; b=L3JRDgIKvBQqP4ZTRTDO1mqLbR3HMEF08w5STKz2vA4sEPlpLmqiCrtSqwNnUmOChG NH9c9IeImBpWiBZLu6imTyDJRZ2DQZfohXD6d8Jhz3EqJqv9t/hTfK2e4h/RB8qDB0E4 0V78GUkvolWG6FJzNzKx7G5bDvhCPP0ai9SXZNh68Y8lUqckW/40dT4XqmBmwZ+ur1hn eBI28vnOPfqbhLeFUXxY3NgyqsBSR78RHQzUTn/LY+HTOMJkom4Bmi7IY0ene9+xeaup BS9wmgJf6xb4Bm5HEq5CbUlGm1cuioHw8VDwvs7SOkZd5pnTA0dFcrO+ctfz1LRguOwQ gTpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=AzxVtwA455vtW09HbBFvb3hp3T/L1wdL1B4pSD9Ee2c=; b=SPAqSxA+iACl8bwkQyTbvHDX4U2aMmNhlfSC64mK8SHjLzDV3PW2HMq1TC0z+dm9b9 nz7K+GnDqeq001Qrh6B2F5HUKPi5Ps7slsU/nwGU09ptv6Bi4n2V4ea88ftZncX4+NHV gJUmbuwPQpl8cbePqo90M/pwBGAJPxbE8hTwj92iFLHSELFvKhELUZLCiAmK2D7XksAQ Nc8bsYKba52UnZkwS6eQG5lgfEP/YUFjNvJYk5m5ebvSIt3Wg7em1ImmoOAaUbunM9/U veTwcUzRRY67GNSt7i16tHCBd07UHHfaXlRjH70uP9FbYRfSvNfYhqvlQQtS2VO80ggW Xwlw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z3si9132455edp.30.2021.10.15.09.49.21; Fri, 15 Oct 2021 09:49:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234600AbhJOI6q (ORCPT + 99 others); Fri, 15 Oct 2021 04:58:46 -0400 Received: from szxga08-in.huawei.com ([45.249.212.255]:25138 "EHLO szxga08-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229656AbhJOI6o (ORCPT ); Fri, 15 Oct 2021 04:58:44 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4HW0TF3Zvtz1DHc9; Fri, 15 Oct 2021 16:54:57 +0800 (CST) Received: from kwepemm600001.china.huawei.com (7.193.23.3) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Fri, 15 Oct 2021 16:56:37 +0800 Received: from huawei.com (10.175.104.82) by kwepemm600001.china.huawei.com (7.193.23.3) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Fri, 15 Oct 2021 16:56:36 +0800 From: Wang Hai To: , CC: , Subject: [PATCH v2] USB: serial: Fix possible memleak in keyspan_port_probe() Date: Fri, 15 Oct 2021 16:55:43 +0800 Message-ID: <20211015085543.1203011-1-wanghai38@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.82] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600001.china.huawei.com (7.193.23.3) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I got memory leak as follows when doing fault injection test: unreferenced object 0xffff888258228440 (size 64): comm "kworker/7:2", pid 2005, jiffies 4294989509 (age 824.540s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [] slab_post_alloc_hook+0x9c/0x490 [] kmem_cache_alloc_trace+0x1f7/0x470 [] keyspan_port_probe+0xa4/0x5d0 [keyspan] [] usb_serial_device_probe+0x97/0x1d0 [usbserial] [] really_probe+0x167/0x460 [] __driver_probe_device+0xf9/0x180 [] driver_probe_device+0x53/0x130 [] __device_attach_driver+0x105/0x130 [] bus_for_each_drv+0x129/0x190 [] __device_attach+0x1c9/0x270 [] device_initial_probe+0x20/0x30 [] bus_probe_device+0x142/0x160 [] device_add+0x829/0x1300 [] usb_serial_probe.cold+0xc9b/0x14ac [usbserial] [] usb_probe_interface+0x1aa/0x3c0 [usbcore] [] really_probe+0x167/0x460 If it fails to allocate memory for an out_buffer[i] or in_buffer[i], the previously allocated memory for out_buffer or in_buffer needs to be freed on the error handling path, otherwise a memory leak will result. Fixes: bad41a5bf177 ("USB: keyspan: fix port DMA-buffer allocations") Reported-by: Hulk Robot Signed-off-by: Wang Hai --- v1->v2: rename error labels drivers/usb/serial/keyspan.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/drivers/usb/serial/keyspan.c b/drivers/usb/serial/keyspan.c index 87b89c99d517..1cfcd805f286 100644 --- a/drivers/usb/serial/keyspan.c +++ b/drivers/usb/serial/keyspan.c @@ -2890,22 +2890,22 @@ static int keyspan_port_probe(struct usb_serial_port *port) for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i) { p_priv->in_buffer[i] = kzalloc(IN_BUFLEN, GFP_KERNEL); if (!p_priv->in_buffer[i]) - goto err_in_buffer; + goto err_free_in_buffer; } for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i) { p_priv->out_buffer[i] = kzalloc(OUT_BUFLEN, GFP_KERNEL); if (!p_priv->out_buffer[i]) - goto err_out_buffer; + goto err_free_out_buffer; } p_priv->inack_buffer = kzalloc(INACK_BUFLEN, GFP_KERNEL); if (!p_priv->inack_buffer) - goto err_inack_buffer; + goto err_free_out_buffer; p_priv->outcont_buffer = kzalloc(OUTCONT_BUFLEN, GFP_KERNEL); if (!p_priv->outcont_buffer) - goto err_outcont_buffer; + goto err_free_inack_buffer; p_priv->device_details = d_details; @@ -2951,15 +2951,14 @@ static int keyspan_port_probe(struct usb_serial_port *port) return 0; -err_outcont_buffer: +err_free_inack_buffer: kfree(p_priv->inack_buffer); -err_inack_buffer: +err_free_out_buffer: for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i) kfree(p_priv->out_buffer[i]); -err_out_buffer: +err_free_in_buffer: for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i) kfree(p_priv->in_buffer[i]); -err_in_buffer: kfree(p_priv); return -ENOMEM; -- 2.25.1