From patchwork Fri Feb 2 05:18:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13542073 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2046.outbound.protection.outlook.com [40.107.6.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FC2817545; Fri, 2 Feb 2024 05:18:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.6.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706851132; cv=fail; b=IoVHQnJUbi179qci2zGbAkfKaqoE603pbPEdaqrpP1I89uotApcn+cSW5bt+KiSVPNZ/J0armfoSA9J9didsMp8w88qDZraWrBN2F4VxFm/Tt2BELqI2AGaCx9qCBcU1ZrI6DqjrCIe2uAIsgoeW7N7iqaptlH4e2KC7Yg7Tgms= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706851132; c=relaxed/simple; bh=sjGEkJhHnXgrHk3IBqgyTJ4WadrJYhFXprieOqhc8tM=; h=From:To:Cc:Subject:Date:Message-Id:Content-Type:MIME-Version; b=ebZhGpmMDXUKi6I977aIRihVYKqmGBJYMRrJ5h0tlGT0DbNXBgVr7QbT4mE35d2HTsRKwI2UujeysUk8F8wnQJpCkTYkEjOI+a1MzTiECb62chxytba1bMMAXzeKvTy0yvqMKtAg2VosAa4ogCXc45L/FDUTlMftQq671mCFMfA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b=Cn+WwtLU; arc=fail smtp.client-ip=40.107.6.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="Cn+WwtLU" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UHHL5ifzNQI7K2UDOB1ywJ65gSY14dPiMSCU4c482U7/k9SeHDvQtpzFbgQVdwd46aYzt3IdEjeSbZCC556lONMC6n2f8BRMh/cUQfMRW7o183owhV4A7mtUFxiM1dbmVAWwu1wjP6UMjSdUixPj6DaSO2huU5MZIlYhehkEEeUWwoePZJKobrwTWPaXQe+GvU9ajQ+CbBBtbTEhKyN9g5botoYawpbsAaIV+Qatfe9zV2itWNn8Gxqp5BZ1PeRb8Ei+P4uW8yefu6e9t6y4p0KSq+xXdWqL1oWO5ft3c4QRGGNMUKZTj1RCBH2HI+swog6kPPBlboGBlq3oUkKYzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1M/l/fgmun9nudPOUbQiJbznRCe1rmKWD/tCAu+ncNQ=; b=Y12i6vv5Vc/5omJ8k2L158ijQwhX8/bxhUKGHlKWUJLFfkDdtFDvn1Dae6GtezcoEEbhuEoOXZjhSKdkGd0AEEVIcgipnM03ZfN4EzSrDp/jcCLu1XRo3ixa1Pl2YunjA2rTN+deytSvM7dQuMhAHRilr+jtTIOtVU4cJO9GQ7B/HS7CKqie155DCevaRp80ACWoPDpIDAt2nOj92gcECqjynidTjN/b32lrhMtDfBta6rWXWtWZ0oUKRdV6PY0Imqbq7FqVKri00tp4E8W4oRMRWGN5nVQEHWSihryXbxnNwewb/PzwdWcR8fwiB8DZpQHxQW1zJD4NwTKmNgHEzw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1M/l/fgmun9nudPOUbQiJbznRCe1rmKWD/tCAu+ncNQ=; b=Cn+WwtLUeRIIelPNc0U8lShmKWN5EMu6MBvGl0waNjMWlInL+4Aq+OoryytuvlIPLwpDQSvpoqsAv7R9CfiswVeMTK8jZzNkRur68SQRWtlA00qqPzVpN+Yum5dFdLWrA8EWPr1aZYQjdkRhvsN5QyKZdf78ps4ugq+CFrJzv1E= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by GVXPR04MB9996.eurprd04.prod.outlook.com (2603:10a6:150:117::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.23; Fri, 2 Feb 2024 05:18:47 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::c8b4:5648:8948:e85c]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::c8b4:5648:8948:e85c%3]) with mapi id 15.20.7249.027; Fri, 2 Feb 2024 05:18:47 +0000 From: Frank Li To: Peter Chen , Pawel Laszczak , Roger Quadros , Greg Kroah-Hartman , Felipe Balbi , linux-usb@vger.kernel.org (open list:CADENCE USB3 DRD IP DRIVER), linux-kernel@vger.kernel.org (open list) Cc: imx@lists.linux.dev Subject: [PATCH 1/2] usb: cdns3: fixed memory use after free at cdns3_gadget_ep_disable() Date: Fri, 2 Feb 2024 00:18:28 -0500 Message-Id: <20240202051829.623273-1-Frank.Li@nxp.com> X-Mailer: git-send-email 2.34.1 X-ClientProxiedBy: BYAPR01CA0014.prod.exchangelabs.com (2603:10b6:a02:80::27) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|GVXPR04MB9996:EE_ X-MS-Office365-Filtering-Correlation-Id: 69c35422-9454-4245-962a-08dc23ae6ed2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tIu8BsNwDkKIIY95JubNiwFYV8WBr81zEdbCjOYUyTF82k+51us/JRbnEj2wvfiwMQqlyMEwzhBWDPN83lOGxs7+OzQGUJEpD4XztWxNjotm5u01Dr1hXTT6wpIW+3Cz0nx1W8UCzJVUK2MsVbmM+gxMzmJEY3O4af1pLvnqTwleVdwyP8TT1FhMusF5/h9luQhMrGCcFRw5rNPolLwWVnFiAfY/34m4KvUBrqoPOxrgMO+8+UEVNoE5P9PtI3LSNAamPKZCNl8ldbtAoafKl0+fE842zJPSL8dtFvCZIABVdwYVfoLGdPTOwGxC/OJJoWk+vSdBCfzwq9DEzxnLKmeVrO7gL8lD04WKcFJ8idsgN6f5hSyMWI4qt0Z8hNgSmBvh17eh812TjBeGvpuK5lQIMM4Do1k/kT6pLan3dUeKuJwpCu7vcksEUpnWLdj9aNoxcz1Ki6xwxNVVOtXJUKB5IXer+mcj2BbDiJcKdino0irHiythizQ8YDMgFn2vtXHZEECUjPBhzQ+S9OOo7sY7NuO6Ja8tS6U1IpCvgSdUM/CfuUhSS3G48RfsJZ133qc/WrKZeJMtD7sW9Z6xy6d3boa4JwI31AyrQaAcTpaQLpbqCE5WbHg2gkZD8778 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376002)(366004)(346002)(396003)(136003)(39860400002)(230922051799003)(451199024)(1800799012)(64100799003)(186009)(66556008)(2906002)(5660300002)(86362001)(38100700002)(6486002)(478600001)(36756003)(2616005)(83380400001)(52116002)(1076003)(6506007)(6512007)(38350700005)(26005)(41300700001)(6666004)(316002)(8936002)(8676002)(4326008)(66946007)(110136005)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 86K7WEAw2MR2gBLJhrENjoO/DyscrO7Z+VH0tMoRYeWc68GoGkcUsVibBFdiEfM+8iy6kW6lsfdxodlg0Px6x+xdFT9TBYo+5BWqE+w/j9kYpsFcuPsmhKjgk/nLFxYg1iwMaGG3wvNDyHRC5SKYSJEPX8lE8AUNqj/LxJmeoXFRK8vXJVga3X+34xaGNq8QVforxZI1URSAsbkg+puPh7qVM21phr5Is8YkBymv5su+cN/Q5AakXqCvWxfRl82O4XN1m5nUqHhFrD1onsCPDtIRc0rL13IvgpqVKLdc5k4SIrjWnJ9bPtRAtl1diS4RKUlauM72c1ZmYXTWw4na8TAHl+osF8m6jkjTYFssGzkaIYSk1BmzTDo/3Nf+/OyMPZqyJl2N5qulK+yHB4du5OMTKmGpiASPUOshFiBwO7L8jPC6EJTquIuHV+px4xOOUohRaP47xDQfHI4Ua1E1upxAn0Sz+necGULuOnyASo8/kRHeQGMW8ckMwSiF5o+bOlU+D+gn/SxJTQhiHQmUsomNEpYEUuAXW8pd6EWmnYwSrJ1Fl4Og/JUJg1P47husJYDSm1MVnSfpAaYHe/9DT1jKsNw6/ynZAVKPkCCf+P/dznFO9xZQIyvU+5uc+JhkJo/jGzv9DsvmizjCehrAsAcAtyoXEtgl6Ms/SJA4H2bF4z0N9lBX1Swdx6BMqam1/PhKoBRIm0CnQQIIQRHpPkq7f75vXrIGumWHh9TiouWxZaolWYNNd316sK+HJ9sDJNwl1xWqWE7V6ShwzgXPXAqlLy5UCYbHs99tUyNLaTqCenJr9fyBieo8NglpodDF4wLMIsyQU6znuPykaoryWEG3pzKXMg3nS/QRVqfUNs7hGD00hxsw1L6+qZ2t7/IjsAbEsQfGBS+GSWxpBb4DXeKM6RxOHGFz0HAudsmFOQro+CYRne2I8eCCDbL5/Stf2Z/ipXBrlF5x9U9WSRzTtBq8qxPIJ07UZ75symABpflbEv0qUBZ4jbL/uB5g53dGnkpRXeB1KqiJUG7iZpTrYLVqalVmYZ57BSKYvm5+DxT5bWazy6Ny4aSDMeJhkcso2FLeBejJI0eg6tJnSJa6Nb/4gDghyDle9fYnL1OfnIWIZx1eSDGVlexliLEkj7JcUitgIxV5UQiT48x5l2hyl6lr9P3Yghwq9R4l4q1F8x9TMvAwePXMhRmHeEUkAeheY+MkZ7PB40ZcVJu160NhfI/WDUUKU56VBzOHZM+OTUWBEZ29EQwm2O9xJzXa/z90ONoKQGzbj7VKosQpIU+QB5V2RKZgjQYGyaL1X+iYSZI9sfqRWxGPfRIMWXgN3Xk6QgkyAm/ojXEysNO325XS+YucG0pAZk04ZNJ+quduVFNxOyEX/RYHqp5HtGY8w+V1dEKZY2/Ca5dVZ4M2eEZYTK479zEcOaFGyPhqwD0Z7AhCPFMH/Tgk6T7T7kLajzSr4dovikfrwQM41J4Bfngir2hMAbmHXoeL3QlIdcui+lMrjfvWShspzpyaobDPOgqEM+8ytt2IUxJPm/BkrwdHd59kDHvBudt0XKwpl9s40nwLiwuqZV+/+HesOlrU+ArE X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 69c35422-9454-4245-962a-08dc23ae6ed2 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 05:18:47.1657 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 3NHgR78uPQuPN+1nsOwEQUgvhWoYLA+EkNSvzc2UJheg/8XiMy9KCuvBQSH030tPoozE+VcJ7AvlSqFdD6y41A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXPR04MB9996 ... cdns3_gadget_ep_free_request(&priv_ep->endpoint, &priv_req->request); list_del_init(&priv_req->list); ... 'priv_req' actually free at cdns3_gadget_ep_free_request(). But list_del_init() use priv_req->list after it. [ 1542.642868][ T534] BUG: KFENCE: use-after-free read in __list_del_entry_valid+0x10/0xd4 [ 1542.642868][ T534] [ 1542.653162][ T534] Use-after-free read at 0x000000009ed0ba99 (in kfence-#3): [ 1542.660311][ T534] __list_del_entry_valid+0x10/0xd4 [ 1542.665375][ T534] cdns3_gadget_ep_disable+0x1f8/0x388 [cdns3] [ 1542.671571][ T534] usb_ep_disable+0x44/0xe4 [ 1542.675948][ T534] ffs_func_eps_disable+0x64/0xc8 [ 1542.680839][ T534] ffs_func_set_alt+0x74/0x368 [ 1542.685478][ T534] ffs_func_disable+0x18/0x28 Move list_del_init() before cdns3_gadget_ep_free_request() to resolve this problem. Cc: Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver") Signed-off-by: Frank Li Reviewed-by: Roger Quadros --- drivers/usb/cdns3/cdns3-gadget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c index aeca902ab6cc4..d6723d31fc6e2 100644 --- a/drivers/usb/cdns3/cdns3-gadget.c +++ b/drivers/usb/cdns3/cdns3-gadget.c @@ -2540,11 +2540,11 @@ static int cdns3_gadget_ep_disable(struct usb_ep *ep) while (!list_empty(&priv_ep->wa2_descmiss_req_list)) { priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list); + list_del_init(&priv_req->list); kfree(priv_req->request.buf); cdns3_gadget_ep_free_request(&priv_ep->endpoint, &priv_req->request); - list_del_init(&priv_req->list); --priv_ep->wa2_counter; } From patchwork Fri Feb 2 05:18:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13542074 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2046.outbound.protection.outlook.com [40.107.6.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC0221754C; Fri, 2 Feb 2024 05:18:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.6.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706851134; cv=fail; b=E05qFK9X9ci/JQ0kZa2PcgPDNsLS3+mZWZ6ZsTo/RZyJyyaouj6y7eN7TqetYDiqMc9dY92Etn0iBtB7fMCeYXjfsNS5sNgGOv50rsGan/wTBvJMtuqNjJSKZTugGWGycUkwLXA54PjkOl9AuQs/PsLWmZ4t3TY2Flpdhr2Rhog= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706851134; c=relaxed/simple; bh=lsk/iVFsP9nlmr7jHiv2i49qnNPXmbJmJhEOzGjLckY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=awzj9WAdhtEjqpOrEAkvGhmmoGqwpPeLXHNQThBRf64L1vNFBHoDCDgY1mvZIj3sAA0cSGn2s/NG0rWoBzhvD5g1IMLfbczN6TO8vesO/9SEJw9N8dkT5IZ2C6l8pFItDRRXgHlY3uHd9slH1ymxWfPF+Y8KjBGmqnjiPvREewg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b=UyuNC/yT; arc=fail smtp.client-ip=40.107.6.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="UyuNC/yT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gR4SED6hXIAyh6+f0FuF1UkiiCzEGlEhUq/+pNt1ZgOdLE/uhHj3KHtbm/9o/es5zaw1yyqQJ3HHypDn+lkmpBRCcCfj5FeWgGaW0stOCLngE0CE7W6Gdijljn2ce2vP5+CsmW3nlcNpcq1uD+51PiqU3+YwBWdzJ+gv+d+x0Rc9nKdBgBrRhHULtvxHcGAQA0jkqK7f24UVJhcnPkzVBGyqln58R2l2Z4C15ieSfHzjcfUo50JjGIzg1mk/PImWIED6uEOZ9AHFI9ODQNxefImLb0VoCPaiNF5jUpWcV1nFb9UyavGRSj9D9Vh7kCvfNtoYki8ZQFBaJr2GUvYTKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3NJIn2VbuszeDtrqGFo1tgAtJhNFZHjI0jcBiXg0rYY=; b=FkUdcX4RpixmycmqJQ9DjNT4TYoI/Zx2WVD6c0/t9v+eg68EcU0stmZoFysx0w/U4xCFZs7oiu4IV58gtFdGRpBa0ZCNEEdZtkJfEZugRKPuilAq+nYK4wK05OkaduIXQiyZ7AYwq4BYhf5iPgBZEod94M+WQJ11jHppmchnS9GwR5do4KpNLhMxaBJgrWEnb2H5nfn1p0XgXoigAELcpioSj0CTgjgHI3TVxzEtcEaJj6/4p45by9F9eUP6Y23XVpD1Eu7j3TDv3qHTgorQFLwKAocRASAT9b15WehseqwOR+mvpZoRtNOR7cSywpExc6vmEu/mAlW+xu3uVxFGZw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3NJIn2VbuszeDtrqGFo1tgAtJhNFZHjI0jcBiXg0rYY=; b=UyuNC/yTNrHkOmS9bDPAuvl0gtNsoH776ndk6q9vu1YMOXecpOryYB1AlgQoS3oEvwHAFHkBNVn/f/g+UWI4zy986ESIsfjIuKcLIT9qTYIwMgt8Zjlc2Z85KWJkGz5flDxFxBd+qLTFsT7ETmqxUR5rYTZlrl3pgpwtRmDgAy0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by GVXPR04MB9996.eurprd04.prod.outlook.com (2603:10a6:150:117::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.23; Fri, 2 Feb 2024 05:18:49 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::c8b4:5648:8948:e85c]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::c8b4:5648:8948:e85c%3]) with mapi id 15.20.7249.027; Fri, 2 Feb 2024 05:18:49 +0000 From: Frank Li To: Peter Chen , Pawel Laszczak , Roger Quadros , Greg Kroah-Hartman , Felipe Balbi , linux-usb@vger.kernel.org (open list:CADENCE USB3 DRD IP DRIVER), linux-kernel@vger.kernel.org (open list) Cc: imx@lists.linux.dev Subject: [PATCH 2/2] usb: cdns3: fix memory double free when handle zero package Date: Fri, 2 Feb 2024 00:18:29 -0500 Message-Id: <20240202051829.623273-2-Frank.Li@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202051829.623273-1-Frank.Li@nxp.com> References: <20240202051829.623273-1-Frank.Li@nxp.com> X-ClientProxiedBy: BYAPR01CA0014.prod.exchangelabs.com (2603:10b6:a02:80::27) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|GVXPR04MB9996:EE_ X-MS-Office365-Filtering-Correlation-Id: 376e8b67-5392-4962-7604-08dc23ae706a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: La23VpTt84hSfsl2fkZ0LrxP2Y2PpjFVFi8MdU0aTAILtxige3PcU/E5OZ/P0VkzM+dtpwF0jztXsIXL1ux5LC8XEpO5kqp+S8DwHOTE3ojLzjSVFfHDs8ud/LlJ/xqeGztp9qB81TigFm9iV6Hb8ysStC+YQf+cdE2qODA0XXJffPgTsRX2tUHxSbG1sedtcejMc4WgajwDlTzZgvk3K3FAmbBJvmKkD3R6WaRbPPn1JF1d+iDKPDma64zAcPPbAw/BYxFf4FrfWBJECtbgzQU/EFn10OILpwUm0dzt3aun17LJu+AlvHtL02vU5XxXr3UUswBGuGgJpe/6NrwM26ai9AcYGf/fh11FCa2nXRg7e/USQNX4J4UszTUVp2od2HX1tsctNNfYTdRljnjVxixek0IyIaQVkSoIPLzCNFRxJYMjiatNEL03YKtk0wqFGIsKTKTEGQ5cupAshBWniLWKG0mMfNFNTZURcWtyrO+gbzXWakfootDne1ahVnrQ8tIDbEXPXLuaoNBoEuFtJw0QIf41Mzmh+pNu6fS90rs57iyREqrPrUnLW0Ud5L9vGPbLT3zmsdr3C5ixboyh3nEB+FV56s/oMSEGyfRnlKOtXcE5pCNFbU1zBxDlg5Py X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376002)(366004)(346002)(396003)(136003)(39860400002)(230922051799003)(451199024)(1800799012)(64100799003)(186009)(66556008)(2906002)(5660300002)(86362001)(38100700002)(6486002)(478600001)(36756003)(2616005)(83380400001)(52116002)(1076003)(6506007)(6512007)(38350700005)(26005)(41300700001)(6666004)(316002)(8936002)(8676002)(4326008)(66946007)(110136005)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 46CsLKynPyvzVsCuK6wglCArFbeXaUIQwiZXdo6oGrDF07SvGdLUmweWt120aIAbavVawYuUQmJo2JM2vggVwTTq38wBY8JiB//hvxX84Kvk4YCqMu7tdzJbLQT8aO89RCx/D2SwtMp2SjRk43VL6HFFffeLFhE4a7RrFzSEuxVAi30AEx5mLWXjGzBdUV4E8jUOcdA2MRINAA6cgGRXtogwPidUAp/BCIA2DfXthArhrktPqqGbsYsqmQ6yrJtqsBvwfENHWaxomow2d0mH736rNceROKaPkqNMk+Dc9lkWx6Y5CzBadyImduN/3EP7rJ9nO8TfwFUU5RldYD1j4qKcNx4EdXCcSUapHxyaFWNycQ+UN2C1wv6bs3AZoGUrJX7Q2rG/p2HGwDbSk2mP//ydk6DWdabp3LOutm0K+WjCUmhvwwJ6k2yAxfpkq123OsLUW8EsQupooT4n7+LEPyaREMdi65vuaF8eSFwX0h9Q70xEsaWL3SUmHjrzNUgj1spNf6rJVLTpFlTrsgE93QNdiOr6E4rcdPfhBzJSKDyfiax9o4JcTpNruJK+f1jfNLsozddRpFe7xAKqn9WDNotZcLj/8QqzB7V5MYVnmHR7vftn/x9+EvJmD5EZtFFknsTuy0hvHFJ/eIJe1mdJfgM1vaR+geKEKEc7nBskq61O07PQKyUP4pgpbnJRyCwgylmo1UlHnoZLQcEFPh0iWLB4swwgqgblY23cxQ5y1/B+8VZqU+XN1YWqP8wZj0kK3Mfth3o9UidNpcHuOLyRwTCtG9h1jDi9bql33BzDNQjxcDiT1UKBMPv8ojPDyfpeH3fz5JTfHLgy9Tpb0zhsv2k0wDYFFGh5WuIA9Ephv4Upu5qJNBG3TInnX6TSWPNod4+fhN9aGb62I8Mkx6SIjycx3vpxCk720axVR3rtOG0YoNe4PtK4bNt5VFRuJ9rMnydPc9wG9r7vsErnSn4n/EkPMXFRBR+v9FGW3dLHrrEVUp6sPdUmaw0ftox9hb/myYWbO6px9VMQDy7jzTVtpZJBu6WxYS4e5xrn1MdVE3QrWLelOO8ViFF/JgRdPpmOYMYasoAah23wbpAI5Oh+9a2ynOR6wUEMA5MphGgBVGnzkVFMHve20c0W5Yg6jlYKRPs5dMsST83neF+SuC4CMtYqFAw0n87Y2AEC2bhKzqcxfZLfVjlyo+8pJGcm+j11NpjYpLKTPqx4uHbXtoSDTOvs6Rzwf2a9+OMkPJmWMgv0DQvKG+43nwdgoZI/NYaXYk+VTlwzjqPurdSV9Mf+Gh/dWvXJmAMzhDU+BBk+QNe6TbBJeWQJaNJULgyDh46X/MQyHqjkuRpobuoCEid8/inZv30O8YdlwNGlWGwIGSjCAci6u6rCPDpIQLWFXLouVcl2SOoeq/5svv5j/oYD9vnt/QOMnyLUeOti49zyucbEcnRQB1Zy351x22l8kE8N24rcIZPGOQdg9RfgPpDxtDIyM/gUhiH6iUmoMc17cyrOFItPiy3+mURzn+uQ0hP6o7UHni02yeGgTICbzWea5nc5UQuHwDVReLmD4ivILCIILjHIjsAsCuUbEQSsLR9I X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 376e8b67-5392-4962-7604-08dc23ae706a X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 05:18:49.8477 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: o3sCC72JGThI4zKWyZjHZu/M1ibEXV7c0VUSOjEqW5KatfbxBeGPdqy48Moa3Y65j5dmGgfK1JEOz/R40xtXOA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXPR04MB9996 829 if (request->complete) { 830 spin_unlock(&priv_dev->lock); 831 usb_gadget_giveback_request(&priv_ep->endpoint, 832 request); 833 spin_lock(&priv_dev->lock); 834 } 835 836 if (request->buf == priv_dev->zlp_buf) 837 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request); Driver append an additional zero package request when queue a package, which length mod max package size is 0. When transfer complete, run to line 831, usb_gadget_giveback_request() will free this requestion. 836 condition is true, so cdns3_gadget_ep_free_request() free this request again. Log: [ 1920.140696][ T150] BUG: KFENCE: use-after-free read in cdns3_gadget_giveback+0x134/0x2c0 [cdns3] [ 1920.140696][ T150] [ 1920.151837][ T150] Use-after-free read at 0x000000003d1cd10b (in kfence-#36): [ 1920.159082][ T150] cdns3_gadget_giveback+0x134/0x2c0 [cdns3] [ 1920.164988][ T150] cdns3_transfer_completed+0x438/0x5f8 [cdns3] Add check at line 829, skip call usb_gadget_giveback_request() if it is additional zero length package request. Needn't call usb_gadget_giveback_request() because it is allocated in this driver. Cc: Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver") Signed-off-by: Frank Li --- drivers/usb/cdns3/cdns3-gadget.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c index d6723d31fc6e2..fd1beb10bba72 100644 --- a/drivers/usb/cdns3/cdns3-gadget.c +++ b/drivers/usb/cdns3/cdns3-gadget.c @@ -828,7 +828,11 @@ void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep, return; } - if (request->complete) { + /* + * zlp request is appended by driver, needn't call usb_gadget_giveback_request() to notify + * gadget composite driver. + */ + if (request->complete && request->buf != priv_dev->zlp_buf) { spin_unlock(&priv_dev->lock); usb_gadget_giveback_request(&priv_ep->endpoint, request);