From patchwork Wed Sep 25 09:29:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 11160277 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 85FAD13BD for ; Wed, 25 Sep 2019 09:29:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 64A3920872 for ; Wed, 25 Sep 2019 09:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569403766; bh=hXFOlyeaNQM+XSUr9JB6/L52cy2cgWALgNViUGqYHJg=; h=From:To:Cc:Subject:Date:List-ID:From; b=pZbYgCGMHlCqovijA+a8DWgtUT7xqYb+UolwpJ9wGJBcEVzVcRJ2QYAbfIe/RWjlU yYlB0rIdWSGyYiOSND9V1d/VCrcGJevfYua34T1mICJrkKsg/sbNsFDD7zRZcKv2IQ 0qU6RuT7J2nGI9BOtnMCyTHId/cbE2DMkKOT2TPM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731685AbfIYJ3Z (ORCPT ); Wed, 25 Sep 2019 05:29:25 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:35102 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731569AbfIYJ3Y (ORCPT ); Wed, 25 Sep 2019 05:29:24 -0400 Received: by mail-lf1-f68.google.com with SMTP id w6so3674264lfl.2; Wed, 25 Sep 2019 02:29:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3A//nQ4U0ch//9NRjZkGVGrLiOj9F1zd5dYhN2nB9J0=; b=bOuoov9amIJClorT7w/Mt1UYfrquhlserOOWvVpzztMaXTV6aaOHc/Mp1yXcFaP0rG zFfyI6/NtNYM64JDo5TsKVKP4qx9JnnU3T4k85kJR6JQkzQmAwraG3H2I/aCIgIlXTiP Vf4Ecm/FmIpmUTWMbgswT1VrXRgpgz87/Nq0jVNztFiBK3Py+/2nWW2hszPcQWXFjTfj AdFiXkegQU6XlEq1i12cxkFKohca5cf576XaOAXDfNd4KXoiwlyUcG3D2ZAlJyr95bWC 5JlhKzzY7L367VVQ+EWxuxR4vCN0/xvKjiBv7q+jMsIPZJXpN8xJAnIuEirorAYdJYJL 5k7w== X-Gm-Message-State: APjAAAVFlqBSmhYIsRn7RockvubPzaL/+gUCkGcgYNbscBOtA08RcKgr /Adz5Ar1JcZ1BQ+RGciFyH4= X-Google-Smtp-Source: APXvYqzhIOrwZ5xeA83zTwMncUhIL9gjkSxGjou7BjEKHVXBSk8cG52Z3Fz5DChZNYIXrIPwyVmiaQ== X-Received: by 2002:a19:4f5a:: with SMTP id a26mr5079832lfk.116.1569403763000; Wed, 25 Sep 2019 02:29:23 -0700 (PDT) Received: from xi.terra (c-51f1e055.07-184-6d6c6d4.bbcust.telenor.se. [85.224.241.81]) by smtp.gmail.com with ESMTPSA id r6sm1069401ljr.77.2019.09.25.02.29.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Sep 2019 02:29:21 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.92.2) (envelope-from ) id 1iD3bz-0002Fa-7g; Wed, 25 Sep 2019 11:29:27 +0200 From: Johan Hovold To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Johan Hovold , stable , syzbot+0243cb250a51eeefb8cc@syzkaller.appspotmail.com Subject: [PATCH 1/2] USB: adutux: fix use-after-free on disconnect Date: Wed, 25 Sep 2019 11:29:12 +0200 Message-Id: <20190925092913.8608-1-johan@kernel.org> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org The driver was clearing its struct usb_device pointer, which it used as an inverted disconnected flag, before deregistering the character device and without serialising against racing release(). This could lead to a use-after-free if a racing release() callback observes the cleared pointer and frees the driver data before disconnect() is finished with it. This could also lead to NULL-pointer dereferences in a racing open(). Fixes: f08812d5eb8f ("USB: FIx locks and urb->status in adutux (updated)") Cc: stable # 2.6.24 Reported-by: syzbot+0243cb250a51eeefb8cc@syzkaller.appspotmail.com Tested-by: syzbot+0243cb250a51eeefb8cc@syzkaller.appspotmail.com Signed-off-by: Johan Hovold --- drivers/usb/misc/adutux.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/usb/misc/adutux.c b/drivers/usb/misc/adutux.c index 344d523b0502..bcc138990e2f 100644 --- a/drivers/usb/misc/adutux.c +++ b/drivers/usb/misc/adutux.c @@ -762,14 +762,15 @@ static void adu_disconnect(struct usb_interface *interface) dev = usb_get_intfdata(interface); - mutex_lock(&dev->mtx); /* not interruptible */ - dev->udev = NULL; /* poison */ usb_deregister_dev(interface, &adu_class); - mutex_unlock(&dev->mtx); mutex_lock(&adutux_mutex); usb_set_intfdata(interface, NULL); + mutex_lock(&dev->mtx); /* not interruptible */ + dev->udev = NULL; /* poison */ + mutex_unlock(&dev->mtx); + /* if the device is not opened, then we clean up right now */ if (!dev->open_count) adu_delete(dev); From patchwork Wed Sep 25 09:29:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 11160279 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D1E65112B for ; Wed, 25 Sep 2019 09:29:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B00AC214DA for ; Wed, 25 Sep 2019 09:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569403766; bh=Z1JhtUH7O8cb4lbeRdPRWO1Wb9Eaagx5bnKwaI5e9EI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=iYX9Ae9IRfCiNPT/DOiVQ3WOubtYE8P2MzxpbagK3hE5SV6yZSDTz++cogY9xeEXH BT8vb1EM1sH1zqA85Hea40L/e0Ql1M22mgGX0M+dXCo4BegxkGtCF0S4eONbxB3b/S 4mv4PE0mLfowVJ18pF3DryRbQRn53RfbHroW7fcs= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731601AbfIYJ3Z (ORCPT ); Wed, 25 Sep 2019 05:29:25 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:35249 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727635AbfIYJ3Y (ORCPT ); Wed, 25 Sep 2019 05:29:24 -0400 Received: by mail-lj1-f195.google.com with SMTP id m7so4904784lji.2; Wed, 25 Sep 2019 02:29:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Dd8Bj+SsAYNVwg79OrKDBOgClQX6Coby2DC7H5g+GYE=; b=UiP/ID2ZahT5uajSj3GoWeoAu6hfIZrUyFJN8IQr0cJwzX1l+9X5CYz7chewQipwfI hHVSMnOvRJmvvPbOIR5A9EP4Cixxj47bw5CT/omrOtRc2CQImHb/VYWSEBLHGsLODD1H UxKL9NXzJsZN6VOvF42HFgxAq2GbTyVuWok+7Ml6Lzw1j807Wr9BInSbqzf6OrgW532l pKl4Zl7Ucrun9UmCkhV3jgWKT2s+aux7OS3gwK3xWGpwE6N60n+MB4oQGp23MHH4Q0Wl pah1krtgtQcqFxWONElP3GvF0VX+XZI1+zBn8CQWdLEwo0ubZJOTnYuWqhc6wOr5/Eni x2yg== X-Gm-Message-State: APjAAAXRESkCj6lyY1vQPQoTH6IPu12iLpVu76amWxutJhCdJJHI6ir9 s+SF3Q3aNxek89nurWOei/Q= X-Google-Smtp-Source: APXvYqyiIMY4Vxtvl6Umln32hktQBkXEMinsP7PdVS4Fs+Ubv+S1yRPlBOeR/rElGGfRj6Nfegsf5g== X-Received: by 2002:a05:651c:21b:: with SMTP id y27mr5256626ljn.219.1569403762537; Wed, 25 Sep 2019 02:29:22 -0700 (PDT) Received: from xi.terra (c-51f1e055.07-184-6d6c6d4.bbcust.telenor.se. [85.224.241.81]) by smtp.gmail.com with ESMTPSA id t24sm1085858ljc.23.2019.09.25.02.29.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Sep 2019 02:29:21 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.92.2) (envelope-from ) id 1iD3c0-0002Fe-1f; Wed, 25 Sep 2019 11:29:28 +0200 From: Johan Hovold To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Johan Hovold , stable Subject: [PATCH 2/2] USB: adutux: fix NULL-derefs on disconnect Date: Wed, 25 Sep 2019 11:29:13 +0200 Message-Id: <20190925092913.8608-2-johan@kernel.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190925092913.8608-1-johan@kernel.org> References: <20190925092913.8608-1-johan@kernel.org> MIME-Version: 1.0 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org The driver was using its struct usb_device pointer as an inverted disconnected flag, but was setting it to NULL before making sure all completion handlers had run. This could lead to a NULL-pointer dereference in a number of dev_dbg statements in the completion handlers which relies on said pointer. The pointer was also dereferenced unconditionally in a dev_dbg statement release() something which would lead to a NULL-deref whenever a device was disconnected before the final character-device close if debugging was enabled. Fix this by unconditionally stopping all I/O and preventing resubmissions by poisoning the interrupt URBs at disconnect and using a dedicated disconnected flag. This also makes sure that all I/O has completed by the time the disconnect callback returns. Fixes: 1ef37c6047fe ("USB: adutux: remove custom debug macro and module parameter") Fixes: 66d4bc30d128 ("USB: adutux: remove custom debug macro") Cc: stable # 3.12 Signed-off-by: Johan Hovold --- drivers/usb/misc/adutux.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/usb/misc/adutux.c b/drivers/usb/misc/adutux.c index bcc138990e2f..f9efec719359 100644 --- a/drivers/usb/misc/adutux.c +++ b/drivers/usb/misc/adutux.c @@ -75,6 +75,7 @@ struct adu_device { char serial_number[8]; int open_count; /* number of times this port has been opened */ + unsigned long disconnected:1; char *read_buffer_primary; int read_buffer_length; @@ -116,7 +117,7 @@ static void adu_abort_transfers(struct adu_device *dev) { unsigned long flags; - if (dev->udev == NULL) + if (dev->disconnected) return; /* shutdown transfer */ @@ -243,7 +244,7 @@ static int adu_open(struct inode *inode, struct file *file) } dev = usb_get_intfdata(interface); - if (!dev || !dev->udev) { + if (!dev) { retval = -ENODEV; goto exit_no_device; } @@ -326,7 +327,7 @@ static int adu_release(struct inode *inode, struct file *file) } adu_release_internal(dev); - if (dev->udev == NULL) { + if (dev->disconnected) { /* the device was unplugged before the file was released */ if (!dev->open_count) /* ... and we're the last user */ adu_delete(dev); @@ -354,7 +355,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, return -ERESTARTSYS; /* verify that the device wasn't unplugged */ - if (dev->udev == NULL) { + if (dev->disconnected) { retval = -ENODEV; pr_err("No device or device unplugged %d\n", retval); goto exit; @@ -518,7 +519,7 @@ static ssize_t adu_write(struct file *file, const __user char *buffer, goto exit_nolock; /* verify that the device wasn't unplugged */ - if (dev->udev == NULL) { + if (dev->disconnected) { retval = -ENODEV; pr_err("No device or device unplugged %d\n", retval); goto exit; @@ -764,11 +765,14 @@ static void adu_disconnect(struct usb_interface *interface) usb_deregister_dev(interface, &adu_class); + usb_poison_urb(dev->interrupt_in_urb); + usb_poison_urb(dev->interrupt_out_urb); + mutex_lock(&adutux_mutex); usb_set_intfdata(interface, NULL); mutex_lock(&dev->mtx); /* not interruptible */ - dev->udev = NULL; /* poison */ + dev->disconnected = 1; mutex_unlock(&dev->mtx); /* if the device is not opened, then we clean up right now */