From patchwork Tue Sep 19 05:21:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meng Xu X-Patchwork-Id: 9958235 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 046156038F for ; Tue, 19 Sep 2017 05:27:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB18A22B1F for ; Tue, 19 Sep 2017 05:27:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ACA8226E55; Tue, 19 Sep 2017 05:27:31 +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=-6.9 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,RCVD_IN_DNSWL_HI 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 5D50B22B1F for ; Tue, 19 Sep 2017 05:27:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750774AbdISF1a (ORCPT ); Tue, 19 Sep 2017 01:27:30 -0400 Received: from mx1.gtisc.gatech.edu ([143.215.130.81]:52247 "EHLO mx1.gtisc.gatech.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750713AbdISF1a (ORCPT ); Tue, 19 Sep 2017 01:27:30 -0400 X-Greylist: delayed 330 seconds by postgrey-1.27 at vger.kernel.org; Tue, 19 Sep 2017 01:27:30 EDT Received: from bombshell.gtisc.gatech.edu (unknown [172.30.240.76]) by mx1.gtisc.gatech.edu (Postfix) with SMTP id 9A356C209D; Tue, 19 Sep 2017 01:21:54 -0400 (EDT) Received: (nullmailer pid 22479 invoked by uid 1026); Tue, 19 Sep 2017 05:21:56 -0000 From: Meng Xu To: dh.herrmann@googlemail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Cc: meng.xu@gatech.edu, sanidhya@gatech.edu, taesoo@gatech.edu, Meng Xu Subject: [PATCH] hid/uhid: fix a double-fetch bug when copying event from user Date: Tue, 19 Sep 2017 01:21:37 -0400 Message-Id: <1505798497-22436-1-git-send-email-mengxu.gatech@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When in_compat_syscall(), a user could make type != UHID_CREATE when get_user(type, buffer) [first fetch] and later make event->type == UHID_CREATE in copy_from_user(event, buffer, ...) [second fetch]. By doing so, an attacker might circumvent the specific logic to handle the type == UHID_CREATE case and later cause undefined behaviors. This patch enforces that event->type is overriden to the type value copied in the first fetch and thus, mitigate this race condition attack. Signed-off-by: Meng Xu --- drivers/hid/uhid.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c index 7f8ff39..4bbfd8a 100644 --- a/drivers/hid/uhid.c +++ b/drivers/hid/uhid.c @@ -448,11 +448,20 @@ static int uhid_event_from_user(const char __user *buffer, size_t len, kfree(compat); return 0; } + /* All others can be copied directly */ - } + if (copy_from_user(event, buffer, min(len, sizeof(*event)))) + return -EFAULT; - if (copy_from_user(event, buffer, min(len, sizeof(*event)))) - return -EFAULT; + /* + * Override type in case the user process rushes to change it + * between two fetches + * */ + event->type = type; + } else { + if (copy_from_user(event, buffer, min(len, sizeof(*event)))) + return -EFAULT; + } return 0; }