Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6549328ybx; Mon, 11 Nov 2019 10:46:54 -0800 (PST) X-Google-Smtp-Source: APXvYqxdUqReNkimYCY45dWWaq1r7eIrvVrt633ZofhOLcdGJpzHgkAQILrhlfkDICSEJAMtrbbU X-Received: by 2002:a50:f058:: with SMTP id u24mr28517268edl.288.1573498014192; Mon, 11 Nov 2019 10:46:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573498014; cv=none; d=google.com; s=arc-20160816; b=Ai17JiFkkpYJQvmGiyQEc5cmyAMMlwVBamQjOhwlOFA6deZGaMe/h6BtJoG76q4emM CHrxYUOdPu5giETdsn21RZXpU6H/TcQI5Gsqoac0fLF2CH95BiwwAWEPzsL2cf+nup5z 4sjZUkZOT6fPTiYMeMLcFlzPFcDcsLilgebSofx8h2ECub4BC+zE9X5iiTLPKLQB6R3z B/aA0xqx/UV2Qr28JVgM4eoXWZzeK+FO7aYvz+R9JbvaenMZa0DrL3Ys1ixdNTktGB09 lcee8qbDhavaKr70+ZhFoOnMPrfELdF1NdvDfNveKN9bXx5yo79gu/T82WN0kwvNT3XH HKVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=StTvw5cAJ/aB1RAKKpqaYXLRoN9oNEM2vafwtOFTIXc=; b=pp6+q+BloMLHHTxO0Aplw/TesoB8BdWMZszI4lWFuxCS6DYTLo9LBEC1JhncvY6YXA klfYIdxbcpbIOl5DKBMIXn5LFi5i/VzwNX1jULFSXe3ltNh20/rFlU2Ya1lzmGJKQOzH hlsmqJwgbFbMw0n+ms6KeuPcMbqMGZL2injJzl/6S7wm7CJoJdiiBPTz8n8xwoq7ULaa x4x+0eiWf5J28gRxO5PjpWFfFswBgAbBJnw9LfcnktfG/PvoKBWpusjQskTIWljToSIl fQ4DhQVqNu1tDwN37/WWdVBDxAuy6ZO2Pe1Cv11ZFDVugNS2cdTl7soVLP1Nd/7TsKEA gy5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DONU9sxW; 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 dv23si9538187ejb.338.2019.11.11.10.46.30; Mon, 11 Nov 2019 10:46:54 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=DONU9sxW; 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 S1729272AbfKKSpD (ORCPT + 99 others); Mon, 11 Nov 2019 13:45:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:36838 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729801AbfKKSo7 (ORCPT ); Mon, 11 Nov 2019 13:44:59 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 13D4420674; Mon, 11 Nov 2019 18:44:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573497899; bh=G8BUvuGqCsCb7yjKvZ8Fd+KEP4P+4gbsycA/xzIZ9ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DONU9sxWjjwJRrkP7H7xfK9+9LA7BbfQfUDZvuVjfwNyia4+MacpUmhfyYkZqzOkS 9HGr87F1pEpdzXhtT5OciGYmXo1loTjVr73B1UVNP4S4F2rC6dZKdYB/74TBBlkE2e Xs9A9fxyblMcCNtvVnCLyvevOS1SccuRm3ToBMjA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chandana Kishori Chiluveru , Felipe Balbi , Sasha Levin Subject: [PATCH 4.19 091/125] usb: gadget: composite: Fix possible double free memory bug Date: Mon, 11 Nov 2019 19:28:50 +0100 Message-Id: <20191111181452.115930114@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191111181438.945353076@linuxfoundation.org> References: <20191111181438.945353076@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chandana Kishori Chiluveru [ Upstream commit 1c20c89b0421b52b2417bb0f62a611bc669eda1d ] composite_dev_cleanup call from the failure of configfs_composite_bind frees up the cdev->os_desc_req and cdev->req. If the previous calls of bind and unbind is successful these will carry stale values. Consider the below sequence of function calls: configfs_composite_bind() composite_dev_prepare() - Allocate cdev->req, cdev->req->buf composite_os_desc_req_prepare() - Allocate cdev->os_desc_req, cdev->os_desc_req->buf configfs_composite_unbind() composite_dev_cleanup() - free the cdev->os_desc_req->buf and cdev->req->buf Next composition switch configfs_composite_bind() - If it fails goto err_comp_cleanup will call the composite_dev_cleanup() function composite_dev_cleanup() - calls kfree up with the stale values of cdev->req->buf and cdev->os_desc_req from the previous configfs_composite_bind call. The free call on these stale values leads to double free. Hence, Fix this issue by setting request and buffer pointer to NULL after kfree. Signed-off-by: Chandana Kishori Chiluveru Signed-off-by: Felipe Balbi Signed-off-by: Sasha Levin --- drivers/usb/gadget/composite.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c index dfcabadeed01b..33115e19756c6 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -2156,14 +2156,18 @@ void composite_dev_cleanup(struct usb_composite_dev *cdev) usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req); kfree(cdev->os_desc_req->buf); + cdev->os_desc_req->buf = NULL; usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req); + cdev->os_desc_req = NULL; } if (cdev->req) { if (cdev->setup_pending) usb_ep_dequeue(cdev->gadget->ep0, cdev->req); kfree(cdev->req->buf); + cdev->req->buf = NULL; usb_ep_free_request(cdev->gadget->ep0, cdev->req); + cdev->req = NULL; } cdev->next_string_id = 0; device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); -- 2.20.1