Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1024965ybe; Wed, 4 Sep 2019 11:19:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqx/53a8+2t6HOouJUXoq9e2eYF9lfZaEB3idg3QPRD7eKwCMnP3ualY0GcMj5bh2cMiWQmN X-Received: by 2002:a17:902:8484:: with SMTP id c4mr41841140plo.223.1567621171389; Wed, 04 Sep 2019 11:19:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567621171; cv=none; d=google.com; s=arc-20160816; b=Kze/TOuEERdyarWNrvfe8S5tMu7LqX1KDsg07C3e7tMGV7ImvRKBoi/EB9FAKeX864 DVPSPv/oSQCxAohpwdj47uMWADxDVJIbBEv0gRTkA7DqTqz66eFYJaetyZrVUXXTip4N zTyGBjX4M9mD51f0bnXfyA2P+d65XyAZ4lfn0QDdwzqsBV7fxk7z5IOOc55BQkL3172m 4dooVd1shJ5Yby2HMC2ERyljacXdxsw6jd834luFC198W8paYa+4T3q4R+ltzxt6zsTZ M9m1w6dXgHwkCzOxie/2oqpEWMLd1fDoLnRPwHH7L5L5xEvXlDKfx5eClYHmztcgWWkT ikdw== 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=OLb6OhlKnbmPuaKs3jrF83rURnHUdPrZw0+jkjcZtrQ=; b=IAxnm4ZPMP/n5wbU4U8HpLmN+Sr00ZqyZfLGIX1paNrf66BL+cGV0ufQ7jhuPM3iVf EOb+8KUMWhxPs4LKg+W28jlTaWMgq5uX3QUI1h6ya5THVgoyLuY89zI9hORa7yJQ15ww iTe5D3apmK6jhONMRvEpO3CG6WgEnlksFLH3Hw6u/J2Ntg+iaTngMZxWWYCnEmWiQz55 kQI9dGT73ykG8nKsL/GKPsxl/8q2i8zvo0LYJk2THPUHPKj36j7YJWQEXuy0OkHJ0WBD pNhO59CbLhB6Gq/eShiSM9MNZisB9S3VxvBJukNdb3JM+j2meGCNNY+Hx5SEjxLYidut 5rgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eRRtUhN+; 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 u190si17684129pgd.547.2019.09.04.11.19.15; Wed, 04 Sep 2019 11:19:31 -0700 (PDT) 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=eRRtUhN+; 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 S2389545AbfIDSM3 (ORCPT + 99 others); Wed, 4 Sep 2019 14:12:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:56884 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388962AbfIDSM1 (ORCPT ); Wed, 4 Sep 2019 14:12:27 -0400 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 01F7E2341D; Wed, 4 Sep 2019 18:12:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620747; bh=RwAewO1EypVLVJLkigioLHmRtyVfLIej5e1mBvm8MZg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eRRtUhN+vuXHBulMqL1djbluABqZhiL8CAnukGpRRN3SIJVkgH6rql70N2CVfLvpV C9ljZ4orasK1MEnsskSPutLPa5VY+XRlTf3YiwA7qnDBks1rzTThKOyRbBS0o3/uLh 1lr0GOZdZWEDrVxpLzM+wZanCNzig4+pFyupnBFU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+d232cca6ec42c2edb3fc@syzkaller.appspotmail.com, Oliver Neukum Subject: [PATCH 5.2 080/143] USB: cdc-wdm: fix race between write and disconnect due to flag abuse Date: Wed, 4 Sep 2019 19:53:43 +0200 Message-Id: <20190904175317.189160805@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175314.206239922@linuxfoundation.org> References: <20190904175314.206239922@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: Oliver Neukum commit 1426bd2c9f7e3126e2678e7469dca9fd9fc6dd3e upstream. In case of a disconnect an ongoing flush() has to be made fail. Nevertheless we cannot be sure that any pending URB has already finished, so although they will never succeed, they still must not be touched. The clean solution for this is to check for WDM_IN_USE and WDM_DISCONNECTED in flush(). There is no point in ever clearing WDM_IN_USE, as no further writes make sense. The issue is as old as the driver. Fixes: afba937e540c9 ("USB: CDC WDM driver") Reported-by: syzbot+d232cca6ec42c2edb3fc@syzkaller.appspotmail.com Signed-off-by: Oliver Neukum Cc: stable Link: https://lore.kernel.org/r/20190827103436.21143-1-oneukum@suse.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/class/cdc-wdm.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -587,10 +587,20 @@ static int wdm_flush(struct file *file, { struct wdm_device *desc = file->private_data; - wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); + wait_event(desc->wait, + /* + * needs both flags. We cannot do with one + * because resetting it would cause a race + * with write() yet we need to signal + * a disconnect + */ + !test_bit(WDM_IN_USE, &desc->flags) || + test_bit(WDM_DISCONNECTING, &desc->flags)); /* cannot dereference desc->intf if WDM_DISCONNECTING */ - if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags)) + if (test_bit(WDM_DISCONNECTING, &desc->flags)) + return -ENODEV; + if (desc->werr < 0) dev_err(&desc->intf->dev, "Error in flush path: %d\n", desc->werr); @@ -974,8 +984,6 @@ static void wdm_disconnect(struct usb_in spin_lock_irqsave(&desc->iuspin, flags); set_bit(WDM_DISCONNECTING, &desc->flags); set_bit(WDM_READ, &desc->flags); - /* to terminate pending flushes */ - clear_bit(WDM_IN_USE, &desc->flags); spin_unlock_irqrestore(&desc->iuspin, flags); wake_up_all(&desc->wait); mutex_lock(&desc->rlock);