From nobody Tue Apr 30 02:08:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1658232775; cv=none; d=zohomail.com; s=zohoarc; b=GeOPJ/rToErzSd3CYoWZ+bvHxZwMuEsVI1mpxbI+jDfUgXW9dKd93le7MlOEB9ooAvpLkPBIMAb7/O+Ru+S95Q4QHbiJ4ybAw5Dum9fu7ZHHjFpUGRBbWfQTE3k2u6C5XXX3IsMnHV2IxTtOWckrEEzssA4SgxfR3Rmide7Uk1Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658232775; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=VP1puZ7Kqisr5dvxsNpLvw7ulkMOEZ3tezwiO4vRy/Q=; b=JsuM0Gn+hkcSLr956Jbwe7d3MTf/m7HZ25omM9EZfS7ypxMWV9iVyUPJqu2sxrImzyZkGr06zjGwGF0TMi45BqXhp97afPGsgO2LHKh15xdOvYAWB59MiFHdX0t3si/UwWe4Sx1pq0qVSetuahDy8jTKTPCDX+ttSCaCkHTn9fA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658232775330306.166234819563; Tue, 19 Jul 2022 05:12:55 -0700 (PDT) Received: from localhost ([::1]:51550 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5t-0006zw-7R for importer@patchew.org; Tue, 19 Jul 2022 08:12:53 -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) 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) 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) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1658232777074100003 Content-Type: text/plain; charset="utf-8" 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 *b= uf, 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 =3D qemu_chr_write_all(console.chr, (uint8_t *)buf, len); + return r < 0 ? 0 : r; } else { return fwrite(buf, 1, len, stderr); } --=20 2.25.1 From nobody Tue Apr 30 02:08:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1658232890; cv=none; d=zohomail.com; s=zohoarc; b=j2Jyg2fp2HomhyW7xuQ1JI+amSqQO5Fnbky6McYQ3RUTyYpexalLJMY1r4MNLWrAHy749slHqsjs1pj49BsUwHccEA6QUkqSgvcOGHKzoK0IiQONbn9Dw0ZRMnqKPayIufewy+Qa5NW+VtecAPKU/YAAHxRSqCshXsZFou80diI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658232890; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9cayaFc8tQ7+xrReAxHB9jz1B9jTb17uFGh0FvMC508=; b=EZhiOu29+WtsQaZv8lIcxi58WdoNBSMY1AEHOiYGU8AMnrZ+itH1EVVQBER7PajuvaX9t11sspStULvPT2kYx4EJOqMKpL2fJyBkwDOhkuJAVuBhSSARxWTaKsTb4F18u0WQgYZb6Dfr3UWDlNT3zgNTvUP1Ge6VVJEc0l43iUQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658232890584813.086560726664; Tue, 19 Jul 2022 05:14:50 -0700 (PDT) Received: from localhost ([::1]:56686 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm7l-0001zU-KX for importer@patchew.org; Tue, 19 Jul 2022 08:14:49 -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) 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) 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) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1658232892524100001 Content-Type: text/plain; charset="utf-8" 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_com= plete_cb complete, } ret =3D qemu_semihosting_console_write(ptr, len); complete(cs, ret ? ret : -1, ret ? 0 : EIO); - unlock_user(ptr, buf, ret); + unlock_user(ptr, buf, 0); } =20 static void console_fstat(CPUState *cs, gdb_syscall_complete_cb complete, --=20 2.25.1 From nobody Tue Apr 30 02:08:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1658232774; cv=none; d=zohomail.com; s=zohoarc; b=XltrNaIt1oPYYoRKaTex8p8qZBLXVx3hM56RlvbY7A12DhO06ppQYUaIeJ7Gvpd1W9mS5sdittjJS2S4eXCGIVue1YqeGy7Jjgy25Jvfl4ea5mEGzIkQTlImhU9mwc6VDS+UgshHkGs9iYS0lLkkxrxZVKUmSZAqVepbQa1iY8Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658232774; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=DiqGOB6lF2zKW4pGoquTd/9NIZHL+wdvtiLXsg7395o=; b=cD9wOOO3RzOuKjuWos8J0/4HbClVYdlKhe5BtOH4BNUQx3OEXWhvCbIKTe8ZVaguNNBjhk3JfwmBCaC3P4BPhZxUI6ZLfe0IujjsKLqgH/slqjzQ2QkbooN+3t1rxWYS10sQqZdpOhUd0ktV+0A6UXYlE3YH4KY9zLN5PkuDR54= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658232774080858.684186538522; Tue, 19 Jul 2022 05:12:54 -0700 (PDT) Received: from localhost ([::1]:51674 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5s-000745-HN for importer@patchew.org; Tue, 19 Jul 2022 08:12:52 -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) 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) 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) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1658232775953100001 Content-Type: text/plain; charset="utf-8" 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. */ =20 #define GET_ARG(n) do { \ @@ -739,10 +745,14 @@ void do_common_semihosting(CPUState *cs) case TARGET_SYS_ELAPSED: elapsed =3D get_clock() - clock_start; if (sizeof(target_ulong) =3D=3D 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; --=20 2.25.1 From nobody Tue Apr 30 02:08:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1658232775; cv=none; d=zohomail.com; s=zohoarc; b=I7WyJOVXKmoftzgKEyVAdahREjCCATb8RN/UIjviiqE8o84FGow+lzVRXsAqv1ZME42NJbSG9fK5PzPbrHAtvVw8JWEQVSA61jX6AoA76EoTFCSBf6wDcNnyc0S+lpRHacYvd2O5+RhfR0ssCGbaG+kmSZEyylHtBZbyyQ5LKpA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658232775; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=n0jV5MQYEHlUpbzFeLvMwdocTJjQytrctrAlIyOcjAnfANM8/ckYokTL1O2azL/de9PqPz98xhiTMtCAScmswL+BbrStChNRUcYDNUybIz0ATx7sn861yXYEFvCBapgYi0fyF+zSXjn6TiAQ6VOgiQjNMVMbKoKvhbydJ9/L5O8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658232775947583.3978594665487; Tue, 19 Jul 2022 05:12:55 -0700 (PDT) Received: from localhost ([::1]:51622 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5u-00072N-UO for importer@patchew.org; Tue, 19 Jul 2022 08:12:54 -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) 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) 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) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1658232777079100004 Content-Type: text/plain; charset="utf-8" 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 =3D asprintf(&s, "/tmp/qemu-%x%02x", getpid(), (int)arg1 & 0xf= f); + 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 >=3D arg2) { + if (len > arg2) { + free(s); common_semi_set_ret(cs, -1); break; } p =3D 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); --=20 2.25.1