Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5472301rwb; Tue, 1 Aug 2023 03:14:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlG1v0IcKpWKQB7sckyEE1qrFoGSFK0ox0sudBD1QEC2DuwHAeZP3Y49zKS10ZhiYkcpAQKP X-Received: by 2002:a05:6a20:8412:b0:130:a15d:54eb with SMTP id c18-20020a056a20841200b00130a15d54ebmr13689011pzd.24.1690884848122; Tue, 01 Aug 2023 03:14:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690884848; cv=none; d=google.com; s=arc-20160816; b=Z/5vYNaZjt2ffJM1N7ww48dJlAWl6JTPEHhRZxp2io2tOuwdjhL5WwhHGyp29L4btu nv7za701HqlX54sQPLWbWZZOAsEImS/FUsRhLK27D/rVBtHqaVED3VlD/sPat6c81Ip5 Ymj9sRmaZkhTNa/02xJzt89IXN2tOaDe+ACm5bae6+kjeABCblMNDoozbG2hwwDkyfdy Jcu8UmVu8gDkk2MoArUiyvgx/lVXtw5AzrW3sG6nyDZVtUFSYwqUO523WwPJf29s6/EP HDEVu0hhzEdKVY4tqc64CcfmcwXbYcE0AlTUIYxhOUjrLhMBEf0binHkDMfrG+B6Opp5 vRPw== 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=Mfd9BOHXijcDuOUt2ooz1oaO5LP0hk7oVRVfhKJ8maY=; fh=mncjQ1dhFH3T7E4im/SyMM9ssjbG4KvQhmqSdytv15E=; b=R0sqI9FD+VhYiVLo68y4GJn2QjsJrrJUPp4JXNw99VeUgl8nxclC7cPBG5+i+kZ+r3 mHBqmKcX0rfKmQ80CWh3fHQpzqi28q5EwYl9F8toNOVx8J8np0irpjjkbfL2u5LZZncw h0IS7L/kbny2LJRH1YmxtScu4lk9LHz/T7/hiNTCvmmp9Gcd/bbppD6AQ1WRcUtGGjZ3 271BA3Su4D7ZKqbPobQeaDa1aNA/9ADMVD8fKPPnRhC0LDoUD8FGURJtcp7ksc4d3GOH cAo5+nLpHopiQwchgvRWlbvRfdu2BuSyfjRztpUE8HNy1Drh84SN9pGzMFrtFA9IxkTo +kdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=naRWqOLq; 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 i7-20020a6561a7000000b0055ba8970e7dsi8656024pgv.677.2023.08.01.03.13.56; Tue, 01 Aug 2023 03:14:08 -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=naRWqOLq; 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 S231811AbjHAJbr (ORCPT + 99 others); Tue, 1 Aug 2023 05:31:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232228AbjHAJbb (ORCPT ); Tue, 1 Aug 2023 05:31:31 -0400 Received: from mgamail.intel.com (unknown [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2260330FC; Tue, 1 Aug 2023 02:30:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690882209; x=1722418209; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=+ZeJP/xkyI2kTZxoz/E3PvGR0Hq4tjVxJSVN6RH3DV8=; b=naRWqOLqdQwUa+Xp6A2dfSC8+UUimTc+StMR/A/RyWErR+h2gxTyTNIV tHqOxQajXPeLKbsvXo05PnmMp6k9WWb7SCMo1nxxdpGvUtL920SrEZ/Mn ARXdS0g6/KOhFV44tW37z4G5NwmMGWBC3WmBPwM7bAzXki0cqzDDpFkf2 266fQdxRFu93SH6U7KODTMTLa781rT9dPaN/lyjFInPq/C5TJGhVyPGwj n7oCNjpG0AcV7zCLcU7FgQN34yukkESfZPsGQxSTe1+Ep7FgPPX35vYxr P4uX35dXi0/a56adLu4iPNTXWhV8f6G74KX858vfKQ+Ii4DybhznA7Rru A==; X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="371961912" X-IronPort-AV: E=Sophos;i="6.01,246,1684825200"; d="scan'208";a="371961912" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Aug 2023 02:30:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="798596374" X-IronPort-AV: E=Sophos;i="6.01,246,1684825200"; d="scan'208";a="798596374" Received: from igorban-mobl1.ccr.corp.intel.com (HELO localhost) ([10.252.36.188]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Aug 2023 02:30:02 -0700 From: Jani Nikula To: Ziqi Zhao , 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: netdev@vger.kernel.org, dsahern@kernel.org, syzkaller-bugs@googlegroups.com, linux-kernel@vger.kernel.org, edumazet@google.com, jiri@nvidia.com, jacob.e.keller@intel.com, kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, syzbot+622bba18029bcde672e1@syzkaller.appspotmail.com Subject: Re: [PATCH] drm/modes: Fix division by zero error In-Reply-To: <20230721160749.8489-1-astrajoan@yahoo.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo References: <20230709011213.17890-1-astrajoan@yahoo.com> <20230721160749.8489-1-astrajoan@yahoo.com> Date: Tue, 01 Aug 2023 12:30:00 +0300 Message-ID: <87o7jrw1nr.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, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 Fri, 21 Jul 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 > --- > 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..aa98bd7b8bc9 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; > + unsigned long long num, den; I think making them u64 would be more clear. > > 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 >> 32)) More intuitively, den > UINT_MAX. > + return div64_u64(num + (den >> 1), den); More intuitively, DIV64_U64_ROUND_CLOSEST(num, den). > + else The else after a branch with return is unnecessary. Someone's going to send a patch to remove it later if you leave it in. BR, Jani. > + return DIV_ROUND_CLOSEST_ULL(num, (unsigned int) den); > } > EXPORT_SYMBOL(drm_mode_vrefresh); -- Jani Nikula, Intel Open Source Graphics Center