Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3473490imu; Sun, 11 Nov 2018 15:55:38 -0800 (PST) X-Google-Smtp-Source: AJdET5eRu/L4CTsPw4O7GBgVri1wCr2QeaDknwqfmuJSxV0r6T08J08eOIHRtm1OcRnklHX9UZb2 X-Received: by 2002:a63:2f86:: with SMTP id v128mr14990721pgv.407.1541980538032; Sun, 11 Nov 2018 15:55:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541980538; cv=none; d=google.com; s=arc-20160816; b=TCWvc1xgNQFpnRGIOiRq8hT+mdTukPr/wF/YixoG4R0bT0fb+Bkklqc3Cv3YWOCX5n Vdt3htREsvd0rlM1jH7l0ix46YJTBLcxlE6borAfeTm6NRQ6eoRHgJj10sawDeOtltvv jxegbawPMSNhcqsoOUg86p3u7Z590knblzwFDzhGmrLEjgo4t1KS6yK6t6SuiEPd6Xfb 1bb9zY/G1FYeFpBwioOjcMfNDOYGpcPRJIvbgzxVVSI47HqhjhQfwoC4z+fyPQIxioE2 xExmxSj/zLNOqMkRsKkHUZzkGEM3zrA0rYo4Mja/OuaShC/eAumGd7ojZ2LIPCDJqpmK DmvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=h0ZoWELxQnX6OetOkd2Bkla6Zck8bvIGA8Ryc5+7DUs=; b=J+W+CSpo1lajcLDrmZL2yDbos07X7MMnYReHYypu2hvLtBuhheoZr3R+El6pTWbwD3 Vvrqjvy6mX1sGLUuzAg6uVPipF4n/pj5iX9s8Ag+WY50Q63sP3CVZ/za9DvxYbr1XZxn MwCn1McWdIYLYxuYqk2Xlvux3B7HYqld0Ze0EkUZ9CsfkSf6CDkelLldYrPFaA8rChsV kNGdVEKyYgVr+iI5CsMR4/IcuK3CBRWbFBQa5K/Q6rzCu5Ot/Bp8MA154vQ5LW/L80yK fFIgCJtT03+Zdo+1bwZm6GWWmu9ub0NIMENmE6G2eR9MMJzjv9dsPzfgf4eTEDt9Zafb OauQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JPtaoxka; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k15-v6si15560922pll.216.2018.11.11.15.55.23; Sun, 11 Nov 2018 15:55:37 -0800 (PST) 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=@kernel.org header.s=default header.b=JPtaoxka; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732775AbeKLISY (ORCPT + 99 others); Mon, 12 Nov 2018 03:18:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:37238 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732732AbeKLISX (ORCPT ); Mon, 12 Nov 2018 03:18:23 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7F9B521527; Sun, 11 Nov 2018 22:28:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975306; bh=Jm7k4xB1B+WYhI453kUhLXtsrU2a9OxI2c8ALHu4hzs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JPtaoxkaBb8CI5UXFR0ysLly14d54CTStwj76ObRjBDVCE88Gu54U/UNBT5IK3kAt Vpu/B4P6jRVDTaTK+4v2jSOkumjjyaxcyhZ46i13M+K0XSh9D7+EXk4L+zdsN2NeNE VcgcIDMhzjZvL4SKNkBy9GshMGq9qx8ZnW8OwQb0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wenwen Wang , Mike Snitzer Subject: [PATCH 4.19 275/361] dm ioctl: harden copy_params()s copy_from_user() from malicious users Date: Sun, 11 Nov 2018 14:20:22 -0800 Message-Id: <20181111221655.352396081@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Wenwen Wang commit 800a7340ab7dd667edf95e74d8e4f23a17e87076 upstream. In copy_params(), the struct 'dm_ioctl' is first copied from the user space buffer 'user' to 'param_kernel' and the field 'data_size' is checked against 'minimum_data_size' (size of 'struct dm_ioctl' payload up to its 'data' member). If the check fails, an error code EINVAL will be returned. Otherwise, param_kernel->data_size is used to do a second copy, which copies from the same user-space buffer to 'dmi'. After the second copy, only 'dmi->data_size' is checked against 'param_kernel->data_size'. Given that the buffer 'user' resides in the user space, a malicious user-space process can race to change the content in the buffer between the two copies. This way, the attacker can inject inconsistent data into 'dmi' (versus previously validated 'param_kernel'). Fix redundant copying of 'minimum_data_size' from user-space buffer by using the first copy stored in 'param_kernel'. Also remove the 'data_size' check after the second copy because it is now unnecessary. Cc: stable@vger.kernel.org Signed-off-by: Wenwen Wang Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-ioctl.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) --- a/drivers/md/dm-ioctl.c +++ b/drivers/md/dm-ioctl.c @@ -1720,8 +1720,7 @@ static void free_params(struct dm_ioctl } static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel, - int ioctl_flags, - struct dm_ioctl **param, int *param_flags) + int ioctl_flags, struct dm_ioctl **param, int *param_flags) { struct dm_ioctl *dmi; int secure_data; @@ -1762,18 +1761,13 @@ static int copy_params(struct dm_ioctl _ *param_flags |= DM_PARAMS_MALLOC; - if (copy_from_user(dmi, user, param_kernel->data_size)) - goto bad; + /* Copy from param_kernel (which was already copied from user) */ + memcpy(dmi, param_kernel, minimum_data_size); -data_copied: - /* - * Abort if something changed the ioctl data while it was being copied. - */ - if (dmi->data_size != param_kernel->data_size) { - DMERR("rejecting ioctl: data size modified while processing parameters"); + if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size, + param_kernel->data_size - minimum_data_size)) goto bad; - } - +data_copied: /* Wipe the user buffer so we do not return it to userspace */ if (secure_data && clear_user(user, param_kernel->data_size)) goto bad;