Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp2047476rwo; Thu, 3 Aug 2023 04:04:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlEFDjGfILsoD/3JBqLpIkAd0/dzZhQ1hiSFYoqGtTf0N09o9SBNC++uVK99x8jkVnelP8D6 X-Received: by 2002:a17:90a:a888:b0:268:341:57d0 with SMTP id h8-20020a17090aa88800b00268034157d0mr16667482pjq.6.1691060654590; Thu, 03 Aug 2023 04:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691060654; cv=none; d=google.com; s=arc-20160816; b=xsoZMytYtrpEUHp8+RaVu20IzXzVDTlDAbE6oaIN3aIHIhWES5GMJ7hP7FI1IMABpf yaKGXuEsO1eJUvgfr2YkVWqu2Hx0cmM6f0oSP5qynZOsPlh1NUVhof7bs36yh1/2vN2L wQnsMUYsj00ToBAOnBPel9FuUUscGx72d75k/1TwREq1hBufY5BWp5NHz6i+afSGKb3J OKjJOrm7qj2KsKYmIV2glVCQ7jrWn4ePQNUzkNsKdnLhB9VcILRtlFPMDhFgilxHpqOa Mj2hJMQrTrEyPu2EdFk3ogKgB/aB2K6XRDW4/ItXgOXzaIHBbXoUesSSdXsQE+sl+Enn AFGg== 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=KksfRaAHxaBZLCokss4LVZQw/jibtxAkKoYEfyIHc68=; fh=I/wzVTGdScYjJkXjERqmxAJ6r5bcVzHvv939ZUVFvKA=; b=vzo0XiOykR9k72w13CU9+mgG6QggFtsCthM9DOhpzWB28tX9BYr3TcUzAmqZUjdrIN 3iDtmxHawlxR67ZyGPN6thgE9zr777F9NDAzcywhrU6h6vkncW4iUZnZ4/ojit1cbHyn BD0RI0lrK96wte4aKQWYJ0geQWb1WxEtCGMVrUye2opUzh1M7kwDuyljHM5eBLYUcw2/ DaF8mmcA+9nSR2p0xaXZ2tDUWjsgm1dlzP9NNgGQTdX/BRLSyiYiUUAQdIN+xSycd5wz pGXoirthBc2RDHvTkAKMPJGy+0Qw4z8k/0OSrYTdyqYEjgnf2JsJKo+s+WoQ0Y1KOotF 95qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ag5NWUCT; 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 on13-20020a17090b1d0d00b0025bec4468c2si3112744pjb.167.2023.08.03.04.04.01; Thu, 03 Aug 2023 04:04:14 -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=ag5NWUCT; 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 S235115AbjHCJgt (ORCPT + 99 others); Thu, 3 Aug 2023 05:36:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232825AbjHCJf7 (ORCPT ); Thu, 3 Aug 2023 05:35:59 -0400 Received: from mgamail.intel.com (unknown [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C79463ABA; Thu, 3 Aug 2023 02:35:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691055355; x=1722591355; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=N5jGfZQ8A78DMSVEz0/KZZUx/pejQfPJtpi3xQENQBE=; b=ag5NWUCTxvo0KsTNEcexyZhuGyKd5Q9hczQDqb2kxMAT6m+GaMXttymq /pPCy1EShT896NHVGOooXdPoRuh6FrD1mtgpzefilE/+RODtjG8owBG0S xYecKTEq4VBhjKsLjkKyZb5n4q/cJPaMButY7Uh+7B/t60MrZGEFn4tXt flNj0rLiFg9hyd5qdmG2cM5uyhmjQctn48pxnci/hwi+JJK2AQjOCrfM0 wWFoun/S5TK2FdRQVODrwu+yl57m4JZpf46dv6ZhLCFdZwzjE+QA5KMER vJ9vhe8jat5a87PozeVlhezNIVUhfIBCEnRfOPFU9q7mrQs9LFCDWHenB w==; X-IronPort-AV: E=McAfee;i="6600,9927,10790"; a="433664741" X-IronPort-AV: E=Sophos;i="6.01,251,1684825200"; d="scan'208";a="433664741" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Aug 2023 02:35:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10790"; a="729502060" X-IronPort-AV: E=Sophos;i="6.01,251,1684825200"; d="scan'208";a="729502060" Received: from jnikula-mobl4.fi.intel.com (HELO localhost) ([10.237.66.162]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Aug 2023 02:35:49 -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 v3] drm/modes: Fix division by zero due to overflow In-Reply-To: <20230802174746.2256-1-astrajoan@yahoo.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo References: <00000000000034cf5d05fea52dd4@google.com> <20230802174746.2256-1-astrajoan@yahoo.com> Date: Thu, 03 Aug 2023 12:35:47 +0300 Message-ID: <87bkfov570.fsf@intel.com> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,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 On Wed, 02 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 Reviewed-by: Jani Nikula > --- > V1 -> V2: address style comments suggested by Jani Nikula > > V2 -> V3: change title to include context on overflow causing the > division by zero > > 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