Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp424257pxb; Tue, 1 Feb 2022 03:01:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJxTTTEAzOliVA+KCo2XhuLJIcv1lIQFJtB+fTazaapsrOTHRBHIHfS4JmkG9bmSF9sukK4I X-Received: by 2002:a05:6402:1ac5:: with SMTP id ba5mr24919775edb.337.1643713280927; Tue, 01 Feb 2022 03:01:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643713280; cv=none; d=google.com; s=arc-20160816; b=jV5rPmpenV8LHVDwY5dPnvmBi7ygzITH2+EHFEBzFtfm+ZW7BiVCS9BMIM6zqEDlos YMVzKmAjuVvo42ZvRaMmUbE0Lco1KG9d5f6L0FDaJSXtJKGHCwKAPUDaHufsumnHpAui df2qlXYwhk0BJYn1glpgOjqqti4Pl9mvI3ZaTsYtO+gkjY8ww9PyZigKqzVYhQB4Ibew m/j0uarjxKoLALAksQH4tD3b/PLZnjf31j41AY6kqh/ksZuX273t4ykIkUTvsn2HuSod nvSYUtoOCmIHlJIFzQzkSL5C4WlU3JQmJVvqsswyM/PqHbTk5RvvoqNTcdLVvrT2a+eN ZTOA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=A3rHofytIHL6hqd3yZWm4UUK7FUFMg5OI+YIKcI4+yE=; b=jTapHp7qZNcIycH13cjfz5c2aENLI+RqsbwKxNKct1AFe0DRZbmeRpp4BJr3WbgTbn C/0SXwkZjksCZfk/OhJbTz0F7SojqCNZjOiKafexWsRBgt0P5F/G8qBRjQDSpma3lerA zkBsdo7JiHeFEdGPPaHgQimbInS7CNXQzLVrmHrR7/6IcPGkRmKUkWRO6/UkpDQvoQ5p Ban72ErX/z1YYy9F7/GLCcpfGkrZlSEObHBviMcVNBQ4rkv7Pasyxq6T7TZU585/lIB3 WOmKkwZvAOJzumUBPdLfAhbNgU76ZPGdYD09Sta32HhpPgnnBQq0vou2mD+zSdyo2sqh gyJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Q0YBL9m8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y13si11623234eda.1.2022.02.01.03.00.56; Tue, 01 Feb 2022 03:01:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Q0YBL9m8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355551AbiA3QLc (ORCPT + 99 others); Sun, 30 Jan 2022 11:11:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355506AbiA3QL0 (ORCPT ); Sun, 30 Jan 2022 11:11:26 -0500 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25AA0C061751; Sun, 30 Jan 2022 08:11:24 -0800 (PST) Received: by mail-qk1-x729.google.com with SMTP id 13so9901158qkd.13; Sun, 30 Jan 2022 08:11:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=A3rHofytIHL6hqd3yZWm4UUK7FUFMg5OI+YIKcI4+yE=; b=Q0YBL9m8be2oqdHgbAKUEE51LMQddjeNY8fpF4YEguWnm4oZvjL4Giu63VKreF34xI fqWxtlNhLhFe+7YbBE7YUR0r1laPemGLfV+3q0y/M3/F/MYN+mFVDo9vHTRGcaZ1YARR vC52UpvuW4RBBYygubr5/zlWfqoBfdfBRCM7TA6otPpNEVEK0FtNhpPXhvwgfMCrNVav zqihEAJatfzr1vtEnAGbkQAMUrrfSfdo3dLStFvn3hLUFlbdHcemWIG8/btIKwwE4vux fIvWCYr+3DgsPQ7xv9Mp2u5g+SC5V65Q61ZWySQyK+1zQOLsLfM9pf69B7VEqH3wR7Dc B53g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=A3rHofytIHL6hqd3yZWm4UUK7FUFMg5OI+YIKcI4+yE=; b=EwYoHOzgjQISJ4RPk4mOzy0Oos7gmGrJhs51PiYMadxKDtVrOltTwpC6wmHgvdCRbh mMhB0zOJ4fheHwWuNBqX9ZMuzEWDvX7TPek+nG3veG+ApKy8IB7hDj7skyJf/4R/IMIk ipyHCc56OnGMoKAh9JzWCDGSWCoGfZBX/Hw2WKWhkfy6K68S5e7RYKy4XDu20lBernEv JtobNyGPQegppwgffcrXWJtzon6jxnIOMM5q9bBHGeXiMqbJZQxKLVGW79mrbutbi2yY KlwA8W5pFX95vO4n/GOEsOX1prgLVgMpQkoPgvWOnADxc4hVt8DE51YFcX+tIkmo4D7z Q5Gg== X-Gm-Message-State: AOAM533Vx9SyVJkAkI6m8M3MEoqtep/UZG8ByC5CoYniueESkWzfbDM5 /k2VZoLdjJev5ngvhNkYzgc= X-Received: by 2002:a05:620a:2886:: with SMTP id j6mr10949233qkp.316.1643559083333; Sun, 30 Jan 2022 08:11:23 -0800 (PST) Received: from shaak.. (modemcable055.92-163-184.mc.videotron.ca. [184.163.92.55]) by smtp.gmail.com with ESMTPSA id v73sm6906148qkb.51.2022.01.30.08.11.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 08:11:22 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v13 05/11] iio: afe: rescale: reduce risk of integer overflow Date: Sun, 30 Jan 2022 11:10:55 -0500 Message-Id: <20220130161101.1067691-6-liambeguin@gmail.com> X-Mailer: git-send-email 2.35.1.4.g5d01301f2b86 In-Reply-To: <20220130161101.1067691-1-liambeguin@gmail.com> References: <20220130161101.1067691-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reduce the risk of integer overflow by doing the scale calculation on a 64-bit integer. Since the rescaling is only performed on *val, reuse the IIO_VAL_FRACTIONAL_LOG2 case. Signed-off-by: Liam Beguin Reviewed-by: Peter Rosin --- drivers/iio/afe/iio-rescale.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 468e6c345bd1..67273de46843 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -25,21 +25,31 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { s64 tmp; + int _val, _val2; s32 rem, rem2; u32 mult; u32 neg; switch (scale_type) { - case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; - return scale_type; case IIO_VAL_INT: *val *= rescale->numerator; if (rescale->denominator == 1) return scale_type; *val2 = rescale->denominator; return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL: + /* + * When the product of both scales doesn't overflow, avoid + * potential accuracy loss (for in kernel consumers) by + * keeping a fractional representation. + */ + if (!check_mul_overflow(*val, rescale->numerator, &_val) && + !check_mul_overflow(*val2, rescale->denominator, &_val2)) { + *val = _val; + *val2 = _val2; + return IIO_VAL_FRACTIONAL; + } + fallthrough; case IIO_VAL_FRACTIONAL_LOG2: tmp = (s64)*val * 1000000000LL; tmp = div_s64(tmp, rescale->denominator); @@ -51,7 +61,10 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, if (!rem) return scale_type; - tmp = 1 << *val2; + if (scale_type == IIO_VAL_FRACTIONAL) + tmp = *val2; + else + tmp = ULL(1) << *val2; rem2 = *val % (int)tmp; *val = *val / (int)tmp; -- 2.35.1.4.g5d01301f2b86