Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3026948rwb; Mon, 15 Aug 2022 16:25:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR6RsUgJlSFqfR6H54V/4qAYMuXpZArhOL96qU/5QeFcVd9tOWh9H/ppBmQY7I1h80orgyCf X-Received: by 2002:a17:907:6288:b0:72f:90ba:f0b2 with SMTP id nd8-20020a170907628800b0072f90baf0b2mr11418101ejc.696.1660605793247; Mon, 15 Aug 2022 16:23:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605793; cv=none; d=google.com; s=arc-20160816; b=jvo1Jb2K1PHvDNVAnRu3RKzhcyOJg+iToJDkFDIiOahWtvvx0+r28iu/OF6yEpD4p8 8M9zoFp394Qlgre+sI2YqKpprX7uSKpfcyetXKetPPX+g1jzjiBLAVbd9AOpiqIZSE9W kv1J6YNG0+Ue4PP/y3xqzOkunk5JO5e1MycdD2y6AXU5cI/Lcu0qra3bvrqbnjIa+lUi lglJXLwRE1JE/4xPlIxDoK+lWu6ERmb+SOxu1HAzI/URRhedgHIqdVYIP0uFZj6KDwWm Rl6W8DXfacrnxMND6FwrkZIug0mrMUCXNzM6EpZNHWgZN0/bCvHUskuXRMIkGBAV6f95 zusw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+S5wCv82ouhEBOjPogLUxZkj/I5qdl5yqA9F9KX2juw=; b=UoWXNQDcprQ8gS5x9p62R47B33vIbEvUI+DggoAEtdpHj7oJmiXtywv/z3kR3H+UpW EL9QLs0rLd26xm1NMOcHTQ0s/KU3KBEOnMDo8F9MWHKV85OkV5b2w2U6nq7OSe6A8DI6 vnwyWKDFt2pWEixTKJyHrdgGWt9DC0OGh1LsGZh5dla0CyCF7iFqCshwAZi5vhLLKS+u nbCxBL9tnz5VJ366mH1yovqHZIvhAgOLiey26KUY9I+QNUVRY5WxyzuTVjti5SDQuDIG Wnhw1QQz6KjLxBT1w6isIZkWerdQAh/jUXSKb7x1VlAC105QKey2cowlHum0rt3vmj32 697w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QvkyQPJI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n8-20020a05640205c800b0043ac721c313si10078604edx.550.2022.08.15.16.22.47; Mon, 15 Aug 2022 16:23:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QvkyQPJI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347494AbiHOWWa (ORCPT + 99 others); Mon, 15 Aug 2022 18:22:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350491AbiHOWRz (ORCPT ); Mon, 15 Aug 2022 18:17:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BCAA4D262; Mon, 15 Aug 2022 12:41:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 95A5C611DD; Mon, 15 Aug 2022 19:40:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83E4FC433D6; Mon, 15 Aug 2022 19:40:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660592458; bh=yqibmGB8lQdsS1eWP4a+ONsj3+OHTEMl5n8rAQ0Jouo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QvkyQPJIW8+UxDc4D3suHZ+nRU6NYBc0vocM4SBGkgvT5Z51PmwF4MzfuI9fPu255 7RzBczu8zCWuNWI9vj+fqxanL4XjEqOCG5sQz56ABqVLXwoCQTIXbA+xy/gl77yn6M CMwNDZMRlMghzSDy+h8vlPBe2ibnzWk/5TjUXt6o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Nuno=20Gon=C3=A7alves?= , Thomas Zimmermann , Javier Martinez Canillas , Maarten Lankhorst , Maxime Ripard Subject: [PATCH 5.19 0086/1157] drm/fb-helper: Fix out-of-bounds access Date: Mon, 15 Aug 2022 19:50:42 +0200 Message-Id: <20220815180442.996471118@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Zimmermann commit ae25885bdf59fde40726863c57fd20e4a0642183 upstream. Clip memory range to screen-buffer size to avoid out-of-bounds access in fbdev deferred I/O's damage handling. Fbdev's deferred I/O can only track pages. From the range of pages, the damage handler computes the clipping rectangle for the display update. If the fbdev screen buffer ends near the beginning of a page, that page could contain more scanlines. The damage handler would then track these non-existing scanlines as dirty and provoke an out-of-bounds access during the screen update. Hence, clip the maximum memory range to the size of the screen buffer. While at it, rename the variables min/max to min_off/max_off in drm_fb_helper_deferred_io(). This avoids confusion with the macros of the same name. Reported-by: Nuno Gonçalves Signed-off-by: Thomas Zimmermann Reviewed-by: Javier Martinez Canillas Tested-by: Nuno Gonçalves Fixes: 67b723f5b742 ("drm/fb-helper: Calculate damaged area in separate helper") Cc: Thomas Zimmermann Cc: Javier Martinez Canillas Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: # v5.18+ Link: https://patchwork.freedesktop.org/patch/msgid/20220621104617.8817-1-tzimmermann@suse.de Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/drm_fb_helper.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) --- a/drivers/gpu/drm/drm_fb_helper.c +++ b/drivers/gpu/drm/drm_fb_helper.c @@ -680,7 +680,11 @@ static void drm_fb_helper_damage(struct schedule_work(&helper->damage_work); } -/* Convert memory region into area of scanlines and pixels per scanline */ +/* + * Convert memory region into area of scanlines and pixels per + * scanline. The parameters off and len must not reach beyond + * the end of the framebuffer. + */ static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len, struct drm_rect *clip) { @@ -715,22 +719,29 @@ static void drm_fb_helper_memory_range_t */ void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist) { - unsigned long start, end, min, max; + unsigned long start, end, min_off, max_off; struct fb_deferred_io_pageref *pageref; struct drm_rect damage_area; - min = ULONG_MAX; - max = 0; + min_off = ULONG_MAX; + max_off = 0; list_for_each_entry(pageref, pagereflist, list) { start = pageref->offset; end = start + PAGE_SIZE; - min = min(min, start); - max = max(max, end); + min_off = min(min_off, start); + max_off = max(max_off, end); } - if (min >= max) + if (min_off >= max_off) return; - drm_fb_helper_memory_range_to_clip(info, min, max - min, &damage_area); + /* + * As we can only track pages, we might reach beyond the end + * of the screen and account for non-existing scanlines. Hence, + * keep the covered memory area within the screen buffer. + */ + max_off = min(max_off, info->screen_size); + + drm_fb_helper_memory_range_to_clip(info, min_off, max_off - min_off, &damage_area); drm_fb_helper_damage(info, damage_area.x1, damage_area.y1, drm_rect_width(&damage_area), drm_rect_height(&damage_area));