Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2820034ybz; Sun, 19 Apr 2020 10:23:02 -0700 (PDT) X-Google-Smtp-Source: APiQypLiYMabHXWkbM2qGBJrItHLM5FHp3BERBmZUtoKW6DtYYTOLYSrLROPJhRTbwes48e20PQW X-Received: by 2002:a17:906:4048:: with SMTP id y8mr12522002ejj.258.1587316981892; Sun, 19 Apr 2020 10:23:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587316981; cv=none; d=google.com; s=arc-20160816; b=TzJW4NU/KyFuA/CkD307N9MYkEhQd9AvfTdFefAUBkVZ4BcoNsA24jw6JQ7ILdtxO6 /XEu+1tDmvUypbpAUU30xD/ZyvH/FqSfsRcZ62BSC6pQ2/h56jaVhX5QiKWeFFbgyDBG 0uQUfVtrhpaHpylHjB4QMeGbYhBCkbVqq77BaeJz9E+c/6+NO0WkHxW8jRrtpHFf72ah jKwCLkd8Kd3nPxDUyIY5nOJUdOXN1WLCgBXNT5q9bdEeOQcjD/6q+RncZueTYiUc5szC ZYVYWVUaB/qmOO8vhSv4Kykxbuc/38TIThpLXc53ml4BLChqZOBiQiHGiJeS4s6GkDlB WBCQ== 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:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=qapmg8H0xsf8FtuVuTZYqLZSXqZnlrHTeBzdG8nPoxQ=; b=eQ7yopy8d1gFVxRh+0bBmyxvT27Tfg+hqDOstWti5Fyy2sHSImJsxFpBBnpABuJbEM AjHgNoeGLSEvutHvUF5ghSSMgpYt5Be9USfaSm+hmrIKk9IkEUfd7bGxHlPLFn8+Ozsw A19zlGicHYU9BMT1x+ztz9qZEhWr/+bP6JgCcGQ9ATVbnIw5foYe5coHaXZxJzgF4sEw 0ccnmalRppPQwu7ExFWCJH93PuXtktly9EkXihIr3UgMmAI/GpHZXA3vXch3RrWEsmXu ImxIRt084QD0C2PLjXClkkAlxpdksr365yLu/eJsDqneTfnsnMeRQZNjQy0k04c1Hurp yrqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZLj0QO7J; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w21si8095809edt.595.2020.04.19.10.22.38; Sun, 19 Apr 2020 10:23:01 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZLj0QO7J; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726465AbgDSRS7 (ORCPT + 99 others); Sun, 19 Apr 2020 13:18:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725932AbgDSRS6 (ORCPT ); Sun, 19 Apr 2020 13:18:58 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D180C061A0C; Sun, 19 Apr 2020 10:18:58 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id 145so1506609pfw.13; Sun, 19 Apr 2020 10:18:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=qapmg8H0xsf8FtuVuTZYqLZSXqZnlrHTeBzdG8nPoxQ=; b=ZLj0QO7JBhhyg2TkZzrF+7SNq3YZXndE/CqFNKiGMtdjt9k32EAhBGxNkIZIIP7oqu iAX9gedTI9rI8ozBIW3Uqycn1NbdiuLkxJtKetbn64NN2dyX1yITXvGbBuFy/jZ2WodC FEiZ1vjh6No0KocBqnIVkdi3Qsr8sOpfwb3fsvt6gKcEQGDlqrldwa9QvrrI1sNhFl3j KL8BZIs/jO2fDXlD8p/yaMeg5A63UP4cXEBfZvgz0pHyKNKkOoODGubbdxv+Q+l9Kn1C liD791cj7zzYUIvOhrzeh0t7pmmKLfydHg2b/vUlmMChzQrwbtMTptZXblc4CI/0VlsX bCjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=qapmg8H0xsf8FtuVuTZYqLZSXqZnlrHTeBzdG8nPoxQ=; b=PhEcdo6mr9GiEqIWros97z6UdS6W1Yi+mCHjPSNi8eLG+6fsGIfqnn35JH12r/OKWw gxp5zKeFsszzdm8NB7AP6mqJP27VBetSW05og5wGfPSiPazIDVWL4yBskYr9b0dqaRV3 LGqqMOzvRR7iA1fz1dUNEEK1aN58H4vr7TyQyZYZCtiYjneWTZQ8om6nEUfDl8hH8owC enI1aXsL1O1rIwrY8RgXbBm7flaCSrrGLw4p+KpZFutoO6EcJ5NlZNbtffjowHeRxB91 AyhKZ1vNNi75cEOJ6T4N6+xcZij3CdfcY7cRqBf36nbXhWZDfCP6Ys1rAjOPIiLVaTV6 6g4w== X-Gm-Message-State: AGi0PuYUCG2LvPN1JxX3LnXQtAeObgTqZPxIOiG/CvF788Bpj9ZUgUoU Zt543GZtz5ogreS9mkDG7Swa3HrG X-Received: by 2002:a63:4a59:: with SMTP id j25mr12358714pgl.336.1587316737845; Sun, 19 Apr 2020 10:18:57 -0700 (PDT) Received: from dtor-ws ([2620:15c:202:201:3c2a:73a9:c2cf:7f45]) by smtp.gmail.com with ESMTPSA id c15sm2406100pfo.188.2020.04.19.10.18.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2020 10:18:57 -0700 (PDT) Date: Sun, 19 Apr 2020 10:18:55 -0700 From: Dmitry Torokhov To: Alan Stern Cc: Julian Squires , Hans de Goede , Jiri Kosina , Benjamin Tissoires , syzbot , linux-input@vger.kernel.org, andreyknvl@google.com, gregkh@linuxfoundation.org, ingrassia@epigenesys.com, Kernel development list , USB list , syzkaller-bugs@googlegroups.com, Ping Cheng , pinglinux@gmail.com, killertofu@gmail.com Subject: Re: KASAN: use-after-free Read in usbhid_close (3) Message-ID: <20200419171855.GJ166864@dtor-ws> References: <20200419041344.GC166864@dtor-ws> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > 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? > > > > It could be that we again get confused by the "easiness" of devm APIs > > > and completely screwing up unwind order. > > That's probably what happened. Thanks. -- Dmitry