From patchwork Fri Jan 29 10:59:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 373299 Delivered-To: patch@linaro.org Received: by 2002:a02:a60d:0:0:0:0:0 with SMTP id c13csp2094144jam; Fri, 29 Jan 2021 03:09:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlfVANBySCbtN18QiPrCQFOPFy1hYEFSeQzAJbXEkidooN+Gqep2InqU5lAoTjaP6c746v X-Received: by 2002:a25:d94:: with SMTP id 142mr4747102ybn.424.1611918565878; Fri, 29 Jan 2021 03:09:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611918565; cv=none; d=google.com; s=arc-20160816; b=e7NhmasCu3NaL84ikVZ305P0ym1IIpMqNGEXPGJVmECM8PXIui9wUrl+lRxSfyY8dw 35CGHst7tNLEvZAT90Gpdk/GpxRtUnP1bP+6/EdJO0yl8liKGeyOflCKlaHur7T+YhG2 JEXthn0JIKSEfln81z9cQ6pLv96WabKaT++eAcjy0/tJKgnVN3h1NV8Q6Onm19/nA8FZ AsqNLH7cNmdCHPHwo6OD+ytlvdTsWBohHoSNwUSKfuel+qzdcjXLkb7HFe3RtgwFjyYc PR0JW7f4w/V9V6RQZNE5t294TByLZ0IyH/YziW/saDvMrpRY0UCnssq5ccH57ayELC09 e4xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=TPZILYkgh6hHHCNCFPKdewpdFM5U2Lja1SNGzqSj66g=; b=zbGzuC27MCLtLadl2SvhD1qki/oxPXFcc+fEgLzfSMfA9vGCGEMf1LUcPBlWaK5hp6 tDZq4+thBHmnX8v9ufkEKhhn/pQHlgo/khn9j5Mw/2bqhtwuiazuLR9s/mrchmkCbUUx 1HjqGBqHjCCnk0LXqXbYdzpN6KDIiwdKHEUMlxy1+oJKXq9Dif0DY3CbXrY45NKJ/tav 89aqIkhFP2leDY5la903LVQMP3N0Zv36+HPFxAtzq1TjNHdNEkA5gunzbObQERwqWuTq dAWuG+HMS+2l0lO3BqSZGkEut+q7Y9uOKkKQ3S4PQGisiDswBw8WReHzcy+dQLo4bGcR 53LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NTj2LZVu; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id z184si8768520ybc.57.2021.01.29.03.09.25 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Jan 2021 03:09:25 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NTj2LZVu; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:35634 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l5ReX-0001CZ-9q for patch@linaro.org; Fri, 29 Jan 2021 06:09:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59638) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l5RW3-0004NN-5c for qemu-devel@nongnu.org; Fri, 29 Jan 2021 06:00:39 -0500 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]:34319) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l5RVn-0006uP-PS for qemu-devel@nongnu.org; Fri, 29 Jan 2021 06:00:38 -0500 Received: by mail-wm1-x32c.google.com with SMTP id o10so6794960wmc.1 for ; Fri, 29 Jan 2021 03:00:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=TPZILYkgh6hHHCNCFPKdewpdFM5U2Lja1SNGzqSj66g=; b=NTj2LZVunRd2c32EUbiQZgWQ42M6nvRxLlXU/HDTtZJWWxc5gLXfXwDjd06AAiTYVU SfYZqmlbIdY5wth+CeklBS5ryOfOIf9Di8pxVsF71Eopk6EpJfARHfI/BhW3upLixWmO MFtJfFmAuMeAJemD6ctMw2YSmqBW23V5X+ur/hSi4vogV0V2SIEw87bTNKeVuELc2dpj XAFzFCxDYCqyIdWFw0Uw8SDTTUONeCTlqzF0UVz/Q9DREftiuDdcHTzNDI27qK5P+ged UUAyTXIvDy0MuSzimSQOABSyHSjsnjuzo8C/jSi6YWOyTdkyt5ZO4s2W9RGEaf0fB7D3 F3gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TPZILYkgh6hHHCNCFPKdewpdFM5U2Lja1SNGzqSj66g=; b=YZTObGZvZ1faugeLxEF9tyw17SGR5/O7VSWpGOmir87WdRBZIHnLPb+719SHShOCwG GGmg4U8p/ZhERa2rOsadxOIaVXlrGQplu8iSAxgIIpxoPtGAqoqRbaZ8OCALAAhyWMOI TYhbv77+tBl5bmObvxzg9lNWh2glBPZwzJymmT/KA2GpR7t7iGuLnObUnQ4noTtfktGj HxZi/d6cxBvTnyPTi2weq7D639xGaEIkQEaclpGvR2NE9Ic/FyW3E5f+xRw2r/eEVyVW O7fYBhmE9pu1hTykGlokOUxSCQJTBy3XyGc9OsB+3YqQXQRRDCTKoImmHH21yGUKMoeS pzfw== X-Gm-Message-State: AOAM530tds0/ndDHbtQ4Cg1CMEKHtPk7XXItkw/vJf3DsDuhcU23DoOu OcIsASn4cqja8jOUK6Z5pMVcD8g6JDQheQ== X-Received: by 2002:a05:600c:35d6:: with SMTP id r22mr3280098wmq.44.1611918021527; Fri, 29 Jan 2021 03:00:21 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id w20sm9268761wmm.12.2021.01.29.03.00.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jan 2021 03:00:20 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 07/46] hw/misc: Fix arith overflow in NPCM7XX PWM module Date: Fri, 29 Jan 2021 10:59:33 +0000 Message-Id: <20210129110012.8660-8-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210129110012.8660-1-peter.maydell@linaro.org> References: <20210129110012.8660-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x32c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Hao Wu Fix potential overflow problem when calculating pwm_duty. 1. Ensure p->cmr and p->cnr to be from [0,65535], according to the hardware specification. 2. Changed duty to uint32_t. However, since MAX_DUTY * (p->cmr+1) can excceed UINT32_MAX, we convert them to uint64_t in computation and converted them back to uint32_t. (duty is guaranteed to be <= MAX_DUTY so it won't overflow.) Fixes: CID 1442342 Suggested-by: Peter Maydell Reviewed-by: Doug Evans Signed-off-by: Hao Wu Message-id: 20210127011142.2122790-1-wuhaotsh@google.com Reviewed-by: Peter Maydell Signed-off-by: Peter Maydell --- hw/misc/npcm7xx_pwm.c | 23 +++++++++++++++++++---- tests/qtest/npcm7xx_pwm-test.c | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-) -- 2.20.1 diff --git a/hw/misc/npcm7xx_pwm.c b/hw/misc/npcm7xx_pwm.c index e99e3cc7ef3..dabcb6c0f95 100644 --- a/hw/misc/npcm7xx_pwm.c +++ b/hw/misc/npcm7xx_pwm.c @@ -58,6 +58,9 @@ REG32(NPCM7XX_PWM_PWDR3, 0x50); #define NPCM7XX_CH_INV BIT(2) #define NPCM7XX_CH_MOD BIT(3) +#define NPCM7XX_MAX_CMR 65535 +#define NPCM7XX_MAX_CNR 65535 + /* Offset of each PWM channel's prescaler in the PPR register. */ static const int npcm7xx_ppr_base[] = { 0, 0, 8, 8 }; /* Offset of each PWM channel's clock selector in the CSR register. */ @@ -96,7 +99,7 @@ static uint32_t npcm7xx_pwm_calculate_freq(NPCM7xxPWM *p) static uint32_t npcm7xx_pwm_calculate_duty(NPCM7xxPWM *p) { - uint64_t duty; + uint32_t duty; if (p->running) { if (p->cnr == 0) { @@ -104,7 +107,7 @@ static uint32_t npcm7xx_pwm_calculate_duty(NPCM7xxPWM *p) } else if (p->cmr >= p->cnr) { duty = NPCM7XX_PWM_MAX_DUTY; } else { - duty = NPCM7XX_PWM_MAX_DUTY * (p->cmr + 1) / (p->cnr + 1); + duty = (uint64_t)NPCM7XX_PWM_MAX_DUTY * (p->cmr + 1) / (p->cnr + 1); } } else { duty = 0; @@ -357,7 +360,13 @@ static void npcm7xx_pwm_write(void *opaque, hwaddr offset, case A_NPCM7XX_PWM_CNR2: case A_NPCM7XX_PWM_CNR3: p = &s->pwm[npcm7xx_cnr_index(offset)]; - p->cnr = value; + if (value > NPCM7XX_MAX_CNR) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid cnr value: %u", __func__, value); + p->cnr = NPCM7XX_MAX_CNR; + } else { + p->cnr = value; + } npcm7xx_pwm_update_output(p); break; @@ -366,7 +375,13 @@ static void npcm7xx_pwm_write(void *opaque, hwaddr offset, case A_NPCM7XX_PWM_CMR2: case A_NPCM7XX_PWM_CMR3: p = &s->pwm[npcm7xx_cmr_index(offset)]; - p->cmr = value; + if (value > NPCM7XX_MAX_CMR) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid cmr value: %u", __func__, value); + p->cmr = NPCM7XX_MAX_CMR; + } else { + p->cmr = value; + } npcm7xx_pwm_update_output(p); break; diff --git a/tests/qtest/npcm7xx_pwm-test.c b/tests/qtest/npcm7xx_pwm-test.c index 63557d2c061..3d82654b81a 100644 --- a/tests/qtest/npcm7xx_pwm-test.c +++ b/tests/qtest/npcm7xx_pwm-test.c @@ -272,7 +272,7 @@ static uint64_t pwm_compute_freq(QTestState *qts, uint32_t ppr, uint32_t csr, static uint64_t pwm_compute_duty(uint32_t cnr, uint32_t cmr, bool inverted) { - uint64_t duty; + uint32_t duty; if (cnr == 0) { /* PWM is stopped. */ @@ -280,7 +280,7 @@ static uint64_t pwm_compute_duty(uint32_t cnr, uint32_t cmr, bool inverted) } else if (cmr >= cnr) { duty = MAX_DUTY; } else { - duty = MAX_DUTY * (cmr + 1) / (cnr + 1); + duty = (uint64_t)MAX_DUTY * (cmr + 1) / (cnr + 1); } if (inverted) {