Received: by 10.192.165.148 with SMTP id m20csp4198476imm; Tue, 8 May 2018 04:47:20 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpkGI1sLJHE7NmVomzb0BiolsCKLMhRYOsQ6qF2dsQAUlwVPb9OsfTmM0jqqQyQCFJEoO8d X-Received: by 2002:a63:7003:: with SMTP id l3-v6mr18985028pgc.14.1525780040620; Tue, 08 May 2018 04:47:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525780040; cv=none; d=google.com; s=arc-20160816; b=I4dgldzIucuQF+XFu+VKTerHMub+j4T9aR0AAj3DfWsby6AvmRhrpuvgxRVb+AQWPC ed8p39ln+e1d7yLvj0355ZuRroYjWtGYWnRseolM/iEHTE3RbpVBrhT6b6pMFRkTNnA0 vZO/EFM4JpbIOrAwAjOp1E5zCX136hdemMLxK2PBU8MhJXvLV+6UU0B3a3Ck5X3gyjYK KWSs2Q38tQn2ixCdSZSWUeeL7UqXOwsHE7IHgUuo4YCy40yUdwQwdDTEdmD5oLsZXJff J1bmxZYX3UVKvyllyBezns/taYjLB+Gg7zXIiunX9bKp/qvH7/DAj7MfjyjJxPiPN/Q6 f/Lg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=R7/dNltBoYetjWA5Hs9c3/dRuB2tu2NJz5Wnt0Qp+j4=; b=tkESGokgm3Mu6xzqZULveP5dy8AWDCpAzatNvA2R1UHQOcqEY3VDR5f9Jzw4trDUbw EmCWX1Jfxijsm5ZdHnPxRgpQAcs3l4Z9umLZMnLnBVC7aM5IdfmUj3GryPm8pUYFBeWa P66DciHFs82USG7414+OF52A+gqXevk9shXmI4wA1erjkJ/i0sctnSoJBKhx/kwUY4y7 Fs61DY/NLU0Q60KHkGUWppK1Nh3a+9wE3PWhOXVR7nDR9hsC7tyvynB5CLsO+kW53wFz Ry/Ls1c1lTkheFHVl2XHx3qtjn7/3F2NTcO97OKtQ12QnAKDAtJ3OD/r2IlnkzkC3N2r NalQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a2-v6si7273387pgu.26.2018.05.08.04.47.06; Tue, 08 May 2018 04:47:20 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754593AbeEHLqI (ORCPT + 99 others); Tue, 8 May 2018 07:46:08 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:40158 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754550AbeEHLqG (ORCPT ); Tue, 8 May 2018 07:46:06 -0400 Received: by mail-wm0-f66.google.com with SMTP id j5-v6so20972070wme.5 for ; Tue, 08 May 2018 04:46:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=R7/dNltBoYetjWA5Hs9c3/dRuB2tu2NJz5Wnt0Qp+j4=; b=f92a1OJwbVzVJrESpng0FvMU12O523SFBX5TrBhuhUoxsUDPZKdTjUEeFTEbN9hI+4 32SUhErnqlP3YLcMofTKH+pbgOWr57/HI4phh0Ug8uxilAFHChBDFD/5qB7jKvFFFoR/ clbX3gFFzcWQ+waXN2pNz8HYiUbwZxgVTFO7Cj0N72HjYxm5jV8ZLhKmLcwUEMUSpix3 QF5zjMu1nPyr/CCX/m82i9sQ1zkdpk3jRP5+aMjY1sIFO4WtacewdfjCs+k+TMXKtyaq mGF3z6KPufL84BF3ZcUEKI5JZ1FNStS9OXaN/vMDbk6k8McqpTwM/2jRK+9UVwjmHHae wSZQ== X-Gm-Message-State: ALKqPwcqFoMWL9aEWx0gC/qEHXdsVoNol/2GDWPITG2pv522TgTIcfzw UicrLzZxe8RdeVbQmKWfmSxRwJHJI2U= X-Received: by 10.28.130.75 with SMTP id e72mr3098166wmd.64.1525779965213; Tue, 08 May 2018 04:46:05 -0700 (PDT) Received: from shalem.localdomain (546A5441.cm-12-3b.dynamic.ziggo.nl. [84.106.84.65]) by smtp.gmail.com with ESMTPSA id a10-v6sm31982169wri.10.2018.05.08.04.46.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 04:46:04 -0700 (PDT) Subject: Re: [PATCH] virt: vbox: fix a missing-check bug To: Wenwen Wang Cc: Kangjie Lu , Arnd Bergmann , Greg Kroah-Hartman , open list References: <1525577448-16071-1-git-send-email-wang6495@umn.edu> From: Hans de Goede Message-ID: <8db08d8e-c618-d806-b824-0b8990ef0785@redhat.com> Date: Tue, 8 May 2018 13:46:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <1525577448-16071-1-git-send-email-wang6495@umn.edu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Wenwen, On 06-05-18 05:30, Wenwen Wang wrote: > In vbg_misc_device_ioctl(), the header of the ioctl argument is copied from > the userspace pointer 'arg' and saved to the kernel object 'hdr'. Then the > 'version', 'size_in', and 'size_out' fields of 'hdr' are verified. For > example, if 'hdr.version' is not VBG_IOCTL_HDR_VERSION, an error code > -EINVAL will be returned. If 'hdr' can pass all verifications, the whole > structure of the ioctl argument is copied once again from 'arg' and saved > to 'buf'. Then the function vbg_core_ioctl() is invoked to execute the > ioctl command. Given that the 'arg' pointer resides in userspace, a > malicious userspace process can race to change the data pointed to by 'arg' > between the two copies. By doing so, the user can bypass the verifications > on the ioctl argument, which can cause vbg_core_ioctl() to work on unsecure > data because it assumes the 'version', 'size_in', and 'size_out' have been > verified by vbg_misc_device_ioctl(), as mentioned in the comment in > vbg_core_ioctl(): > > /* > * hdr->version and hdr->size_in / hdr->size_out minimum size are > * already checked by vbg_misc_device_ioctl(). > */ > > This patch adds checks after the second copy to ensure the consistency > between the data obtained in the two copies. In case an inconsistency is > detected, an error code -EINVAL will be returned. > > Signed-off-by: Wenwen Wang Thank you for finding this. I don't think that doing a second check is a good solution, by copy and pasting the checks we run the risk that any future additional checks are omitted from one copy of the checks. Instead I think we should simply avoid the 2nd copy of the header, like this: From 0c50b0dce3cf25a0ee9794c5816d9a0232d29e0a Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Tue, 8 May 2018 13:23:01 +0200 Subject: [PATCH 3/3] virt: vbox: Only copy_from_user the request-header once In vbg_misc_device_ioctl(), the header of the ioctl argument is copied from the userspace pointer 'arg' and saved to the kernel object 'hdr'. Then the 'version', 'size_in', and 'size_out' fields of 'hdr' are verified. Before this commit, after the checks a buffer for the entire request would be allocated and then all data including the verified header would be copied from the userspace 'arg' pointer again. Given that the 'arg' pointer resides in userspace, a malicious userspace process can race to change the data pointed to by 'arg' between the two copies. By doing so, the user can bypass the verifications on the ioctl argument. This commit fixes this by using the already checked copy of the header to fill the header part of the allocated buffer and only copying the remainder of the data from userspace. Reported-by: Wenwen Wang Signed-off-by: Hans de Goede --- drivers/virt/vboxguest/vboxguest_linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/virt/vboxguest/vboxguest_linux.c b/drivers/virt/vboxguest/vboxguest_linux.c index 398d22693234..6e2a9619192d 100644 --- a/drivers/virt/vboxguest/vboxguest_linux.c +++ b/drivers/virt/vboxguest/vboxguest_linux.c @@ -121,7 +121,9 @@ static long vbg_misc_device_ioctl(struct file *filp, unsigned int req, if (!buf) return -ENOMEM; - if (copy_from_user(buf, (void *)arg, hdr.size_in)) { + *((struct vbg_ioctl_hdr *)buf) = hdr; + if (copy_from_user(buf + sizeof(hdr), (void *)arg + sizeof(hdr), + hdr.size_in - sizeof(hdr))) { ret = -EFAULT; goto out; } Do you agree that this would also fix the race window you found? Regards, Hans > --- > drivers/virt/vboxguest/vboxguest_linux.c | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/drivers/virt/vboxguest/vboxguest_linux.c b/drivers/virt/vboxguest/vboxguest_linux.c > index 398d226..6b525259 100644 > --- a/drivers/virt/vboxguest/vboxguest_linux.c > +++ b/drivers/virt/vboxguest/vboxguest_linux.c > @@ -125,6 +125,12 @@ static long vbg_misc_device_ioctl(struct file *filp, unsigned int req, > ret = -EFAULT; > goto out; > } > + if (((struct vbg_ioctl_hdr *)buf)->version != hdr.version || > + ((struct vbg_ioctl_hdr *)buf)->size_in != hdr.size_in || > + ((struct vbg_ioctl_hdr *)buf)->size_out != hdr.size_out) { > + ret = -EINVAL; > + goto out; > + } > if (hdr.size_in < size) > memset(buf + hdr.size_in, 0, size - hdr.size_in); > > @@ -133,11 +139,6 @@ static long vbg_misc_device_ioctl(struct file *filp, unsigned int req, > goto out; > > returned_size = ((struct vbg_ioctl_hdr *)buf)->size_out; > - if (returned_size > size) { > - vbg_debug("%s: too much output data %zu > %zu\n", > - __func__, returned_size, size); > - returned_size = size; > - } > if (copy_to_user((void *)arg, buf, returned_size) != 0) > ret = -EFAULT; > >