Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp7306339imm; Sun, 20 May 2018 23:58:54 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpD+PlohgHqn5lNbU7DCoRhm1tG2feNxFUEd57dI7J86AA8ekaltCEv+Zjilsq6e0z2a+aK X-Received: by 2002:a17:902:b68f:: with SMTP id c15-v6mr19202223pls.201.1526885934481; Sun, 20 May 2018 23:58:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526885934; cv=none; d=google.com; s=arc-20160816; b=dKIsSwLrE7FWF0yVE4icr1SFwJ/PIaDr2+LAKcKejIg4WFUPuUfADA/tRMIbQmQckW EahZC+eE+dcBdZA8JnDA4vedlu2Ycd5MvW8XfYhh7oEpNhEwVTXpYD+kqZfulBqk5omg +zm46rSRDmLp4L1M7r+ntTWXnz6nTlMqklqxxNFL6l/Iu6mOTqmEfOSTR0QxGK/yrvuR gvL8lqJAb1LJ/NVCOEmwcSwBMoOLEFNtuEJGguPS+FVKeG1bajWx8wCIl4NeHOien5Fz 3MxBFXlH54vqqKZuhF7wb/L3p15T4/MKynxnO/LIBzmIpEohv7Xo7PAPvXfuK4VEV2xh 48iQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=n3fBayPL3nKOJ2dXn7YcezDoDT69lbHt4xnWxqhI0W8=; b=llSrmSBqUg+ASjsOk6Fgyp3XYmuopI2h5mmzTB/5ttiXsiEougQVNKq69Alkxxxj5J g24v7Ig8tKkvl5S24n8BuUP1o56/4mOXet/SMHfT2hFy7pris6oPkhQnPkNhbVzfJVt4 bRaxx16A9OClrgup8oA3xGQC6S6bszrdKGdtm5juWsxtY3UUGBs0ciOEW8WaCpp3O/F7 giD43uDrziT/8XXQdGcSWSyMuziuxLnbea8LVdt1yLVcAMQ/YOdc3/Stan163QUEkbh/ MNZp9/gDBRRl0pFosGq1LJ3BdC0hobOw7tgdQuIITed2pcmlQM0sAKcnwF2NXpZ73b5h zVWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umn.edu header.s=google header.b=pqo+r8fl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umn.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p3-v6si13752079pfb.171.2018.05.20.23.58.38; Sun, 20 May 2018 23:58:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@umn.edu header.s=google header.b=pqo+r8fl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umn.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751075AbeEUG6Y (ORCPT + 99 others); Mon, 21 May 2018 02:58:24 -0400 Received: from mta-p5.oit.umn.edu ([134.84.196.205]:45468 "EHLO mta-p5.oit.umn.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750965AbeEUG6V (ORCPT ); Mon, 21 May 2018 02:58:21 -0400 Received: from localhost (unknown [127.0.0.1]) by mta-p5.oit.umn.edu (Postfix) with ESMTP id F1265B4A for ; Mon, 21 May 2018 06:58:20 +0000 (UTC) X-Virus-Scanned: amavisd-new at umn.edu Received: from mta-p5.oit.umn.edu ([127.0.0.1]) by localhost (mta-p5.oit.umn.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HGGYgM6b0zOF for ; Mon, 21 May 2018 01:58:20 -0500 (CDT) Received: from mail-io0-f199.google.com (mail-io0-f199.google.com [209.85.223.199]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mta-p5.oit.umn.edu (Postfix) with ESMTPS id B70E4B0B for ; Mon, 21 May 2018 01:58:20 -0500 (CDT) Received: by mail-io0-f199.google.com with SMTP id h70-v6so11208227iof.10 for ; Sun, 20 May 2018 23:58:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umn.edu; s=google; h=from:to:cc:subject:date:message-id; bh=n3fBayPL3nKOJ2dXn7YcezDoDT69lbHt4xnWxqhI0W8=; b=pqo+r8flZa18BhFbjjNFKoKlFDutngoh38CBXq+vBchbgKRBewTWaD5+B04rXOMUYq zlQzYyRl3QhL4P2DA1fNL/SaoKiOcP8ApXbPL4su8iXDkSbzz6r5ZPyYQknorCOG1XY0 hDvNlNbMo2xST86AiJGl7V9YY54Ea7VnMjM+BgB40Ta3mHo7SdDuRWuFI0rvFFGnOCex CsCrR67pkDmY6GFGv+o17DiGwmDMYKKkhF/vQiKTESwTQqzA8CSdjIfcI349mPlUa4sq M9wvr7nCHPgusHTMbj/Wa/4HffNuvjfRhptsLeY20Z8Z4FX8QrpSKWPvAGXtWieuKlEF ELFg== 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; bh=n3fBayPL3nKOJ2dXn7YcezDoDT69lbHt4xnWxqhI0W8=; b=CVBriNSWUCe7Blq0apufMAfpjh051M1KR/SoyjHDKKqqu/toLmGnnN/duxrw+ssV2W lqxvkkvG7rLKIkgDzhpQ6OC7/7ogLrlhM6L/tsUv4tEYTR3FXjyjtcBRno8y6pgAz3W0 jhSXi6HFIvnJyoyH3c2w/Y9nTnDuTJjblX5KgvvXem1YX3BCakoivcRi7Zck9/oRWvRz dheBk1hNWzRmIrhVx+UD3m0w7wOZM1tz3Q7bnLDLo1wTztRiTVK/2PoOe6YgZOvUEnv0 9+yNZibzqgbHTeTWScDSZV4jmpjiGcfbXOhTAysBwolRayXatn4i0/CPYv74HZgXHSjb x9Hw== X-Gm-Message-State: ALKqPwfj5zxsB+G35eoZtnliAasvMowduIIlAia9CX2UBkF2k6jftRcr owmecjMMxozknYDdBpeo4vcDj3dn0rc0bI7KsRypdkzKjNSu8xr1yQqRBeS/OQRYna1TqsUPGpa fpz6k7ZzNOj0XgyKzhxW9L+cNtrXV X-Received: by 2002:a6b:5009:: with SMTP id e9-v6mr20466625iob.5.1526885900047; Sun, 20 May 2018 23:58:20 -0700 (PDT) X-Received: by 2002:a6b:5009:: with SMTP id e9-v6mr20466613iob.5.1526885899750; Sun, 20 May 2018 23:58:19 -0700 (PDT) Received: from cs-u-cslp16.cs.umn.edu (cs-u-cslp16.cs.umn.edu. [134.84.121.95]) by smtp.gmail.com with ESMTPSA id u125-v6sm5193808itb.36.2018.05.20.23.58.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 20 May 2018 23:58:18 -0700 (PDT) From: Wenwen Wang To: Wenwen Wang Cc: Kangjie Lu , Armin Schindler , Karsten Keil , netdev@vger.kernel.org (open list:ISDN SUBSYSTEM), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v3] isdn: eicon: fix a missing-check bug Date: Mon, 21 May 2018 01:58:07 -0500 Message-Id: <1526885887-9759-1-git-send-email-wang6495@umn.edu> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In divasmain.c, the function divas_write() firstly invokes the function diva_xdi_open_adapter() to open the adapter that matches with the adapter number provided by the user, and then invokes the function diva_xdi_write() to perform the write operation using the matched adapter. The two functions diva_xdi_open_adapter() and diva_xdi_write() are located in diva.c. In diva_xdi_open_adapter(), the user command is copied to the object 'msg' from the userspace pointer 'src' through the function pointer 'cp_fn', which eventually calls copy_from_user() to do the copy. Then, the adapter number 'msg.adapter' is used to find out a matched adapter from the 'adapter_queue'. A matched adapter will be returned if it is found. Otherwise, NULL is returned to indicate the failure of the verification on the adapter number. As mentioned above, if a matched adapter is returned, the function diva_xdi_write() is invoked to perform the write operation. In this function, the user command is copied once again from the userspace pointer 'src', which is the same as the 'src' pointer in diva_xdi_open_adapter() as both of them are from the 'buf' pointer in divas_write(). Similarly, the copy is achieved through the function pointer 'cp_fn', which finally calls copy_from_user(). After the successful copy, the corresponding command processing handler of the matched adapter is invoked to perform the write operation. It is obvious that there are two copies here from userspace, one is in diva_xdi_open_adapter(), and one is in diva_xdi_write(). Plus, both of these two copies share the same source userspace pointer, i.e., the 'buf' pointer in divas_write(). Given that a malicious userspace process can race to change the content pointed by the 'buf' pointer, this can pose potential security issues. For example, in the first copy, the user provides a valid adapter number to pass the verification process and a valid adapter can be found. Then the user can modify the adapter number to an invalid number. This way, the user can bypass the verification process of the adapter number and inject inconsistent data. This patch reuses the data copied in diva_xdi_open_adapter() and passes it to diva_xdi_write(). This way, the above issues can be avoided. Signed-off-by: Wenwen Wang --- drivers/isdn/hardware/eicon/diva.c | 22 +++++++++++++++------- drivers/isdn/hardware/eicon/diva.h | 5 +++-- drivers/isdn/hardware/eicon/divasmain.c | 18 +++++++++++------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/drivers/isdn/hardware/eicon/diva.c b/drivers/isdn/hardware/eicon/diva.c index 944a7f3..1b25d8b 100644 --- a/drivers/isdn/hardware/eicon/diva.c +++ b/drivers/isdn/hardware/eicon/diva.c @@ -388,10 +388,10 @@ void divasa_xdi_driver_unload(void) ** Receive and process command from user mode utility */ void *diva_xdi_open_adapter(void *os_handle, const void __user *src, - int length, + int length, void *mptr, divas_xdi_copy_from_user_fn_t cp_fn) { - diva_xdi_um_cfg_cmd_t msg; + diva_xdi_um_cfg_cmd_t *msg = (diva_xdi_um_cfg_cmd_t *)mptr; diva_os_xdi_adapter_t *a = NULL; diva_os_spin_lock_magic_t old_irql; struct list_head *tmp; @@ -401,21 +401,21 @@ void *diva_xdi_open_adapter(void *os_handle, const void __user *src, length, sizeof(diva_xdi_um_cfg_cmd_t))) return NULL; } - if ((*cp_fn) (os_handle, &msg, src, sizeof(msg)) <= 0) { + if ((*cp_fn) (os_handle, msg, src, sizeof(*msg)) <= 0) { DBG_ERR(("A: A(?) open, write error")) return NULL; } diva_os_enter_spin_lock(&adapter_lock, &old_irql, "open_adapter"); list_for_each(tmp, &adapter_queue) { a = list_entry(tmp, diva_os_xdi_adapter_t, link); - if (a->controller == (int)msg.adapter) + if (a->controller == (int)msg->adapter) break; a = NULL; } diva_os_leave_spin_lock(&adapter_lock, &old_irql, "open_adapter"); if (!a) { - DBG_ERR(("A: A(%d) open, adapter not found", msg.adapter)) + DBG_ERR(("A: A(%d) open, adapter not found", msg->adapter)) } return (a); @@ -437,8 +437,10 @@ void diva_xdi_close_adapter(void *adapter, void *os_handle) int diva_xdi_write(void *adapter, void *os_handle, const void __user *src, - int length, divas_xdi_copy_from_user_fn_t cp_fn) + int length, void *mptr, + divas_xdi_copy_from_user_fn_t cp_fn) { + diva_xdi_um_cfg_cmd_t *msg = (diva_xdi_um_cfg_cmd_t *)mptr; diva_os_xdi_adapter_t *a = (diva_os_xdi_adapter_t *) adapter; void *data; @@ -459,7 +461,13 @@ diva_xdi_write(void *adapter, void *os_handle, const void __user *src, return (-2); } - length = (*cp_fn) (os_handle, data, src, length); + if (msg) { + *(diva_xdi_um_cfg_cmd_t *)data = *msg; + length = (*cp_fn) (os_handle, (char *)data + sizeof(*msg), + src + sizeof(*msg), length - sizeof(*msg)); + } else { + length = (*cp_fn) (os_handle, data, src, length); + } if (length > 0) { if ((*(a->interface.cmd_proc)) (a, (diva_xdi_um_cfg_cmd_t *) data, length)) { diff --git a/drivers/isdn/hardware/eicon/diva.h b/drivers/isdn/hardware/eicon/diva.h index b067032..1ad7665 100644 --- a/drivers/isdn/hardware/eicon/diva.h +++ b/drivers/isdn/hardware/eicon/diva.h @@ -20,10 +20,11 @@ int diva_xdi_read(void *adapter, void *os_handle, void __user *dst, int max_length, divas_xdi_copy_to_user_fn_t cp_fn); int diva_xdi_write(void *adapter, void *os_handle, const void __user *src, - int length, divas_xdi_copy_from_user_fn_t cp_fn); + int length, void *msg, + divas_xdi_copy_from_user_fn_t cp_fn); void *diva_xdi_open_adapter(void *os_handle, const void __user *src, - int length, + int length, void *msg, divas_xdi_copy_from_user_fn_t cp_fn); void diva_xdi_close_adapter(void *adapter, void *os_handle); diff --git a/drivers/isdn/hardware/eicon/divasmain.c b/drivers/isdn/hardware/eicon/divasmain.c index b9980e8..b6a3950 100644 --- a/drivers/isdn/hardware/eicon/divasmain.c +++ b/drivers/isdn/hardware/eicon/divasmain.c @@ -591,19 +591,22 @@ static int divas_release(struct inode *inode, struct file *file) static ssize_t divas_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { + diva_xdi_um_cfg_cmd_t msg; int ret = -EINVAL; if (!file->private_data) { file->private_data = diva_xdi_open_adapter(file, buf, - count, + count, &msg, xdi_copy_from_user); - } - if (!file->private_data) { - return (-ENODEV); + if (!file->private_data) + return (-ENODEV); + ret = diva_xdi_write(file->private_data, file, + buf, count, &msg, xdi_copy_from_user); + } else { + ret = diva_xdi_write(file->private_data, file, + buf, count, NULL, xdi_copy_from_user); } - ret = diva_xdi_write(file->private_data, file, - buf, count, xdi_copy_from_user); switch (ret) { case -1: /* Message should be removed from rx mailbox first */ ret = -EBUSY; @@ -622,11 +625,12 @@ static ssize_t divas_write(struct file *file, const char __user *buf, static ssize_t divas_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { + diva_xdi_um_cfg_cmd_t msg; int ret = -EINVAL; if (!file->private_data) { file->private_data = diva_xdi_open_adapter(file, buf, - count, + count, &msg, xdi_copy_from_user); } if (!file->private_data) { -- 2.7.4