Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3428814imu; Sun, 11 Nov 2018 14:54:15 -0800 (PST) X-Google-Smtp-Source: AJdET5fMU44MJxnrfEsUkIksz30SiWRaL9n8GxlsrGs2x9w9XEPdEnqu1kqA/BYCaVXxOpKWBmHh X-Received: by 2002:a62:1fca:: with SMTP id l71-v6mr18370134pfj.23.1541976855126; Sun, 11 Nov 2018 14:54:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541976855; cv=none; d=google.com; s=arc-20160816; b=LYgLIfDoHOlVyE8tFy+p4Y+jFPX3kBPMq2Nb9Yj/xZ3cXSrZReUEa6/lD4TT8BLdL5 +5yXiE4bC6NOE3zBjQ0nx8xPfSiaDUYc79oQ0RX+WDygNthIJU2nsKjvxGvDWu6wWbcI McXO15aPbB8ZBs4IjDJGDhdyg9IcebTSyYNp5kRvchNwbxr9Q7ySvvmLfGlkJdbEQe0Y QP5HXoc7jQeVXJ/aEafmmRa4uOeHQvcoA9mHzUQXmRfLnlZA3NZX3kmFRInUPVBynaSx vtay7DWsP95r6pti0NTDW5k28MOcYAU7MvicK9sb5jNaAOWDrc5Q6N+GkdDq1s2Cgu7e 4a6g== 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=R/+Erv4uWdMpYpTFyvopBAnqjurxFrZcdfdfv8M3M+s=; b=tx3YLo6WzsJ5OgrofLSsbaOKW8Tlv3QQYVArK69eKBttZ3VGTi29mpuKb+z0+BhDMq /JCtcHBEQSC1LBaQh3rXPml3/MjQ3qELzIgEwuPJfAqr9ROiRjOZSVGWMNhNXwsw2gOO r/4b90kXIG2v833S0p9bEQt0FF7N/gAF0h17NXD2Aj1Xlfchkxnhe5CyWezdPMUU82e5 NNRkU1wRNYTydsIPGpulraCz1u6jq5QLxZFQVPVRuVwbzWFyYoaURZXpxxPO2U1F1r27 +WvVfxROgk+E1uK3zQtXKBCpeFP3nBOGn43DXf944yCMHhqDpzFW2Hz9+YCK8yHmoxsO cHtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gXL9Hckn; 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 65-v6si16733009pfl.220.2018.11.11.14.54.00; Sun, 11 Nov 2018 14:54:15 -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=gXL9Hckn; 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 S2404024AbeKLIW7 (ORCPT + 99 others); Mon, 12 Nov 2018 03:22:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:54642 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404008AbeKLIW6 (ORCPT ); Mon, 12 Nov 2018 03:22:58 -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 7B4902243E; Sun, 11 Nov 2018 22:33:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975581; bh=Dbw/oRHaI+fCeZsmO6YXTswIlDt3QSJkVImqKbFZOVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gXL9HcknYwZx3qwhFQR2ulNCWIa9mOggk4SVXtRs7cjEOlEcL/libtFl+nbT3e+Ym h6q8CATYKmJEa9GOAyXps54J7/0ZGiSrfqI8VcdCV7wGX/m/6qJAm6LycbXGEpfUMs Eti/TdGYY3+J8K9fkJ6RrepmQRZF9YzDJtpmK/2g= 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.14 173/222] dm ioctl: harden copy_params()s copy_from_user() from malicious users Date: Sun, 11 Nov 2018 14:24:30 -0800 Message-Id: <20181111221702.631595334@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221647.665769131@linuxfoundation.org> References: <20181111221647.665769131@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.14-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 @@ -1719,8 +1719,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; @@ -1761,18 +1760,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;