Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2279616pxb; Mon, 11 Jan 2021 05:56:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJyhT1HO3YLguBbSm+QtSwUAtCfXB5EGlyyr3ySUcHNDCyEXdxiwGqZeSybkrKXuNZWib3Wy X-Received: by 2002:a17:906:c09:: with SMTP id s9mr10793632ejf.539.1610373413042; Mon, 11 Jan 2021 05:56:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610373413; cv=none; d=google.com; s=arc-20160816; b=mdB0cLAsHvY4+33/J0L89ONu2StY+2n4b0cFB+V4ZNlNlLVY002EhjEXVJGU1GcUTH vdmy58Ko/BRVpJ5rlVgxO+ScFhM+vjD7NJ82oLW5PWBxa6eO6lemQTNyQnG9Bw25qzcr V0lNaco8NdloYgZY1qn2zZsjcZDy/2uTrgsL5+HMYg873kC/MPS6raJ7Dbg+kqiHOewv JiTaCJL6JLC8Q/8iWIgRtcLbPGEKQskljtA0+JNEgeWb2DV+48B4jFGvUVQo5GLg0yIe 8cpJE26kjWP/e0tCGXqUZI6IrbegKsARF+smf8gHeG4XUE31lapC7h+0/SNQ+F//pndW /7WA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sq7nVe/FFKiFvQ+i9f4CBTBFQLAvCGnRCxv/Bf2h7L8=; b=LXmRdH4awg8WvggTGSyLknRCT+vWWP2XHtOTkhYzd8i5xTVw/nzMFh32DdX/+IsDoP ls+4efcOabnlNT7S+crz/O4XCkLbEeCwMpFoApRuMU3c0hnvSpTUGp6YX6R+l/n1sdpI MHaupAxCqISGbSGnZkAFF2LX0w3hscd6+3QqdR0SAShcM+NXiF9MSNP1HlkqpFZmBkCT 6DXyYhDXYVy7mUY5Xh+Oj/2PsPvrCYup4lTkIKS0f7HICk11ERX9X4p5w+2xmXh98enG LLJIfjMh0QrSRf1pG1tRwSpAdCQ2ZPa3YpSd051fDPB5hSh8TbRRSDYbg2rg5vYyiWNe q9fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WODxA2yU; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dt5si6793478ejb.639.2021.01.11.05.56.28; Mon, 11 Jan 2021 05:56:53 -0800 (PST) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WODxA2yU; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730399AbhAKNxx (ORCPT + 99 others); Mon, 11 Jan 2021 08:53:53 -0500 Received: from mail.kernel.org ([198.145.29.99]:55254 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731048AbhAKNIZ (ORCPT ); Mon, 11 Jan 2021 08:08:25 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 05AEE2255F; Mon, 11 Jan 2021 13:08:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610370489; bh=RMD2vaXVrjkPvjcRICMHAwtmJRNH0mjnsLDPtwWFHYs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WODxA2yU3q1MfMZJmwtIm6YpQPROdhJu1UOGnw9qLpCALQijc7ntuhrSdyFOExtob dR6xNdhEXQ5MloLEDojWiHMMyCJl1yApm3InXuAzUIQDQ7Pl0bXQl51ka1M+lj4eTe D0RA/FnzhAWlo66MjHQIUbgob6dqaeRZLiFNojo4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Tetsuo Handa Subject: [PATCH 4.19 45/77] USB: cdc-wdm: Fix use after free in service_outstanding_interrupt(). Date: Mon, 11 Jan 2021 14:01:54 +0100 Message-Id: <20210111130038.572212124@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210111130036.414620026@linuxfoundation.org> References: <20210111130036.414620026@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tetsuo Handa commit 5e5ff0b4b6bcb4d17b7a26ec8bcfc7dd4651684f upstream. syzbot is reporting UAF at usb_submit_urb() [1], for service_outstanding_interrupt() is not checking WDM_DISCONNECTING before calling usb_submit_urb(). Close the race by doing same checks wdm_read() does upon retry. Also, while wdm_read() checks WDM_DISCONNECTING with desc->rlock held, service_interrupt_work() does not hold desc->rlock. Thus, it is possible that usb_submit_urb() is called from service_outstanding_interrupt() from service_interrupt_work() after WDM_DISCONNECTING was set and kill_urbs() from wdm_disconnect() completed. Thus, move kill_urbs() in wdm_disconnect() to after cancel_work_sync() (which makes sure that service_interrupt_work() is no longer running) completed. Although it seems to be safe to dereference desc->intf->dev in service_outstanding_interrupt() even if WDM_DISCONNECTING was already set because desc->rlock or cancel_work_sync() prevents wdm_disconnect() from reaching list_del() before service_outstanding_interrupt() completes, let's not emit error message if WDM_DISCONNECTING is set by wdm_disconnect() while usb_submit_urb() is in progress. [1] https://syzkaller.appspot.com/bug?extid=9e04e2df4a32fb661daf Reported-by: syzbot Signed-off-by: Tetsuo Handa Cc: stable Link: https://lore.kernel.org/r/620e2ee0-b9a3-dbda-a25b-a93e0ed03ec5@i-love.sakura.ne.jp Signed-off-by: Greg Kroah-Hartman --- drivers/usb/class/cdc-wdm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -465,13 +465,23 @@ static int service_outstanding_interrupt if (!desc->resp_count || !--desc->resp_count) goto out; + if (test_bit(WDM_DISCONNECTING, &desc->flags)) { + rv = -ENODEV; + goto out; + } + if (test_bit(WDM_RESETTING, &desc->flags)) { + rv = -EIO; + goto out; + } + set_bit(WDM_RESPONDING, &desc->flags); spin_unlock_irq(&desc->iuspin); rv = usb_submit_urb(desc->response, GFP_KERNEL); spin_lock_irq(&desc->iuspin); if (rv) { - dev_err(&desc->intf->dev, - "usb_submit_urb failed with result %d\n", rv); + if (!test_bit(WDM_DISCONNECTING, &desc->flags)) + dev_err(&desc->intf->dev, + "usb_submit_urb failed with result %d\n", rv); /* make sure the next notification trigger a submit */ clear_bit(WDM_RESPONDING, &desc->flags); @@ -1026,9 +1036,9 @@ static void wdm_disconnect(struct usb_in wake_up_all(&desc->wait); mutex_lock(&desc->rlock); mutex_lock(&desc->wlock); - kill_urbs(desc); cancel_work_sync(&desc->rxwork); cancel_work_sync(&desc->service_outs_intr); + kill_urbs(desc); mutex_unlock(&desc->wlock); mutex_unlock(&desc->rlock);