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: 1657953 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=CV9iQqsL; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LnHks49Gfz9sG1 for ; Tue, 19 Jul 2022 22:12:05 +1000 (AEST) Received: from localhost ([::1]:48612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm55-0004vf-E8 for incoming@patchwork.ozlabs.org; Tue, 19 Jul 2022 08:12:03 -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-Google-Smtp-Source: AGRyM1vwpew5RXH8GO/a14EwGIcBGUdWhSeqAwg3e/T6SHsNP2hKl8PqkuYlDtk8o8CmaUCMlkcQqQ== 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+incoming=patchwork.ozlabs.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: 1657956 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=u5zDMWpl; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LnHnZ1vlKz9sG1 for ; Tue, 19 Jul 2022 22:14:25 +1000 (AEST) Received: from localhost ([::1]:54940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm7L-0000pa-3N for incoming@patchwork.ozlabs.org; Tue, 19 Jul 2022 08:14:23 -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-Google-Smtp-Source: AGRyM1uMAAcVX40uAZuarENqR+rcgSOnO1Mxi34WMFeUCyweZVBbDUkpUJG7CaQ/mM5GPkRtk45zAQ== 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+incoming=patchwork.ozlabs.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: 1657954 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=mijQYTc8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LnHkv0074z9sG1 for ; Tue, 19 Jul 2022 22:12:06 +1000 (AEST) Received: from localhost ([::1]:48662 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm56-0004xP-Fx for incoming@patchwork.ozlabs.org; Tue, 19 Jul 2022 08:12:04 -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-Google-Smtp-Source: AGRyM1s/QaR70aO8sESdkrIHdCwdZWgysJjpb2j8wMHeVFFDEd5xWXPBluemHkcY+ovelBBsT+5n3A== 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+incoming=patchwork.ozlabs.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: 1657955 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=pjRScg81; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LnHkw0kQmz9sG1 for ; Tue, 19 Jul 2022 22:12:07 +1000 (AEST) Received: from localhost ([::1]:48678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm57-0004yJ-Ly for incoming@patchwork.ozlabs.org; Tue, 19 Jul 2022 08:12:05 -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-Google-Smtp-Source: AGRyM1vHpWIFmryRkJtg67b+268GgOT2DO3XId+d2McqGh6y7gjWBQUiMMgXq+zbbXJ6qpFRiFNXdg== 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+incoming=patchwork.ozlabs.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);