From patchwork Sun Feb 10 21:49:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Bianconi X-Patchwork-Id: 10804923 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0D25E922 for ; Sun, 10 Feb 2019 21:49:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1F9E29934 for ; Sun, 10 Feb 2019 21:49:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E602F2994A; Sun, 10 Feb 2019 21:49:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95E2829934 for ; Sun, 10 Feb 2019 21:49:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726751AbfBJVtq (ORCPT ); Sun, 10 Feb 2019 16:49:46 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:52326 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726008AbfBJVtp (ORCPT ); Sun, 10 Feb 2019 16:49:45 -0500 Received: by mail-wm1-f68.google.com with SMTP id m1so13786909wml.2 for ; Sun, 10 Feb 2019 13:49:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EQeYL9uyP/dFrsUbpXBcL0FMiGD+/51bsSe3i9CZAqY=; b=IWr/+S5DC+NRvdTCf/mtaknx3Imiqv1Or6qWxJ7WZg+xOgmMFNJT1JLTWSymuK34bt 5XrU1WbrONIPdnrY3QH0pWQAvWBWTRuR7KkqDntgXjX033QTCmat5MDRJUTc4oSGd886 jKl9n3U3gALbC3YHt+xOnegbghewjCV+7n7mcnQw+h0yPGkqZxo5vzrMLh6xgfMZlfik 54V1tMuMFX7tFEWvI8R3j1OZioVQnueltjyzUOqPmRujTSpcLkwRsNzFkfs8xKUX+n9F Bd06obelDlBdDCYcKVNoZo9+/L9/icjhLEvB2948Mgq9tu/1VPO3fKHidDIPxl01u2vS 5olA== X-Gm-Message-State: AHQUAuYadaPVMZFbMdT38FKl5mPmF4sVXCxfTJcL29jtJk5rPRR8M+1p eN9xUteIooJRp4E+U8iN/HEHFjxeneM= X-Google-Smtp-Source: AHgI3IYP1UXiI/MFhhLiUSFVe6JnFIOnckkhVOkw4ZAp0wb4F4i6bxkqaOfK5PIUSnSkeTLffNcpNw== X-Received: by 2002:a7b:c302:: with SMTP id k2mr6800452wmj.71.1549835383485; Sun, 10 Feb 2019 13:49:43 -0800 (PST) Received: from lore-desk.lan ([151.66.10.5]) by smtp.gmail.com with ESMTPSA id q14sm5436323wrw.39.2019.02.10.13.49.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 10 Feb 2019 13:49:42 -0800 (PST) From: Lorenzo Bianconi To: linux-wireless@vger.kernel.org Cc: nbd@nbd.name, sgruszka@redhat.com, stefan.wahren@i2se.com Subject: [PATCH wireless-drivers 2/3] mt76: usb: fix possible memory leak in mt76u_buf_free Date: Sun, 10 Feb 2019 22:49:14 +0100 Message-Id: <5d9891a95014b3bf724d81fc5693f3c7d00d4df2.1549832428.git.lorenzo.bianconi@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Move q->ndesc initialization before the for loop in mt76u_alloc_rx since otherwise allocated urbs will not be freed in mt76u_buf_free Double-check scatterlist pointer in mt76u_buf_free Fixes: b40b15e1521f ("mt76: add usb support to mt76 layer") Signed-off-by: Lorenzo Bianconi --- drivers/net/wireless/mediatek/mt76/usb.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/net/wireless/mediatek/mt76/usb.c b/drivers/net/wireless/mediatek/mt76/usb.c index b061263453d4..31c18ac1c94a 100644 --- a/drivers/net/wireless/mediatek/mt76/usb.c +++ b/drivers/net/wireless/mediatek/mt76/usb.c @@ -331,10 +331,16 @@ EXPORT_SYMBOL_GPL(mt76u_buf_alloc); void mt76u_buf_free(struct mt76u_buf *buf) { struct urb *urb = buf->urb; + struct scatterlist *sg; int i; - for (i = 0; i < urb->num_sgs; i++) - skb_free_frag(sg_virt(&urb->sg[i])); + for (i = 0; i < urb->num_sgs; i++) { + sg = &urb->sg[i]; + if (!sg) + continue; + + skb_free_frag(sg_virt(sg)); + } usb_free_urb(buf->urb); } EXPORT_SYMBOL_GPL(mt76u_buf_free); @@ -540,7 +546,8 @@ static int mt76u_alloc_rx(struct mt76_dev *dev) nsgs = 1; } - for (i = 0; i < MT_NUM_RX_ENTRIES; i++) { + q->ndesc = MT_NUM_RX_ENTRIES; + for (i = 0; i < q->ndesc; i++) { err = mt76u_buf_alloc(dev, &q->entry[i].ubuf, nsgs, q->buf_size, SKB_WITH_OVERHEAD(q->buf_size), @@ -548,7 +555,6 @@ static int mt76u_alloc_rx(struct mt76_dev *dev) if (err < 0) return err; } - q->ndesc = MT_NUM_RX_ENTRIES; return mt76u_submit_rx_buffers(dev); }