From patchwork Fri Jul 6 15:12:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 10511933 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 36040603D7 for ; Fri, 6 Jul 2018 15:13:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 27555286AA for ; Fri, 6 Jul 2018 15:13:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1B988286BC; Fri, 6 Jul 2018 15:13:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B94F5286AA for ; Fri, 6 Jul 2018 15:13:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932706AbeGFPNO (ORCPT ); Fri, 6 Jul 2018 11:13:14 -0400 Received: from mail-vk0-f73.google.com ([209.85.213.73]:40141 "EHLO mail-vk0-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932448AbeGFPNN (ORCPT ); Fri, 6 Jul 2018 11:13:13 -0400 Received: by mail-vk0-f73.google.com with SMTP id y200-v6so4693400vky.7 for ; Fri, 06 Jul 2018 08:13:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:message-id:subject:from:to:cc; bh=R1zve2zgmlb4kooJs7DKxUHxVCMuzZdZmn6sw6MVakw=; b=FLPY33x1Dr6j+4+2rCld0CVuTnbleh3jrGvFcAMi628PqViySrTHx4qRu86EkrwBcD Jnk98jZYKaOSCOZsf2vQE4RMFxwzqFliELNGNPZpoC0/pFpG1QMlNTTNjTk2Xcpgbce/ kHxFtWk2AFa4YjbiE7fYFDh4whjhwf/nnk7MbHUSpTOxKUXLKOgKtWY9stX1WiIMLSln NLl/OS3r0dx8/LnPFsqDofO22rFIZB0L8D97fiWZnsRyMQuqAAdjpayOqrrhAx2E/yj/ jEnol4ndflRnFb8qixhl1w3esAFoFSpLneE/XNETZqd1lq3BoKQa0lPsThuGe4xL5ElC PBYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc; bh=R1zve2zgmlb4kooJs7DKxUHxVCMuzZdZmn6sw6MVakw=; b=HaGDaPf6YhFO5mqtLcyJG7MwuGtcBzDaOIVHijylaNkdaZyuZn3wAD74GKwoAFVORb WA+XmPiiIu9b6SX2sv7+h1dn8kY5EMNS1aIlj3kDK2/14BpQ8VyhR8ZjCz2Bn1V0bqCr l45WJUGtV5cPufb8xzEJ6Tdv2S74lWbd8Jv5nvXghF3XGXYk7mjachD2upCnaDi1pfyN yIOQB3cIbVzlrPPvkrH2rtb/CKfYPuNuYWePmbLQ68yRA1OxQzFE1CsgKvaSUTrmJM5q +c4mVwqbM82n8GhhpuCnM4rDte/jpokAJVnnqlx/Gxz05//KCIMBCp4n75lKe2hf8Ee6 K5iA== X-Gm-Message-State: APt69E1615Hk+u9FvCjv5nyxG5Dev8hTsdJEFzDk5XsfZnQHPP5i4RMU pm+0fgfQ7LSYndPrDWEIEqac7L9w6w== X-Google-Smtp-Source: AAOMgpeNy1oNY4n9jj5KyQJF6nX5Tn2t4DnoqWdblniqid3ZwFNXpPIfmz4R5opDMrxeUabLQeDuSUwUgw== MIME-Version: 1.0 X-Received: by 2002:a1f:36c3:: with SMTP id d186-v6mr4901010vka.69.1530889992182; Fri, 06 Jul 2018 08:13:12 -0700 (PDT) Date: Fri, 6 Jul 2018 17:12:56 +0200 Message-Id: <20180706151256.30359-1-jannh@google.com> X-Mailer: git-send-email 2.18.0.399.gad0ab374a1-goog Subject: [PATCH] USB: yurex: fix out-of-bounds uaccess in read handler From: Jann Horn To: Greg Kroah-Hartman , jannh@google.com Cc: Tomoki Sekiyama , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In general, accessing userspace memory beyond the length of the supplied buffer in VFS read/write handlers can lead to both kernel memory corruption (via kernel_read()/kernel_write(), which can e.g. be triggered via sys_splice()) and privilege escalation inside userspace. Fix it by using simple_read_from_buffer() instead of custom logic. Fixes: 6bc235a2e24a ("USB: add driver for Meywa-Denki & Kayac YUREX") Signed-off-by: Jann Horn --- drivers/usb/misc/yurex.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/drivers/usb/misc/yurex.c b/drivers/usb/misc/yurex.c index 8abb6cbbd98a..3be40eaa1ac9 100644 --- a/drivers/usb/misc/yurex.c +++ b/drivers/usb/misc/yurex.c @@ -396,8 +396,7 @@ static ssize_t yurex_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) { struct usb_yurex *dev; - int retval = 0; - int bytes_read = 0; + int len = 0; char in_buffer[20]; unsigned long flags; @@ -405,26 +404,16 @@ static ssize_t yurex_read(struct file *file, char __user *buffer, size_t count, mutex_lock(&dev->io_mutex); if (!dev->interface) { /* already disconnected */ - retval = -ENODEV; - goto exit; + mutex_unlock(&dev->io_mutex); + return -ENODEV; } spin_lock_irqsave(&dev->lock, flags); - bytes_read = snprintf(in_buffer, 20, "%lld\n", dev->bbu); + len = snprintf(in_buffer, 20, "%lld\n", dev->bbu); spin_unlock_irqrestore(&dev->lock, flags); - - if (*ppos < bytes_read) { - if (copy_to_user(buffer, in_buffer + *ppos, bytes_read - *ppos)) - retval = -EFAULT; - else { - retval = bytes_read - *ppos; - *ppos += bytes_read; - } - } - -exit: mutex_unlock(&dev->io_mutex); - return retval; + + return simple_read_from_buffer(buffer, count, ppos, in_buffer, len); } static ssize_t yurex_write(struct file *file, const char __user *user_buffer,