From patchwork Tue Jul 19 12:11:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 591653 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp3898703max; Tue, 19 Jul 2022 05:12:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t3BYb+EThhxLzCEbS0AoE5FdVV9q6TXsDfxI2EzhqqFuFQw0kR7p+EYv+lbUClVf23A39Q X-Received: by 2002:ac8:5c13:0:b0:31e:b532:ada3 with SMTP id i19-20020ac85c13000000b0031eb532ada3mr25179926qti.63.1658232744452; Tue, 19 Jul 2022 05:12:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658232744; cv=none; d=google.com; s=arc-20160816; b=z7NynJclIYQj5QyDPJnjo/jwPsh1OGIHHRS/05yyYTcu6aPbsh2MAy8dfNMoFYUl6r QIjtwtT80/BuCpEcaHHKbzmS9xdy0s5nQtTxX95HnpQLf2kZL0hKsZdQogfGCE9Me3lb tKahFtLR8Dar9/eDezyrFtLhHWn5TjrF/zvTEvIjUkivgM25wgdhAzmRQ9LzqfonsRQ6 uspvKou8t2NHu2co6fZbmvIiNojzTqn39HsN8eRXGBEyE4q0KMxIP3aGTVt9IbxWkT5d VW4jqDDQXaeOC+W8HUweuSCJNdxWZLB5qOYm+ux5O7L74w4MON6Es9QjCutCbbQMxOI1 80bA== 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=VP1puZ7Kqisr5dvxsNpLvw7ulkMOEZ3tezwiO4vRy/Q=; b=K0QYSB5JR5/4ufAiqchymxtaMe70VXQl+D4yCXniA24XY7SgRDqG8o9aK+IXSmuUEC aYZt/EqKF2IReVKhXbHV3WFYd6w8lbxn5JobtgD2xRuRLz6ak5941YVmPKmmIZTJ5erM 4BlWuqzoXuwUqPG7d/fjOasgny4bBlLBIz7PNZ1xf5nVkyKdCDtSP3WzKv9eVddFT8Uf JSjk+BvXHca7JQC9A0AHV0O1tEZgO3hTxJ2yo5KvOV4RBkdE5Oh7CdFJKnEaQwic78Bt +5lRapCurO+CwivO6EnWZyw40mCHJXN8ktK8mFfxaLotroTSV/uq8ggXPa/e88jfdOiT zQGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CV9iQqsL; 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 y1-20020a05620a44c100b006b5e45ff82esi3155227qkp.485.2022.07.19.05.12.24 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jul 2022 05:12: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=CV9iQqsL; 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]:48612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5Q-0004vf-10 for patch@linaro.org; Tue, 19 Jul 2022 08:12:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51840) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDm4L-0004v2-6D for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:17 -0400 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]:35577) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oDm4J-0001lL-M2 for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:16 -0400 Received: by mail-wr1-x42a.google.com with SMTP id b26so21314502wrc.2 for ; Tue, 19 Jul 2022 05:11:15 -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=VP1puZ7Kqisr5dvxsNpLvw7ulkMOEZ3tezwiO4vRy/Q=; b=CV9iQqsLtK0pdtRCCunDRofgTsU9rxGpsO0gM7uef8FDNKH8Wh3uIHLzeY+m3fKjRM jiQeg3RhAk5vl5NoRDyrJFaTzfa6pAavti6qNf4fZa1kPdH2p2KApqKwBFr2Og9FonE1 jXUhSbi3mUF3qMmas5w3Yv0Z/GN8D6Sg7bW6fWIN/rrWkhKyRmR/1hm8y0VVQ0o1xTXu ciCf1+d5UtMVlcOZwNXE2zm0caUNaFH8Dq0l8pZLy+N4/wcKjZwSeQo/KF0ylEVpK9Vc RjvzyoRmFwa+PZKe7/l+P9YhRbcLe1iWrD6i7nRlGWrddEKkMLhGE8X0qs0myCDOCwNl D2SA== 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=VP1puZ7Kqisr5dvxsNpLvw7ulkMOEZ3tezwiO4vRy/Q=; b=wQVq+nVL2uUd30QmRq3iiBq8FnyafyxojnNsiLWV4iLAmZSsO+ZFJ59bzZSrrTxzgS S7yGayoNRoHstmpsg76eVG94VcO8sx91VeD3q/s46xK40IbKqwCpMVcDqvI18kHbMVDi EihnKPpqL836WcxrMNM7d0EoaLJI0VvzXgE03kmKiXPVQcpld6LMnZuCuOV/7hXGRqsQ jT0kinGbvUOx776wc/wca+piuY/YxgPO71VKyjMYYexZnicscw3qL724TaCKjnyGMgdd 0S36yLg+wuedzQ7puYrbrufU/47V0Bm6ep0OGPbbRvtMaXbgMcLWJ9m5YOJQ7Hg6hz+e C2vQ== X-Gm-Message-State: AJIora+dLOdyJK6Xkq43st2KO0utkAbl537omPyF6US0OnbfKg8vOh/u 4DsKsQ4Ec/RuuyX4+WRZEGo/Gv4DhLEAHQ== X-Received: by 2002:adf:f306:0:b0:21e:4377:f956 with SMTP id i6-20020adff306000000b0021e4377f956mr779932wro.530.1658232674278; Tue, 19 Jul 2022 05:11:14 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id m21-20020a05600c4f5500b0039c5ab7167dsm22111287wmq.48.2022.07.19.05.11.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jul 2022 05:11:13 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 1/4] semihosting: Don't return negative values on qemu_semihosting_console_write() failure Date: Tue, 19 Jul 2022 13:11:07 +0100 Message-Id: <20220719121110.225657-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220719121110.225657-1-peter.maydell@linaro.org> References: <20220719121110.225657-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42a; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42a.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" The documentation comment for qemu_semihosting_console_write() says * Returns: number of bytes written -- this should only ever be short * on some sort of i/o error. and the callsites rely on this. However, the implementation code path which sends console output to a chardev doesn't honour this, and will return negative values on error. Bring it into line with the other implementation codepaths and the documentation, so that it returns 0 on error. Spotted by Coverity, because console_write() passes the return value to unlock_user(), which doesn't accept a negative length. Resolves: Coverity CID 1490288 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- console_write() doesn't need to pass the length to unlock_user() at all, as it happens -- see the next patch. --- semihosting/console.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/semihosting/console.c b/semihosting/console.c index 5b1ec0a1c39..0f976fe8cb1 100644 --- a/semihosting/console.c +++ b/semihosting/console.c @@ -111,7 +111,8 @@ int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) int qemu_semihosting_console_write(void *buf, int len) { if (console.chr) { - return qemu_chr_write_all(console.chr, (uint8_t *)buf, len); + int r = qemu_chr_write_all(console.chr, (uint8_t *)buf, len); + return r < 0 ? 0 : r; } else { return fwrite(buf, 1, len, stderr); } From patchwork Tue Jul 19 12:11:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 591655 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp3900485max; Tue, 19 Jul 2022 05:14:34 -0700 (PDT) X-Google-Smtp-Source: AGRyM1spaBOJDyuXD2cuXA8eSVXjuPO/b63UP2SPLYBjvnFnmiGeAHY1hfIKpm29hPQ99f5jbFfg X-Received: by 2002:a37:b8c4:0:b0:6b5:79e4:36f0 with SMTP id i187-20020a37b8c4000000b006b579e436f0mr19952321qkf.126.1658232874398; Tue, 19 Jul 2022 05:14:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658232874; cv=none; d=google.com; s=arc-20160816; b=K5gXL6n+AAUeinXriwPmJdOwdmB4SXT5+4xIAq3brk76HU84Mvi0KNdLJV4Z1ryECp aGuLffem0sSRm0yzau+QS2/nui2haTDIHi8RKGMixvpFiqVeVz06Y/8MM2kCMPBqQps7 QThzQ0kRDO/D8VXdZER5q1vuO3qf4p8EQcI/r+ZtnFc/i5Ashl2s4b+hxs+VtS+zV1ZD 6wRWJGK1OatM05Jn6BOoNS/Rbcb35+QRyBaM0wJ+C8NBYikho1dESorVDn81/OY7uL/H AdvkybrUJYbFCizpQ9sCcw+kZtuXZOFt1n4JS+1cU49qNJTmmQG45Th75cZN0xMkDQ8Z Vh4w== 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=9cayaFc8tQ7+xrReAxHB9jz1B9jTb17uFGh0FvMC508=; b=SXXWLznPHpsKSW704HvekIR2Tsg7+A/kyzGaOcKnVqRpvMhY4CiZzMP+AcOtgybMe6 Dx+zlcG1gq90AimSWNhUxTaql1sswGQ/ePx1/xEw5RDC99NLQALypzUasMnkFhEX1/kw l3+Wzm31BtVwnZKvH0jidZNfG2p7baBE9l69+zmYlysQEF1RwzritVSy4ReIiBq4Zwye rzmTLjKoMEx4eAwiDgmXEBHG4mEbXKx2Dpzgqqv3v5p9CpplbJCYlcBUGtrNhnCCqkhl kUvj+IkXEbNfCOwj3yGVzv5kYRv998/OTVzbNHaHpcEUM/k63LdukErMJB+92PVjnR6a TP/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=u5zDMWpl; 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 h7-20020ac85687000000b003066a4f3252si879584qta.759.2022.07.19.05.14.34 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jul 2022 05:14:34 -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=u5zDMWpl; 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]:54940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm7W-0000pa-1Z for patch@linaro.org; Tue, 19 Jul 2022 08:14:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51870) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDm4M-0004vA-Dg for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:18 -0400 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]:40885) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oDm4K-0001lQ-A8 for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:18 -0400 Received: by mail-wr1-x430.google.com with SMTP id z12so21306405wrq.7 for ; Tue, 19 Jul 2022 05:11:15 -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=9cayaFc8tQ7+xrReAxHB9jz1B9jTb17uFGh0FvMC508=; b=u5zDMWpldDwcRecedV1pWu+GkqLvWRSUaiWjV+TNkbQ1NLGBIbBHH4mq/42KWZNeB9 neRKyclFbO4w94E9AwlgCJgjJrNTxiZgLgwQe+JS7FuSxdDxGOah5/rV78ag+KU2dpvT jrfaO2DWd/HoheVmGsbmKhvBun5L80PnsEV2nDTa5U8Zil8KUKhO3rKVlRe5xtQx0ljc /nSMdAgG9fo0E0BpJf3qKmSr50CmeekvSat8f7W0qIQQo2EOMOX1l0Zy3ICTRfovhNR+ kj47YowsZdiKwWb9u8MmMpIDCuXD1ALoppCf0Cx8JfPP5zU3F/QqWKOu7UZnCxL5VzrQ cWyw== 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=9cayaFc8tQ7+xrReAxHB9jz1B9jTb17uFGh0FvMC508=; b=ljY5GHizSwYXRJAqMVPwKs7teCDfzgU8TLdtQu7Ut8WNvB7F6Qk/L33hHJTQhIrgCB Ca7MFiAmAwW19twtI/7ArJ/pnGhJwwtvuQQrvISuTPbTs82GGseTy2SRqqObGeoI/shv 6m3SIwH1spMfkXRjPmS/mLeojYnvPKkWnVuArSvNzp573bAG5JInRyMptZnf+JiamyIr vy00yn57iWe2nKAAf9Fsd7bVZuAp4/oVnFzhnx6Xq5leENjTvdUHKWZwc0Hsd5zw2oTq LZu30jrMtQFe3ib6djjn1lV8oLLpijHXX32ib0Ypx7MgnNODAcnKk3Zzp1kDlNTHYxi0 AoFg== X-Gm-Message-State: AJIora+InXpz4D81OcF8jpYQnZwMAow4qypgIPUDwtJkMM6/uDOj75Yk 8gl/dvnQcR+XTdo8LRxPxKk0HmCLOiOulA== X-Received: by 2002:a5d:6f19:0:b0:21e:2dab:83b with SMTP id ay25-20020a5d6f19000000b0021e2dab083bmr3279915wrb.139.1658232675022; Tue, 19 Jul 2022 05:11:15 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id m21-20020a05600c4f5500b0039c5ab7167dsm22111287wmq.48.2022.07.19.05.11.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jul 2022 05:11:14 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 2/4] semihosting: Don't copy buffer after console_write() Date: Tue, 19 Jul 2022 13:11:08 +0100 Message-Id: <20220719121110.225657-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220719121110.225657-1-peter.maydell@linaro.org> References: <20220719121110.225657-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::430; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x430.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" The console_write() semihosting function outputs guest data from a buffer; it doesn't update that buffer. It therefore doesn't need to pass a length value to unlock_user(), but can pass 0, meaning "do not copy any data back to the guest memory". Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- semihosting/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c index 4847f66c023..508a0ad88c6 100644 --- a/semihosting/syscalls.c +++ b/semihosting/syscalls.c @@ -627,7 +627,7 @@ static void console_write(CPUState *cs, gdb_syscall_complete_cb complete, } ret = qemu_semihosting_console_write(ptr, len); complete(cs, ret ? ret : -1, ret ? 0 : EIO); - unlock_user(ptr, buf, ret); + unlock_user(ptr, buf, 0); } static void console_fstat(CPUState *cs, gdb_syscall_complete_cb complete, From patchwork Tue Jul 19 12:11:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 591652 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp3898702max; Tue, 19 Jul 2022 05:12:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sfI2QpuCPr1FTP6cyOXml4vSLTqw67UVIqCwXo1NQlyjXbuCDh5WY2cPJOmBRKSKtR4Ufd X-Received: by 2002:a05:622a:16:b0:31e:f25a:3ef5 with SMTP id x22-20020a05622a001600b0031ef25a3ef5mr5864563qtw.191.1658232744419; Tue, 19 Jul 2022 05:12:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658232744; cv=none; d=google.com; s=arc-20160816; b=d6DnmkouUpNEEADXnyYYRSVaj45YKoQuTnbx34ElQim+fhu5kGSSxNlbjfcNR41S2C qs25hE9DOxwgq2Jg4QD2v6d8oI2nlIr6z1lBXYC9wH7N3R/Evc7JgSXGLaSbNTKHi6wz Ht/l9PQswQuYip6nf/UX4FxaDanf2HpXbA8+Ft/DfG4Naoz9d1DcZlg3OUxBjgnuInHH EEyIMwUssm1H0XW2yFOzP2NreHhq3rBT/AfCKL36TfE1DsczKBI/6d9PtV7e7v6fG3N4 YkiN8POikRrgBBQf6KDol2nBaQtmeT9PRpb3lKSQ1cy/giZe5e2JVZCrsIti3cdIacbs +IJg== 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=DiqGOB6lF2zKW4pGoquTd/9NIZHL+wdvtiLXsg7395o=; b=ORXMvwBafwYGU0QPAKH1xnT+3OwnS6bJSrR7xIn48SITIISLOLwaM2VPHYzLNuUUAZ 2YFa2CGTqb3vdzZkSgot13+Mr4Vm+B9PHhPULUCIIM/ED4Xr+Zd6qWhEqXUMJ03qldru I7xlWxwoX19AhVnLlB9lxe27kuRIuMtta2uosF1dbEXtlc7/HdNP6eZs/C7a5l+MCt3X /0KiJKhMsTzl/BIu0lc2TDtAR3yoPKWtYdxH/CTQApyfjfWTHDiub6PnBRIcD7aWVIXI 1PEMagjZboI3NKMguBw98+R8PI7RxxkvAYiO4kYz8p+KpChgYM6vPFO85gGg7JPQP0U8 BdbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mijQYTc8; 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 m5-20020a05620a24c500b006b5e4954e4fsi112265qkn.33.2022.07.19.05.12.24 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jul 2022 05:12: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=mijQYTc8; 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]:48662 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5Q-0004xP-1q for patch@linaro.org; Tue, 19 Jul 2022 08:12:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51876) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDm4M-0004vJ-UC for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:18 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]:43657) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oDm4L-0001lY-9h for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:18 -0400 Received: by mail-wm1-x334.google.com with SMTP id j29-20020a05600c1c1d00b003a2fdafdefbso9045104wms.2 for ; Tue, 19 Jul 2022 05:11:16 -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=DiqGOB6lF2zKW4pGoquTd/9NIZHL+wdvtiLXsg7395o=; b=mijQYTc8UzHhSn7IsCAn9Y8Z8Q3RFZrRUyrsI9DgpW5vSVji+OldmMN552K76n8fAY qDGy7/VlZkQkwyO3DdnG7Z2nljfAj9FOiKXqU7tSoFNBYLeOs2wgkUdhJQsQa7uvv9Ml m+69wv+UVg/+sIxNhrsm4l4XF11eKsPb+keRQIDgjBzrJdzzY/WxfEj3wi+ANHwTd4+b mvVx16tps7VkOZW36xQwFC5JKNSzEgRtrLE/78CoUmCt50P8S+ZiVZmvBFCCHxOaiIeM MygEVLeNR+nQCD+k57Flq39r+xCVlN4Ftx0242UUcS+mCUBScKAEa42k2S3A0x2WoYnZ XLeg== 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=DiqGOB6lF2zKW4pGoquTd/9NIZHL+wdvtiLXsg7395o=; b=OUkAGnVBtc9eNJG4ERBzCdg/tFVyipKQSoutbAZTLGrjdDgKD6WpUfgJ2/8R45pTNs 9Bd6HIrJraM6kneAF5fuqSmQvE0I3rbVLvQWuGUzJblxL801N0aQ1scHUB7dqERCm312 K1V21FkXOsDBnx4LjG2mWoJHsw+Y/9KxnI6YM1grznfgC2GH/TuziYAPdRCEmRpY9YcU zaVqXcaKKdb6YMrqmzwxRsHAYwhEozx46ycQhIzpNjzPCxOH3yxuudIiXMdK+duqL0uQ ga+oEdjjHeNMisEzoXuoIb+0SBu8lg5GkuItXUZA/rTYOiHfL2MtwxVbwJpqoN0AuDFK gBFg== X-Gm-Message-State: AJIora93zIRycX2c2Miw3vxW62zm/P78amMYau3tEAIXyTJYfJuJwfMe Mr1MVmcs+1HdJpg0ngJ40slUCQ3N8rT0xQ== X-Received: by 2002:a05:600c:1f0a:b0:3a3:15a8:a8e1 with SMTP id bd10-20020a05600c1f0a00b003a315a8a8e1mr13013694wmb.167.1658232675977; Tue, 19 Jul 2022 05:11:15 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id m21-20020a05600c4f5500b0039c5ab7167dsm22111287wmq.48.2022.07.19.05.11.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jul 2022 05:11:15 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 3/4] semihosting: Check for errors on SET_ARG() Date: Tue, 19 Jul 2022 13:11:09 +0100 Message-Id: <20220719121110.225657-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220719121110.225657-1-peter.maydell@linaro.org> References: <20220719121110.225657-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x334.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" 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 --- semihosting/arm-compat-semi.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index 1a1e2a69605..d12288fc806 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; From patchwork Tue Jul 19 12:11:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 591654 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp3898718max; Tue, 19 Jul 2022 05:12:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sd4UsRXKoNGtcnjmvmm0hEVNrsyNnZIj/NeTT5c9UsxgnMoo6S0A2IF/tup6X7LaAjjCN8 X-Received: by 2002:a0c:8ec5:0:b0:473:f142:118b with SMTP id y5-20020a0c8ec5000000b00473f142118bmr2267956qvb.97.1658232745982; Tue, 19 Jul 2022 05:12:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658232745; cv=none; d=google.com; s=arc-20160816; b=FU2QVWX2EZzUQHOpxLekL5tZH/Wp/l8NlYWUlc+H4EioMKafMabR1ehnZQbZo2eCh4 6ndyz9bCiwo6KCbxETDKXM/YsannGTO7WCtUHSxzSIVz8t199D4ZkG8MqDU0zFfMS1Jr nsNI4dW3YHw6UQZcIVKiwJVRb5zT04HXGx3ISF2YP0BJqExrZaKpUu9ZrFnln34NL1IV lB+jcgIwSTV/qVwYLXTJu0OEJ9b4EURorhWwM1vbGddzx+I7VWvYnOqx3v4LVuJabY9C cNEzI3Y3Bbs+2hJZP2wB+4ocADnMDX5XswCzXavywjnRoEzpRJRe+GkMPQ24YO/nRSgN O/wQ== 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=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=XW4HjCCyTyOJE8NekDtDl30I4ABgvUbL4gZAGlDl8oVYM2qIFu+ivA5KDQiweqKKrW rbAY09TsSGTKb1R9OT+xr6Jrr3rjQB3WX9mJjjjXI210AloJBGMEn2AnjSAGksvKRLri 3U9a/okNAuUd1YlUunTxe677N2OOepFeE5pPQoShVH61hP9gSFAbbVZbe7X5qDTypfIa J4sbqKd3NVFZgvPjvADVneu78b9m+RM7SEHkxI3Wu5JG0f5rl6meCQuitjie+vM+6C6Q KSSZtpw0rzpZ8UCtusk5Lb2Gp18W1VinMqGHUvq30Y0+PpZbhgpFE7rh8TdaWX4BBtKA NM0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pjRScg81; 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 do11-20020a05620a2b0b00b006b5e8ab439bsi2564622qkb.726.2022.07.19.05.12.25 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jul 2022 05:12:25 -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=pjRScg81; 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]:48678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5R-0004yJ-Iw for patch@linaro.org; Tue, 19 Jul 2022 08:12:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51882) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDm4N-0004vw-P9 for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:19 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:44782) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oDm4M-0001lg-0N for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:19 -0400 Received: by mail-wr1-x429.google.com with SMTP id bk26so21274754wrb.11 for ; Tue, 19 Jul 2022 05:11:17 -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=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=pjRScg81sOpxeHDV2gj/WY7amhwmdAz5lOXZIUSO4maz9uNprEfztim7cIpDBO+kBx ndbF3vCip/DqGky9Ls/Vobba8pdIOr9BHpYTun66eDSVmAiVMUmGN+GLSUF1tjntdNfW NcbOYWCbdiZ/Ye9SKHzI4ZuKxbCn3mRBqI3IyqD+JGf2ECA1TpWRdssGh9hcsiwIKPX/ R3aAVmeejcuI128+ioJadeWfu3S2i77Dt3wkoA+jy9Dd0eK0Dy+j9CKAW38Jdj795w82 2NER3XIbAFA+2pvHpDH8pLQop7GgnaBObIF4v54HtC24qZLsBuyYG8IrIb61kN0HWti6 BqxA== 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=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=Vl3McLHG7sI6C2wg0z24csKPyKocx1u+dPrM6Pbp2glTq1+V+Nrqhv+xzu3jGIMMSo TSSh7gUs/XGvZpbmNwIgaoI2ckU0GAjOOzCpmNq752+vG98TSxcKjx2EW1Rlh5NRQGyN EtgeVdB5ZulWgMp9oSoIHuBUai6e5OqiB001aHW8vLr0UIoGJX1quqPoxhaQNJ7u+4Kr RlLA94xSPeNJtsFZ+2HugjfHhRsZG6PV3G5FuX+0QYZjwj9dqzICb56fyQh8Bzv8Ltq7 JjBpayw6t5SyV0mk4mHeFbMdjnURDVBny3jQWl5wSNWEH0r/UCdfMU2Q0FH1m25mPlbs 6Igw== X-Gm-Message-State: AJIora+LvVEWwzTveip/khqeaJxSMl2pCqmoLpJeFa3iUWaoB/rc4Wnl M62fgq17aOpJoUYqibDGaxBcXUVHVuFZzg== X-Received: by 2002:adf:cf0a:0:b0:21e:2970:bade with SMTP id o10-20020adfcf0a000000b0021e2970bademr4583652wrj.455.1658232676729; Tue, 19 Jul 2022 05:11:16 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id m21-20020a05600c4f5500b0039c5ab7167dsm22111287wmq.48.2022.07.19.05.11.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jul 2022 05:11:16 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 4/4] semihosting: Fix handling of buffer in TARGET_SYS_TMPNAM Date: Tue, 19 Jul 2022 13:11:10 +0100 Message-Id: <20220719121110.225657-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220719121110.225657-1-peter.maydell@linaro.org> References: <20220719121110.225657-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x429.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" The TARGET_SYS_TMPNAM implementation has two bugs spotted by Coverity: * confusion about whether 'len' has the length of the string including or excluding the terminating NUL means we lock_user() len bytes of memory but memcpy() len + 1 bytes * In the error-exit cases we forget to free() the buffer that asprintf() returned to us Resolves: Coverity CID 1490285, 1490289 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- semihosting/arm-compat-semi.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index d12288fc806..e741674238f 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -504,16 +504,25 @@ void do_common_semihosting(CPUState *cs) GET_ARG(1); GET_ARG(2); len = asprintf(&s, "/tmp/qemu-%x%02x", getpid(), (int)arg1 & 0xff); + if (len < 0) { + common_semi_set_ret(cs, -1); + break; + } + + /* Allow for trailing NUL */ + len++; /* Make sure there's enough space in the buffer */ - if (len < 0 || len >= arg2) { + if (len > arg2) { + free(s); common_semi_set_ret(cs, -1); break; } p = lock_user(VERIFY_WRITE, arg0, len, 0); if (!p) { + free(s); goto do_fault; } - memcpy(p, s, len + 1); + memcpy(p, s, len); unlock_user(p, arg0, len); free(s); common_semi_set_ret(cs, 0);