From patchwork Thu Jan 5 22:42:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9499771 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BF98D606DE for ; Thu, 5 Jan 2017 23:18:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ACE1B28429 for ; Thu, 5 Jan 2017 23:18:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9E2BE2845A; Thu, 5 Jan 2017 23:18:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 490AB28429 for ; Thu, 5 Jan 2017 23:18:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934035AbdAEXSJ (ORCPT ); Thu, 5 Jan 2017 18:18:09 -0500 Received: from mail-pf0-f181.google.com ([209.85.192.181]:36709 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761731AbdAEXSI (ORCPT ); Thu, 5 Jan 2017 18:18:08 -0500 Received: by mail-pf0-f181.google.com with SMTP id 189so89750148pfz.3 for ; Thu, 05 Jan 2017 15:17:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=TmSx8RTEDt/QS6jo4ladbSZWAUlL5RZIM/SzFWNk9Iw=; b=KmhFInAqf6Tz/65UXSAHMid6EH0aCvPf4EYTDqmQNW5BIUfJ0f5nUvqqlzhLbltHk9 WUGurjgRZWnzyLaOJB0tqCX27zIZLVPk6mHphHhBmojaJQS3LM3NPsD/HeRDgZMTQlka +CZMggUoe47H8IU9pgyqWfZ8RGjfyM7MoExjU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=TmSx8RTEDt/QS6jo4ladbSZWAUlL5RZIM/SzFWNk9Iw=; b=gZq7v5TsvhW/jr0+dBX6vE6E+x8QEjgiNejIgf6hddvOoSgJAe2QV2ySYb3kEaz1q1 wxr8nS9O6o9zgRg1tREDzq8ZmqEi+6w6+D/cy5rh4ZXFZG6oiKXEqNpnT7h5vuDRli9Z 57Z2ypSTeK9xTFOTMo4gP0FOVWLHsX1YGDczjW1fq6oBCdc+9O5403sMGkkeL5P5h1CB 8/pZrT4ONiJcvBlODKRvjw+be3sPOq7RZVjmuT25j0+8L22bGwDSi3r8tyOLuppHQUPv ofxaXjpGioFCoLYFN6wkWw37F1e3wXZrPt3PjysvDRQjE0ZqwHnZ3/E4Nrc3lrTRvxmi ozrA== X-Gm-Message-State: AIkVDXLwZzHdsdMDgr31jgLbEFDZ79ikvHAD/nQGTNsADjzYnqmc/nED5yRoSO3ntf5oxFiu X-Received: by 10.99.228.5 with SMTP id a5mr138048427pgi.1.1483656171021; Thu, 05 Jan 2017 14:42:51 -0800 (PST) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id y29sm155592378pfd.63.2017.01.05.14.42.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jan 2017 14:42:50 -0800 (PST) Date: Thu, 5 Jan 2017 14:42:49 -0800 From: Kees Cook To: Tomi Valkeinen Cc: linux-kernel@vger.kernel.org, linux-fbdev@vger.kernel.org, Min Chong , Dan Carpenter Subject: [PATCH] fbdev: color map copying bounds checking Message-ID: <20170105224249.GA50925@beast> MIME-Version: 1.0 Content-Disposition: inline Sender: linux-fbdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Copying color maps to userspace doesn't check the value of to->start, which will cause kernel heap buffer OOB read due to signedness wraps. CVE-2016-8405 Reported-by: Peter Pi (@heisecode) of Trend Micro Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Kees Cook --- drivers/video/fbdev/core/fbcmap.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/drivers/video/fbdev/core/fbcmap.c b/drivers/video/fbdev/core/fbcmap.c index f89245b8ba8e..68a113594808 100644 --- a/drivers/video/fbdev/core/fbcmap.c +++ b/drivers/video/fbdev/core/fbcmap.c @@ -163,17 +163,18 @@ void fb_dealloc_cmap(struct fb_cmap *cmap) int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to) { - int tooff = 0, fromoff = 0; - int size; + unsigned int tooff = 0, fromoff = 0; + size_t size; if (to->start > from->start) fromoff = to->start - from->start; else tooff = from->start - to->start; - size = to->len - tooff; - if (size > (int) (from->len - fromoff)) - size = from->len - fromoff; - if (size <= 0) + if (fromoff >= from->len || tooff >= to->len) + return -EINVAL; + + size = min_t(size_t, to->len - tooff, from->len - fromoff); + if (size == 0) return -EINVAL; size *= sizeof(u16); @@ -187,17 +188,18 @@ int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to) int fb_cmap_to_user(const struct fb_cmap *from, struct fb_cmap_user *to) { - int tooff = 0, fromoff = 0; - int size; + unsigned int tooff = 0, fromoff = 0; + size_t size; if (to->start > from->start) fromoff = to->start - from->start; else tooff = from->start - to->start; - size = to->len - tooff; - if (size > (int) (from->len - fromoff)) - size = from->len - fromoff; - if (size <= 0) + if (fromoff >= from->len || tooff >= to->len) + return -EINVAL; + + size = min_t(size_t, to->len - tooff, from->len - fromoff); + if (size == 0) return -EINVAL; size *= sizeof(u16);