Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp786392rwo; Wed, 2 Aug 2023 04:19:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlH+rddcrTBGbVqyG13qPaWS42CLGEh3nZlSWB7W0dO9YRsJARPhuthTob86FCidzHBY5zkU X-Received: by 2002:a17:902:8216:b0:1b8:c6:ec8f with SMTP id x22-20020a170902821600b001b800c6ec8fmr11962186pln.46.1690975140618; Wed, 02 Aug 2023 04:19:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690975140; cv=none; d=google.com; s=arc-20160816; b=HalXLBSScheuiZnS7bjGuZe1edkrOWms4LoDTlUVUaASWLHHms1l2gyvcVEwZLZMNs QgySi4WMZcypirzCGSt01e5m9lwxJhNxdkwSd8/afx7AIy8D8Bg5G5YIusOV2x7l9WwD nGUpDu8dQyQ7RDIvoHPk0Ies6pc0J2H3jr0uJhu/bManQttrlRhMR1n/zqXAuFUukiPu zZmL0SuBaFngSzELcunhxD4otBgfDFLON7ZC0XnGiB5QqAqA3O2oMgAVKa5nuCV1p2uw rRCHXLOwQZRVbo0cpbZdAuXcdb1+LLewVBAfYKX3FUiqgfJPCj01AYdO8RF/eKMGGsnu SSEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :organization:in-reply-to:subject:cc:to:from:dkim-signature; bh=w+Kt+VilpDpT26gabKqgEOXVmI4Bi8D/rd2BVXn8gc4=; fh=I/wzVTGdScYjJkXjERqmxAJ6r5bcVzHvv939ZUVFvKA=; b=o034ffhCgPl4R/DVx13aqgUQRYe4VAyMnpMmSmq4SaNEKu1x/YPLUmG/FsJd3gQwzg eVujZsLbl+cSZKpMNvSAc2t7tcFEsQr4cdIeUQ5S+HXIhdIzO3HWXiwXofgGS86e2q+0 JBlyhKWCtUdW8NPlUaZvR2b8y08t4dCdObzOiqjavDogGQml1aQL++yf6+3FRylgiExM 9K+UE6RAmNIu+owpoQ6Ua+QDOSlzs5fZn1O7L7aVLY0I+QIw3bzjtk1iYt0R9bzBorWv EQF1X7JOgPR/7gy46IyBncFRfGJntW4t8ztIgcM+GT/iSStf3y19gg90CI8JzbtHEmof Sixw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OcBDq3ya; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c6-20020a170902d48600b001b88ff529cfsi11146413plg.619.2023.08.02.04.18.38; Wed, 02 Aug 2023 04:19:00 -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=@intel.com header.s=Intel header.b=OcBDq3ya; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234281AbjHBKEy (ORCPT + 99 others); Wed, 2 Aug 2023 06:04:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233978AbjHBKEt (ORCPT ); Wed, 2 Aug 2023 06:04:49 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CE5919B9; Wed, 2 Aug 2023 03:04:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690970688; x=1722506688; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=PzZF7rmesK94EuU0OFUxjXjzDJ2Wb9i4El3T2lkjt20=; b=OcBDq3yaG6IfKHn/iFhm/lYjfncbHCnIs3LLtDXI53dqr3CC+zuz2s47 mNNQQzIaz63Rt+LOGqZDQ4jYhFPYKB4f5GvFWgG7MWKVP2Xixw6qYdmVc GVs/BCSCjw3rL9eSg7bRPkzZoD2FfqdWKGSwJCwhBOJZGOgPlr3elBIH0 82/fwavfaP4NUAXzOzR981WLxcSreVc/B565zQGYfbcGogI8Crf5buM54 9xRU9BAMN+tTaX2VXPteJ2z9SWfCxnffgMtvh/uHw2oNDseHVmHHfu5hl Tj5bNBIL1ngMdnAcEsGdWoXO0tNYq+dtekuwk8g7eSlU7WP2x0riwSaRk Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10789"; a="367001579" X-IronPort-AV: E=Sophos;i="6.01,248,1684825200"; d="scan'208";a="367001579" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Aug 2023 03:04:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10789"; a="679014600" X-IronPort-AV: E=Sophos;i="6.01,248,1684825200"; d="scan'208";a="679014600" Received: from osalyk-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.62.215]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Aug 2023 03:04:42 -0700 From: Jani Nikula To: Ziqi Zhao , syzbot+622bba18029bcde672e1@syzkaller.appspotmail.com, astrajoan@yahoo.com, airlied@gmail.com, daniel@ffwll.ch, dri-devel@lists.freedesktop.org, ivan.orlov0322@gmail.com, maarten.lankhorst@linux.intel.com, mripard@kernel.org, skhan@linuxfoundation.org, tzimmermann@suse.de Cc: davem@davemloft.net, dsahern@kernel.org, edumazet@google.com, jacob.e.keller@intel.com, jiri@nvidia.com, kuba@kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, pabeni@redhat.com, syzkaller-bugs@googlegroups.com Subject: Re: [PATCH v2] drm/modes: Fix division by zero error In-Reply-To: <20230801215538.105255-1-astrajoan@yahoo.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo References: <00000000000034cf5d05fea52dd4@google.com> <20230801215538.105255-1-astrajoan@yahoo.com> Date: Wed, 02 Aug 2023 13:04:39 +0300 Message-ID: <87sf91vjyg.fsf@intel.com> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On Tue, 01 Aug 2023, Ziqi Zhao wrote: > In the bug reported by Syzbot, the variable `den == (1 << 22)` and > `mode->vscan == (1 << 10)`, causing the multiplication to overflow and > accidentally make `den == 0`. To prevent any chance of overflow, we > replace `num` and `den` with 64-bit unsigned integers, and explicitly > check if the divisor `den` will overflow. If so, we employ full 64-bit > division with rounding; otherwise we keep the 64-bit to 32-bit division > that could potentially be better optimized. > > In order to minimize the performance overhead, the overflow check for > `den` is wrapped with an `unlikely` condition. Please let me know if > this usage is appropriate. > > Reported-by: syzbot+622bba18029bcde672e1@syzkaller.appspotmail.com > Signed-off-by: Ziqi Zhao Come to think of it, maybe the subject should mention "fix overflow" instead, but no biggie. Reviewed-by: Jani Nikula > --- > V1 -> V2: address style comments suggested by Jani Nikula > > > drivers/gpu/drm/drm_modes.c | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c > index ac9a406250c5..137101960690 100644 > --- a/drivers/gpu/drm/drm_modes.c > +++ b/drivers/gpu/drm/drm_modes.c > @@ -1285,13 +1285,13 @@ EXPORT_SYMBOL(drm_mode_set_name); > */ > int drm_mode_vrefresh(const struct drm_display_mode *mode) > { > - unsigned int num, den; > + u64 num, den; > > if (mode->htotal == 0 || mode->vtotal == 0) > return 0; > > - num = mode->clock; > - den = mode->htotal * mode->vtotal; > + num = mul_u32_u32(mode->clock, 1000); > + den = mul_u32_u32(mode->htotal, mode->vtotal); > > if (mode->flags & DRM_MODE_FLAG_INTERLACE) > num *= 2; > @@ -1300,7 +1300,10 @@ int drm_mode_vrefresh(const struct drm_display_mode *mode) > if (mode->vscan > 1) > den *= mode->vscan; > > - return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(num, 1000), den); > + if (unlikely(den > UINT_MAX)) > + return DIV64_U64_ROUND_CLOSEST(num, den); > + > + return DIV_ROUND_CLOSEST_ULL(num, (u32) den); > } > EXPORT_SYMBOL(drm_mode_vrefresh); -- Jani Nikula, Intel Open Source Graphics Center