From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03442C33C9E for ; Tue, 14 Jan 2020 16:06:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D6B4A24655 for ; Tue, 14 Jan 2020 16:06:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729074AbgANQGY (ORCPT ); Tue, 14 Jan 2020 11:06:24 -0500 Received: from iolanthe.rowland.org ([192.131.102.54]:50730 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1726365AbgANQGY (ORCPT ); Tue, 14 Jan 2020 11:06:24 -0500 Received: (qmail 4192 invoked by uid 2102); 14 Jan 2020 11:06:22 -0500 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 14 Jan 2020 11:06:22 -0500 Date: Tue, 14 Jan 2020 11:06:22 -0500 (EST) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Dan Carpenter cc: syzbot , , , , , , , Subject: Re: KASAN: use-after-free Write in hiddev_disconnect In-Reply-To: <20200114152414.GC3719@kadam> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org On Tue, 14 Jan 2020, Dan Carpenter wrote: > I'm trying to take a stab at diagnosing these bugs. (But I'm seldom > smart enough to actually fix anything). These hiddev_disconnect() bugs > are a race condition: > > devel/drivers/hid/usbhid/hiddev.c > 924 void hiddev_disconnect(struct hid_device *hid) > 925 { > 926 struct hiddev *hiddev = hid->hiddev; > 927 struct usbhid_device *usbhid = hid->driver_data; > 928 > 929 usb_deregister_dev(usbhid->intf, &hiddev_class); > 930 > 931 mutex_lock(&hiddev->existancelock); > 932 hiddev->exist = 0; > ^^^^^^^^^^^^^^^^^^ > We set "exist = 0;" > > 933 > 934 if (hiddev->open) { > 935 mutex_unlock(&hiddev->existancelock); > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > Then we drop the lock. > > 936 hid_hw_close(hiddev->hid); > 937 wake_up_interruptible(&hiddev->wait); > ^^^^^^ > The other thread frees hiddev and it crashes here. > > 938 } else { > 939 mutex_unlock(&hiddev->existancelock); > 940 kfree(hiddev); > 941 } > 942 } > > The other thread is doing: > > drivers/hid/usbhid/hiddev.c > 216 static int hiddev_release(struct inode * inode, struct file * file) > 217 { > 218 struct hiddev_list *list = file->private_data; > 219 unsigned long flags; > 220 > 221 spin_lock_irqsave(&list->hiddev->list_lock, flags); > 222 list_del(&list->node); > 223 spin_unlock_irqrestore(&list->hiddev->list_lock, flags); > 224 > 225 mutex_lock(&list->hiddev->existancelock); > ^^^^^^^^^^^^^^^^^^^^^^^^^^^ > Takes the lock. > > 226 if (!--list->hiddev->open) { > ^^^^^^^^^^^^^^^^^^^^ > Decrements open. > > 227 if (list->hiddev->exist) { > ^^^^^^^^^^^^^^^^^^^ > This is false. > > 228 hid_hw_close(list->hiddev->hid); > 229 hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL); > 230 } else { > 231 mutex_unlock(&list->hiddev->existancelock); > 232 kfree(list->hiddev); > ^^^^^^^^^^^^ > Freed here. > > 233 vfree(list); > 234 return 0; > 235 } > 236 } > 237 > 238 mutex_unlock(&list->hiddev->existancelock); > 239 vfree(list); > 240 > 241 return 0; > 242 } > > I'm not sure what the fix should be though. Good sleuthing! It certainly looks as though hiddev_disconnect() should not try to reference hiddev after dropping the existance_lock -- at least, not in the case that the device file is still open. So I suggest reordering the statements in that routine: First call hid_hw_close() and wake_up_interruptible(), then drop the mutex. Too bad syzbot doesn't have a reproducer to test this, though. Alan Stern