Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp548981rwr; Thu, 20 Apr 2023 03:14:06 -0700 (PDT) X-Google-Smtp-Source: AKy350bjDjh6l3a1No5r7AJUbnEKHEPjAthTLA2nfup0yBFfEW4C41Mgz0nrBo6p68Asc/N6YDfc X-Received: by 2002:a05:6a00:810:b0:63d:2990:deb with SMTP id m16-20020a056a00081000b0063d29900debmr832666pfk.30.1681985645808; Thu, 20 Apr 2023 03:14:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681985645; cv=none; d=google.com; s=arc-20160816; b=a6BA8k9ZTbPKhlGNH+5xO9ZajN9tJFNvSUH1ECqquvG6GTMbqcIEVDztk+kf9VQq9+ pa/lf/tWWfxo9NV94clGASag35Hm7SvPsi4Pqx0wTrmp3SxemAbeQUGXc8sTxFox/EZP QA6qr+2wNYKETqaRhO//UWCMxGeRY1W3X739KY92mOLGzTSEP/NSLPYl2pyJn1i7b3xV u9Cf6pCuqQFEqYvAD0z/XGQUYuMITzcviq5YBdd83Tn3wAGKmDHKgNXD54XgRNNzyjUH F8cKicQrJVNT3sSd5cpA8CL0Bicfh2FOGWzkY20Z3IjOIJwVgIKZyvx6Qlb6lAod6AeQ zOMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:sender:hmm_source_type:hmm_attache_num :hmm_source_ip; bh=cir1Wtdxfowt8UMeZFP/zAe7WTdwdIJzhACjvgsu92E=; b=rOPzCSOSxs8RUX/BF4VyIOWz7Kudeg+ndviuDFcJnIRB25qmnEA0QczAO91cUsyhlx AhtV/g0oX6wySw6vPDIzy9oRPzxR3UKrMNKO9192hdnWi5EvSZEKTWumx3VDtnlJFTOv pwhhg5odmo+LVXvPElHdvYkkMjYLp9W/mVMzTxu42LHm5/JtLocMeTOOSJS83FrYlgZH xESA9Rf4RreXEOpAT5vRRh6eVe+nle9LeIDMg11gKTe2QFCVDOWzrtR1ixFLD6I7ua4c PEBmhkLEodzVTXp3ve+PJDFbE2KljUkk52O8WuPfMqI+pPzw2EEsfA2lSAZ97XxbypUJ eMmw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f5-20020a625105000000b0063b8cbcbc53si1330426pfb.1.2023.04.20.03.13.51; Thu, 20 Apr 2023 03:14:05 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234108AbjDTKNf (ORCPT + 99 others); Thu, 20 Apr 2023 06:13:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234551AbjDTKNJ (ORCPT ); Thu, 20 Apr 2023 06:13:09 -0400 Received: from 189.cn (ptr.189.cn [183.61.185.104]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4BF725FEA; Thu, 20 Apr 2023 03:09:53 -0700 (PDT) HMM_SOURCE_IP: 10.64.8.41:33082.432946535 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP Received: from clientip-114.242.206.180 (unknown [10.64.8.41]) by 189.cn (HERMES) with SMTP id 7ACB010023C; Thu, 20 Apr 2023 18:09:49 +0800 (CST) Received: from ([114.242.206.180]) by gateway-151646-dep-7b48884fd-ljp89 with ESMTP id ed7479d50c7f4249afd79f87783beb1f for geert@linux-m68k.org; Thu, 20 Apr 2023 18:09:52 CST X-Transaction-ID: ed7479d50c7f4249afd79f87783beb1f X-Real-From: 15330273260@189.cn X-Receive-IP: 114.242.206.180 X-MEDUSA-Status: 0 Sender: 15330273260@189.cn Message-ID: Date: Thu, 20 Apr 2023 18:09:49 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v5] drm/fbdev-generic: prohibit potential out-of-bounds access Content-Language: en-US To: Geert Uytterhoeven Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Li Yi , Helge Deller , Lucas De Marchi , linux-kernel@vger.kernel.org, linux-fbdev@vger.kernel.org, dri-devel@lists.freedesktop.org, loongson-kernel@lists.loongnix.cn References: <20230420030500.1578756-1-suijingfeng@loongson.cn> From: Sui Jingfeng <15330273260@189.cn> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,FROM_LOCAL_DIGITS, FROM_LOCAL_HEX,NICE_REPLY_A,SPF_HELO_PASS,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 Hi, On 2023/4/20 17:04, Geert Uytterhoeven wrote: > Hi Sui, > > On Thu, Apr 20, 2023 at 5:09 AM Sui Jingfeng wrote: >> The fbdev test of IGT may write after EOF, which lead to out-of-bound >> access for drm drivers hire fbdev-generic. For example, run fbdev test >> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >> linux kernel hang with the following call trace: >> >> Oops: 0000 [#1] PREEMPT SMP PTI >> [IGT] fbdev: starting subtest eof >> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >> [IGT] fbdev: starting subtest nullptr >> >> RIP: 0010:memcpy_erms+0xa/0x20 >> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >> FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >> Call Trace: >> >> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >> process_one_work+0x21f/0x430 >> worker_thread+0x4e/0x3c0 >> ? __pfx_worker_thread+0x10/0x10 >> kthread+0xf4/0x120 >> ? __pfx_kthread+0x10/0x10 >> ret_from_fork+0x2c/0x50 >> >> CR2: ffffa17d40e0b000 >> ---[ end trace 0000000000000000 ]--- >> >> The is because damage rectangles computed by >> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >> bound in the screen's active display area. Possible reasons are: >> >> 1) Buffers are allocated in the granularity of page size, for mmap system >> call support. The shadow screen buffer consumed by fbdev emulation may >> also choosed be page size aligned. >> >> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >> will introduce off-by-one error. >> >> For example, on a 16KB page size system, in order to store a 1920x1080 >> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >> 1920*1080*4 can not be divided exactly by 16KB. >> >> 1920 * 1080 * 4 = 8294400 bytes >> 506 * 16 * 1024 = 8290304 bytes >> 507 * 16 * 1024 = 8306688 bytes >> >> line_length = 1920*4 = 7680 bytes >> >> 507 * 16 * 1024 / 7680 = 1081.6 >> >> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >> >> memcpy_toio() typically issue the copy line by line, when copy the last >> line, out-of-bound access will be happen. Because: >> >> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >> >> Note that userspace may stil write to the invisiable area if a larger >> buffer than width x stride is exposed. But it is not a big issue as >> long as there still have memory resolve the access if not drafting so >> far. >> >> - Also limit the y1 (Daniel) >> - keep fix patch it to minimal (Daniel) >> - screen_size is page size aligned because of it need mmap (Thomas) >> - Adding fixes tag (Thomas) >> >> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >> >> Signed-off-by: Sui Jingfeng >> Reviewed-by: Thomas Zimmermann >> Tested-by: Geert Uytterhoeven > Thanks for the update! This v5 is completely different from the v3 > I tested before, so keeping my Tested-by is not really appropriate... Thanks for testing. I'm a bit of confident that it will works. Your tested-by is valuable, really don't want drop this. So I keep it. > I have retested fbtest with shmob-drm on Armadillo-800-EVA > (800x480@RG16, i.e. 187.5 pages), and fortunately this version still > works fine, so > Tested-by: Geert Uytterhoeven > > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org > > In personal conversations with technical people, I call myself a hacker. But > when I'm talking to journalists I just say "programmer" or something like that. > -- Linus Torvalds