Received: by 10.223.185.116 with SMTP id b49csp6343080wrg; Wed, 28 Feb 2018 07:54:28 -0800 (PST) X-Google-Smtp-Source: AH8x226uPH0czhyuOMNzUoMDg5t1+KBgGpVEjTfD25GLUr38FVgsxQvryHMLOcDfDqMljiPsSAcf X-Received: by 10.98.3.131 with SMTP id 125mr18292006pfd.65.1519833267888; Wed, 28 Feb 2018 07:54:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519833267; cv=none; d=google.com; s=arc-20160816; b=PQEHs+rd4upIjQUh5NBzQggeATC4SYjSUDszq062A7XqY8ZF1Al2d0oLC3MY18UXFc 9l3UaiCsDY3yTED3dLMOMxe+j+cQ2/qB84BMINcGRFq4cYBIGgFhZ3FSfvEC+IF429pP U9vdJ+Tro7xVj4c6wAPMZuXAstc5XV9yrbQE3QVJfs+8Gpvbh6VcycOmEQG+H4r8dGKj 3y1qHV4/6DvLd4HQkfvW8KQTDOJcZYD7hjE15zWfSsuAkPYaic4FGQ8IA3WWlSDSQmdY TyqgTOMubIljLqiEQi+Z6cDd2q9HTEJhj60KzgTgdIM6ik4pZwhXWlXitz6i0hT8JjMJ oGqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=MOFL4Nj8ENYUMR9+0g2RqeVMuBCkucSJ7D+Ys4SNTao=; b=DqdFgwkfS9GzWsBZwvcAEFNP1ku6bMVk/ZS7QlQINVFr7gc5mY+EjgZ8DQ4ZpFeLGR VaacmjjO5izxTBv03BN9AFQlxgscxelJ9HhWbFAnErqSqWoxN7G8UVzk7+PffgNmtcpL lcAxTGWGDLghLuveHUnCMAcaPl1XMgAcNFUOQF6+zje7cFzSPDJbGRaJd2J4GehjhTgw 3rNB7GdDqDsUp+4TtdIfMih15R3Mj17+aeb5+CgJUN53GhO7u48f3dR0zRO1KH58mIH0 Jnm5A2QEP5ohqYwD/InUMHkVeAssJrx03dgZWzMmVy4vNiCjp3BVCWkN0SqAV8Hcvs3U ecPw== 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 d5si1163900pgf.616.2018.02.28.07.54.12; Wed, 28 Feb 2018 07:54:27 -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; 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 S934066AbeB1Pxh (ORCPT + 99 others); Wed, 28 Feb 2018 10:53:37 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:34461 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934041AbeB1Pxf (ORCPT ); Wed, 28 Feb 2018 10:53:35 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1er3Yq-0006Xk-6w; Wed, 28 Feb 2018 15:22:28 +0000 Received: from ben by deadeye with local (Exim 4.90_1) (envelope-from ) id 1er3Yj-0000F8-Dw; Wed, 28 Feb 2018 15:22:21 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Felipe Balbi" , "Peter Chen" , "Greg Kroah-Hartman" , "Fengguang Wu" , "Alan Stern" Date: Wed, 28 Feb 2018 15:20:18 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 193/254] USB: UDC core: fix double-free in usb_add_gadget_udc_release In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.55-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Alan Stern commit 7ae2c3c280db183ca9ada2675c34ec2f7378abfa upstream. The error-handling pathways in usb_add_gadget_udc_release() are messed up. Aside from the uninformative statement labels, they can deallocate the udc structure after calling put_device(), which is a double-free. This was observed by KASAN in automatic testing. This patch cleans up the routine. It preserves the requirement that when any failure occurs, we call put_device(&gadget->dev). Signed-off-by: Alan Stern Reported-by: Fengguang Wu Reviewed-by: Peter Chen Acked-by: Felipe Balbi Signed-off-by: Greg Kroah-Hartman [bwh: Backported to 3.16: - The err5 path is not presnet - Adjust filename] Signed-off-by: Ben Hutchings --- --- a/drivers/usb/gadget/udc-core.c +++ b/drivers/usb/gadget/udc-core.c @@ -219,11 +219,7 @@ int usb_add_gadget_udc_release(struct de udc = kzalloc(sizeof(*udc), GFP_KERNEL); if (!udc) - goto err1; - - ret = device_add(&gadget->dev); - if (ret) - goto err2; + goto err_put_gadget; device_initialize(&udc->dev); udc->dev.release = usb_udc_release; @@ -232,7 +228,11 @@ int usb_add_gadget_udc_release(struct de udc->dev.parent = parent; ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); if (ret) - goto err3; + goto err_put_udc; + + ret = device_add(&gadget->dev); + if (ret) + goto err_put_udc; udc->gadget = gadget; @@ -241,7 +241,7 @@ int usb_add_gadget_udc_release(struct de ret = device_add(&udc->dev); if (ret) - goto err4; + goto err_unlist_udc; usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); @@ -249,18 +249,16 @@ int usb_add_gadget_udc_release(struct de return 0; -err4: + err_unlist_udc: list_del(&udc->list); mutex_unlock(&udc_lock); -err3: - put_device(&udc->dev); device_del(&gadget->dev); -err2: - kfree(udc); + err_put_udc: + put_device(&udc->dev); -err1: + err_put_gadget: put_device(&gadget->dev); return ret; }