From patchwork Tue Jul 23 13:10:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 813950 Delivered-To: patch@linaro.org Received: by 2002:adf:f288:0:b0:367:895a:4699 with SMTP id k8csp2309301wro; Tue, 23 Jul 2024 06:12:16 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWwaYfqDaKBNr7Oj/BwqiyK8P9SGdgSj3JCdMR5M+85Rz8+otvpEpX/cVwCZiRjAFkMXtUilGDXNAQzB0Pb/2DL X-Google-Smtp-Source: AGHT+IEhWoQmL7Dg1LtDed6WyRB8JQV1hXajC0fwxgSlVDZtA4/04oELsYzKzKBgLexh0u7H5lNp X-Received: by 2002:a05:6359:5a81:b0:1aa:c492:1d34 with SMTP id e5c5f4694b2df-1acc5c0dc08mr1173416055d.23.1721740336288; Tue, 23 Jul 2024 06:12:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721740336; cv=none; d=google.com; s=arc-20160816; b=zqkgbV4hx9Hc5Sx22y6j+cMaLJW8Awplm2d5igyWsQE4LX010L8M40pQsOhT0kYxYC uEm/Z1IFSrfTPeirjKQ71cZ/gi0K80cCGz/0JEbKjQTxMzEE1BLyj1LUxh/WuAG9f5II a6Vn2e7BxE0SN94f7/XNaB1P4kYrIxVDOemdaJjodpaqziny30Po8QwyNuwqQRooKa+5 ZtE22t5snCMXXrOpQ4Nw2TQm+zQDZx+5eweYE9nTENx3h1Nnn0j9/zRXixhJb/VvyH0Z wsS6MsM8ozikGZcXIi08RokBIW3378mWkMpBJCbYKdzy+4PgQz5mi0IoYUvHIDUEY3te q64w== 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:cc:to :from:dkim-signature; bh=GDjhGJy1uudDNK18CuZuB7KjPJkSAemQ7/iwEkdj0Zo=; fh=le3ZqmOmhgx1ZJs5I/i7KpYCkgJZo4XYTL5j9DJp65c=; b=OVjKuu9Ec1jbZATKwSKXJgxuMdpf/PUex9RhXKd9R7lNCjPUiQcwkDpise8XLWINUF ZsMq2/Tl4vnljLpx5mK+ilF6OncebCjgwNGn+Q750aJ2jmNpjRKZqVCXmIa4W6CVAhNb 0OPrdXtUdvrFQWwV26oqoSEoWvBPi0I4ikDbmsB2lCr8mGX3RHl2rfYTDaqzOmZj/jOj vXaQ7cQb6Ga/0y0p2X2s47zyPhrhr84s5SZjJfBCc064r/wO32CQrFJe8gj638CQ+Hwp OTskYCtD3HcOMEP+9qoBYb7tXs3FFMFykoZytS6lgA4zrWqW0mJaAEwzx1a+GJZQvyxi h3SQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RrdPzu6+; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 71dfb90a1353d-4f6a8a1db58si173166e0c.303.2024.07.23.06.12.16 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 Jul 2024 06:12:16 -0700 (PDT) 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=RrdPzu6+; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWFHu-0006Cg-8q; Tue, 23 Jul 2024 09:10:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sWFHr-00061h-Pt for qemu-devel@nongnu.org; Tue, 23 Jul 2024 09:10:39 -0400 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWFHm-0000IU-Km for qemu-devel@nongnu.org; Tue, 23 Jul 2024 09:10:39 -0400 Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3683f56b9bdso2853560f8f.1 for ; Tue, 23 Jul 2024 06:10:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1721740233; x=1722345033; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GDjhGJy1uudDNK18CuZuB7KjPJkSAemQ7/iwEkdj0Zo=; b=RrdPzu6+30zuIbQp484MFe5pJaqtTFWPHwaXfTnw1C+2Hn20rGby5L2eqDOtMF7O8G HRvWeyC2FMlRuH/g7dlcVdTNO2oHn25oilBXnnwAekgvm9gN9Tsoe5kVOK/SRoOJkQFq kuJ0MGlBSDKxuYfndb6slv2Mmv1itYPa5d8FJbOS66HUCr3cQcbsiZ7pcpYE1dBvngGZ CBE3DQ77DuLnsPnZeVNu2hY7yL75pIf7TVKWLqnr8o0rbdO9bNaESTe2tc5KQsow9Q3x ay9Ws4vGDQ0qioVod2CYj0YoqXFzXX3GYRWqHT4KN6Folw1oX8VsWCVCAxW0uPMF/fil P0ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721740233; x=1722345033; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GDjhGJy1uudDNK18CuZuB7KjPJkSAemQ7/iwEkdj0Zo=; b=L+d9bWlUDV80ycfh+zUy0hf2sBeZCcb4qQGXnirIUQAbdEtsJkTbV6jgdl+G5aNeAm XGP4IMcs+fVJmFS42z1XskwVXQZAXmRF8M4lGWGLFnxgGNIJYNPPQOFHatJZ2jcY073c scZKmGoeEoX8JslUthuAIqsrTqmm+ELD3ObbJvsmrNmvcQotq5yQpiWWRfHliXm7s8KP EP4vfnPewUYmTvG5thQGrD15nc48812YJizS4KljdW5Rwy4n6fQZEtDlLq3DnaixUdWI dYat9ZIonyv+zVgCMxZRuYVXVjL2l6/lJsbpp3aoKlHg+5aphAavu1uXb9gA/r3wwdLN UyXA== X-Forwarded-Encrypted: i=1; AJvYcCWkQq7g/xoZ7dqa3AdRad0nAHwnLvQiBPmEgRnSdjAH8iigHqOv/5i5IYnsUuIcJ2pXapd0Eg+lNLJr2XRdQiUGu25oHOo= X-Gm-Message-State: AOJu0Yxly8Y46FYIGgKzYxisprC7hghdaRmvrfjaxSSx4Hjmri1yL+46 2XNIxqS//RueVDrI1zQH4m3OnsHSvugNi7KPXlS8ND1Tac6CA7fltT9djBozB94= X-Received: by 2002:a05:6000:44:b0:369:b849:61b0 with SMTP id ffacd0b85a97d-369bbc69874mr6079499f8f.43.1721740232571; Tue, 23 Jul 2024 06:10:32 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36878684773sm11560157f8f.7.2024.07.23.06.10.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 06:10:32 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-stable@nongnu.org Subject: [PATCH 2/4] hw/misc/bcm2835_property: Avoid overflow in OTP access properties Date: Tue, 23 Jul 2024 14:10:27 +0100 Message-Id: <20240723131029.1159908-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240723131029.1159908-1-peter.maydell@linaro.org> References: <20240723131029.1159908-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::432; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x432.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=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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-bounces+patch=linaro.org@nongnu.org Coverity points out that in our handling of the property RPI_FWREQ_SET_CUSTOMER_OTP we have a potential overflow. This happens because we read start_num and number from the guest as unsigned 32 bit integers, but then the variable 'n' we use as a loop counter as we iterate from start_num to start_num + number is only an "int". That means that if the guest passes us a very large start_num we will interpret it as negative. This will result in an assertion failure inside bcm2835_otp_set_row(), which checks that we didn't pass it an invalid row number. A similar issue applies to all the properties for accessing OTP rows where we are iterating through with a start and length read from the guest. Use uint32_t for the loop counter to avoid this problem. Because in all cases 'n' is only used as a loop counter, we can do this as part of the for(), restricting its scope to exactly where we need it. Resolves: Coverity CID 1549401 Cc: qemu-stable@nongnu.org Signed-off-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daudé --- hw/misc/bcm2835_property.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hw/misc/bcm2835_property.c b/hw/misc/bcm2835_property.c index e28fdca9846..7eb623b4e90 100644 --- a/hw/misc/bcm2835_property.c +++ b/hw/misc/bcm2835_property.c @@ -30,7 +30,6 @@ static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value) uint32_t tot_len; size_t resplen; uint32_t tmp; - int n; uint32_t start_num, number, otp_row; /* @@ -337,7 +336,7 @@ static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value) resplen = 8 + 4 * number; - for (n = start_num; n < start_num + number && + for (uint32_t n = start_num; n < start_num + number && n < BCM2835_OTP_CUSTOMER_OTP_LEN; n++) { otp_row = bcm2835_otp_get_row(s->otp, BCM2835_OTP_CUSTOMER_OTP + n); @@ -366,7 +365,7 @@ static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value) break; } - for (n = start_num; n < start_num + number && + for (uint32_t n = start_num; n < start_num + number && n < BCM2835_OTP_CUSTOMER_OTP_LEN; n++) { otp_row = ldl_le_phys(&s->dma_as, value + 20 + ((n - start_num) << 2)); @@ -383,7 +382,7 @@ static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value) resplen = 8 + 4 * number; - for (n = start_num; n < start_num + number && + for (uint32_t n = start_num; n < start_num + number && n < BCM2835_OTP_PRIVATE_KEY_LEN; n++) { otp_row = bcm2835_otp_get_row(s->otp, BCM2835_OTP_PRIVATE_KEY + n); @@ -403,7 +402,7 @@ static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value) break; } - for (n = start_num; n < start_num + number && + for (uint32_t n = start_num; n < start_num + number && n < BCM2835_OTP_PRIVATE_KEY_LEN; n++) { otp_row = ldl_le_phys(&s->dma_as, value + 20 + ((n - start_num) << 2));