From patchwork Fri May 13 16:38:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 572086 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:66c4:0:0:0:0 with SMTP id x4csp628843mal; Fri, 13 May 2022 09:43:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyMwWqiGksQCZuiQdP30QOE9yMRzx1lTkDniB9IWsyG/hTq6riCf4748b0MZjZP05/FThw8 X-Received: by 2002:a05:620a:40cc:b0:6a0:54ac:f94 with SMTP id g12-20020a05620a40cc00b006a054ac0f94mr4475439qko.486.1652460204875; Fri, 13 May 2022 09:43:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652460204; cv=none; d=google.com; s=arc-20160816; b=gEt06qFgDTNppuSqHkgePSadAh6i9pxYB8jamRzq/zq16bmAnRwKPDllXFygnVshxT /R2jQu9V+RnPYwVqmrETr5upiXzITlu1SqxLnYsE6p3s5spMvP2e7w4iiSfbxInsMMxq aECZJsoMdkrd9wFLNEpvPtBgMIQEiXIqK6oFw/5XC10Vady1l7QhZyRYOrpdiBkArJsj 5ngmqObWVZYKVfavK+dqV2+1nCfZ7o0Y4KTYwxHdVbuIDhzaC6JIiXLB1Mc80o8LYLFH BBoXePRLvHqosIO4RbMzE+yrlnuUPYL1pZEGAJtVovLqDw3sKz0YlkDOF3+JeYM7LJWx fipA== 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:message-id:date:subject:cc:to:from:dkim-signature; bh=bB1/WMuJrvjSdc38PzEzRywr291IFLBWO0kli8cjXxY=; b=hSWlSZ8EC0cm4ezSjC29ZMJX6JFKvYj528d9EoKEGtZPqysUUo//iSnX7NFx6Kwr50 uPsmYZ3USg5stxWECjbjGaHPS2aJfyZlqwI5Zj9htMa17jW8KU5hQIykI7Miy0wLViNz uvqdUb0V5h5gA4Tl/rhhCCH6/mJHy6hkgsOpUsxob/jbt0ebHRQVBliCxE4bhmpLz6qL dXdke0iCEFUIUr3uUYg9SdW7fEdfsADlIRhBVob87g/yzBKikXFrsGaHc8kiGy6SmxrK 3uij8lQC2nG6K6IEzMwH4kiRuStfFeRds78B5+bw3Fs9jaIorHQKp6q+zNdjCDHNQvJJ rahw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fzAwQ9Nj; 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 l18-20020a05622a051200b002f3b76e1af2si1979341qtx.296.2022.05.13.09.43.24 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 13 May 2022 09:43:24 -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=fzAwQ9Nj; 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]:57752 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1npYNw-0000vl-HA for patch@linaro.org; Fri, 13 May 2022 12:43:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34100) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npYJF-0002WX-4O for qemu-devel@nongnu.org; Fri, 13 May 2022 12:38:33 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]:43819) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1npYJD-00015f-Cf for qemu-devel@nongnu.org; Fri, 13 May 2022 12:38:32 -0400 Received: by mail-wr1-x436.google.com with SMTP id v12so12123563wrv.10 for ; Fri, 13 May 2022 09:38:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bB1/WMuJrvjSdc38PzEzRywr291IFLBWO0kli8cjXxY=; b=fzAwQ9NjSTfC/sizt1ZeKasZYl9vBJWU864x1qM158DVyn2xG71/fv+Ofp8OuFyw/T TfmX05H9aXdbRLggWLRnMP/uscwDYWJrqZzHSIUg5uh9bWPym3RNEbO9/ptTfE5/xkEN I+etu3IyZvqszI570HQJcw2LUGVkdsPnbEk6szEZQ9oAjpvETGvd0IJ0HOxYM3K89sUp 5+cpnHfZFE+RPocJEnNKnTv1wElZyS+K2kWSOMhm779f31QH6NohEIF/di/jl3cH4Tl7 KoJuBOejUGt7trKbjltahqzufSINRn5fVP6EWu9tBfL1DEVhk60EtdLmNGnk/gOInYOg pqXw== 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:mime-version :content-transfer-encoding; bh=bB1/WMuJrvjSdc38PzEzRywr291IFLBWO0kli8cjXxY=; b=Rl04gJx5/LYwpoaZIflmQAfy6h2SQtlH6GJPmgDZFRILATIwBdawwV8J8HRTwMJ23k ahI1FqYza5Vkvl3M14yv/KEBWAJ4rJ13NppdKzKHDF+iFzqgBSkn/e1xlT0M/xlWBXAA ZSaF0YUBh3Ts4xjptL3nUQffP/gGdP3BeJLFL2hNcdllLNAkeXk+Od+pF91Cb3kUnCgc CpSEh9wXT06FO5x34RrYBg01EN4BZTbLU5xbW1GhAcKnpcIUzINILSR09+lhfSphvcY+ ar/Q2qGxKCW+PTIFn1RUjRmBCAOSErSRXavXvVM0CgOAkeXUHnyv92m3PaS7I206CAyE 0u1Q== X-Gm-Message-State: AOAM530RZRWXO5lRSWRP4FlZginEXFM9UvP2yKnL078ieS0dGIpL/X7H ikELW6PgBtBgrJdl5nqjsdDTt0DvZZjTQA== X-Received: by 2002:a05:6000:18ac:b0:20c:ba84:1260 with SMTP id b12-20020a05600018ac00b0020cba841260mr4795841wri.379.1652459909851; Fri, 13 May 2022 09:38:29 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id r64-20020a1c2b43000000b003942a244edbsm3963635wmr.32.2022.05.13.09.38.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 09:38:29 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: Stefan Berger Subject: [PATCH] hw/tpm/tpm_tis_common.c: Assert that locty is in range Date: Fri, 13 May 2022 17:38:27 +0100 Message-Id: <20220513163827.26281-1-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x436.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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" In tpm_tis_mmio_read(), tpm_tis_mmio_write() and tpm_tis_dump_state(), we calculate a locality index with tpm_tis_locality_from_addr() and then use it as an index into the s->loc[] array. In all these cases, the array index can't overflow because the MemoryRegion is sized to be TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT bytes. However, Coverity can't see that, and it complains (CID 1487138, 1487180, 1487188, 1487198, 1487240). Add assertions that the calculated locality index is valid, which will help Coverity and also catch any potential future bug where the MemoryRegion isn't sized exactly. Signed-off-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Stefan Berger --- Tested with 'make check' only... hw/tpm/tpm_tis_common.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hw/tpm/tpm_tis_common.c b/hw/tpm/tpm_tis_common.c index e700d821816..81edae410c8 100644 --- a/hw/tpm/tpm_tis_common.c +++ b/hw/tpm/tpm_tis_common.c @@ -295,6 +295,8 @@ static void tpm_tis_dump_state(TPMState *s, hwaddr addr) uint8_t locty = tpm_tis_locality_from_addr(addr); hwaddr base = addr & ~0xfff; + assert(TPM_TIS_IS_VALID_LOCTY(locty)); + printf("tpm_tis: active locality : %d\n" "tpm_tis: state of locality %d : %d\n" "tpm_tis: register dump:\n", @@ -336,6 +338,8 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, uint32_t avail; uint8_t v; + assert(TPM_TIS_IS_VALID_LOCTY(locty)); + if (tpm_backend_had_startup_error(s->be_driver)) { return 0; } @@ -458,6 +462,8 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, uint16_t len; uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0); + assert(TPM_TIS_IS_VALID_LOCTY(locty)); + trace_tpm_tis_mmio_write(size, addr, val); if (locty == 4) {