From patchwork Fri Jul 29 09:19:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 594350 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:b345:0:0:0:0 with SMTP id w5csp1179697maz; Fri, 29 Jul 2022 02:26:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u1X1G3tAt8zUkUohL9DaYzBUeN6re2iXryFM70suGznm/SjstQYjVN65JhHWwwCeCwAQsy X-Received: by 2002:ac8:7d91:0:b0:31f:37ea:98e9 with SMTP id c17-20020ac87d91000000b0031f37ea98e9mr2488486qtd.511.1659086772862; Fri, 29 Jul 2022 02:26:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659086772; cv=none; d=google.com; s=arc-20160816; b=cV7P3iy2P52tNzIbB7PE/ah6i78fTg99O3A8aMYZR1LQABkUIz3BYXVDZ4qLCbucN0 BXz/lEvcYamx6YdoYiwD2NMlXE22kakOnBesha7imnGaY9IPmru/N0HLcNHw9Q+BkaTB CPR8zTAnnRFu65KsmrhK9e6um8IDtvUDF8zhoZphYpdtKy6dK0V3F7SFV/mGJeTBWvC2 LvcxMaRscPeTRcPDVmVIqs6JPdTroZBiUX0M5wNETgmWMUtcpduWLhCfRC+6pwZY9rtH ZY5s6C95LpBRFzWS9aiErQQH8HGoz/cvobF+GgnJ8qJg0ezOnJJ/7Gl4nKSVf10zT4W3 mDnw== 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=7CojBDnzsSadEUfkkQHrKsxO1PC9/FIoZzsN5dLDDhc=; b=utjbcOLFbGB6ce7Tia1ZUvUTgzEoX9H1K/qZaZxTQSnW8V/tCKbEB+3HYU07hl1vZy dV1CadA8juR4F4NXm7cHlBSbBcIX6v3A28wCzpEwdiQzMdNJeEOKZq9YOMvbNuh7DH6E yN25z50lofNvx5EU0pR19kOwy6x7T85mzFYxVVCFdx30iKW0QLtgW3t0BNYWqm8TLw2k 24iY2HAQvSBENSd0d1jq0vkbkBnYNccdq8B6hDXDyBvHTWfAc5jq+YLCcO7I910zLS6y oSoBXiN3W+SdB/Mmn1ceiP7S0mYco4RFdwo7AMHeBzVcZRiLtzqQTT8x8E83ZWKiE8Hl IIfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Gyo0oAw3; 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 x16-20020ac87310000000b0031ef51fbccfsi1649397qto.673.2022.07.29.02.26.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Jul 2022 02:26:12 -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=Gyo0oAw3; 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]:59782 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oHMG4-00038t-8E for patch@linaro.org; Fri, 29 Jul 2022 05:26:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44274) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oHMA5-0002Vw-PK for qemu-devel@nongnu.org; Fri, 29 Jul 2022 05:20:02 -0400 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]:34315) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oHMA3-0005t2-C2 for qemu-devel@nongnu.org; Fri, 29 Jul 2022 05:20:01 -0400 Received: by mail-wr1-x42f.google.com with SMTP id h8so5277468wrw.1 for ; Fri, 29 Jul 2022 02:19:58 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=7CojBDnzsSadEUfkkQHrKsxO1PC9/FIoZzsN5dLDDhc=; b=Gyo0oAw39D9Q2vO8UwjCUhbuDDYMkqRXifp5gYOEQMkqI/JSYZ/TgtHXRkkGoH/t2e R50wWFLBMd1Uc5p4eLpV7JVhFX4E3QTQbEPfo0QDT4JGJE7QaHOZFptgDYdSAPXCaaw2 gQRj2QTOuC8IrFaDvKocPorg9f6Q1vRTnGtRgThZily5+I5tJaZ1MF91jehmPw/B/IRJ fuXfb+hXUo+Re46sQyHTblstmZiMGio5IrfOONynd/2b8/uuOI/5kBkcTnUwZSi3aTCE kpZeBul2j2m8OGc+id9JaOib8FfVOBXsCuC0qrMndr/3UH/b+nDY4MApNX0FRMyFU/Km rCNg== 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=7CojBDnzsSadEUfkkQHrKsxO1PC9/FIoZzsN5dLDDhc=; b=wxfpsjV/NKLKpJavKHSnpZzVVaVsRjeV87Exem5nM+zI/yu3NV69jlVXfzerrDU1Zr h+77YkEEXI9kkSQS0Z8dOLcQrMKp5gNv4VRidr1cpW8QSrtGozAhGQ8yO0nkZHlJUh1y H5Wwk0xq4kg/fvnaY90GdkdIF9Zv1FWeH6Gt+pOdt/KnWVd2IRc0gqOnbWBdPBDQqZHk XsVyF1Lc/ASZpc8iGRdmVdbNKa35EcbP/QVRXzPsekVLSA1nuAqIweZjzF+zyiVvlWH5 Z5AVXSk6omIYSOLGe4tNbDbNbLPLwgLnV4MCFUH0V+5cxLlMIeCfEY7pi1hhoFaJvniU D3ww== X-Gm-Message-State: ACgBeo2u/0q8BJU7sp5rpq+svR8VHOx5CMKC18LQ43xHo6nfz3gehL63 SQrE90kBvOxQUAOH5yXVB6q1HA== X-Received: by 2002:a05:6000:608:b0:21e:b5f4:4bc9 with SMTP id bn8-20020a056000060800b0021eb5f44bc9mr1890182wrb.196.1659086397986; Fri, 29 Jul 2022 02:19:57 -0700 (PDT) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id o12-20020a05600c4fcc00b003a31d200a7dsm4122665wmq.9.2022.07.29.02.19.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 02:19:52 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 7FD4B1FFC0; Fri, 29 Jul 2022 10:19:44 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org, richard.henderson@linaro.org Cc: qemu-devel@nongnu.org, =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 08/13] semihosting: Check for errors on SET_ARG() Date: Fri, 29 Jul 2022 10:19:38 +0100 Message-Id: <20220729091943.2152410-9-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220729091943.2152410-1-alex.bennee@linaro.org> References: <20220729091943.2152410-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42f; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42f.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" From: Peter Maydell The SET_ARG() macro returns an error indication; we check this in the TARGET_SYS_GET_CMDLINE case but not when we use it in implementing TARGET_SYS_ELAPSED. Check for and handle the errors via the do_fault codepath, and update the comment documenting the SET_ARG() and GET_ARG() macros to note how they handle memory access errors. Resolves: Coverity CID 1490287 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-Id: <20220719121110.225657-4-peter.maydell@linaro.org> Signed-off-by: Alex Bennée Message-Id: <20220725140520.515340-9-alex.bennee@linaro.org> diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index 1a1e2a6960..d12288fc80 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -171,6 +171,12 @@ static LayoutInfo common_semi_find_bases(CPUState *cs) * Read the input value from the argument block; fail the semihosting * call if the memory read fails. Eventually we could use a generic * CPUState helper function here. + * Note that GET_ARG() handles memory access errors by jumping to + * do_fault, so must be used as the first thing done in handling a + * semihosting call, to avoid accidentally leaking allocated resources. + * SET_ARG(), since it unavoidably happens late, instead returns an + * error indication (0 on success, non-0 for error) which the caller + * should check. */ #define GET_ARG(n) do { \ @@ -739,10 +745,14 @@ void do_common_semihosting(CPUState *cs) case TARGET_SYS_ELAPSED: elapsed = get_clock() - clock_start; if (sizeof(target_ulong) == 8) { - SET_ARG(0, elapsed); + if (SET_ARG(0, elapsed)) { + goto do_fault; + } } else { - SET_ARG(0, (uint32_t) elapsed); - SET_ARG(1, (uint32_t) (elapsed >> 32)); + if (SET_ARG(0, (uint32_t) elapsed) || + SET_ARG(1, (uint32_t) (elapsed >> 32))) { + goto do_fault; + } } common_semi_set_ret(cs, 0); break;