From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751829AbcCFWOL (ORCPT ); Sun, 6 Mar 2016 17:14:11 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:34710 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751577AbcCFWNy (ORCPT ); Sun, 6 Mar 2016 17:13:54 -0500 Date: Sun, 6 Mar 2016 12:11:31 -0800 From: Olof Johansson To: Gwendal Grignou Cc: rdunlap@infradead.org, olofj@chromium.org, alan@lxorguk.ukuu.org.uk, cernekee@chromium.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] platform/chrome: cros_ec_dev - Fix security issue Message-ID: <20160306201131.GE20171@localhost> References: <56D883F7.6020002@infradead.org> <1457031613-35255-1-git-send-email-gwendal@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1457031613-35255-1-git-send-email-gwendal@chromium.org> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Thu, Mar 03, 2016 at 11:00:13AM -0800, Gwendal Grignou wrote: > Add a check to prevent memory scribble when sending an ioctl with .insize > set so large that memory allocation argument overflows. > > Signed-off-by: Gwendal Grignou > --- > drivers/platform/chrome/cros_ec_dev.c | 12 +++++++++++- > 1 file changed, 11 insertions(+), 1 deletion(-) > > diff --git a/drivers/platform/chrome/cros_ec_dev.c b/drivers/platform/chrome/cros_ec_dev.c > index d45cd25..0b2f730 100644 > --- a/drivers/platform/chrome/cros_ec_dev.c > +++ b/drivers/platform/chrome/cros_ec_dev.c > @@ -131,13 +131,23 @@ static ssize_t ec_device_read(struct file *filp, char __user *buffer, > static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg) > { > long ret; > + size_t data_size; > struct cros_ec_command u_cmd; > struct cros_ec_command *s_cmd; > > if (copy_from_user(&u_cmd, arg, sizeof(u_cmd))) > return -EFAULT; > > - s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize), > + /* > + * Prevent malicious attack where .insize is so big that amount > + * kmalloc'ed rollover, allowing memcpy to write beyond the allocated > + * space. > + */ > + data_size = max(u_cmd.outsize, u_cmd.insize); > + if (data_size + sizeof(*s_cmd) < data_size) > + return -EINVAL; > + > + s_cmd = kmalloc(sizeof(*s_cmd) + data_size, > GFP_KERNEL); This test does work, but it's sort of silly to even try to allow almost 4GB of allocation here. How about you introduce a reasonable max size for a transaction instead (256K?), and compare data_size with that? Might want to check with the EC folks what they expect larges transactions to be from their side, and go with a margin above that. Also, in your commit message you should refer to the CVE this fixes. -Olof