Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3023748ybz; Sun, 19 Apr 2020 15:45:17 -0700 (PDT) X-Google-Smtp-Source: APiQypL7YG1U2d1+OxzaGYW6QRA76XiFTlN9rfWi9VRbZaScELMPW16qEU9zANTE88ajxVmuxXMo X-Received: by 2002:a17:906:60d4:: with SMTP id f20mr13143199ejk.209.1587336317739; Sun, 19 Apr 2020 15:45:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587336317; cv=none; d=google.com; s=arc-20160816; b=chxb6IjanlRv6DCIhb+Fg7RVaaN+bTOCYyTg1/nDCF1oGIEbneVawJOwb2ueHGj2tg BZT9t8I1J5ziuGMI63O5+3CEUvv6xh36lkWoInA1n2noRG1dsIXP9CJMBXsykAMqujiU nEnqvmJp9olObfIZruMlok8UbtGF2yX83llaAevTMwenEn/9SMX/c17hc+8BGoutoZ6T 70SyTnvJP9gjHzWi0Bz8ZbvGEf2tVR6TNfj+KUBeT2YZeE6bCsI+wZ5QGPTDyOIGoeuI TNxB9bWAIwvoejiwxCs+o3mNWgbtL2O42y19LND5ouzDZuwm3iX1avFmg5IucIdpGB5s 6JSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date; bh=d2K3tQ+rwF8UQL73lQ66LQH+Jji2GqFfxRhcaj3LijE=; b=vw9/yzsVod5mDAqUmAZ2VeJO8TdH8dhvK/EUHmktpI/ltf8Tczk7GhqvkzLkZogETm Y/lorGHM+yDtaP1aHTHT/uQPECfyd7IbAhBb6SwHn1k7+PfiaerQjlb5ee6S+0LgbzBi znt90oJisb0v51CtRDvmMoiLrIlDuTc9iAI6v6KoaNQmv5LUwcIWK0TabpRN1VSxAvN5 8qx6U4LLanaLBYETP4M5jE9BhU2UL4uK8HE7O22vAkt2pAgSpkcZ6/qECB/mXDCfR0V+ FoHch18eBKXxYE4C5h+CIgsMGACcc/w+vG6Dgg9GZuU++Gtd4lsW7rCjh6IZP55rAiG3 MPoA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x4si19016665edc.458.2020.04.19.15.44.41; Sun, 19 Apr 2020 15:45:17 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725987AbgDSWm1 (ORCPT + 99 others); Sun, 19 Apr 2020 18:42:27 -0400 Received: from netrider.rowland.org ([192.131.102.5]:41821 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1725905AbgDSWm0 (ORCPT ); Sun, 19 Apr 2020 18:42:26 -0400 Received: (qmail 29224 invoked by uid 500); 19 Apr 2020 18:42:25 -0400 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 19 Apr 2020 18:42:25 -0400 Date: Sun, 19 Apr 2020 18:42:25 -0400 (EDT) From: Alan Stern X-X-Sender: stern@netrider.rowland.org To: Dmitry Torokhov , Jiri Kosina cc: Julian Squires , Hans de Goede , Benjamin Tissoires , syzbot , , , , , Kernel development list , USB list , , Ping Cheng , , Subject: Re: KASAN: use-after-free Read in usbhid_close (3) In-Reply-To: <20200419171855.GJ166864@dtor-ws> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 19 Apr 2020, Dmitry Torokhov wrote: > On Sun, Apr 19, 2020 at 10:07:34AM -0400, Alan Stern wrote: > > On Sat, 18 Apr 2020, Dmitry Torokhov wrote: > > > > > On Sat, Apr 18, 2020 at 09:09:44PM -0700, Dmitry Torokhov wrote: > > > > Hi Alan, > > > > > > > > On Sat, Apr 18, 2020 at 10:16:32PM -0400, Alan Stern wrote: > > > > > linux-input people: > > > > > > > > > > syzbot has found a bug related to USB/HID/input, and I have narrowed it > > > > > down to the wacom driver. As far as I can tell, the problem is caused > > > > > the fact that drivers/hid/wacom_sys.c calls input_register_device() > > > > > in several places, but it never calls input_unregister_device(). > > > > > > > > > > I know very little about the input subsystem, but this certainly seems > > > > > like a bug. > > > > > > > > Wacom driver uses devm_input_allocate_device(), so unregister should > > > > happen automatically on device removal once we exit wacom_probe(). > > > > > > > > > > > > > > When the device is unplugged, the disconnect pathway doesn't call > > > > > hid_hw_close(). That routine doesn't get called until the user closes > > > > > the device file (which can be long after the device is gone and > > > > > hid_hw_stop() has run). Then usbhid_close() gets a use-after-free > > > > > error when it tries to access data structures that were deallocated by > > > > > usbhid_stop(). No doubt there are other problems too, but this is > > > > > the one that syzbot found. > > > > > > > > Unregistering the input device should result in calling wacom_close() > > > > (if device was previously opened), which, as far as I can tell, calls > > > > hid_hw_close(). > > > > > > > > I wonder if it is valid to call hid_hw_stop() before hid_hw_close()? > > > > No, it isn't. If it were, for example, why would evdev_disconnect() -> > > evdev_cleanup() need to call input_close_device()? > > Because input and HID are not the same. For input, when we attempt to > unregister an input device we will go through all attached input > handlers (like evdev) and if they believe they have the device open they > will attempt to close it. How close is implemented is up to particular > driver. > > I am not sure about HID implementation details, but I could envision > transports where you can tell the transport that you no longer want > events to be delivered to you ("close") vs you want to disable hardware > ("stop") and support any order of them. Jiri, you should know: Are HID drivers supposed to work okay when the ->close callback is issued after (or concurrently with) the ->stop callback? The actual bug found by syzbot was a race between those two routines in usbhid. > > And why would > > usbhid_disconnect() deallocate the usbhid structure which usbhid_stop() > > accesses? > > This happens only after we return from hid_destroy_device(), so > even in the presence of devm I'd expect that all devm-related stuff > instantiated by hid-wacom would have been completed before we get back > to usbhid_disconnect(). > > Can we validate that calls to wacom_close() happen? I could find out if you think it's important. In the syzbot tests, the crash occurs before wacom_close() is called. Alan Stern